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