Statistics
| Branch: | Revision:

ffmpeg / libavformat / rtpenc.c @ 594a9aeb

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

    
26
#include <unistd.h>
27

    
28
#include "rtpenc.h"
29

    
30
//#define DEBUG
31

    
32
#define RTCP_SR_SIZE 28
33

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

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

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

    
75
        return -1;
76
    }
77

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

    
82
// following 2 FIXMEs could be set based on the current time, there is normally no info leak, as RTP will likely be transmitted immediately
83
    s->base_timestamp = 0; /* FIXME: was random(), what should this be? */
84
    s->timestamp = s->base_timestamp;
85
    s->cur_timestamp = 0;
86
    s->ssrc = 0; /* FIXME: was random(), what should this be? */
87
    s->first_packet = 1;
88
    s->first_rtcp_ntp_time = ff_ntp_time();
89

    
90
    max_packet_size = url_fget_max_packet_size(s1->pb);
91
    if (max_packet_size <= 12)
92
        return AVERROR(EIO);
93
    s->buf = av_malloc(max_packet_size);
94
    if (s->buf == NULL) {
95
        return AVERROR(ENOMEM);
96
    }
97
    s->max_payload_size = max_packet_size - 12;
98

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

    
114
    av_set_pts_info(st, 32, 1, 90000);
115
    switch(st->codec->codec_id) {
116
    case CODEC_ID_MP2:
117
    case CODEC_ID_MP3:
118
        s->buf_ptr = s->buf + 4;
119
        break;
120
    case CODEC_ID_MPEG1VIDEO:
121
    case CODEC_ID_MPEG2VIDEO:
122
        break;
123
    case CODEC_ID_MPEG2TS:
124
        n = s->max_payload_size / TS_PACKET_SIZE;
125
        if (n < 1)
126
            n = 1;
127
        s->max_payload_size = n * TS_PACKET_SIZE;
128
        s->buf_ptr = s->buf;
129
        break;
130
    case CODEC_ID_AMR_NB:
131
    case CODEC_ID_AMR_WB:
132
        if (!s->max_frames_per_packet)
133
            s->max_frames_per_packet = 12;
134
        if (st->codec->codec_id == CODEC_ID_AMR_NB)
135
            n = 31;
136
        else
137
            n = 61;
138
        /* max_header_toc_size + the largest AMR payload must fit */
139
        if (1 + s->max_frames_per_packet + n > s->max_payload_size) {
140
            av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for AMR\n");
141
            return -1;
142
        }
143
        if (st->codec->channels != 1) {
144
            av_log(s1, AV_LOG_ERROR, "Only mono is supported\n");
145
            return -1;
146
        }
147
    case CODEC_ID_AAC:
148
        s->num_frames = 0;
149
    default:
150
        if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
151
            av_set_pts_info(st, 32, 1, st->codec->sample_rate);
152
        }
153
        s->buf_ptr = s->buf;
154
        break;
155
    }
156

    
157
    return 0;
158
}
159

    
160
/* send an rtcp sender report packet */
161
static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time)
162
{
163
    RTPMuxContext *s = s1->priv_data;
164
    uint32_t rtp_ts;
165

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

    
168
    s->last_rtcp_ntp_time = ntp_time;
169
    rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, (AVRational){1, 1000000},
170
                          s1->streams[0]->time_base) + s->base_timestamp;
171
    put_byte(s1->pb, (RTP_VERSION << 6));
172
    put_byte(s1->pb, 200);
173
    put_be16(s1->pb, 6); /* length in words - 1 */
174
    put_be32(s1->pb, s->ssrc);
175
    put_be32(s1->pb, ntp_time / 1000000);
176
    put_be32(s1->pb, ((ntp_time % 1000000) << 32) / 1000000);
177
    put_be32(s1->pb, rtp_ts);
178
    put_be32(s1->pb, s->packet_count);
179
    put_be32(s1->pb, s->octet_count);
180
    put_flush_packet(s1->pb);
181
}
182

    
183
/* send an rtp packet. sequence number is incremented, but the caller
184
   must update the timestamp itself */
