Statistics
| Branch: | Revision:

ffmpeg / libavformat / rtpenc.c @ dfd2a005

History | View | Annotate | Download (13.6 KB)

1
/*
2
 * RTP 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

    
22
#include "avformat.h"
23
#include "mpegts.h"
24
#include "internal.h"
25
#include "libavutil/random_seed.h"
26

    
27
#include "rtpenc.h"
28

    
29
//#define DEBUG
30

    
31
#define RTCP_SR_SIZE 28
32

    
33
static int is_supported(enum CodecID id)
34
{
35
    switch(id) {
36
    case CODEC_ID_H263:
37
    case CODEC_ID_H263P:
38
    case CODEC_ID_H264:
39
    case CODEC_ID_MPEG1VIDEO:
40
    case CODEC_ID_MPEG2VIDEO:
41
    case CODEC_ID_MPEG4:
42
    case CODEC_ID_AAC:
43
    case CODEC_ID_MP2:
44
    case CODEC_ID_MP3:
45
    case CODEC_ID_PCM_ALAW:
46
    case CODEC_ID_PCM_MULAW:
47
    case CODEC_ID_PCM_S8:
48
    case CODEC_ID_PCM_S16BE:
49
    case CODEC_ID_PCM_S16LE:
50
    case CODEC_ID_PCM_U16BE:
51
    case CODEC_ID_PCM_U16LE:
52
    case CODEC_ID_PCM_U8:
53
    case CODEC_ID_MPEG2TS:
54
    case CODEC_ID_AMR_NB:
55
    case CODEC_ID_AMR_WB:
56
    case CODEC_ID_VORBIS:
57
    case CODEC_ID_THEORA:
58
    case CODEC_ID_VP8:
59
    case CODEC_ID_ADPCM_G722:
60
        return 1;
61
    default:
62
        return 0;
63
    }
64
}
65

    
66
static int rtp_write_header(AVFormatContext *s1)
67
{
68
    RTPMuxContext *s = s1->priv_data;
69
    int max_packet_size, n;
70
    AVStream *st;
71

    
72
    if (s1->nb_streams != 1)
73
        return -1;
74
    st = s1->streams[0];
75
    if (!is_supported(st->codec->codec_id)) {
76
        av_log(s1, AV_LOG_ERROR, "Unsupported codec %x\n", st->codec->codec_id);
77

    
78
        return -1;
79
    }
80

    
81
    s->payload_type = ff_rtp_get_payload_type(st->codec);
82
    if (s->payload_type < 0)
83
        s->payload_type = RTP_PT_PRIVATE + (st->codec->codec_type == AVMEDIA_TYPE_AUDIO);
84

    
85
    s->base_timestamp = av_get_random_seed();
86
    s->timestamp = s->base_timestamp;
87
    s->cur_timestamp = 0;
88
    s->ssrc = av_get_random_seed();
89
    s->first_packet = 1;
90
    s->first_rtcp_ntp_time = ff_ntp_time();
91
    if (s1->start_time_realtime)
92
        /* Round the NTP time to whole milliseconds. */
93
        s->first_rtcp_ntp_time = (s1->start_time_realtime / 1000) * 1000 +
94
                                 NTP_OFFSET_US;
95

    
96
    max_packet_size = url_fget_max_packet_size(s1->pb);
97
    if (max_packet_size <= 12)
98
        return AVERROR(EIO);
99
    s->buf = av_malloc(max_packet_size);
100
    if (s->buf == NULL) {
101
        return AVERROR(ENOMEM);
102
    }
103
    s->max_payload_size = max_packet_size - 12;
104

    
105
    s->max_frames_per_packet = 0;
106
    if (s1->max_delay) {
107
        if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
108
            if (st->codec->frame_size == 0) {
109
                av_log(s1, AV_LOG_ERROR, "Cannot respect max delay: frame size = 0\n");
110
            } else {
111
                s->max_frames_per_packet = av_rescale_rnd(s1->max_delay, st->codec->sample_rate, AV_TIME_BASE * st->codec->frame_size, AV_ROUND_DOWN);
112
            }
113
        }
114
        if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
115
            /* FIXME: We should round down here... */
116
            s->max_frames_per_packet = av_rescale_q(s1->max_delay, (AVRational){1, 1000000}, st->codec->time_base);
117
        }
118
    }
119

    
120
    av_set_pts_info(st, 32, 1, 90000);
