capi_track.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * Copyright (c) 2020 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.h>
  19. #include <cstdio>
  20. #include <cstdlib>
  21. #include <cstring>
  22. #ifdef _WIN32
  23. #include <windows.h>
  24. static void sleep(unsigned int secs) { Sleep(secs * 1000); }
  25. #else
  26. #include <unistd.h> // for sleep
  27. #endif
  28. typedef struct {
  29. rtcState state;
  30. rtcGatheringState gatheringState;
  31. int pc;
  32. int tr;
  33. bool connected;
  34. } Peer;
  35. static Peer *peer1 = NULL;
  36. static Peer *peer2 = NULL;
  37. static const char *mediaDescription = "video 9 UDP/TLS/RTP/SAVPF\r\n"
  38. "a=mid:video\r\n";
  39. static void descriptionCallback(int pc, const char *sdp, const char *type, void *ptr) {
  40. Peer *peer = (Peer *)ptr;
  41. printf("Description %d:\n%s\n", peer == peer1 ? 1 : 2, sdp);
  42. Peer *other = peer == peer1 ? peer2 : peer1;
  43. rtcSetRemoteDescription(other->pc, sdp, type);
  44. }
  45. static void candidateCallback(int pc, const char *cand, const char *mid, void *ptr) {
  46. Peer *peer = (Peer *)ptr;
  47. printf("Candidate %d: %s\n", peer == peer1 ? 1 : 2, cand);
  48. Peer *other = peer == peer1 ? peer2 : peer1;
  49. rtcAddRemoteCandidate(other->pc, cand, mid);
  50. }
  51. static void stateChangeCallback(int pc, rtcState state, void *ptr) {
  52. Peer *peer = (Peer *)ptr;
  53. peer->state = state;
  54. printf("State %d: %d\n", peer == peer1 ? 1 : 2, (int)state);
  55. }
  56. static void gatheringStateCallback(int pc, rtcGatheringState state, void *ptr) {
  57. Peer *peer = (Peer *)ptr;
  58. peer->gatheringState = state;
  59. printf("Gathering state %d: %d\n", peer == peer1 ? 1 : 2, (int)state);
  60. }
  61. static void openCallback(int id, void *ptr) {
  62. Peer *peer = (Peer *)ptr;
  63. peer->connected = true;
  64. printf("Track %d: Open\n", peer == peer1 ? 1 : 2);
  65. }
  66. static void closedCallback(int id, void *ptr) {
  67. Peer *peer = (Peer *)ptr;
  68. peer->connected = false;
  69. }
  70. static void trackCallback(int pc, int tr, void *ptr) {
  71. Peer *peer = (Peer *)ptr;
  72. peer->tr = tr;
  73. peer->connected = true;
  74. rtcSetClosedCallback(tr, closedCallback);
  75. char buffer[1024];
  76. if (rtcGetTrackDescription(tr, buffer, 1024) >= 0)
  77. printf("Track %d: Received with media description: \n%s\n", peer == peer1 ? 1 : 2, buffer);
  78. }
  79. static Peer *createPeer(const rtcConfiguration *config) {
  80. Peer *peer = (Peer *)malloc(sizeof(Peer));
  81. if (!peer)
  82. return nullptr;
  83. memset(peer, 0, sizeof(Peer));
  84. // Create peer connection
  85. peer->pc = rtcCreatePeerConnection(config);
  86. rtcSetUserPointer(peer->pc, peer);
  87. rtcSetTrackCallback(peer->pc, trackCallback);
  88. rtcSetLocalDescriptionCallback(peer->pc, descriptionCallback);
  89. rtcSetLocalCandidateCallback(peer->pc, candidateCallback);
  90. rtcSetStateChangeCallback(peer->pc, stateChangeCallback);
  91. rtcSetGatheringStateChangeCallback(peer->pc, gatheringStateCallback);
  92. return peer;
  93. }
  94. static void deletePeer(Peer *peer) {
  95. if (peer) {
  96. if (peer->tr)
  97. rtcDeleteTrack(peer->tr);
  98. if (peer->pc)
  99. rtcDeletePeerConnection(peer->pc);
  100. free(peer);
  101. }
  102. }
  103. int test_capi_track_main() {
  104. int attempts;
  105. rtcInitLogger(RTC_LOG_DEBUG, nullptr);
  106. // Create peer 1
  107. rtcConfiguration config1;
  108. memset(&config1, 0, sizeof(config1));
  109. // STUN server example
  110. // const char *iceServers[1] = {"stun:stun.l.google.com:19302"};
  111. // config1.iceServers = iceServers;
  112. // config1.iceServersCount = 1;
  113. peer1 = createPeer(&config1);
  114. if (!peer1)
  115. goto error;
  116. // Create peer 2
  117. rtcConfiguration config2;
  118. memset(&config2, 0, sizeof(config2));
  119. // STUN server example
  120. // config2.iceServers = iceServers;
  121. // config2.iceServersCount = 1;
  122. // Port range example
  123. config2.portRangeBegin = 5000;
  124. config2.portRangeEnd = 6000;
  125. peer2 = createPeer(&config2);
  126. if (!peer2)
  127. goto error;
  128. // Peer 1: Create track
  129. peer1->tr = rtcAddTrack(peer1->pc, mediaDescription);
  130. rtcSetOpenCallback(peer1->tr, openCallback);
  131. rtcSetClosedCallback(peer1->tr, closedCallback);
  132. // Initiate the handshake
  133. rtcSetLocalDescription(peer1->pc, NULL);
  134. attempts = 10;
  135. while ((!peer2->connected || !peer1->connected) && attempts--)
  136. sleep(1);
  137. if (peer1->state != RTC_CONNECTED || peer2->state != RTC_CONNECTED) {
  138. fprintf(stderr, "PeerConnection is not connected\n");
  139. goto error;
  140. }
  141. if (!peer1->connected || !peer2->connected) {
  142. fprintf(stderr, "Track is not connected\n");
  143. goto error;
  144. }
  145. deletePeer(peer1);
  146. sleep(1);
  147. deletePeer(peer2);
  148. sleep(1);
  149. // You may call rtcCleanup() when finished to free static resources
  150. rtcCleanup();
  151. sleep(2);
  152. printf("Success\n");
  153. return 0;
  154. error:
  155. deletePeer(peer1);
  156. deletePeer(peer2);
  157. return -1;
  158. }
  159. #include <stdexcept>
  160. void test_capi_track() {
  161. if (test_capi_track_main())
  162. throw std::runtime_error("Connection failed");
  163. }