offerer.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /**
  2. * Copyright (c) 2020 Paul-Louis Ageneau
  3. * Copyright (c) 2020 Stevedan Ogochukwu Omodolor
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  8. */
  9. #include <rtc/rtc.h>
  10. #include <ctype.h>
  11. #include <stdbool.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #ifdef _WIN32
  16. #include "getline.h"
  17. #include <windows.h>
  18. static void sleep(unsigned int secs) { Sleep(secs * 1000); }
  19. #else
  20. #include <unistd.h> // for sleep
  21. #endif
  22. typedef struct {
  23. rtcState state;
  24. rtcGatheringState gatheringState;
  25. int pc;
  26. int dc;
  27. bool connected;
  28. } Peer;
  29. static void RTC_API descriptionCallback(int pc, const char *sdp, const char *type, void *ptr);
  30. static void RTC_API candidateCallback(int pc, const char *cand, const char *mid, void *ptr);
  31. static void RTC_API stateChangeCallback(int pc, rtcState state, void *ptr);
  32. static void RTC_API gatheringStateCallback(int pc, rtcGatheringState state, void *ptr);
  33. static void RTC_API openCallback(int id, void *ptr);
  34. static void RTC_API closedCallback(int id, void *ptr);
  35. static void RTC_API messageCallback(int id, const char *message, int size, void *ptr);
  36. static void deletePeer(Peer *peer);
  37. char *state_print(rtcState state);
  38. char *rtcGatheringState_print(rtcGatheringState state);
  39. int all_space(const char *str);
  40. int main(int argc, char **argv) {
  41. rtcInitLogger(RTC_LOG_WARNING, NULL);
  42. // Create peer
  43. rtcConfiguration config;
  44. memset(&config, 0, sizeof(config));
  45. Peer *peer = (Peer *)malloc(sizeof(Peer));
  46. if (!peer) {
  47. fprintf(stderr, "Error allocating memory for peer\n");
  48. return -1;
  49. }
  50. memset(peer, 0, sizeof(Peer));
  51. printf("Peer created\n");
  52. // Create peer connection
  53. peer->pc = rtcCreatePeerConnection(&config);
  54. rtcSetUserPointer(peer->pc, peer);
  55. rtcSetLocalDescriptionCallback(peer->pc, descriptionCallback);
  56. rtcSetLocalCandidateCallback(peer->pc, candidateCallback);
  57. rtcSetStateChangeCallback(peer->pc, stateChangeCallback);
  58. rtcSetGatheringStateChangeCallback(peer->pc, gatheringStateCallback);
  59. // Since we are the offerer, we will create a datachannel
  60. peer->dc = rtcCreateDataChannel(peer->pc, "test");
  61. rtcSetOpenCallback(peer->dc, openCallback);
  62. rtcSetClosedCallback(peer->dc, closedCallback);
  63. rtcSetMessageCallback(peer->dc, messageCallback);
  64. sleep(1);
  65. bool exit = false;
  66. while (!exit) {
  67. printf("\n");
  68. printf("***********************************************************************************"
  69. "****\n");
  70. printf("* 0: Exit /"
  71. " 1: Enter remote description /"
  72. " 2: Enter remote candidate /"
  73. " 3: Send message /"
  74. " 4: Print Connection Info *\n"
  75. "[Command]: ");
  76. int command = -1;
  77. int c;
  78. if (!scanf("%d", &command)) {
  79. break;
  80. }
  81. while ((c = getchar()) != '\n' && c != EOF) {
  82. }
  83. fflush(stdin);
  84. switch (command) {
  85. case 0: {
  86. exit = true;
  87. break;
  88. }
  89. case 1: {
  90. // Parse Description
  91. printf("[Description]: ");
  92. char *line = NULL;
  93. size_t len = 0;
  94. size_t read = 0;
  95. char *sdp = (char *)malloc(sizeof(char));
  96. while ((read = getline(&line, &len, stdin)) != -1 && !all_space(line)) {
  97. sdp = (char *)realloc(sdp, (strlen(sdp) + 1) + strlen(line) + 1);
  98. strcat(sdp, line);
  99. }
  100. printf("%s\n", sdp);
  101. rtcSetRemoteDescription(peer->pc, sdp, "answer");
  102. free(sdp);
  103. free(line);
  104. break;
  105. }
  106. case 2: {
  107. // Parse Candidate
  108. printf("[Candidate]: ");
  109. char *candidate = NULL;
  110. size_t candidate_size = 0;
  111. if (getline(&candidate, &candidate_size, stdin)) {
  112. rtcAddRemoteCandidate(peer->pc, candidate, NULL);
  113. free(candidate);
  114. } else {
  115. printf("Error reading line\n");
  116. break;
  117. }
  118. break;
  119. }
  120. case 3: {
  121. // Send Message
  122. if (!peer->connected) {
  123. printf("** Channel is not Open **");
  124. break;
  125. }
  126. printf("[Message]: ");
  127. char *message = NULL;
  128. size_t message_size = 0;
  129. if (getline(&message, &message_size, stdin)) {
  130. rtcSendMessage(peer->dc, message, -1);
  131. free(message);
  132. } else {
  133. printf("Error reading line\n");
  134. break;
  135. }
  136. break;
  137. }
  138. case 4: {
  139. // Connection Info
  140. if (!peer->connected) {
  141. printf("** Channel is not Open **");
  142. break;
  143. }
  144. char buffer[256];
  145. if (rtcGetLocalAddress(peer->pc, buffer, 256) >= 0)
  146. printf("Local address 1: %s\n", buffer);
  147. if (rtcGetRemoteAddress(peer->pc, buffer, 256) >= 0)
  148. printf("Remote address 1: %s\n", buffer);
  149. else
  150. printf("Could not get Candidate Pair Info\n");
  151. break;
  152. }
  153. default: {
  154. printf("** Invalid Command **");
  155. break;
  156. }
  157. }
  158. }
  159. deletePeer(peer);
  160. return 0;
  161. }
  162. static void RTC_API descriptionCallback(int pc, const char *sdp, const char *type, void *ptr) {
  163. printf("Description %s:\n%s\n", "offerer", sdp);
  164. }
  165. static void RTC_API candidateCallback(int pc, const char *cand, const char *mid, void *ptr) {
  166. printf("Candidate %s: %s\n", "offerer", cand);
  167. }
  168. static void RTC_API stateChangeCallback(int pc, rtcState state, void *ptr) {
  169. Peer *peer = (Peer *)ptr;
  170. peer->state = state;
  171. printf("State %s: %s\n", "offerer", state_print(state));
  172. }
  173. static void RTC_API gatheringStateCallback(int pc, rtcGatheringState state, void *ptr) {
  174. Peer *peer = (Peer *)ptr;
  175. peer->gatheringState = state;
  176. printf("Gathering state %s: %s\n", "offerer", rtcGatheringState_print(state));
  177. }
  178. static void RTC_API openCallback(int id, void *ptr) {
  179. Peer *peer = (Peer *)ptr;
  180. peer->connected = true;
  181. char buffer[256];
  182. if (rtcGetDataChannelLabel(peer->dc, buffer, 256) >= 0)
  183. printf("DataChannel %s: Received with label \"%s\"\n", "offerer", buffer);
  184. }
  185. static void RTC_API closedCallback(int id, void *ptr) {
  186. Peer *peer = (Peer *)ptr;
  187. peer->connected = false;
  188. }
  189. static void RTC_API messageCallback(int id, const char *message, int size, void *ptr) {
  190. if (size < 0) { // negative size indicates a null-terminated string
  191. printf("Message %s: %s\n", "offerer", message);
  192. } else {
  193. printf("Message %s: [binary of size %d]\n", "offerer", size);
  194. }
  195. }
  196. static void deletePeer(Peer *peer) {
  197. if (peer) {
  198. if (peer->dc)
  199. rtcDeleteDataChannel(peer->dc);
  200. if (peer->pc)
  201. rtcDeletePeerConnection(peer->pc);
  202. free(peer);
  203. }
  204. }
  205. char *state_print(rtcState state) {
  206. char *str = NULL;
  207. switch (state) {
  208. case RTC_NEW:
  209. str = "RTC_NEW";
  210. break;
  211. case RTC_CONNECTING:
  212. str = "RTC_CONNECTING";
  213. break;
  214. case RTC_CONNECTED:
  215. str = "RTC_CONNECTED";
  216. break;
  217. case RTC_DISCONNECTED:
  218. str = "RTC_DISCONNECTED";
  219. break;
  220. case RTC_FAILED:
  221. str = "RTC_FAILED";
  222. break;
  223. case RTC_CLOSED:
  224. str = "RTC_CLOSED";
  225. break;
  226. default:
  227. break;
  228. }
  229. return str;
  230. }
  231. char *rtcGatheringState_print(rtcGatheringState state) {
  232. char *str = NULL;
  233. switch (state) {
  234. case RTC_GATHERING_NEW:
  235. str = "RTC_GATHERING_NEW";
  236. break;
  237. case RTC_GATHERING_INPROGRESS:
  238. str = "RTC_GATHERING_INPROGRESS";
  239. break;
  240. case RTC_GATHERING_COMPLETE:
  241. str = "RTC_GATHERING_COMPLETE";
  242. break;
  243. default:
  244. break;
  245. }
  246. return str;
  247. }
  248. int all_space(const char *str) {
  249. while (*str) {
  250. if (!isspace(*str++)) {
  251. return 0;
  252. }
  253. }
  254. return 1;
  255. }