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.

166 lines
5.2KB

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