Mercurial > mplayer.hg
comparison stream/rtp.c @ 19271:64d82a45a05d
introduce new 'stream' directory for all stream layer related components and split them from libmpdemux
author | ben |
---|---|
date | Mon, 31 Jul 2006 17:39:17 +0000 |
parents | libmpdemux/rtp.c@317e0fd394c5 |
children | 0792ad01e9bf |
comparison
equal
deleted
inserted
replaced
19270:7d39b911f0bd | 19271:64d82a45a05d |
---|---|
1 /* Imported from the dvbstream-0.2 project | |
2 * | |
3 * Modified for use with MPlayer, for details see the changelog at | |
4 * http://svn.mplayerhq.hu/mplayer/trunk/ | |
5 * $Id$ | |
6 */ | |
7 | |
8 #include <stdlib.h> | |
9 #include <string.h> | |
10 #include <unistd.h> | |
11 #include <stdlib.h> | |
12 #include <stdio.h> | |
13 #include <sys/types.h> | |
14 #include <ctype.h> | |
15 #include "config.h" | |
16 #ifndef HAVE_WINSOCK2 | |
17 #include <netinet/in.h> | |
18 #include <sys/socket.h> | |
19 #include <arpa/inet.h> | |
20 #define closesocket close | |
21 #else | |
22 #include <winsock2.h> | |
23 #include <ws2tcpip.h> | |
24 #endif | |
25 #include <errno.h> | |
26 #include "stream.h" | |
27 | |
28 /* MPEG-2 TS RTP stack */ | |
29 | |
30 #define DEBUG 1 | |
31 #include "rtp.h" | |
32 | |
33 extern int network_bandwidth; | |
34 | |
35 | |
36 #define DEBUG 1 | |
37 #include "../mp_msg.h" | |
38 #include "rtp.h" | |
39 | |
40 // RTP reorder routines | |
41 // Also handling of repeated UDP packets (a bug of ExtremeNetworks switches firmware) | |
42 // rtpreord procedures | |
43 // write rtp packets in cache | |
44 // get rtp packets reordered | |
45 | |
46 #define MAXRTPPACKETSIN 32 // The number of max packets being reordered | |
47 | |
48 struct rtpbuffer | |
49 { | |
50 unsigned char data[MAXRTPPACKETSIN][STREAM_BUFFER_SIZE]; | |
51 unsigned short seq[MAXRTPPACKETSIN]; | |
52 unsigned short len[MAXRTPPACKETSIN]; | |
53 unsigned short first; | |
54 }; | |
55 static struct rtpbuffer rtpbuf; | |
56 | |
57 // RTP Reordering functions | |
58 // Algorithm works as follows: | |
59 // If next packet is in sequence just copy it to buffer | |
60 // Otherwise copy it in cache according to its sequence number | |
61 // Cache is a circular array where "rtpbuf.first" points to next sequence slot | |
62 // and keeps track of expected sequence | |
63 | |
64 // Initialize rtp cache | |
65 static void rtp_cache_reset(unsigned short seq) | |
66 { | |
67 int i; | |
68 | |
69 rtpbuf.first = 0; | |
70 rtpbuf.seq[0] = ++seq; | |
71 | |
72 for (i=0; i<MAXRTPPACKETSIN; i++) { | |
73 rtpbuf.len[i] = 0; | |
74 } | |
75 } | |
76 | |
77 // Write in a cache the rtp packet in right rtp sequence order | |
78 static int rtp_cache(int fd, char *buffer, int length) | |
79 { | |
80 struct rtpheader rh; | |
81 int newseq; | |
82 char *data; | |
83 unsigned short seq; | |
84 static int is_first = 1; | |
85 | |
86 getrtp2(fd, &rh, &data, &length); | |
87 if(!length) | |
88 return 0; | |
89 seq = rh.b.sequence; | |
90 | |
91 newseq = seq - rtpbuf.seq[rtpbuf.first]; | |
92 | |
93 if ((newseq == 0) || is_first) | |
94 { | |
95 is_first = 0; | |
96 | |
97 //mp_msg(MSGT_NETWORK, MSGL_DBG4, "RTP (seq[%d]=%d seq=%d, newseq=%d)\n", rtpbuf.first, rtpbuf.seq[rtpbuf.first], seq, newseq); | |
98 rtpbuf.first = ( 1 + rtpbuf.first ) % MAXRTPPACKETSIN; | |
99 rtpbuf.seq[rtpbuf.first] = ++seq; | |
100 goto feed; | |
101 } | |
102 | |
103 if (newseq > MAXRTPPACKETSIN) | |
104 { | |
105 mp_msg(MSGT_NETWORK, MSGL_DBG2, "Overrun(seq[%d]=%d seq=%d, newseq=%d)\n", rtpbuf.first, rtpbuf.seq[rtpbuf.first], seq, newseq); | |
106 rtp_cache_reset(seq); | |
107 goto feed; | |
108 } | |
109 | |
110 if (newseq < 0) | |
111 { | |
112 int i; | |
113 | |
114 // Is it a stray packet re-sent to network? | |
115 for (i=0; i<MAXRTPPACKETSIN; i++) { | |
116 if (rtpbuf.seq[i] == seq) { | |
117 mp_msg(MSGT_NETWORK, MSGL_ERR, "Stray packet (seq[%d]=%d seq=%d, newseq=%d found at %d)\n", rtpbuf.first, rtpbuf.seq[rtpbuf.first], seq, newseq, i); | |
118 return 0; // Yes, it is! | |
119 } | |
120 } | |
121 // Some heuristic to decide when to drop packet or to restart everything | |
122 if (newseq > -(3 * MAXRTPPACKETSIN)) { | |
123 mp_msg(MSGT_NETWORK, MSGL_ERR, "Too Old packet (seq[%d]=%d seq=%d, newseq=%d)\n", rtpbuf.first, rtpbuf.seq[rtpbuf.first], seq, newseq); | |
124 return 0; // Yes, it is! | |
125 } | |
126 | |
127 mp_msg(MSGT_NETWORK, MSGL_ERR, "Underrun(seq[%d]=%d seq=%d, newseq=%d)\n", rtpbuf.first, rtpbuf.seq[rtpbuf.first], seq, newseq); | |
128 | |
129 rtp_cache_reset(seq); | |
130 goto feed; | |
131 } | |
132 | |
133 mp_msg(MSGT_NETWORK, MSGL_DBG4, "Out of Seq (seq[%d]=%d seq=%d, newseq=%d)\n", rtpbuf.first, rtpbuf.seq[rtpbuf.first], seq, newseq); | |
134 newseq = ( newseq + rtpbuf.first ) % MAXRTPPACKETSIN; | |
135 memcpy (rtpbuf.data[newseq], data, length); | |
136 rtpbuf.len[newseq] = length; | |
137 rtpbuf.seq[newseq] = seq; | |
138 | |
139 return 0; | |
140 | |
141 feed: | |
142 memcpy (buffer, data, length); | |
143 return length; | |
144 } | |
145 | |
146 // Get next packet in cache | |
147 // Look in cache to get first packet in sequence | |
148 static int rtp_get_next(int fd, char *buffer, int length) | |
149 { | |
150 int i; | |
151 unsigned short nextseq; | |
152 | |
153 // If we have empty buffer we loop to fill it | |
154 for (i=0; i < MAXRTPPACKETSIN -3; i++) { | |
155 if (rtpbuf.len[rtpbuf.first] != 0) break; | |
156 | |
157 length = rtp_cache(fd, buffer, length) ; | |
158 | |
159 // returns on first packet in sequence | |
160 if (length > 0) { | |
161 //mp_msg(MSGT_NETWORK, MSGL_DBG4, "Getting rtp [%d] %hu\n", i, rtpbuf.first); | |
162 return length; | |
163 } else if (length < 0) break; | |
164 | |
165 // Only if length == 0 loop continues! | |
166 } | |
167 | |
168 i = rtpbuf.first; | |
169 while (rtpbuf.len[i] == 0) { | |
170 mp_msg(MSGT_NETWORK, MSGL_ERR, "Lost packet %hu\n", rtpbuf.seq[i]); | |
171 i = ( 1 + i ) % MAXRTPPACKETSIN; | |
172 if (rtpbuf.first == i) break; | |
173 } | |
174 rtpbuf.first = i; | |
175 | |
176 // Copy next non empty packet from cache | |
177 mp_msg(MSGT_NETWORK, MSGL_DBG4, "Getting rtp from cache [%d] %hu\n", rtpbuf.first, rtpbuf.seq[rtpbuf.first]); | |
178 memcpy (buffer, rtpbuf.data[rtpbuf.first], rtpbuf.len[rtpbuf.first]); | |
179 length = rtpbuf.len[rtpbuf.first]; // can be zero? | |
180 | |
181 // Reset fisrt slot and go next in cache | |
182 rtpbuf.len[rtpbuf.first] = 0; | |
183 nextseq = rtpbuf.seq[rtpbuf.first]; | |
184 rtpbuf.first = ( 1 + rtpbuf.first ) % MAXRTPPACKETSIN; | |
185 rtpbuf.seq[rtpbuf.first] = nextseq + 1; | |
186 | |
187 return length; | |
188 } | |
189 | |
190 | |
191 // Read next rtp packet using cache | |
192 int read_rtp_from_server(int fd, char *buffer, int length) { | |
193 // Following test is ASSERT (i.e. uneuseful if code is correct) | |
194 if(buffer==NULL || length<STREAM_BUFFER_SIZE) { | |
195 mp_msg(MSGT_NETWORK, MSGL_ERR, "RTP buffer invalid; no data return from network\n"); | |
196 return 0; | |
197 } | |
198 | |
199 // loop just to skip empty packets | |
200 while ((length = rtp_get_next(fd, buffer, length)) == 0) { | |
201 mp_msg(MSGT_NETWORK, MSGL_ERR, "Got empty packet from RTP cache!?\n"); | |
202 } | |
203 | |
204 return(length); | |
205 } | |
206 | |
207 // Start listening on a UDP port. If multicast, join the group. | |
208 static int rtp_open_socket( URL_t *url ) { | |
209 int socket_server_fd, rxsockbufsz; | |
210 int err, err_len; | |
211 fd_set set; | |
212 struct sockaddr_in server_address; | |
213 struct ip_mreq mcast; | |
214 struct timeval tv; | |
215 struct hostent *hp; | |
216 | |
217 mp_msg(MSGT_NETWORK,MSGL_V,"Listening for traffic on %s:%d ...\n", url->hostname, url->port ); | |
218 | |
219 socket_server_fd = socket(AF_INET, SOCK_DGRAM, 0); | |
220 // fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) | O_NONBLOCK ); | |
221 if( socket_server_fd==-1 ) { | |
222 mp_msg(MSGT_NETWORK,MSGL_ERR,"Failed to create socket\n"); | |
223 return -1; | |
224 } | |
225 | |
226 if( isalpha(url->hostname[0]) ) { | |
227 #ifndef HAVE_WINSOCK2 | |
228 hp =(struct hostent*)gethostbyname( url->hostname ); | |
229 if( hp==NULL ) { | |
230 mp_msg(MSGT_NETWORK,MSGL_ERR,"Counldn't resolve name: %s\n", url->hostname); | |
231 goto err_out; | |
232 } | |
233 memcpy( (void*)&server_address.sin_addr.s_addr, (void*)hp->h_addr_list[0], hp->h_length ); | |
234 #else | |
235 server_address.sin_addr.s_addr = htonl(INADDR_ANY); | |
236 #endif | |
237 } else { | |
238 #ifndef HAVE_WINSOCK2 | |
239 #ifdef USE_ATON | |
240 inet_aton(url->hostname, &server_address.sin_addr); | |
241 #else | |
242 inet_pton(AF_INET, url->hostname, &server_address.sin_addr); | |
243 #endif | |
244 #else | |
245 server_address.sin_addr.s_addr = htonl(INADDR_ANY); | |
246 #endif | |
247 } | |
248 server_address.sin_family=AF_INET; | |
249 server_address.sin_port=htons(url->port); | |
250 | |
251 if( bind( socket_server_fd, (struct sockaddr*)&server_address, sizeof(server_address) )==-1 ) { | |
252 #ifndef HAVE_WINSOCK2 | |
253 if( errno!=EINPROGRESS ) { | |
254 #else | |
255 if( WSAGetLastError() != WSAEINPROGRESS ) { | |
256 #endif | |
257 mp_msg(MSGT_NETWORK,MSGL_ERR,"Failed to connect to server\n"); | |
258 goto err_out; | |
259 } | |
260 } | |
261 | |
262 #ifdef HAVE_WINSOCK2 | |
263 if (isalpha(url->hostname[0])) { | |
264 hp =(struct hostent*)gethostbyname( url->hostname ); | |
265 if( hp==NULL ) { | |
266 mp_msg(MSGT_NETWORK,MSGL_ERR,"Counldn't resolve name: %s\n", url->hostname); | |
267 goto err_out; | |
268 } | |
269 memcpy( (void*)&server_address.sin_addr.s_addr, (void*)hp->h_addr, hp->h_length ); | |
270 } else { | |
271 unsigned int addr = inet_addr(url->hostname); | |
272 memcpy( (void*)&server_address.sin_addr, (void*)&addr, sizeof(addr) ); | |
273 } | |
274 #endif | |
275 | |
276 // Increase the socket rx buffer size to maximum -- this is UDP | |
277 rxsockbufsz = 240 * 1024; | |
278 if( setsockopt( socket_server_fd, SOL_SOCKET, SO_RCVBUF, &rxsockbufsz, sizeof(rxsockbufsz))) { | |
279 mp_msg(MSGT_NETWORK,MSGL_ERR,"Couldn't set receive socket buffer size\n"); | |
280 } | |
281 | |
282 if((ntohl(server_address.sin_addr.s_addr) >> 28) == 0xe) { | |
283 mcast.imr_multiaddr.s_addr = server_address.sin_addr.s_addr; | |
284 //mcast.imr_interface.s_addr = inet_addr("10.1.1.2"); | |
285 mcast.imr_interface.s_addr = 0; | |
286 if( setsockopt( socket_server_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mcast, sizeof(mcast))) { | |
287 mp_msg(MSGT_NETWORK,MSGL_ERR,"IP_ADD_MEMBERSHIP failed (do you have multicasting enabled in your kernel?)\n"); | |
288 goto err_out; | |
289 } | |
290 } | |
291 | |
292 tv.tv_sec = 0; | |
293 tv.tv_usec = (1 * 1000000); // 1 second timeout | |
294 FD_ZERO( &set ); | |
295 FD_SET( socket_server_fd, &set ); | |
296 err = select(socket_server_fd+1, &set, NULL, NULL, &tv); | |
297 if (err < 0) { | |
298 mp_msg(MSGT_NETWORK, MSGL_FATAL, "Select failed: %s\n", strerror(errno)); | |
299 goto err_out; | |
300 } | |
301 if (err == 0) { | |
302 mp_msg(MSGT_NETWORK,MSGL_ERR,"Timeout! No data from host %s\n", url->hostname ); | |
303 goto err_out; | |
304 } | |
305 err_len = sizeof( err ); | |
306 getsockopt( socket_server_fd, SOL_SOCKET, SO_ERROR, &err, &err_len ); | |
307 if( err ) { | |
308 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Socket error: %d\n", err ); | |
309 goto err_out; | |
310 } | |
311 return socket_server_fd; | |
312 | |
313 err_out: | |
314 closesocket(socket_server_fd); | |
315 return -1; | |
316 } | |
317 | |
318 static int rtp_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *streaming_ctrl ) { | |
319 return read_rtp_from_server( fd, buffer, size ); | |
320 } | |
321 | |
322 static int rtp_streaming_start( stream_t *stream, int raw_udp ) { | |
323 streaming_ctrl_t *streaming_ctrl; | |
324 int fd; | |
325 | |
326 if( stream==NULL ) return -1; | |
327 streaming_ctrl = stream->streaming_ctrl; | |
328 fd = stream->fd; | |
329 | |
330 if( fd<0 ) { | |
331 fd = rtp_open_socket( (streaming_ctrl->url) ); | |
332 if( fd<0 ) return -1; | |
333 stream->fd = fd; | |
334 } | |
335 | |
336 if(raw_udp) | |
337 streaming_ctrl->streaming_read = nop_streaming_read; | |
338 else | |
339 streaming_ctrl->streaming_read = rtp_streaming_read; | |
340 streaming_ctrl->streaming_seek = nop_streaming_seek; | |
341 streaming_ctrl->prebuffer_size = 64*1024; // 64 KBytes | |
342 streaming_ctrl->buffering = 0; | |
343 streaming_ctrl->status = streaming_playing_e; | |
344 return 0; | |
345 } | |
346 | |
347 | |
348 static int getrtp2(int fd, struct rtpheader *rh, char** data, int* lengthData) { | |
349 static char buf[1600]; | |
350 unsigned int intP; | |
351 char* charP = (char*) &intP; | |
352 int headerSize; | |
353 int lengthPacket; | |
354 lengthPacket=recv(fd,buf,1590,0); | |
355 if (lengthPacket<0) | |
356 mp_msg(MSGT_NETWORK,MSGL_ERR,"rtp: socket read error\n"); | |
357 else if (lengthPacket<12) | |
358 mp_msg(MSGT_NETWORK,MSGL_ERR,"rtp: packet too small (%d) to be an rtp frame (>12bytes)\n", lengthPacket); | |
359 if(lengthPacket<12) { | |
360 *lengthData = 0; | |
361 return 0; | |
362 } | |
363 rh->b.v = (unsigned int) ((buf[0]>>6)&0x03); | |
364 rh->b.p = (unsigned int) ((buf[0]>>5)&0x01); | |
365 rh->b.x = (unsigned int) ((buf[0]>>4)&0x01); | |
366 rh->b.cc = (unsigned int) ((buf[0]>>0)&0x0f); | |
367 rh->b.m = (unsigned int) ((buf[1]>>7)&0x01); | |
368 rh->b.pt = (unsigned int) ((buf[1]>>0)&0x7f); | |
369 intP = 0; | |
370 memcpy(charP+2,&buf[2],2); | |
371 rh->b.sequence = ntohl(intP); | |
372 intP = 0; | |
373 memcpy(charP,&buf[4],4); | |
374 rh->timestamp = ntohl(intP); | |
375 | |
376 headerSize = 12 + 4*rh->b.cc; /* in bytes */ | |
377 | |
378 *lengthData = lengthPacket - headerSize; | |
379 *data = (char*) buf + headerSize; | |
380 | |
381 // mp_msg(MSGT_NETWORK,MSGL_DBG2,"Reading rtp: v=%x p=%x x=%x cc=%x m=%x pt=%x seq=%x ts=%x lgth=%d\n",rh->b.v,rh->b.p,rh->b.x,rh->b.cc,rh->b.m,rh->b.pt,rh->b.sequence,rh->timestamp,lengthPacket); | |
382 | |
383 return(0); | |
384 } | |
385 | |
386 | |
387 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) { | |
388 URL_t *url; | |
389 int udp = 0; | |
390 | |
391 mp_msg(MSGT_OPEN, MSGL_INFO, "STREAM_RTP, URL: %s\n", stream->url); | |
392 stream->streaming_ctrl = streaming_ctrl_new(); | |
393 if( stream->streaming_ctrl==NULL ) { | |
394 return STREAM_ERROR; | |
395 } | |
396 stream->streaming_ctrl->bandwidth = network_bandwidth; | |
397 url = url_new(stream->url); | |
398 stream->streaming_ctrl->url = check4proxies(url); | |
399 | |
400 if( url->port==0 ) { | |
401 mp_msg(MSGT_NETWORK,MSGL_ERR,"You must enter a port number for RTP and UDP streams!\n"); | |
402 goto fail; | |
403 } | |
404 if(!strncmp(stream->url, "udp", 3)) | |
405 udp = 1; | |
406 | |
407 if(rtp_streaming_start(stream, udp) < 0) { | |
408 mp_msg(MSGT_NETWORK,MSGL_ERR,"rtp_streaming_start(rtp) failed\n"); | |
409 goto fail; | |
410 } | |
411 | |
412 stream->type = STREAMTYPE_STREAM; | |
413 fixup_network_stream_cache(stream); | |
414 return STREAM_OK; | |
415 | |
416 fail: | |
417 streaming_ctrl_free( stream->streaming_ctrl ); | |
418 stream->streaming_ctrl = NULL; | |
419 return STREAM_UNSUPORTED; | |
420 } | |
421 | |
422 | |
423 stream_info_t stream_info_rtp_udp = { | |
424 "mpeg rtp and upd streaming", | |
425 "rtp and udp", | |
426 "Dave Chapman", | |
427 "native rtp support", | |
428 open_s, | |
429 {"rtp", "udp", NULL}, | |
430 NULL, | |
431 0 // Urls are an option string | |
432 }; | |
433 | |
434 |