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.

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