]> git.sesse.net Git - ffmpeg/blob - libavformat/rtpenc_mpegts.c
avformat/rtpenc_mpegts: convey options for rtp muxer
[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 "libavutil/opt.h"
24 #include "avformat.h"
25 #include "avio_internal.h"
26
27 typedef struct MuxChain {
28     AVFormatContext *mpegts_ctx;
29     AVFormatContext *rtp_ctx;
30     AVPacket *pkt;
31     AVDictionary* mpegts_muxer_options;
32     AVDictionary* rtp_muxer_options;
33 } MuxChain;
34
35 static int rtp_mpegts_write_close(AVFormatContext *s)
36 {
37     MuxChain *chain = s->priv_data;
38
39     if (chain->mpegts_ctx) {
40         av_write_trailer(chain->mpegts_ctx);
41         ffio_free_dyn_buf(&chain->mpegts_ctx->pb);
42         avformat_free_context(chain->mpegts_ctx);
43     }
44     if (chain->rtp_ctx) {
45         av_write_trailer(chain->rtp_ctx);
46         avformat_free_context(chain->rtp_ctx);
47     }
48
49     av_packet_free(&chain->pkt);
50
51     return 0;
52 }
53
54 static int rtp_mpegts_write_header(AVFormatContext *s)
55 {
56     MuxChain *chain = s->priv_data;
57     AVFormatContext *mpegts_ctx = NULL, *rtp_ctx = NULL;
58     ff_const59 AVOutputFormat *mpegts_format = av_guess_format("mpegts", NULL, NULL);
59     ff_const59 AVOutputFormat *rtp_format    = av_guess_format("rtp", NULL, NULL);
60     int i, ret = AVERROR(ENOMEM);
61     AVStream *st;
62     AVDictionary *mpegts_muxer_options = NULL;
63     AVDictionary *rtp_muxer_options = NULL;
64
65     if (!mpegts_format || !rtp_format)
66         return AVERROR(ENOSYS);
67     mpegts_ctx = avformat_alloc_context();
68     if (!mpegts_ctx)
69         return AVERROR(ENOMEM);
70     chain->pkt = av_packet_alloc();
71     if (!chain->pkt)
72         goto fail;
73     mpegts_ctx->oformat   = mpegts_format;
74     mpegts_ctx->max_delay = s->max_delay;
75     av_dict_copy(&mpegts_ctx->metadata, s->metadata, 0);
76     for (i = 0; i < s->nb_streams; i++) {
77         AVStream* st = avformat_new_stream(mpegts_ctx, NULL);
78         if (!st)
79             goto fail;
80         st->time_base           = s->streams[i]->time_base;
81         st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
82         st->id                  = s->streams[i]->id;
83         avcodec_parameters_copy(st->codecpar, s->streams[i]->codecpar);
84     }
85     if ((ret = avio_open_dyn_buf(&mpegts_ctx->pb)) < 0)
86         goto fail;
87
88     av_dict_copy(&mpegts_muxer_options, chain->mpegts_muxer_options, 0);
89
90     if ((ret = avformat_write_header(mpegts_ctx, &mpegts_muxer_options)) < 0)
91         goto fail;
92     for (i = 0; i < s->nb_streams; i++)
93         s->streams[i]->time_base = mpegts_ctx->streams[i]->time_base;
94
95     chain->mpegts_ctx = mpegts_ctx;
96     mpegts_ctx = NULL;
97
98     rtp_ctx = avformat_alloc_context();
99     if (!rtp_ctx) {
100         ret = AVERROR(ENOMEM);
101         goto fail;
102     }
103     rtp_ctx->oformat = rtp_format;
104     st = avformat_new_stream(rtp_ctx, NULL);
105     if (!st) {
106         ret = AVERROR(ENOMEM);
107         goto fail;
108     }
109     st->time_base.num   = 1;
110     st->time_base.den   = 90000;
111     st->codecpar->codec_id = AV_CODEC_ID_MPEG2TS;
112     rtp_ctx->pb = s->pb;
113     av_dict_copy(&rtp_muxer_options, chain->rtp_muxer_options, 0);
114     if ((ret = avformat_write_header(rtp_ctx, &rtp_muxer_options)) < 0)
115         goto fail;
116     chain->rtp_ctx = rtp_ctx;
117
118     return 0;
119
120 fail:
121     if (mpegts_ctx) {
122         ffio_free_dyn_buf(&mpegts_ctx->pb);
123         av_dict_free(&mpegts_ctx->metadata);
124         av_dict_free(&mpegts_muxer_options);
125         avformat_free_context(mpegts_ctx);
126     }
127     av_dict_free(&rtp_muxer_options);
128     avformat_free_context(rtp_ctx);
129     rtp_mpegts_write_close(s);
130     return ret;
131 }
132
133 static int rtp_mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
134 {
135     MuxChain *chain = s->priv_data;
136     int ret = 0, size;
137     uint8_t *buf;
138     AVPacket *local_pkt = chain->pkt;
139
140     if (!chain->mpegts_ctx->pb) {
141         if ((ret = avio_open_dyn_buf(&chain->mpegts_ctx->pb)) < 0)
142             return ret;
143     }
144     if ((ret = av_write_frame(chain->mpegts_ctx, pkt)) < 0)
145         return ret;
146     size = avio_close_dyn_buf(chain->mpegts_ctx->pb, &buf);
147     chain->mpegts_ctx->pb = NULL;
148     if (size == 0) {
149         av_free(buf);
150         return 0;
151     }
152     av_packet_unref(local_pkt);
153     local_pkt->data         = buf;
154     local_pkt->size         = size;
155     local_pkt->stream_index = 0;
156     if (pkt->pts != AV_NOPTS_VALUE)
157         local_pkt->pts = av_rescale_q(pkt->pts,
158                                      s->streams[pkt->stream_index]->time_base,
159                                      chain->rtp_ctx->streams[0]->time_base);
160     if (pkt->dts != AV_NOPTS_VALUE)
161         local_pkt->dts = av_rescale_q(pkt->dts,
162                                      s->streams[pkt->stream_index]->time_base,
163                                      chain->rtp_ctx->streams[0]->time_base);
164     ret = av_write_frame(chain->rtp_ctx, local_pkt);
165     av_free(buf);
166
167     return ret;
168 }
169
170 #define OFFSET(x) offsetof(MuxChain, x)
171 #define E AV_OPT_FLAG_ENCODING_PARAM
172 static const AVOption options[] = {
173     { "mpegts_muxer_options", "set list of options for the MPEG-TS muxer", OFFSET(mpegts_muxer_options), AV_OPT_TYPE_DICT, {.str = NULL}, 0, 0, E },
174     { "rtp_muxer_options",    "set list of options for the RTP muxer",     OFFSET(rtp_muxer_options),    AV_OPT_TYPE_DICT, {.str = NULL}, 0, 0, E },
175     { NULL },
176 };
177
178 static const AVClass rtp_mpegts_class = {
179     .class_name = "rtp_mpegts muxer",
180     .item_name  = av_default_item_name,
181     .option     = options,
182     .version    = LIBAVUTIL_VERSION_INT,
183 };
184
185 AVOutputFormat ff_rtp_mpegts_muxer = {
186     .name              = "rtp_mpegts",
187     .long_name         = NULL_IF_CONFIG_SMALL("RTP/mpegts output format"),
188     .priv_data_size    = sizeof(MuxChain),
189     .audio_codec       = AV_CODEC_ID_AAC,
190     .video_codec       = AV_CODEC_ID_MPEG4,
191     .write_header      = rtp_mpegts_write_header,
192     .write_packet      = rtp_mpegts_write_packet,
193     .write_trailer     = rtp_mpegts_write_close,
194     .priv_class        = &rtp_mpegts_class,
195 };