rtp.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * rtp socket communication functions
  3. *
  4. * initially contributed by Felix von Leitner
  5. *
  6. * Copyright (c) 2000 Mark Taylor
  7. * 2010 Robert Hegemann
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Library General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Library General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Library General Public
  20. * License along with this library; if not, write to the
  21. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  22. * Boston, MA 02111-1307, USA.
  23. */
  24. /* $Id$ */
  25. #ifdef HAVE_CONFIG_H
  26. # include <config.h>
  27. #endif
  28. #ifdef HAVE_STDINT_H
  29. # include <stdint.h>
  30. #endif
  31. struct rtpbits {
  32. int sequence:16; /* sequence number: random */
  33. int pt:7; /* payload type: 14 for MPEG audio */
  34. int m:1; /* marker: 0 */
  35. int cc:4; /* number of CSRC identifiers: 0 */
  36. int x:1; /* number of extension headers: 0 */
  37. int p:1; /* is there padding appended: 0 */
  38. int v:2; /* version: 2 */
  39. };
  40. struct rtpheader { /* in network byte order */
  41. struct rtpbits b;
  42. int timestamp; /* start: random */
  43. int ssrc; /* random */
  44. int iAudioHeader; /* =0?! */
  45. };
  46. #if !defined( _WIN32 ) && !defined(__MINGW32__)
  47. #ifdef STDC_HEADERS
  48. # include <stdio.h>
  49. # include <stdarg.h>
  50. # include <stdlib.h>
  51. # include <string.h>
  52. #else
  53. # ifndef HAVE_MEMCPY
  54. # define memcpy(d, s, n) bcopy ((s), (d), (n))
  55. # define memmove(d, s, n) bcopy ((s), (d), (n))
  56. # endif
  57. #endif
  58. #ifdef HAVE_UNISTD_H
  59. # include <unistd.h>
  60. #endif
  61. #include <sys/types.h>
  62. #include <sys/socket.h>
  63. #ifdef __int8_t_defined
  64. #undef uint8_t
  65. #undef uint16_t
  66. #undef uint32_t
  67. #undef uint64_t
  68. #endif
  69. #include <netinet/in.h>
  70. #include <arpa/inet.h>
  71. #ifdef WITH_DMALLOC
  72. #include <dmalloc.h>
  73. #endif
  74. #include "rtp.h"
  75. #include "console.h"
  76. typedef int SOCKET;
  77. struct rtpheader RTPheader;
  78. SOCKET rtpsocket;
  79. /* create a sender socket. */
  80. int
  81. rtp_socket(char const *address, unsigned int port, unsigned int TTL)
  82. {
  83. int iRet, iLoop = 1;
  84. struct sockaddr_in sin;
  85. unsigned char cTtl = TTL;
  86. char cLoop = 0;
  87. unsigned int tempaddr;
  88. int iSocket = socket(AF_INET, SOCK_DGRAM, 0);
  89. if (iSocket < 0) {
  90. error_printf("socket() failed.\n");
  91. return 1;
  92. }
  93. memset(&sin, 0, sizeof(sin));
  94. tempaddr = inet_addr(address);
  95. sin.sin_family = AF_INET;
  96. sin.sin_port = htons(port);
  97. sin.sin_addr.s_addr = tempaddr;
  98. iRet = setsockopt(iSocket, SOL_SOCKET, SO_REUSEADDR, &iLoop, sizeof(int));
  99. if (iRet < 0) {
  100. error_printf("setsockopt SO_REUSEADDR failed\n");
  101. return 1;
  102. }
  103. if ((ntohl(tempaddr) >> 28) == 0xe) {
  104. /* only set multicast parameters for multicast destination IPs */
  105. iRet = setsockopt(iSocket, IPPROTO_IP, IP_MULTICAST_TTL, &cTtl, sizeof(char));
  106. if (iRet < 0) {
  107. error_printf("setsockopt IP_MULTICAST_TTL failed. multicast in kernel?\n");
  108. return 1;
  109. }
  110. cLoop = 1; /* !? */
  111. iRet = setsockopt(iSocket, IPPROTO_IP, IP_MULTICAST_LOOP, &cLoop, sizeof(char));
  112. if (iRet < 0) {
  113. error_printf("setsockopt IP_MULTICAST_LOOP failed. multicast in kernel?\n");
  114. return 1;
  115. }
  116. }
  117. iRet = connect(iSocket, (struct sockaddr *) &sin, sizeof(struct sockaddr_in));
  118. if (iRet < 0) {
  119. error_printf("connect IP_MULTICAST_LOOP failed. multicast in kernel?\n");
  120. return 1;
  121. }
  122. rtpsocket = iSocket;
  123. return 0;
  124. }
  125. static void
  126. rtp_initialization_extra(void)
  127. {
  128. }
  129. static void
  130. rtp_close_extra(void)
  131. {
  132. }
  133. #else
  134. #include <Winsock2.h>
  135. #ifndef IP_MULTICAST_TTL
  136. #define IP_MULTICAST_TTL 3
  137. #endif
  138. #include <stdio.h>
  139. #include <stdarg.h>
  140. #include "rtp.h"
  141. #include "console.h"
  142. struct rtpheader RTPheader;
  143. SOCKET rtpsocket;
  144. static char *
  145. last_error_message(int err_code)
  146. {
  147. char *msg;
  148. void *p_msg_buf;
  149. FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
  150. (void *) 0,
  151. (DWORD) err_code,
  152. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) & p_msg_buf, 0, NULL);
  153. msg = strdup(p_msg_buf);
  154. LocalFree(p_msg_buf);
  155. return msg;
  156. }
  157. static int
  158. print_socket_error(int error)
  159. {
  160. char *err_txt = last_error_message(error);
  161. error_printf("error %d\n%s\n", error, err_txt);
  162. free(err_txt);
  163. return error;
  164. }
  165. static int
  166. on_socket_error(SOCKET s)
  167. {
  168. int error = WSAGetLastError();
  169. print_socket_error(error);
  170. if (s != INVALID_SOCKET) {
  171. closesocket(s);
  172. }
  173. return error;
  174. }
  175. /* create a sender socket. */
  176. int
  177. rtp_socket(char const *address, unsigned int port, unsigned int TTL)
  178. {
  179. char const True = 1;
  180. char const *c = "";
  181. int error;
  182. UINT ip;
  183. PHOSTENT host;
  184. SOCKET s;
  185. SOCKADDR_IN source, dest;
  186. source.sin_family = AF_INET;
  187. source.sin_addr.s_addr = htonl(INADDR_ANY);
  188. source.sin_port = htons(0);
  189. dest.sin_family = AF_INET;
  190. dest.sin_addr.s_addr = inet_addr(address);
  191. if (!strcmp(address, "255.255.255.255")) {
  192. }
  193. else if (dest.sin_addr.s_addr == INADDR_NONE) {
  194. host = gethostbyname(address);
  195. if (host) {
  196. dest.sin_addr = *(PIN_ADDR) host->h_addr;
  197. }
  198. else {
  199. error_printf("Unknown host %s\r\n", address);
  200. return 1;
  201. }
  202. }
  203. dest.sin_port = htons((u_short) port);
  204. ip = ntohl(dest.sin_addr.s_addr);
  205. if (IN_CLASSA(ip))
  206. c = "class A";
  207. if (IN_CLASSB(ip))
  208. c = "class B";
  209. if (IN_CLASSC(ip))
  210. c = "class C";
  211. if (IN_CLASSD(ip))
  212. c = "class D";
  213. if (ip == INADDR_LOOPBACK)
  214. c = "loopback";
  215. if (ip == INADDR_BROADCAST)
  216. c = "broadcast";
  217. s = socket(AF_INET, SOCK_DGRAM, PF_UNSPEC);
  218. if (s == INVALID_SOCKET) {
  219. error_printf("socket () ");
  220. return on_socket_error(s);
  221. }
  222. error = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &True, sizeof(True));
  223. error = bind(s, (struct sockaddr *) &source, sizeof(source));
  224. if (error == SOCKET_ERROR) {
  225. error_printf("bind () ");
  226. return on_socket_error(s);
  227. }
  228. if (ip == INADDR_BROADCAST) {
  229. error_printf("broadcast %s:%u %s\r\n", inet_ntoa(dest.sin_addr), ntohs(dest.sin_port), c);
  230. error = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &True, sizeof(True));
  231. if (error == SOCKET_ERROR) {
  232. error_printf("setsockopt (%u, SOL_SOCKET, SO_BROADCAST, ...) ", s);
  233. return on_socket_error(s);
  234. }
  235. }
  236. if (IN_CLASSD(ip)) {
  237. error_printf("multicast %s:%u %s\r\n", inet_ntoa(dest.sin_addr), ntohs(dest.sin_port), c);
  238. error = setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (const char *) &TTL, sizeof(TTL));
  239. if (error == SOCKET_ERROR) {
  240. error_printf("setsockopt (%u, IPPROTO_IP, IP_MULTICAST_TTL, ...) ", s);
  241. return on_socket_error(s);
  242. }
  243. }
  244. error = connect(s, (PSOCKADDR) & dest, sizeof(SOCKADDR_IN));
  245. if (error == SOCKET_ERROR) {
  246. error_printf("connect: ");
  247. return on_socket_error(s);
  248. }
  249. rtpsocket = s;
  250. return 0;
  251. }
  252. static void
  253. rtp_initialization_extra(void)
  254. {
  255. WSADATA wsaData;
  256. int rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
  257. if (rc != 0) {
  258. print_socket_error(rc);
  259. }
  260. }
  261. static void
  262. rtp_close_extra(void)
  263. {
  264. WSACleanup();
  265. }
  266. #endif
  267. static int
  268. rtp_send(unsigned char const *data, int len)
  269. {
  270. SOCKET s = rtpsocket;
  271. struct rtpheader *foo = &RTPheader;
  272. char *buffer = malloc(len + sizeof(struct rtpheader));
  273. int *cast = (int *) foo;
  274. int *outcast = (int *) buffer;
  275. int count, size;
  276. outcast[0] = htonl(cast[0]);
  277. outcast[1] = htonl(cast[1]);
  278. outcast[2] = htonl(cast[2]);
  279. outcast[3] = htonl(cast[3]);
  280. memmove(buffer + sizeof(struct rtpheader), data, len);
  281. size = len + sizeof(*foo);
  282. count = send(s, buffer, size, 0);
  283. free(buffer);
  284. return count != size;
  285. }
  286. void
  287. rtp_output(unsigned char const *mp3buffer, int mp3size)
  288. {
  289. rtp_send(mp3buffer, mp3size);
  290. RTPheader.timestamp += 5;
  291. RTPheader.b.sequence++;
  292. }
  293. void
  294. rtp_initialization(void)
  295. {
  296. struct rtpheader *foo = &RTPheader;
  297. foo->b.v = 2;
  298. foo->b.p = 0;
  299. foo->b.x = 0;
  300. foo->b.cc = 0;
  301. foo->b.m = 0;
  302. foo->b.pt = 14; /* MPEG Audio */
  303. foo->b.sequence = rand() & 65535;
  304. foo->timestamp = rand();
  305. foo->ssrc = rand();
  306. foo->iAudioHeader = 0;
  307. rtp_initialization_extra();
  308. }
  309. void
  310. rtp_deinitialization(void)
  311. {
  312. rtp_close_extra();
  313. }