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.

267 lines
7.9KB

  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. #define MAX_FILENAME_SIZE 1024
  40. typedef struct WebMChunkContext {
  41. const AVClass *class;
  42. int chunk_start_index;
  43. char *header_filename;
  44. int chunk_duration;
  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. *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
  64. av_opt_set_defaults(oc->priv_data);
  65. av_opt_set_int(oc->priv_data, "dash", 1, 0);
  66. av_opt_set_int(oc->priv_data, "cluster_time_limit", wc->chunk_duration, 0);
  67. av_opt_set_int(oc->priv_data, "live", 1, 0);
  68. oc->streams = s->streams;
  69. oc->nb_streams = s->nb_streams;
  70. return 0;
  71. }
  72. static int get_chunk_filename(AVFormatContext *s, int is_header, char *filename)
  73. {
  74. WebMChunkContext *wc = s->priv_data;
  75. AVFormatContext *oc = wc->avf;
  76. if (!filename) {
  77. return AVERROR(EINVAL);
  78. }
  79. if (is_header) {
  80. if (!wc->header_filename) {
  81. return AVERROR(EINVAL);
  82. }
  83. av_strlcpy(filename, wc->header_filename, strlen(wc->header_filename) + 1);
  84. } else {
  85. if (av_get_frame_filename(filename, MAX_FILENAME_SIZE,
  86. s->filename, wc->chunk_index - 1) < 0) {
  87. av_log(oc, AV_LOG_ERROR, "Invalid chunk filename template '%s'\n", s->filename);
  88. return AVERROR(EINVAL);
  89. }
  90. }
  91. return 0;
  92. }
  93. static int webm_chunk_write_header(AVFormatContext *s)
  94. {
  95. WebMChunkContext *wc = s->priv_data;
  96. AVFormatContext *oc = NULL;
  97. int ret;
  98. // DASH Streams can only have either one track per file.
  99. if (s->nb_streams != 1) { return AVERROR_INVALIDDATA; }
  100. wc->chunk_index = wc->chunk_start_index;
  101. wc->oformat = av_guess_format("webm", s->filename, "video/webm");
  102. if (!wc->oformat)
  103. return AVERROR_MUXER_NOT_FOUND;
  104. ret = chunk_mux_init(s);
  105. if (ret < 0)
  106. return ret;
  107. oc = wc->avf;
  108. ret = get_chunk_filename(s, 1, oc->filename);
  109. if (ret < 0)
  110. return ret;
  111. ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  112. &s->interrupt_callback, NULL);
  113. if (ret < 0)
  114. return ret;
  115. oc->pb->seekable = 0;
  116. ret = oc->oformat->write_header(oc);
  117. if (ret < 0)
  118. return ret;
  119. avio_close(oc->pb);
  120. return 0;
  121. }
  122. static int chunk_start(AVFormatContext *s)
  123. {
  124. WebMChunkContext *wc = s->priv_data;
  125. AVFormatContext *oc = wc->avf;
  126. int ret;
  127. ret = avio_open_dyn_buf(&oc->pb);
  128. if (ret < 0)
  129. return ret;
  130. wc->chunk_index++;
  131. return 0;
  132. }
  133. static int chunk_end(AVFormatContext *s)
  134. {
  135. WebMChunkContext *wc = s->priv_data;
  136. AVFormatContext *oc = wc->avf;
  137. int ret;
  138. int buffer_size;
  139. uint8_t *buffer;
  140. AVIOContext *pb;
  141. char filename[MAX_FILENAME_SIZE];
  142. if (wc->chunk_start_index == wc->chunk_index)
  143. return 0;
  144. // Flush the cluster in WebM muxer.
  145. oc->oformat->write_packet(oc, NULL);
  146. buffer_size = avio_close_dyn_buf(oc->pb, &buffer);
  147. ret = get_chunk_filename(s, 0, filename);
  148. if (ret < 0)
  149. goto fail;
  150. ret = avio_open2(&pb, filename, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
  151. if (ret < 0)
  152. goto fail;
  153. avio_write(pb, buffer, buffer_size);
  154. ret = avio_close(pb);
  155. if (ret < 0)
  156. goto fail;
  157. oc->pb = NULL;
  158. fail:
  159. av_free(buffer);
  160. return (ret < 0) ? ret : 0;
  161. }
  162. static int webm_chunk_write_packet(AVFormatContext *s, AVPacket *pkt)
  163. {
  164. WebMChunkContext *wc = s->priv_data;
  165. AVFormatContext *oc = wc->avf;
  166. AVStream *st = s->streams[pkt->stream_index];
  167. int ret;
  168. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  169. wc->duration_written += av_rescale_q(pkt->pts - wc->prev_pts,
  170. st->time_base,
  171. (AVRational) {1, 1000});
  172. wc->prev_pts = pkt->pts;
  173. }
  174. // For video, a new chunk is started only on key frames. For audio, a new
  175. // chunk is started based on chunk_duration.
  176. if ((st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  177. (pkt->flags & AV_PKT_FLAG_KEY)) ||
  178. (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
  179. (pkt->pts == 0 || wc->duration_written >= wc->chunk_duration))) {
  180. wc->duration_written = 0;
  181. if ((ret = chunk_end(s)) < 0 || (ret = chunk_start(s)) < 0) {
  182. goto fail;
  183. }
  184. }
  185. ret = oc->oformat->write_packet(oc, pkt);
  186. if (ret < 0)
  187. goto fail;
  188. fail:
  189. if (ret < 0) {
  190. oc->streams = NULL;
  191. oc->nb_streams = 0;
  192. avformat_free_context(oc);
  193. }
  194. return ret;
  195. }
  196. static int webm_chunk_write_trailer(AVFormatContext *s)
  197. {
  198. WebMChunkContext *wc = s->priv_data;
  199. AVFormatContext *oc = wc->avf;
  200. oc->oformat->write_trailer(oc);
  201. chunk_end(s);
  202. oc->streams = NULL;
  203. oc->nb_streams = 0;
  204. avformat_free_context(oc);
  205. return 0;
  206. }
  207. #define OFFSET(x) offsetof(WebMChunkContext, x)
  208. static const AVOption options[] = {
  209. { "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 },
  210. { "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 },
  211. { "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 },
  212. { NULL },
  213. };
  214. #if CONFIG_WEBM_CHUNK_MUXER
  215. static const AVClass webm_chunk_class = {
  216. .class_name = "WebM Chunk Muxer",
  217. .item_name = av_default_item_name,
  218. .option = options,
  219. .version = LIBAVUTIL_VERSION_INT,
  220. };
  221. AVOutputFormat ff_webm_chunk_muxer = {
  222. .name = "webm_chunk",
  223. .long_name = NULL_IF_CONFIG_SMALL("WebM Chunk Muxer"),
  224. .mime_type = "video/webm",
  225. .extensions = "chk",
  226. .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER | AVFMT_NEEDNUMBER |
  227. AVFMT_TS_NONSTRICT | AVFMT_ALLOW_FLUSH,
  228. .priv_data_size = sizeof(WebMChunkContext),
  229. .write_header = webm_chunk_write_header,
  230. .write_packet = webm_chunk_write_packet,
  231. .write_trailer = webm_chunk_write_trailer,
  232. .priv_class = &webm_chunk_class,
  233. };
  234. #endif