offerer.c 7.5 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 descriptionCallback(int pc, const char *sdp, const char *type, void *ptr);
  40. static void RTC_API candidateCallback(int pc, const char *cand, const char *mid, void *ptr);
  41. static void RTC_API stateChangeCallback(int pc, rtcState state, void *ptr);
  42. static void RTC_API gatheringStateCallback(int pc, rtcGatheringState state, void *ptr);
  43. static void RTC_API openCallback(int id, 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 RTC_API 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. // Since we are the offerer, we will create a datachannel
  70. peer->dc = rtcCreateDataChannel(peer->pc, "test");
  71. rtcSetOpenCallback(peer->dc, openCallback);
  72. rtcSetClosedCallback(peer->dc, closedCallback);
  73. rtcSetMessageCallback(peer->dc, messageCallback);
  74. sleep(1);
  75. bool exit = false;
  76. while (!exit) {
  77. printf("\n");
  78. printf("***********************************************************************************"
  79. "****\n");
  80. printf("* 0: Exit /"
  81. " 1: Enter remote description /"
  82. " 2: Enter remote candidate /"
  83. " 3: Send message /"
  84. " 4: Print Connection Info *\n"
  85. "[Command]: ");
  86. int command = -1;
  87. int c;
  88. if (!scanf("%d", &command)) {
  89. break;
  90. }
  91. while ((c = getchar()) != '\n' && c != EOF) {
  92. }
  93. fflush(stdin);
  94. switch (command) {
  95. case 0: {
  96. exit = true;
  97. break;
  98. }
  99. case 1: {
  100. // Parse Description
  101. printf("[Description]: ");
  102. char *line = NULL;
  103. size_t len = 0;
  104. size_t read = 0;
  105. char *sdp = (char *)malloc(sizeof(char));
  106. while ((read = getline(&line, &len, stdin)) != -1 && !all_space(line)) {
  107. sdp = (char *)realloc(sdp, (strlen(sdp) + 1) + strlen(line) + 1);
  108. strcat(sdp, line);
  109. }
  110. printf("%s\n", sdp);
  111. rtcSetRemoteDescription(peer->pc, sdp, "answer");
  112. free(sdp);
  113. free(line);
  114. break;
  115. }
  116. case 2: {
  117. // Parse Candidate
  118. printf("[Candidate]: ");
  119. char *candidate = NULL;
  120. size_t candidate_size = 0;
  121. if (getline(&candidate, &candidate_size, stdin)) {
  122. rtcAddRemoteCandidate(peer->pc, candidate, "0");
  123. free(candidate);
  124. } else {
  125. printf("Error reading line\n");
  126. break;
  127. }
  128. break;
  129. }
  130. case 3: {
  131. // Send Message
  132. if (!peer->connected) {
  133. printf("** Channel is not Open **");
  134. break;
  135. }
  136. printf("[Message]: ");
  137. char *message = NULL;
  138. size_t message_size = 0;
  139. if (getline(&message, &message_size, stdin)) {
  140. rtcSendMessage(peer->dc, message, -1);
  141. free(message);
  142. } else {
  143. printf("Error reading line\n");
  144. break;
  145. }
  146. break;
  147. }
  148. case 4: {
  149. // Connection Info
  150. if (!peer->connected) {
  151. printf("** Channel is not Open **");
  152. break;
  153. }
  154. char buffer[256];
  155. if (rtcGetLocalAddress(peer->pc, buffer, 256) >= 0)
  156. printf("Local address 1: %s\n", buffer);
  157. if (rtcGetRemoteAddress(peer->pc, buffer, 256) >= 0)
  158. printf("Remote address 1: %s\n", buffer);
  159. else
  160. printf("Could not get Candidate Pair Info\n");
  161. break;
  162. }
  163. default: {
  164. printf("** Invalid Command **");
  165. break;
  166. }
  167. }
  168. }
  169. deletePeer(peer);
  170. return 0;
  171. }
  172. static void RTC_API descriptionCallback(int pc, const char *sdp, const char *type, void *ptr) {
  173. printf("Description %s:\n%s\n", "offerer", sdp);
  174. }
  175. static void RTC_API candidateCallback(int pc, const char *cand, const char *mid, void *ptr) {
  176. printf("Candidate %s: %s\n", "offerer", cand);
  177. }
  178. static void RTC_API stateChangeCallback(int pc, rtcState state, void *ptr) {
  179. Peer *peer = (Peer *)ptr;
  180. peer->state = state;
  181. printf("State %s: %s\n", "offerer", state_print(state));
  182. }
  183. static void RTC_API gatheringStateCallback(int pc, rtcGatheringState state, void *ptr) {
  184. Peer *peer = (Peer *)ptr;
  185. peer->gatheringState = state;
  186. printf("Gathering state %s: %s\n", "offerer", rtcGatheringState_print(state));
  187. }
  188. static void RTC_API openCallback(int id, void *ptr) {
  189. Peer *peer = (Peer *)ptr;
  190. peer->connected = true;
  191. char buffer[256];
  192. if (rtcGetDataChannelLabel(peer->dc, buffer, 256) >= 0)
  193. printf("DataChannel %s: Received with label \"%s\"\n", "offerer", buffer);
  194. }
  195. static void RTC_API closedCallback(int id, void *ptr) {
  196. Peer *peer = (Peer *)ptr;
  197. peer->connected = false;
  198. }
  199. static void RTC_API messageCallback(int id, const char *message, int size, void *ptr) {
  200. if (size < 0) { // negative size indicates a null-terminated string
  201. printf("Message %s: %s\n", "offerer", message);
  202. } else {
  203. printf("Message %s: [binary of size %d]\n", "offerer", size);
  204. }
  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. }