FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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  AVOutputFormat *mpegts_format = av_guess_format("mpegts", NULL, NULL);
52  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  for (i = 0; i < s->nb_streams; i++) {
64  AVStream* st = avformat_new_stream(mpegts_ctx, NULL);
65  if (!st)
66  goto fail;
67  st->time_base = s->streams[i]->time_base;
70  }
71  if ((ret = avio_open_dyn_buf(&mpegts_ctx->pb)) < 0)
72  goto fail;
73  if ((ret = avformat_write_header(mpegts_ctx, NULL)) < 0)
74  goto fail;
75  for (i = 0; i < s->nb_streams; i++)
76  s->streams[i]->time_base = mpegts_ctx->streams[i]->time_base;
77 
78  chain->mpegts_ctx = mpegts_ctx;
79  mpegts_ctx = NULL;
80 
81  rtp_ctx = avformat_alloc_context();
82  if (!rtp_ctx) {
83  ret = AVERROR(ENOMEM);
84  goto fail;
85  }
86  rtp_ctx->oformat = rtp_format;
87  st = avformat_new_stream(rtp_ctx, NULL);
88  st->time_base.num = 1;
89  st->time_base.den = 90000;
91  chain->rtp_ctx = rtp_ctx;
92  rtp_ctx->pb = s->pb;
93  if ((ret = avformat_write_header(rtp_ctx, NULL)) < 0)
94  goto fail;
95  rtp_ctx = NULL;
96 
97  return 0;
98 
99 fail:
100  if (mpegts_ctx) {
101  ffio_free_dyn_buf(&chain->mpegts_ctx->pb);
102  avformat_free_context(mpegts_ctx);
103  }
104  if (rtp_ctx)
105  avformat_free_context(rtp_ctx);
107  return ret;
108 }
109 
111 {
112  struct MuxChain *chain = s->priv_data;
113  int ret = 0, size;
114  uint8_t *buf;
115  AVPacket local_pkt;
116 
117  if (!chain->mpegts_ctx->pb) {
118  if ((ret = avio_open_dyn_buf(&chain->mpegts_ctx->pb)) < 0)
119  return ret;
120  }
121  if ((ret = av_write_frame(chain->mpegts_ctx, pkt)) < 0)
122  return ret;
123  size = avio_close_dyn_buf(chain->mpegts_ctx->pb, &buf);
124  chain->mpegts_ctx->pb = NULL;
125  if (size == 0) {
126  av_free(buf);
127  return 0;
128  }
129  av_init_packet(&local_pkt);
130  local_pkt.data = buf;
131  local_pkt.size = size;
132  local_pkt.stream_index = 0;
133  if (pkt->pts != AV_NOPTS_VALUE)
134  local_pkt.pts = av_rescale_q(pkt->pts,
135  s->streams[pkt->stream_index]->time_base,
136  chain->rtp_ctx->streams[0]->time_base);
137  if (pkt->dts != AV_NOPTS_VALUE)
138  local_pkt.dts = av_rescale_q(pkt->dts,
139  s->streams[pkt->stream_index]->time_base,
140  chain->rtp_ctx->streams[0]->time_base);
141  ret = av_write_frame(chain->rtp_ctx, &local_pkt);
142  av_free(buf);
143 
144  return ret;
145 }
146 
148  .name = "rtp_mpegts",
149  .long_name = NULL_IF_CONFIG_SMALL("RTP/mpegts output format"),
150  .priv_data_size = sizeof(struct MuxChain),
151  .audio_codec = AV_CODEC_ID_AAC,
152  .video_codec = AV_CODEC_ID_MPEG4,
153  .write_header = rtp_mpegts_write_header,
154  .write_packet = rtp_mpegts_write_packet,
155  .write_trailer = rtp_mpegts_write_close,
156 };