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.

164 lines
5.1KB

  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 "avformat.h"
  23. struct MuxChain {
  24. AVFormatContext *mpegts_ctx;
  25. AVFormatContext *rtp_ctx;
  26. };
  27. static int rtp_mpegts_write_close(AVFormatContext *s)
  28. {
  29. struct MuxChain *chain = s->priv_data;
  30. if (chain->mpegts_ctx) {
  31. av_write_trailer(chain->mpegts_ctx);
  32. if (chain->mpegts_ctx->pb) {
  33. uint8_t *buf;
  34. avio_close_dyn_buf(chain->mpegts_ctx->pb, &buf);
  35. av_free(buf);
  36. }
  37. avformat_free_context(chain->mpegts_ctx);
  38. }
  39. if (chain->rtp_ctx) {
  40. av_write_trailer(chain->rtp_ctx);
  41. avformat_free_context(chain->rtp_ctx);
  42. }
  43. return 0;
  44. }
  45. static int rtp_mpegts_write_header(AVFormatContext *s)
  46. {
  47. struct MuxChain *chain = s->priv_data;
  48. AVFormatContext *mpegts_ctx = NULL, *rtp_ctx = NULL;
  49. AVOutputFormat *mpegts_format = av_guess_format("mpegts", NULL, NULL);
  50. AVOutputFormat *rtp_format = av_guess_format("rtp", NULL, NULL);
  51. int i, ret = AVERROR(ENOMEM);
  52. AVStream *st;
  53. if (!mpegts_format || !rtp_format)
  54. return AVERROR(ENOSYS);
  55. mpegts_ctx = avformat_alloc_context();
  56. if (!mpegts_ctx)
  57. return AVERROR(ENOMEM);
  58. mpegts_ctx->oformat = mpegts_format;
  59. mpegts_ctx->max_delay = s->max_delay;
  60. for (i = 0; i < s->nb_streams; i++) {
  61. AVStream* st = avformat_new_stream(mpegts_ctx, NULL);
  62. if (!st)
  63. goto fail;
  64. st->time_base = s->streams[i]->time_base;
  65. st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
  66. avcodec_copy_context(st->codec, s->streams[i]->codec);
  67. }
  68. if ((ret = avio_open_dyn_buf(&mpegts_ctx->pb)) < 0)
  69. goto fail;
  70. if ((ret = avformat_write_header(mpegts_ctx, NULL)) < 0)
  71. goto fail;
  72. for (i = 0; i < s->nb_streams; i++)
  73. s->streams[i]->time_base = mpegts_ctx->streams[i]->time_base;
  74. chain->mpegts_ctx = mpegts_ctx;
  75. mpegts_ctx = NULL;
  76. rtp_ctx = avformat_alloc_context();
  77. if (!rtp_ctx) {
  78. ret = AVERROR(ENOMEM);
  79. goto fail;
  80. }
  81. rtp_ctx->oformat = rtp_format;
  82. st = avformat_new_stream(rtp_ctx, NULL);
  83. st->time_base.num = 1;
  84. st->time_base.den = 90000;
  85. st->codec->codec_id = AV_CODEC_ID_MPEG2TS;
  86. chain->rtp_ctx = rtp_ctx;
  87. rtp_ctx->pb = s->pb;
  88. if ((ret = avformat_write_header(rtp_ctx, NULL)) < 0)
  89. goto fail;
  90. rtp_ctx = NULL;
  91. return 0;
  92. fail:
  93. if (mpegts_ctx) {
  94. if (mpegts_ctx->pb) {
  95. uint8_t *buf;
  96. avio_close_dyn_buf(mpegts_ctx->pb, &buf);
  97. av_free(buf);
  98. }
  99. avformat_free_context(mpegts_ctx);
  100. }
  101. if (rtp_ctx)
  102. avformat_free_context(rtp_ctx);
  103. rtp_mpegts_write_close(s);
  104. return ret;
  105. }
  106. static int rtp_mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
  107. {
  108. struct MuxChain *chain = s->priv_data;
  109. int ret = 0, size;
  110. uint8_t *buf;
  111. AVPacket local_pkt;
  112. if (!chain->mpegts_ctx->pb) {
  113. if ((ret = avio_open_dyn_buf(&chain->mpegts_ctx->pb)) < 0)
  114. return ret;
  115. }
  116. if ((ret = av_write_frame(chain->mpegts_ctx, pkt)) < 0)
  117. return ret;
  118. size = avio_close_dyn_buf(chain->mpegts_ctx->pb, &buf);
  119. chain->mpegts_ctx->pb = NULL;
  120. if (size == 0) {
  121. av_free(buf);
  122. return 0;
  123. }
  124. av_init_packet(&local_pkt);
  125. local_pkt.data = buf;
  126. local_pkt.size = size;
  127. local_pkt.stream_index = 0;
  128. if (pkt->pts != AV_NOPTS_VALUE)
  129. local_pkt.pts = av_rescale_q(pkt->pts,
  130. s->streams[pkt->stream_index]->time_base,
  131. chain->rtp_ctx->streams[0]->time_base);
  132. if (pkt->dts != AV_NOPTS_VALUE)
  133. local_pkt.dts = av_rescale_q(pkt->dts,
  134. s->streams[pkt->stream_index]->time_base,
  135. chain->rtp_ctx->streams[0]->time_base);
  136. ret = av_write_frame(chain->rtp_ctx, &local_pkt);
  137. av_free(buf);
  138. return ret;
  139. }
  140. AVOutputFormat ff_rtp_mpegts_muxer = {
  141. .name = "rtp_mpegts",
  142. .long_name = NULL_IF_CONFIG_SMALL("RTP/mpegts output format"),
  143. .priv_data_size = sizeof(struct MuxChain),
  144. .audio_codec = AV_CODEC_ID_AAC,
  145. .video_codec = AV_CODEC_ID_MPEG4,
  146. .write_header = rtp_mpegts_write_header,
  147. .write_packet = rtp_mpegts_write_packet,
  148. .write_trailer = rtp_mpegts_write_close,
  149. };