Statistics
| Branch: | Revision:

ffmpeg / libavformat / rtp.c @ e1315fb1

History | View | Annotate | Download (37.7 KB)

1 e309128f Fabrice Bellard
/*
2
 * RTP input/output format
3
 * Copyright (c) 2002 Fabrice Bellard.
4
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 e309128f Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 e309128f Fabrice Bellard
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 e309128f Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 e309128f Fabrice Bellard
 */
21
#include "avformat.h"
22 8b1ab7bf Fabrice Bellard
#include "mpegts.h"
23 d1ccf0e0 Romain Degez
#include "bitstream.h"
24 e309128f Fabrice Bellard
25
#include <unistd.h>
26 42572ef5 Ramiro Polla
#include "network.h"
27 e309128f Fabrice Bellard
28 4934884a Ryan Martell
#include "rtp_internal.h"
29 1d1be919 Ryan Martell
#include "rtp_h264.h"
30 4934884a Ryan Martell
31 e309128f Fabrice Bellard
//#define DEBUG
32
33
34
/* TODO: - add RTCP statistics reporting (should be optional).
35

36
         - add support for h263/mpeg4 packetized output : IDEA: send a
37
         buffer to 'rtp_write_packet' contains all the packets for ONE
38
         frame. Each packet should have a four byte header containing
39
         the length in big endian format (same trick as
40 115329f1 Diego Biurrun
         'url_open_dyn_packet_buf')
41 e309128f Fabrice Bellard
*/
42
43 d1ccf0e0 Romain Degez
/* from http://www.iana.org/assignments/rtp-parameters last updated 05 January 2005 */
44
AVRtpPayloadType_t AVRtpPayloadTypes[]=
45
{
46
  {0, "PCMU",        CODEC_TYPE_AUDIO,   CODEC_ID_PCM_MULAW, 8000, 1},
47
  {1, "Reserved",    CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
48
  {2, "Reserved",    CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
49
  {3, "GSM",         CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
50
  {4, "G723",        CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
51
  {5, "DVI4",        CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
52
  {6, "DVI4",        CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 16000, 1},
53
  {7, "LPC",         CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
54
  {8, "PCMA",        CODEC_TYPE_AUDIO,   CODEC_ID_PCM_ALAW, 8000, 1},
55
  {9, "G722",        CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
56
  {10, "L16",        CODEC_TYPE_AUDIO,   CODEC_ID_PCM_S16BE, 44100, 2},
57
  {11, "L16",        CODEC_TYPE_AUDIO,   CODEC_ID_PCM_S16BE, 44100, 1},
58 6c496518 Benjamin Larsson
  {12, "QCELP",      CODEC_TYPE_AUDIO,   CODEC_ID_QCELP, 8000, 1},
59 d1ccf0e0 Romain Degez
  {13, "CN",         CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
60
  {14, "MPA",        CODEC_TYPE_AUDIO,   CODEC_ID_MP2, 90000, -1},
61
  {15, "G728",       CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
62
  {16, "DVI4",       CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 11025, 1},
63
  {17, "DVI4",       CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 22050, 1},
64
  {18, "G729",       CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
65
  {19, "reserved",   CODEC_TYPE_AUDIO,   CODEC_ID_NONE, -1, -1},
66
  {20, "unassigned", CODEC_TYPE_AUDIO,   CODEC_ID_NONE, -1, -1},
67
  {21, "unassigned", CODEC_TYPE_AUDIO,   CODEC_ID_NONE, -1, -1},
68
  {22, "unassigned", CODEC_TYPE_AUDIO,   CODEC_ID_NONE, -1, -1},
69
  {23, "unassigned", CODEC_TYPE_AUDIO,   CODEC_ID_NONE, -1, -1},
70
  {24, "unassigned", CODEC_TYPE_VIDEO,   CODEC_ID_NONE, -1, -1},
71
  {25, "CelB",       CODEC_TYPE_VIDEO,   CODEC_ID_NONE, 90000, -1},
72
  {26, "JPEG",       CODEC_TYPE_VIDEO,   CODEC_ID_MJPEG, 90000, -1},
73
  {27, "unassigned", CODEC_TYPE_VIDEO,   CODEC_ID_NONE, -1, -1},
74
  {28, "nv",         CODEC_TYPE_VIDEO,   CODEC_ID_NONE, 90000, -1},
75
  {29, "unassigned", CODEC_TYPE_VIDEO,   CODEC_ID_NONE, -1, -1},
76
  {30, "unassigned", CODEC_TYPE_VIDEO,   CODEC_ID_NONE, -1, -1},
77
  {31, "H261",       CODEC_TYPE_VIDEO,   CODEC_ID_H261, 90000, -1},
78
  {32, "MPV",        CODEC_TYPE_VIDEO,   CODEC_ID_MPEG1VIDEO, 90000, -1},
79
  {33, "MP2T",       CODEC_TYPE_DATA,    CODEC_ID_MPEG2TS, 90000, -1},
80
  {34, "H263",       CODEC_TYPE_VIDEO,   CODEC_ID_H263, 90000, -1},
81
  {35, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
82
  {36, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
83
  {37, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
84
  {38, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
85
  {39, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
86
  {40, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
87
  {41, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
88
  {42, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
89
  {43, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
90
  {44, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
91
  {45, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
92
  {46, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
93
  {47, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
94
  {48, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
95
  {49, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
96
  {50, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
97
  {51, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
98
  {52, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
99
  {53, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
100
  {54, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
101
  {55, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
102
  {56, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
103
  {57, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
104
  {58, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
105
  {59, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
106
  {60, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
107
  {61, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
108
  {62, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
109
  {63, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
110
  {64, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
111
  {65, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
112
  {66, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
113
  {67, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
114
  {68, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
115
  {69, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
116
  {70, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
117
  {71, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
118
  {72, "reserved for RTCP conflict avoidance", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
119
  {73, "reserved for RTCP conflict avoidance", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
120
  {74, "reserved for RTCP conflict avoidance", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
121
  {75, "reserved for RTCP conflict avoidance", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
122
  {76, "reserved for RTCP conflict avoidance", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
123
  {77, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
124
  {78, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
125
  {79, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
126
  {80, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
127
  {81, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
128
  {82, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
129
  {83, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
130
  {84, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
131
  {85, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
132
  {86, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
133
  {87, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
134
  {88, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
135
  {89, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
136
  {90, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
137
  {91, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
138
  {92, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
139
  {93, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
140
  {94, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
141
  {95, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
142
  {96, "dynamic",    CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
143
  {97, "dynamic",    CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
144
  {98, "dynamic",    CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
145
  {99, "dynamic",    CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
146
  {100, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
147
  {101, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
148
  {102, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
149
  {103, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
150
  {104, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
151
  {105, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
152
  {106, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
153
  {107, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
154
  {108, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
155
  {109, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
156
  {110, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
157
  {111, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
158
  {112, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
159
  {113, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
160
  {114, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
161
  {115, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
162
  {116, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
163
  {117, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
164
  {118, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
165
  {119, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
166
  {120, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
167
  {121, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
168
  {122, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
169
  {123, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
170
  {124, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
171
  {125, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
172
  {126, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
173
  {127, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
174
  {-1, "",           CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}
175
};
176
177 4934884a Ryan Martell
/* statistics functions */
178
RTPDynamicProtocolHandler *RTPFirstDynamicPayloadHandler= NULL;
179
180
static RTPDynamicProtocolHandler mp4v_es_handler= {"MP4V-ES", CODEC_TYPE_VIDEO, CODEC_ID_MPEG4};
181 cbee7a69 Baptiste Coudurier
static RTPDynamicProtocolHandler mpeg4_generic_handler= {"mpeg4-generic", CODEC_TYPE_AUDIO, CODEC_ID_AAC};
182 4934884a Ryan Martell
183
static void register_dynamic_payload_handler(RTPDynamicProtocolHandler *handler)
184 d1ccf0e0 Romain Degez
{
185 4934884a Ryan Martell
    handler->next= RTPFirstDynamicPayloadHandler;
186
    RTPFirstDynamicPayloadHandler= handler;
187
}
188 e309128f Fabrice Bellard
189 4934884a Ryan Martell
void av_register_rtp_dynamic_payload_handlers()
190
{
191
    register_dynamic_payload_handler(&mp4v_es_handler);
192
    register_dynamic_payload_handler(&mpeg4_generic_handler);
193
    register_dynamic_payload_handler(&ff_h264_dynamic_handler);
194
}
195 e309128f Fabrice Bellard
196
int rtp_get_codec_info(AVCodecContext *codec, int payload_type)
197
{
198 d1ccf0e0 Romain Degez
    if (AVRtpPayloadTypes[payload_type].codec_id != CODEC_ID_NONE) {
199
        codec->codec_type = AVRtpPayloadTypes[payload_type].codec_type;
200 3c47c174 Luca Abeni
        codec->codec_id = AVRtpPayloadTypes[payload_type].codec_id;
201 d1ccf0e0 Romain Degez
        if (AVRtpPayloadTypes[payload_type].audio_channels > 0)
202
            codec->channels = AVRtpPayloadTypes[payload_type].audio_channels;
203
        if (AVRtpPayloadTypes[payload_type].clock_rate > 0)
204
            codec->sample_rate = AVRtpPayloadTypes[payload_type].clock_rate;
205
        return 0;
206 e309128f Fabrice Bellard
    }
207 d1ccf0e0 Romain Degez
    return -1;
208 e309128f Fabrice Bellard
}
209
210
int rtp_get_payload_type(AVCodecContext *codec)
211
{
212 d1ccf0e0 Romain Degez
    int i, payload_type;
213 e309128f Fabrice Bellard
214
    /* compute the payload type */
215 d1ccf0e0 Romain Degez
    for (payload_type = -1, i = 0; AVRtpPayloadTypes[i].pt >= 0; ++i)
216
        if (AVRtpPayloadTypes[i].codec_id == codec->codec_id) {
217
            if (codec->codec_id == CODEC_ID_PCM_S16BE)
218
                if (codec->channels != AVRtpPayloadTypes[i].audio_channels)
219
                    continue;
220
            payload_type = AVRtpPayloadTypes[i].pt;
221 e309128f Fabrice Bellard
        }
222
    return payload_type;
223
}
224
225 0c1a9eda Zdenek Kabelac
static inline uint32_t decode_be32(const uint8_t *p)
226 e309128f Fabrice Bellard
{
227
    return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
228
}
229
230 e5526b2c Fabrice Bellard
static inline uint64_t decode_be64(const uint8_t *p)
231 e309128f Fabrice Bellard
{
232 0c1a9eda Zdenek Kabelac
    return ((uint64_t)decode_be32(p) << 32) | decode_be32(p + 4);
233 e309128f Fabrice Bellard
}
234
235 8b1ab7bf Fabrice Bellard
static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf, int len)
236 e309128f Fabrice Bellard
{
237
    if (buf[1] != 200)
238
        return -1;
239
    s->last_rtcp_ntp_time = decode_be64(buf + 8);
240 e5526b2c Fabrice Bellard
    if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE)
241
        s->first_rtcp_ntp_time = s->last_rtcp_ntp_time;
242 e309128f Fabrice Bellard
    s->last_rtcp_timestamp = decode_be32(buf + 16);
243
    return 0;
244
}
245
246 4a6cc061 Ryan Martell
#define RTP_SEQ_MOD (1<<16)
247
248
/**
249
* called on parse open packet
250
*/
251
static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence) // called on parse open packet.
252
{
253
    memset(s, 0, sizeof(RTPStatistics));
254
    s->max_seq= base_sequence;
255
    s->probation= 1;
256
}
257
258
/**
259
* called whenever there is a large jump in sequence numbers, or when they get out of probation...
260
*/
261
static void rtp_init_sequence(RTPStatistics *s, uint16_t seq)
262
{
263
    s->max_seq= seq;
264
    s->cycles= 0;
265
    s->base_seq= seq -1;
266
    s->bad_seq= RTP_SEQ_MOD + 1;
267
    s->received= 0;
268
    s->expected_prior= 0;
269
    s->received_prior= 0;
270
    s->jitter= 0;
271
    s->transit= 0;
272
}
273
274
/**
275
* returns 1 if we should handle this packet.
276
*/
277
static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq)
278
{
279
    uint16_t udelta= seq - s->max_seq;
280
    const int MAX_DROPOUT= 3000;
281
    const int MAX_MISORDER = 100;
282
    const int MIN_SEQUENTIAL = 2;
283
284
    /* source not valid until MIN_SEQUENTIAL packets with sequence seq. numbers have been received */
285
    if(s->probation)
286
    {
287
        if(seq==s->max_seq + 1) {
288
            s->probation--;
289
            s->max_seq= seq;
290
            if(s->probation==0) {
291
                rtp_init_sequence(s, seq);
292
                s->received++;
293
                return 1;
294
            }
295
        } else {
296
            s->probation= MIN_SEQUENTIAL - 1;
297
            s->max_seq = seq;
298
        }
299
    } else if (udelta < MAX_DROPOUT) {
300
        // in order, with permissible gap
301
        if(seq < s->max_seq) {
302
            //sequence number wrapped; count antother 64k cycles
303
            s->cycles += RTP_SEQ_MOD;
304
        }
305
        s->max_seq= seq;
306
    } else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) {
307
        // sequence made a large jump...
308
        if(seq==s->bad_seq) {
309
            // two sequential packets-- assume that the other side restarted without telling us; just resync.
310
            rtp_init_sequence(s, seq);
311
        } else {
312
            s->bad_seq= (seq + 1) & (RTP_SEQ_MOD-1);
313
            return 0;
314
        }
315
    } else {
316
        // duplicate or reordered packet...
317
    }
318
    s->received++;
319
    return 1;
320
}
321
322
#if 0
323
/**
324
* This function is currently unused; without a valid local ntp time, I don't see how we could calculate the
325
* difference between the arrival and sent timestamp.  As a result, the jitter and transit statistics values
326
* never change.  I left this in in case someone else can see a way. (rdm)
327
*/
328
static void rtcp_update_jitter(RTPStatistics *s, uint32_t sent_timestamp, uint32_t arrival_timestamp)
329
{
330
    uint32_t transit= arrival_timestamp - sent_timestamp;
331
    int d;
332
    s->transit= transit;
333
    d= FFABS(transit - s->transit);
334
    s->jitter += d - ((s->jitter + 8)>>4);
335
}
336
#endif
337
338 dbf30963 Thijs
int rtp_check_and_send_back_rr(RTPDemuxContext *s, int count)
339
{
340
    ByteIOContext pb;
341
    uint8_t *buf;
342
    int len;
343
    int rtcp_bytes;
344 4a6cc061 Ryan Martell
    RTPStatistics *stats= &s->statistics;
345
    uint32_t lost;
346
    uint32_t extended_max;
347
    uint32_t expected_interval;
348
    uint32_t received_interval;
349
    uint32_t lost_interval;
350
    uint32_t expected;
351
    uint32_t fraction;
352
    uint64_t ntp_time= s->last_rtcp_ntp_time; // TODO: Get local ntp time?
353 dbf30963 Thijs
354
    if (!s->rtp_ctx || (count < 1))
355
        return -1;
356
357 4a6cc061 Ryan Martell
    /* TODO: I think this is way too often; RFC 1889 has algorithm for this */
358 dbf30963 Thijs
    /* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */
359
    s->octet_count += count;
360
    rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
361
        RTCP_TX_RATIO_DEN;
362
    rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !?
363
    if (rtcp_bytes < 28)
364
        return -1;
365
    s->last_octet_count = s->octet_count;
366
367
    if (url_open_dyn_buf(&pb) < 0)
368
        return -1;
369
370
    // Receiver Report
371
    put_byte(&pb, (RTP_VERSION << 6) + 1); /* 1 report block */
372
    put_byte(&pb, 201);
373
    put_be16(&pb, 7); /* length in words - 1 */
374
    put_be32(&pb, s->ssrc); // our own SSRC
375
    put_be32(&pb, s->ssrc); // XXX: should be the server's here!
376
    // some placeholders we should really fill...
377 4a6cc061 Ryan Martell
    // RFC 1889/p64
378
    extended_max= stats->cycles + stats->max_seq;
379
    expected= extended_max - stats->base_seq + 1;
380
    lost= expected - stats->received;
381
    lost= FFMIN(lost, 0xffffff); // clamp it since it's only 24 bits...
382
    expected_interval= expected - stats->expected_prior;
383
    stats->expected_prior= expected;
384
    received_interval= stats->received - stats->received_prior;
385
    stats->received_prior= stats->received;
386
    lost_interval= expected_interval - received_interval;
387
    if (expected_interval==0 || lost_interval<=0) fraction= 0;
388
    else fraction = (lost_interval<<8)/expected_interval;
389
390
    fraction= (fraction<<24) | lost;
391
392
    put_be32(&pb, fraction); /* 8 bits of fraction, 24 bits of total packets lost */
393
    put_be32(&pb, extended_max); /* max sequence received */
394
    put_be32(&pb, stats->jitter>>4); /* jitter */
395
396
    if(s->last_rtcp_ntp_time==AV_NOPTS_VALUE)
397
    {
398
        put_be32(&pb, 0); /* last SR timestamp */
399
        put_be32(&pb, 0); /* delay since last SR */
400
    } else {
401
        uint32_t middle_32_bits= s->last_rtcp_ntp_time>>16; // this is valid, right? do we need to handle 64 bit values special?
402
        uint32_t delay_since_last= ntp_time - s->last_rtcp_ntp_time;
403
404
        put_be32(&pb, middle_32_bits); /* last SR timestamp */
405
        put_be32(&pb, delay_since_last); /* delay since last SR */
406
    }
407 dbf30963 Thijs
408
    // CNAME
409
    put_byte(&pb, (RTP_VERSION << 6) + 1); /* 1 report block */
410
    put_byte(&pb, 202);
411
    len = strlen(s->hostname);
412
    put_be16(&pb, (6 + len + 3) / 4); /* length in words - 1 */
413
    put_be32(&pb, s->ssrc);
414
    put_byte(&pb, 0x01);
415
    put_byte(&pb, len);
416
    put_buffer(&pb, s->hostname, len);
417
    // padding
418
    for (len = (6 + len) % 4; len % 4; len++) {
419
        put_byte(&pb, 0);
420
    }
421
422
    put_flush_packet(&pb);
423
    len = url_close_dyn_buf(&pb, &buf);
424
    if ((len > 0) && buf) {
425 4a6cc061 Ryan Martell
        int result;
426 dbf30963 Thijs
#if defined(DEBUG)
427
        printf("sending %d bytes of RR\n", len);
428
#endif
429 4a6cc061 Ryan Martell
        result= url_write(s->rtp_ctx, buf, len);
430
#if defined(DEBUG)
431
        printf("result from url_write: %d\n", result);
432
#endif
433 dbf30963 Thijs
        av_free(buf);
434
    }
435
    return 0;
436
}
437
438
/**
439 8b1ab7bf Fabrice Bellard
 * open a new RTP parse context for stream 'st'. 'st' can be NULL for
440
 * MPEG2TS streams to indicate that they should be demuxed inside the
441 115329f1 Diego Biurrun
 * rtp demux (otherwise CODEC_ID_MPEG2TS packets are returned)
442 d0deedcb Ryan Martell
 * TODO: change this to not take rtp_payload data, and use the new dynamic payload system.
443 8b1ab7bf Fabrice Bellard
 */
444 dbf30963 Thijs
RTPDemuxContext *rtp_parse_open(AVFormatContext *s1, AVStream *st, URLContext *rtpc, int payload_type, rtp_payload_data_t *rtp_payload_data)
445 8b1ab7bf Fabrice Bellard
{
446
    RTPDemuxContext *s;
447
448
    s = av_mallocz(sizeof(RTPDemuxContext));
449
    if (!s)
450
        return NULL;
451
    s->payload_type = payload_type;
452
    s->last_rtcp_ntp_time = AV_NOPTS_VALUE;
453
    s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
454
    s->ic = s1;
455
    s->st = st;
456 d1ccf0e0 Romain Degez
    s->rtp_payload_data = rtp_payload_data;
457 4a6cc061 Ryan Martell
    rtp_init_statistics(&s->statistics, 0); // do we know the initial sequence from sdp?
458 d1ccf0e0 Romain Degez
    if (!strcmp(AVRtpPayloadTypes[payload_type].enc_name, "MP2T")) {
459 8b1ab7bf Fabrice Bellard
        s->ts = mpegts_parse_open(s->ic);
460
        if (s->ts == NULL) {
461
            av_free(s);
462
            return NULL;
463
        }
464 ccd39ae6 Fabrice Bellard
    } else {
465 01f4895c Michael Niedermayer
        switch(st->codec->codec_id) {
466 ccd39ae6 Fabrice Bellard
        case CODEC_ID_MPEG1VIDEO:
467
        case CODEC_ID_MPEG2VIDEO:
468
        case CODEC_ID_MP2:
469
        case CODEC_ID_MP3:
470
        case CODEC_ID_MPEG4:
471 4934884a Ryan Martell
        case CODEC_ID_H264:
472 ccd39ae6 Fabrice Bellard
            st->need_parsing = 1;
473
            break;
474
        default:
475
            break;
476
        }
477 8b1ab7bf Fabrice Bellard
    }
478 dbf30963 Thijs
    // needed to send back RTCP RR in RTSP sessions
479
    s->rtp_ctx = rtpc;
480
    gethostname(s->hostname, sizeof(s->hostname));
481 8b1ab7bf Fabrice Bellard
    return s;
482
}
483
484 d1ccf0e0 Romain Degez
static int rtp_parse_mp4_au(RTPDemuxContext *s, const uint8_t *buf)
485
{
486
    int au_headers_length, au_header_size, i;
487
    GetBitContext getbitcontext;
488
    rtp_payload_data_t *infos;
489
490
    infos = s->rtp_payload_data;
491
492
    if (infos == NULL)
493
        return -1;
494
495
    /* decode the first 2 bytes where are stored the AUHeader sections
496
       length in bits */
497 fead30d4 Alex Beregszaszi
    au_headers_length = AV_RB16(buf);
498 d1ccf0e0 Romain Degez
499
    if (au_headers_length > RTP_MAX_PACKET_LENGTH)
500
      return -1;
501
502
    infos->au_headers_length_bytes = (au_headers_length + 7) / 8;
503
504
    /* skip AU headers length section (2 bytes) */
505
    buf += 2;
506
507
    init_get_bits(&getbitcontext, buf, infos->au_headers_length_bytes * 8);
508
509
    /* XXX: Wrong if optionnal additional sections are present (cts, dts etc...) */
510
    au_header_size = infos->sizelength + infos->indexlength;
511
    if (au_header_size <= 0 || (au_headers_length % au_header_size != 0))
512
        return -1;
513
514
    infos->nb_au_headers = au_headers_length / au_header_size;
515
    infos->au_headers = av_malloc(sizeof(struct AUHeaders) * infos->nb_au_headers);
516
517
    /* XXX: We handle multiple AU Section as only one (need to fix this for interleaving)
518
       In my test, the faad decoder doesnt behave correctly when sending each AU one by one
519
       but does when sending the whole as one big packet...  */
520
    infos->au_headers[0].size = 0;
521
    infos->au_headers[0].index = 0;
522
    for (i = 0; i < infos->nb_au_headers; ++i) {
523
        infos->au_headers[0].size += get_bits_long(&getbitcontext, infos->sizelength);
524
        infos->au_headers[0].index = get_bits_long(&getbitcontext, infos->indexlength);
525
    }
526
527
    infos->nb_au_headers = 1;
528
529
    return 0;
530
}
531
532 8b1ab7bf Fabrice Bellard
/**
533 d0deedcb Ryan Martell
 * This was the second switch in rtp_parse packet.  Normalizes time, if required, sets stream_index, etc.
534
 */
535
static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
536
{
537
    switch(s->st->codec->codec_id) {
538
        case CODEC_ID_MP2:
539
        case CODEC_ID_MPEG1VIDEO:
540
            if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE) {
541
                int64_t addend;
542
543
                int delta_timestamp;
544
                /* XXX: is it really necessary to unify the timestamp base ? */
545
                /* compute pts from timestamp with received ntp_time */
546
                delta_timestamp = timestamp - s->last_rtcp_timestamp;
547
                /* convert to 90 kHz without overflow */
548
                addend = (s->last_rtcp_ntp_time - s->first_rtcp_ntp_time) >> 14;
549
                addend = (addend * 5625) >> 14;
550
                pkt->pts = addend + delta_timestamp;
551
            }
552
            break;
553 cbee7a69 Baptiste Coudurier
        case CODEC_ID_AAC:
554 d0deedcb Ryan Martell
        case CODEC_ID_H264:
555
        case CODEC_ID_MPEG4:
556
            pkt->pts = timestamp;
557
            break;
558
        default:
559
            /* no timestamp info yet */
560
            break;
561
    }
562
    pkt->stream_index = s->st->index;
563
}
564
565
/**
566 115329f1 Diego Biurrun
 * Parse an RTP or RTCP packet directly sent as a buffer.
567 8b1ab7bf Fabrice Bellard
 * @param s RTP parse context.
568 e309128f Fabrice Bellard
 * @param pkt returned packet
569 8b1ab7bf Fabrice Bellard
 * @param buf input buffer or NULL to read the next packets
570 e309128f Fabrice Bellard
 * @param len buffer len
571 115329f1 Diego Biurrun
 * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
572 8b1ab7bf Fabrice Bellard
 * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
573 e309128f Fabrice Bellard
 */
574 115329f1 Diego Biurrun
int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
575 8b1ab7bf Fabrice Bellard
                     const uint8_t *buf, int len)
576 e309128f Fabrice Bellard
{
577
    unsigned int ssrc, h;
578 d0deedcb Ryan Martell
    int payload_type, seq, ret;
579 e309128f Fabrice Bellard
    AVStream *st;
580 0c1a9eda Zdenek Kabelac
    uint32_t timestamp;
581 d0deedcb Ryan Martell
    int rv= 0;
582 115329f1 Diego Biurrun
583 8b1ab7bf Fabrice Bellard
    if (!buf) {
584
        /* return the next packets, if any */
585 4934884a Ryan Martell
        if(s->st && s->parse_packet) {
586 d0deedcb Ryan Martell
            timestamp= 0; ///< Should not be used if buf is NULL, but should be set to the timestamp of the packet returned....
587
            rv= s->parse_packet(s, pkt, &timestamp, NULL, 0);
588
            finalize_packet(s, pkt, timestamp);
589
            return rv;
590 4934884a Ryan Martell
        } else {
591 d0deedcb Ryan Martell
            // TODO: Move to a dynamic packet handler (like above)
592 ed787542 Ryan Martell
            if (s->read_buf_index >= s->read_buf_size)
593
                return -1;
594
            ret = mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index,
595
                                      s->read_buf_size - s->read_buf_index);
596
            if (ret < 0)
597
                return -1;
598
            s->read_buf_index += ret;
599
            if (s->read_buf_index < s->read_buf_size)
600
                return 1;
601
            else
602
                return 0;
603 4934884a Ryan Martell
        }
604 8b1ab7bf Fabrice Bellard
    }
605
606 e309128f Fabrice Bellard
    if (len < 12)
607
        return -1;
608
609
    if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
610
        return -1;
611
    if (buf[1] >= 200 && buf[1] <= 204) {
612 8b1ab7bf Fabrice Bellard
        rtcp_parse_packet(s, buf, len);
613 e309128f Fabrice Bellard
        return -1;
614
    }
615
    payload_type = buf[1] & 0x7f;
616
    seq  = (buf[2] << 8) | buf[3];
617
    timestamp = decode_be32(buf + 4);
618
    ssrc = decode_be32(buf + 8);
619 dbf30963 Thijs
    /* store the ssrc in the RTPDemuxContext */
620
    s->ssrc = ssrc;
621 115329f1 Diego Biurrun
622 e309128f Fabrice Bellard
    /* NOTE: we can handle only one payload type */
623
    if (s->payload_type != payload_type)
624
        return -1;
625 7581c5f5 François Revol
626
    st = s->st;
627 4a6cc061 Ryan Martell
    // only do something with this if all the rtp checks pass...
628
    if(!rtp_valid_packet_in_sequence(&s->statistics, seq))
629
    {
630 7581c5f5 François Revol
        av_log(st?st->codec:NULL, AV_LOG_ERROR, "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
631 e309128f Fabrice Bellard
               payload_type, seq, ((s->seq + 1) & 0xffff));
632 4a6cc061 Ryan Martell
        return -1;
633 e309128f Fabrice Bellard
    }
634 4a6cc061 Ryan Martell
635 d1ccf0e0 Romain Degez
    s->seq = seq;
636 e309128f Fabrice Bellard
    len -= 12;
637
    buf += 12;
638 8b1ab7bf Fabrice Bellard
639
    if (!st) {
640
        /* specific MPEG2TS demux support */
641
        ret = mpegts_parse_packet(s->ts, pkt, buf, len);
642
        if (ret < 0)
643 e309128f Fabrice Bellard
            return -1;
644 8b1ab7bf Fabrice Bellard
        if (ret < len) {
645
            s->read_buf_size = len - ret;
646
            memcpy(s->buf, buf + ret, s->read_buf_size);
647
            s->read_buf_index = 0;
648
            return 1;
649
        }
650
    } else {
651 4934884a Ryan Martell
        // at this point, the RTP header has been stripped;  This is ASSUMING that there is only 1 CSRC, which in't wise.
652 01f4895c Michael Niedermayer
        switch(st->codec->codec_id) {
653 8b1ab7bf Fabrice Bellard
        case CODEC_ID_MP2:
654
            /* better than nothing: skip mpeg audio RTP header */
655
            if (len <= 4)
656
                return -1;
657
            h = decode_be32(buf);
658
            len -= 4;
659
            buf += 4;
660
            av_new_packet(pkt, len);
661
            memcpy(pkt->data, buf, len);
662
            break;
663
        case CODEC_ID_MPEG1VIDEO:
664 ccd39ae6 Fabrice Bellard
            /* better than nothing: skip mpeg video RTP header */
665 e309128f Fabrice Bellard
            if (len <= 4)
666
                return -1;
667 8b1ab7bf Fabrice Bellard
            h = decode_be32(buf);
668 e309128f Fabrice Bellard
            buf += 4;
669
            len -= 4;
670 8b1ab7bf Fabrice Bellard
            if (h & (1 << 26)) {
671
                /* mpeg2 */
672
                if (len <= 4)
673
                    return -1;
674
                buf += 4;
675
                len -= 4;
676
            }
677
            av_new_packet(pkt, len);
678
            memcpy(pkt->data, buf, len);
679
            break;
680 d8372330 Ryan Martell
            // moved from below, verbatim.  this is because this section handles packets, and the lower switch handles
681
            // timestamps.
682
            // TODO: Put this into a dynamic packet handler...
683 cbee7a69 Baptiste Coudurier
        case CODEC_ID_AAC:
684 d8372330 Ryan Martell
            if (rtp_parse_mp4_au(s, buf))
685
                return -1;
686
            {
687
                rtp_payload_data_t *infos = s->rtp_payload_data;
688
                if (infos == NULL)
689
                    return -1;
690
                buf += infos->au_headers_length_bytes + 2;
691
                len -= infos->au_headers_length_bytes + 2;
692
693
                /* XXX: Fixme we only handle the case where rtp_parse_mp4_au define
694
                    one au_header */
695
                av_new_packet(pkt, infos->au_headers[0].size);
696
                memcpy(pkt->data, buf, infos->au_headers[0].size);
697
                buf += infos->au_headers[0].size;
698
                len -= infos->au_headers[0].size;
699
            }
700
            s->read_buf_size = len;
701
            s->buf_ptr = buf;
702 d0deedcb Ryan Martell
            rv= 0;
703 d8372330 Ryan Martell
            break;
704 8b1ab7bf Fabrice Bellard
        default:
705 4934884a Ryan Martell
            if(s->parse_packet) {
706 d0deedcb Ryan Martell
                rv= s->parse_packet(s, pkt, &timestamp, buf, len);
707 4934884a Ryan Martell
            } else {
708 ed787542 Ryan Martell
                av_new_packet(pkt, len);
709
                memcpy(pkt->data, buf, len);
710 4934884a Ryan Martell
            }
711 8b1ab7bf Fabrice Bellard
            break;
712 e309128f Fabrice Bellard
        }
713 115329f1 Diego Biurrun
714 d0deedcb Ryan Martell
        // now perform timestamp things....
715
        finalize_packet(s, pkt, timestamp);
716 e309128f Fabrice Bellard
    }
717 d0deedcb Ryan Martell
    return rv;
718 e309128f Fabrice Bellard
}
719
720 8b1ab7bf Fabrice Bellard
void rtp_parse_close(RTPDemuxContext *s)
721 e309128f Fabrice Bellard
{
722 4934884a Ryan Martell
    // TODO: fold this into the protocol specific data fields.
723 d1ccf0e0 Romain Degez
    if (!strcmp(AVRtpPayloadTypes[s->payload_type].enc_name, "MP2T")) {
724 8b1ab7bf Fabrice Bellard
        mpegts_parse_close(s->ts);
725 e309128f Fabrice Bellard
    }
726 8b1ab7bf Fabrice Bellard
    av_free(s);
727 e309128f Fabrice Bellard
}
728
729
/* rtp output */
730
731
static int rtp_write_header(AVFormatContext *s1)
732
{
733 8b1ab7bf Fabrice Bellard
    RTPDemuxContext *s = s1->priv_data;
734
    int payload_type, max_packet_size, n;
735 e309128f Fabrice Bellard
    AVStream *st;
736
737
    if (s1->nb_streams != 1)
738
        return -1;
739
    st = s1->streams[0];
740
741 01f4895c Michael Niedermayer
    payload_type = rtp_get_payload_type(st->codec);
742 e309128f Fabrice Bellard
    if (payload_type < 0)
743 65e70450 Fabrice Bellard
        payload_type = RTP_PT_PRIVATE; /* private payload type */
744 e309128f Fabrice Bellard
    s->payload_type = payload_type;
745
746 ac2750ec Michael Niedermayer
// following 2 FIXMies could be set based on the current time, theres normaly no info leak, as rtp will likely be transmitted immedeatly
747 f8801993 D Richard Felker III
    s->base_timestamp = 0; /* FIXME: was random(), what should this be? */
748 e309128f Fabrice Bellard
    s->timestamp = s->base_timestamp;
749 f8801993 D Richard Felker III
    s->ssrc = 0; /* FIXME: was random(), what should this be? */
750 e309128f Fabrice Bellard
    s->first_packet = 1;
751
752
    max_packet_size = url_fget_max_packet_size(&s1->pb);
753
    if (max_packet_size <= 12)
754
        return AVERROR_IO;
755
    s->max_payload_size = max_packet_size - 12;
756
757 01f4895c Michael Niedermayer
    switch(st->codec->codec_id) {
758 e309128f Fabrice Bellard
    case CODEC_ID_MP2:
759 80783dc2 Fabrice Bellard
    case CODEC_ID_MP3:
760 e309128f Fabrice Bellard
        s->buf_ptr = s->buf + 4;
761
        s->cur_timestamp = 0;
762
        break;
763
    case CODEC_ID_MPEG1VIDEO:
764
        s->cur_timestamp = 0;
765
        break;
766 8b1ab7bf Fabrice Bellard
    case CODEC_ID_MPEG2TS:
767
        n = s->max_payload_size / TS_PACKET_SIZE;
768
        if (n < 1)
769
            n = 1;
770
        s->max_payload_size = n * TS_PACKET_SIZE;
771
        s->buf_ptr = s->buf;
772
        break;
773 e309128f Fabrice Bellard
    default:
774
        s->buf_ptr = s->buf;
775
        break;
776
    }
777
778
    return 0;
779
}
780
781
/* send an rtcp sender report packet */
782 0c1a9eda Zdenek Kabelac
static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time)
783 e309128f Fabrice Bellard
{
784 8b1ab7bf Fabrice Bellard
    RTPDemuxContext *s = s1->priv_data;
785 e309128f Fabrice Bellard
#if defined(DEBUG)
786 949b1a13 Steve L'Homme
    printf("RTCP: %02x %"PRIx64" %x\n", s->payload_type, ntp_time, s->timestamp);
787 e309128f Fabrice Bellard
#endif
788
    put_byte(&s1->pb, (RTP_VERSION << 6));
789
    put_byte(&s1->pb, 200);
790
    put_be16(&s1->pb, 6); /* length in words - 1 */
791
    put_be32(&s1->pb, s->ssrc);
792
    put_be64(&s1->pb, ntp_time);
793
    put_be32(&s1->pb, s->timestamp);
794
    put_be32(&s1->pb, s->packet_count);
795
    put_be32(&s1->pb, s->octet_count);
796
    put_flush_packet(&s1->pb);
797
}
798
799
/* send an rtp packet. sequence number is incremented, but the caller
800
   must update the timestamp itself */
801 00364063 Luca Abeni
static void rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
802 e309128f Fabrice Bellard
{
803 8b1ab7bf Fabrice Bellard
    RTPDemuxContext *s = s1->priv_data;
804 e309128f Fabrice Bellard
805
#ifdef DEBUG
806
    printf("rtp_send_data size=%d\n", len);
807
#endif
808
809
    /* build the RTP header */
810
    put_byte(&s1->pb, (RTP_VERSION << 6));
811 00364063 Luca Abeni
    put_byte(&s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
812 e309128f Fabrice Bellard
    put_be16(&s1->pb, s->seq);
813
    put_be32(&s1->pb, s->timestamp);
814
    put_be32(&s1->pb, s->ssrc);
815 115329f1 Diego Biurrun
816 e309128f Fabrice Bellard
    put_buffer(&s1->pb, buf1, len);
817
    put_flush_packet(&s1->pb);
818 115329f1 Diego Biurrun
819 e309128f Fabrice Bellard
    s->seq++;
820
    s->octet_count += len;
821
    s->packet_count++;
822
}
823
824
/* send an integer number of samples and compute time stamp and fill
825
   the rtp send buffer before sending. */
826
static void rtp_send_samples(AVFormatContext *s1,
827 49057904 Fabrice Bellard
                             const uint8_t *buf1, int size, int sample_size)
828 e309128f Fabrice Bellard
{
829 8b1ab7bf Fabrice Bellard
    RTPDemuxContext *s = s1->priv_data;
830 e309128f Fabrice Bellard
    int len, max_packet_size, n;
831
832
    max_packet_size = (s->max_payload_size / sample_size) * sample_size;
833
    /* not needed, but who nows */
834
    if ((size % sample_size) != 0)
835
        av_abort();
836
    while (size > 0) {
837
        len = (max_packet_size - (s->buf_ptr - s->buf));
838
        if (len > size)
839
            len = size;
840
841
        /* copy data */
842
        memcpy(s->buf_ptr, buf1, len);
843
        s->buf_ptr += len;
844
        buf1 += len;
845
        size -= len;
846
        n = (s->buf_ptr - s->buf);
847
        /* if buffer full, then send it */
848
        if (n >= max_packet_size) {
849 00364063 Luca Abeni
            rtp_send_data(s1, s->buf, n, 0);
850 e309128f Fabrice Bellard
            s->buf_ptr = s->buf;
851
            /* update timestamp */
852
            s->timestamp += n / sample_size;
853
        }
854
    }
855 115329f1 Diego Biurrun
}
856 e309128f Fabrice Bellard
857
/* NOTE: we suppose that exactly one frame is given as argument here */
858
/* XXX: test it */
859
static void rtp_send_mpegaudio(AVFormatContext *s1,
860 49057904 Fabrice Bellard
                               const uint8_t *buf1, int size)
861 e309128f Fabrice Bellard
{
862 8b1ab7bf Fabrice Bellard
    RTPDemuxContext *s = s1->priv_data;
863 e309128f Fabrice Bellard
    AVStream *st = s1->streams[0];
864
    int len, count, max_packet_size;
865
866
    max_packet_size = s->max_payload_size;
867
868
    /* test if we must flush because not enough space */
869
    len = (s->buf_ptr - s->buf);
870
    if ((len + size) > max_packet_size) {
871
        if (len > 4) {
872 00364063 Luca Abeni
            rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
873 e309128f Fabrice Bellard
            s->buf_ptr = s->buf + 4;
874
            /* 90 KHz time stamp */
875 115329f1 Diego Biurrun
            s->timestamp = s->base_timestamp +
876 01f4895c Michael Niedermayer
                (s->cur_timestamp * 90000LL) / st->codec->sample_rate;
877 e309128f Fabrice Bellard
        }
878
    }
879
880
    /* add the packet */
881
    if (size > max_packet_size) {
882
        /* big packet: fragment */
883
        count = 0;
884
        while (size > 0) {
885
            len = max_packet_size - 4;
886
            if (len > size)
887
                len = size;
888
            /* build fragmented packet */
889
            s->buf[0] = 0;
890
            s->buf[1] = 0;
891
            s->buf[2] = count >> 8;
892
            s->buf[3] = count;
893
            memcpy(s->buf + 4, buf1, len);
894 00364063 Luca Abeni
            rtp_send_data(s1, s->buf, len + 4, 0);
895 e309128f Fabrice Bellard
            size -= len;
896
            buf1 += len;
897
            count += len;
898
        }
899
    } else {
900
        if (s->buf_ptr == s->buf + 4) {
901
            /* no fragmentation possible */
902
            s->buf[0] = 0;
903
            s->buf[1] = 0;
904
            s->buf[2] = 0;
905
            s->buf[3] = 0;
906
        }
907
        memcpy(s->buf_ptr, buf1, size);
908
        s->buf_ptr += size;
909
    }
910 01f4895c Michael Niedermayer
    s->cur_timestamp += st->codec->frame_size;
911 e309128f Fabrice Bellard
}
912
913
/* NOTE: a single frame must be passed with sequence header if
914
   needed. XXX: use slices. */
915
static void rtp_send_mpegvideo(AVFormatContext *s1,
916 49057904 Fabrice Bellard
                               const uint8_t *buf1, int size)
917 e309128f Fabrice Bellard
{
918 8b1ab7bf Fabrice Bellard
    RTPDemuxContext *s = s1->priv_data;
919 e309128f Fabrice Bellard
    AVStream *st = s1->streams[0];
920
    int len, h, max_packet_size;
921 0c1a9eda Zdenek Kabelac
    uint8_t *q;
922 e309128f Fabrice Bellard
923
    max_packet_size = s->max_payload_size;
924
925
    while (size > 0) {
926
        /* XXX: more correct headers */
927
        h = 0;
928 01f4895c Michael Niedermayer
        if (st->codec->sub_id == 2)
929 e309128f Fabrice Bellard
            h |= 1 << 26; /* mpeg 2 indicator */
930
        q = s->buf;
931
        *q++ = h >> 24;
932
        *q++ = h >> 16;
933
        *q++ = h >> 8;
934
        *q++ = h;
935
936 01f4895c Michael Niedermayer
        if (st->codec->sub_id == 2) {
937 e309128f Fabrice Bellard
            h = 0;
938
            *q++ = h >> 24;
939
            *q++ = h >> 16;
940
            *q++ = h >> 8;
941
            *q++ = h;
942
        }
943 115329f1 Diego Biurrun
944 e309128f Fabrice Bellard
        len = max_packet_size - (q - s->buf);
945
        if (len > size)
946
            len = size;
947
948
        memcpy(q, buf1, len);
949
        q += len;
950
951
        /* 90 KHz time stamp */
952 115329f1 Diego Biurrun
        s->timestamp = s->base_timestamp +
953 01f4895c Michael Niedermayer
            av_rescale((int64_t)s->cur_timestamp * st->codec->time_base.num, 90000, st->codec->time_base.den); //FIXME pass timestamps
954 760fb54f Michael Niedermayer
        rtp_send_data(s1, s->buf, q - s->buf, (len == size));
955 e309128f Fabrice Bellard
956
        buf1 += len;
957
        size -= len;
958
    }
959
    s->cur_timestamp++;
960
}
961
962 65e70450 Fabrice Bellard
static void rtp_send_raw(AVFormatContext *s1,
963 49057904 Fabrice Bellard
                         const uint8_t *buf1, int size)
964 65e70450 Fabrice Bellard
{
965 8b1ab7bf Fabrice Bellard
    RTPDemuxContext *s = s1->priv_data;
966 65e70450 Fabrice Bellard
    AVStream *st = s1->streams[0];
967
    int len, max_packet_size;
968
969
    max_packet_size = s->max_payload_size;
970
971
    while (size > 0) {
972
        len = max_packet_size;
973
        if (len > size)
974
            len = size;
975
976
        /* 90 KHz time stamp */
977 115329f1 Diego Biurrun
        s->timestamp = s->base_timestamp +
978 01f4895c Michael Niedermayer
            av_rescale((int64_t)s->cur_timestamp * st->codec->time_base.num, 90000, st->codec->time_base.den); //FIXME pass timestamps
979 00364063 Luca Abeni
        rtp_send_data(s1, buf1, len, (len == size));
980 65e70450 Fabrice Bellard
981
        buf1 += len;
982
        size -= len;
983
    }
984
    s->cur_timestamp++;
985
}
986
987 8b1ab7bf Fabrice Bellard
/* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
988
static void rtp_send_mpegts_raw(AVFormatContext *s1,
989
                                const uint8_t *buf1, int size)
990
{
991
    RTPDemuxContext *s = s1->priv_data;
992
    int len, out_len;
993
994
    while (size >= TS_PACKET_SIZE) {
995
        len = s->max_payload_size - (s->buf_ptr - s->buf);
996
        if (len > size)
997
            len = size;
998
        memcpy(s->buf_ptr, buf1, len);
999
        buf1 += len;
1000
        size -= len;
1001
        s->buf_ptr += len;
1002 115329f1 Diego Biurrun
1003 8b1ab7bf Fabrice Bellard
        out_len = s->buf_ptr - s->buf;
1004
        if (out_len >= s->max_payload_size) {
1005 00364063 Luca Abeni
            rtp_send_data(s1, s->buf, out_len, 0);
1006 8b1ab7bf Fabrice Bellard
            s->buf_ptr = s->buf;
1007
        }
1008
    }
1009
}
1010
1011 e309128f Fabrice Bellard
/* write an RTP packet. 'buf1' must contain a single specific frame. */
1012 e928649b Michael Niedermayer
static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
1013 e309128f Fabrice Bellard
{
1014 8b1ab7bf Fabrice Bellard
    RTPDemuxContext *s = s1->priv_data;
1015 e309128f Fabrice Bellard
    AVStream *st = s1->streams[0];
1016
    int rtcp_bytes;
1017 0c1a9eda Zdenek Kabelac
    int64_t ntp_time;
1018 e928649b Michael Niedermayer
    int size= pkt->size;
1019
    uint8_t *buf1= pkt->data;
1020 115329f1 Diego Biurrun
1021 e309128f Fabrice Bellard
#ifdef DEBUG
1022 e928649b Michael Niedermayer
    printf("%d: write len=%d\n", pkt->stream_index, size);
1023 e309128f Fabrice Bellard
#endif
1024
1025
    /* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */
1026 115329f1 Diego Biurrun
    rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
1027 e309128f Fabrice Bellard
        RTCP_TX_RATIO_DEN;
1028
    if (s->first_packet || rtcp_bytes >= 28) {
1029
        /* compute NTP time */
1030 e5526b2c Fabrice Bellard
        /* XXX: 90 kHz timestamp hardcoded */
1031 e928649b Michael Niedermayer
        ntp_time = (pkt->pts << 28) / 5625;
1032 115329f1 Diego Biurrun
        rtcp_send_sr(s1, ntp_time);
1033 e309128f Fabrice Bellard
        s->last_octet_count = s->octet_count;
1034
        s->first_packet = 0;
1035
    }
1036
1037 01f4895c Michael Niedermayer
    switch(st->codec->codec_id) {
1038 e309128f Fabrice Bellard
    case CODEC_ID_PCM_MULAW:
1039
    case CODEC_ID_PCM_ALAW:
1040
    case CODEC_ID_PCM_U8:
1041
    case CODEC_ID_PCM_S8:
1042 01f4895c Michael Niedermayer
        rtp_send_samples(s1, buf1, size, 1 * st->codec->channels);
1043 e309128f Fabrice Bellard
        break;
1044
    case CODEC_ID_PCM_U16BE:
1045
    case CODEC_ID_PCM_U16LE:
1046
    case CODEC_ID_PCM_S16BE:
1047
    case CODEC_ID_PCM_S16LE:
1048 01f4895c Michael Niedermayer
        rtp_send_samples(s1, buf1, size, 2 * st->codec->channels);
1049 e309128f Fabrice Bellard
        break;
1050
    case CODEC_ID_MP2:
1051 80783dc2 Fabrice Bellard
    case CODEC_ID_MP3:
1052 e309128f Fabrice Bellard
        rtp_send_mpegaudio(s1, buf1, size);
1053
        break;
1054
    case CODEC_ID_MPEG1VIDEO:
1055
        rtp_send_mpegvideo(s1, buf1, size);
1056
        break;
1057 8b1ab7bf Fabrice Bellard
    case CODEC_ID_MPEG2TS:
1058
        rtp_send_mpegts_raw(s1, buf1, size);
1059
        break;
1060 e309128f Fabrice Bellard
    default:
1061 65e70450 Fabrice Bellard
        /* better than nothing : send the codec raw data */
1062
        rtp_send_raw(s1, buf1, size);
1063
        break;
1064 e309128f Fabrice Bellard
    }
1065
    return 0;
1066
}
1067
1068
static int rtp_write_trailer(AVFormatContext *s1)
1069
{
1070 8b1ab7bf Fabrice Bellard
    //    RTPDemuxContext *s = s1->priv_data;
1071 e309128f Fabrice Bellard
    return 0;
1072
}
1073
1074 d2a067d1 Måns Rullgård
AVOutputFormat rtp_muxer = {
1075 e309128f Fabrice Bellard
    "rtp",
1076
    "RTP output format",
1077
    NULL,
1078
    NULL,
1079 8b1ab7bf Fabrice Bellard
    sizeof(RTPDemuxContext),
1080 e309128f Fabrice Bellard
    CODEC_ID_PCM_MULAW,
1081
    CODEC_ID_NONE,
1082
    rtp_write_header,
1083
    rtp_write_packet,
1084
    rtp_write_trailer,
1085
};