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.

397 lines
12KB

  1. /*
  2. * Generic segmenter
  3. * Copyright (c) 2011, Luca Barbato
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <float.h>
  22. #include "avformat.h"
  23. #include "internal.h"
  24. #include "libavutil/log.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/parseutils.h"
  28. #include "libavutil/mathematics.h"
  29. typedef struct {
  30. const AVClass *class; /**< Class for private options. */
  31. int number;
  32. AVOutputFormat *oformat;
  33. AVFormatContext *avf;
  34. char *format; /**< Set by a private option. */
  35. char *list; /**< Set by a private option. */
  36. char *entry_prefix; /**< Set by a private option. */
  37. int list_type; /**< Set by a private option. */
  38. float time; /**< Set by a private option. */
  39. int size; /**< Set by a private option. */
  40. int wrap; /**< Set by a private option. */
  41. int individual_header_trailer; /**< Set by a private option. */
  42. int write_header_trailer; /**< Set by a private option. */
  43. int64_t offset_time;
  44. int64_t recording_time;
  45. int has_video;
  46. AVIOContext *pb;
  47. } SegmentContext;
  48. enum {
  49. LIST_FLAT,
  50. LIST_HLS
  51. };
  52. static int segment_mux_init(AVFormatContext *s)
  53. {
  54. SegmentContext *seg = s->priv_data;
  55. AVFormatContext *oc;
  56. int i;
  57. seg->avf = oc = avformat_alloc_context();
  58. if (!oc)
  59. return AVERROR(ENOMEM);
  60. oc->oformat = seg->oformat;
  61. oc->interrupt_callback = s->interrupt_callback;
  62. for (i = 0; i < s->nb_streams; i++) {
  63. AVStream *st;
  64. if (!(st = avformat_new_stream(oc, NULL)))
  65. return AVERROR(ENOMEM);
  66. avcodec_copy_context(st->codec, s->streams[i]->codec);
  67. st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
  68. }
  69. return 0;
  70. }
  71. static int segment_hls_window(AVFormatContext *s, int last)
  72. {
  73. SegmentContext *seg = s->priv_data;
  74. int i, ret = 0;
  75. char buf[1024];
  76. if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
  77. &s->interrupt_callback, NULL)) < 0)
  78. goto fail;
  79. avio_printf(seg->pb, "#EXTM3U\n");
  80. avio_printf(seg->pb, "#EXT-X-VERSION:3\n");
  81. avio_printf(seg->pb, "#EXT-X-TARGETDURATION:%d\n", (int)seg->time);
  82. avio_printf(seg->pb, "#EXT-X-MEDIA-SEQUENCE:%d\n",
  83. FFMAX(0, seg->number - seg->size));
  84. av_log(s, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%d\n",
  85. FFMAX(0, seg->number - seg->size));
  86. for (i = FFMAX(0, seg->number - seg->size);
  87. i < seg->number; i++) {
  88. avio_printf(seg->pb, "#EXTINF:%d,\n", (int)seg->time);
  89. if (seg->entry_prefix != NULL) {
  90. avio_printf(seg->pb, "%s", seg->entry_prefix);
  91. }
  92. av_get_frame_filename(buf, sizeof(buf), s->filename, i);
  93. avio_printf(seg->pb, "%s\n", buf);
  94. }
  95. if (last)
  96. avio_printf(seg->pb, "#EXT-X-ENDLIST\n");
  97. fail:
  98. avio_closep(&seg->pb);
  99. return ret;
  100. }
  101. static int segment_start(AVFormatContext *s, int write_header)
  102. {
  103. SegmentContext *c = s->priv_data;
  104. AVFormatContext *oc = c->avf;
  105. int err = 0;
  106. if (write_header) {
  107. avformat_free_context(oc);
  108. c->avf = NULL;
  109. if ((err = segment_mux_init(s)) < 0)
  110. return err;
  111. oc = c->avf;
  112. }
  113. if (c->wrap)
  114. c->number %= c->wrap;
  115. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  116. s->filename, c->number++) < 0)
  117. return AVERROR(EINVAL);
  118. if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  119. &s->interrupt_callback, NULL)) < 0)
  120. return err;
  121. if (oc->oformat->priv_class && oc->priv_data)
  122. av_opt_set(oc->priv_data, "resend_headers", "1", 0); /* mpegts specific */
  123. if (write_header) {
  124. if ((err = avformat_write_header(oc, NULL)) < 0)
  125. return err;
  126. }
  127. return 0;
  128. }
  129. static int segment_end(AVFormatContext *oc, int write_trailer)
  130. {
  131. int ret = 0;
  132. av_write_frame(oc, NULL); /* Flush any buffered data (fragmented mp4) */
  133. if (write_trailer)
  134. av_write_trailer(oc);
  135. avio_close(oc->pb);
  136. return ret;
  137. }
  138. static int open_null_ctx(AVIOContext **ctx)
  139. {
  140. int buf_size = 32768;
  141. uint8_t *buf = av_malloc(buf_size);
  142. if (!buf)
  143. return AVERROR(ENOMEM);
  144. *ctx = avio_alloc_context(buf, buf_size, AVIO_FLAG_WRITE, NULL, NULL, NULL, NULL);
  145. if (!*ctx) {
  146. av_free(buf);
  147. return AVERROR(ENOMEM);
  148. }
  149. return 0;
  150. }
  151. static void close_null_ctx(AVIOContext *pb)
  152. {
  153. av_free(pb->buffer);
  154. av_free(pb);
  155. }
  156. static int seg_write_header(AVFormatContext *s)
  157. {
  158. SegmentContext *seg = s->priv_data;
  159. AVFormatContext *oc = NULL;
  160. int ret, i;
  161. seg->number = 0;
  162. seg->offset_time = 0;
  163. seg->recording_time = seg->time * 1000000;
  164. if (!seg->write_header_trailer)
  165. seg->individual_header_trailer = 0;
  166. if (seg->list && seg->list_type != LIST_HLS)
  167. if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
  168. &s->interrupt_callback, NULL)) < 0)
  169. goto fail;
  170. for (i = 0; i < s->nb_streams; i++)
  171. seg->has_video +=
  172. (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
  173. if (seg->has_video > 1)
  174. av_log(s, AV_LOG_WARNING,
  175. "More than a single video stream present, "
  176. "expect issues decoding it.\n");
  177. seg->oformat = av_guess_format(seg->format, s->filename, NULL);
  178. if (!seg->oformat) {
  179. ret = AVERROR_MUXER_NOT_FOUND;
  180. goto fail;
  181. }
  182. if (seg->oformat->flags & AVFMT_NOFILE) {
  183. av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
  184. seg->oformat->name);
  185. ret = AVERROR(EINVAL);
  186. goto fail;
  187. }
  188. if ((ret = segment_mux_init(s)) < 0)
  189. goto fail;
  190. oc = seg->avf;
  191. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  192. s->filename, seg->number++) < 0) {
  193. ret = AVERROR(EINVAL);
  194. goto fail;
  195. }
  196. if (seg->write_header_trailer) {
  197. if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  198. &s->interrupt_callback, NULL)) < 0)
  199. goto fail;
  200. } else {
  201. if ((ret = open_null_ctx(&oc->pb)) < 0)
  202. goto fail;
  203. }
  204. if ((ret = avformat_write_header(oc, NULL)) < 0) {
  205. avio_close(oc->pb);
  206. goto fail;
  207. }
  208. if (!seg->write_header_trailer) {
  209. close_null_ctx(oc->pb);
  210. if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  211. &s->interrupt_callback, NULL)) < 0)
  212. goto fail;
  213. }
  214. if (seg->list) {
  215. if (seg->list_type == LIST_HLS) {
  216. if ((ret = segment_hls_window(s, 0)) < 0)
  217. goto fail;
  218. } else {
  219. avio_printf(seg->pb, "%s\n", oc->filename);
  220. avio_flush(seg->pb);
  221. }
  222. }
  223. fail:
  224. if (ret) {
  225. if (seg->list)
  226. avio_close(seg->pb);
  227. if (seg->avf)
  228. avformat_free_context(seg->avf);
  229. }
  230. return ret;
  231. }
  232. static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
  233. {
  234. SegmentContext *seg = s->priv_data;
  235. AVFormatContext *oc = seg->avf;
  236. AVStream *st = s->streams[pkt->stream_index];
  237. int64_t end_pts = seg->recording_time * seg->number;
  238. int ret, can_split = 1;
  239. if (seg->has_video) {
  240. can_split = st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  241. pkt->flags & AV_PKT_FLAG_KEY;
  242. }
  243. if (can_split && av_compare_ts(pkt->pts, st->time_base, end_pts,
  244. AV_TIME_BASE_Q) >= 0) {
  245. av_log(s, AV_LOG_DEBUG, "Next segment starts at %d %"PRId64"\n",
  246. pkt->stream_index, pkt->pts);
  247. ret = segment_end(oc, seg->individual_header_trailer);
  248. if (!ret)
  249. ret = segment_start(s, seg->individual_header_trailer);
  250. if (ret)
  251. goto fail;
  252. oc = seg->avf;
  253. if (seg->list) {
  254. if (seg->list_type == LIST_HLS) {
  255. if ((ret = segment_hls_window(s, 0)) < 0)
  256. goto fail;
  257. } else {
  258. avio_printf(seg->pb, "%s\n", oc->filename);
  259. avio_flush(seg->pb);
  260. if (seg->size && !(seg->number % seg->size)) {
  261. avio_closep(&seg->pb);
  262. if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
  263. &s->interrupt_callback, NULL)) < 0)
  264. goto fail;
  265. }
  266. }
  267. }
  268. }
  269. ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
  270. fail:
  271. if (ret < 0) {
  272. if (seg->list)
  273. avio_close(seg->pb);
  274. avformat_free_context(oc);
  275. }
  276. return ret;
  277. }
  278. static int seg_write_trailer(struct AVFormatContext *s)
  279. {
  280. SegmentContext *seg = s->priv_data;
  281. AVFormatContext *oc = seg->avf;
  282. int ret;
  283. if (!seg->write_header_trailer) {
  284. if ((ret = segment_end(oc, 0)) < 0)
  285. goto fail;
  286. open_null_ctx(&oc->pb);
  287. ret = av_write_trailer(oc);
  288. close_null_ctx(oc->pb);
  289. } else {
  290. ret = segment_end(oc, 1);
  291. }
  292. if (ret < 0)
  293. goto fail;
  294. if (seg->list && seg->list_type == LIST_HLS) {
  295. if ((ret = segment_hls_window(s, 1) < 0))
  296. goto fail;
  297. }
  298. fail:
  299. avio_close(seg->pb);
  300. avformat_free_context(oc);
  301. return ret;
  302. }
  303. #define OFFSET(x) offsetof(SegmentContext, x)
  304. #define E AV_OPT_FLAG_ENCODING_PARAM
  305. static const AVOption options[] = {
  306. { "segment_format", "container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  307. { "segment_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E },
  308. { "segment_list", "output the segment list", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  309. { "segment_list_size", "maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E },
  310. { "segment_list_type", "segment list format", OFFSET(list_type), AV_OPT_TYPE_INT, {.i64 = LIST_FLAT}, 0, 2, E, "list_type" },
  311. { "flat", "plain list (default)", 0, AV_OPT_TYPE_CONST, {.i64 = LIST_FLAT}, 0, 0, E, "list_type" },
  312. { "hls", "Apple HTTP Live Streaming compatible", 0, AV_OPT_TYPE_CONST, {.i64 = LIST_HLS}, 0, 0, E, "list_type" },
  313. { "segment_wrap", "number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  314. { "segment_list_entry_prefix", "base url prefix for segments", OFFSET(entry_prefix), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  315. { "individual_header_trailer", "write header/trailer to each segment", OFFSET(individual_header_trailer), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
  316. { "write_header_trailer", "write a header to the first segment and a trailer to the last one", OFFSET(write_header_trailer), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
  317. { NULL },
  318. };
  319. static const AVClass seg_class = {
  320. .class_name = "segment muxer",
  321. .item_name = av_default_item_name,
  322. .option = options,
  323. .version = LIBAVUTIL_VERSION_INT,
  324. };
  325. AVOutputFormat ff_segment_muxer = {
  326. .name = "segment",
  327. .long_name = NULL_IF_CONFIG_SMALL("segment"),
  328. .priv_data_size = sizeof(SegmentContext),
  329. .flags = AVFMT_NOFILE,
  330. .write_header = seg_write_header,
  331. .write_packet = seg_write_packet,
  332. .write_trailer = seg_write_trailer,
  333. .priv_class = &seg_class,
  334. };