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.

306 lines
9.6KB

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