185
void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
186
{
187
    RTPMuxContext *s = s1->priv_data;
188

    
189
    dprintf(s1, "rtp_send_data size=%d\n", len);
190

    
191
    /* build the RTP header */
192
    put_byte(s1->pb, (RTP_VERSION << 6));
193
    put_byte(s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
194
    put_be16(s1->pb, s->seq);
195
    put_be32(s1->pb, s->timestamp);
196
    put_be32(s1->pb, s->ssrc);
197

    
198
    put_buffer(s1->pb, buf1, len);
199
    put_flush_packet(s1->pb);
200

    
201
    s->seq++;
202
    s->octet_count += len;
203
    s->packet_count++;
204
}
205

    
206
/* send an integer number of samples and compute time stamp and fill
207
   the rtp send buffer before sending. */
208
static void rtp_send_samples(AVFormatContext *s1,
209
                             const uint8_t *buf1, int size, int sample_size)
210
{
211
    RTPMuxContext *s = s1->priv_data;
212
    int len, max_packet_size, n;
213

    
214
    max_packet_size = (s->max_payload_size / sample_size) * sample_size;
215
    /* not needed, but who nows */
216
    if ((size % sample_size) != 0)
217
        av_abort();
218
    n = 0;
219
    while (size > 0) {
220
        s->buf_ptr = s->buf;
221
        len = FFMIN(max_packet_size, size);
222

    
223
        /* copy data */
224
        memcpy(s->buf_ptr, buf1, len);
225
        s->buf_ptr += len;
226
        buf1 += len;
227
        size -= len;
228
        s->timestamp = s->cur_timestamp + n / sample_size;
229
        ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
230
        n += (s->buf_ptr - s->buf);
231
    }
232
}
233

    
234
static void rtp_send_mpegaudio(AVFormatContext *s1,
235
                               const uint8_t *buf1, int size)
236
{
237
    RTPMuxContext *s = s1->priv_data;
238
    int len, count, max_packet_size;
239

    
240
    max_packet_size = s->max_payload_size;
241

    
242
    /* test if we must flush because not enough space */
243
    len = (s->buf_ptr - s->buf);
244
    if ((len + size) > max_packet_size) {
245
        if (len > 4) {
246
            ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
247
            s->buf_ptr = s->buf + 4;
248
        }
249
    }
250
    if (s->buf_ptr == s->buf + 4) {
251
        s->timestamp = s->cur_timestamp;
252
    }
253

    
254
    /* add the packet */
255
    if (size > max_packet_size) {
256
        /* big packet: fragment */
257
        count = 0;
258
        while (size > 0) {
259
            len = max_packet_size - 4;
260
            if (len > size)
261
                len = size;
262
            /* build fragmented packet */
263
            s->buf[0] = 0;
264
            s->buf[1] = 0;
265
            s->buf[2] = count >> 8;
266
            s->buf[3] = count;
267
            memcpy(s->buf + 4, buf1, len);
268
            ff_rtp_send_data(s1, s->buf, len + 4, 0);
269
            size -= len;
270
            buf1 += len;
271
            count += len;
272
        }
273
    } else {
274
        if (s->buf_ptr == s->buf + 4) {
275
            /* no fragmentation possible */
276
            s->buf[0] = 0;
277
            s->buf[1] = 0;
278
            s->buf[2] = 0;
279
            s->buf[3] = 0;
280
        }
281
        memcpy(s->buf_ptr, buf1, size);
282
        s->buf_ptr += size;
283
    }
284
}
285

    
286
static void rtp_send_raw(AVFormatContext *s1,
287
                         const uint8_t *buf1, int size)
288
{
289
    RTPMuxContext *s = s1->priv_data;
290
    int len, max_packet_size;
291

    
292
    max_packet_size = s->max_payload_size;
293

    
294
    while (size > 0) {
295
        len = max_packet_size;
296
        if (len > size)
297
            len = size;
298

    
299
        s->timestamp = s->cur_timestamp;
300
        ff_rtp_send_data(s1, buf1, len, (len == size));
301

    
302
        buf1 += len;
303
        size -= len;
304
    }
305
}
306

    
307
/* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
308
static void rtp_send_mpegts_raw(AVFormatContext *s1,
309
                                const uint8_t *buf1, int size)
310
{
311
    RTPMuxContext *s = s1->priv_data;
312
    int len, out_len;
313

    
314
    while (size >= TS_PACKET_SIZE) {
315
        len = s->max_payload_size - (s->buf_ptr - s->buf);
316
        if (len > size)
317
            len = size;
318
        memcpy(s->buf_ptr, buf1, len);
319
        buf1 += len;
320
        size -= len;
321
        s->buf_ptr += len;
322

    
323
        out_len = s->buf_ptr - s->buf;
324
        if (out_len >= s->max_payload_size) {
325
            ff_rtp_send_data(s1, s->buf, out_len, 0);
326
            s->buf_ptr = s->buf;
327
        }
328
    }
329
}
330

    
331
static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
332
{
333
    RTPMuxContext *s = s1->priv_data;
334
    AVStream *st = s1->streams[0];
335
    int rtcp_bytes;
336
    int size= pkt->size;
337

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

    
340
    rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
341
        RTCP_TX_RATIO_DEN;
342
    if (s->first_packet || ((rtcp_bytes >= RTCP_SR_SIZE) &&
343
                           (ff_ntp_time() - s->last_rtcp_ntp_time > 5000000))) {
344
        rtcp_send_sr(s1, ff_ntp_time());
345
        s->last_octet_count = s->octet_count;
346
        s->first_packet = 0;
347
    }
348
    s->cur_timestamp = s->base_timestamp + pkt->pts;
349

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

    
396
static int rtp_write_trailer(AVFormatContext *s1)
397
{
398
    RTPMuxContext *s = s1->priv_data;
399

    
400
    av_freep(&s->buf);
401

    
402
    return 0;
403
}
404

    
405
AVOutputFormat rtp_muxer = {
406
    "rtp",
407
    NULL_IF_CONFIG_SMALL("RTP output format"),
408
    NULL,
409
    NULL,
410
    sizeof(RTPMuxContext),
411
    CODEC_ID_PCM_MULAW,
412
    CODEC_ID_NONE,
413
    rtp_write_header,
414
    rtp_write_packet,
415
    rtp_write_trailer,
416
};