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.

295 lines
9.0KB

  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. if (!filename) {
  78. return AVERROR(EINVAL);
  79. }
  80. if (is_header) {
  81. int len;
  82. if (!wc->header_filename) {
  83. av_log(s, AV_LOG_ERROR, "No header filename provided\n");
  84. return AVERROR(EINVAL);
  85. }
  86. len = av_strlcpy(filename, wc->header_filename, MAX_FILENAME_SIZE);
  87. if (len >= MAX_FILENAME_SIZE) {
  88. av_log(s, AV_LOG_ERROR, "Header filename too long\n");
  89. return AVERROR(EINVAL);
  90. }
  91. } else {
  92. if (av_get_frame_filename(filename, MAX_FILENAME_SIZE,
  93. s->url, wc->chunk_index - 1) < 0) {
  94. av_log(s, AV_LOG_ERROR, "Invalid chunk filename template '%s'\n", s->url);
  95. return AVERROR(EINVAL);
  96. }
  97. }
  98. return 0;
  99. }
  100. static int webm_chunk_write_header(AVFormatContext *s)
  101. {
  102. WebMChunkContext *wc = s->priv_data;
  103. AVFormatContext *oc = NULL;
  104. int ret;
  105. int i;
  106. AVDictionary *options = NULL;
  107. char oc_filename[MAX_FILENAME_SIZE];
  108. char *oc_url;
  109. // DASH Streams can only have either one track per file.
  110. if (s->nb_streams != 1) { return AVERROR_INVALIDDATA; }
  111. wc->chunk_index = wc->chunk_start_index;
  112. wc->oformat = av_guess_format("webm", s->url, "video/webm");
  113. if (!wc->oformat)
  114. return AVERROR_MUXER_NOT_FOUND;
  115. wc->prev_pts = AV_NOPTS_VALUE;
  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. ff_format_io_close(s, &oc->pb);
  136. if (ret < 0)
  137. return ret;
  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, int flush)
  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 (!oc->pb)
  166. return 0;
  167. if (flush)
  168. // Flush the cluster in WebM muxer.
  169. oc->oformat->write_packet(oc, NULL);
  170. buffer_size = avio_close_dyn_buf(oc->pb, &buffer);
  171. oc->pb = NULL;
  172. ret = get_chunk_filename(s, 0, filename);
  173. if (ret < 0)
  174. goto fail;
  175. if (wc->http_method)
  176. av_dict_set(&options, "method", wc->http_method, 0);
  177. ret = s->io_open(s, &pb, filename, AVIO_FLAG_WRITE, &options);
  178. if (ret < 0)
  179. goto fail;
  180. avio_write(pb, buffer, buffer_size);
  181. ff_format_io_close(s, &pb);
  182. fail:
  183. av_dict_free(&options);
  184. av_free(buffer);
  185. return (ret < 0) ? ret : 0;
  186. }
  187. static int webm_chunk_write_packet(AVFormatContext *s, AVPacket *pkt)
  188. {
  189. WebMChunkContext *wc = s->priv_data;
  190. AVFormatContext *oc = wc->avf;
  191. AVStream *st = s->streams[pkt->stream_index];
  192. int ret;
  193. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  194. if (wc->prev_pts != AV_NOPTS_VALUE)
  195. wc->duration_written += av_rescale_q(pkt->pts - wc->prev_pts,
  196. st->time_base,
  197. (AVRational) {1, 1000});
  198. wc->prev_pts = pkt->pts;
  199. }
  200. // For video, a new chunk is started only on key frames. For audio, a new
  201. // chunk is started based on chunk_duration. Also, a new chunk is started
  202. // unconditionally if there is no currently open chunk.
  203. if (!oc->pb || (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  204. (pkt->flags & AV_PKT_FLAG_KEY)) ||
  205. (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
  206. wc->duration_written >= wc->chunk_duration)) {
  207. wc->duration_written = 0;
  208. if ((ret = chunk_end(s, 1)) < 0 || (ret = chunk_start(s)) < 0) {
  209. return ret;
  210. }
  211. }
  212. ret = oc->oformat->write_packet(oc, pkt);
  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. int ret;
  220. if (!oc->pb) {
  221. ret = chunk_start(s);
  222. if (ret < 0)
  223. goto fail;
  224. }
  225. oc->oformat->write_trailer(oc);
  226. ret = chunk_end(s, 0);
  227. fail:
  228. oc->streams = NULL;
  229. oc->nb_streams = 0;
  230. avformat_free_context(oc);
  231. return ret;
  232. }
  233. #define OFFSET(x) offsetof(WebMChunkContext, x)
  234. static const AVOption options[] = {
  235. { "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 },
  236. { "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 },
  237. { "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 },
  238. { "method", "set the HTTP method", OFFSET(http_method), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  239. { NULL },
  240. };
  241. #if CONFIG_WEBM_CHUNK_MUXER
  242. static const AVClass webm_chunk_class = {
  243. .class_name = "WebM Chunk Muxer",
  244. .item_name = av_default_item_name,
  245. .option = options,
  246. .version = LIBAVUTIL_VERSION_INT,
  247. };
  248. AVOutputFormat ff_webm_chunk_muxer = {
  249. .name = "webm_chunk",
  250. .long_name = NULL_IF_CONFIG_SMALL("WebM Chunk Muxer"),
  251. .mime_type = "video/webm",
  252. .extensions = "chk",
  253. .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER | AVFMT_NEEDNUMBER |
  254. AVFMT_TS_NONSTRICT,
  255. .priv_data_size = sizeof(WebMChunkContext),
  256. .write_header = webm_chunk_write_header,
  257. .write_packet = webm_chunk_write_packet,
  258. .write_trailer = webm_chunk_write_trailer,
  259. .priv_class = &webm_chunk_class,
  260. };
  261. #endif