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.

317 lines
10KB

  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. AVStream *st, *ost = s->streams[0];
  58. AVDictionary *dict = NULL;
  59. int ret;
  60. ret = avformat_alloc_output_context2(&wc->avf, wc->oformat, NULL, NULL);
  61. if (ret < 0)
  62. return ret;
  63. oc = wc->avf;
  64. oc->interrupt_callback = s->interrupt_callback;
  65. oc->max_delay = s->max_delay;
  66. oc->flags = s->flags;
  67. oc->strict_std_compliance = s->strict_std_compliance;
  68. oc->avoid_negative_ts = s->avoid_negative_ts;
  69. av_dict_copy(&oc->metadata, s->metadata, 0);
  70. if (!(st = avformat_new_stream(oc, NULL)))
  71. return AVERROR(ENOMEM);
  72. if ((ret = avcodec_parameters_copy(st->codecpar, ost->codecpar)) < 0 ||
  73. (ret = av_dict_copy(&st->metadata, ost->metadata, 0)) < 0)
  74. return ret;
  75. st->sample_aspect_ratio = ost->sample_aspect_ratio;
  76. st->disposition = ost->disposition;
  77. avpriv_set_pts_info(st, ost->pts_wrap_bits, ost->time_base.num,
  78. ost->time_base.den);
  79. av_dict_set_int(&dict, "dash", 1, 0);
  80. av_dict_set_int(&dict, "cluster_time_limit", wc->chunk_duration, 0);
  81. av_dict_set_int(&dict, "live", 1, 0);
  82. ret = avformat_init_output(oc, &dict);
  83. av_dict_free(&dict);
  84. if (ret < 0)
  85. return ret;
  86. // Copy the timing info back to the original stream
  87. // so that the timestamps of the packets are directly usable
  88. avpriv_set_pts_info(ost, st->pts_wrap_bits, st->time_base.num,
  89. st->time_base.den);
  90. // This ensures that the timestamps will already be properly shifted
  91. // when the packets arrive here, so we don't need to shift again.
  92. s->avoid_negative_ts = oc->avoid_negative_ts;
  93. s->internal->avoid_negative_ts_use_pts =
  94. oc->internal->avoid_negative_ts_use_pts;
  95. oc->avoid_negative_ts = 0;
  96. return 0;
  97. }
  98. static int get_chunk_filename(AVFormatContext *s, int is_header, char filename[MAX_FILENAME_SIZE])
  99. {
  100. WebMChunkContext *wc = s->priv_data;
  101. if (!filename) {
  102. return AVERROR(EINVAL);
  103. }
  104. if (is_header) {
  105. int len;
  106. if (!wc->header_filename) {
  107. av_log(s, AV_LOG_ERROR, "No header filename provided\n");
  108. return AVERROR(EINVAL);
  109. }
  110. len = av_strlcpy(filename, wc->header_filename, MAX_FILENAME_SIZE);
  111. if (len >= MAX_FILENAME_SIZE) {
  112. av_log(s, AV_LOG_ERROR, "Header filename too long\n");
  113. return AVERROR(EINVAL);
  114. }
  115. } else {
  116. if (av_get_frame_filename(filename, MAX_FILENAME_SIZE,
  117. s->url, wc->chunk_index - 1) < 0) {
  118. av_log(s, AV_LOG_ERROR, "Invalid chunk filename template '%s'\n", s->url);
  119. return AVERROR(EINVAL);
  120. }
  121. }
  122. return 0;
  123. }
  124. static int webm_chunk_write_header(AVFormatContext *s)
  125. {
  126. WebMChunkContext *wc = s->priv_data;
  127. AVFormatContext *oc = NULL;
  128. int ret;
  129. AVDictionary *options = NULL;
  130. char oc_filename[MAX_FILENAME_SIZE];
  131. char *oc_url;
  132. // DASH Streams can only have either one track per file.
  133. if (s->nb_streams != 1) { return AVERROR_INVALIDDATA; }
  134. wc->chunk_index = wc->chunk_start_index;
  135. wc->oformat = av_guess_format("webm", s->url, "video/webm");
  136. if (!wc->oformat)
  137. return AVERROR_MUXER_NOT_FOUND;
  138. wc->prev_pts = AV_NOPTS_VALUE;
  139. ret = chunk_mux_init(s);
  140. if (ret < 0)
  141. return ret;
  142. oc = wc->avf;
  143. ret = get_chunk_filename(s, 1, oc_filename);
  144. if (ret < 0)
  145. return ret;
  146. oc_url = av_strdup(oc_filename);
  147. if (!oc_url)
  148. return AVERROR(ENOMEM);
  149. ff_format_set_url(oc, oc_url);
  150. if (wc->http_method)
  151. av_dict_set(&options, "method", wc->http_method, 0);
  152. ret = s->io_open(s, &oc->pb, oc->url, AVIO_FLAG_WRITE, &options);
  153. av_dict_free(&options);
  154. if (ret < 0)
  155. return ret;
  156. oc->pb->seekable = 0;
  157. ret = avformat_write_header(oc, NULL);
  158. ff_format_io_close(s, &oc->pb);
  159. if (ret < 0)
  160. return ret;
  161. return 0;
  162. }
  163. static int chunk_start(AVFormatContext *s)
  164. {
  165. WebMChunkContext *wc = s->priv_data;
  166. AVFormatContext *oc = wc->avf;
  167. int ret;
  168. ret = avio_open_dyn_buf(&oc->pb);
  169. if (ret < 0)
  170. return ret;
  171. wc->chunk_index++;
  172. return 0;
  173. }
  174. static int chunk_end(AVFormatContext *s, int flush)
  175. {
  176. WebMChunkContext *wc = s->priv_data;
  177. AVFormatContext *oc = wc->avf;
  178. int ret;
  179. int buffer_size;
  180. uint8_t *buffer;
  181. AVIOContext *pb;
  182. char filename[MAX_FILENAME_SIZE];
  183. AVDictionary *options = NULL;
  184. if (!oc->pb)
  185. return 0;
  186. if (flush)
  187. // Flush the cluster in WebM muxer.
  188. av_write_frame(oc, NULL);
  189. buffer_size = avio_close_dyn_buf(oc->pb, &buffer);
  190. oc->pb = NULL;
  191. ret = get_chunk_filename(s, 0, filename);
  192. if (ret < 0)
  193. goto fail;
  194. if (wc->http_method)
  195. av_dict_set(&options, "method", wc->http_method, 0);
  196. ret = s->io_open(s, &pb, filename, AVIO_FLAG_WRITE, &options);
  197. if (ret < 0)
  198. goto fail;
  199. avio_write(pb, buffer, buffer_size);
  200. ff_format_io_close(s, &pb);
  201. fail:
  202. av_dict_free(&options);
  203. av_free(buffer);
  204. return (ret < 0) ? ret : 0;
  205. }
  206. static int webm_chunk_write_packet(AVFormatContext *s, AVPacket *pkt)
  207. {
  208. WebMChunkContext *wc = s->priv_data;
  209. AVFormatContext *oc = wc->avf;
  210. AVStream *st = s->streams[pkt->stream_index];
  211. int ret;
  212. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  213. if (wc->prev_pts != AV_NOPTS_VALUE)
  214. wc->duration_written += av_rescale_q(pkt->pts - wc->prev_pts,
  215. st->time_base,
  216. (AVRational) {1, 1000});
  217. wc->prev_pts = pkt->pts;
  218. }
  219. // For video, a new chunk is started only on key frames. For audio, a new
  220. // chunk is started based on chunk_duration. Also, a new chunk is started
  221. // unconditionally if there is no currently open chunk.
  222. if (!oc->pb || (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  223. (pkt->flags & AV_PKT_FLAG_KEY)) ||
  224. (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
  225. wc->duration_written >= wc->chunk_duration)) {
  226. wc->duration_written = 0;
  227. if ((ret = chunk_end(s, 1)) < 0 || (ret = chunk_start(s)) < 0) {
  228. return ret;
  229. }
  230. }
  231. // We only have one stream, so use the non-interleaving av_write_frame.
  232. return av_write_frame(oc, pkt);
  233. }
  234. static int webm_chunk_write_trailer(AVFormatContext *s)
  235. {
  236. WebMChunkContext *wc = s->priv_data;
  237. AVFormatContext *oc = wc->avf;
  238. int ret;
  239. if (!oc->pb) {
  240. ret = chunk_start(s);
  241. if (ret < 0)
  242. goto fail;
  243. }
  244. av_write_trailer(oc);
  245. ret = chunk_end(s, 0);
  246. fail:
  247. avformat_free_context(oc);
  248. return ret;
  249. }
  250. #define OFFSET(x) offsetof(WebMChunkContext, x)
  251. static const AVOption options[] = {
  252. { "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 },
  253. { "header", "filename of the header where the initialization data will be written", OFFSET(header_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  254. { "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 },
  255. { "method", "set the HTTP method", OFFSET(http_method), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  256. { NULL },
  257. };
  258. #if CONFIG_WEBM_CHUNK_MUXER
  259. static const AVClass webm_chunk_class = {
  260. .class_name = "WebM Chunk Muxer",
  261. .item_name = av_default_item_name,
  262. .option = options,
  263. .version = LIBAVUTIL_VERSION_INT,
  264. };
  265. AVOutputFormat ff_webm_chunk_muxer = {
  266. .name = "webm_chunk",
  267. .long_name = NULL_IF_CONFIG_SMALL("WebM Chunk Muxer"),
  268. .mime_type = "video/webm",
  269. .extensions = "chk",
  270. .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER | AVFMT_NEEDNUMBER |
  271. AVFMT_TS_NONSTRICT,
  272. .priv_data_size = sizeof(WebMChunkContext),
  273. .write_header = webm_chunk_write_header,
  274. .write_packet = webm_chunk_write_packet,
  275. .write_trailer = webm_chunk_write_trailer,
  276. .priv_class = &webm_chunk_class,
  277. };
  278. #endif