Statistics
| Branch: | Revision:

ffmpeg / libavformat / rtpenc.c @ cd7bfe40

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 "libavcodec/get_bits.h"
23
#include "avformat.h"
24
#include "mpegts.h"
25

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

    
29
#include "rtpenc.h"
30

    
31
//#define DEBUG
32

    
33
#define RTCP_SR_SIZE 28
34
#define NTP_OFFSET 2208988800ULL
35
#define NTP_OFFSET_US (NTP_OFFSET * 1000000ULL)
36

    
37
static uint64_t ntp_time(void)
38
{
39
  return (av_gettime() / 1000) * 1000 + NTP_OFFSET_US;
40
}
41

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

    
71
static int rtp_write_header(AVFormatContext *s1)
72
{
73
    RTPMuxContext *s = s1->priv_data;
74
    int max_packet_size, n;
75
    AVStream *st;
76

    
77
    if (s1->nb_streams != 1)
78
        return -1;
79
    st = s1->streams[0];
80
    if (!is_supported(st->codec->codec_id)) {
81
        av_log(s1, AV_LOG_ERROR, "Unsupported codec %x\n", st->codec->codec_id);
82

    
83
        return -1;
84
    }
85

    
86
    s->payload_type = ff_rtp_get_payload_type(st->codec);
87
    if (s->payload_type < 0)
88
        s->payload_type = RTP_PT_PRIVATE + (st->codec->codec_type == CODEC_TYPE_AUDIO);
89

    
90
// following 2 FIXMEs could be set based on the current time, there is normally no info leak, as RTP will likely be transmitted immediately
91
    s->base_timestamp = 0; /* FIXME: was random(), what should this be? */
92
    s->timestamp = s->base_timestamp;
93
    s->cur_timestamp = 0;
94
    s->ssrc = 0; /* FIXME: was random(), what should this be? */
95
    s->first_packet = 1;
96
    s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
97

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

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

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

    
165
    return 0;
166
}
167

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

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

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

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

    
198
    dprintf(s1, "rtp_send_data size=%d\n", len);
199

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

    
207
    put_buffer(s1->pb, buf1, len);
208
    put_flush_packet(s1->pb);
209

    
210
    s->seq++;
211
    s->octet_count += len;
212
    s->packet_count++;
213
}
214

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

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

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

    
243
/* NOTE: we suppose that exactly one frame is given as argument here */
244
/* XXX: test it */
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
    uint8_t *buf1= pkt->data;
349

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

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

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

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

    
412
    av_freep(&s->buf);
413

    
414
    return 0;
415
}
416

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