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.

287 lines
8.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 "avio_internal.h"
  30. #include "internal.h"
  31. #include "libavutil/avassert.h"
  32. #include "libavutil/log.h"
  33. #include "libavutil/opt.h"
  34. #include "libavutil/avstring.h"
  35. #include "libavutil/parseutils.h"
  36. #include "libavutil/mathematics.h"
  37. #include "libavutil/time.h"
  38. #include "libavutil/time_internal.h"
  39. #include "libavutil/timestamp.h"
  40. #define MAX_FILENAME_SIZE 1024
  41. typedef struct WebMChunkContext {
  42. const AVClass *class;
  43. int chunk_start_index;
  44. char *header_filename;
  45. int chunk_duration;
  46. int chunk_index;
  47. char *http_method;
  48. uint64_t duration_written;
  49. int prev_pts;
  50. AVOutputFormat *oformat;
  51. AVFormatContext *avf;
  52. } WebMChunkContext;
  53. static int chunk_mux_init(AVFormatContext *s)
  54. {
  55. WebMChunkContext *wc = s->priv_data;
  56. AVFormatContext *oc;
  57. int ret;
  58. ret = avformat_alloc_output_context2(&wc->avf, wc->oformat, NULL, NULL);
  59. if (ret < 0)
  60. return ret;
  61. oc = wc->avf;
  62. oc->interrupt_callback = s->interrupt_callback;
  63. oc->max_delay = s->max_delay;
  64. av_dict_copy(&oc->metadata, s->metadata, 0);
  65. *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
  66. av_opt_set_defaults(oc->priv_data);
  67. av_opt_set_int(oc->priv_data, "dash", 1, 0);
  68. av_opt_set_int(oc->priv_data, "cluster_time_limit", wc->chunk_duration, 0);
  69. av_opt_set_int(oc->priv_data, "live", 1, 0);
  70. oc->streams = s->streams;
  71. oc->nb_streams = s->nb_streams;
  72. return 0;
  73. }
  74. static int get_chunk_filename(AVFormatContext *s, int is_header, char *filename)
  75. {
  76. WebMChunkContext *wc = s->priv_data;
  77. AVFormatContext *oc = wc->avf;
  78. if (!filename) {
  79. return AVERROR(EINVAL);
  80. }
  81. if (is_header) {
  82. if (!wc->header_filename) {
  83. av_log(oc, AV_LOG_ERROR, "No header filename provided\n");
  84. return AVERROR(EINVAL);
  85. }
  86. av_strlcpy(filename, wc->header_filename, strlen(wc->header_filename) + 1);
  87. } else {
  88. if (av_get_frame_filename(filename, MAX_FILENAME_SIZE,
  89. s->url, wc->chunk_index - 1) < 0) {
  90. av_log(oc, AV_LOG_ERROR, "Invalid chunk filename template '%s'\n", s->url);
  91. return AVERROR(EINVAL);
  92. }
  93. }
  94. return 0;
  95. }
  96. static int webm_chunk_write_header(AVFormatContext *s)
  97. {
  98. WebMChunkContext *wc = s->priv_data;
  99. AVFormatContext *oc = NULL;
  100. int ret;
  101. int i;
  102. AVDictionary *options = NULL;
  103. char oc_filename[MAX_FILENAME_SIZE];
  104. char *oc_url;
  105. // DASH Streams can only have either one track per file.
  106. if (s->nb_streams != 1) { return AVERROR_INVALIDDATA; }
  107. wc->chunk_index = wc->chunk_start_index;
  108. wc->oformat = av_guess_format("webm", s->url, "video/webm");
  109. if (!wc->oformat)
  110. return AVERROR_MUXER_NOT_FOUND;
  111. ret = chunk_mux_init(s);
  112. if (ret < 0)
  113. return ret;
  114. oc = wc->avf;
  115. ret = get_chunk_filename(s, 1, oc_filename);
  116. if (ret < 0)
  117. return ret;
  118. oc_url = av_strdup(oc_filename);
  119. if (!oc_url)
  120. return AVERROR(ENOMEM);
  121. ff_format_set_url(oc, oc_url);
  122. if (wc->http_method)
  123. av_dict_set(&options, "method", wc->http_method, 0);
  124. ret = s->io_open(s, &oc->pb, oc->url, AVIO_FLAG_WRITE, &options);
  125. av_dict_free(&options);
  126. if (ret < 0)
  127. return ret;
  128. oc->pb->seekable = 0;
  129. ret = oc->oformat->write_header(oc);
  130. if (ret < 0)
  131. return ret;
  132. ff_format_io_close(s, &oc->pb);
  133. for (i = 0; i < s->nb_streams; i++) {
  134. // ms precision is the de-facto standard timescale for mkv files.
  135. avpriv_set_pts_info(s->streams[i], 64, 1, 1000);
  136. }
  137. return 0;
  138. }
  139. static int chunk_start(AVFormatContext *s)
  140. {
  141. WebMChunkContext *wc = s->priv_data;
  142. AVFormatContext *oc = wc->avf;
  143. int ret;
  144. ret = avio_open_dyn_buf(&oc->pb);
  145. if (ret < 0)
  146. return ret;
  147. wc->chunk_index++;
  148. return 0;
  149. }
  150. static int chunk_end(AVFormatContext *s)
  151. {
  152. WebMChunkContext *wc = s->priv_data;
  153. AVFormatContext *oc = wc->avf;
  154. int ret;
  155. int buffer_size;
  156. uint8_t *buffer;
  157. AVIOContext *pb;
  158. char filename[MAX_FILENAME_SIZE];
  159. AVDictionary *options = NULL;
  160. if (wc->chunk_start_index == wc->chunk_index)
  161. return 0;
  162. // Flush the cluster in WebM muxer.
  163. oc->oformat->write_packet(oc, NULL);
  164. buffer_size = avio_close_dyn_buf(oc->pb, &buffer);
  165. ret = get_chunk_filename(s, 0, filename);
  166. if (ret < 0)
  167. goto fail;
  168. if (wc->http_method)
  169. av_dict_set(&options, "method", wc->http_method, 0);
  170. ret = s->io_open(s, &pb, filename, AVIO_FLAG_WRITE, &options);
  171. if (ret < 0)
  172. goto fail;
  173. avio_write(pb, buffer, buffer_size);
  174. ff_format_io_close(s, &pb);
  175. oc->pb = NULL;
  176. fail:
  177. av_dict_free(&options);
  178. av_free(buffer);
  179. return (ret < 0) ? ret : 0;
  180. }
  181. static int webm_chunk_write_packet(AVFormatContext *s, AVPacket *pkt)
  182. {
  183. WebMChunkContext *wc = s->priv_data;
  184. AVFormatContext *oc = wc->avf;
  185. AVStream *st = s->streams[pkt->stream_index];
  186. int ret;
  187. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  188. wc->duration_written += av_rescale_q(pkt->pts - wc->prev_pts,
  189. st->time_base,
  190. (AVRational) {1, 1000});
  191. wc->prev_pts = pkt->pts;
  192. }
  193. // For video, a new chunk is started only on key frames. For audio, a new
  194. // chunk is started based on chunk_duration.
  195. if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  196. (pkt->flags & AV_PKT_FLAG_KEY)) ||
  197. (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
  198. (pkt->pts == 0 || wc->duration_written >= wc->chunk_duration))) {
  199. wc->duration_written = 0;
  200. if ((ret = chunk_end(s)) < 0 || (ret = chunk_start(s)) < 0) {
  201. goto fail;
  202. }
  203. }
  204. ret = oc->oformat->write_packet(oc, pkt);
  205. if (ret < 0)
  206. goto fail;
  207. fail:
  208. if (ret < 0) {
  209. oc->streams = NULL;
  210. oc->nb_streams = 0;
  211. avformat_free_context(oc);
  212. }
  213. return ret;
  214. }
  215. static int webm_chunk_write_trailer(AVFormatContext *s)
  216. {
  217. WebMChunkContext *wc = s->priv_data;
  218. AVFormatContext *oc = wc->avf;
  219. oc->oformat->write_trailer(oc);
  220. chunk_end(s);
  221. oc->streams = NULL;
  222. oc->nb_streams = 0;
  223. avformat_free_context(oc);
  224. return 0;
  225. }
  226. #define OFFSET(x) offsetof(WebMChunkContext, x)
  227. static const AVOption options[] = {
  228. { "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 },
  229. { "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 },
  230. { "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 },
  231. { "method", "set the HTTP method", OFFSET(http_method), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  232. { NULL },
  233. };
  234. #if CONFIG_WEBM_CHUNK_MUXER
  235. static const AVClass webm_chunk_class = {
  236. .class_name = "WebM Chunk Muxer",
  237. .item_name = av_default_item_name,
  238. .option = options,
  239. .version = LIBAVUTIL_VERSION_INT,
  240. };
  241. AVOutputFormat ff_webm_chunk_muxer = {
  242. .name = "webm_chunk",
  243. .long_name = NULL_IF_CONFIG_SMALL("WebM Chunk Muxer"),
  244. .mime_type = "video/webm",
  245. .extensions = "chk",
  246. .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER | AVFMT_NEEDNUMBER |
  247. AVFMT_TS_NONSTRICT,
  248. .priv_data_size = sizeof(WebMChunkContext),
  249. .write_header = webm_chunk_write_header,
  250. .write_packet = webm_chunk_write_packet,
  251. .write_trailer = webm_chunk_write_trailer,
  252. .priv_class = &webm_chunk_class,
  253. };
  254. #endif