FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/rtpenc_mpegts.c
Date: 2024-03-29 01:21:52
Exec Total Coverage
Lines: 0 106 0.0%
Functions: 0 3 0.0%
Branches: 0 42 0.0%

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