main.cpp 4.5 KB

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