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.

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