Mercurial > mplayer.hg
annotate stream/rtp.c @ 19316:81b239327a3e
fix compilation of librtsp
author | ben |
---|---|
date | Fri, 04 Aug 2006 18:44:52 +0000 |
parents | 4591b597b62c |
children | a3ddd3320b47 |
rev | line source |
---|---|
15178
8dd7a656eaf8
Mark modified imported files as such to comply more closely with GPL ¡ø2a.
diego
parents:
12799
diff
changeset
|
1 /* Imported from the dvbstream-0.2 project |
8dd7a656eaf8
Mark modified imported files as such to comply more closely with GPL ¡ø2a.
diego
parents:
12799
diff
changeset
|
2 * |
18783 | 3 * Modified for use with MPlayer, for details see the changelog at |
4 * http://svn.mplayerhq.hu/mplayer/trunk/ | |
15178
8dd7a656eaf8
Mark modified imported files as such to comply more closely with GPL ¡ø2a.
diego
parents:
12799
diff
changeset
|
5 * $Id$ |
8dd7a656eaf8
Mark modified imported files as such to comply more closely with GPL ¡ø2a.
diego
parents:
12799
diff
changeset
|
6 */ |
8dd7a656eaf8
Mark modified imported files as such to comply more closely with GPL ¡ø2a.
diego
parents:
12799
diff
changeset
|
7 |
3686 | 8 #include <stdlib.h> |
9 #include <string.h> | |
3716 | 10 #include <unistd.h> |
3686 | 11 #include <stdlib.h> |
12 #include <stdio.h> | |
13 #include <sys/types.h> | |
15585 | 14 #include <ctype.h> |
10281 | 15 #include "config.h" |
16 #ifndef HAVE_WINSOCK2 | |
17 #include <netinet/in.h> | |
3686 | 18 #include <sys/socket.h> |
19 #include <arpa/inet.h> | |
15585 | 20 #define closesocket close |
10281 | 21 #else |
22 #include <winsock2.h> | |
23 #include <ws2tcpip.h> | |
24 #endif | |
15585 | 25 #include <errno.h> |
26 #include "stream.h" | |
3686 | 27 |
28 /* MPEG-2 TS RTP stack */ | |
29 | |
30 #define DEBUG 1 | |
19315 | 31 #include "mp_msg.h" |
18802
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
32 #include "rtp.h" |
15585 | 33 |
18802
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
34 // RTP reorder routines |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
35 // Also handling of repeated UDP packets (a bug of ExtremeNetworks switches firmware) |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
36 // rtpreord procedures |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
37 // write rtp packets in cache |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
38 // get rtp packets reordered |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
39 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
40 #define MAXRTPPACKETSIN 32 // The number of max packets being reordered |
15585 | 41 |
18802
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
42 struct rtpbuffer |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
43 { |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
44 unsigned char data[MAXRTPPACKETSIN][STREAM_BUFFER_SIZE]; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
45 unsigned short seq[MAXRTPPACKETSIN]; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
46 unsigned short len[MAXRTPPACKETSIN]; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
47 unsigned short first; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
48 }; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
49 static struct rtpbuffer rtpbuf; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
50 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
51 // RTP Reordering functions |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
52 // Algorithm works as follows: |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
53 // If next packet is in sequence just copy it to buffer |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
54 // Otherwise copy it in cache according to its sequence number |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
55 // Cache is a circular array where "rtpbuf.first" points to next sequence slot |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
56 // and keeps track of expected sequence |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
57 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
58 // Initialize rtp cache |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
59 static void rtp_cache_reset(unsigned short seq) |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
60 { |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
61 int i; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
62 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
63 rtpbuf.first = 0; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
64 rtpbuf.seq[0] = ++seq; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
65 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
66 for (i=0; i<MAXRTPPACKETSIN; i++) { |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
67 rtpbuf.len[i] = 0; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
68 } |
15585 | 69 } |
70 | |
18802
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
71 // Write in a cache the rtp packet in right rtp sequence order |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
72 static int rtp_cache(int fd, char *buffer, int length) |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
73 { |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
74 struct rtpheader rh; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
75 int newseq; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
76 char *data; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
77 unsigned short seq; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
78 static int is_first = 1; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
79 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
80 getrtp2(fd, &rh, &data, &length); |
18805
1f8d9010ce2f
exit() is not allowed in any other function than main(); removed
nicodvb
parents:
18804
diff
changeset
|
81 if(!length) |
1f8d9010ce2f
exit() is not allowed in any other function than main(); removed
nicodvb
parents:
18804
diff
changeset
|
82 return 0; |
18802
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
83 seq = rh.b.sequence; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
84 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
85 newseq = seq - rtpbuf.seq[rtpbuf.first]; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
86 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
87 if ((newseq == 0) || is_first) |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
88 { |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
89 is_first = 0; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
90 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
91 //mp_msg(MSGT_NETWORK, MSGL_DBG4, "RTP (seq[%d]=%d seq=%d, newseq=%d)\n", rtpbuf.first, rtpbuf.seq[rtpbuf.first], seq, newseq); |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
92 rtpbuf.first = ( 1 + rtpbuf.first ) % MAXRTPPACKETSIN; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
93 rtpbuf.seq[rtpbuf.first] = ++seq; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
94 goto feed; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
95 } |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
96 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
97 if (newseq > MAXRTPPACKETSIN) |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
98 { |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
99 mp_msg(MSGT_NETWORK, MSGL_DBG2, "Overrun(seq[%d]=%d seq=%d, newseq=%d)\n", rtpbuf.first, rtpbuf.seq[rtpbuf.first], seq, newseq); |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
100 rtp_cache_reset(seq); |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
101 goto feed; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
102 } |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
103 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
104 if (newseq < 0) |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
105 { |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
106 int i; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
107 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
108 // Is it a stray packet re-sent to network? |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
109 for (i=0; i<MAXRTPPACKETSIN; i++) { |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
110 if (rtpbuf.seq[i] == seq) { |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
111 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); |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
112 return 0; // Yes, it is! |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
113 } |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
114 } |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
115 // Some heuristic to decide when to drop packet or to restart everything |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
116 if (newseq > -(3 * MAXRTPPACKETSIN)) { |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
117 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); |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
118 return 0; // Yes, it is! |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
119 } |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
120 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
121 mp_msg(MSGT_NETWORK, MSGL_ERR, "Underrun(seq[%d]=%d seq=%d, newseq=%d)\n", rtpbuf.first, rtpbuf.seq[rtpbuf.first], seq, newseq); |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
122 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
123 rtp_cache_reset(seq); |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
124 goto feed; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
125 } |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
126 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
127 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); |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
128 newseq = ( newseq + rtpbuf.first ) % MAXRTPPACKETSIN; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
129 memcpy (rtpbuf.data[newseq], data, length); |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
130 rtpbuf.len[newseq] = length; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
131 rtpbuf.seq[newseq] = seq; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
132 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
133 return 0; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
134 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
135 feed: |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
136 memcpy (buffer, data, length); |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
137 return length; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
138 } |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
139 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
140 // Get next packet in cache |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
141 // Look in cache to get first packet in sequence |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
142 static int rtp_get_next(int fd, char *buffer, int length) |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
143 { |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
144 int i; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
145 unsigned short nextseq; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
146 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
147 // If we have empty buffer we loop to fill it |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
148 for (i=0; i < MAXRTPPACKETSIN -3; i++) { |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
149 if (rtpbuf.len[rtpbuf.first] != 0) break; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
150 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
151 length = rtp_cache(fd, buffer, length) ; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
152 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
153 // returns on first packet in sequence |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
154 if (length > 0) { |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
155 //mp_msg(MSGT_NETWORK, MSGL_DBG4, "Getting rtp [%d] %hu\n", i, rtpbuf.first); |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
156 return length; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
157 } else if (length < 0) break; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
158 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
159 // Only if length == 0 loop continues! |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
160 } |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
161 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
162 i = rtpbuf.first; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
163 while (rtpbuf.len[i] == 0) { |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
164 mp_msg(MSGT_NETWORK, MSGL_ERR, "Lost packet %hu\n", rtpbuf.seq[i]); |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
165 i = ( 1 + i ) % MAXRTPPACKETSIN; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
166 if (rtpbuf.first == i) break; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
167 } |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
168 rtpbuf.first = i; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
169 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
170 // Copy next non empty packet from cache |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
171 mp_msg(MSGT_NETWORK, MSGL_DBG4, "Getting rtp from cache [%d] %hu\n", rtpbuf.first, rtpbuf.seq[rtpbuf.first]); |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
172 memcpy (buffer, rtpbuf.data[rtpbuf.first], rtpbuf.len[rtpbuf.first]); |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
173 length = rtpbuf.len[rtpbuf.first]; // can be zero? |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
174 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
175 // Reset fisrt slot and go next in cache |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
176 rtpbuf.len[rtpbuf.first] = 0; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
177 nextseq = rtpbuf.seq[rtpbuf.first]; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
178 rtpbuf.first = ( 1 + rtpbuf.first ) % MAXRTPPACKETSIN; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
179 rtpbuf.seq[rtpbuf.first] = nextseq + 1; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
180 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
181 return length; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
182 } |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
183 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
184 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
185 // Read next rtp packet using cache |
18829
317e0fd394c5
added new native rtsp demuxer code for mpeg-ts over rtp (now both real and non-real servers should be handled)
ben
parents:
18805
diff
changeset
|
186 int read_rtp_from_server(int fd, char *buffer, int length) { |
18802
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
187 // Following test is ASSERT (i.e. uneuseful if code is correct) |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
188 if(buffer==NULL || length<STREAM_BUFFER_SIZE) { |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
189 mp_msg(MSGT_NETWORK, MSGL_ERR, "RTP buffer invalid; no data return from network\n"); |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
190 return 0; |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
191 } |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
192 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
193 // loop just to skip empty packets |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
194 while ((length = rtp_get_next(fd, buffer, length)) == 0) { |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
195 mp_msg(MSGT_NETWORK, MSGL_ERR, "Got empty packet from RTP cache!?\n"); |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
196 } |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
197 |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
198 return(length); |
b52ed08b17ad
rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents:
18783
diff
changeset
|
199 } |
15585 | 200 |
201 // Start listening on a UDP port. If multicast, join the group. | |
19313
0792ad01e9bf
split rtp stack, udp input layer and rtp input layer from rtp.c
ben
parents:
19271
diff
changeset
|
202 int rtp_open_socket( URL_t *url ) { |
15585 | 203 int socket_server_fd, rxsockbufsz; |
204 int err, err_len; | |
205 fd_set set; | |
206 struct sockaddr_in server_address; | |
207 struct ip_mreq mcast; | |
208 struct timeval tv; | |
209 struct hostent *hp; | |
210 | |
211 mp_msg(MSGT_NETWORK,MSGL_V,"Listening for traffic on %s:%d ...\n", url->hostname, url->port ); | |
212 | |
213 socket_server_fd = socket(AF_INET, SOCK_DGRAM, 0); | |
214 // fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) | O_NONBLOCK ); | |
215 if( socket_server_fd==-1 ) { | |
216 mp_msg(MSGT_NETWORK,MSGL_ERR,"Failed to create socket\n"); | |
217 return -1; | |
218 } | |
219 | |
220 if( isalpha(url->hostname[0]) ) { | |
221 #ifndef HAVE_WINSOCK2 | |
222 hp =(struct hostent*)gethostbyname( url->hostname ); | |
223 if( hp==NULL ) { | |
224 mp_msg(MSGT_NETWORK,MSGL_ERR,"Counldn't resolve name: %s\n", url->hostname); | |
15760
5dcc5524cb68
fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents:
15585
diff
changeset
|
225 goto err_out; |
15585 | 226 } |
18466 | 227 memcpy( (void*)&server_address.sin_addr.s_addr, (void*)hp->h_addr_list[0], hp->h_length ); |
15585 | 228 #else |
229 server_address.sin_addr.s_addr = htonl(INADDR_ANY); | |
230 #endif | |
231 } else { | |
232 #ifndef HAVE_WINSOCK2 | |
233 #ifdef USE_ATON | |
234 inet_aton(url->hostname, &server_address.sin_addr); | |
235 #else | |
236 inet_pton(AF_INET, url->hostname, &server_address.sin_addr); | |
237 #endif | |
238 #else | |
239 server_address.sin_addr.s_addr = htonl(INADDR_ANY); | |
240 #endif | |
241 } | |
242 server_address.sin_family=AF_INET; | |
243 server_address.sin_port=htons(url->port); | |
244 | |
245 if( bind( socket_server_fd, (struct sockaddr*)&server_address, sizeof(server_address) )==-1 ) { | |
246 #ifndef HAVE_WINSOCK2 | |
247 if( errno!=EINPROGRESS ) { | |
248 #else | |
249 if( WSAGetLastError() != WSAEINPROGRESS ) { | |
250 #endif | |
251 mp_msg(MSGT_NETWORK,MSGL_ERR,"Failed to connect to server\n"); | |
15760
5dcc5524cb68
fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents:
15585
diff
changeset
|
252 goto err_out; |
15585 | 253 } |
254 } | |
255 | |
256 #ifdef HAVE_WINSOCK2 | |
257 if (isalpha(url->hostname[0])) { | |
258 hp =(struct hostent*)gethostbyname( url->hostname ); | |
259 if( hp==NULL ) { | |
260 mp_msg(MSGT_NETWORK,MSGL_ERR,"Counldn't resolve name: %s\n", url->hostname); | |
15760
5dcc5524cb68
fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents:
15585
diff
changeset
|
261 goto err_out; |
15585 | 262 } |
263 memcpy( (void*)&server_address.sin_addr.s_addr, (void*)hp->h_addr, hp->h_length ); | |
264 } else { | |
265 unsigned int addr = inet_addr(url->hostname); | |
266 memcpy( (void*)&server_address.sin_addr, (void*)&addr, sizeof(addr) ); | |
267 } | |
268 #endif | |
269 | |
270 // Increase the socket rx buffer size to maximum -- this is UDP | |
271 rxsockbufsz = 240 * 1024; | |
272 if( setsockopt( socket_server_fd, SOL_SOCKET, SO_RCVBUF, &rxsockbufsz, sizeof(rxsockbufsz))) { | |
273 mp_msg(MSGT_NETWORK,MSGL_ERR,"Couldn't set receive socket buffer size\n"); | |
274 } | |
275 | |
276 if((ntohl(server_address.sin_addr.s_addr) >> 28) == 0xe) { | |
277 mcast.imr_multiaddr.s_addr = server_address.sin_addr.s_addr; | |
278 //mcast.imr_interface.s_addr = inet_addr("10.1.1.2"); | |
279 mcast.imr_interface.s_addr = 0; | |
280 if( setsockopt( socket_server_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mcast, sizeof(mcast))) { | |
281 mp_msg(MSGT_NETWORK,MSGL_ERR,"IP_ADD_MEMBERSHIP failed (do you have multicasting enabled in your kernel?)\n"); | |
15760
5dcc5524cb68
fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents:
15585
diff
changeset
|
282 goto err_out; |
15585 | 283 } |
284 } | |
285 | |
286 tv.tv_sec = 0; | |
287 tv.tv_usec = (1 * 1000000); // 1 second timeout | |
288 FD_ZERO( &set ); | |
289 FD_SET( socket_server_fd, &set ); | |
15760
5dcc5524cb68
fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents:
15585
diff
changeset
|
290 err = select(socket_server_fd+1, &set, NULL, NULL, &tv); |
5dcc5524cb68
fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents:
15585
diff
changeset
|
291 if (err < 0) { |
5dcc5524cb68
fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents:
15585
diff
changeset
|
292 mp_msg(MSGT_NETWORK, MSGL_FATAL, "Select failed: %s\n", strerror(errno)); |
5dcc5524cb68
fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents:
15585
diff
changeset
|
293 goto err_out; |
5dcc5524cb68
fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents:
15585
diff
changeset
|
294 } |
5dcc5524cb68
fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents:
15585
diff
changeset
|
295 if (err == 0) { |
5dcc5524cb68
fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents:
15585
diff
changeset
|
296 mp_msg(MSGT_NETWORK,MSGL_ERR,"Timeout! No data from host %s\n", url->hostname ); |
5dcc5524cb68
fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents:
15585
diff
changeset
|
297 goto err_out; |
5dcc5524cb68
fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents:
15585
diff
changeset
|
298 } |
15585 | 299 err_len = sizeof( err ); |
300 getsockopt( socket_server_fd, SOL_SOCKET, SO_ERROR, &err, &err_len ); | |
301 if( err ) { | |
302 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Socket error: %d\n", err ); | |
15760
5dcc5524cb68
fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents:
15585
diff
changeset
|
303 goto err_out; |
15585 | 304 } |
305 return socket_server_fd; | |
15760
5dcc5524cb68
fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents:
15585
diff
changeset
|
306 |
5dcc5524cb68
fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents:
15585
diff
changeset
|
307 err_out: |
5dcc5524cb68
fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents:
15585
diff
changeset
|
308 closesocket(socket_server_fd); |
5dcc5524cb68
fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents:
15585
diff
changeset
|
309 return -1; |
15585 | 310 } |
311 | |
18803 | 312 static int getrtp2(int fd, struct rtpheader *rh, char** data, int* lengthData) { |
3686 | 313 static char buf[1600]; |
314 unsigned int intP; | |
315 char* charP = (char*) &intP; | |
316 int headerSize; | |
317 int lengthPacket; | |
318 lengthPacket=recv(fd,buf,1590,0); | |
18805
1f8d9010ce2f
exit() is not allowed in any other function than main(); removed
nicodvb
parents:
18804
diff
changeset
|
319 if (lengthPacket<0) |
18804 | 320 mp_msg(MSGT_NETWORK,MSGL_ERR,"rtp: socket read error\n"); |
18805
1f8d9010ce2f
exit() is not allowed in any other function than main(); removed
nicodvb
parents:
18804
diff
changeset
|
321 else if (lengthPacket<12) |
18804 | 322 mp_msg(MSGT_NETWORK,MSGL_ERR,"rtp: packet too small (%d) to be an rtp frame (>12bytes)\n", lengthPacket); |
18805
1f8d9010ce2f
exit() is not allowed in any other function than main(); removed
nicodvb
parents:
18804
diff
changeset
|
323 if(lengthPacket<12) { |
1f8d9010ce2f
exit() is not allowed in any other function than main(); removed
nicodvb
parents:
18804
diff
changeset
|
324 *lengthData = 0; |
1f8d9010ce2f
exit() is not allowed in any other function than main(); removed
nicodvb
parents:
18804
diff
changeset
|
325 return 0; |
3686 | 326 } |
327 rh->b.v = (unsigned int) ((buf[0]>>6)&0x03); | |
328 rh->b.p = (unsigned int) ((buf[0]>>5)&0x01); | |
329 rh->b.x = (unsigned int) ((buf[0]>>4)&0x01); | |
330 rh->b.cc = (unsigned int) ((buf[0]>>0)&0x0f); | |
331 rh->b.m = (unsigned int) ((buf[1]>>7)&0x01); | |
332 rh->b.pt = (unsigned int) ((buf[1]>>0)&0x7f); | |
333 intP = 0; | |
334 memcpy(charP+2,&buf[2],2); | |
335 rh->b.sequence = ntohl(intP); | |
336 intP = 0; | |
337 memcpy(charP,&buf[4],4); | |
338 rh->timestamp = ntohl(intP); | |
339 | |
340 headerSize = 12 + 4*rh->b.cc; /* in bytes */ | |
341 | |
342 *lengthData = lengthPacket - headerSize; | |
343 *data = (char*) buf + headerSize; | |
344 | |
18804 | 345 // 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); |
3686 | 346 |
347 return(0); | |
348 } |