You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
6.4KB

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