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.

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