Statistics
| Branch: | Revision:

ffmpeg / libavformat / rtpenc.c @ 8b889b34

History | View | Annotate | Download (12.3 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 <unistd.h>
28

    
29
#include "rtpenc.h"
30

    
31
//#define DEBUG
32

    
33
#define RTCP_SR_SIZE 28
34

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

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

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

    
76
        return -1;
77
    }
78

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

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

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

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

    
118
    av_set_pts_info(st, 32, 1, 90000);
119
    switch(st->codec->codec_id) {
120
    case CODEC_ID_MP2:
121
    case CODEC_ID_MP3:
122
        s->buf_ptr = s->buf + 4;
123
        break;
124
    case CODEC_ID_MPEG1VIDEO:
125
    case CODEC_ID_MPEG2VIDEO:
126
        break;
127
    case CODEC_ID_MPEG2TS:
128
        n = s->max_payload_size / TS_PACKET_SIZE;
129
        if (n < 1)
130
            n = 1;
131
        s->max_payload_size = n * TS_PACKET_SIZE;
132
        s->buf_ptr = s->buf;
133
        break;
134
    case CODEC_ID_H264:
135
        /* check for H.264 MP4 syntax */
136
        if (st->codec->extradata_size > 0 && st->codec->extradata &&
137
            st->codec->extradata[0] == 1) {
138
            s->nal_length_size = (st->codec->extradata[4] & 0x03) + 1;
139
        }
140
        break;
141
    case CODEC_ID_AMR_NB:
142
    case CODEC_ID_AMR_WB:
143
        if (!s->max_frames_per_packet)
144
            s->max_frames_per_packet = 12;
145
        if (st->codec->codec_id == CODEC_ID_AMR_NB)
146
            n = 31;
147
        else
148
            n = 61;
149
        /* max_header_toc_size + the largest AMR payload must fit */
150
        if (1 + s->max_frames_per_packet + n > s->max_payload_size) {
151
            av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for AMR\n");
152
            return -1;
153
        }
154
        if (st->codec->channels != 1) {
155
            av_log(s1, AV_LOG_ERROR, "Only mono is supported\n");
156
            return -1;
157
        }
158
    case CODEC_ID_AAC:
159
        s->num_frames = 0;
160
    default:
161
        if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
162
            av_set_pts_info(st, 32, 1, st->codec->sample_rate);
163
        }
164
        s->buf_ptr = s->buf;
165
        break;
166
    }
167

    
168
    return 0;
169
}
170

    
171
/* send an rtcp sender report packet */
172
static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time)
173
{
174
    RTPMuxContext *s = s1->priv_data;
175
    uint32_t rtp_ts;
176

    
177
    dprintf(s1, "RTCP: %02x %"PRIx64" %x\n", s->payload_type, ntp_time, s->timestamp);
178

    
179
    s->last_rtcp_ntp_time = ntp_time;
180
    rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, (AVRational){1, 1000000},
181
                          s1->streams[0]->time_base) + s->base_timestamp;
182
    put_byte(s1->pb, (RTP_VERSION << 6));
183
    put_byte(s1->pb, 200);
184
    put_be16(s1->pb, 6); /* length in words - 1 */
185
    put_be32(s1->pb, s->ssrc);
186
    put_be32(s1->pb, ntp_time / 1000000);
187
    put_be32(s1->pb, ((ntp_time % 1000000) << 32) / 1000000);
188
    put_be32(s1->pb, rtp_ts);
189
    put_be32(s1->pb, s->packet_count);
190
    put_be32(s1->pb, s->octet_count);
191
    put_flush_packet(s1->pb);
192
}
193

    
194
/* send an rtp packet. sequence number is incremented, but the caller
195
   must update the timestamp itself */
