negotiated.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Copyright (c) 2019 Paul-Louis Ageneau
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #include "rtc/rtc.hpp"
  9. #include <atomic>
  10. #include <chrono>
  11. #include <iostream>
  12. #include <memory>
  13. #include <thread>
  14. using namespace rtc;
  15. using namespace std;
  16. void test_negotiated() {
  17. InitLogger(LogLevel::Debug);
  18. Configuration config1;
  19. config1.disableAutoNegotiation = true;
  20. PeerConnection pc1(config1);
  21. Configuration config2;
  22. config2.disableAutoNegotiation = true;
  23. PeerConnection pc2(config2);
  24. pc1.onLocalDescription([&pc2](Description sdp) {
  25. cout << "Description 1: " << sdp << endl;
  26. pc2.setRemoteDescription(string(sdp));
  27. pc2.setLocalDescription(); // Make the answer
  28. });
  29. pc1.onLocalCandidate([&pc2](Candidate candidate) {
  30. cout << "Candidate 1: " << candidate << endl;
  31. pc2.addRemoteCandidate(string(candidate));
  32. });
  33. pc2.onLocalDescription([&pc1](Description sdp) {
  34. cout << "Description 2: " << sdp << endl;
  35. pc1.setRemoteDescription(string(sdp));
  36. });
  37. pc2.onLocalCandidate([&pc1](Candidate candidate) {
  38. cout << "Candidate 2: " << candidate << endl;
  39. pc1.addRemoteCandidate(string(candidate));
  40. });
  41. // Try to open a negotiated channel
  42. DataChannelInit init;
  43. init.negotiated = true;
  44. init.id = 1;
  45. auto negotiated1 = pc1.createDataChannel("negotiated", init);
  46. auto negotiated2 = pc2.createDataChannel("negotiated", init);
  47. // Make the offer
  48. pc1.setLocalDescription();
  49. // Wait a bit
  50. int attempts = 10;
  51. while (!negotiated1->isOpen() || !negotiated2->isOpen() && attempts--)
  52. this_thread::sleep_for(1s);
  53. if (pc1.state() != PeerConnection::State::Connected ||
  54. pc2.state() != PeerConnection::State::Connected)
  55. throw runtime_error("PeerConnection is not connected");
  56. if (!negotiated1->isOpen() || !negotiated2->isOpen())
  57. throw runtime_error("Negotiated DataChannel is not open");
  58. std::atomic<bool> received = false;
  59. negotiated2->onMessage([&received](const variant<binary, string> &message) {
  60. if (holds_alternative<string>(message)) {
  61. cout << "Message 2: " << get<string>(message) << endl;
  62. received = true;
  63. }
  64. });
  65. negotiated1->send("Hello from negotiated channel");
  66. // Wait a bit
  67. attempts = 5;
  68. while (!received && attempts--)
  69. this_thread::sleep_for(1s);
  70. if (!received)
  71. throw runtime_error("Negotiated DataChannel failed");
  72. // Delay close of peer 2 to check closing works properly
  73. pc1.close();
  74. this_thread::sleep_for(1s);
  75. pc2.close();
  76. this_thread::sleep_for(1s);
  77. cout << "Success" << endl;
  78. }