00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avformat.h"
00023 #include "mpegts.h"
00024 #include "internal.h"
00025 #include "libavutil/random_seed.h"
00026 #include "libavutil/opt.h"
00027
00028 #include "rtpenc.h"
00029
00030
00031
00032 static const AVOption options[] = {
00033 FF_RTP_FLAG_OPTS(RTPMuxContext, flags),
00034 { NULL },
00035 };
00036
00037 static const AVClass rtp_muxer_class = {
00038 .class_name = "RTP muxer",
00039 .item_name = av_default_item_name,
00040 .option = options,
00041 .version = LIBAVUTIL_VERSION_INT,
00042 };
00043
00044 #define RTCP_SR_SIZE 28
00045
00046 static int is_supported(enum CodecID id)
00047 {
00048 switch(id) {
00049 case CODEC_ID_H263:
00050 case CODEC_ID_H263P:
00051 case CODEC_ID_H264:
00052 case CODEC_ID_MPEG1VIDEO:
00053 case CODEC_ID_MPEG2VIDEO:
00054 case CODEC_ID_MPEG4:
00055 case CODEC_ID_AAC:
00056 case CODEC_ID_MP2:
00057 case CODEC_ID_MP3:
00058 case CODEC_ID_PCM_ALAW:
00059 case CODEC_ID_PCM_MULAW:
00060 case CODEC_ID_PCM_S8:
00061 case CODEC_ID_PCM_S16BE:
00062 case CODEC_ID_PCM_S16LE:
00063 case CODEC_ID_PCM_U16BE:
00064 case CODEC_ID_PCM_U16LE:
00065 case CODEC_ID_PCM_U8:
00066 case CODEC_ID_MPEG2TS:
00067 case CODEC_ID_AMR_NB:
00068 case CODEC_ID_AMR_WB:
00069 case CODEC_ID_VORBIS:
00070 case CODEC_ID_THEORA:
00071 case CODEC_ID_VP8:
00072 case CODEC_ID_ADPCM_G722:
00073 return 1;
00074 default:
00075 return 0;
00076 }
00077 }
00078
00079 static int rtp_write_header(AVFormatContext *s1)
00080 {
00081 RTPMuxContext *s = s1->priv_data;
00082 int max_packet_size, n;
00083 AVStream *st;
00084
00085 if (s1->nb_streams != 1)
00086 return -1;
00087 st = s1->streams[0];
00088 if (!is_supported(st->codec->codec_id)) {
00089 av_log(s1, AV_LOG_ERROR, "Unsupported codec %x\n", st->codec->codec_id);
00090
00091 return -1;
00092 }
00093
00094 s->payload_type = ff_rtp_get_payload_type(st->codec);
00095 if (s->payload_type < 0)
00096 s->payload_type = RTP_PT_PRIVATE + (st->codec->codec_type == AVMEDIA_TYPE_AUDIO);
00097
00098 s->base_timestamp = av_get_random_seed();
00099 s->timestamp = s->base_timestamp;
00100 s->cur_timestamp = 0;
00101 s->ssrc = av_get_random_seed();
00102 s->first_packet = 1;
00103 s->first_rtcp_ntp_time = ff_ntp_time();
00104 if (s1->start_time_realtime)
00105
00106 s->first_rtcp_ntp_time = (s1->start_time_realtime / 1000) * 1000 +
00107 NTP_OFFSET_US;
00108
00109 max_packet_size = s1->pb->max_packet_size;
00110 if (max_packet_size <= 12)
00111 return AVERROR(EIO);
00112 s->buf = av_malloc(max_packet_size);
00113 if (s->buf == NULL) {
00114 return AVERROR(ENOMEM);
00115 }
00116 s->max_payload_size = max_packet_size - 12;
00117
00118 s->max_frames_per_packet = 0;
00119 if (s1->max_delay) {
00120 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00121 if (st->codec->frame_size == 0) {
00122 av_log(s1, AV_LOG_ERROR, "Cannot respect max delay: frame size = 0\n");
00123 } else {
00124 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);
00125 }
00126 }
00127 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00128
00129 s->max_frames_per_packet = av_rescale_q(s1->max_delay, (AVRational){1, 1000000}, st->codec->time_base);
00130 }
00131 }
00132
00133 av_set_pts_info(st, 32, 1, 90000);
00134 switch(st->codec->codec_id) {
00135 case CODEC_ID_MP2:
00136 case CODEC_ID_MP3:
00137 s->buf_ptr = s->buf + 4;
00138 break;
00139 case CODEC_ID_MPEG1VIDEO:
00140 case CODEC_ID_MPEG2VIDEO:
00141 break;
00142 case CODEC_ID_MPEG2TS:
00143 n = s->max_payload_size / TS_PACKET_SIZE;
00144 if (n < 1)
00145 n = 1;
00146 s->max_payload_size = n * TS_PACKET_SIZE;
00147 s->buf_ptr = s->buf;
00148 break;
00149 case CODEC_ID_H264:
00150
00151 if (st->codec->extradata_size > 4 && st->codec->extradata[0] == 1) {
00152 s->nal_length_size = (st->codec->extradata[4] & 0x03) + 1;
00153 }
00154 break;
00155 case CODEC_ID_VORBIS:
00156 case CODEC_ID_THEORA:
00157 if (!s->max_frames_per_packet) s->max_frames_per_packet = 15;
00158 s->max_frames_per_packet = av_clip(s->max_frames_per_packet, 1, 15);
00159 s->max_payload_size -= 6;
00160 s->num_frames = 0;
00161 goto defaultcase;
00162 case CODEC_ID_VP8:
00163 av_log(s1, AV_LOG_ERROR, "RTP VP8 payload implementation is "
00164 "incompatible with the latest spec drafts.\n");
00165 break;
00166 case CODEC_ID_ADPCM_G722:
00167
00168
00169 av_set_pts_info(st, 32, 1, 8000);
00170 break;
00171 case CODEC_ID_AMR_NB:
00172 case CODEC_ID_AMR_WB:
00173 if (!s->max_frames_per_packet)
00174 s->max_frames_per_packet = 12;
00175 if (st->codec->codec_id == CODEC_ID_AMR_NB)
00176 n = 31;
00177 else
00178 n = 61;
00179
00180 if (1 + s->max_frames_per_packet + n > s->max_payload_size) {
00181 av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for AMR\n");
00182 return -1;
00183 }
00184 if (st->codec->channels != 1) {
00185 av_log(s1, AV_LOG_ERROR, "Only mono is supported\n");
00186 return -1;
00187 }
00188 case CODEC_ID_AAC:
00189 s->num_frames = 0;
00190 default:
00191 defaultcase:
00192 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00193 av_set_pts_info(st, 32, 1, st->codec->sample_rate);
00194 }
00195 s->buf_ptr = s->buf;
00196 break;
00197 }
00198
00199 return 0;
00200 }
00201
00202
00203 static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time)
00204 {
00205 RTPMuxContext *s = s1->priv_data;
00206 uint32_t rtp_ts;
00207
00208 av_dlog(s1, "RTCP: %02x %"PRIx64" %x\n", s->payload_type, ntp_time, s->timestamp);
00209
00210 s->last_rtcp_ntp_time = ntp_time;
00211 rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, (AVRational){1, 1000000},
00212 s1->streams[0]->time_base) + s->base_timestamp;
00213 avio_w8(s1->pb, (RTP_VERSION << 6));
00214 avio_w8(s1->pb, RTCP_SR);
00215 avio_wb16(s1->pb, 6);
00216 avio_wb32(s1->pb, s->ssrc);
00217 avio_wb32(s1->pb, ntp_time / 1000000);
00218 avio_wb32(s1->pb, ((ntp_time % 1000000) << 32) / 1000000);
00219 avio_wb32(s1->pb, rtp_ts);
00220 avio_wb32(s1->pb, s->packet_count);
00221 avio_wb32(s1->pb, s->octet_count);
00222 avio_flush(s1->pb);
00223 }
00224
00225
00226
00227 void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
00228 {
00229 RTPMuxContext *s = s1->priv_data;
00230
00231 av_dlog(s1, "rtp_send_data size=%d\n", len);
00232
00233
00234 avio_w8(s1->pb, (RTP_VERSION << 6));
00235 avio_w8(s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
00236 avio_wb16(s1->pb, s->seq);
00237 avio_wb32(s1->pb, s->timestamp);
00238 avio_wb32(s1->pb, s->ssrc);
00239
00240 avio_write(s1->pb, buf1, len);
00241 avio_flush(s1->pb);
00242
00243 s->seq++;
00244 s->octet_count += len;
00245 s->packet_count++;
00246 }
00247
00248
00249
00250 static void rtp_send_samples(AVFormatContext *s1,
00251 const uint8_t *buf1, int size, int sample_size)
00252 {
00253 RTPMuxContext *s = s1->priv_data;
00254 int len, max_packet_size, n;
00255
00256 max_packet_size = (s->max_payload_size / sample_size) * sample_size;
00257
00258 if ((size % sample_size) != 0)
00259 av_abort();
00260 n = 0;
00261 while (size > 0) {
00262 s->buf_ptr = s->buf;
00263 len = FFMIN(max_packet_size, size);
00264
00265
00266 memcpy(s->buf_ptr, buf1, len);
00267 s->buf_ptr += len;
00268 buf1 += len;
00269 size -= len;
00270 s->timestamp = s->cur_timestamp + n / sample_size;
00271 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
00272 n += (s->buf_ptr - s->buf);
00273 }
00274 }
00275
00276 static void rtp_send_mpegaudio(AVFormatContext *s1,
00277 const uint8_t *buf1, int size)
00278 {
00279 RTPMuxContext *s = s1->priv_data;
00280 int len, count, max_packet_size;
00281
00282 max_packet_size = s->max_payload_size;
00283
00284
00285 len = (s->buf_ptr - s->buf);
00286 if ((len + size) > max_packet_size) {
00287 if (len > 4) {
00288 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
00289 s->buf_ptr = s->buf + 4;
00290 }
00291 }
00292 if (s->buf_ptr == s->buf + 4) {
00293 s->timestamp = s->cur_timestamp;
00294 }
00295
00296
00297 if (size > max_packet_size) {
00298
00299 count = 0;
00300 while (size > 0) {
00301 len = max_packet_size - 4;
00302 if (len > size)
00303 len = size;
00304
00305 s->buf[0] = 0;
00306 s->buf[1] = 0;
00307 s->buf[2] = count >> 8;
00308 s->buf[3] = count;
00309 memcpy(s->buf + 4, buf1, len);
00310 ff_rtp_send_data(s1, s->buf, len + 4, 0);
00311 size -= len;
00312 buf1 += len;
00313 count += len;
00314 }
00315 } else {
00316 if (s->buf_ptr == s->buf + 4) {
00317
00318 s->buf[0] = 0;
00319 s->buf[1] = 0;
00320 s->buf[2] = 0;
00321 s->buf[3] = 0;
00322 }
00323 memcpy(s->buf_ptr, buf1, size);
00324 s->buf_ptr += size;
00325 }
00326 }
00327
00328 static void rtp_send_raw(AVFormatContext *s1,
00329 const uint8_t *buf1, int size)
00330 {
00331 RTPMuxContext *s = s1->priv_data;
00332 int len, max_packet_size;
00333
00334 max_packet_size = s->max_payload_size;
00335
00336 while (size > 0) {
00337 len = max_packet_size;
00338 if (len > size)
00339 len = size;
00340
00341 s->timestamp = s->cur_timestamp;
00342 ff_rtp_send_data(s1, buf1, len, (len == size));
00343
00344 buf1 += len;
00345 size -= len;
00346 }
00347 }
00348
00349
00350 static void rtp_send_mpegts_raw(AVFormatContext *s1,
00351 const uint8_t *buf1, int size)
00352 {
00353 RTPMuxContext *s = s1->priv_data;
00354 int len, out_len;
00355
00356 while (size >= TS_PACKET_SIZE) {
00357 len = s->max_payload_size - (s->buf_ptr - s->buf);
00358 if (len > size)
00359 len = size;
00360 memcpy(s->buf_ptr, buf1, len);
00361 buf1 += len;
00362 size -= len;
00363 s->buf_ptr += len;
00364
00365 out_len = s->buf_ptr - s->buf;
00366 if (out_len >= s->max_payload_size) {
00367 ff_rtp_send_data(s1, s->buf, out_len, 0);
00368 s->buf_ptr = s->buf;
00369 }
00370 }
00371 }
00372
00373 static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
00374 {
00375 RTPMuxContext *s = s1->priv_data;
00376 AVStream *st = s1->streams[0];
00377 int rtcp_bytes;
00378 int size= pkt->size;
00379
00380 av_dlog(s1, "%d: write len=%d\n", pkt->stream_index, size);
00381
00382 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
00383 RTCP_TX_RATIO_DEN;
00384 if (s->first_packet || ((rtcp_bytes >= RTCP_SR_SIZE) &&
00385 (ff_ntp_time() - s->last_rtcp_ntp_time > 5000000))) {
00386 rtcp_send_sr(s1, ff_ntp_time());
00387 s->last_octet_count = s->octet_count;
00388 s->first_packet = 0;
00389 }
00390 s->cur_timestamp = s->base_timestamp + pkt->pts;
00391
00392 switch(st->codec->codec_id) {
00393 case CODEC_ID_PCM_MULAW:
00394 case CODEC_ID_PCM_ALAW:
00395 case CODEC_ID_PCM_U8:
00396 case CODEC_ID_PCM_S8:
00397 rtp_send_samples(s1, pkt->data, size, 1 * st->codec->channels);
00398 break;
00399 case CODEC_ID_PCM_U16BE:
00400 case CODEC_ID_PCM_U16LE:
00401 case CODEC_ID_PCM_S16BE:
00402 case CODEC_ID_PCM_S16LE:
00403 rtp_send_samples(s1, pkt->data, size, 2 * st->codec->channels);
00404 break;
00405 case CODEC_ID_ADPCM_G722:
00406
00407
00408
00409 rtp_send_samples(s1, pkt->data, size, 1 * st->codec->channels);
00410 break;
00411 case CODEC_ID_MP2:
00412 case CODEC_ID_MP3:
00413 rtp_send_mpegaudio(s1, pkt->data, size);
00414 break;
00415 case CODEC_ID_MPEG1VIDEO:
00416 case CODEC_ID_MPEG2VIDEO:
00417 ff_rtp_send_mpegvideo(s1, pkt->data, size);
00418 break;
00419 case CODEC_ID_AAC:
00420 if (s->flags & FF_RTP_FLAG_MP4A_LATM)
00421 ff_rtp_send_latm(s1, pkt->data, size);
00422 else
00423 ff_rtp_send_aac(s1, pkt->data, size);
00424 break;
00425 case CODEC_ID_AMR_NB:
00426 case CODEC_ID_AMR_WB:
00427 ff_rtp_send_amr(s1, pkt->data, size);
00428 break;
00429 case CODEC_ID_MPEG2TS:
00430 rtp_send_mpegts_raw(s1, pkt->data, size);
00431 break;
00432 case CODEC_ID_H264:
00433 ff_rtp_send_h264(s1, pkt->data, size);
00434 break;
00435 case CODEC_ID_H263:
00436 case CODEC_ID_H263P:
00437 ff_rtp_send_h263(s1, pkt->data, size);
00438 break;
00439 case CODEC_ID_VORBIS:
00440 case CODEC_ID_THEORA:
00441 ff_rtp_send_xiph(s1, pkt->data, size);
00442 break;
00443 case CODEC_ID_VP8:
00444 ff_rtp_send_vp8(s1, pkt->data, size);
00445 break;
00446 default:
00447
00448 rtp_send_raw(s1, pkt->data, size);
00449 break;
00450 }
00451 return 0;
00452 }
00453
00454 static int rtp_write_trailer(AVFormatContext *s1)
00455 {
00456 RTPMuxContext *s = s1->priv_data;
00457
00458 av_freep(&s->buf);
00459
00460 return 0;
00461 }
00462
00463 AVOutputFormat ff_rtp_muxer = {
00464 "rtp",
00465 NULL_IF_CONFIG_SMALL("RTP output format"),
00466 NULL,
00467 NULL,
00468 sizeof(RTPMuxContext),
00469 CODEC_ID_PCM_MULAW,
00470 CODEC_ID_NONE,
00471 rtp_write_header,
00472 rtp_write_packet,
00473 rtp_write_trailer,
00474 .priv_class = &rtp_muxer_class,
00475 };