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.

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