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/mathematics.h"
00026 #include "libavutil/random_seed.h"
00027 #include "libavutil/opt.h"
00028
00029 #include "rtpenc.h"
00030
00031
00032
00033 static const AVOption options[] = {
00034 FF_RTP_FLAG_OPTS(RTPMuxContext, flags),
00035 { "payload_type", "Specify RTP payload type", offsetof(RTPMuxContext, payload_type), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, 127, AV_OPT_FLAG_ENCODING_PARAM },
00036 { "ssrc", "Stream identifier", offsetof(RTPMuxContext, ssrc), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
00037 { NULL },
00038 };
00039
00040 static const AVClass rtp_muxer_class = {
00041 .class_name = "RTP muxer",
00042 .item_name = av_default_item_name,
00043 .option = options,
00044 .version = LIBAVUTIL_VERSION_INT,
00045 };
00046
00047 #define RTCP_SR_SIZE 28
00048
00049 static int is_supported(enum AVCodecID id)
00050 {
00051 switch(id) {
00052 case AV_CODEC_ID_H263:
00053 case AV_CODEC_ID_H263P:
00054 case AV_CODEC_ID_H264:
00055 case AV_CODEC_ID_MPEG1VIDEO:
00056 case AV_CODEC_ID_MPEG2VIDEO:
00057 case AV_CODEC_ID_MPEG4:
00058 case AV_CODEC_ID_AAC:
00059 case AV_CODEC_ID_MP2:
00060 case AV_CODEC_ID_MP3:
00061 case AV_CODEC_ID_PCM_ALAW:
00062 case AV_CODEC_ID_PCM_MULAW:
00063 case AV_CODEC_ID_PCM_S8:
00064 case AV_CODEC_ID_PCM_S16BE:
00065 case AV_CODEC_ID_PCM_S16LE:
00066 case AV_CODEC_ID_PCM_U16BE:
00067 case AV_CODEC_ID_PCM_U16LE:
00068 case AV_CODEC_ID_PCM_U8:
00069 case AV_CODEC_ID_MPEG2TS:
00070 case AV_CODEC_ID_AMR_NB:
00071 case AV_CODEC_ID_AMR_WB:
00072 case AV_CODEC_ID_VORBIS:
00073 case AV_CODEC_ID_THEORA:
00074 case AV_CODEC_ID_VP8:
00075 case AV_CODEC_ID_ADPCM_G722:
00076 case AV_CODEC_ID_ADPCM_G726:
00077 case AV_CODEC_ID_ILBC:
00078 case AV_CODEC_ID_MJPEG:
00079 case AV_CODEC_ID_SPEEX:
00080 return 1;
00081 default:
00082 return 0;
00083 }
00084 }
00085
00086 static int rtp_write_header(AVFormatContext *s1)
00087 {
00088 RTPMuxContext *s = s1->priv_data;
00089 int n;
00090 AVStream *st;
00091
00092 if (s1->nb_streams != 1) {
00093 av_log(s1, AV_LOG_ERROR, "Only one stream supported in the RTP muxer\n");
00094 return AVERROR(EINVAL);
00095 }
00096 st = s1->streams[0];
00097 if (!is_supported(st->codec->codec_id)) {
00098 av_log(s1, AV_LOG_ERROR, "Unsupported codec %s\n", avcodec_get_name(st->codec->codec_id));
00099
00100 return -1;
00101 }
00102
00103 if (s->payload_type < 0)
00104 s->payload_type = ff_rtp_get_payload_type(s1, st->codec);
00105 s->base_timestamp = av_get_random_seed();
00106 s->timestamp = s->base_timestamp;
00107 s->cur_timestamp = 0;
00108 if (!s->ssrc)
00109 s->ssrc = av_get_random_seed();
00110 s->first_packet = 1;
00111 s->first_rtcp_ntp_time = ff_ntp_time();
00112 if (s1->start_time_realtime)
00113
00114 s->first_rtcp_ntp_time = (s1->start_time_realtime / 1000) * 1000 +
00115 NTP_OFFSET_US;
00116
00117 if (s1->packet_size) {
00118 if (s1->pb->max_packet_size)
00119 s1->packet_size = FFMIN(s1->packet_size,
00120 s1->pb->max_packet_size);
00121 } else
00122 s1->packet_size = s1->pb->max_packet_size;
00123 if (s1->packet_size <= 12) {
00124 av_log(s1, AV_LOG_ERROR, "Max packet size %d too low\n", s1->packet_size);
00125 return AVERROR(EIO);
00126 }
00127 s->buf = av_malloc(s1->packet_size);
00128 if (s->buf == NULL) {
00129 return AVERROR(ENOMEM);
00130 }
00131 s->max_payload_size = s1->packet_size - 12;
00132
00133 s->max_frames_per_packet = 0;
00134 if (s1->max_delay > 0) {
00135 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00136 int frame_size = av_get_audio_frame_duration(st->codec, 0);
00137 if (!frame_size)
00138 frame_size = st->codec->frame_size;
00139 if (frame_size == 0) {
00140 av_log(s1, AV_LOG_ERROR, "Cannot respect max delay: frame size = 0\n");
00141 } else {
00142 s->max_frames_per_packet =
00143 av_rescale_q_rnd(s1->max_delay,
00144 AV_TIME_BASE_Q,
00145 (AVRational){ frame_size, st->codec->sample_rate },
00146 AV_ROUND_DOWN);
00147 }
00148 }
00149 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00150
00151 s->max_frames_per_packet = av_rescale_q(s1->max_delay, (AVRational){1, 1000000}, st->codec->time_base);
00152 }
00153 }
00154
00155 avpriv_set_pts_info(st, 32, 1, 90000);
00156 switch(st->codec->codec_id) {
00157 case AV_CODEC_ID_MP2:
00158 case AV_CODEC_ID_MP3:
00159 s->buf_ptr = s->buf + 4;
00160 break;
00161 case AV_CODEC_ID_MPEG1VIDEO:
00162 case AV_CODEC_ID_MPEG2VIDEO:
00163 break;
00164 case AV_CODEC_ID_MPEG2TS:
00165 n = s->max_payload_size / TS_PACKET_SIZE;
00166 if (n < 1)
00167 n = 1;
00168 s->max_payload_size = n * TS_PACKET_SIZE;
00169 s->buf_ptr = s->buf;
00170 break;
00171 case AV_CODEC_ID_H264:
00172
00173 if (st->codec->extradata_size > 4 && st->codec->extradata[0] == 1) {
00174 s->nal_length_size = (st->codec->extradata[4] & 0x03) + 1;
00175 }
00176 break;
00177 case AV_CODEC_ID_VORBIS:
00178 case AV_CODEC_ID_THEORA:
00179 if (!s->max_frames_per_packet) s->max_frames_per_packet = 15;
00180 s->max_frames_per_packet = av_clip(s->max_frames_per_packet, 1, 15);
00181 s->max_payload_size -= 6;
00182 s->num_frames = 0;
00183 goto defaultcase;
00184 case AV_CODEC_ID_VP8:
00185 av_log(s1, AV_LOG_ERROR, "RTP VP8 payload implementation is "
00186 "incompatible with the latest spec drafts.\n");
00187 break;
00188 case AV_CODEC_ID_ADPCM_G722:
00189
00190
00191 avpriv_set_pts_info(st, 32, 1, 8000);
00192 break;
00193 case AV_CODEC_ID_ILBC:
00194 if (st->codec->block_align != 38 && st->codec->block_align != 50) {
00195 av_log(s1, AV_LOG_ERROR, "Incorrect iLBC block size specified\n");
00196 goto fail;
00197 }
00198 if (!s->max_frames_per_packet)
00199 s->max_frames_per_packet = 1;
00200 s->max_frames_per_packet = FFMIN(s->max_frames_per_packet,
00201 s->max_payload_size / st->codec->block_align);
00202 goto defaultcase;
00203 case AV_CODEC_ID_AMR_NB:
00204 case AV_CODEC_ID_AMR_WB:
00205 if (!s->max_frames_per_packet)
00206 s->max_frames_per_packet = 12;
00207 if (st->codec->codec_id == AV_CODEC_ID_AMR_NB)
00208 n = 31;
00209 else
00210 n = 61;
00211
00212 if (1 + s->max_frames_per_packet + n > s->max_payload_size) {
00213 av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for AMR\n");
00214 goto fail;
00215 }
00216 if (st->codec->channels != 1) {
00217 av_log(s1, AV_LOG_ERROR, "Only mono is supported\n");
00218 goto fail;
00219 }
00220 case AV_CODEC_ID_AAC:
00221 s->num_frames = 0;
00222 default:
00223 defaultcase:
00224 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00225 avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
00226 }
00227 s->buf_ptr = s->buf;
00228 break;
00229 }
00230
00231 return 0;
00232
00233 fail:
00234 av_freep(&s->buf);
00235 return AVERROR(EINVAL);
00236 }
00237
00238
00239 static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time)
00240 {
00241 RTPMuxContext *s = s1->priv_data;
00242 uint32_t rtp_ts;
00243
00244 av_dlog(s1, "RTCP: %02x %"PRIx64" %x\n", s->payload_type, ntp_time, s->timestamp);
00245
00246 s->last_rtcp_ntp_time = ntp_time;
00247 rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, (AVRational){1, 1000000},
00248 s1->streams[0]->time_base) + s->base_timestamp;
00249 avio_w8(s1->pb, (RTP_VERSION << 6));
00250 avio_w8(s1->pb, RTCP_SR);
00251 avio_wb16(s1->pb, 6);
00252 avio_wb32(s1->pb, s->ssrc);
00253 avio_wb32(s1->pb, ntp_time / 1000000);
00254 avio_wb32(s1->pb, ((ntp_time % 1000000) << 32) / 1000000);
00255 avio_wb32(s1->pb, rtp_ts);
00256 avio_wb32(s1->pb, s->packet_count);
00257 avio_wb32(s1->pb, s->octet_count);
00258 avio_flush(s1->pb);
00259 }
00260
00261
00262
00263 void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
00264 {
00265 RTPMuxContext *s = s1->priv_data;
00266
00267 av_dlog(s1, "rtp_send_data size=%d\n", len);
00268
00269
00270 avio_w8(s1->pb, (RTP_VERSION << 6));
00271 avio_w8(s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
00272 avio_wb16(s1->pb, s->seq);
00273 avio_wb32(s1->pb, s->timestamp);
00274 avio_wb32(s1->pb, s->ssrc);
00275
00276 avio_write(s1->pb, buf1, len);
00277 avio_flush(s1->pb);
00278
00279 s->seq++;
00280 s->octet_count += len;
00281 s->packet_count++;
00282 }
00283
00284
00285
00286 static int rtp_send_samples(AVFormatContext *s1,
00287 const uint8_t *buf1, int size, int sample_size_bits)
00288 {
00289 RTPMuxContext *s = s1->priv_data;
00290 int len, max_packet_size, n;
00291
00292 int aligned_samples_size = sample_size_bits/av_gcd(sample_size_bits, 8);
00293
00294 max_packet_size = (s->max_payload_size / aligned_samples_size) * aligned_samples_size;
00295
00296 if ((sample_size_bits % 8) == 0 && ((8 * size) % sample_size_bits) != 0)
00297 return AVERROR(EINVAL);
00298 n = 0;
00299 while (size > 0) {
00300 s->buf_ptr = s->buf;
00301 len = FFMIN(max_packet_size, size);
00302
00303
00304 memcpy(s->buf_ptr, buf1, len);
00305 s->buf_ptr += len;
00306 buf1 += len;
00307 size -= len;
00308 s->timestamp = s->cur_timestamp + n * 8 / sample_size_bits;
00309 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
00310 n += (s->buf_ptr - s->buf);
00311 }
00312 return 0;
00313 }
00314
00315 static void rtp_send_mpegaudio(AVFormatContext *s1,
00316 const uint8_t *buf1, int size)
00317 {
00318 RTPMuxContext *s = s1->priv_data;
00319 int len, count, max_packet_size;
00320
00321 max_packet_size = s->max_payload_size;
00322
00323
00324 len = (s->buf_ptr - s->buf);
00325 if ((len + size) > max_packet_size) {
00326 if (len > 4) {
00327 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
00328 s->buf_ptr = s->buf + 4;
00329 }
00330 }
00331 if (s->buf_ptr == s->buf + 4) {
00332 s->timestamp = s->cur_timestamp;
00333 }
00334
00335
00336 if (size > max_packet_size) {
00337
00338 count = 0;
00339 while (size > 0) {
00340 len = max_packet_size - 4;
00341 if (len > size)
00342 len = size;
00343
00344 s->buf[0] = 0;
00345 s->buf[1] = 0;
00346 s->buf[2] = count >> 8;
00347 s->buf[3] = count;
00348 memcpy(s->buf + 4, buf1, len);
00349 ff_rtp_send_data(s1, s->buf, len + 4, 0);
00350 size -= len;
00351 buf1 += len;
00352 count += len;
00353 }
00354 } else {
00355 if (s->buf_ptr == s->buf + 4) {
00356
00357 s->buf[0] = 0;
00358 s->buf[1] = 0;
00359 s->buf[2] = 0;
00360 s->buf[3] = 0;
00361 }
00362 memcpy(s->buf_ptr, buf1, size);
00363 s->buf_ptr += size;
00364 }
00365 }
00366
00367 static void rtp_send_raw(AVFormatContext *s1,
00368 const uint8_t *buf1, int size)
00369 {
00370 RTPMuxContext *s = s1->priv_data;
00371 int len, max_packet_size;
00372
00373 max_packet_size = s->max_payload_size;
00374
00375 while (size > 0) {
00376 len = max_packet_size;
00377 if (len > size)
00378 len = size;
00379
00380 s->timestamp = s->cur_timestamp;
00381 ff_rtp_send_data(s1, buf1, len, (len == size));
00382
00383 buf1 += len;
00384 size -= len;
00385 }
00386 }
00387
00388
00389 static void rtp_send_mpegts_raw(AVFormatContext *s1,
00390 const uint8_t *buf1, int size)
00391 {
00392 RTPMuxContext *s = s1->priv_data;
00393 int len, out_len;
00394
00395 while (size >= TS_PACKET_SIZE) {
00396 len = s->max_payload_size - (s->buf_ptr - s->buf);
00397 if (len > size)
00398 len = size;
00399 memcpy(s->buf_ptr, buf1, len);
00400 buf1 += len;
00401 size -= len;
00402 s->buf_ptr += len;
00403
00404 out_len = s->buf_ptr - s->buf;
00405 if (out_len >= s->max_payload_size) {
00406 ff_rtp_send_data(s1, s->buf, out_len, 0);
00407 s->buf_ptr = s->buf;
00408 }
00409 }
00410 }
00411
00412 static int rtp_send_ilbc(AVFormatContext *s1, const uint8_t *buf, int size)
00413 {
00414 RTPMuxContext *s = s1->priv_data;
00415 AVStream *st = s1->streams[0];
00416 int frame_duration = av_get_audio_frame_duration(st->codec, 0);
00417 int frame_size = st->codec->block_align;
00418 int frames = size / frame_size;
00419
00420 while (frames > 0) {
00421 int n = FFMIN(s->max_frames_per_packet - s->num_frames, frames);
00422
00423 if (!s->num_frames) {
00424 s->buf_ptr = s->buf;
00425 s->timestamp = s->cur_timestamp;
00426 }
00427 memcpy(s->buf_ptr, buf, n * frame_size);
00428 frames -= n;
00429 s->num_frames += n;
00430 s->buf_ptr += n * frame_size;
00431 buf += n * frame_size;
00432 s->cur_timestamp += n * frame_duration;
00433
00434 if (s->num_frames == s->max_frames_per_packet) {
00435 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 1);
00436 s->num_frames = 0;
00437 }
00438 }
00439 return 0;
00440 }
00441
00442 static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
00443 {
00444 RTPMuxContext *s = s1->priv_data;
00445 AVStream *st = s1->streams[0];
00446 int rtcp_bytes;
00447 int size= pkt->size;
00448
00449 av_dlog(s1, "%d: write len=%d\n", pkt->stream_index, size);
00450
00451 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
00452 RTCP_TX_RATIO_DEN;
00453 if ((s->first_packet || ((rtcp_bytes >= RTCP_SR_SIZE) &&
00454 (ff_ntp_time() - s->last_rtcp_ntp_time > 5000000))) &&
00455 !(s->flags & FF_RTP_FLAG_SKIP_RTCP)) {
00456 rtcp_send_sr(s1, ff_ntp_time());
00457 s->last_octet_count = s->octet_count;
00458 s->first_packet = 0;
00459 }
00460 s->cur_timestamp = s->base_timestamp + pkt->pts;
00461
00462 switch(st->codec->codec_id) {
00463 case AV_CODEC_ID_PCM_MULAW:
00464 case AV_CODEC_ID_PCM_ALAW:
00465 case AV_CODEC_ID_PCM_U8:
00466 case AV_CODEC_ID_PCM_S8:
00467 return rtp_send_samples(s1, pkt->data, size, 8 * st->codec->channels);
00468 case AV_CODEC_ID_PCM_U16BE:
00469 case AV_CODEC_ID_PCM_U16LE:
00470 case AV_CODEC_ID_PCM_S16BE:
00471 case AV_CODEC_ID_PCM_S16LE:
00472 return rtp_send_samples(s1, pkt->data, size, 16 * st->codec->channels);
00473 case AV_CODEC_ID_ADPCM_G722:
00474
00475
00476
00477
00478 return rtp_send_samples(s1, pkt->data, size, 8 * st->codec->channels);
00479 case AV_CODEC_ID_ADPCM_G726:
00480 return rtp_send_samples(s1, pkt->data, size,
00481 st->codec->bits_per_coded_sample * st->codec->channels);
00482 case AV_CODEC_ID_MP2:
00483 case AV_CODEC_ID_MP3:
00484 rtp_send_mpegaudio(s1, pkt->data, size);
00485 break;
00486 case AV_CODEC_ID_MPEG1VIDEO:
00487 case AV_CODEC_ID_MPEG2VIDEO:
00488 ff_rtp_send_mpegvideo(s1, pkt->data, size);
00489 break;
00490 case AV_CODEC_ID_AAC:
00491 if (s->flags & FF_RTP_FLAG_MP4A_LATM)
00492 ff_rtp_send_latm(s1, pkt->data, size);
00493 else
00494 ff_rtp_send_aac(s1, pkt->data, size);
00495 break;
00496 case AV_CODEC_ID_AMR_NB:
00497 case AV_CODEC_ID_AMR_WB:
00498 ff_rtp_send_amr(s1, pkt->data, size);
00499 break;
00500 case AV_CODEC_ID_MPEG2TS:
00501 rtp_send_mpegts_raw(s1, pkt->data, size);
00502 break;
00503 case AV_CODEC_ID_H264:
00504 ff_rtp_send_h264(s1, pkt->data, size);
00505 break;
00506 case AV_CODEC_ID_H263:
00507 if (s->flags & FF_RTP_FLAG_RFC2190) {
00508 int mb_info_size = 0;
00509 const uint8_t *mb_info =
00510 av_packet_get_side_data(pkt, AV_PKT_DATA_H263_MB_INFO,
00511 &mb_info_size);
00512 ff_rtp_send_h263_rfc2190(s1, pkt->data, size, mb_info, mb_info_size);
00513 break;
00514 }
00515
00516 case AV_CODEC_ID_H263P:
00517 ff_rtp_send_h263(s1, pkt->data, size);
00518 break;
00519 case AV_CODEC_ID_VORBIS:
00520 case AV_CODEC_ID_THEORA:
00521 ff_rtp_send_xiph(s1, pkt->data, size);
00522 break;
00523 case AV_CODEC_ID_VP8:
00524 ff_rtp_send_vp8(s1, pkt->data, size);
00525 break;
00526 case AV_CODEC_ID_ILBC:
00527 rtp_send_ilbc(s1, pkt->data, size);
00528 break;
00529 case AV_CODEC_ID_MJPEG:
00530 ff_rtp_send_jpeg(s1, pkt->data, size);
00531 break;
00532 default:
00533
00534 rtp_send_raw(s1, pkt->data, size);
00535 break;
00536 }
00537 return 0;
00538 }
00539
00540 static int rtp_write_trailer(AVFormatContext *s1)
00541 {
00542 RTPMuxContext *s = s1->priv_data;
00543
00544 av_freep(&s->buf);
00545
00546 return 0;
00547 }
00548
00549 AVOutputFormat ff_rtp_muxer = {
00550 .name = "rtp",
00551 .long_name = NULL_IF_CONFIG_SMALL("RTP output"),
00552 .priv_data_size = sizeof(RTPMuxContext),
00553 .audio_codec = AV_CODEC_ID_PCM_MULAW,
00554 .video_codec = AV_CODEC_ID_MPEG4,
00555 .write_header = rtp_write_header,
00556 .write_packet = rtp_write_packet,
00557 .write_trailer = rtp_write_trailer,
00558 .priv_class = &rtp_muxer_class,
00559 };