answerer.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /**
  2. * Copyright (c) 2020 Paul-Louis Ageneau
  3. * Copyright (c) 2020 Stevedan Ogochukwu Omodolor
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include <rtc/rtc.h>
  20. #include <ctype.h>
  21. #include <stdbool.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #ifdef _WIN32
  26. #include "getline.h"
  27. #include <windows.h>
  28. static void sleep(unsigned int secs) { Sleep(secs * 1000); }
  29. #else
  30. #include <unistd.h> // for sleep
  31. #endif
  32. typedef struct {
  33. rtcState state;
  34. rtcGatheringState gatheringState;
  35. int pc;
  36. int dc;
  37. bool connected;
  38. } Peer;
  39. static void RTC_API dataChannelCallback(int pc, int dc, void *ptr);
  40. static void RTC_API descriptionCallback(int pc, const char *sdp, const char *type, void *ptr);
  41. static void RTC_API candidateCallback(int pc, const char *cand, const char *mid, void *ptr);
  42. static void RTC_API stateChangeCallback(int pc, rtcState state, void *ptr);
  43. static void RTC_API gatheringStateCallback(int pc, rtcGatheringState state, void *ptr);
  44. static void RTC_API closedCallback(int id, void *ptr);
  45. static void RTC_API messageCallback(int id, const char *message, int size, void *ptr);
  46. static void deletePeer(Peer *peer);
  47. char *state_print(rtcState state);
  48. char *rtcGatheringState_print(rtcGatheringState state);
  49. int all_space(const char *str);
  50. int main(int argc, char **argv) {
  51. rtcInitLogger(RTC_LOG_DEBUG, NULL);
  52. // Create peer
  53. rtcConfiguration config;
  54. memset(&config, 0, sizeof(config));
  55. Peer *peer = (Peer *)malloc(sizeof(Peer));
  56. if (!peer) {
  57. fprintf(stderr, "Error allocating memory for peer\n");
  58. return -1;
  59. }
  60. memset(peer, 0, sizeof(Peer));
  61. printf("Peer created\n");
  62. // Create peer connection
  63. peer->pc = rtcCreatePeerConnection(&config);
  64. rtcSetUserPointer(peer->pc, peer);
  65. rtcSetLocalDescriptionCallback(peer->pc, descriptionCallback);
  66. rtcSetLocalCandidateCallback(peer->pc, candidateCallback);
  67. rtcSetStateChangeCallback(peer->pc, stateChangeCallback);
  68. rtcSetGatheringStateChangeCallback(peer->pc, gatheringStateCallback);
  69. rtcSetUserPointer(peer->dc, NULL);
  70. rtcSetDataChannelCallback(peer->pc, dataChannelCallback);
  71. sleep(1);
  72. bool exit = false;
  73. while (!exit) {
  74. printf("\n");
  75. printf("***********************************************************************************"
  76. "****\n");
  77. printf("* 0: Exit /"
  78. " 1: Enter remote description /"
  79. " 2: Enter remote candidate /"
  80. " 3: Send message /"
  81. " 4: Print Connection Info *\n"
  82. "[Command]: ");
  83. int command = -1;
  84. int c;
  85. if (!scanf("%d", &command)) {
  86. break;
  87. }
  88. while ((c = getchar()) != '\n' && c != EOF) {
  89. }
  90. fflush(stdin);
  91. switch (command) {
  92. case 0: {
  93. exit = true;
  94. break;
  95. }
  96. case 1: {
  97. // Parse Description
  98. printf("[Description]: ");
  99. char *line = NULL;
  100. size_t len = 0;
  101. size_t read = 0;
  102. char *sdp = (char *)malloc(sizeof(char));
  103. while ((read = getline(&line, &len, stdin)) != -1 && !all_space(line)) {
  104. sdp = (char *)realloc(sdp, (strlen(sdp) + 1) + strlen(line) + 1);
  105. strcat(sdp, line);
  106. }
  107. printf("%s\n", sdp);
  108. rtcSetRemoteDescription(peer->pc, sdp, "offer");
  109. free(sdp);
  110. free(line);
  111. break;
  112. }
  113. case 2: {
  114. // Parse Candidate
  115. printf("[Candidate]: ");
  116. char *candidate = NULL;
  117. size_t candidate_size = 0;
  118. if (getline(&candidate, &candidate_size, stdin)) {
  119. rtcAddRemoteCandidate(peer->pc, candidate, NULL);
  120. free(candidate);
  121. } else {
  122. printf("Error reading line\n");
  123. break;
  124. }
  125. break;
  126. }
  127. case 3: {
  128. // Send Message
  129. if (!peer->connected) {
  130. printf("** Channel is not Open **");
  131. break;
  132. }
  133. printf("[Message]: ");
  134. char *message = NULL;
  135. size_t message_size = 0;
  136. if (getline(&message, &message_size, stdin)) {
  137. rtcSendMessage(peer->dc, message, -1);
  138. free(message);
  139. } else {
  140. printf("Error reading line\n");
  141. break;
  142. }
  143. break;
  144. }
  145. case 4: {
  146. // Connection Info
  147. if (!peer->connected) {
  148. printf("** Channel is not Open **");
  149. break;
  150. }
  151. char buffer[256];
  152. if (rtcGetLocalAddress(peer->pc, buffer, 256) >= 0)
  153. printf("Local address 1: %s\n", buffer);
  154. if (rtcGetRemoteAddress(peer->pc, buffer, 256) >= 0)
  155. printf("Remote address 1: %s\n", buffer);
  156. else
  157. printf("Could not get Candidate Pair Info\n");
  158. break;
  159. }
  160. default: {
  161. printf("** Invalid Command **");
  162. break;
  163. }
  164. }
  165. }
  166. deletePeer(peer);
  167. return 0;
  168. }
  169. static void RTC_API descriptionCallback(int pc, const char *sdp, const char *type, void *ptr) {
  170. printf("Description %s:\n%s\n", "answerer", sdp);
  171. }
  172. static void RTC_API candidateCallback(int pc, const char *cand, const char *mid, void *ptr) {
  173. printf("Candidate %s: %s\n", "answerer", cand);
  174. }
  175. static void RTC_API stateChangeCallback(int pc, rtcState state, void *ptr) {
  176. Peer *peer = (Peer *)ptr;
  177. peer->state = state;
  178. printf("State %s: %s\n", "answerer", state_print(state));
  179. }
  180. static void RTC_API gatheringStateCallback(int pc, rtcGatheringState state, void *ptr) {
  181. Peer *peer = (Peer *)ptr;
  182. peer->gatheringState = state;
  183. printf("Gathering state %s: %s\n", "answerer", rtcGatheringState_print(state));
  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", "answerer", message);
  192. } else {
  193. printf("Message %s: [binary of size %d]\n", "answerer", size);
  194. }
  195. }
  196. static void RTC_API dataChannelCallback(int pc, int dc, void *ptr) {
  197. Peer *peer = (Peer *)ptr;
  198. peer->dc = dc;
  199. peer->connected = true;
  200. rtcSetClosedCallback(dc, closedCallback);
  201. rtcSetMessageCallback(dc, messageCallback);
  202. char buffer[256];
  203. if (rtcGetDataChannelLabel(dc, buffer, 256) >= 0)
  204. printf("DataChannel %s: Received with label \"%s\"\n", "answerer", buffer);
  205. }
  206. static void deletePeer(Peer *peer) {
  207. if (peer) {
  208. if (peer->dc)
  209. rtcDeleteDataChannel(peer->dc);
  210. if (peer->pc)
  211. rtcDeletePeerConnection(peer->pc);
  212. free(peer);
  213. }
  214. }
  215. char *state_print(rtcState state) {
  216. char *str = NULL;
  217. switch (state) {
  218. case RTC_NEW:
  219. str = "RTC_NEW";
  220. break;
  221. case RTC_CONNECTING:
  222. str = "RTC_CONNECTING";
  223. break;
  224. case RTC_CONNECTED:
  225. str = "RTC_CONNECTED";
  226. break;
  227. case RTC_DISCONNECTED:
  228. str = "RTC_DISCONNECTED";
  229. break;
  230. case RTC_FAILED:
  231. str = "RTC_FAILED";
  232. break;
  233. case RTC_CLOSED:
  234. str = "RTC_CLOSED";
  235. break;
  236. default:
  237. break;
  238. }
  239. return str;
  240. }
  241. char *rtcGatheringState_print(rtcGatheringState state) {
  242. char *str = NULL;
  243. switch (state) {
  244. case RTC_GATHERING_NEW:
  245. str = "RTC_GATHERING_NEW";
  246. break;
  247. case RTC_GATHERING_INPROGRESS:
  248. str = "RTC_GATHERING_INPROGRESS";
  249. break;
  250. case RTC_GATHERING_COMPLETE:
  251. str = "RTC_GATHERING_COMPLETE";
  252. break;
  253. default:
  254. break;
  255. }
  256. return str;
  257. }
  258. int all_space(const char *str) {
  259. while (*str) {
  260. if (!isspace(*str++)) {
  261. return 0;
  262. }
  263. }
  264. return 1;
  265. }