peerconnection.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /**
  2. * Copyright (c) 2019 Paul-Louis Ageneau
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "peerconnection.hpp"
  19. #include "certificate.hpp"
  20. #include "dtlstransport.hpp"
  21. #include "icetransport.hpp"
  22. #include "include.hpp"
  23. #include "sctptransport.hpp"
  24. #include <iostream>
  25. #include <thread>
  26. namespace rtc {
  27. using namespace std::placeholders;
  28. using std::shared_ptr;
  29. using std::weak_ptr;
  30. template <typename F, typename T, typename... Args> auto weak_bind(F &&f, T *t, Args &&... _args) {
  31. return [bound = std::bind(f, t, _args...), weak_this = t->weak_from_this()](auto &&... args) {
  32. if (auto shared_this = weak_this.lock())
  33. bound(args...);
  34. };
  35. }
  36. template <typename F, typename T, typename... Args>
  37. auto weak_bind_verifier(F &&f, T *t, Args &&... _args) {
  38. return [bound = std::bind(f, t, _args...), weak_this = t->weak_from_this()](auto &&... args) {
  39. if (auto shared_this = weak_this.lock())
  40. return bound(args...);
  41. else
  42. return false;
  43. };
  44. }
  45. PeerConnection::PeerConnection() : PeerConnection(Configuration()) {}
  46. PeerConnection::PeerConnection(const Configuration &config)
  47. : mConfig(config), mCertificate(make_certificate("libdatachannel")), mState(State::New) {}
  48. PeerConnection::~PeerConnection() { close(); }
  49. void PeerConnection::close() {
  50. closeDataChannels();
  51. closeTransports();
  52. }
  53. const Configuration *PeerConnection::config() const { return &mConfig; }
  54. PeerConnection::State PeerConnection::state() const { return mState; }
  55. PeerConnection::GatheringState PeerConnection::gatheringState() const { return mGatheringState; }
  56. std::optional<Description> PeerConnection::localDescription() const {
  57. std::lock_guard lock(mLocalDescriptionMutex);
  58. return mLocalDescription;
  59. }
  60. std::optional<Description> PeerConnection::remoteDescription() const {
  61. std::lock_guard lock(mRemoteDescriptionMutex);
  62. return mRemoteDescription;
  63. }
  64. void PeerConnection::setRemoteDescription(Description description) {
  65. description.hintType(localDescription() ? Description::Type::Answer : Description::Type::Offer);
  66. auto remoteCandidates = description.extractCandidates();
  67. std::lock_guard lock(mRemoteDescriptionMutex);
  68. mRemoteDescription.emplace(std::move(description));
  69. auto iceTransport = std::atomic_load(&mIceTransport);
  70. if (!iceTransport)
  71. iceTransport = initIceTransport(Description::Role::ActPass);
  72. iceTransport->setRemoteDescription(*mRemoteDescription);
  73. if (mRemoteDescription->type() == Description::Type::Offer) {
  74. // This is an offer and we are the answerer.
  75. processLocalDescription(iceTransport->getLocalDescription(Description::Type::Answer));
  76. iceTransport->gatherLocalCandidates();
  77. } else {
  78. // This is an answer and we are the offerer.
  79. auto sctpTransport = std::atomic_load(&mSctpTransport);
  80. if (!sctpTransport && iceTransport->role() == Description::Role::Active) {
  81. // Since we assumed passive role during DataChannel creation, we need to shift the
  82. // stream numbers by one to shift them from odd to even.
  83. std::unique_lock lock(mDataChannelsMutex); // we are going to swap the container
  84. decltype(mDataChannels) newDataChannels;
  85. auto it = mDataChannels.begin();
  86. while (it != mDataChannels.end()) {
  87. auto channel = it->second.lock();
  88. if (channel->stream() % 2 == 1)
  89. channel->mStream -= 1;
  90. newDataChannels.emplace(channel->stream(), channel);
  91. ++it;
  92. }
  93. std::swap(mDataChannels, newDataChannels);
  94. }
  95. }
  96. for (const auto &candidate : remoteCandidates)
  97. addRemoteCandidate(candidate);
  98. }
  99. void PeerConnection::addRemoteCandidate(Candidate candidate) {
  100. std::lock_guard lock(mRemoteDescriptionMutex);
  101. auto iceTransport = std::atomic_load(&mIceTransport);
  102. if (!mRemoteDescription || !iceTransport)
  103. throw std::logic_error("Remote candidate set without remote description");
  104. mRemoteDescription->addCandidate(candidate);
  105. if (candidate.resolve(Candidate::ResolveMode::Simple)) {
  106. iceTransport->addRemoteCandidate(candidate);
  107. } else {
  108. // OK, we might need a lookup, do it asynchronously
  109. weak_ptr<IceTransport> weakIceTransport{iceTransport};
  110. std::thread t([weakIceTransport, candidate]() mutable {
  111. if (candidate.resolve(Candidate::ResolveMode::Lookup))
  112. if (auto iceTransport = weakIceTransport.lock())
  113. iceTransport->addRemoteCandidate(candidate);
  114. });
  115. t.detach();
  116. }
  117. }
  118. std::optional<string> PeerConnection::localAddress() const {
  119. auto iceTransport = std::atomic_load(&mIceTransport);
  120. return iceTransport ? iceTransport->getLocalAddress() : nullopt;
  121. }
  122. std::optional<string> PeerConnection::remoteAddress() const {
  123. auto iceTransport = std::atomic_load(&mIceTransport);
  124. return iceTransport ? iceTransport->getRemoteAddress() : nullopt;
  125. }
  126. shared_ptr<DataChannel> PeerConnection::createDataChannel(const string &label,
  127. const string &protocol,
  128. const Reliability &reliability) {
  129. // RFC 5763: The answerer MUST use either a setup attribute value of setup:active or
  130. // setup:passive. [...] Thus, setup:active is RECOMMENDED.
  131. // See https://tools.ietf.org/html/rfc5763#section-5
  132. // Therefore, we assume passive role when we are the offerer.
  133. auto iceTransport = std::atomic_load(&mIceTransport);
  134. auto role = iceTransport ? iceTransport->role() : Description::Role::Passive;
  135. auto channel = emplaceDataChannel(role, label, protocol, reliability);
  136. if (!iceTransport) {
  137. // RFC 5763: The endpoint that is the offerer MUST use the setup attribute value of
  138. // setup:actpass.
  139. // See https://tools.ietf.org/html/rfc5763#section-5
  140. iceTransport = initIceTransport(Description::Role::ActPass);
  141. processLocalDescription(iceTransport->getLocalDescription(Description::Type::Offer));
  142. iceTransport->gatherLocalCandidates();
  143. } else {
  144. if (auto transport = std::atomic_load(&mSctpTransport))
  145. if (transport->state() == SctpTransport::State::Connected)
  146. channel->open(transport);
  147. }
  148. return channel;
  149. }
  150. void PeerConnection::onDataChannel(
  151. std::function<void(shared_ptr<DataChannel> dataChannel)> callback) {
  152. mDataChannelCallback = callback;
  153. }
  154. void PeerConnection::onLocalDescription(
  155. std::function<void(const Description &description)> callback) {
  156. mLocalDescriptionCallback = callback;
  157. }
  158. void PeerConnection::onLocalCandidate(std::function<void(const Candidate &candidate)> callback) {
  159. mLocalCandidateCallback = callback;
  160. }
  161. void PeerConnection::onStateChange(std::function<void(State state)> callback) {
  162. mStateChangeCallback = callback;
  163. }
  164. void PeerConnection::onGatheringStateChange(std::function<void(GatheringState state)> callback) {
  165. mGatheringStateChangeCallback = callback;
  166. }
  167. shared_ptr<IceTransport> PeerConnection::initIceTransport(Description::Role role) {
  168. try {
  169. if (auto transport = std::atomic_load(&mIceTransport))
  170. return transport;
  171. auto transport = std::make_shared<IceTransport>(
  172. mConfig, role, weak_bind(&PeerConnection::processLocalCandidate, this, _1),
  173. [this, weak_this = weak_from_this()](IceTransport::State state) {
  174. auto shared_this = weak_this.lock();
  175. if (!shared_this)
  176. return;
  177. switch (state) {
  178. case IceTransport::State::Connecting:
  179. changeState(State::Connecting);
  180. break;
  181. case IceTransport::State::Failed:
  182. changeState(State::Failed);
  183. break;
  184. case IceTransport::State::Connected:
  185. initDtlsTransport();
  186. break;
  187. case IceTransport::State::Disconnected:
  188. changeState(State::Disconnected);
  189. break;
  190. default:
  191. // Ignore
  192. break;
  193. }
  194. },
  195. [this, weak_this = weak_from_this()](IceTransport::GatheringState state) {
  196. auto shared_this = weak_this.lock();
  197. if (!shared_this)
  198. return;
  199. switch (state) {
  200. case IceTransport::GatheringState::InProgress:
  201. changeGatheringState(GatheringState::InProgress);
  202. break;
  203. case IceTransport::GatheringState::Complete:
  204. endLocalCandidates();
  205. changeGatheringState(GatheringState::Complete);
  206. break;
  207. default:
  208. // Ignore
  209. break;
  210. }
  211. });
  212. std::atomic_store(&mIceTransport, transport);
  213. if (mState == State::Closed) {
  214. mIceTransport.reset();
  215. transport->stop();
  216. throw std::runtime_error("Connection is closed");
  217. }
  218. return transport;
  219. } catch (const std::exception &e) {
  220. PLOG_ERROR << e.what();
  221. changeState(State::Failed);
  222. throw std::runtime_error("ICE transport initialization failed");
  223. }
  224. }
  225. shared_ptr<DtlsTransport> PeerConnection::initDtlsTransport() {
  226. try {
  227. if (auto transport = std::atomic_load(&mDtlsTransport))
  228. return transport;
  229. auto lower = std::atomic_load(&mIceTransport);
  230. auto transport = std::make_shared<DtlsTransport>(
  231. lower, mCertificate, weak_bind_verifier(&PeerConnection::checkFingerprint, this, _1),
  232. [this, weak_this = weak_from_this()](DtlsTransport::State state) {
  233. auto shared_this = weak_this.lock();
  234. if (!shared_this)
  235. return;
  236. switch (state) {
  237. case DtlsTransport::State::Connected:
  238. initSctpTransport();
  239. break;
  240. case DtlsTransport::State::Failed:
  241. changeState(State::Failed);
  242. break;
  243. case DtlsTransport::State::Disconnected:
  244. changeState(State::Disconnected);
  245. break;
  246. default:
  247. // Ignore
  248. break;
  249. }
  250. });
  251. std::atomic_store(&mDtlsTransport, transport);
  252. if (mState == State::Closed) {
  253. mDtlsTransport.reset();
  254. transport->stop();
  255. throw std::runtime_error("Connection is closed");
  256. }
  257. return transport;
  258. } catch (const std::exception &e) {
  259. PLOG_ERROR << e.what();
  260. changeState(State::Failed);
  261. throw std::runtime_error("DTLS transport initialization failed");
  262. }
  263. }
  264. shared_ptr<SctpTransport> PeerConnection::initSctpTransport() {
  265. try {
  266. if (auto transport = std::atomic_load(&mSctpTransport))
  267. return transport;
  268. uint16_t sctpPort = remoteDescription()->sctpPort().value_or(DEFAULT_SCTP_PORT);
  269. auto lower = std::atomic_load(&mDtlsTransport);
  270. auto transport = std::make_shared<SctpTransport>(
  271. lower, sctpPort, weak_bind(&PeerConnection::forwardMessage, this, _1),
  272. weak_bind(&PeerConnection::forwardBufferedAmount, this, _1, _2),
  273. [this, weak_this = weak_from_this()](SctpTransport::State state) {
  274. auto shared_this = weak_this.lock();
  275. if (!shared_this)
  276. return;
  277. switch (state) {
  278. case SctpTransport::State::Connected:
  279. changeState(State::Connected);
  280. openDataChannels();
  281. break;
  282. case SctpTransport::State::Failed:
  283. remoteCloseDataChannels();
  284. changeState(State::Failed);
  285. break;
  286. case SctpTransport::State::Disconnected:
  287. remoteCloseDataChannels();
  288. changeState(State::Disconnected);
  289. break;
  290. default:
  291. // Ignore
  292. break;
  293. }
  294. });
  295. std::atomic_store(&mSctpTransport, transport);
  296. if (mState == State::Closed) {
  297. mSctpTransport.reset();
  298. transport->stop();
  299. throw std::runtime_error("Connection is closed");
  300. }
  301. return transport;
  302. } catch (const std::exception &e) {
  303. PLOG_ERROR << e.what();
  304. changeState(State::Failed);
  305. throw std::runtime_error("SCTP transport initialization failed");
  306. }
  307. }
  308. void PeerConnection::closeTransports() {
  309. // Change state to sink state Closed to block init methods
  310. changeState(State::Closed);
  311. // Reset callbacks now that state is changed
  312. resetCallbacks();
  313. // Pass the references to a thread, allowing to terminate a transport from its own thread
  314. auto sctp = std::atomic_exchange(&mSctpTransport, decltype(mSctpTransport)(nullptr));
  315. auto dtls = std::atomic_exchange(&mDtlsTransport, decltype(mDtlsTransport)(nullptr));
  316. auto ice = std::atomic_exchange(&mIceTransport, decltype(mIceTransport)(nullptr));
  317. if (sctp || dtls || ice) {
  318. std::thread t([sctp, dtls, ice]() mutable {
  319. if (sctp)
  320. sctp->stop();
  321. if (dtls)
  322. dtls->stop();
  323. if (ice)
  324. ice->stop();
  325. sctp.reset();
  326. dtls.reset();
  327. ice.reset();
  328. });
  329. t.detach();
  330. }
  331. }
  332. void PeerConnection::endLocalCandidates() {
  333. std::lock_guard lock(mLocalDescriptionMutex);
  334. if (mLocalDescription)
  335. mLocalDescription->endCandidates();
  336. }
  337. bool PeerConnection::checkFingerprint(const std::string &fingerprint) const {
  338. std::lock_guard lock(mRemoteDescriptionMutex);
  339. if (auto expectedFingerprint =
  340. mRemoteDescription ? mRemoteDescription->fingerprint() : nullopt) {
  341. return *expectedFingerprint == fingerprint;
  342. }
  343. return false;
  344. }
  345. void PeerConnection::forwardMessage(message_ptr message) {
  346. if (!message) {
  347. remoteCloseDataChannels();
  348. return;
  349. }
  350. auto channel = findDataChannel(message->stream);
  351. auto iceTransport = std::atomic_load(&mIceTransport);
  352. auto sctpTransport = std::atomic_load(&mSctpTransport);
  353. if (!iceTransport || !sctpTransport)
  354. return;
  355. if (!channel) {
  356. const byte dataChannelOpenMessage{0x03};
  357. unsigned int remoteParity = (iceTransport->role() == Description::Role::Active) ? 1 : 0;
  358. if (message->type == Message::Control && *message->data() == dataChannelOpenMessage &&
  359. message->stream % 2 == remoteParity) {
  360. channel =
  361. std::make_shared<DataChannel>(shared_from_this(), sctpTransport, message->stream);
  362. channel->onOpen(weak_bind(&PeerConnection::triggerDataChannel, this,
  363. weak_ptr<DataChannel>{channel}));
  364. mDataChannels.insert(std::make_pair(message->stream, channel));
  365. } else {
  366. // Invalid, close the DataChannel by resetting the stream
  367. sctpTransport->reset(message->stream);
  368. return;
  369. }
  370. }
  371. channel->incoming(message);
  372. }
  373. void PeerConnection::forwardBufferedAmount(uint16_t stream, size_t amount) {
  374. if (auto channel = findDataChannel(stream))
  375. channel->triggerBufferedAmount(amount);
  376. }
  377. shared_ptr<DataChannel> PeerConnection::emplaceDataChannel(Description::Role role,
  378. const string &label,
  379. const string &protocol,
  380. const Reliability &reliability) {
  381. // The active side must use streams with even identifiers, whereas the passive side must use
  382. // streams with odd identifiers.
  383. // See https://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-09#section-6
  384. std::unique_lock lock(mDataChannelsMutex); // we are going to emplace
  385. unsigned int stream = (role == Description::Role::Active) ? 0 : 1;
  386. while (mDataChannels.find(stream) != mDataChannels.end()) {
  387. stream += 2;
  388. if (stream >= 65535)
  389. throw std::runtime_error("Too many DataChannels");
  390. }
  391. auto channel =
  392. std::make_shared<DataChannel>(shared_from_this(), stream, label, protocol, reliability);
  393. mDataChannels.emplace(std::make_pair(stream, channel));
  394. return channel;
  395. }
  396. shared_ptr<DataChannel> PeerConnection::findDataChannel(uint16_t stream) {
  397. std::shared_lock lock(mDataChannelsMutex); // read-only
  398. if (auto it = mDataChannels.find(stream); it != mDataChannels.end())
  399. if (auto channel = it->second.lock())
  400. return channel;
  401. return nullptr;
  402. }
  403. void PeerConnection::iterateDataChannels(
  404. std::function<void(shared_ptr<DataChannel> channel)> func) {
  405. // Iterate
  406. {
  407. std::shared_lock lock(mDataChannelsMutex); // read-only
  408. auto it = mDataChannels.begin();
  409. while (it != mDataChannels.end()) {
  410. auto channel = it->second.lock();
  411. if (channel && !channel->isClosed())
  412. func(channel);
  413. ++it;
  414. }
  415. }
  416. // Cleanup
  417. {
  418. std::unique_lock lock(mDataChannelsMutex); // we are going to erase
  419. auto it = mDataChannels.begin();
  420. while (it != mDataChannels.end()) {
  421. if (!it->second.lock()) {
  422. it = mDataChannels.erase(it);
  423. continue;
  424. }
  425. ++it;
  426. }
  427. }
  428. }
  429. void PeerConnection::openDataChannels() {
  430. if (auto transport = std::atomic_load(&mSctpTransport))
  431. iterateDataChannels([&](shared_ptr<DataChannel> channel) { channel->open(transport); });
  432. }
  433. void PeerConnection::closeDataChannels() {
  434. iterateDataChannels([&](shared_ptr<DataChannel> channel) { channel->close(); });
  435. }
  436. void PeerConnection::remoteCloseDataChannels() {
  437. iterateDataChannels([&](shared_ptr<DataChannel> channel) { channel->remoteClose(); });
  438. }
  439. void PeerConnection::processLocalDescription(Description description) {
  440. std::optional<uint16_t> remoteSctpPort;
  441. if (auto remote = remoteDescription())
  442. remoteSctpPort = remote->sctpPort();
  443. std::lock_guard lock(mLocalDescriptionMutex);
  444. mLocalDescription.emplace(std::move(description));
  445. mLocalDescription->setFingerprint(mCertificate->fingerprint());
  446. mLocalDescription->setSctpPort(remoteSctpPort.value_or(DEFAULT_SCTP_PORT));
  447. mLocalDescription->setMaxMessageSize(LOCAL_MAX_MESSAGE_SIZE);
  448. mLocalDescriptionCallback(*mLocalDescription);
  449. }
  450. void PeerConnection::processLocalCandidate(Candidate candidate) {
  451. std::lock_guard lock(mLocalDescriptionMutex);
  452. if (!mLocalDescription)
  453. throw std::logic_error("Got a local candidate without local description");
  454. mLocalDescription->addCandidate(candidate);
  455. mLocalCandidateCallback(candidate);
  456. }
  457. void PeerConnection::triggerDataChannel(weak_ptr<DataChannel> weakDataChannel) {
  458. auto dataChannel = weakDataChannel.lock();
  459. if (!dataChannel)
  460. return;
  461. mDataChannelCallback(dataChannel);
  462. }
  463. bool PeerConnection::changeState(State state) {
  464. State current;
  465. do {
  466. current = mState.load();
  467. if (current == state)
  468. return true;
  469. if (current == State::Closed)
  470. return false;
  471. } while (!mState.compare_exchange_weak(current, state));
  472. mStateChangeCallback(state);
  473. return true;
  474. }
  475. bool PeerConnection::changeGatheringState(GatheringState state) {
  476. if (mGatheringState.exchange(state) != state)
  477. mGatheringStateChangeCallback(state);
  478. return true;
  479. }
  480. void PeerConnection::resetCallbacks() {
  481. // Unregister all callbacks
  482. mDataChannelCallback = nullptr;
  483. mLocalDescriptionCallback = nullptr;
  484. mLocalCandidateCallback = nullptr;
  485. mStateChangeCallback = nullptr;
  486. mGatheringStateChangeCallback = nullptr;
  487. }
  488. bool PeerConnection::getSelectedCandidatePair(CandidateInfo *local, CandidateInfo *remote) {
  489. #if not USE_JUICE
  490. auto iceTransport = std::atomic_load(&mIceTransport);
  491. return iceTransport->getSelectedCandidatePair(local, remote);
  492. #else
  493. PLOG_WARNING << "getSelectedCandidatePair is not implemented for libjuice";
  494. return false;
  495. #endif
  496. }
  497. void PeerConnection::clearStats(){
  498. auto sctpTransport = std::atomic_load(&mSctpTransport);
  499. if (sctpTransport)
  500. return sctpTransport->clearStats();
  501. }
  502. const size_t PeerConnection::bytesSent() {
  503. auto sctpTransport = std::atomic_load(&mSctpTransport);
  504. if (sctpTransport)
  505. return sctpTransport->bytesSent();
  506. return 0;
  507. }
  508. const size_t PeerConnection::bytesReceived() {
  509. auto sctpTransport = std::atomic_load(&mSctpTransport);
  510. if (sctpTransport)
  511. return sctpTransport->bytesReceived();
  512. return 0;
  513. }
  514. const std::chrono::milliseconds PeerConnection::rtt() {
  515. auto sctpTransport = std::atomic_load(&mSctpTransport);
  516. if (sctpTransport)
  517. return sctpTransport->rtt();
  518. return std::chrono::milliseconds(0);
  519. }
  520. } // namespace rtc
  521. std::ostream &operator<<(std::ostream &out, const rtc::PeerConnection::State &state) {
  522. using State = rtc::PeerConnection::State;
  523. std::string str;
  524. switch (state) {
  525. case State::New:
  526. str = "new";
  527. break;
  528. case State::Connecting:
  529. str = "connecting";
  530. break;
  531. case State::Connected:
  532. str = "connected";
  533. break;
  534. case State::Disconnected:
  535. str = "disconnected";
  536. break;
  537. case State::Failed:
  538. str = "failed";
  539. break;
  540. case State::Closed:
  541. str = "closed";
  542. break;
  543. default:
  544. str = "unknown";
  545. break;
  546. }
  547. return out << str;
  548. }
  549. std::ostream &operator<<(std::ostream &out, const rtc::PeerConnection::GatheringState &state) {
  550. using GatheringState = rtc::PeerConnection::GatheringState;
  551. std::string str;
  552. switch (state) {
  553. case GatheringState::New:
  554. str = "new";
  555. break;
  556. case GatheringState::InProgress:
  557. str = "in_progress";
  558. break;
  559. case GatheringState::Complete:
  560. str = "complete";
  561. break;
  562. default:
  563. str = "unknown";
  564. break;
  565. }
  566. return out << str;
  567. }