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.

304 lines
9.5KB

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