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.

157 lines
5.0KB

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