negotiated.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "rtc/rtc.hpp"
  19. #include <atomic>
  20. #include <chrono>
  21. #include <iostream>
  22. #include <memory>
  23. #include <thread>
  24. using namespace rtc;
  25. using namespace std;
  26. void test_negotiated() {
  27. InitLogger(LogLevel::Debug);
  28. Configuration config1;
  29. config1.disableAutoNegotiation = true;
  30. PeerConnection pc1(config1);
  31. Configuration config2;
  32. config2.disableAutoNegotiation = true;
  33. PeerConnection pc2(config2);
  34. pc1.onLocalDescription([&pc2](Description sdp) {
  35. cout << "Description 1: " << sdp << endl;
  36. pc2.setRemoteDescription(string(sdp));
  37. pc2.setLocalDescription(); // Make the answer
  38. });
  39. pc1.onLocalCandidate([&pc2](Candidate candidate) {
  40. cout << "Candidate 1: " << candidate << endl;
  41. pc2.addRemoteCandidate(string(candidate));
  42. });
  43. pc2.onLocalDescription([&pc1](Description sdp) {
  44. cout << "Description 2: " << sdp << endl;
  45. pc1.setRemoteDescription(string(sdp));
  46. });
  47. pc2.onLocalCandidate([&pc1](Candidate candidate) {
  48. cout << "Candidate 2: " << candidate << endl;
  49. pc1.addRemoteCandidate(string(candidate));
  50. });
  51. // Try to open a negotiated channel
  52. DataChannelInit init;
  53. init.negotiated = true;
  54. init.id = 1;
  55. auto negotiated1 = pc1.createDataChannel("negotiated", init);
  56. auto negotiated2 = pc2.createDataChannel("negotiated", init);
  57. // Make the offer
  58. pc1.setLocalDescription();
  59. // Wait a bit
  60. int attempts = 10;
  61. while (!negotiated1->isOpen() || !negotiated2->isOpen() && attempts--)
  62. this_thread::sleep_for(1s);
  63. if (pc1.state() != PeerConnection::State::Connected &&
  64. pc2.state() != PeerConnection::State::Connected)
  65. throw runtime_error("PeerConnection is not connected");
  66. if (!negotiated1->isOpen() || !negotiated2->isOpen())
  67. throw runtime_error("Negotiated DataChannel is not open");
  68. std::atomic<bool> received = false;
  69. negotiated2->onMessage([&received](const variant<binary, string> &message) {
  70. if (holds_alternative<string>(message)) {
  71. cout << "Message 2: " << get<string>(message) << endl;
  72. received = true;
  73. }
  74. });
  75. negotiated1->send("Hello from negotiated channel");
  76. // Wait a bit
  77. attempts = 5;
  78. while (!received && attempts--)
  79. this_thread::sleep_for(1s);
  80. if (!received)
  81. throw runtime_error("Negotiated DataChannel failed");
  82. // Delay close of peer 2 to check closing works properly
  83. pc1.close();
  84. this_thread::sleep_for(1s);
  85. pc2.close();
  86. this_thread::sleep_for(1s);
  87. cout << "Success" << endl;
  88. }