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.

272 lines
8.1KB

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