annotate stream/rtp.c @ 30643:81099607ec11

Add names to anonymous structs sh_common, sh_audio, sh_video, sh_sub.
author diego
date Sun, 21 Feb 2010 20:02:50 +0000
parents 0f1b5b68af32
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
0783dd397f74 CVS --> Subversion in copyright notices
diego
parents: 18466
diff changeset
3 * Modified for use with MPlayer, for details see the changelog at
0783dd397f74 CVS --> Subversion in copyright notices
diego
parents: 18466
diff changeset
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
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
8 #include <stdlib.h>
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
9 #include <string.h>
3716
f3f2566fa4aa FreeBSD fix
nexus
parents: 3686
diff changeset
10 #include <unistd.h>
3686
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
11 #include <stdlib.h>
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
12 #include <stdio.h>
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
13 #include <sys/types.h>
15585
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
14 #include <ctype.h>
27472
c0b233cd30ca Revert moving closesocket definition and network headers to network.h.
diego
parents: 27464
diff changeset
15 #include "config.h"
28402
c884d17bd005 Convert HAVE_WINSOCK2_H into a 0/1 definition.
diego
parents: 27473
diff changeset
16 #if !HAVE_WINSOCK2_H
27472
c0b233cd30ca Revert moving closesocket definition and network headers to network.h.
diego
parents: 27464
diff changeset
17 #include <netinet/in.h>
c0b233cd30ca Revert moving closesocket definition and network headers to network.h.
diego
parents: 27464
diff changeset
18 #include <sys/socket.h>
c0b233cd30ca Revert moving closesocket definition and network headers to network.h.
diego
parents: 27464
diff changeset
19 #include <arpa/inet.h>
c0b233cd30ca Revert moving closesocket definition and network headers to network.h.
diego
parents: 27464
diff changeset
20 #else
c0b233cd30ca Revert moving closesocket definition and network headers to network.h.
diego
parents: 27464
diff changeset
21 #include <winsock2.h>
c0b233cd30ca Revert moving closesocket definition and network headers to network.h.
diego
parents: 27464
diff changeset
22 #include <ws2tcpip.h>
c0b233cd30ca Revert moving closesocket definition and network headers to network.h.
diego
parents: 27464
diff changeset
23 #endif
27464
be85aae103f2 Move duplicated '#define closesocket close' into network.h along with
diego
parents: 27461
diff changeset
24 #include <errno.h>
27473
ae5da477539e Move '#define closesocket close' preprocessor directive to a common place
diego
parents: 27472
diff changeset
25 #include "network.h"
15585
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
26 #include "stream.h"
3686
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
27
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
28 /* MPEG-2 TS RTP stack */
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
29
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
30 #define DEBUG 1
19315
4591b597b62c few cosmectic changes to remove duplication
ben
parents: 19313
diff changeset
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
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
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
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
41
19319
c565b6ac1d2a moved some definitions from rtp.h to rtp.c as they're not exported or used anywhere else
ben
parents: 19318
diff changeset
42 struct rtpbits {
c565b6ac1d2a moved some definitions from rtp.h to rtp.c as they're not exported or used anywhere else
ben
parents: 19318
diff changeset
43 unsigned int v:2; /* version: 2 */
c565b6ac1d2a moved some definitions from rtp.h to rtp.c as they're not exported or used anywhere else
ben
parents: 19318
diff changeset
44 unsigned int p:1; /* is there padding appended: 0 */
c565b6ac1d2a moved some definitions from rtp.h to rtp.c as they're not exported or used anywhere else
ben
parents: 19318
diff changeset
45 unsigned int x:1; /* number of extension headers: 0 */
c565b6ac1d2a moved some definitions from rtp.h to rtp.c as they're not exported or used anywhere else
ben
parents: 19318
diff changeset
46 unsigned int cc:4; /* number of CSRC identifiers: 0 */
c565b6ac1d2a moved some definitions from rtp.h to rtp.c as they're not exported or used anywhere else
ben
parents: 19318
diff changeset
47 unsigned int m:1; /* marker: 0 */
c565b6ac1d2a moved some definitions from rtp.h to rtp.c as they're not exported or used anywhere else
ben
parents: 19318
diff changeset
48 unsigned int pt:7; /* payload type: 33 for MPEG2 TS - RFC 1890 */
c565b6ac1d2a moved some definitions from rtp.h to rtp.c as they're not exported or used anywhere else
ben
parents: 19318
diff changeset
49 unsigned int sequence:16; /* sequence number: random */
c565b6ac1d2a moved some definitions from rtp.h to rtp.c as they're not exported or used anywhere else
ben
parents: 19318
diff changeset
50 };
c565b6ac1d2a moved some definitions from rtp.h to rtp.c as they're not exported or used anywhere else
ben
parents: 19318
diff changeset
51
c565b6ac1d2a moved some definitions from rtp.h to rtp.c as they're not exported or used anywhere else
ben
parents: 19318
diff changeset
52 struct rtpheader { /* in network byte order */
c565b6ac1d2a moved some definitions from rtp.h to rtp.c as they're not exported or used anywhere else
ben
parents: 19318
diff changeset
53 struct rtpbits b;
c565b6ac1d2a moved some definitions from rtp.h to rtp.c as they're not exported or used anywhere else
ben
parents: 19318
diff changeset
54 int timestamp; /* start: random */
c565b6ac1d2a moved some definitions from rtp.h to rtp.c as they're not exported or used anywhere else
ben
parents: 19318
diff changeset
55 int ssrc; /* random */
c565b6ac1d2a moved some definitions from rtp.h to rtp.c as they're not exported or used anywhere else
ben
parents: 19318
diff changeset
56 };
c565b6ac1d2a moved some definitions from rtp.h to rtp.c as they're not exported or used anywhere else
ben
parents: 19318
diff changeset
57
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
58 struct rtpbuffer
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
59 {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
60 unsigned char data[MAXRTPPACKETSIN][STREAM_BUFFER_SIZE];
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
61 unsigned short seq[MAXRTPPACKETSIN];
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
62 unsigned short len[MAXRTPPACKETSIN];
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
63 unsigned short first;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
64 };
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
65 static struct rtpbuffer rtpbuf;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
66
19319
c565b6ac1d2a moved some definitions from rtp.h to rtp.c as they're not exported or used anywhere else
ben
parents: 19318
diff changeset
67 static int getrtp2(int fd, struct rtpheader *rh, char** data, int* lengthData);
c565b6ac1d2a moved some definitions from rtp.h to rtp.c as they're not exported or used anywhere else
ben
parents: 19318
diff changeset
68
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
69 // RTP Reordering functions
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
70 // Algorithm works as follows:
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
71 // 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
72 // 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
73 // 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
74 // and keeps track of expected sequence
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
75
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
76 // Initialize rtp cache
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
77 static void rtp_cache_reset(unsigned short seq)
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
78 {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
79 int i;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
80
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
81 rtpbuf.first = 0;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
82 rtpbuf.seq[0] = ++seq;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
83
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
84 for (i=0; i<MAXRTPPACKETSIN; i++) {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
85 rtpbuf.len[i] = 0;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
86 }
15585
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
87 }
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
88
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
89 // 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
90 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
91 {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
92 struct rtpheader rh;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
93 int newseq;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
94 char *data;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
95 unsigned short seq;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
96 static int is_first = 1;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
97
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
98 getrtp2(fd, &rh, &data, &length);
18805
1f8d9010ce2f exit() is not allowed in any other function than main(); removed
nicodvb
parents: 18804
diff changeset
99 if(!length)
1f8d9010ce2f exit() is not allowed in any other function than main(); removed
nicodvb
parents: 18804
diff changeset
100 return 0;
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
101 seq = rh.b.sequence;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
102
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
103 newseq = seq - rtpbuf.seq[rtpbuf.first];
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
104
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
105 if ((newseq == 0) || is_first)
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
106 {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
107 is_first = 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
108
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
109 //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
110 rtpbuf.first = ( 1 + rtpbuf.first ) % MAXRTPPACKETSIN;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
111 rtpbuf.seq[rtpbuf.first] = ++seq;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
112 goto feed;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
113 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
114
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
115 if (newseq > MAXRTPPACKETSIN)
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
116 {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
117 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
118 rtp_cache_reset(seq);
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
119 goto feed;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
120 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
121
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
122 if (newseq < 0)
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
123 {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
124 int i;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
125
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
126 // 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
127 for (i=0; i<MAXRTPPACKETSIN; i++) {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
128 if (rtpbuf.seq[i] == seq) {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
129 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
130 return 0; // Yes, it is!
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
131 }
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 // 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
134 if (newseq > -(3 * MAXRTPPACKETSIN)) {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
135 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
136 return 0; // Yes, it is!
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
137 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
138
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
139 mp_msg(MSGT_NETWORK, MSGL_ERR, "Underrun(seq[%d]=%d seq=%d, newseq=%d)\n", rtpbuf.first, rtpbuf.seq[rtpbuf.first], seq, newseq);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
140
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
141 rtp_cache_reset(seq);
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
142 goto feed;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
143 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
144
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
145 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
146 newseq = ( newseq + rtpbuf.first ) % MAXRTPPACKETSIN;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
147 memcpy (rtpbuf.data[newseq], data, length);
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
148 rtpbuf.len[newseq] = length;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
149 rtpbuf.seq[newseq] = seq;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
150
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
151 return 0;
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 feed:
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
154 memcpy (buffer, data, length);
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
155 return length;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
156 }
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
157
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
158 // Get next packet in cache
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
159 // 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
160 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
161 {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
162 int i;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
163 unsigned short nextseq;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
164
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
165 // 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
166 for (i=0; i < MAXRTPPACKETSIN -3; i++) {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
167 if (rtpbuf.len[rtpbuf.first] != 0) break;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
168
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
169 length = rtp_cache(fd, buffer, length) ;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
170
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
171 // returns on first packet in sequence
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
172 if (length > 0) {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
173 //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
174 return length;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
175 } else if (length < 0) break;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
176
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
177 // Only if length == 0 loop continues!
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
178 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
179
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
180 i = rtpbuf.first;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
181 while (rtpbuf.len[i] == 0) {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
182 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
183 i = ( 1 + i ) % MAXRTPPACKETSIN;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
184 if (rtpbuf.first == i) break;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
185 }
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
186 rtpbuf.first = i;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
187
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
188 // Copy next non empty packet from cache
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
189 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
190 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
191 length = rtpbuf.len[rtpbuf.first]; // can be zero?
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
192
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
193 // Reset fisrt slot and go next in cache
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
194 rtpbuf.len[rtpbuf.first] = 0;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
195 nextseq = rtpbuf.seq[rtpbuf.first];
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
196 rtpbuf.first = ( 1 + rtpbuf.first ) % MAXRTPPACKETSIN;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
197 rtpbuf.seq[rtpbuf.first] = nextseq + 1;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
198
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
199 return length;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
200 }
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
201
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
202
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
203 // 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
204 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
205 // 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
206 if(buffer==NULL || length<STREAM_BUFFER_SIZE) {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
207 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
208 return 0;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
209 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
210
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
211 // loop just to skip empty packets
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
212 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
213 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
214 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28402
diff changeset
215
26756
c43ce7268677 cosmetics: Remove useless parentheses from return statements.
diego
parents: 19319
diff changeset
216 return length;
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
217 }
15585
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
218
18803
c56ae984dbc1 static-ized a couple of functions
nicodvb
parents: 18802
diff changeset
219 static int getrtp2(int fd, struct rtpheader *rh, char** data, int* lengthData) {
3686
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
220 static char buf[1600];
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
221 unsigned int intP;
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
222 char* charP = (char*) &intP;
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
223 int headerSize;
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
224 int lengthPacket;
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
225 lengthPacket=recv(fd,buf,1590,0);
18805
1f8d9010ce2f exit() is not allowed in any other function than main(); removed
nicodvb
parents: 18804
diff changeset
226 if (lengthPacket<0)
18804
2cc4eeaa0627 fprintf -> ms_msg
nicodvb
parents: 18803
diff changeset
227 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
228 else if (lengthPacket<12)
18804
2cc4eeaa0627 fprintf -> ms_msg
nicodvb
parents: 18803
diff changeset
229 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
230 if(lengthPacket<12) {
1f8d9010ce2f exit() is not allowed in any other function than main(); removed
nicodvb
parents: 18804
diff changeset
231 *lengthData = 0;
1f8d9010ce2f exit() is not allowed in any other function than main(); removed
nicodvb
parents: 18804
diff changeset
232 return 0;
3686
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
233 }
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
234 rh->b.v = (unsigned int) ((buf[0]>>6)&0x03);
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
235 rh->b.p = (unsigned int) ((buf[0]>>5)&0x01);
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
236 rh->b.x = (unsigned int) ((buf[0]>>4)&0x01);
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
237 rh->b.cc = (unsigned int) ((buf[0]>>0)&0x0f);
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
238 rh->b.m = (unsigned int) ((buf[1]>>7)&0x01);
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
239 rh->b.pt = (unsigned int) ((buf[1]>>0)&0x7f);
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
240 intP = 0;
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
241 memcpy(charP+2,&buf[2],2);
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
242 rh->b.sequence = ntohl(intP);
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
243 intP = 0;
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
244 memcpy(charP,&buf[4],4);
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
245 rh->timestamp = ntohl(intP);
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
246
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
247 headerSize = 12 + 4*rh->b.cc; /* in bytes */
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
248
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
249 *lengthData = lengthPacket - headerSize;
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
250 *data = (char*) buf + headerSize;
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
251
18804
2cc4eeaa0627 fprintf -> ms_msg
nicodvb
parents: 18803
diff changeset
252 // 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
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
253
26756
c43ce7268677 cosmetics: Remove useless parentheses from return statements.
diego
parents: 19319
diff changeset
254 return 0;
3686
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
255 }