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