negotiated.cpp 2.6 KB

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