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.

156 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_parameters_copy(st->codecpar, s->streams[i]->codecpar);
  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->codecpar->codec_id = AV_CODEC_ID_MPEG2TS;
  83. rtp_ctx->pb = s->pb;
  84. if ((ret = avformat_write_header(rtp_ctx, NULL)) < 0)
  85. goto fail;
  86. chain->rtp_ctx = rtp_ctx;
  87. return 0;
  88. fail:
  89. if (mpegts_ctx) {
  90. ffio_free_dyn_buf(&mpegts_ctx->pb);
  91. avformat_free_context(mpegts_ctx);
  92. }
  93. if (rtp_ctx)
  94. avformat_free_context(rtp_ctx);
  95. rtp_mpegts_write_close(s);
  96. return ret;
  97. }
  98. static int rtp_mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
  99. {
  100. struct MuxChain *chain = s->priv_data;
  101. int ret = 0, size;
  102. uint8_t *buf;
  103. AVPacket local_pkt;
  104. if (!chain->mpegts_ctx->pb) {
  105. if ((ret = avio_open_dyn_buf(&chain->mpegts_ctx->pb)) < 0)
  106. return ret;
  107. }
  108. if ((ret = av_write_frame(chain->mpegts_ctx, pkt)) < 0)
  109. return ret;
  110. size = avio_close_dyn_buf(chain->mpegts_ctx->pb, &buf);
  111. chain->mpegts_ctx->pb = NULL;
  112. if (size == 0) {
  113. av_free(buf);
  114. return 0;
  115. }
  116. av_init_packet(&local_pkt);
  117. local_pkt.data = buf;
  118. local_pkt.size = size;
  119. local_pkt.stream_index = 0;
  120. if (pkt->pts != AV_NOPTS_VALUE)
  121. local_pkt.pts = av_rescale_q(pkt->pts,
  122. s->streams[pkt->stream_index]->time_base,
  123. chain->rtp_ctx->streams[0]->time_base);
  124. if (pkt->dts != AV_NOPTS_VALUE)
  125. local_pkt.dts = av_rescale_q(pkt->dts,
  126. s->streams[pkt->stream_index]->time_base,
  127. chain->rtp_ctx->streams[0]->time_base);
  128. ret = av_write_frame(chain->rtp_ctx, &local_pkt);
  129. av_free(buf);
  130. return ret;
  131. }
  132. AVOutputFormat ff_rtp_mpegts_muxer = {
  133. .name = "rtp_mpegts",
  134. .long_name = NULL_IF_CONFIG_SMALL("RTP/mpegts output format"),
  135. .priv_data_size = sizeof(struct MuxChain),
  136. .audio_codec = AV_CODEC_ID_AAC,
  137. .video_codec = AV_CODEC_ID_MPEG4,
  138. .write_header = rtp_mpegts_write_header,
  139. .write_packet = rtp_mpegts_write_packet,
  140. .write_trailer = rtp_mpegts_write_close,
  141. };