]> git.sesse.net Git - ffmpeg/blob - libavformat/rtpenc_mpegts.c
Merge commit '7e4e010b80e76862e83afbd41c25d50e72f0b44c'
[ffmpeg] / libavformat / rtpenc_mpegts.c
1 /*
2  * RTP/mpegts muxer
3  * Copyright (c) 2011 Martin Storsjo
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 "libavutil/mathematics.h"
23 #include "avformat.h"
24
25 struct MuxChain {
26     AVFormatContext *mpegts_ctx;
27     AVFormatContext *rtp_ctx;
28 };
29
30 static int rtp_mpegts_write_close(AVFormatContext *s)
31 {
32     struct MuxChain *chain = s->priv_data;
33
34     if (chain->mpegts_ctx) {
35         av_write_trailer(chain->mpegts_ctx);
36         if (chain->mpegts_ctx->pb) {
37             uint8_t *buf;
38             avio_close_dyn_buf(chain->mpegts_ctx->pb, &buf);
39             av_free(buf);
40         }
41         avformat_free_context(chain->mpegts_ctx);
42     }
43     if (chain->rtp_ctx) {
44         av_write_trailer(chain->rtp_ctx);
45         avformat_free_context(chain->rtp_ctx);
46     }
47     return 0;
48 }
49
50 static int rtp_mpegts_write_header(AVFormatContext *s)
51 {
52     struct MuxChain *chain = s->priv_data;
53     AVFormatContext *mpegts_ctx = NULL, *rtp_ctx = NULL;
54     AVOutputFormat *mpegts_format = av_guess_format("mpegts", NULL, NULL);
55     AVOutputFormat *rtp_format    = av_guess_format("rtp", NULL, NULL);
56     int i, ret = AVERROR(ENOMEM);
57     AVStream *st;
58
59     if (!mpegts_format || !rtp_format)
60         return AVERROR(ENOSYS);
61     mpegts_ctx = avformat_alloc_context();
62     if (!mpegts_ctx)
63         return AVERROR(ENOMEM);
64     mpegts_ctx->oformat   = mpegts_format;
65     mpegts_ctx->max_delay = s->max_delay;
66     for (i = 0; i < s->nb_streams; i++) {
67         AVStream* st = avformat_new_stream(mpegts_ctx, NULL);
68         if (!st)
69             goto fail;
70         st->time_base           = s->streams[i]->time_base;
71         st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
72         avcodec_copy_context(st->codec, s->streams[i]->codec);
73     }
74     if ((ret = avio_open_dyn_buf(&mpegts_ctx->pb)) < 0)
75         goto fail;
76     if ((ret = avformat_write_header(mpegts_ctx, NULL)) < 0)
77         goto fail;
78     for (i = 0; i < s->nb_streams; i++)
79         s->streams[i]->time_base = mpegts_ctx->streams[i]->time_base;
80
81     chain->mpegts_ctx = mpegts_ctx;
82     mpegts_ctx = NULL;
83
84     rtp_ctx = avformat_alloc_context();
85     if (!rtp_ctx) {
86         ret = AVERROR(ENOMEM);
87         goto fail;
88     }
89     rtp_ctx->oformat = rtp_format;
90     st = avformat_new_stream(rtp_ctx, NULL);
91     st->time_base.num   = 1;
92     st->time_base.den   = 90000;
93     st->codec->codec_id = AV_CODEC_ID_MPEG2TS;
94     chain->rtp_ctx = rtp_ctx;
95     rtp_ctx->pb = s->pb;
96     if ((ret = avformat_write_header(rtp_ctx, NULL)) < 0)
97         goto fail;
98     rtp_ctx = NULL;
99
100     return 0;
101
102 fail:
103     if (mpegts_ctx) {
104         if (mpegts_ctx->pb) {
105             uint8_t *buf;
106             avio_close_dyn_buf(mpegts_ctx->pb, &buf);
107             av_free(buf);
108         }
109         avformat_free_context(mpegts_ctx);
110     }
111     if (rtp_ctx)
112         avformat_free_context(rtp_ctx);
113     rtp_mpegts_write_close(s);
114     return ret;
115 }
116
117 static int rtp_mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
118 {
119     struct MuxChain *chain = s->priv_data;
120     int ret = 0, size;
121     uint8_t *buf;
122     AVPacket local_pkt;
123
124     if (!chain->mpegts_ctx->pb) {
125         if ((ret = avio_open_dyn_buf(&chain->mpegts_ctx->pb)) < 0)
126             return ret;
127     }
128     if ((ret = av_write_frame(chain->mpegts_ctx, pkt)) < 0)
129         return ret;
130     size = avio_close_dyn_buf(chain->mpegts_ctx->pb, &buf);
131     chain->mpegts_ctx->pb = NULL;
132     if (size == 0) {
133         av_free(buf);
134         return 0;
135     }
136     av_init_packet(&local_pkt);
137     local_pkt.data         = buf;
138     local_pkt.size         = size;
139     local_pkt.stream_index = 0;
140     if (pkt->pts != AV_NOPTS_VALUE)
141         local_pkt.pts = av_rescale_q(pkt->pts,
142                                      s->streams[pkt->stream_index]->time_base,
143                                      chain->rtp_ctx->streams[0]->time_base);
144     if (pkt->dts != AV_NOPTS_VALUE)
145         local_pkt.dts = av_rescale_q(pkt->dts,
146                                      s->streams[pkt->stream_index]->time_base,
147                                      chain->rtp_ctx->streams[0]->time_base);
148     ret = av_write_frame(chain->rtp_ctx, &local_pkt);
149     av_free(buf);
150
151     return ret;
152 }
153
154 AVOutputFormat ff_rtp_mpegts_muxer = {
155     .name              = "rtp_mpegts",
156     .long_name         = NULL_IF_CONFIG_SMALL("RTP/mpegts output format"),
157     .priv_data_size    = sizeof(struct MuxChain),
158     .audio_codec       = AV_CODEC_ID_AAC,
159     .video_codec       = AV_CODEC_ID_MPEG4,
160     .write_header      = rtp_mpegts_write_header,
161     .write_packet      = rtp_mpegts_write_packet,
162     .write_trailer     = rtp_mpegts_write_close,
163 };