196
void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
197
{
198
    RTPMuxContext *s = s1->priv_data;
199

    
200
    dprintf(s1, "rtp_send_data size=%d\n", len);
201

    
202
    /* build the RTP header */
203
    put_byte(s1->pb, (RTP_VERSION << 6));
204
    put_byte(s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
205
    put_be16(s1->pb, s->seq);
206
    put_be32(s1->pb, s->timestamp);
207
    put_be32(s1->pb, s->ssrc);
208

    
209
    put_buffer(s1->pb, buf1, len);
210
    put_flush_packet(s1->pb);
211

    
212
    s->seq++;
213
    s->octet_count += len;
214
    s->packet_count++;
215
}
216

    
217
/* send an integer number of samples and compute time stamp and fill
218
   the rtp send buffer before sending. */
219
static void rtp_send_samples(AVFormatContext *s1,
220
                             const uint8_t *buf1, int size, int sample_size)
221
{
222
    RTPMuxContext *s = s1->priv_data;
223
    int len, max_packet_size, n;
224

    
225
    max_packet_size = (s->max_payload_size / sample_size) * sample_size;
226
    /* not needed, but who nows */
227
    if ((size % sample_size) != 0)
228
        av_abort();
229
    n = 0;
230
    while (size > 0) {
231
        s->buf_ptr = s->buf;
232
        len = FFMIN(max_packet_size, size);
233

    
234
        /* copy data */
235
        memcpy(s->buf_ptr, buf1, len);
236
        s->buf_ptr += len;
237
        buf1 += len;
238
        size -= len;
239
        s->timestamp = s->cur_timestamp + n / sample_size;
240
        ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
241
        n += (s->buf_ptr - s->buf);
242
    }
243
}
244

    
245
static void rtp_send_mpegaudio(AVFormatContext *s1,
246
                               const uint8_t *buf1, int size)
247
{
248
    RTPMuxContext *s = s1->priv_data;
249
    int len, count, max_packet_size;
250

    
251
    max_packet_size = s->max_payload_size;
252

    
253
    /* test if we must flush because not enough space */
254
    len = (s->buf_ptr - s->buf);
255
    if ((len + size) > max_packet_size) {
256
        if (len > 4) {
257
            ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
258
            s->buf_ptr = s->buf + 4;
259
        }
260
    }
261
    if (s->buf_ptr == s->buf + 4) {
262
        s->timestamp = s->cur_timestamp;
263
    }
264

    
265
    /* add the packet */
266
    if (size > max_packet_size) {
267
        /* big packet: fragment */
268
        count = 0;
269
        while (size > 0) {
270
            len = max_packet_size - 4;
271
            if (len > size)
272
                len = size;
273
            /* build fragmented packet */
274
            s->buf[0] = 0;
275
            s->buf[1] = 0;
276
            s->buf[2] = count >> 8;
277
            s->buf[3] = count;
278
            memcpy(s->buf + 4, buf1, len);
279
            ff_rtp_send_data(s1, s->buf, len + 4, 0);
280
            size -= len;
281
            buf1 += len;
282
            count += len;
283
        }
284
    } else {
285
        if (s->buf_ptr == s->buf + 4) {
286
            /* no fragmentation possible */
287
            s->buf[0] = 0;
288
            s->buf[1] = 0;
289
            s->buf[2] = 0;
290
            s->buf[3] = 0;
291
        }
292
        memcpy(s->buf_ptr, buf1, size);
293
        s->buf_ptr += size;
294
    }
295
}
296

    
297
static void rtp_send_raw(AVFormatContext *s1,
298
                         const uint8_t *buf1, int size)
299
{
300
    RTPMuxContext *s = s1->priv_data;
301
    int len, max_packet_size;
302

    
303
    max_packet_size = s->max_payload_size;
304

    
305
    while (size > 0) {
306
        len = max_packet_size;
307
        if (len > size)
308
            len = size;
309

    
310
        s->timestamp = s->cur_timestamp;
311
        ff_rtp_send_data(s1, buf1, len, (len == size));
312

    
313
        buf1 += len;
314
        size -= len;
315
    }
316
}
317

    
318
/* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
319
static void rtp_send_mpegts_raw(AVFormatContext *s1,
320
                                const uint8_t *buf1, int size)
321
{
322
    RTPMuxContext *s = s1->priv_data;
323
    int len, out_len;
324

    
325
    while (size >= TS_PACKET_SIZE) {
326
        len = s->max_payload_size - (s->buf_ptr - s->buf);
327
        if (len > size)
328
            len = size;
329
        memcpy(s->buf_ptr, buf1, len);
330
        buf1 += len;
331
        size -= len;
332
        s->buf_ptr += len;
333

    
334
        out_len = s->buf_ptr - s->buf;
335
        if (out_len >= s->max_payload_size) {
336
            ff_rtp_send_data(s1, s->buf, out_len, 0);
337
            s->buf_ptr = s->buf;
338
        }
339
    }
340
}
341

    
342
static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
343
{
344
    RTPMuxContext *s = s1->priv_data;
345
    AVStream *st = s1->streams[0];
346
    int rtcp_bytes;
347
    int size= pkt->size;
348

    
349
    dprintf(s1, "%d: write len=%d\n", pkt->stream_index, size);
350

    
351
    rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
352
        RTCP_TX_RATIO_DEN;
353
    if (s->first_packet || ((rtcp_bytes >= RTCP_SR_SIZE) &&
354
                           (ff_ntp_time() - s->last_rtcp_ntp_time > 5000000))) {
355
        rtcp_send_sr(s1, ff_ntp_time());
356
        s->last_octet_count = s->octet_count;
357
        s->first_packet = 0;
358
    }
359
    s->cur_timestamp = s->base_timestamp + pkt->pts;
360

    
361
    switch(st->codec->codec_id) {
362
    case CODEC_ID_PCM_MULAW:
363
    case CODEC_ID_PCM_ALAW:
364
    case CODEC_ID_PCM_U8:
365
    case CODEC_ID_PCM_S8:
366
        rtp_send_samples(s1, pkt->data, size, 1 * st->codec->channels);
367
        break;
368
    case CODEC_ID_PCM_U16BE:
369
    case CODEC_ID_PCM_U16LE:
370
    case CODEC_ID_PCM_S16BE:
371
    case CODEC_ID_PCM_S16LE:
372
        rtp_send_samples(s1, pkt->data, size, 2 * st->codec->channels);
373
        break;
374
    case CODEC_ID_MP2:
375
    case CODEC_ID_MP3:
376
        rtp_send_mpegaudio(s1, pkt->data, size);
377
        break;
378
    case CODEC_ID_MPEG1VIDEO:
379
    case CODEC_ID_MPEG2VIDEO:
380
        ff_rtp_send_mpegvideo(s1, pkt->data, size);
381
        break;
382
    case CODEC_ID_AAC:
383
        ff_rtp_send_aac(s1, pkt->data, size);
384
        break;
385
    case CODEC_ID_AMR_NB:
386
    case CODEC_ID_AMR_WB:
387
        ff_rtp_send_amr(s1, pkt->data, size);
388
        break;
389
    case CODEC_ID_MPEG2TS:
390
        rtp_send_mpegts_raw(s1, pkt->data, size);
391
        break;
392
    case CODEC_ID_H264:
393
        ff_rtp_send_h264(s1, pkt->data, size);
394
        break;
395
    case CODEC_ID_H263:
396
    case CODEC_ID_H263P:
397
        ff_rtp_send_h263(s1, pkt->data, size);
398
        break;
399
    default:
400
        /* better than nothing : send the codec raw data */
401
        rtp_send_raw(s1, pkt->data, size);
402
        break;
403
    }
404
    return 0;
405
}
406

    
407
static int rtp_write_trailer(AVFormatContext *s1)
408
{
409
    RTPMuxContext *s = s1->priv_data;
410

    
411
    av_freep(&s->buf);
412

    
413
    return 0;
414
}
415

    
416
AVOutputFormat rtp_muxer = {
417
    "rtp",
418
    NULL_IF_CONFIG_SMALL("RTP output format"),
419
    NULL,
420
    NULL,
421
    sizeof(RTPMuxContext),
422
    CODEC_ID_PCM_MULAW,
423
    CODEC_ID_NONE,
424
    rtp_write_header,
425
    rtp_write_packet,
426
    rtp_write_trailer,
427
};