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.

325 lines
8.4KB

  1. /*
  2. * Apple HTTP Live Streaming segmenter
  3. * Copyright (c) 2012, 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 "libavutil/mathematics.h"
  23. #include "libavutil/parseutils.h"
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/log.h"
  27. #include "avformat.h"
  28. #include "internal.h"
  29. typedef struct ListEntry {
  30. char name[1024];
  31. int duration;
  32. struct ListEntry *next;
  33. } ListEntry;
  34. typedef struct HLSContext {
  35. const AVClass *class; // Class for private options.
  36. int number;
  37. AVOutputFormat *oformat;
  38. AVFormatContext *avf;
  39. float time; // Set by a private option.
  40. int size; // Set by a private option.
  41. int wrap; // Set by a private option.
  42. int64_t recording_time;
  43. int has_video;
  44. int64_t start_pts;
  45. int64_t end_pts;
  46. ListEntry *list;
  47. ListEntry *end_list;
  48. char *basename;
  49. AVIOContext *pb;
  50. } HLSContext;
  51. static int hls_mux_init(AVFormatContext *s)
  52. {
  53. HLSContext *hls = s->priv_data;
  54. AVFormatContext *oc;
  55. int i;
  56. hls->avf = oc = avformat_alloc_context();
  57. if (!oc)
  58. return AVERROR(ENOMEM);
  59. oc->oformat = hls->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 append_entry(HLSContext *hls, uint64_t duration)
  71. {
  72. ListEntry *en = av_malloc(sizeof(*en));
  73. if (!en)
  74. return AVERROR(ENOMEM);
  75. av_get_frame_filename(en->name, sizeof(en->name), hls->basename,
  76. hls->number -1);
  77. en->duration = duration;
  78. en->next = NULL;
  79. if (!hls->list)
  80. hls->list = en;
  81. else
  82. hls->end_list->next = en;
  83. hls->end_list = en;
  84. if (hls->number >= hls->size) {
  85. en = hls->list;
  86. hls->list = en->next;
  87. av_free(en);
  88. }
  89. return 0;
  90. }
  91. static void free_entries(HLSContext *hls)
  92. {
  93. ListEntry *p = hls->list, *en;
  94. while(p) {
  95. en = p;
  96. p = p->next;
  97. av_free(en);
  98. }
  99. }
  100. static int hls_window(AVFormatContext *s, int last)
  101. {
  102. HLSContext *hls = s->priv_data;
  103. ListEntry *en;
  104. int ret = 0;
  105. if ((ret = avio_open2(&hls->pb, s->filename, AVIO_FLAG_WRITE,
  106. &s->interrupt_callback, NULL)) < 0)
  107. goto fail;
  108. avio_printf(hls->pb, "#EXTM3U\n");
  109. avio_printf(hls->pb, "#EXT-X-VERSION:3\n");
  110. avio_printf(hls->pb, "#EXT-X-TARGETDURATION:%d\n", (int)hls->time);
  111. avio_printf(hls->pb, "#EXT-X-MEDIA-SEQUENCE:%d\n",
  112. FFMAX(0, hls->number - hls->size));
  113. for (en = hls->list; en; en = en->next) {
  114. avio_printf(hls->pb, "#EXTINF:%d,\n", en->duration);
  115. avio_printf(hls->pb, "%s\n", en->name);
  116. }
  117. if (last)
  118. avio_printf(hls->pb, "#EXT-X-ENDLIST\n");
  119. fail:
  120. avio_closep(&hls->pb);
  121. return ret;
  122. }
  123. static int hls_start(AVFormatContext *s)
  124. {
  125. HLSContext *c = s->priv_data;
  126. AVFormatContext *oc = c->avf;
  127. int err = 0;
  128. if (c->wrap)
  129. c->number %= c->wrap;
  130. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  131. c->basename, c->number++) < 0)
  132. return AVERROR(EINVAL);
  133. if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  134. &s->interrupt_callback, NULL)) < 0)
  135. return err;
  136. if (oc->oformat->priv_class && oc->priv_data)
  137. av_opt_set(oc->priv_data, "mpegts_flags", "resend_headers", 0);
  138. return 0;
  139. }
  140. static int hls_write_header(AVFormatContext *s)
  141. {
  142. HLSContext *hls = s->priv_data;
  143. int ret, i;
  144. char *p;
  145. const char *pattern = "%d.ts";
  146. int basename_size = strlen(s->filename) + strlen(pattern);
  147. hls->number = 0;
  148. hls->recording_time = hls->time * 1000000;
  149. hls->start_pts = AV_NOPTS_VALUE;
  150. for (i = 0; i < s->nb_streams; i++)
  151. hls->has_video +=
  152. s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO;
  153. if (hls->has_video > 1)
  154. av_log(s, AV_LOG_WARNING,
  155. "More than a single video stream present, "
  156. "expect issues decoding it.\n");
  157. hls->oformat = av_guess_format("mpegts", NULL, NULL);
  158. if (!hls->oformat) {
  159. ret = AVERROR_MUXER_NOT_FOUND;
  160. goto fail;
  161. }
  162. hls->basename = av_malloc(basename_size);
  163. if (!hls->basename) {
  164. ret = AVERROR(ENOMEM);
  165. goto fail;
  166. }
  167. strcpy(hls->basename, s->filename);
  168. p = strrchr(hls->basename, '.');
  169. if (p)
  170. *p = '\0';
  171. av_strlcat(hls->basename, "%d.ts", basename_size);
  172. if ((ret = hls_mux_init(s)) < 0)
  173. goto fail;
  174. if ((ret = hls_start(s)) < 0)
  175. goto fail;
  176. if ((ret = avformat_write_header(hls->avf, NULL)) < 0)
  177. return ret;
  178. fail:
  179. if (ret) {
  180. av_free(hls->basename);
  181. if (hls->avf)
  182. avformat_free_context(hls->avf);
  183. }
  184. return ret;
  185. }
  186. static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
  187. {
  188. HLSContext *hls = s->priv_data;
  189. AVFormatContext *oc = hls->avf;
  190. AVStream *st = s->streams[pkt->stream_index];
  191. int64_t end_pts = hls->recording_time * hls->number;
  192. int ret;
  193. if (hls->start_pts == AV_NOPTS_VALUE) {
  194. hls->start_pts = pkt->pts;
  195. hls->end_pts = pkt->pts;
  196. }
  197. end_pts += hls->start_pts;
  198. if ((hls->has_video && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
  199. av_compare_ts(pkt->pts, st->time_base, end_pts, AV_TIME_BASE_Q) >= 0 &&
  200. pkt->flags & AV_PKT_FLAG_KEY) {
  201. append_entry(hls, av_rescale(pkt->pts - hls->end_pts,
  202. st->time_base.num,
  203. st->time_base.den));
  204. hls->end_pts = pkt->pts;
  205. av_write_frame(oc, NULL); /* Flush any buffered data */
  206. avio_close(oc->pb);
  207. ret = hls_start(s);
  208. if (ret)
  209. return ret;
  210. oc = hls->avf;
  211. if ((ret = hls_window(s, 0)) < 0)
  212. return ret;
  213. }
  214. ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
  215. return ret;
  216. }
  217. static int hls_write_trailer(struct AVFormatContext *s)
  218. {
  219. HLSContext *hls = s->priv_data;
  220. AVFormatContext *oc = hls->avf;
  221. av_write_trailer(oc);
  222. avio_closep(&oc->pb);
  223. avformat_free_context(oc);
  224. av_free(hls->basename);
  225. hls_window(s, 1);
  226. free_entries(hls);
  227. avio_close(hls->pb);
  228. return 0;
  229. }
  230. #define OFFSET(x) offsetof(HLSContext, x)
  231. #define E AV_OPT_FLAG_ENCODING_PARAM
  232. static const AVOption options[] = {
  233. {"hls_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E},
  234. {"hls_list_size", "maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E},
  235. {"hls_wrap", "number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E},
  236. { NULL },
  237. };
  238. static const AVClass hls_class = {
  239. .class_name = "hls muxer",
  240. .item_name = av_default_item_name,
  241. .option = options,
  242. .version = LIBAVUTIL_VERSION_INT,
  243. };
  244. AVOutputFormat ff_hls_muxer = {
  245. .name = "hls",
  246. .long_name = NULL_IF_CONFIG_SMALL("hls"),
  247. .extensions = "m3u8",
  248. .priv_data_size = sizeof(HLSContext),
  249. .audio_codec = AV_CODEC_ID_MP2,
  250. .video_codec = AV_CODEC_ID_MPEG2VIDEO,
  251. .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
  252. .write_header = hls_write_header,
  253. .write_packet = hls_write_packet,
  254. .write_trailer = hls_write_trailer,
  255. .priv_class = &hls_class,
  256. };