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.

398 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. st->time_base = s->streams[i]->time_base;
  69. }
  70. return 0;
  71. }
  72. static int segment_hls_window(AVFormatContext *s, int last)
  73. {
  74. SegmentContext *seg = s->priv_data;
  75. int i, ret = 0;
  76. char buf[1024];
  77. if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
  78. &s->interrupt_callback, NULL)) < 0)
  79. goto fail;
  80. avio_printf(seg->pb, "#EXTM3U\n");
  81. avio_printf(seg->pb, "#EXT-X-VERSION:3\n");
  82. avio_printf(seg->pb, "#EXT-X-TARGETDURATION:%d\n", (int)seg->time);
  83. avio_printf(seg->pb, "#EXT-X-MEDIA-SEQUENCE:%d\n",
  84. FFMAX(0, seg->number - seg->size));
  85. av_log(s, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%d\n",
  86. FFMAX(0, seg->number - seg->size));
  87. for (i = FFMAX(0, seg->number - seg->size);
  88. i < seg->number; i++) {
  89. avio_printf(seg->pb, "#EXTINF:%d,\n", (int)seg->time);
  90. if (seg->entry_prefix) {
  91. avio_printf(seg->pb, "%s", seg->entry_prefix);
  92. }
  93. av_get_frame_filename(buf, sizeof(buf), s->filename, i);
  94. avio_printf(seg->pb, "%s\n", buf);
  95. }
  96. if (last)
  97. avio_printf(seg->pb, "#EXT-X-ENDLIST\n");
  98. fail:
  99. avio_closep(&seg->pb);
  100. return ret;
  101. }
  102. static int segment_start(AVFormatContext *s, int write_header)
  103. {
  104. SegmentContext *c = s->priv_data;
  105. AVFormatContext *oc = c->avf;
  106. int err = 0;
  107. if (write_header) {
  108. avformat_free_context(oc);
  109. c->avf = NULL;
  110. if ((err = segment_mux_init(s)) < 0)
  111. return err;
  112. oc = c->avf;
  113. }
  114. if (c->wrap)
  115. c->number %= c->wrap;
  116. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  117. s->filename, c->number++) < 0)
  118. return AVERROR(EINVAL);
  119. if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  120. &s->interrupt_callback, NULL)) < 0)
  121. return err;
  122. if (oc->oformat->priv_class && oc->priv_data)
  123. av_opt_set(oc->priv_data, "resend_headers", "1", 0); /* mpegts specific */
  124. if (write_header) {
  125. if ((err = avformat_write_header(oc, NULL)) < 0)
  126. return err;
  127. }
  128. return 0;
  129. }
  130. static int segment_end(AVFormatContext *oc, int write_trailer)
  131. {
  132. int ret = 0;
  133. av_write_frame(oc, NULL); /* Flush any buffered data (fragmented mp4) */
  134. if (write_trailer)
  135. av_write_trailer(oc);
  136. avio_close(oc->pb);
  137. return ret;
  138. }
  139. static int open_null_ctx(AVIOContext **ctx)
  140. {
  141. int buf_size = 32768;
  142. uint8_t *buf = av_malloc(buf_size);
  143. if (!buf)
  144. return AVERROR(ENOMEM);
  145. *ctx = avio_alloc_context(buf, buf_size, AVIO_FLAG_WRITE, NULL, NULL, NULL, NULL);
  146. if (!*ctx) {
  147. av_free(buf);
  148. return AVERROR(ENOMEM);
  149. }
  150. return 0;
  151. }
  152. static void close_null_ctx(AVIOContext *pb)
  153. {
  154. av_free(pb->buffer);
  155. av_free(pb);
  156. }
  157. static int seg_write_header(AVFormatContext *s)
  158. {
  159. SegmentContext *seg = s->priv_data;
  160. AVFormatContext *oc = NULL;
  161. int ret, i;
  162. seg->number = 0;
  163. seg->offset_time = 0;
  164. seg->recording_time = seg->time * 1000000;
  165. if (!seg->write_header_trailer)
  166. seg->individual_header_trailer = 0;
  167. if (seg->list && seg->list_type != LIST_HLS)
  168. if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
  169. &s->interrupt_callback, NULL)) < 0)
  170. goto fail;
  171. for (i = 0; i < s->nb_streams; i++)
  172. seg->has_video +=
  173. (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
  174. if (seg->has_video > 1)
  175. av_log(s, AV_LOG_WARNING,
  176. "More than a single video stream present, "
  177. "expect issues decoding it.\n");
  178. seg->oformat = av_guess_format(seg->format, s->filename, NULL);
  179. if (!seg->oformat) {
  180. ret = AVERROR_MUXER_NOT_FOUND;
  181. goto fail;
  182. }
  183. if (seg->oformat->flags & AVFMT_NOFILE) {
  184. av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
  185. seg->oformat->name);
  186. ret = AVERROR(EINVAL);
  187. goto fail;
  188. }
  189. if ((ret = segment_mux_init(s)) < 0)
  190. goto fail;
  191. oc = seg->avf;
  192. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  193. s->filename, seg->number++) < 0) {
  194. ret = AVERROR(EINVAL);
  195. goto fail;
  196. }
  197. if (seg->write_header_trailer) {
  198. if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  199. &s->interrupt_callback, NULL)) < 0)
  200. goto fail;
  201. } else {
  202. if ((ret = open_null_ctx(&oc->pb)) < 0)
  203. goto fail;
  204. }
  205. if ((ret = avformat_write_header(oc, NULL)) < 0) {
  206. avio_close(oc->pb);
  207. goto fail;
  208. }
  209. if (!seg->write_header_trailer) {
  210. close_null_ctx(oc->pb);
  211. if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  212. &s->interrupt_callback, NULL)) < 0)
  213. goto fail;
  214. }
  215. if (seg->list) {
  216. if (seg->list_type == LIST_HLS) {
  217. if ((ret = segment_hls_window(s, 0)) < 0)
  218. goto fail;
  219. } else {
  220. avio_printf(seg->pb, "%s\n", oc->filename);
  221. avio_flush(seg->pb);
  222. }
  223. }
  224. fail:
  225. if (ret) {
  226. if (seg->list)
  227. avio_close(seg->pb);
  228. if (seg->avf)
  229. avformat_free_context(seg->avf);
  230. }
  231. return ret;
  232. }
  233. static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
  234. {
  235. SegmentContext *seg = s->priv_data;
  236. AVFormatContext *oc = seg->avf;
  237. AVStream *st = s->streams[pkt->stream_index];
  238. int64_t end_pts = seg->recording_time * seg->number;
  239. int ret, can_split = 1;
  240. if (seg->has_video) {
  241. can_split = st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  242. pkt->flags & AV_PKT_FLAG_KEY;
  243. }
  244. if (can_split && av_compare_ts(pkt->pts, st->time_base, end_pts,
  245. AV_TIME_BASE_Q) >= 0) {
  246. av_log(s, AV_LOG_DEBUG, "Next segment starts at %d %"PRId64"\n",
  247. pkt->stream_index, pkt->pts);
  248. ret = segment_end(oc, seg->individual_header_trailer);
  249. if (!ret)
  250. ret = segment_start(s, seg->individual_header_trailer);
  251. if (ret)
  252. goto fail;
  253. oc = seg->avf;
  254. if (seg->list) {
  255. if (seg->list_type == LIST_HLS) {
  256. if ((ret = segment_hls_window(s, 0)) < 0)
  257. goto fail;
  258. } else {
  259. avio_printf(seg->pb, "%s\n", oc->filename);
  260. avio_flush(seg->pb);
  261. if (seg->size && !(seg->number % seg->size)) {
  262. avio_closep(&seg->pb);
  263. if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
  264. &s->interrupt_callback, NULL)) < 0)
  265. goto fail;
  266. }
  267. }
  268. }
  269. }
  270. ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
  271. fail:
  272. if (ret < 0) {
  273. if (seg->list)
  274. avio_close(seg->pb);
  275. avformat_free_context(oc);
  276. }
  277. return ret;
  278. }
  279. static int seg_write_trailer(struct AVFormatContext *s)
  280. {
  281. SegmentContext *seg = s->priv_data;
  282. AVFormatContext *oc = seg->avf;
  283. int ret;
  284. if (!seg->write_header_trailer) {
  285. if ((ret = segment_end(oc, 0)) < 0)
  286. goto fail;
  287. open_null_ctx(&oc->pb);
  288. ret = av_write_trailer(oc);
  289. close_null_ctx(oc->pb);
  290. } else {
  291. ret = segment_end(oc, 1);
  292. }
  293. if (ret < 0)
  294. goto fail;
  295. if (seg->list && seg->list_type == LIST_HLS) {
  296. if ((ret = segment_hls_window(s, 1) < 0))
  297. goto fail;
  298. }
  299. fail:
  300. avio_close(seg->pb);
  301. avformat_free_context(oc);
  302. return ret;
  303. }
  304. #define OFFSET(x) offsetof(SegmentContext, x)
  305. #define E AV_OPT_FLAG_ENCODING_PARAM
  306. static const AVOption options[] = {
  307. { "segment_format", "container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  308. { "segment_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E },
  309. { "segment_list", "output the segment list", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  310. { "segment_list_size", "maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E },
  311. { "segment_list_type", "segment list format", OFFSET(list_type), AV_OPT_TYPE_INT, {.i64 = LIST_FLAT}, 0, 2, E, "list_type" },
  312. { "flat", "plain list (default)", 0, AV_OPT_TYPE_CONST, {.i64 = LIST_FLAT}, 0, 0, E, "list_type" },
  313. { "hls", "Apple HTTP Live Streaming compatible", 0, AV_OPT_TYPE_CONST, {.i64 = LIST_HLS}, 0, 0, E, "list_type" },
  314. { "segment_wrap", "number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  315. { "segment_list_entry_prefix", "base url prefix for segments", OFFSET(entry_prefix), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  316. { "individual_header_trailer", "write header/trailer to each segment", OFFSET(individual_header_trailer), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
  317. { "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 },
  318. { NULL },
  319. };
  320. static const AVClass seg_class = {
  321. .class_name = "segment muxer",
  322. .item_name = av_default_item_name,
  323. .option = options,
  324. .version = LIBAVUTIL_VERSION_INT,
  325. };
  326. AVOutputFormat ff_segment_muxer = {
  327. .name = "segment",
  328. .long_name = NULL_IF_CONFIG_SMALL("segment"),
  329. .priv_data_size = sizeof(SegmentContext),
  330. .flags = AVFMT_NOFILE,
  331. .write_header = seg_write_header,
  332. .write_packet = seg_write_packet,
  333. .write_trailer = seg_write_trailer,
  334. .priv_class = &seg_class,
  335. };