annotate libmpdemux/rtp.c @ 18917:d9a75b26da6c

Add a new video pts tracking mode, enabled by option -correct-pts. This mode has the following differences: - Video timing is correct for streams with B frames, at least with some demuxers. - Video filters can modify frame timestamps and insert new frames, and removing frames is handled better than before. - Some things are known to break, it's not usable as the default yet. Things should work as before when the -correct-pts option is not used.
author uau
date Thu, 06 Jul 2006 06:58:17 +0000
parents 317e0fd394c5
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>
10281
54bcbf28698a Networking support under MinGW.
diego
parents: 7472
diff changeset
15 #include "config.h"
54bcbf28698a Networking support under MinGW.
diego
parents: 7472
diff changeset
16 #ifndef HAVE_WINSOCK2
54bcbf28698a Networking support under MinGW.
diego
parents: 7472
diff changeset
17 #include <netinet/in.h>
3686
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
18 #include <sys/socket.h>
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
19 #include <arpa/inet.h>
15585
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
20 #define closesocket close
10281
54bcbf28698a Networking support under MinGW.
diego
parents: 7472
diff changeset
21 #else
54bcbf28698a Networking support under MinGW.
diego
parents: 7472
diff changeset
22 #include <winsock2.h>
54bcbf28698a Networking support under MinGW.
diego
parents: 7472
diff changeset
23 #include <ws2tcpip.h>
54bcbf28698a Networking support under MinGW.
diego
parents: 7472
diff changeset
24 #endif
15585
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
25 #include <errno.h>
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
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
31 #include "rtp.h"
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
32
15585
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
33 extern int network_bandwidth;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
34
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
35
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
36 #define DEBUG 1
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
37 #include "../mp_msg.h"
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
38 #include "rtp.h"
15585
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
39
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
40 // RTP reorder routines
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
41 // 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
42 // rtpreord procedures
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
43 // write rtp packets in cache
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
44 // get rtp packets reordered
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
45
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
46 #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
47
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
48 struct rtpbuffer
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
49 {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
50 unsigned char data[MAXRTPPACKETSIN][STREAM_BUFFER_SIZE];
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
51 unsigned short seq[MAXRTPPACKETSIN];
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
52 unsigned short len[MAXRTPPACKETSIN];
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
53 unsigned short first;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
54 };
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
55 static struct rtpbuffer rtpbuf;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
56
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
57 // RTP Reordering functions
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
58 // Algorithm works as follows:
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
59 // 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
60 // 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
61 // 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
62 // and keeps track of expected sequence
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
63
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
64 // Initialize rtp cache
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
65 static void rtp_cache_reset(unsigned short seq)
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
66 {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
67 int i;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
68
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
69 rtpbuf.first = 0;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
70 rtpbuf.seq[0] = ++seq;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
71
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
72 for (i=0; i<MAXRTPPACKETSIN; i++) {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
73 rtpbuf.len[i] = 0;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
74 }
15585
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
75 }
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
76
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
77 // 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
78 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
79 {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
80 struct rtpheader rh;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
81 int newseq;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
82 char *data;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
83 unsigned short seq;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
84 static int is_first = 1;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
85
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
86 getrtp2(fd, &rh, &data, &length);
18805
1f8d9010ce2f exit() is not allowed in any other function than main(); removed
nicodvb
parents: 18804
diff changeset
87 if(!length)
1f8d9010ce2f exit() is not allowed in any other function than main(); removed
nicodvb
parents: 18804
diff changeset
88 return 0;
18802
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
89 seq = rh.b.sequence;
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 newseq = seq - rtpbuf.seq[rtpbuf.first];
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
92
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
93 if ((newseq == 0) || is_first)
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
94 {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
95 is_first = 0;
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 //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
98 rtpbuf.first = ( 1 + rtpbuf.first ) % MAXRTPPACKETSIN;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
99 rtpbuf.seq[rtpbuf.first] = ++seq;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
100 goto feed;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
101 }
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 if (newseq > MAXRTPPACKETSIN)
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
104 {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
105 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
106 rtp_cache_reset(seq);
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
107 goto feed;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
108 }
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
109
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
110 if (newseq < 0)
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
111 {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
112 int i;
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 // 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
115 for (i=0; i<MAXRTPPACKETSIN; i++) {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
116 if (rtpbuf.seq[i] == seq) {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
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);
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 // 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
122 if (newseq > -(3 * MAXRTPPACKETSIN)) {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
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);
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
124 return 0; // Yes, it is!
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_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
128
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
129 rtp_cache_reset(seq);
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
130 goto feed;
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 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
134 newseq = ( newseq + rtpbuf.first ) % MAXRTPPACKETSIN;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
135 memcpy (rtpbuf.data[newseq], data, length);
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
136 rtpbuf.len[newseq] = length;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
137 rtpbuf.seq[newseq] = seq;
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 return 0;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
140
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
141 feed:
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
142 memcpy (buffer, data, length);
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
143 return length;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
144 }
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
145
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
146 // Get next packet in cache
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
147 // 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
148 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
149 {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
150 int i;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
151 unsigned short nextseq;
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 // 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
154 for (i=0; i < MAXRTPPACKETSIN -3; i++) {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
155 if (rtpbuf.len[rtpbuf.first] != 0) break;
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 length = rtp_cache(fd, buffer, length) ;
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 // returns on first packet in sequence
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
160 if (length > 0) {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
161 //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
162 return length;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
163 } else if (length < 0) break;
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 // Only if length == 0 loop continues!
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
166 }
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 i = rtpbuf.first;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
169 while (rtpbuf.len[i] == 0) {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
170 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
171 i = ( 1 + i ) % MAXRTPPACKETSIN;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
172 if (rtpbuf.first == i) break;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
173 }
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
174 rtpbuf.first = i;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
175
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
176 // Copy next non empty packet from cache
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
177 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
178 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
179 length = rtpbuf.len[rtpbuf.first]; // can be zero?
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 // Reset fisrt slot and go next in cache
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
182 rtpbuf.len[rtpbuf.first] = 0;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
183 nextseq = rtpbuf.seq[rtpbuf.first];
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
184 rtpbuf.first = ( 1 + rtpbuf.first ) % MAXRTPPACKETSIN;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
185 rtpbuf.seq[rtpbuf.first] = nextseq + 1;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
186
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
187 return length;
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
188 }
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
189
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
190
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
191 // 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
192 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
193 // 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
194 if(buffer==NULL || length<STREAM_BUFFER_SIZE) {
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
195 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
196 return 0;
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
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
199 // loop just to skip empty packets
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
200 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
201 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
202 }
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
203
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
204 return(length);
b52ed08b17ad rtp reordering of packets; patch by Ernitron (ernitron@fastwebnet.it)
nicodvb
parents: 18783
diff changeset
205 }
15585
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
206
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
207 // Start listening on a UDP port. If multicast, join the group.
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
208 static int rtp_open_socket( URL_t *url ) {
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
209 int socket_server_fd, rxsockbufsz;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
210 int err, err_len;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
211 fd_set set;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
212 struct sockaddr_in server_address;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
213 struct ip_mreq mcast;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
214 struct timeval tv;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
215 struct hostent *hp;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
216
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
217 mp_msg(MSGT_NETWORK,MSGL_V,"Listening for traffic on %s:%d ...\n", url->hostname, url->port );
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
218
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
219 socket_server_fd = socket(AF_INET, SOCK_DGRAM, 0);
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
220 // fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) | O_NONBLOCK );
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
221 if( socket_server_fd==-1 ) {
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
222 mp_msg(MSGT_NETWORK,MSGL_ERR,"Failed to create socket\n");
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
223 return -1;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
224 }
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
225
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
226 if( isalpha(url->hostname[0]) ) {
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
227 #ifndef HAVE_WINSOCK2
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
228 hp =(struct hostent*)gethostbyname( url->hostname );
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
229 if( hp==NULL ) {
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
230 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
231 goto err_out;
15585
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
232 }
18466
028e4c7a749e posix compliance, dont use ancient legacy name
rfelker
parents: 15760
diff changeset
233 memcpy( (void*)&server_address.sin_addr.s_addr, (void*)hp->h_addr_list[0], hp->h_length );
15585
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
234 #else
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
235 server_address.sin_addr.s_addr = htonl(INADDR_ANY);
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
236 #endif
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
237 } else {
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
238 #ifndef HAVE_WINSOCK2
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
239 #ifdef USE_ATON
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
240 inet_aton(url->hostname, &server_address.sin_addr);
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
241 #else
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
242 inet_pton(AF_INET, url->hostname, &server_address.sin_addr);
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
243 #endif
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
244 #else
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
245 server_address.sin_addr.s_addr = htonl(INADDR_ANY);
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
246 #endif
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
247 }
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
248 server_address.sin_family=AF_INET;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
249 server_address.sin_port=htons(url->port);
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
250
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
251 if( bind( socket_server_fd, (struct sockaddr*)&server_address, sizeof(server_address) )==-1 ) {
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
252 #ifndef HAVE_WINSOCK2
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
253 if( errno!=EINPROGRESS ) {
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
254 #else
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
255 if( WSAGetLastError() != WSAEINPROGRESS ) {
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
256 #endif
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
257 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
258 goto err_out;
15585
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
259 }
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
260 }
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
261
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
262 #ifdef HAVE_WINSOCK2
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
263 if (isalpha(url->hostname[0])) {
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
264 hp =(struct hostent*)gethostbyname( url->hostname );
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
265 if( hp==NULL ) {
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
266 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
267 goto err_out;
15585
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
268 }
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
269 memcpy( (void*)&server_address.sin_addr.s_addr, (void*)hp->h_addr, hp->h_length );
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
270 } else {
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
271 unsigned int addr = inet_addr(url->hostname);
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
272 memcpy( (void*)&server_address.sin_addr, (void*)&addr, sizeof(addr) );
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
273 }
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
274 #endif
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
275
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
276 // Increase the socket rx buffer size to maximum -- this is UDP
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
277 rxsockbufsz = 240 * 1024;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
278 if( setsockopt( socket_server_fd, SOL_SOCKET, SO_RCVBUF, &rxsockbufsz, sizeof(rxsockbufsz))) {
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
279 mp_msg(MSGT_NETWORK,MSGL_ERR,"Couldn't set receive socket buffer size\n");
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
280 }
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
281
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
282 if((ntohl(server_address.sin_addr.s_addr) >> 28) == 0xe) {
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
283 mcast.imr_multiaddr.s_addr = server_address.sin_addr.s_addr;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
284 //mcast.imr_interface.s_addr = inet_addr("10.1.1.2");
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
285 mcast.imr_interface.s_addr = 0;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
286 if( setsockopt( socket_server_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mcast, sizeof(mcast))) {
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
287 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
288 goto err_out;
15585
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
289 }
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
290 }
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
291
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
292 tv.tv_sec = 0;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
293 tv.tv_usec = (1 * 1000000); // 1 second timeout
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
294 FD_ZERO( &set );
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
295 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
296 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
297 if (err < 0) {
5dcc5524cb68 fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents: 15585
diff changeset
298 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
299 goto err_out;
5dcc5524cb68 fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents: 15585
diff changeset
300 }
5dcc5524cb68 fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents: 15585
diff changeset
301 if (err == 0) {
5dcc5524cb68 fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents: 15585
diff changeset
302 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
303 goto err_out;
5dcc5524cb68 fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents: 15585
diff changeset
304 }
15585
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
305 err_len = sizeof( err );
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
306 getsockopt( socket_server_fd, SOL_SOCKET, SO_ERROR, &err, &err_len );
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
307 if( err ) {
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
308 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
309 goto err_out;
15585
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
310 }
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
311 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
312
5dcc5524cb68 fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents: 15585
diff changeset
313 err_out:
5dcc5524cb68 fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents: 15585
diff changeset
314 closesocket(socket_server_fd);
5dcc5524cb68 fix wrong usage of select() (based on patch by Selwyn Tang selwyn hectrix com),
reimar
parents: 15585
diff changeset
315 return -1;
15585
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
316 }
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
317
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
318 static int rtp_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *streaming_ctrl ) {
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
319 return read_rtp_from_server( fd, buffer, size );
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
320 }
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
321
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
322 static int rtp_streaming_start( stream_t *stream, int raw_udp ) {
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
323 streaming_ctrl_t *streaming_ctrl;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
324 int fd;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
325
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
326 if( stream==NULL ) return -1;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
327 streaming_ctrl = stream->streaming_ctrl;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
328 fd = stream->fd;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
329
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
330 if( fd<0 ) {
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
331 fd = rtp_open_socket( (streaming_ctrl->url) );
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
332 if( fd<0 ) return -1;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
333 stream->fd = fd;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
334 }
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
335
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
336 if(raw_udp)
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
337 streaming_ctrl->streaming_read = nop_streaming_read;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
338 else
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
339 streaming_ctrl->streaming_read = rtp_streaming_read;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
340 streaming_ctrl->streaming_seek = nop_streaming_seek;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
341 streaming_ctrl->prebuffer_size = 64*1024; // 64 KBytes
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
342 streaming_ctrl->buffering = 0;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
343 streaming_ctrl->status = streaming_playing_e;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
344 return 0;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
345 }
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
346
3686
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
347
18803
c56ae984dbc1 static-ized a couple of functions
nicodvb
parents: 18802
diff changeset
348 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
349 static char buf[1600];
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
350 unsigned int intP;
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
351 char* charP = (char*) &intP;
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
352 int headerSize;
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
353 int lengthPacket;
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
354 lengthPacket=recv(fd,buf,1590,0);
18805
1f8d9010ce2f exit() is not allowed in any other function than main(); removed
nicodvb
parents: 18804
diff changeset
355 if (lengthPacket<0)
18804
2cc4eeaa0627 fprintf -> ms_msg
nicodvb
parents: 18803
diff changeset
356 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
357 else if (lengthPacket<12)
18804
2cc4eeaa0627 fprintf -> ms_msg
nicodvb
parents: 18803
diff changeset
358 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
359 if(lengthPacket<12) {
1f8d9010ce2f exit() is not allowed in any other function than main(); removed
nicodvb
parents: 18804
diff changeset
360 *lengthData = 0;
1f8d9010ce2f exit() is not allowed in any other function than main(); removed
nicodvb
parents: 18804
diff changeset
361 return 0;
3686
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
362 }
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
363 rh->b.v = (unsigned int) ((buf[0]>>6)&0x03);
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
364 rh->b.p = (unsigned int) ((buf[0]>>5)&0x01);
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
365 rh->b.x = (unsigned int) ((buf[0]>>4)&0x01);
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
366 rh->b.cc = (unsigned int) ((buf[0]>>0)&0x0f);
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
367 rh->b.m = (unsigned int) ((buf[1]>>7)&0x01);
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
368 rh->b.pt = (unsigned int) ((buf[1]>>0)&0x7f);
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
369 intP = 0;
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
370 memcpy(charP+2,&buf[2],2);
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
371 rh->b.sequence = ntohl(intP);
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
372 intP = 0;
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
373 memcpy(charP,&buf[4],4);
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
374 rh->timestamp = ntohl(intP);
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
375
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
376 headerSize = 12 + 4*rh->b.cc; /* in bytes */
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
377
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
378 *lengthData = lengthPacket - headerSize;
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
379 *data = (char*) buf + headerSize;
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
380
18804
2cc4eeaa0627 fprintf -> ms_msg
nicodvb
parents: 18803
diff changeset
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);
3686
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
382
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
383 return(0);
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
384 }
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
385
bed6226ffb46 RTP support patch by Brian Kuschak <bkuschak@yahoo.com>
arpi
parents:
diff changeset
386
15585
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
387 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
388 URL_t *url;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
389 int udp = 0;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
390
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
391 mp_msg(MSGT_OPEN, MSGL_INFO, "STREAM_RTP, URL: %s\n", stream->url);
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
392 stream->streaming_ctrl = streaming_ctrl_new();
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
393 if( stream->streaming_ctrl==NULL ) {
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
394 return STREAM_ERROR;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
395 }
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
396 stream->streaming_ctrl->bandwidth = network_bandwidth;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
397 url = url_new(stream->url);
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
398 stream->streaming_ctrl->url = check4proxies(url);
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
399
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
400 if( url->port==0 ) {
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
401 mp_msg(MSGT_NETWORK,MSGL_ERR,"You must enter a port number for RTP and UDP streams!\n");
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
402 goto fail;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
403 }
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
404 if(!strncmp(stream->url, "udp", 3))
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
405 udp = 1;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
406
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
407 if(rtp_streaming_start(stream, udp) < 0) {
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
408 mp_msg(MSGT_NETWORK,MSGL_ERR,"rtp_streaming_start(rtp) failed\n");
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
409 goto fail;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
410 }
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
411
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
412 stream->type = STREAMTYPE_STREAM;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
413 fixup_network_stream_cache(stream);
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
414 return STREAM_OK;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
415
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
416 fail:
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
417 streaming_ctrl_free( stream->streaming_ctrl );
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
418 stream->streaming_ctrl = NULL;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
419 return STREAM_UNSUPORTED;
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
420 }
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
421
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
422
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
423 stream_info_t stream_info_rtp_udp = {
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
424 "mpeg rtp and upd streaming",
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
425 "rtp and udp",
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
426 "Dave Chapman",
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
427 "native rtp support",
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
428 open_s,
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
429 {"rtp", "udp", NULL},
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
430 NULL,
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
431 0 // Urls are an option string
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
432 };
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
433
281d155fb37f ported all network streams to the new API
nicodvb
parents: 15178
diff changeset
434