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