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.

161 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. #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. ff_const59 AVOutputFormat *mpegts_format = av_guess_format("mpegts", NULL, NULL);
  47. ff_const59 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. av_dict_copy(&mpegts_ctx->metadata, s->metadata, 0);
  58. for (i = 0; i < s->nb_streams; i++) {
  59. AVStream* st = avformat_new_stream(mpegts_ctx, NULL);
  60. if (!st)
  61. goto fail;
  62. st->time_base = s->streams[i]->time_base;
  63. st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
  64. avcodec_parameters_copy(st->codecpar, s->streams[i]->codecpar);
  65. }
  66. if ((ret = avio_open_dyn_buf(&mpegts_ctx->pb)) < 0)
  67. goto fail;
  68. if ((ret = avformat_write_header(mpegts_ctx, NULL)) < 0)
  69. goto fail;
  70. for (i = 0; i < s->nb_streams; i++)
  71. s->streams[i]->time_base = mpegts_ctx->streams[i]->time_base;
  72. chain->mpegts_ctx = mpegts_ctx;
  73. mpegts_ctx = NULL;
  74. rtp_ctx = avformat_alloc_context();
  75. if (!rtp_ctx) {
  76. ret = AVERROR(ENOMEM);
  77. goto fail;
  78. }
  79. rtp_ctx->oformat = rtp_format;
  80. st = avformat_new_stream(rtp_ctx, NULL);
  81. if (!st) {
  82. ret = AVERROR(ENOMEM);
  83. goto fail;
  84. }
  85. st->time_base.num = 1;
  86. st->time_base.den = 90000;
  87. st->codecpar->codec_id = AV_CODEC_ID_MPEG2TS;
  88. rtp_ctx->pb = s->pb;
  89. if ((ret = avformat_write_header(rtp_ctx, NULL)) < 0)
  90. goto fail;
  91. chain->rtp_ctx = rtp_ctx;
  92. return 0;
  93. fail:
  94. if (mpegts_ctx) {
  95. ffio_free_dyn_buf(&mpegts_ctx->pb);
  96. av_dict_free(&mpegts_ctx->metadata);
  97. avformat_free_context(mpegts_ctx);
  98. }
  99. avformat_free_context(rtp_ctx);
  100. rtp_mpegts_write_close(s);
  101. return ret;
  102. }
  103. static int rtp_mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
  104. {
  105. struct MuxChain *chain = s->priv_data;
  106. int ret = 0, size;
  107. uint8_t *buf;
  108. AVPacket local_pkt;
  109. if (!chain->mpegts_ctx->pb) {
  110. if ((ret = avio_open_dyn_buf(&chain->mpegts_ctx->pb)) < 0)
  111. return ret;
  112. }
  113. if ((ret = av_write_frame(chain->mpegts_ctx, pkt)) < 0)
  114. return ret;
  115. size = avio_close_dyn_buf(chain->mpegts_ctx->pb, &buf);
  116. chain->mpegts_ctx->pb = NULL;
  117. if (size == 0) {
  118. av_free(buf);
  119. return 0;
  120. }
  121. av_init_packet(&local_pkt);
  122. local_pkt.data = buf;
  123. local_pkt.size = size;
  124. local_pkt.stream_index = 0;
  125. if (pkt->pts != AV_NOPTS_VALUE)
  126. local_pkt.pts = av_rescale_q(pkt->pts,
  127. s->streams[pkt->stream_index]->time_base,
  128. chain->rtp_ctx->streams[0]->time_base);
  129. if (pkt->dts != AV_NOPTS_VALUE)
  130. local_pkt.dts = av_rescale_q(pkt->dts,
  131. s->streams[pkt->stream_index]->time_base,
  132. chain->rtp_ctx->streams[0]->time_base);
  133. ret = av_write_frame(chain->rtp_ctx, &local_pkt);
  134. av_free(buf);
  135. return ret;
  136. }
  137. AVOutputFormat ff_rtp_mpegts_muxer = {
  138. .name = "rtp_mpegts",
  139. .long_name = NULL_IF_CONFIG_SMALL("RTP/mpegts output format"),
  140. .priv_data_size = sizeof(struct MuxChain),
  141. .audio_codec = AV_CODEC_ID_AAC,
  142. .video_codec = AV_CODEC_ID_MPEG4,
  143. .write_header = rtp_mpegts_write_header,
  144. .write_packet = rtp_mpegts_write_packet,
  145. .write_trailer = rtp_mpegts_write_close,
  146. };