121
    switch(st->codec->codec_id) {
122
    case CODEC_ID_MP2:
123
    case CODEC_ID_MP3:
124
        s->buf_ptr = s->buf + 4;
125
        break;
126
    case CODEC_ID_MPEG1VIDEO:
127
    case CODEC_ID_MPEG2VIDEO:
128
        break;
129
    case CODEC_ID_MPEG2TS:
130
        n = s->max_payload_size / TS_PACKET_SIZE;
131
        if (n < 1)
132
            n = 1;
133
        s->max_payload_size = n * TS_PACKET_SIZE;
134
        s->buf_ptr = s->buf;
135
        break;
136
    case CODEC_ID_H264:
137
        /* check for H.264 MP4 syntax */
138
        if (st->codec->extradata_size > 4 && st->codec->extradata[0] == 1) {
139
            s->nal_length_size = (st->codec->extradata[4] & 0x03) + 1;
140
        }
141
        break;
142
    case CODEC_ID_VORBIS:
143
    case CODEC_ID_THEORA:
144
        if (!s->max_frames_per_packet) s->max_frames_per_packet = 15;
145
        s->max_frames_per_packet = av_clip(s->max_frames_per_packet, 1, 15);
146
        s->max_payload_size -= 6; // ident+frag+tdt/vdt+pkt_num+pkt_length
147
        s->num_frames = 0;
148
        goto defaultcase;
149
    case CODEC_ID_VP8:
150
        av_log(s1, AV_LOG_ERROR, "RTP VP8 payload implementation is "
151
                                 "incompatible with the latest spec drafts.\n");
152
        break;
153
    case CODEC_ID_ADPCM_G722:
154
        /* Due to a historical error, the clock rate for G722 in RTP is
155
         * 8000, even if the sample rate is 16000. See RFC 3551. */
156
        av_set_pts_info(st, 32, 1, 8000);
157
        break;
158
    case CODEC_ID_AMR_NB:
159
    case CODEC_ID_AMR_WB:
160
        if (!s->max_frames_per_packet)
161
            s->max_frames_per_packet = 12;
162
        if (st->codec->codec_id == CODEC_ID_AMR_NB)
163
            n = 31;
164
        else
165
            n = 61;
166
        /* max_header_toc_size + the largest AMR payload must fit */
167
        if (1 + s->max_frames_per_packet + n > s->max_payload_size) {
168
            av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for AMR\n");
169
            return -1;
170
        }
171
        if (st->codec->channels != 1) {
172
            av_log(s1, AV_LOG_ERROR, "Only mono is supported\n");
173
            return -1;
174
        }
175
    case CODEC_ID_AAC:
176
        s->num_frames = 0;
177
    default:
178
defaultcase:
179
        if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
180
            av_set_pts_info(st, 32, 1, st->codec->sample_rate);
181
        }
182
        s->buf_ptr = s->buf;
183
        break;
184
    }
185

    
186
    return 0;
187
}
188

    
189
/* send an rtcp sender report packet */
190
static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time)
191
{
192
    RTPMuxContext *s = s1->priv_data;
193
    uint32_t rtp_ts;
194

    
195
    av_dlog(s1, "RTCP: %02x %"PRIx64" %x\n", s->payload_type, ntp_time, s->timestamp);
196

    
197
    s->last_rtcp_ntp_time = ntp_time;
198
    rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, (AVRational){1, 1000000},
199
                          s1->streams[0]->time_base) + s->base_timestamp;
200
    put_byte(s1->pb, (RTP_VERSION << 6));
201
    put_byte(s1->pb, RTCP_SR);
202
    put_be16(s1->pb, 6); /* length in words - 1 */
203
    put_be32(s1->pb, s->ssrc);
204
    put_be32(s1->pb, ntp_time / 1000000);
205
    put_be32(s1->pb, ((ntp_time % 1000000) << 32) / 1000000);
206
    put_be32(s1->pb, rtp_ts);
207
    put_be32(s1->pb, s->packet_count);
208
    put_be32(s1->pb, s->octet_count);
209
    put_flush_packet(s1->pb);
210
}
211

    
212
/* send an rtp packet. sequence number is incremented, but the caller
213
   must update the timestamp itself */
