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.

260 lines
7.7KB

  1. /*
  2. * Copyright (c) 2015, Vignesh Venkatasubramanian
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file WebM Chunk Muxer
  22. * The chunk muxer enables writing WebM Live chunks where there is a header
  23. * chunk, followed by data chunks where each Cluster is written out as a Chunk.
  24. */
  25. #include <float.h>
  26. #include <time.h>
  27. #include "avformat.h"
  28. #include "avio.h"
  29. #include "internal.h"
  30. #include "libavutil/avassert.h"
  31. #include "libavutil/log.h"
  32. #include "libavutil/opt.h"
  33. #include "libavutil/avstring.h"
  34. #include "libavutil/parseutils.h"
  35. #include "libavutil/mathematics.h"
  36. #include "libavutil/time.h"
  37. #include "libavutil/time_internal.h"
  38. #include "libavutil/timestamp.h"
  39. typedef struct WebMChunkContext {
  40. const AVClass *class;
  41. int chunk_start_index;
  42. char *header_filename;
  43. int chunk_duration;
  44. int chunk_count;
  45. int chunk_index;
  46. uint64_t duration_written;
  47. int prev_pts;
  48. AVOutputFormat *oformat;
  49. AVFormatContext *avf;
  50. } WebMChunkContext;
  51. static int chunk_mux_init(AVFormatContext *s)
  52. {
  53. WebMChunkContext *wc = s->priv_data;
  54. AVFormatContext *oc;
  55. int ret;
  56. ret = avformat_alloc_output_context2(&wc->avf, wc->oformat, NULL, NULL);
  57. if (ret < 0)
  58. return ret;
  59. oc = wc->avf;
  60. oc->interrupt_callback = s->interrupt_callback;
  61. oc->max_delay = s->max_delay;
  62. av_dict_copy(&oc->metadata, s->metadata, 0);
  63. oc->priv_data = av_mallocz(oc->oformat->priv_data_size);
  64. if (!oc->priv_data) {
  65. avio_close(oc->pb);
  66. return AVERROR(ENOMEM);
  67. }
  68. *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
  69. av_opt_set_defaults(oc->priv_data);
  70. av_opt_set_int(oc->priv_data, "dash", 1, 0);
  71. av_opt_set_int(oc->priv_data, "cluster_time_limit", wc->chunk_duration, 0);
  72. av_opt_set_int(oc->priv_data, "live", 1, 0);
  73. oc->streams = s->streams;
  74. oc->nb_streams = s->nb_streams;
  75. return 0;
  76. }
  77. static int set_chunk_filename(AVFormatContext *s, int is_header)
  78. {
  79. WebMChunkContext *wc = s->priv_data;
  80. AVFormatContext *oc = wc->avf;
  81. if (is_header) {
  82. if (!wc->header_filename) {
  83. return AVERROR(EINVAL);
  84. }
  85. av_strlcpy(oc->filename, wc->header_filename, strlen(wc->header_filename) + 1);
  86. } else {
  87. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  88. s->filename, wc->chunk_index) < 0) {
  89. av_log(oc, AV_LOG_ERROR, "Invalid chunk filename template '%s'\n", s->filename);
  90. return AVERROR(EINVAL);
  91. }
  92. }
  93. return 0;
  94. }
  95. static int webm_chunk_write_header(AVFormatContext *s)
  96. {
  97. WebMChunkContext *wc = s->priv_data;
  98. AVFormatContext *oc = NULL;
  99. int ret;
  100. // DASH Streams can only have either one track per file.
  101. if (s->nb_streams != 1) { return AVERROR_INVALIDDATA; }
  102. wc->chunk_count = 0;
  103. wc->chunk_index = wc->chunk_start_index;
  104. wc->oformat = av_guess_format("webm", s->filename, "video/webm");
  105. if (!wc->oformat)
  106. return AVERROR_MUXER_NOT_FOUND;
  107. ret = chunk_mux_init(s);
  108. if (ret < 0)
  109. return ret;
  110. oc = wc->avf;
  111. ret = set_chunk_filename(s, 1);
  112. if (ret< 0)
  113. return ret;
  114. ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  115. &s->interrupt_callback, NULL);
  116. if (ret < 0)
  117. return ret;
  118. oc->pb->seekable = 0;
  119. ret = oc->oformat->write_header(oc);
  120. if (ret < 0)
  121. return ret;
  122. avio_close(oc->pb);
  123. return 0;
  124. }
  125. static int chunk_start(AVFormatContext *s)
  126. {
  127. WebMChunkContext *wc = s->priv_data;
  128. AVFormatContext *oc = wc->avf;
  129. int ret;
  130. ret = set_chunk_filename(s, 0);
  131. if (ret < 0)
  132. return ret;
  133. wc->chunk_index++;
  134. ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  135. &s->interrupt_callback, NULL);
  136. if (ret < 0)
  137. return ret;
  138. oc->pb->seekable = 0;
  139. return 0;
  140. }
  141. static int chunk_end(AVFormatContext *s)
  142. {
  143. WebMChunkContext *wc = s->priv_data;
  144. AVFormatContext *oc = wc->avf;
  145. int ret;
  146. if (wc->chunk_start_index == wc->chunk_index)
  147. return 0;
  148. // Flush the cluster in WebM muxer.
  149. oc->oformat->write_packet(oc, NULL);
  150. ret = avio_close(oc->pb);
  151. if (ret < 0)
  152. return ret;
  153. wc->chunk_count++;
  154. return 0;
  155. }
  156. static int webm_chunk_write_packet(AVFormatContext *s, AVPacket *pkt)
  157. {
  158. WebMChunkContext *wc = s->priv_data;
  159. AVFormatContext *oc = wc->avf;
  160. AVStream *st = s->streams[pkt->stream_index];
  161. int ret;
  162. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  163. wc->duration_written += av_rescale_q(pkt->pts - wc->prev_pts,
  164. st->time_base,
  165. (AVRational) {1, 1000});
  166. wc->prev_pts = pkt->pts;
  167. }
  168. // For video, a new chunk is started only on key frames. For audio, a new
  169. // chunk is started based on chunk_duration.
  170. if ((st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  171. (pkt->flags & AV_PKT_FLAG_KEY)) ||
  172. (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
  173. (pkt->pts == 0 || wc->duration_written >= wc->chunk_duration))) {
  174. wc->duration_written = 0;
  175. if ((ret = chunk_end(s)) < 0 || (ret = chunk_start(s)) < 0) {
  176. goto fail;
  177. }
  178. }
  179. ret = oc->oformat->write_packet(oc, pkt);
  180. if (ret < 0)
  181. goto fail;
  182. fail:
  183. if (ret < 0) {
  184. oc->streams = NULL;
  185. oc->nb_streams = 0;
  186. avformat_free_context(oc);
  187. }
  188. return ret;
  189. }
  190. static int webm_chunk_write_trailer(AVFormatContext *s)
  191. {
  192. WebMChunkContext *wc = s->priv_data;
  193. AVFormatContext *oc = wc->avf;
  194. oc->oformat->write_trailer(oc);
  195. chunk_end(s);
  196. oc->streams = NULL;
  197. oc->nb_streams = 0;
  198. avformat_free_context(oc);
  199. return 0;
  200. }
  201. #define OFFSET(x) offsetof(WebMChunkContext, x)
  202. static const AVOption options[] = {
  203. { "chunk_start_index", "start index of the chunk", OFFSET(chunk_start_index), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  204. { "header", "filename of the header where the initialization data will be written", OFFSET(header_filename), AV_OPT_TYPE_STRING, { 0 }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  205. { "audio_chunk_duration", "duration of each chunk in milliseconds", OFFSET(chunk_duration), AV_OPT_TYPE_INT, {.i64 = 5000}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  206. { NULL },
  207. };
  208. #if CONFIG_WEBM_CHUNK_MUXER
  209. static const AVClass webm_chunk_class = {
  210. .class_name = "WebM Chunk Muxer",
  211. .item_name = av_default_item_name,
  212. .option = options,
  213. .version = LIBAVUTIL_VERSION_INT,
  214. };
  215. AVOutputFormat ff_webm_chunk_muxer = {
  216. .name = "webm_chunk",
  217. .long_name = NULL_IF_CONFIG_SMALL("WebM Chunk Muxer"),
  218. .mime_type = "video/webm",
  219. .extensions = "chk",
  220. .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER | AVFMT_NEEDNUMBER |
  221. AVFMT_TS_NONSTRICT | AVFMT_ALLOW_FLUSH,
  222. .priv_data_size = sizeof(WebMChunkContext),
  223. .write_header = webm_chunk_write_header,
  224. .write_packet = webm_chunk_write_packet,
  225. .write_trailer = webm_chunk_write_trailer,
  226. .priv_class = &webm_chunk_class,
  227. };
  228. #endif