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.

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