Statistics
| Branch: | Revision:

ffmpeg / libavformat / rtp.c @ af74c95a

History | View | Annotate | Download (36 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

    
32
//#define DEBUG
33

    
34

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

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

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

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

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

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

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

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

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

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

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

    
237
#define RTP_SEQ_MOD (1<<16)
238

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
481
    infos = s->rtp_payload_data;
482

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

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

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

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

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

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

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

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

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

    
518
    infos->nb_au_headers = 1;
519

    
520
    return 0;
521
}
522

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

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

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

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

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

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

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

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

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

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

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

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

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

    
720
/* rtp output */
721

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

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

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

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

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

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

    
773
    return 0;
774
}
775

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

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

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

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

    
807
#ifdef DEBUG
808
    printf("rtp_send_data size=%d\n", len);
809
#endif
810

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

    
818
    put_buffer(&s1->pb, buf1, len);
819
    put_flush_packet(&s1->pb);
820

    
821
    s->seq++;
822
    s->octet_count += len;
823
    s->packet_count++;
824
}
825

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

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

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

    
854
/* NOTE: we suppose that exactly one frame is given as argument here */
855
/* XXX: test it */
856
static void rtp_send_mpegaudio(AVFormatContext *s1,
857
                               const uint8_t *buf1, int size)
858
{
859
    RTPDemuxContext *s = s1->priv_data;
860
    int len, count, max_packet_size;
861

    
862
    max_packet_size = s->max_payload_size;
863

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

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

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

    
914
    max_packet_size = s->max_payload_size;
915

    
916
    while (size > 0) {
917
        len = max_packet_size;
918
        if (len > size)
919
            len = size;
920

    
921
        s->timestamp = s->cur_timestamp;
922
        ff_rtp_send_data(s1, buf1, len, (len == size));
923

    
924
        buf1 += len;
925
        size -= len;
926
    }
927
}
928

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

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

    
945
        out_len = s->buf_ptr - s->buf;
946
        if (out_len >= s->max_payload_size) {
947
            ff_rtp_send_data(s1, s->buf, out_len, 0);
948
            s->buf_ptr = s->buf;
949
        }
950
    }
951
}
952

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

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

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

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

    
1007
AVOutputFormat rtp_muxer = {
1008
    "rtp",
1009
    "RTP output format",
1010
    NULL,
1011
    NULL,
1012
    sizeof(RTPDemuxContext),
1013
    CODEC_ID_PCM_MULAW,
1014
    CODEC_ID_NONE,
1015
    rtp_write_header,
1016
    rtp_write_packet,
1017
};