Statistics
| Branch: | Revision:

ffmpeg / libavformat / rtp.c @ 0ab1ebea

History | View | Annotate | Download (36.2 KB)

1
/*
2
 * RTP input/output format
3
 * Copyright (c) 2002 Fabrice Bellard.
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * 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
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
#include "avformat.h"
22
#include "mpegts.h"
23
#include "bitstream.h"
24

    
25
#include <unistd.h>
26
#include "network.h"
27

    
28
#include "rtp_internal.h"
29
#include "rtp_h264.h"
30
#include "rtp_mpv.h"
31
#include "rtp_aac.h"
32

    
33
//#define DEBUG
34

    
35

    
36
/* TODO: - add RTCP statistics reporting (should be optional).
37

38
         - add support for h263/mpeg4 packetized output : IDEA: send a
39
         buffer to 'rtp_write_packet' contains all the packets for ONE
40
         frame. Each packet should have a four byte header containing
41
         the length in big endian format (same trick as
42
         'url_open_dyn_packet_buf')
43
*/
44

    
45
/* from http://www.iana.org/assignments/rtp-parameters last updated 05 January 2005 */
46
AVRtpPayloadType_t AVRtpPayloadTypes[]=
47
{
48
  {0, "PCMU",        CODEC_TYPE_AUDIO,   CODEC_ID_PCM_MULAW, 8000, 1},
49
  {1, "Reserved",    CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
50
  {2, "Reserved",    CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
51
  {3, "GSM",         CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
52
  {4, "G723",        CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
53
  {5, "DVI4",        CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
54
  {6, "DVI4",        CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 16000, 1},
55
  {7, "LPC",         CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
56
  {8, "PCMA",        CODEC_TYPE_AUDIO,   CODEC_ID_PCM_ALAW, 8000, 1},
57
  {9, "G722",        CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
58
  {10, "L16",        CODEC_TYPE_AUDIO,   CODEC_ID_PCM_S16BE, 44100, 2},
59
  {11, "L16",        CODEC_TYPE_AUDIO,   CODEC_ID_PCM_S16BE, 44100, 1},
60
  {12, "QCELP",      CODEC_TYPE_AUDIO,   CODEC_ID_QCELP, 8000, 1},
61
  {13, "CN",         CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
62
  {14, "MPA",        CODEC_TYPE_AUDIO,   CODEC_ID_MP2, 90000, -1},
63
  {15, "G728",       CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
64
  {16, "DVI4",       CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 11025, 1},
65
  {17, "DVI4",       CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 22050, 1},
66
  {18, "G729",       CODEC_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
67
  {19, "reserved",   CODEC_TYPE_AUDIO,   CODEC_ID_NONE, -1, -1},
68
  {20, "unassigned", CODEC_TYPE_AUDIO,   CODEC_ID_NONE, -1, -1},
69
  {21, "unassigned", CODEC_TYPE_AUDIO,   CODEC_ID_NONE, -1, -1},
70
  {22, "unassigned", CODEC_TYPE_AUDIO,   CODEC_ID_NONE, -1, -1},
71
  {23, "unassigned", CODEC_TYPE_AUDIO,   CODEC_ID_NONE, -1, -1},
72
  {24, "unassigned", CODEC_TYPE_VIDEO,   CODEC_ID_NONE, -1, -1},
73
  {25, "CelB",       CODEC_TYPE_VIDEO,   CODEC_ID_NONE, 90000, -1},
74
  {26, "JPEG",       CODEC_TYPE_VIDEO,   CODEC_ID_MJPEG, 90000, -1},
75
  {27, "unassigned", CODEC_TYPE_VIDEO,   CODEC_ID_NONE, -1, -1},
76
  {28, "nv",         CODEC_TYPE_VIDEO,   CODEC_ID_NONE, 90000, -1},
77
  {29, "unassigned", CODEC_TYPE_VIDEO,   CODEC_ID_NONE, -1, -1},
78
  {30, "unassigned", CODEC_TYPE_VIDEO,   CODEC_ID_NONE, -1, -1},
79
  {31, "H261",       CODEC_TYPE_VIDEO,   CODEC_ID_H261, 90000, -1},
80
  {32, "MPV",        CODEC_TYPE_VIDEO,   CODEC_ID_MPEG1VIDEO, 90000, -1},
81
  {33, "MP2T",       CODEC_TYPE_DATA,    CODEC_ID_MPEG2TS, 90000, -1},
82
  {34, "H263",       CODEC_TYPE_VIDEO,   CODEC_ID_H263, 90000, -1},
83
  {35, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
84
  {36, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
85
  {37, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
86
  {38, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
87
  {39, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
88
  {40, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
89
  {41, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
90
  {42, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
91
  {43, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
92
  {44, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
93
  {45, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
94
  {46, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
95
  {47, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
96
  {48, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
97
  {49, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
98
  {50, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
99
  {51, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
100
  {52, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
101
  {53, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
102
  {54, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
103
  {55, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
104
  {56, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
105
  {57, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
106
  {58, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
107
  {59, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
108
  {60, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
109
  {61, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
110
  {62, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
111
  {63, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
112
  {64, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
113
  {65, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
114
  {66, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
115
  {67, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
116
  {68, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
117
  {69, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
118
  {70, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
119
  {71, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
120
  {72, "reserved for RTCP conflict avoidance", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
121
  {73, "reserved for RTCP conflict avoidance", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
122
  {74, "reserved for RTCP conflict avoidance", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
123
  {75, "reserved for RTCP conflict avoidance", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
124
  {76, "reserved for RTCP conflict avoidance", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
125
  {77, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
126
  {78, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
127
  {79, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
128
  {80, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
129
  {81, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
130
  {82, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
131
  {83, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
132
  {84, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
133
  {85, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
134
  {86, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
135
  {87, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
136
  {88, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
137
  {89, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
138
  {90, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
139
  {91, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
140
  {92, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
141
  {93, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
142
  {94, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
143
  {95, "unassigned", CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
144
  {96, "dynamic",    CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
145
  {97, "dynamic",    CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
146
  {98, "dynamic",    CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
147
  {99, "dynamic",    CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
148
  {100, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
149
  {101, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
150
  {102, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
151
  {103, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
152
  {104, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
153
  {105, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
154
  {106, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
155
  {107, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
156
  {108, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
157
  {109, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
158
  {110, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
159
  {111, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
160
  {112, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
161
  {113, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
162
  {114, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
163
  {115, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
164
  {116, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
165
  {117, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
166
  {118, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
167
  {119, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
168
  {120, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
169
  {121, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
170
  {122, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
171
  {123, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
172
  {124, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
173
  {125, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
174
  {126, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
175
  {127, "dynamic",   CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1},
176
  {-1, "",           CODEC_TYPE_UNKNOWN, CODEC_ID_NONE, -1, -1}
177
};
178

    
179
/* statistics functions */
180
RTPDynamicProtocolHandler *RTPFirstDynamicPayloadHandler= NULL;
181

    
182
static RTPDynamicProtocolHandler mp4v_es_handler= {"MP4V-ES", CODEC_TYPE_VIDEO, CODEC_ID_MPEG4};
183
static RTPDynamicProtocolHandler mpeg4_generic_handler= {"mpeg4-generic", CODEC_TYPE_AUDIO, CODEC_ID_AAC};
184

    
185
static void register_dynamic_payload_handler(RTPDynamicProtocolHandler *handler)
186
{
187
    handler->next= RTPFirstDynamicPayloadHandler;
188
    RTPFirstDynamicPayloadHandler= handler;
189
}
190

    
191
void av_register_rtp_dynamic_payload_handlers(void)
192
{
193
    register_dynamic_payload_handler(&mp4v_es_handler);
194
    register_dynamic_payload_handler(&mpeg4_generic_handler);
195
    register_dynamic_payload_handler(&ff_h264_dynamic_handler);
196
}
197

    
198
int rtp_get_codec_info(AVCodecContext *codec, int payload_type)
199
{
200
    if (AVRtpPayloadTypes[payload_type].codec_id != CODEC_ID_NONE) {
201
        codec->codec_type = AVRtpPayloadTypes[payload_type].codec_type;
202
        codec->codec_id = AVRtpPayloadTypes[payload_type].codec_id;
203
        if (AVRtpPayloadTypes[payload_type].audio_channels > 0)
204
            codec->channels = AVRtpPayloadTypes[payload_type].audio_channels;
205
        if (AVRtpPayloadTypes[payload_type].clock_rate > 0)
206
            codec->sample_rate = AVRtpPayloadTypes[payload_type].clock_rate;
207
        return 0;
208
    }
209
    return -1;
210
}
211

    
212
int rtp_get_payload_type(AVCodecContext *codec)
213
{
214
    int i, payload_type;
215

    
216
    /* compute the payload type */
217
    for (payload_type = -1, i = 0; AVRtpPayloadTypes[i].pt >= 0; ++i)
218
        if (AVRtpPayloadTypes[i].codec_id == codec->codec_id) {
219
            if (codec->codec_id == CODEC_ID_PCM_S16BE)
220
                if (codec->channels != AVRtpPayloadTypes[i].audio_channels)
221
                    continue;
222
            payload_type = AVRtpPayloadTypes[i].pt;
223
        }
224
    return payload_type;
225
}
226

    
227
static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf, int len)
228
{
229
    if (buf[1] != 200)
230
        return -1;
231
    s->last_rtcp_ntp_time = AV_RB64(buf + 8);
232
    if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE)
233
        s->first_rtcp_ntp_time = s->last_rtcp_ntp_time;
234
    s->last_rtcp_timestamp = AV_RB32(buf + 16);
235
    return 0;
236
}
237

    
238
#define RTP_SEQ_MOD (1<<16)
239

    
240
/**
241
* called on parse open packet
242
*/
243
static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence) // called on parse open packet.
244
{
245
    memset(s, 0, sizeof(RTPStatistics));
246
    s->max_seq= base_sequence;
247
    s->probation= 1;
248
}
249

    
250
/**
251
* called whenever there is a large jump in sequence numbers, or when they get out of probation...
252
*/
253
static void rtp_init_sequence(RTPStatistics *s, uint16_t seq)
254
{
255
    s->max_seq= seq;
256
    s->cycles= 0;
257
    s->base_seq= seq -1;
258
    s->bad_seq= RTP_SEQ_MOD + 1;
259
    s->received= 0;
260
    s->expected_prior= 0;
261
    s->received_prior= 0;
262
    s->jitter= 0;
263
    s->transit= 0;
264
}
265

    
266
/**
267
* returns 1 if we should handle this packet.
268
*/
269
static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq)
270
{
271
    uint16_t udelta= seq - s->max_seq;
272
    const int MAX_DROPOUT= 3000;
273
    const int MAX_MISORDER = 100;
274
    const int MIN_SEQUENTIAL = 2;
275

    
276
    /* source not valid until MIN_SEQUENTIAL packets with sequence seq. numbers have been received */
277
    if(s->probation)
278
    {
279
        if(seq==s->max_seq + 1) {
280
            s->probation--;
281
            s->max_seq= seq;
282
            if(s->probation==0) {
283
                rtp_init_sequence(s, seq);
284
                s->received++;
285
                return 1;
286
            }
287
        } else {
288
            s->probation= MIN_SEQUENTIAL - 1;
289
            s->max_seq = seq;
290
        }
291
    } else if (udelta < MAX_DROPOUT) {
292
        // in order, with permissible gap
293
        if(seq < s->max_seq) {
294
            //sequence number wrapped; count antother 64k cycles
295
            s->cycles += RTP_SEQ_MOD;
296
        }
297
        s->max_seq= seq;
298
    } else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) {
299
        // sequence made a large jump...
300
        if(seq==s->bad_seq) {
301
            // two sequential packets-- assume that the other side restarted without telling us; just resync.
302
            rtp_init_sequence(s, seq);
303
        } else {
304
            s->bad_seq= (seq + 1) & (RTP_SEQ_MOD-1);
305
            return 0;
306
        }
307
    } else {
308
        // duplicate or reordered packet...
309
    }
310
    s->received++;
311
    return 1;
312
}
313

    
314
#if 0
315
/**
316
* This function is currently unused; without a valid local ntp time, I don't see how we could calculate the
317
* difference between the arrival and sent timestamp.  As a result, the jitter and transit statistics values
318
* never change.  I left this in in case someone else can see a way. (rdm)
319
*/
320
static void rtcp_update_jitter(RTPStatistics *s, uint32_t sent_timestamp, uint32_t arrival_timestamp)
321
{
322
    uint32_t transit= arrival_timestamp - sent_timestamp;
323
    int d;
324
    s->transit= transit;
325
    d= FFABS(transit - s->transit);
326
    s->jitter += d - ((s->jitter + 8)>>4);
327
}
328
#endif
329

    
330
int rtp_check_and_send_back_rr(RTPDemuxContext *s, int count)
331
{
332
    ByteIOContext pb;
333
    uint8_t *buf;
334
    int len;
335
    int rtcp_bytes;
336
    RTPStatistics *stats= &s->statistics;
337
    uint32_t lost;
338
    uint32_t extended_max;
339
    uint32_t expected_interval;
340
    uint32_t received_interval;
341
    uint32_t lost_interval;
342
    uint32_t expected;
343
    uint32_t fraction;
344
    uint64_t ntp_time= s->last_rtcp_ntp_time; // TODO: Get local ntp time?
345

    
346
    if (!s->rtp_ctx || (count < 1))
347
        return -1;
348

    
349
    /* TODO: I think this is way too often; RFC 1889 has algorithm for this */
350
    /* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */
351
    s->octet_count += count;
352
    rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
353
        RTCP_TX_RATIO_DEN;
354
    rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !?
355
    if (rtcp_bytes < 28)
356
        return -1;
357
    s->last_octet_count = s->octet_count;
358

    
359
    if (url_open_dyn_buf(&pb) < 0)
360
        return -1;
361

    
362
    // Receiver Report
363
    put_byte(&pb, (RTP_VERSION << 6) + 1); /* 1 report block */
364
    put_byte(&pb, 201);
365
    put_be16(&pb, 7); /* length in words - 1 */
366
    put_be32(&pb, s->ssrc); // our own SSRC
367
    put_be32(&pb, s->ssrc); // XXX: should be the server's here!
368
    // some placeholders we should really fill...
369
    // RFC 1889/p64
370
    extended_max= stats->cycles + stats->max_seq;
371
    expected= extended_max - stats->base_seq + 1;
372
    lost= expected - stats->received;
373
    lost= FFMIN(lost, 0xffffff); // clamp it since it's only 24 bits...
374
    expected_interval= expected - stats->expected_prior;
375
    stats->expected_prior= expected;
376
    received_interval= stats->received - stats->received_prior;
377
    stats->received_prior= stats->received;
378
    lost_interval= expected_interval - received_interval;
379
    if (expected_interval==0 || lost_interval<=0) fraction= 0;
380
    else fraction = (lost_interval<<8)/expected_interval;
381

    
382
    fraction= (fraction<<24) | lost;
383

    
384
    put_be32(&pb, fraction); /* 8 bits of fraction, 24 bits of total packets lost */
385
    put_be32(&pb, extended_max); /* max sequence received */
386
    put_be32(&pb, stats->jitter>>4); /* jitter */
387

    
388
    if(s->last_rtcp_ntp_time==AV_NOPTS_VALUE)
389
    {
390
        put_be32(&pb, 0); /* last SR timestamp */
391
        put_be32(&pb, 0); /* delay since last SR */
392
    } else {
393
        uint32_t middle_32_bits= s->last_rtcp_ntp_time>>16; // this is valid, right? do we need to handle 64 bit values special?
394
        uint32_t delay_since_last= ntp_time - s->last_rtcp_ntp_time;
395

    
396
        put_be32(&pb, middle_32_bits); /* last SR timestamp */
397
        put_be32(&pb, delay_since_last); /* delay since last SR */
398
    }
399

    
400
    // CNAME
401
    put_byte(&pb, (RTP_VERSION << 6) + 1); /* 1 report block */
402
    put_byte(&pb, 202);
403
    len = strlen(s->hostname);
404
    put_be16(&pb, (6 + len + 3) / 4); /* length in words - 1 */
405
    put_be32(&pb, s->ssrc);
406
    put_byte(&pb, 0x01);
407
    put_byte(&pb, len);
408
    put_buffer(&pb, s->hostname, len);
409
    // padding
410
    for (len = (6 + len) % 4; len % 4; len++) {
411
        put_byte(&pb, 0);
412
    }
413

    
414
    put_flush_packet(&pb);
415
    len = url_close_dyn_buf(&pb, &buf);
416
    if ((len > 0) && buf) {
417
        int result;
418
#if defined(DEBUG)
419
        printf("sending %d bytes of RR\n", len);
420
#endif
421
        result= url_write(s->rtp_ctx, buf, len);
422
#if defined(DEBUG)
423
        printf("result from url_write: %d\n", result);
424
#endif
425
        av_free(buf);
426
    }
427
    return 0;
428
}
429

    
430
/**
431
 * open a new RTP parse context for stream 'st'. 'st' can be NULL for
432
 * MPEG2TS streams to indicate that they should be demuxed inside the
433
 * rtp demux (otherwise CODEC_ID_MPEG2TS packets are returned)
434
 * TODO: change this to not take rtp_payload data, and use the new dynamic payload system.
435
 */
436
RTPDemuxContext *rtp_parse_open(AVFormatContext *s1, AVStream *st, URLContext *rtpc, int payload_type, rtp_payload_data_t *rtp_payload_data)
437
{
438
    RTPDemuxContext *s;
439

    
440
    s = av_mallocz(sizeof(RTPDemuxContext));
441
    if (!s)
442
        return NULL;
443
    s->payload_type = payload_type;
444
    s->last_rtcp_ntp_time = AV_NOPTS_VALUE;
445
    s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
446
    s->ic = s1;
447
    s->st = st;
448
    s->rtp_payload_data = rtp_payload_data;
449
    rtp_init_statistics(&s->statistics, 0); // do we know the initial sequence from sdp?
450
    if (!strcmp(AVRtpPayloadTypes[payload_type].enc_name, "MP2T")) {
451
        s->ts = mpegts_parse_open(s->ic);
452
        if (s->ts == NULL) {
453
            av_free(s);
454
            return NULL;
455
        }
456
    } else {
457
        switch(st->codec->codec_id) {
458
        case CODEC_ID_MPEG1VIDEO:
459
        case CODEC_ID_MPEG2VIDEO:
460
        case CODEC_ID_MP2:
461
        case CODEC_ID_MP3:
462
        case CODEC_ID_MPEG4:
463
        case CODEC_ID_H264:
464
            st->need_parsing = AVSTREAM_PARSE_FULL;
465
            break;
466
        default:
467
            break;
468
        }
469
    }
470
    // needed to send back RTCP RR in RTSP sessions
471
    s->rtp_ctx = rtpc;
472
    gethostname(s->hostname, sizeof(s->hostname));
473
    return s;
474
}
475

    
476
static int rtp_parse_mp4_au(RTPDemuxContext *s, const uint8_t *buf)
477
{
478
    int au_headers_length, au_header_size, i;
479
    GetBitContext getbitcontext;
480
    rtp_payload_data_t *infos;
481

    
482
    infos = s->rtp_payload_data;
483

    
484
    if (infos == NULL)
485
        return -1;
486

    
487
    /* decode the first 2 bytes where are stored the AUHeader sections
488
       length in bits */
489
    au_headers_length = AV_RB16(buf);
490

    
491
    if (au_headers_length > RTP_MAX_PACKET_LENGTH)
492
      return -1;
493

    
494
    infos->au_headers_length_bytes = (au_headers_length + 7) / 8;
495

    
496
    /* skip AU headers length section (2 bytes) */
497
    buf += 2;
498

    
499
    init_get_bits(&getbitcontext, buf, infos->au_headers_length_bytes * 8);
500

    
501
    /* XXX: Wrong if optionnal additional sections are present (cts, dts etc...) */
502
    au_header_size = infos->sizelength + infos->indexlength;
503
    if (au_header_size <= 0 || (au_headers_length % au_header_size != 0))
504
        return -1;
505

    
506
    infos->nb_au_headers = au_headers_length / au_header_size;
507
    infos->au_headers = av_malloc(sizeof(struct AUHeaders) * infos->nb_au_headers);
508

    
509
    /* XXX: We handle multiple AU Section as only one (need to fix this for interleaving)
510
       In my test, the FAAD decoder does not behave correctly when sending each AU one by one
511
       but does when sending the whole as one big packet...  */
512
    infos->au_headers[0].size = 0;
513
    infos->au_headers[0].index = 0;
514
    for (i = 0; i < infos->nb_au_headers; ++i) {
515
        infos->au_headers[0].size += get_bits_long(&getbitcontext, infos->sizelength);
516
        infos->au_headers[0].index = get_bits_long(&getbitcontext, infos->indexlength);
517
    }
518

    
519
    infos->nb_au_headers = 1;
520

    
521
    return 0;
522
}
523

    
524
/**
525
 * This was the second switch in rtp_parse packet.  Normalizes time, if required, sets stream_index, etc.
526
 */
527
static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
528
{
529
    switch(s->st->codec->codec_id) {
530
        case CODEC_ID_MP2:
531
        case CODEC_ID_MPEG1VIDEO:
532
            if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE) {
533
                int64_t addend;
534

    
535
                int delta_timestamp;
536
                /* XXX: is it really necessary to unify the timestamp base ? */
537
                /* compute pts from timestamp with received ntp_time */
538
                delta_timestamp = timestamp - s->last_rtcp_timestamp;
539
                /* convert to 90 kHz without overflow */
540
                addend = (s->last_rtcp_ntp_time - s->first_rtcp_ntp_time) >> 14;
541
                addend = (addend * 5625) >> 14;
542
                pkt->pts = addend + delta_timestamp;
543
            }
544
            break;
545
        case CODEC_ID_AAC:
546
        case CODEC_ID_H264:
547
        case CODEC_ID_MPEG4:
548
            pkt->pts = timestamp;
549
            break;
550
        default:
551
            /* no timestamp info yet */
552
            break;
553
    }
554
    pkt->stream_index = s->st->index;
555
}
556

    
557
/**
558
 * Parse an RTP or RTCP packet directly sent as a buffer.
559
 * @param s RTP parse context.
560
 * @param pkt returned packet
561
 * @param buf input buffer or NULL to read the next packets
562
 * @param len buffer len
563
 * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
564
 * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
565
 */
566
int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
567
                     const uint8_t *buf, int len)
568
{
569
    unsigned int ssrc, h;
570
    int payload_type, seq, ret;
571
    AVStream *st;
572
    uint32_t timestamp;
573
    int rv= 0;
574

    
575
    if (!buf) {
576
        /* return the next packets, if any */
577
        if(s->st && s->parse_packet) {
578
            timestamp= 0; ///< Should not be used if buf is NULL, but should be set to the timestamp of the packet returned....
579
            rv= s->parse_packet(s, pkt, &timestamp, NULL, 0);
580
            finalize_packet(s, pkt, timestamp);
581
            return rv;
582
        } else {
583
            // TODO: Move to a dynamic packet handler (like above)
584
            if (s->read_buf_index >= s->read_buf_size)
585
                return -1;
586
            ret = mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index,
587
                                      s->read_buf_size - s->read_buf_index);
588
            if (ret < 0)
589
                return -1;
590
            s->read_buf_index += ret;
591
            if (s->read_buf_index < s->read_buf_size)
592
                return 1;
593
            else
594
                return 0;
595
        }
596
    }
597

    
598
    if (len < 12)
599
        return -1;
600

    
601
    if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
602
        return -1;
603
    if (buf[1] >= 200 && buf[1] <= 204) {
604
        rtcp_parse_packet(s, buf, len);
605
        return -1;
606
    }
607
    payload_type = buf[1] & 0x7f;
608
    seq  = AV_RB16(buf + 2);
609
    timestamp = AV_RB32(buf + 4);
610
    ssrc = AV_RB32(buf + 8);
611
    /* store the ssrc in the RTPDemuxContext */
612
    s->ssrc = ssrc;
613

    
614
    /* NOTE: we can handle only one payload type */
615
    if (s->payload_type != payload_type)
616
        return -1;
617

    
618
    st = s->st;
619
    // only do something with this if all the rtp checks pass...
620
    if(!rtp_valid_packet_in_sequence(&s->statistics, seq))
621
    {
622
        av_log(st?st->codec:NULL, AV_LOG_ERROR, "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
623
               payload_type, seq, ((s->seq + 1) & 0xffff));
624
        return -1;
625
    }
626

    
627
    s->seq = seq;
628
    len -= 12;
629
    buf += 12;
630

    
631
    if (!st) {
632
        /* specific MPEG2TS demux support */
633
        ret = mpegts_parse_packet(s->ts, pkt, buf, len);
634
        if (ret < 0)
635
            return -1;
636
        if (ret < len) {
637
            s->read_buf_size = len - ret;
638
            memcpy(s->buf, buf + ret, s->read_buf_size);
639
            s->read_buf_index = 0;
640
            return 1;
641
        }
642
    } else {
643
        // at this point, the RTP header has been stripped;  This is ASSUMING that there is only 1 CSRC, which in't wise.
644
        switch(st->codec->codec_id) {
645
        case CODEC_ID_MP2:
646
            /* better than nothing: skip mpeg audio RTP header */
647
            if (len <= 4)
648
                return -1;
649
            h = AV_RB32(buf);
650
            len -= 4;
651
            buf += 4;
652
            av_new_packet(pkt, len);
653
            memcpy(pkt->data, buf, len);
654
            break;
655
        case CODEC_ID_MPEG1VIDEO:
656
            /* better than nothing: skip mpeg video RTP header */
657
            if (len <= 4)
658
                return -1;
659
            h = AV_RB32(buf);
660
            buf += 4;
661
            len -= 4;
662
            if (h & (1 << 26)) {
663
                /* mpeg2 */
664
                if (len <= 4)
665
                    return -1;
666
                buf += 4;
667
                len -= 4;
668
            }
669
            av_new_packet(pkt, len);
670
            memcpy(pkt->data, buf, len);
671
            break;
672
            // moved from below, verbatim.  this is because this section handles packets, and the lower switch handles
673
            // timestamps.
674
            // TODO: Put this into a dynamic packet handler...
675
        case CODEC_ID_AAC:
676
            if (rtp_parse_mp4_au(s, buf))
677
                return -1;
678
            {
679
                rtp_payload_data_t *infos = s->rtp_payload_data;
680
                if (infos == NULL)
681
                    return -1;
682
                buf += infos->au_headers_length_bytes + 2;
683
                len -= infos->au_headers_length_bytes + 2;
684

    
685
                /* XXX: Fixme we only handle the case where rtp_parse_mp4_au define
686
                    one au_header */
687
                av_new_packet(pkt, infos->au_headers[0].size);
688
                memcpy(pkt->data, buf, infos->au_headers[0].size);
689
                buf += infos->au_headers[0].size;
690
                len -= infos->au_headers[0].size;
691
            }
692
            s->read_buf_size = len;
693
            s->buf_ptr = buf;
694
            rv= 0;
695
            break;
696
        default:
697
            if(s->parse_packet) {
698
                rv= s->parse_packet(s, pkt, &timestamp, buf, len);
699
            } else {
700
                av_new_packet(pkt, len);
701
                memcpy(pkt->data, buf, len);
702
            }
703
            break;
704
        }
705

    
706
        // now perform timestamp things....
707
        finalize_packet(s, pkt, timestamp);
708
    }
709
    return rv;
710
}
711

    
712
void rtp_parse_close(RTPDemuxContext *s)
713
{
714
    // TODO: fold this into the protocol specific data fields.
715
    if (!strcmp(AVRtpPayloadTypes[s->payload_type].enc_name, "MP2T")) {
716
        mpegts_parse_close(s->ts);
717
    }
718
    av_free(s);
719
}
720

    
721
/* rtp output */
722

    
723
static int rtp_write_header(AVFormatContext *s1)
724
{
725
    RTPDemuxContext *s = s1->priv_data;
726
    int payload_type, max_packet_size, n;
727
    AVStream *st;
728

    
729
    if (s1->nb_streams != 1)
730
        return -1;
731
    st = s1->streams[0];
732

    
733
    payload_type = rtp_get_payload_type(st->codec);
734
    if (payload_type < 0)
735
        payload_type = RTP_PT_PRIVATE; /* private payload type */
736
    s->payload_type = payload_type;
737

    
738
// following 2 FIXMies could be set based on the current time, theres normaly no info leak, as rtp will likely be transmitted immedeatly
739
    s->base_timestamp = 0; /* FIXME: was random(), what should this be? */
740
    s->timestamp = s->base_timestamp;
741
    s->cur_timestamp = 0;
742
    s->ssrc = 0; /* FIXME: was random(), what should this be? */
743
    s->first_packet = 1;
744
    s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
745

    
746
    max_packet_size = url_fget_max_packet_size(&s1->pb);
747
    if (max_packet_size <= 12)
748
        return AVERROR(EIO);
749
    s->max_payload_size = max_packet_size - 12;
750

    
751
    av_set_pts_info(st, 32, 1, 90000);
752
    switch(st->codec->codec_id) {
753
    case CODEC_ID_MP2:
754
    case CODEC_ID_MP3:
755
        s->buf_ptr = s->buf + 4;
756
        break;
757
    case CODEC_ID_MPEG1VIDEO:
758
        break;
759
    case CODEC_ID_MPEG2TS:
760
        n = s->max_payload_size / TS_PACKET_SIZE;
761
        if (n < 1)
762
            n = 1;
763
        s->max_payload_size = n * TS_PACKET_SIZE;
764
        s->buf_ptr = s->buf;
765
        break;
766
    case CODEC_ID_AAC:
767
        s->read_buf_index = 0;
768
    default:
769
        if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
770
            av_set_pts_info(st, 32, 1, st->codec->sample_rate);
771
        }
772
        s->buf_ptr = s->buf;
773
        break;
774
    }
775

    
776
    return 0;
777
}
778

    
779
/* send an rtcp sender report packet */
780
static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time)
781
{
782
    RTPDemuxContext *s = s1->priv_data;
783
    uint32_t rtp_ts;
784

    
785
#if defined(DEBUG)
786
    printf("RTCP: %02x %"PRIx64" %x\n", s->payload_type, ntp_time, s->timestamp);
787
#endif
788

    
789
    if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE) s->first_rtcp_ntp_time = ntp_time;
790
    rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, AV_TIME_BASE_Q,
791
                          s1->streams[0]->time_base) + s->base_timestamp;
792
    put_byte(&s1->pb, (RTP_VERSION << 6));
793
    put_byte(&s1->pb, 200);
794
    put_be16(&s1->pb, 6); /* length in words - 1 */
795
    put_be32(&s1->pb, s->ssrc);
796
    put_be32(&s1->pb, ntp_time / 1000000);
797
    put_be32(&s1->pb, ((ntp_time % 1000000) << 32) / 1000000);
798
    put_be32(&s1->pb, rtp_ts);
799
    put_be32(&s1->pb, s->packet_count);
800
    put_be32(&s1->pb, s->octet_count);
801
    put_flush_packet(&s1->pb);
802
}
803

    
804
/* send an rtp packet. sequence number is incremented, but the caller
805
   must update the timestamp itself */
806
void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
807
{
808
    RTPDemuxContext *s = s1->priv_data;
809

    
810
#ifdef DEBUG
811
    printf("rtp_send_data size=%d\n", len);
812
#endif
813

    
814
    /* build the RTP header */
815
    put_byte(&s1->pb, (RTP_VERSION << 6));
816
    put_byte(&s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
817
    put_be16(&s1->pb, s->seq);
818
    put_be32(&s1->pb, s->timestamp);
819
    put_be32(&s1->pb, s->ssrc);
820

    
821
    put_buffer(&s1->pb, buf1, len);
822
    put_flush_packet(&s1->pb);
823

    
824
    s->seq++;
825
    s->octet_count += len;
826
    s->packet_count++;
827
}
828

    
829
/* send an integer number of samples and compute time stamp and fill
830
   the rtp send buffer before sending. */
831
static void rtp_send_samples(AVFormatContext *s1,
832
                             const uint8_t *buf1, int size, int sample_size)
833
{
834
    RTPDemuxContext *s = s1->priv_data;
835
    int len, max_packet_size, n;
836

    
837
    max_packet_size = (s->max_payload_size / sample_size) * sample_size;
838
    /* not needed, but who nows */
839
    if ((size % sample_size) != 0)
840
        av_abort();
841
    n = 0;
842
    while (size > 0) {
843
        s->buf_ptr = s->buf;
844
        len = FFMIN(max_packet_size, size);
845

    
846
        /* copy data */
847
        memcpy(s->buf_ptr, buf1, len);
848
        s->buf_ptr += len;
849
        buf1 += len;
850
        size -= len;
851
        s->timestamp = s->cur_timestamp + n / sample_size;
852
        ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
853
        n += (s->buf_ptr - s->buf);
854
    }
855
}
856

    
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
                               const uint8_t *buf1, int size)
861
{
862
    RTPDemuxContext *s = s1->priv_data;
863
    int len, count, max_packet_size;
864

    
865
    max_packet_size = s->max_payload_size;
866

    
867
    /* test if we must flush because not enough space */
868
    len = (s->buf_ptr - s->buf);
869
    if ((len + size) > max_packet_size) {
870
        if (len > 4) {
871
            ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
872
            s->buf_ptr = s->buf + 4;
873
        }
874
    }
875
    if (s->buf_ptr == s->buf + 4) {
876
        s->timestamp = s->cur_timestamp;
877
    }
878

    
879
    /* add the packet */
880
    if (size > max_packet_size) {
881
        /* big packet: fragment */
882
        count = 0;
883
        while (size > 0) {
884
            len = max_packet_size - 4;
885
            if (len > size)
886
                len = size;
887
            /* build fragmented packet */
888
            s->buf[0] = 0;
889
            s->buf[1] = 0;
890
            s->buf[2] = count >> 8;
891
            s->buf[3] = count;
892
            memcpy(s->buf + 4, buf1, len);
893
            ff_rtp_send_data(s1, s->buf, len + 4, 0);
894
            size -= len;
895
            buf1 += len;
896
            count += len;
897
        }
898
    } else {
899
        if (s->buf_ptr == s->buf + 4) {
900
            /* no fragmentation possible */
901
            s->buf[0] = 0;
902
            s->buf[1] = 0;
903
            s->buf[2] = 0;
904
            s->buf[3] = 0;
905
        }
906
        memcpy(s->buf_ptr, buf1, size);
907
        s->buf_ptr += size;
908
    }
909
}
910

    
911
static void rtp_send_raw(AVFormatContext *s1,
912
                         const uint8_t *buf1, int size)
913
{
914
    RTPDemuxContext *s = s1->priv_data;
915
    int len, max_packet_size;
916

    
917
    max_packet_size = s->max_payload_size;
918

    
919
    while (size > 0) {
920
        len = max_packet_size;
921
        if (len > size)
922
            len = size;
923

    
924
        s->timestamp = s->cur_timestamp;
925
        ff_rtp_send_data(s1, buf1, len, (len == size));
926

    
927
        buf1 += len;
928
        size -= len;
929
    }
930
}
931

    
932
/* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
933
static void rtp_send_mpegts_raw(AVFormatContext *s1,
934
                                const uint8_t *buf1, int size)
935
{
936
    RTPDemuxContext *s = s1->priv_data;
937
    int len, out_len;
938

    
939
    while (size >= TS_PACKET_SIZE) {
940
        len = s->max_payload_size - (s->buf_ptr - s->buf);
941
        if (len > size)
942
            len = size;
943
        memcpy(s->buf_ptr, buf1, len);
944
        buf1 += len;
945
        size -= len;
946
        s->buf_ptr += len;
947

    
948
        out_len = s->buf_ptr - s->buf;
949
        if (out_len >= s->max_payload_size) {
950
            ff_rtp_send_data(s1, s->buf, out_len, 0);
951
            s->buf_ptr = s->buf;
952
        }
953
    }
954
}
955

    
956
/* write an RTP packet. 'buf1' must contain a single specific frame. */
957
static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
958
{
959
    RTPDemuxContext *s = s1->priv_data;
960
    AVStream *st = s1->streams[0];
961
    int rtcp_bytes;
962
    int size= pkt->size;
963
    uint8_t *buf1= pkt->data;
964

    
965
#ifdef DEBUG
966
    printf("%d: write len=%d\n", pkt->stream_index, size);
967
#endif
968

    
969
    /* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */
970
    rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
971
        RTCP_TX_RATIO_DEN;
972
    if (s->first_packet || rtcp_bytes >= 28) {
973
        rtcp_send_sr(s1, av_gettime());
974
        s->last_octet_count = s->octet_count;
975
        s->first_packet = 0;
976
    }
977
    s->cur_timestamp = s->base_timestamp + pkt->pts;
978

    
979
    switch(st->codec->codec_id) {
980
    case CODEC_ID_PCM_MULAW:
981
    case CODEC_ID_PCM_ALAW:
982
    case CODEC_ID_PCM_U8:
983
    case CODEC_ID_PCM_S8:
984
        rtp_send_samples(s1, buf1, size, 1 * st->codec->channels);
985
        break;
986
    case CODEC_ID_PCM_U16BE:
987
    case CODEC_ID_PCM_U16LE:
988
    case CODEC_ID_PCM_S16BE:
989
    case CODEC_ID_PCM_S16LE:
990
        rtp_send_samples(s1, buf1, size, 2 * st->codec->channels);
991
        break;
992
    case CODEC_ID_MP2:
993
    case CODEC_ID_MP3:
994
        rtp_send_mpegaudio(s1, buf1, size);
995
        break;
996
    case CODEC_ID_MPEG1VIDEO:
997
        ff_rtp_send_mpegvideo(s1, buf1, size);
998
        break;
999
    case CODEC_ID_AAC:
1000
        ff_rtp_send_aac(s1, buf1, size);
1001
        break;
1002
    case CODEC_ID_MPEG2TS:
1003
        rtp_send_mpegts_raw(s1, buf1, size);
1004
        break;
1005
    default:
1006
        /* better than nothing : send the codec raw data */
1007
        rtp_send_raw(s1, buf1, size);
1008
        break;
1009
    }
1010
    return 0;
1011
}
1012

    
1013
AVOutputFormat rtp_muxer = {
1014
    "rtp",
1015
    "RTP output format",
1016
    NULL,
1017
    NULL,
1018
    sizeof(RTPDemuxContext),
1019
    CODEC_ID_PCM_MULAW,
1020
    CODEC_ID_NONE,
1021
    rtp_write_header,
1022
    rtp_write_packet,
1023
};