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.

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