main.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 <chrono>
  9. #include <iostream>
  10. #include <thread>
  11. #include <rtc/rtc.hpp>
  12. using namespace std;
  13. using namespace chrono_literals;
  14. void test_negotiated();
  15. void test_connectivity(bool signal_wrong_fingerprint);
  16. void test_turn_connectivity();
  17. void test_track();
  18. void test_capi_connectivity();
  19. void test_capi_track();
  20. void test_websocket();
  21. void test_websocketserver();
  22. void test_capi_websocketserver();
  23. size_t benchmark(chrono::milliseconds duration);
  24. void test_benchmark() {
  25. size_t goodput = benchmark(10s);
  26. if (goodput == 0)
  27. throw runtime_error("No data received");
  28. const size_t threshold = 1000; // 1 MB/s;
  29. if (goodput < threshold)
  30. throw runtime_error("Goodput is too low");
  31. }
  32. int main(int argc, char **argv) {
  33. // C++ API tests
  34. try {
  35. cout << endl << "*** Running WebRTC connectivity test..." << endl;
  36. test_connectivity(false);
  37. cout << "*** Finished WebRTC connectivity test" << endl;
  38. } catch (const exception &e) {
  39. cerr << "WebRTC connectivity test failed: " << e.what() << endl;
  40. return -1;
  41. }
  42. try {
  43. cout << endl << "*** Running WebRTC broken fingerprint test..." << endl;
  44. test_connectivity(true);
  45. cerr << "WebRTC connectivity test failed to detect broken fingerprint" << endl;
  46. return -1;
  47. } catch (const exception &) {
  48. }
  49. // TODO: Temporarily disabled as the Open Relay TURN server is unreliable
  50. /*
  51. try {
  52. cout << endl << "*** Running WebRTC TURN connectivity test..." << endl;
  53. test_turn_connectivity();
  54. cout << "*** Finished WebRTC TURN connectivity test" << endl;
  55. } catch (const exception &e) {
  56. cerr << "WebRTC TURN connectivity test failed: " << e.what() << endl;
  57. return -1;
  58. }
  59. */
  60. try {
  61. cout << endl << "*** Running WebRTC negotiated DataChannel test..." << endl;
  62. test_negotiated();
  63. cout << "*** Finished WebRTC negotiated DataChannel test" << endl;
  64. } catch (const exception &e) {
  65. cerr << "WebRTC negotiated DataChannel test failed: " << e.what() << endl;
  66. return -1;
  67. }
  68. #if RTC_ENABLE_MEDIA
  69. try {
  70. cout << endl << "*** Running WebRTC Track test..." << endl;
  71. test_track();
  72. cout << "*** Finished WebRTC Track test" << endl;
  73. } catch (const exception &e) {
  74. cerr << "WebRTC Track test failed: " << e.what() << endl;
  75. return -1;
  76. }
  77. #endif
  78. #if RTC_ENABLE_WEBSOCKET
  79. // TODO: Temporarily disabled as the echo service is unreliable
  80. /*
  81. try {
  82. cout << endl << "*** Running WebSocket test..." << endl;
  83. test_websocket();
  84. cout << "*** Finished WebSocket test" << endl;
  85. } catch (const exception &e) {
  86. cerr << "WebSocket test failed: " << e.what() << endl;
  87. return -1;
  88. }
  89. */
  90. try {
  91. cout << endl << "*** Running WebSocketServer test..." << endl;
  92. test_websocketserver();
  93. cout << "*** Finished WebSocketServer test" << endl;
  94. } catch (const exception &e) {
  95. cerr << "WebSocketServer test failed: " << e.what() << endl;
  96. return -1;
  97. }
  98. #endif
  99. try {
  100. // Every created object must have been destroyed, otherwise the wait will block
  101. cout << endl << "*** Running cleanup..." << endl;
  102. if(rtc::Cleanup().wait_for(10s) == future_status::timeout)
  103. throw std::runtime_error("Timeout");
  104. cout << "*** Finished cleanup..." << endl;
  105. } catch (const exception &e) {
  106. cerr << "Cleanup failed: " << e.what() << endl;
  107. return -1;
  108. }
  109. // C API tests
  110. try {
  111. cout << endl << "*** Running WebRTC C API connectivity test..." << endl;
  112. test_capi_connectivity();
  113. cout << "*** Finished WebRTC C API connectivity test" << endl;
  114. } catch (const exception &e) {
  115. cerr << "WebRTC C API connectivity test failed: " << e.what() << endl;
  116. return -1;
  117. }
  118. #if RTC_ENABLE_MEDIA
  119. try {
  120. cout << endl << "*** Running WebRTC C API track test..." << endl;
  121. test_capi_track();
  122. cout << "*** Finished WebRTC C API track test" << endl;
  123. } catch (const exception &e) {
  124. cerr << "WebRTC C API track test failed: " << e.what() << endl;
  125. return -1;
  126. }
  127. #endif
  128. #if RTC_ENABLE_WEBSOCKET
  129. try {
  130. cout << endl << "*** Running WebSocketServer C API test..." << endl;
  131. test_capi_websocketserver();
  132. cout << "*** Finished WebSocketServer C API test" << endl;
  133. } catch (const exception &e) {
  134. cerr << "WebSocketServer C API test failed: " << e.what() << endl;
  135. return -1;
  136. }
  137. #endif
  138. try {
  139. cout << endl << "*** Running C API cleanup..." << endl;
  140. rtcCleanup();
  141. cout << "*** Finished C API cleanup..." << endl;
  142. } catch (const exception &e) {
  143. cerr << "C API cleanup failed: " << e.what() << endl;
  144. return -1;
  145. }
  146. /*
  147. // Benchmark
  148. try {
  149. cout << endl << "*** Running WebRTC benchmark..." << endl;
  150. test_benchmark();
  151. cout << "*** Finished WebRTC benchmark" << endl;
  152. } catch (const exception &e) {
  153. cerr << "WebRTC benchmark failed: " << e.what() << endl;
  154. std::this_thread::sleep_for(2s);
  155. return -1;
  156. }
  157. */
  158. return 0;
  159. }