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.

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