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.

439 lines
13KB

  1. /*
  2. * Apple HTTP Live Streaming segmenter
  3. * Copyright (c) 2012, Luca Barbato
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 <stdint.h>
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/mathematics.h"
  25. #include "libavutil/parseutils.h"
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/log.h"
  29. #include "avformat.h"
  30. #include "internal.h"
  31. typedef struct HLSSegment {
  32. char filename[1024];
  33. double duration; /* in seconds */
  34. int64_t pos;
  35. int64_t size;
  36. struct HLSSegment *next;
  37. } HLSSegment;
  38. typedef enum HLSFlags {
  39. // Generate a single media file and use byte ranges in the playlist.
  40. HLS_SINGLE_FILE = (1 << 0),
  41. } HLSFlags;
  42. typedef struct HLSContext {
  43. const AVClass *class; // Class for private options.
  44. unsigned number;
  45. int64_t sequence;
  46. int64_t start_sequence;
  47. AVOutputFormat *oformat;
  48. AVFormatContext *avf;
  49. float time; // Set by a private option.
  50. int max_nb_segments; // Set by a private option.
  51. int wrap; // Set by a private option.
  52. uint32_t flags; // enum HLSFlags
  53. int allowcache;
  54. int64_t recording_time;
  55. int has_video;
  56. int64_t start_pts;
  57. int64_t end_pts;
  58. double duration; // last segment duration computed so far, in seconds
  59. int64_t start_pos; // last segment starting position
  60. int64_t size; // last segment size
  61. int nb_entries;
  62. HLSSegment *segments;
  63. HLSSegment *last_segment;
  64. char *basename;
  65. char *baseurl;
  66. char *format_options_str;
  67. AVDictionary *format_options;
  68. AVIOContext *pb;
  69. } HLSContext;
  70. static int hls_mux_init(AVFormatContext *s)
  71. {
  72. HLSContext *hls = s->priv_data;
  73. AVFormatContext *oc;
  74. int i, ret;
  75. ret = avformat_alloc_output_context2(&hls->avf, hls->oformat, NULL, NULL);
  76. if (ret < 0)
  77. return ret;
  78. oc = hls->avf;
  79. oc->oformat = hls->oformat;
  80. oc->interrupt_callback = s->interrupt_callback;
  81. oc->max_delay = s->max_delay;
  82. av_dict_copy(&oc->metadata, s->metadata, 0);
  83. for (i = 0; i < s->nb_streams; i++) {
  84. AVStream *st;
  85. if (!(st = avformat_new_stream(oc, NULL)))
  86. return AVERROR(ENOMEM);
  87. avcodec_copy_context(st->codec, s->streams[i]->codec);
  88. st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
  89. st->time_base = s->streams[i]->time_base;
  90. }
  91. hls->start_pos = 0;
  92. return 0;
  93. }
  94. /* Create a new segment and append it to the segment list */
  95. static int hls_append_segment(HLSContext *hls, double duration, int64_t pos,
  96. int64_t size)
  97. {
  98. HLSSegment *en = av_malloc(sizeof(*en));
  99. if (!en)
  100. return AVERROR(ENOMEM);
  101. av_strlcpy(en->filename, av_basename(hls->avf->filename), sizeof(en->filename));
  102. en->duration = duration;
  103. en->pos = pos;
  104. en->size = size;
  105. en->next = NULL;
  106. if (!hls->segments)
  107. hls->segments = en;
  108. else
  109. hls->last_segment->next = en;
  110. hls->last_segment = en;
  111. if (hls->max_nb_segments && hls->nb_entries >= hls->max_nb_segments) {
  112. en = hls->segments;
  113. hls->segments = en->next;
  114. av_free(en);
  115. } else
  116. hls->nb_entries++;
  117. hls->sequence++;
  118. return 0;
  119. }
  120. static void hls_free_segments(HLSContext *hls)
  121. {
  122. HLSSegment *p = hls->segments, *en;
  123. while(p) {
  124. en = p;
  125. p = p->next;
  126. av_free(en);
  127. }
  128. }
  129. static int hls_window(AVFormatContext *s, int last)
  130. {
  131. HLSContext *hls = s->priv_data;
  132. HLSSegment *en;
  133. int target_duration = 0;
  134. int ret = 0;
  135. int64_t sequence = FFMAX(hls->start_sequence, hls->sequence - hls->nb_entries);
  136. int version = hls->flags & HLS_SINGLE_FILE ? 4 : 3;
  137. if ((ret = avio_open2(&hls->pb, s->filename, AVIO_FLAG_WRITE,
  138. &s->interrupt_callback, NULL)) < 0)
  139. goto fail;
  140. for (en = hls->segments; en; en = en->next) {
  141. if (target_duration < en->duration)
  142. target_duration = ceil(en->duration);
  143. }
  144. avio_printf(hls->pb, "#EXTM3U\n");
  145. avio_printf(hls->pb, "#EXT-X-VERSION:%d\n", version);
  146. if (hls->allowcache == 0 || hls->allowcache == 1) {
  147. avio_printf(hls->pb, "#EXT-X-ALLOW-CACHE:%s\n", hls->allowcache == 0 ? "NO" : "YES");
  148. }
  149. avio_printf(hls->pb, "#EXT-X-TARGETDURATION:%d\n", target_duration);
  150. avio_printf(hls->pb, "#EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
  151. av_log(s, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%"PRId64"\n",
  152. sequence);
  153. for (en = hls->segments; en; en = en->next) {
  154. avio_printf(hls->pb, "#EXTINF:%f,\n", en->duration);
  155. if (hls->flags & HLS_SINGLE_FILE)
  156. avio_printf(hls->pb, "#EXT-X-BYTERANGE:%"PRIi64"@%"PRIi64"\n",
  157. en->size, en->pos);
  158. if (hls->baseurl)
  159. avio_printf(hls->pb, "%s", hls->baseurl);
  160. avio_printf(hls->pb, "%s\n", en->filename);
  161. }
  162. if (last)
  163. avio_printf(hls->pb, "#EXT-X-ENDLIST\n");
  164. fail:
  165. avio_closep(&hls->pb);
  166. return ret;
  167. }
  168. static int hls_start(AVFormatContext *s)
  169. {
  170. HLSContext *c = s->priv_data;
  171. AVFormatContext *oc = c->avf;
  172. int err = 0;
  173. if (c->flags & HLS_SINGLE_FILE)
  174. av_strlcpy(oc->filename, c->basename,
  175. sizeof(oc->filename));
  176. else
  177. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  178. c->basename, c->wrap ? c->sequence % c->wrap : c->sequence) < 0) {
  179. av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", c->basename);
  180. return AVERROR(EINVAL);
  181. }
  182. c->number++;
  183. if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  184. &s->interrupt_callback, NULL)) < 0)
  185. return err;
  186. if (oc->oformat->priv_class && oc->priv_data)
  187. av_opt_set(oc->priv_data, "mpegts_flags", "resend_headers", 0);
  188. return 0;
  189. }
  190. static int hls_write_header(AVFormatContext *s)
  191. {
  192. HLSContext *hls = s->priv_data;
  193. int ret, i;
  194. char *p;
  195. const char *pattern = "%d.ts";
  196. AVDictionary *options = NULL;
  197. int basename_size = strlen(s->filename) + strlen(pattern) + 1;
  198. hls->sequence = hls->start_sequence;
  199. hls->recording_time = hls->time * AV_TIME_BASE;
  200. hls->start_pts = AV_NOPTS_VALUE;
  201. if (hls->flags & HLS_SINGLE_FILE)
  202. pattern = ".ts";
  203. if (hls->format_options_str) {
  204. ret = av_dict_parse_string(&hls->format_options, hls->format_options_str, "=", ":", 0);
  205. if (ret < 0) {
  206. av_log(s, AV_LOG_ERROR, "Could not parse format options list '%s'\n", hls->format_options_str);
  207. goto fail;
  208. }
  209. }
  210. for (i = 0; i < s->nb_streams; i++)
  211. hls->has_video +=
  212. s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO;
  213. if (hls->has_video > 1)
  214. av_log(s, AV_LOG_WARNING,
  215. "More than a single video stream present, "
  216. "expect issues decoding it.\n");
  217. hls->oformat = av_guess_format("mpegts", NULL, NULL);
  218. if (!hls->oformat) {
  219. ret = AVERROR_MUXER_NOT_FOUND;
  220. goto fail;
  221. }
  222. hls->basename = av_malloc(basename_size);
  223. if (!hls->basename) {
  224. ret = AVERROR(ENOMEM);
  225. goto fail;
  226. }
  227. strcpy(hls->basename, s->filename);
  228. p = strrchr(hls->basename, '.');
  229. if (p)
  230. *p = '\0';
  231. av_strlcat(hls->basename, pattern, basename_size);
  232. if ((ret = hls_mux_init(s)) < 0)
  233. goto fail;
  234. if ((ret = hls_start(s)) < 0)
  235. goto fail;
  236. av_dict_copy(&options, hls->format_options, 0);
  237. ret = avformat_write_header(hls->avf, &options);
  238. if (av_dict_count(options)) {
  239. av_log(s, AV_LOG_ERROR, "Some of provided format options in '%s' are not recognized\n", hls->format_options_str);
  240. ret = AVERROR(EINVAL);
  241. goto fail;
  242. }
  243. av_assert0(s->nb_streams == hls->avf->nb_streams);
  244. for (i = 0; i < s->nb_streams; i++) {
  245. AVStream *inner_st = hls->avf->streams[i];
  246. AVStream *outer_st = s->streams[i];
  247. avpriv_set_pts_info(outer_st, inner_st->pts_wrap_bits, inner_st->time_base.num, inner_st->time_base.den);
  248. }
  249. fail:
  250. av_dict_free(&options);
  251. if (ret) {
  252. av_free(hls->basename);
  253. if (hls->avf)
  254. avformat_free_context(hls->avf);
  255. }
  256. return ret;
  257. }
  258. static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
  259. {
  260. HLSContext *hls = s->priv_data;
  261. AVFormatContext *oc = hls->avf;
  262. AVStream *st = s->streams[pkt->stream_index];
  263. int64_t end_pts = hls->recording_time * hls->number;
  264. int is_ref_pkt = 1;
  265. int ret, can_split = 1;
  266. if (hls->start_pts == AV_NOPTS_VALUE) {
  267. hls->start_pts = pkt->pts;
  268. hls->end_pts = pkt->pts;
  269. }
  270. if (hls->has_video) {
  271. can_split = st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  272. pkt->flags & AV_PKT_FLAG_KEY;
  273. is_ref_pkt = st->codec->codec_type == AVMEDIA_TYPE_VIDEO;
  274. }
  275. if (pkt->pts == AV_NOPTS_VALUE)
  276. is_ref_pkt = can_split = 0;
  277. if (is_ref_pkt)
  278. hls->duration = (double)(pkt->pts - hls->end_pts)
  279. * st->time_base.num / st->time_base.den;
  280. if (can_split && av_compare_ts(pkt->pts - hls->start_pts, st->time_base,
  281. end_pts, AV_TIME_BASE_Q) >= 0) {
  282. int64_t new_start_pos;
  283. av_write_frame(oc, NULL); /* Flush any buffered data */
  284. new_start_pos = avio_tell(hls->avf->pb);
  285. hls->size = new_start_pos - hls->start_pos;
  286. ret = hls_append_segment(hls, hls->duration, hls->start_pos, hls->size);
  287. hls->start_pos = new_start_pos;
  288. if (ret)
  289. return ret;
  290. hls->end_pts = pkt->pts;
  291. hls->duration = 0;
  292. if (hls->flags & HLS_SINGLE_FILE) {
  293. if (hls->avf->oformat->priv_class && hls->avf->priv_data)
  294. av_opt_set(hls->avf->priv_data, "mpegts_flags", "resend_headers", 0);
  295. hls->number++;
  296. } else {
  297. avio_close(oc->pb);
  298. ret = hls_start(s);
  299. }
  300. if (ret)
  301. return ret;
  302. oc = hls->avf;
  303. if ((ret = hls_window(s, 0)) < 0)
  304. return ret;
  305. }
  306. ret = ff_write_chained(oc, pkt->stream_index, pkt, s, 0);
  307. return ret;
  308. }
  309. static int hls_write_trailer(struct AVFormatContext *s)
  310. {
  311. HLSContext *hls = s->priv_data;
  312. AVFormatContext *oc = hls->avf;
  313. av_write_trailer(oc);
  314. hls->size = avio_tell(hls->avf->pb) - hls->start_pos;
  315. avio_closep(&oc->pb);
  316. avformat_free_context(oc);
  317. av_free(hls->basename);
  318. hls_append_segment(hls, hls->duration, hls->start_pos, hls->size);
  319. hls_window(s, 1);
  320. hls_free_segments(hls);
  321. avio_close(hls->pb);
  322. return 0;
  323. }
  324. #define OFFSET(x) offsetof(HLSContext, x)
  325. #define E AV_OPT_FLAG_ENCODING_PARAM
  326. static const AVOption options[] = {
  327. {"start_number", "set first number in the sequence", OFFSET(start_sequence),AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, E},
  328. {"hls_time", "set segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E},
  329. {"hls_list_size", "set maximum number of playlist entries", OFFSET(max_nb_segments), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E},
  330. {"hls_ts_options","set hls mpegts list of options for the container format used for hls", OFFSET(format_options_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
  331. {"hls_wrap", "set number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E},
  332. {"hls_allow_cache", "explicitly set whether the client MAY (1) or MUST NOT (0) cache media segments", OFFSET(allowcache), AV_OPT_TYPE_INT, {.i64 = -1}, INT_MIN, INT_MAX, E},
  333. {"hls_base_url", "url to prepend to each playlist entry", OFFSET(baseurl), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
  334. {"hls_flags", "set flags affecting HLS playlist and media file generation", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0 }, 0, UINT_MAX, E, "flags"},
  335. {"single_file", "generate a single media file indexed with byte ranges", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_SINGLE_FILE }, 0, UINT_MAX, E, "flags"},
  336. { NULL },
  337. };
  338. static const AVClass hls_class = {
  339. .class_name = "hls muxer",
  340. .item_name = av_default_item_name,
  341. .option = options,
  342. .version = LIBAVUTIL_VERSION_INT,
  343. };
  344. AVOutputFormat ff_hls_muxer = {
  345. .name = "hls",
  346. .long_name = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
  347. .extensions = "m3u8",
  348. .priv_data_size = sizeof(HLSContext),
  349. .audio_codec = AV_CODEC_ID_AAC,
  350. .video_codec = AV_CODEC_ID_H264,
  351. .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
  352. .write_header = hls_write_header,
  353. .write_packet = hls_write_packet,
  354. .write_trailer = hls_write_trailer,
  355. .priv_class = &hls_class,
  356. };