main.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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();
  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();
  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. // TODO: Temporarily disabled as the Open Relay TURN server is unreliable
  43. /*
  44. try {
  45. cout << endl << "*** Running WebRTC TURN connectivity test..." << endl;
  46. test_turn_connectivity();
  47. cout << "*** Finished WebRTC TURN connectivity test" << endl;
  48. } catch (const exception &e) {
  49. cerr << "WebRTC TURN connectivity test failed: " << e.what() << endl;
  50. return -1;
  51. }
  52. */
  53. try {
  54. cout << endl << "*** Running WebRTC negotiated DataChannel test..." << endl;
  55. test_negotiated();
  56. cout << "*** Finished WebRTC negotiated DataChannel test" << endl;
  57. } catch (const exception &e) {
  58. cerr << "WebRTC negotiated DataChannel test failed: " << e.what() << endl;
  59. return -1;
  60. }
  61. #if RTC_ENABLE_MEDIA
  62. try {
  63. cout << endl << "*** Running WebRTC Track test..." << endl;
  64. test_track();
  65. cout << "*** Finished WebRTC Track test" << endl;
  66. } catch (const exception &e) {
  67. cerr << "WebRTC Track test failed: " << e.what() << endl;
  68. return -1;
  69. }
  70. #endif
  71. #if RTC_ENABLE_WEBSOCKET
  72. // TODO: Temporarily disabled as the echo service is unreliable
  73. /*
  74. try {
  75. cout << endl << "*** Running WebSocket test..." << endl;
  76. test_websocket();
  77. cout << "*** Finished WebSocket test" << endl;
  78. } catch (const exception &e) {
  79. cerr << "WebSocket test failed: " << e.what() << endl;
  80. return -1;
  81. }
  82. */
  83. try {
  84. cout << endl << "*** Running WebSocketServer test..." << endl;
  85. test_websocketserver();
  86. cout << "*** Finished WebSocketServer test" << endl;
  87. } catch (const exception &e) {
  88. cerr << "WebSocketServer test failed: " << e.what() << endl;
  89. return -1;
  90. }
  91. #endif
  92. try {
  93. // Every created object must have been destroyed, otherwise the wait will block
  94. cout << endl << "*** Running cleanup..." << endl;
  95. if(rtc::Cleanup().wait_for(10s) == future_status::timeout)
  96. throw std::runtime_error("Timeout");
  97. cout << "*** Finished cleanup..." << endl;
  98. } catch (const exception &e) {
  99. cerr << "Cleanup failed: " << e.what() << endl;
  100. return -1;
  101. }
  102. // C API tests
  103. try {
  104. cout << endl << "*** Running WebRTC C API connectivity test..." << endl;
  105. test_capi_connectivity();
  106. cout << "*** Finished WebRTC C API connectivity test" << endl;
  107. } catch (const exception &e) {
  108. cerr << "WebRTC C API connectivity test failed: " << e.what() << endl;
  109. return -1;
  110. }
  111. #if RTC_ENABLE_MEDIA
  112. try {
  113. cout << endl << "*** Running WebRTC C API track test..." << endl;
  114. test_capi_track();
  115. cout << "*** Finished WebRTC C API track test" << endl;
  116. } catch (const exception &e) {
  117. cerr << "WebRTC C API track test failed: " << e.what() << endl;
  118. return -1;
  119. }
  120. #endif
  121. #if RTC_ENABLE_WEBSOCKET
  122. try {
  123. cout << endl << "*** Running WebSocketServer C API test..." << endl;
  124. test_capi_websocketserver();
  125. cout << "*** Finished WebSocketServer C API test" << endl;
  126. } catch (const exception &e) {
  127. cerr << "WebSocketServer C API test failed: " << e.what() << endl;
  128. return -1;
  129. }
  130. #endif
  131. try {
  132. cout << endl << "*** Running C API cleanup..." << endl;
  133. rtcCleanup();
  134. cout << "*** Finished C API cleanup..." << endl;
  135. } catch (const exception &e) {
  136. cerr << "C API cleanup failed: " << e.what() << endl;
  137. return -1;
  138. }
  139. /*
  140. // Benchmark
  141. try {
  142. cout << endl << "*** Running WebRTC benchmark..." << endl;
  143. test_benchmark();
  144. cout << "*** Finished WebRTC benchmark" << endl;
  145. } catch (const exception &e) {
  146. cerr << "WebRTC benchmark failed: " << e.what() << endl;
  147. std::this_thread::sleep_for(2s);
  148. return -1;
  149. }
  150. */
  151. return 0;
  152. }