214
void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
215
{
216
    RTPMuxContext *s = s1->priv_data;
217

    
218
    av_dlog(s1, "rtp_send_data size=%d\n", len);
219

    
220
    /* build the RTP header */
221
    put_byte(s1->pb, (RTP_VERSION << 6));
222
    put_byte(s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
223
    put_be16(s1->pb, s->seq);
224
    put_be32(s1->pb, s->timestamp);
225
    put_be32(s1->pb, s->ssrc);
226

    
227
    put_buffer(s1->pb, buf1, len);
228
    put_flush_packet(s1->pb);
229

    
230
    s->seq++;
231
    s->octet_count += len;
232
    s->packet_count++;
233
}
234

    
235
/* send an integer number of samples and compute time stamp and fill
236
   the rtp send buffer before sending. */
237
static void rtp_send_samples(AVFormatContext *s1,
238
                             const uint8_t *buf1, int size, int sample_size)
239
{
240
    RTPMuxContext *s = s1->priv_data;
241
    int len, max_packet_size, n;
242

    
243
    max_packet_size = (s->max_payload_size / sample_size) * sample_size;
244
    /* not needed, but who nows */
245
    if ((size % sample_size) != 0)
246
        av_abort();
247
    n = 0;
248
    while (size > 0) {
249
        s->buf_ptr = s->buf;
250
        len = FFMIN(max_packet_size, size);
251

    
252
        /* copy data */
253
        memcpy(s->buf_ptr, buf1, len);
254
        s->buf_ptr += len;
255
        buf1 += len;
256
        size -= len;
257
        s->timestamp = s->cur_timestamp + n / sample_size;
258
        ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
259
        n += (s->buf_ptr - s->buf);
260
    }
261
}
262

    
263
static void rtp_send_mpegaudio(AVFormatContext *s1,
264
                               const uint8_t *buf1, int size)
265
{
266
    RTPMuxContext *s = s1->priv_data;
267
    int len, count, max_packet_size;
268

    
269
    max_packet_size = s->max_payload_size;
270

    
271
    /* test if we must flush because not enough space */
272
    len = (s->buf_ptr - s->buf);
273
    if ((len + size) > max_packet_size) {
274
        if (len > 4) {
275
            ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
276
            s->buf_ptr = s->buf + 4;
277
        }
278
    }
279
    if (s->buf_ptr == s->buf + 4) {
280
        s->timestamp = s->cur_timestamp;
281
    }
282

    
283
    /* add the packet */
284
    if (size > max_packet_size) {
285
        /* big packet: fragment */
286
        count = 0;
287
        while (size > 0) {
288
            len = max_packet_size - 4;
289
            if (len > size)
290
                len = size;
291
            /* build fragmented packet */
292
            s->buf[0] = 0;
293
            s->buf[1] = 0;
294
            s->buf[2] = count >> 8;
295
            s->buf[3] = count;
296
            memcpy(s->buf + 4, buf1, len);
297
            ff_rtp_send_data(s1, s->buf, len + 4, 0);
298
            size -= len;
299
            buf1 += len;
300
            count += len;
301
        }
302
    } else {
303
        if (s->buf_ptr == s->buf + 4) {
304
            /* no fragmentation possible */
305
            s->buf[0] = 0;
306
            s->buf[1] = 0;
307
            s->buf[2] = 0;
308
            s->buf[3] = 0;
309
        }
310
        memcpy(s->buf_ptr, buf1, size);
311
        s->buf_ptr += size;
312
    }
313
}
314

    
315
static void rtp_send_raw(AVFormatContext *s1,
316
                         const uint8_t *buf1, int size)
317
{
318
    RTPMuxContext *s = s1->priv_data;
319
    int len, max_packet_size;
320

    
321
    max_packet_size = s->max_payload_size;
322

    
323
    while (size > 0) {
324
        len = max_packet_size;
325
        if (len > size)
326
            len = size;
327

    
328
        s->timestamp = s->cur_timestamp;
329
        ff_rtp_send_data(s1, buf1, len, (len == size));
330

    
331
        buf1 += len;
332
        size -= len;
333
    }
334
}
335

    
336
/* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
337
static void rtp_send_mpegts_raw(AVFormatContext *s1,
338
                                const uint8_t *buf1, int size)
339
{
340
    RTPMuxContext *s = s1->priv_data;
341
    int len, out_len;
342

    
343
    while (size >= TS_PACKET_SIZE) {
344
        len = s->max_payload_size - (s->buf_ptr - s->buf);
345
        if (len > size)
346
            len = size;
347
        memcpy(s->buf_ptr, buf1, len);
348
        buf1 += len;
349
        size -= len;
350
        s->buf_ptr += len;
351

    
352
        out_len = s->buf_ptr - s->buf;
353
        if (out_len >= s->max_payload_size) {
354
            ff_rtp_send_data(s1, s->buf, out_len, 0);
355
            s->buf_ptr = s->buf;
356
        }
357
    }
358
}
359

    
360
static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
361
{
362
    RTPMuxContext *s = s1->priv_data;
363
    AVStream *st = s1->streams[0];
364
    int rtcp_bytes;
365
    int size= pkt->size;
366

    
367
    av_dlog(s1, "%d: write len=%d\n", pkt->stream_index, size);
368

    
369
    rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
370
        RTCP_TX_RATIO_DEN;
371
    if (s->first_packet || ((rtcp_bytes >= RTCP_SR_SIZE) &&
372
                           (ff_ntp_time() - s->last_rtcp_ntp_time > 5000000))) {
373
        rtcp_send_sr(s1, ff_ntp_time());
374
        s->last_octet_count = s->octet_count;
375
        s->first_packet = 0;
376
    }
377
    s->cur_timestamp = s->base_timestamp + pkt->pts;
378

    
379
    switch(st->codec->codec_id) {
380
    case CODEC_ID_PCM_MULAW:
381
    case CODEC_ID_PCM_ALAW:
382
    case CODEC_ID_PCM_U8:
383
    case CODEC_ID_PCM_S8:
384
        rtp_send_samples(s1, pkt->data, size, 1 * st->codec->channels);
385
        break;
386
    case CODEC_ID_PCM_U16BE:
387
    case CODEC_ID_PCM_U16LE:
388
    case CODEC_ID_PCM_S16BE:
389
    case CODEC_ID_PCM_S16LE:
390
        rtp_send_samples(s1, pkt->data, size, 2 * st->codec->channels);
391
        break;
392
    case CODEC_ID_ADPCM_G722:
393
        /* The actual sample size is half a byte per sample, but since the
394
         * stream clock rate is 8000 Hz while the sample rate is 16000 Hz,
395
         * the correct parameter for send_samples is 1 byte per stream clock. */
396
        rtp_send_samples(s1, pkt->data, size, 1 * st->codec->channels);
397
        break;
398
    case CODEC_ID_MP2:
399
    case CODEC_ID_MP3:
400
        rtp_send_mpegaudio(s1, pkt->data, size);
401
        break;
402
    case CODEC_ID_MPEG1VIDEO:
403
    case CODEC_ID_MPEG2VIDEO:
404
        ff_rtp_send_mpegvideo(s1, pkt->data, size);
405
        break;
406
    case CODEC_ID_AAC:
407
        ff_rtp_send_aac(s1, pkt->data, size);
408
        break;
409
    case CODEC_ID_AMR_NB:
410
    case CODEC_ID_AMR_WB:
411
        ff_rtp_send_amr(s1, pkt->data, size);
412
        break;
413
    case CODEC_ID_MPEG2TS:
414
        rtp_send_mpegts_raw(s1, pkt->data, size);
415
        break;
416
    case CODEC_ID_H264:
417
        ff_rtp_send_h264(s1, pkt->data, size);
418
        break;
419
    case CODEC_ID_H263:
420
    case CODEC_ID_H263P:
421
        ff_rtp_send_h263(s1, pkt->data, size);
422
        break;
423
    case CODEC_ID_VORBIS:
424
    case CODEC_ID_THEORA:
425
        ff_rtp_send_xiph(s1, pkt->data, size);
426
        break;
427
    case CODEC_ID_VP8:
428
        ff_rtp_send_vp8(s1, pkt->data, size);
429
        break;
430
    default:
431
        /* better than nothing : send the codec raw data */
432
        rtp_send_raw(s1, pkt->data, size);
433
        break;
434
    }
435
    return 0;
436
}
437

    
438
static int rtp_write_trailer(AVFormatContext *s1)
439
{
440
    RTPMuxContext *s = s1->priv_data;
441

    
442
    av_freep(&s->buf);
443

    
444
    return 0;
445
}
446

    
447
AVOutputFormat ff_rtp_muxer = {
448
    "rtp",
449
    NULL_IF_CONFIG_SMALL("RTP output format"),
450
    NULL,
451
    NULL,
452
    sizeof(RTPMuxContext),
453
    CODEC_ID_PCM_MULAW,
454
    CODEC_ID_NONE,
455
    rtp_write_header,
456
    rtp_write_packet,
457
    rtp_write_trailer,
458
};