123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- /**
- * Copyright (c) 2019 Paul-Louis Ageneau, Murat Dogan
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
- #include "rtc/rtc.hpp"
- #include <chrono>
- #include <iostream>
- #include <memory>
- using namespace rtc;
- using namespace std;
- template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
- int main(int argc, char **argv) {
- // InitLogger(LogLevel::Debug);
- Configuration config;
- // config.iceServers.emplace_back("stun.l.google.com:19302");
- // config.enableIceTcp = true;
- // TURN Server example
- // IceServer turnServer("TURN_SERVER_URL", "PORT_NO", "USERNAME", "PASSWORD",
- // IceServer::RelayType::TurnUdp);
- // config.iceServers.push_back(turnServer);
- auto pc = std::make_shared<PeerConnection>(config);
- pc->onLocalDescription([](const Description &sdp) {
- std::string s(sdp);
- std::replace(s.begin(), s.end(), '\n', static_cast<char>(94));
- cout << "Local Description (Paste this to other peer):" << endl << s << endl << endl;
- });
- pc->onLocalCandidate([](const Candidate &candidate) {
- cout << "Local Candidate (Paste this to other peer):" << endl << candidate << endl << endl;
- });
- pc->onStateChange(
- [](PeerConnection::State state) { cout << "[ State: " << state << " ]" << endl; });
- pc->onGatheringStateChange([](PeerConnection::GatheringState state) {
- cout << "[ Gathering State: " << state << " ]" << endl;
- });
- shared_ptr<DataChannel> dc = nullptr;
- pc->onDataChannel([&](shared_ptr<DataChannel> _dc) {
- cout << "[ Got a DataChannel with label: " << _dc->label() << " ]" << endl;
- dc = _dc;
- dc->onClosed([&]() { cout << "[ DataChannel closed: " << dc->label() << " ]" << endl; });
- dc->onMessage([](const variant<binary, string> &message) {
- if (holds_alternative<string>(message)) {
- cout << "[ Received: " << get<string>(message) << " ]" << endl;
- }
- });
- });
- bool exit = false;
- while (!exit) {
- cout << endl
- << endl
- << "*************************************************************************" << endl
- << "* 0: Exit /"
- << " 1: Enter Description /"
- << " 2: Enter Candidate /"
- << " 3: Send Message *" << endl
- << " [Command]: ";
- int command;
- std::string sdp, candidate, message;
- const char *a;
- std::unique_ptr<Candidate> candidatePtr;
- std::unique_ptr<Description> descPtr;
- cin >> command;
- switch (command) {
- case 0:
- exit = true;
- break;
- case 1:
- // Parse Description
- cout << "[SDP]: ";
- sdp = "";
- while (sdp.length() == 0)
- getline(cin, sdp);
- std::replace(sdp.begin(), sdp.end(), static_cast<char>(94), '\n');
- descPtr = std::make_unique<Description>(sdp, Description::Type::Offer,
- Description::Role::Passive);
- pc->setRemoteDescription(*descPtr);
- break;
- case 2:
- // Parse Candidate
- cout << "[Candidate]: ";
- candidate = "";
- while (candidate.length() == 0)
- getline(cin, candidate);
- candidatePtr = std::make_unique<Candidate>(candidate);
- pc->addRemoteCandidate(*candidatePtr);
- break;
- case 3:
- // Send Message
- if (!dc || !dc->isOpen()) {
- cout << "** Channel is not Open ** ";
- break;
- }
- cout << "[Message]: ";
- message = "";
- while (message.length() == 0)
- getline(cin, message);
- dc->send(message);
- break;
- default:
- cout << "** Invalid Command ** ";
- break;
- }
- }
- if (dc)
- dc->close();
- if (pc)
- pc->close();
- }
|