2 * rtp socket communication functions
4 * initially contributed by Felix von Leitner
6 * Copyright (c) 2000 Mark Taylor
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.
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.
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.
25 /* $Id: rtp.c,v 1.24 2011/05/07 16:05:17 rbrito Exp $ */
36 int sequence:16; /* sequence number: random */
37 int pt:7; /* payload type: 14 for MPEG audio */
38 int m:1; /* marker: 0 */
39 int cc:4; /* number of CSRC identifiers: 0 */
40 int x:1; /* number of extension headers: 0 */
41 int p:1; /* is there padding appended: 0 */
42 int v:2; /* version: 2 */
45 struct rtpheader { /* in network byte order */
47 int timestamp; /* start: random */
48 int ssrc; /* random */
49 int iAudioHeader; /* =0?! */
53 #if !defined( _WIN32 ) && !defined(__MINGW32__)
62 # define memcpy(d, s, n) bcopy ((s), (d), (n))
63 # define memmove(d, s, n) bcopy ((s), (d), (n))
71 #include <sys/types.h>
72 #include <sys/socket.h>
73 #ifdef __int8_t_defined
79 #include <netinet/in.h>
80 #include <arpa/inet.h>
91 struct rtpheader RTPheader;
95 /* create a sender socket. */
97 rtp_socket(char const *address, unsigned int port, unsigned int TTL)
100 struct sockaddr_in sin;
101 unsigned char cTtl = TTL;
103 unsigned int tempaddr;
105 int iSocket = socket(AF_INET, SOCK_DGRAM, 0);
107 error_printf("socket() failed.\n");
111 memset(&sin, 0, sizeof(sin));
112 tempaddr = inet_addr(address);
113 sin.sin_family = AF_INET;
114 sin.sin_port = htons(port);
115 sin.sin_addr.s_addr = tempaddr;
117 iRet = setsockopt(iSocket, SOL_SOCKET, SO_REUSEADDR, &iLoop, sizeof(int));
119 error_printf("setsockopt SO_REUSEADDR failed\n");
123 if ((ntohl(tempaddr) >> 28) == 0xe) {
124 /* only set multicast parameters for multicast destination IPs */
125 iRet = setsockopt(iSocket, IPPROTO_IP, IP_MULTICAST_TTL, &cTtl, sizeof(char));
127 error_printf("setsockopt IP_MULTICAST_TTL failed. multicast in kernel?\n");
132 iRet = setsockopt(iSocket, IPPROTO_IP, IP_MULTICAST_LOOP, &cLoop, sizeof(char));
134 error_printf("setsockopt IP_MULTICAST_LOOP failed. multicast in kernel?\n");
138 iRet = connect(iSocket, (struct sockaddr *) &sin, sizeof(struct sockaddr_in));
140 error_printf("connect IP_MULTICAST_LOOP failed. multicast in kernel?\n");
151 rtp_initialization_extra(void)
156 rtp_close_extra(void)
162 #include <Winsock2.h>
163 #ifndef IP_MULTICAST_TTL
164 #define IP_MULTICAST_TTL 3
173 struct rtpheader RTPheader;
177 last_error_message(int err_code)
181 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
184 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) & p_msg_buf, 0, NULL);
185 msg = strdup(p_msg_buf);
186 LocalFree(p_msg_buf);
191 print_socket_error(int error)
193 char *err_txt = last_error_message(error);
194 error_printf("error %d\n%s\n", error, err_txt);
200 on_socket_error(SOCKET s)
202 int error = WSAGetLastError();
203 print_socket_error(error);
204 if (s != INVALID_SOCKET) {
210 /* create a sender socket. */
212 rtp_socket(char const *address, unsigned int port, unsigned int TTL)
220 SOCKADDR_IN source, dest;
222 source.sin_family = AF_INET;
223 source.sin_addr.s_addr = htonl(INADDR_ANY);
224 source.sin_port = htons(0);
226 dest.sin_family = AF_INET;
227 dest.sin_addr.s_addr = inet_addr(address);
229 if (!strcmp(address, "255.255.255.255")) {
231 else if (dest.sin_addr.s_addr == INADDR_NONE) {
232 host = gethostbyname(address);
235 dest.sin_addr = *(PIN_ADDR) host->h_addr;
238 error_printf("Unknown host %s\r\n", address);
243 dest.sin_port = htons((u_short) port);
245 ip = ntohl(dest.sin_addr.s_addr);
255 if (ip == INADDR_LOOPBACK)
257 if (ip == INADDR_BROADCAST)
260 s = socket(AF_INET, SOCK_DGRAM, PF_UNSPEC);
261 if (s == INVALID_SOCKET) {
262 error_printf("socket () ");
263 return on_socket_error(s);
265 error = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &True, sizeof(True));
266 error = bind(s, (struct sockaddr *) &source, sizeof(source));
267 if (error == SOCKET_ERROR) {
268 error_printf("bind () ");
269 return on_socket_error(s);
271 if (ip == INADDR_BROADCAST) {
272 error_printf("broadcast %s:%u %s\r\n", inet_ntoa(dest.sin_addr), ntohs(dest.sin_port), c);
273 error = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &True, sizeof(True));
274 if (error == SOCKET_ERROR) {
275 error_printf("setsockopt (%u, SOL_SOCKET, SO_BROADCAST, ...) ", s);
276 return on_socket_error(s);
280 error_printf("multicast %s:%u %s\r\n", inet_ntoa(dest.sin_addr), ntohs(dest.sin_port), c);
281 error = setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (const char *) &TTL, sizeof(TTL));
282 if (error == SOCKET_ERROR) {
283 error_printf("setsockopt (%u, IPPROTO_IP, IP_MULTICAST_TTL, ...) ", s);
284 return on_socket_error(s);
287 error = connect(s, (PSOCKADDR) & dest, sizeof(SOCKADDR_IN));
288 if (error == SOCKET_ERROR) {
289 error_printf("connect: ");
290 return on_socket_error(s);
297 rtp_initialization_extra(void)
300 int rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
302 print_socket_error(rc);
307 rtp_close_extra(void)
316 rtp_send(unsigned char const *data, int len)
318 SOCKET s = rtpsocket;
319 struct rtpheader *foo = &RTPheader;
320 char *buffer = malloc(len + sizeof(struct rtpheader));
321 int *cast = (int *) foo;
322 int *outcast = (int *) buffer;
325 outcast[0] = htonl(cast[0]);
326 outcast[1] = htonl(cast[1]);
327 outcast[2] = htonl(cast[2]);
328 outcast[3] = htonl(cast[3]);
329 memmove(buffer + sizeof(struct rtpheader), data, len);
330 size = len + sizeof(*foo);
331 count = send(s, buffer, size, 0);
334 return count != size;
338 rtp_output(unsigned char const *mp3buffer, int mp3size)
340 rtp_send(mp3buffer, mp3size);
341 RTPheader.timestamp += 5;
342 RTPheader.b.sequence++;
346 rtp_initialization(void)
348 struct rtpheader *foo = &RTPheader;
354 foo->b.pt = 14; /* MPEG Audio */
355 foo->b.sequence = rand() & 65535;
356 foo->timestamp = rand();
358 foo->iAudioHeader = 0;
359 rtp_initialization_extra();
363 rtp_deinitialization(void)