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.

296 lines
9.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 "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. int64_t 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. wc->prev_pts = AV_NOPTS_VALUE;
  117. ret = chunk_mux_init(s);
  118. if (ret < 0)
  119. return ret;
  120. oc = wc->avf;
  121. ret = get_chunk_filename(s, 1, oc_filename);
  122. if (ret < 0)
  123. return ret;
  124. oc_url = av_strdup(oc_filename);
  125. if (!oc_url)
  126. return AVERROR(ENOMEM);
  127. ff_format_set_url(oc, oc_url);
  128. if (wc->http_method)
  129. av_dict_set(&options, "method", wc->http_method, 0);
  130. ret = s->io_open(s, &oc->pb, oc->url, AVIO_FLAG_WRITE, &options);
  131. av_dict_free(&options);
  132. if (ret < 0)
  133. return ret;
  134. oc->pb->seekable = 0;
  135. ret = oc->oformat->write_header(oc);
  136. if (ret < 0)
  137. return ret;
  138. ff_format_io_close(s, &oc->pb);
  139. for (i = 0; i < s->nb_streams; i++) {
  140. // ms precision is the de-facto standard timescale for mkv files.
  141. avpriv_set_pts_info(s->streams[i], 64, 1, 1000);
  142. }
  143. return 0;
  144. }
  145. static int chunk_start(AVFormatContext *s)
  146. {
  147. WebMChunkContext *wc = s->priv_data;
  148. AVFormatContext *oc = wc->avf;
  149. int ret;
  150. ret = avio_open_dyn_buf(&oc->pb);
  151. if (ret < 0)
  152. return ret;
  153. wc->chunk_index++;
  154. return 0;
  155. }
  156. static int chunk_end(AVFormatContext *s, int flush)
  157. {
  158. WebMChunkContext *wc = s->priv_data;
  159. AVFormatContext *oc = wc->avf;
  160. int ret;
  161. int buffer_size;
  162. uint8_t *buffer;
  163. AVIOContext *pb;
  164. char filename[MAX_FILENAME_SIZE];
  165. AVDictionary *options = NULL;
  166. if (!oc->pb)
  167. return 0;
  168. if (flush)
  169. // Flush the cluster in WebM muxer.
  170. oc->oformat->write_packet(oc, NULL);
  171. buffer_size = avio_close_dyn_buf(oc->pb, &buffer);
  172. oc->pb = NULL;
  173. ret = get_chunk_filename(s, 0, filename);
  174. if (ret < 0)
  175. goto fail;
  176. if (wc->http_method)
  177. av_dict_set(&options, "method", wc->http_method, 0);
  178. ret = s->io_open(s, &pb, filename, AVIO_FLAG_WRITE, &options);
  179. if (ret < 0)
  180. goto fail;
  181. avio_write(pb, buffer, buffer_size);
  182. ff_format_io_close(s, &pb);
  183. fail:
  184. av_dict_free(&options);
  185. av_free(buffer);
  186. return (ret < 0) ? ret : 0;
  187. }
  188. static int webm_chunk_write_packet(AVFormatContext *s, AVPacket *pkt)
  189. {
  190. WebMChunkContext *wc = s->priv_data;
  191. AVFormatContext *oc = wc->avf;
  192. AVStream *st = s->streams[pkt->stream_index];
  193. int ret;
  194. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  195. if (wc->prev_pts != AV_NOPTS_VALUE)
  196. wc->duration_written += av_rescale_q(pkt->pts - wc->prev_pts,
  197. st->time_base,
  198. (AVRational) {1, 1000});
  199. wc->prev_pts = pkt->pts;
  200. }
  201. // For video, a new chunk is started only on key frames. For audio, a new
  202. // chunk is started based on chunk_duration. Also, a new chunk is started
  203. // unconditionally if there is no currently open chunk.
  204. if (!oc->pb || (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  205. (pkt->flags & AV_PKT_FLAG_KEY)) ||
  206. (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
  207. wc->duration_written >= wc->chunk_duration)) {
  208. wc->duration_written = 0;
  209. if ((ret = chunk_end(s, 1)) < 0 || (ret = chunk_start(s)) < 0) {
  210. return ret;
  211. }
  212. }
  213. ret = oc->oformat->write_packet(oc, pkt);
  214. return ret;
  215. }
  216. static int webm_chunk_write_trailer(AVFormatContext *s)
  217. {
  218. WebMChunkContext *wc = s->priv_data;
  219. AVFormatContext *oc = wc->avf;
  220. int ret;
  221. if (!oc->pb) {
  222. ret = chunk_start(s);
  223. if (ret < 0)
  224. goto fail;
  225. }
  226. oc->oformat->write_trailer(oc);
  227. ret = chunk_end(s, 0);
  228. fail:
  229. oc->streams = NULL;
  230. oc->nb_streams = 0;
  231. avformat_free_context(oc);
  232. return ret;
  233. }
  234. #define OFFSET(x) offsetof(WebMChunkContext, x)
  235. static const AVOption options[] = {
  236. { "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 },
  237. { "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 },
  238. { "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 },
  239. { "method", "set the HTTP method", OFFSET(http_method), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  240. { NULL },
  241. };
  242. #if CONFIG_WEBM_CHUNK_MUXER
  243. static const AVClass webm_chunk_class = {
  244. .class_name = "WebM Chunk Muxer",
  245. .item_name = av_default_item_name,
  246. .option = options,
  247. .version = LIBAVUTIL_VERSION_INT,
  248. };
  249. AVOutputFormat ff_webm_chunk_muxer = {
  250. .name = "webm_chunk",
  251. .long_name = NULL_IF_CONFIG_SMALL("WebM Chunk Muxer"),
  252. .mime_type = "video/webm",
  253. .extensions = "chk",
  254. .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER | AVFMT_NEEDNUMBER |
  255. AVFMT_TS_NONSTRICT,
  256. .priv_data_size = sizeof(WebMChunkContext),
  257. .write_header = webm_chunk_write_header,
  258. .write_packet = webm_chunk_write_packet,
  259. .write_trailer = webm_chunk_write_trailer,
  260. .priv_class = &webm_chunk_class,
  261. };
  262. #endif