FFmpeg  4.3.7
rtpenc_mpegts.c
Go to the documentation of this file.
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 #include "avio_internal.h"
25 
26 struct MuxChain {
29 };
30 
32 {
33  struct MuxChain *chain = s->priv_data;
34 
35  if (chain->mpegts_ctx) {
39  }
40  if (chain->rtp_ctx) {
41  av_write_trailer(chain->rtp_ctx);
43  }
44  return 0;
45 }
46 
48 {
49  struct MuxChain *chain = s->priv_data;
51  ff_const59 AVOutputFormat *mpegts_format = av_guess_format("mpegts", NULL, NULL);
52  ff_const59 AVOutputFormat *rtp_format = av_guess_format("rtp", NULL, NULL);
53  int i, ret = AVERROR(ENOMEM);
54  AVStream *st;
55 
56  if (!mpegts_format || !rtp_format)
57  return AVERROR(ENOSYS);
58  mpegts_ctx = avformat_alloc_context();
59  if (!mpegts_ctx)
60  return AVERROR(ENOMEM);
61  mpegts_ctx->oformat = mpegts_format;
62  mpegts_ctx->max_delay = s->max_delay;
63  av_dict_copy(&mpegts_ctx->metadata, s->metadata, 0);
64  for (i = 0; i < s->nb_streams; i++) {
65  AVStream* st = avformat_new_stream(mpegts_ctx, NULL);
66  if (!st)
67  goto fail;
68  st->time_base = s->streams[i]->time_base;
71  }
72  if ((ret = avio_open_dyn_buf(&mpegts_ctx->pb)) < 0)
73  goto fail;
74  if ((ret = avformat_write_header(mpegts_ctx, NULL)) < 0)
75  goto fail;
76  for (i = 0; i < s->nb_streams; i++)
77  s->streams[i]->time_base = mpegts_ctx->streams[i]->time_base;
78 
79  chain->mpegts_ctx = mpegts_ctx;
80  mpegts_ctx = NULL;
81 
82  rtp_ctx = avformat_alloc_context();
83  if (!rtp_ctx) {
84  ret = AVERROR(ENOMEM);
85  goto fail;
86  }
87  rtp_ctx->oformat = rtp_format;
88  st = avformat_new_stream(rtp_ctx, NULL);
89  if (!st) {
90  ret = AVERROR(ENOMEM);
91  goto fail;
92  }
93  st->time_base.num = 1;
94  st->time_base.den = 90000;
96  rtp_ctx->pb = s->pb;
97  if ((ret = avformat_write_header(rtp_ctx, NULL)) < 0)
98  goto fail;
99  chain->rtp_ctx = rtp_ctx;
100 
101  return 0;
102 
103 fail:
104  if (mpegts_ctx) {
105  ffio_free_dyn_buf(&mpegts_ctx->pb);
106  av_dict_free(&mpegts_ctx->metadata);
107  avformat_free_context(mpegts_ctx);
108  }
109  avformat_free_context(rtp_ctx);
111  return ret;
112 }
113 
115 {
116  struct MuxChain *chain = s->priv_data;
117  int ret = 0, size;
118  uint8_t *buf;
119  AVPacket local_pkt;
120 
121  if (!chain->mpegts_ctx->pb) {
122  if ((ret = avio_open_dyn_buf(&chain->mpegts_ctx->pb)) < 0)
123  return ret;
124  }
125  if ((ret = av_write_frame(chain->mpegts_ctx, pkt)) < 0)
126  return ret;
127  size = avio_close_dyn_buf(chain->mpegts_ctx->pb, &buf);
128  chain->mpegts_ctx->pb = NULL;
129  if (size == 0) {
130  av_free(buf);
131  return 0;
132  }
133  av_init_packet(&local_pkt);
134  local_pkt.data = buf;
135  local_pkt.size = size;
136  local_pkt.stream_index = 0;
137  if (pkt->pts != AV_NOPTS_VALUE)
138  local_pkt.pts = av_rescale_q(pkt->pts,
139  s->streams[pkt->stream_index]->time_base,
140  chain->rtp_ctx->streams[0]->time_base);
141  if (pkt->dts != AV_NOPTS_VALUE)
142  local_pkt.dts = av_rescale_q(pkt->dts,
143  s->streams[pkt->stream_index]->time_base,
144  chain->rtp_ctx->streams[0]->time_base);
145  ret = av_write_frame(chain->rtp_ctx, &local_pkt);
146  av_free(buf);
147 
148  return ret;
149 }
150 
152  .name = "rtp_mpegts",
153  .long_name = NULL_IF_CONFIG_SMALL("RTP/mpegts output format"),
154  .priv_data_size = sizeof(struct MuxChain),
155  .audio_codec = AV_CODEC_ID_AAC,
156  .video_codec = AV_CODEC_ID_MPEG4,
157  .write_header = rtp_mpegts_write_header,
158  .write_packet = rtp_mpegts_write_packet,
159  .write_trailer = rtp_mpegts_write_close,
160 };
AVOutputFormat ff_rtp_mpegts_muxer
#define NULL
Definition: coverity.c:32
int size
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1401
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:1190
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:938
int num
Numerator.
Definition: rational.h:59
int size
Definition: packet.h:356
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:217
static AVPacket pkt
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1356
Format I/O context.
Definition: avformat.h:1351
uint8_t
static int rtp_mpegts_write_header(AVFormatContext *s)
Definition: rtpenc_mpegts.c:47
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4526
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1419
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:144
uint8_t * data
Definition: packet.h:355
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
#define ff_const59
The ff_const59 define is not part of the public API and will be removed without further warning...
Definition: avformat.h:544
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1591
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:203
static int rtp_mpegts_write_close(AVFormatContext *s)
Definition: rtpenc_mpegts.c:31
#define fail()
Definition: checkasm.h:123
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1407
av_warn_unused_result int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
Definition: mux.c:505
static int rtp_mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
const char * name
Definition: avformat.h:500
#define s(width, name)
Definition: cbs_vp9.c:257
ff_const59 struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1370
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1431
Stream structure.
Definition: avformat.h:876
AVIOContext * pb
I/O context.
Definition: avformat.h:1393
ff_const59 AVOutputFormat * av_guess_format(const char *short_name, const char *filename, const char *mime_type)
Return the output format in the list of registered output formats which best matches the provided par...
Definition: format.c:51
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:4455
Main libavformat public API header.
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:35
int den
Denominator.
Definition: rational.h:60
FAKE codec to indicate a raw MPEG-2 TS stream (only used by libavformat)
Definition: codec_id.h:554
#define av_free(p)
void * priv_data
Format private data.
Definition: avformat.h:1379
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: packet.h:354
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:1251
AVFormatContext * mpegts_ctx
Definition: rtpenc_mpegts.c:27
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1023
int stream_index
Definition: packet.h:357
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:905
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: utils.c:2115
This structure stores compressed data.
Definition: packet.h:332
AVFormatContext * rtp_ctx
Definition: rtpenc_mpegts.c:28
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:348
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248