FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hlsenc.c
Go to the documentation of this file.
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 
22 #include "config.h"
23 #include <float.h>
24 #include <stdint.h>
25 #if HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 
29 #include "libavutil/avassert.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/parseutils.h"
32 #include "libavutil/avstring.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/log.h"
35 
36 #include "avformat.h"
37 #include "internal.h"
38 #include "os_support.h"
39 
40 typedef struct HLSSegment {
41  char filename[1024];
42  double duration; /* in seconds */
43  int64_t pos;
44  int64_t size;
45 
46  struct HLSSegment *next;
47 } HLSSegment;
48 
49 typedef enum HLSFlags {
50  // Generate a single media file and use byte ranges in the playlist.
51  HLS_SINGLE_FILE = (1 << 0),
52  HLS_DELETE_SEGMENTS = (1 << 1),
53 } HLSFlags;
54 
55 typedef struct HLSContext {
56  const AVClass *class; // Class for private options.
57  unsigned number;
58  int64_t sequence;
59  int64_t start_sequence;
61 
63 
64  float time; // Set by a private option.
65  int max_nb_segments; // Set by a private option.
66  int wrap; // Set by a private option.
67  uint32_t flags; // enum HLSFlags
69 
71  int64_t recording_time;
72  int has_video;
73  int64_t start_pts;
74  int64_t end_pts;
75  double duration; // last segment duration computed so far, in seconds
76  int64_t start_pos; // last segment starting position
77  int64_t size; // last segment size
79 
83 
84  char *basename;
85  char *baseurl;
88 
90 } HLSContext;
91 
93 
94  HLSSegment *segment, *previous_segment = NULL;
95  float playlist_duration = 0.0f;
96  int ret = 0, path_size;
97  char *dirname = NULL, *p, *path;
98 
99  segment = hls->segments;
100  while (segment) {
101  playlist_duration += segment->duration;
102  segment = segment->next;
103  }
104 
105  segment = hls->old_segments;
106  while (segment) {
107  playlist_duration -= segment->duration;
108  previous_segment = segment;
109  segment = previous_segment->next;
110  if (playlist_duration <= -previous_segment->duration) {
111  previous_segment->next = NULL;
112  break;
113  }
114  }
115 
116  if (segment) {
117  if (hls->segment_filename) {
118  dirname = av_strdup(hls->segment_filename);
119  } else {
120  dirname = av_strdup(hls->avf->filename);
121  }
122  if (!dirname) {
123  ret = AVERROR(ENOMEM);
124  goto fail;
125  }
126  p = (char *)av_basename(dirname);
127  *p = '\0';
128  }
129 
130  while (segment) {
131  av_log(hls, AV_LOG_DEBUG, "deleting old segment %s\n",
132  segment->filename);
133  path_size = strlen(dirname) + strlen(segment->filename) + 1;
134  path = av_malloc(path_size);
135  if (!path) {
136  ret = AVERROR(ENOMEM);
137  goto fail;
138  }
139  av_strlcpy(path, dirname, path_size);
140  av_strlcat(path, segment->filename, path_size);
141  if (unlink(path) < 0) {
142  av_log(hls, AV_LOG_ERROR, "failed to delete old segment %s: %s\n",
143  path, strerror(errno));
144  }
145  av_free(path);
146  previous_segment = segment;
147  segment = previous_segment->next;
148  av_free(previous_segment);
149  }
150 
151 fail:
152  av_free(dirname);
153 
154  return ret;
155 }
156 
158 {
159  HLSContext *hls = s->priv_data;
160  AVFormatContext *oc;
161  int i, ret;
162 
163  ret = avformat_alloc_output_context2(&hls->avf, hls->oformat, NULL, NULL);
164  if (ret < 0)
165  return ret;
166  oc = hls->avf;
167 
168  oc->oformat = hls->oformat;
170  oc->max_delay = s->max_delay;
171  av_dict_copy(&oc->metadata, s->metadata, 0);
172 
173  for (i = 0; i < s->nb_streams; i++) {
174  AVStream *st;
175  if (!(st = avformat_new_stream(oc, NULL)))
176  return AVERROR(ENOMEM);
179  st->time_base = s->streams[i]->time_base;
180  }
181  hls->start_pos = 0;
182 
183  return 0;
184 }
185 
186 /* Create a new segment and append it to the segment list */
187 static int hls_append_segment(HLSContext *hls, double duration, int64_t pos,
188  int64_t size)
189 {
190  HLSSegment *en = av_malloc(sizeof(*en));
191  int ret;
192 
193  if (!en)
194  return AVERROR(ENOMEM);
195 
196  av_strlcpy(en->filename, av_basename(hls->avf->filename), sizeof(en->filename));
197 
198  en->duration = duration;
199  en->pos = pos;
200  en->size = size;
201  en->next = NULL;
202 
203  if (!hls->segments)
204  hls->segments = en;
205  else
206  hls->last_segment->next = en;
207 
208  hls->last_segment = en;
209 
210  if (hls->max_nb_segments && hls->nb_entries >= hls->max_nb_segments) {
211  en = hls->segments;
212  hls->segments = en->next;
213  if (en && hls->flags & HLS_DELETE_SEGMENTS &&
214  !(hls->flags & HLS_SINGLE_FILE || hls->wrap)) {
215  en->next = hls->old_segments;
216  hls->old_segments = en;
217  if ((ret = hls_delete_old_segments(hls)) < 0)
218  return ret;
219  } else
220  av_free(en);
221  } else
222  hls->nb_entries++;
223 
224  hls->sequence++;
225 
226  return 0;
227 }
228 
230 {
231  HLSSegment *en;
232 
233  while(p) {
234  en = p;
235  p = p->next;
236  av_free(en);
237  }
238 }
239 
240 static int hls_window(AVFormatContext *s, int last)
241 {
242  HLSContext *hls = s->priv_data;
243  HLSSegment *en;
244  int target_duration = 0;
245  int ret = 0;
246  int64_t sequence = FFMAX(hls->start_sequence, hls->sequence - hls->nb_entries);
247  int version = hls->flags & HLS_SINGLE_FILE ? 4 : 3;
248 
249  if ((ret = avio_open2(&hls->pb, s->filename, AVIO_FLAG_WRITE,
250  &s->interrupt_callback, NULL)) < 0)
251  goto fail;
252 
253  for (en = hls->segments; en; en = en->next) {
254  if (target_duration < en->duration)
255  target_duration = ceil(en->duration);
256  }
257 
258  avio_printf(hls->pb, "#EXTM3U\n");
259  avio_printf(hls->pb, "#EXT-X-VERSION:%d\n", version);
260  if (hls->allowcache == 0 || hls->allowcache == 1) {
261  avio_printf(hls->pb, "#EXT-X-ALLOW-CACHE:%s\n", hls->allowcache == 0 ? "NO" : "YES");
262  }
263  avio_printf(hls->pb, "#EXT-X-TARGETDURATION:%d\n", target_duration);
264  avio_printf(hls->pb, "#EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
265 
266  av_log(s, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%"PRId64"\n",
267  sequence);
268 
269  for (en = hls->segments; en; en = en->next) {
270  avio_printf(hls->pb, "#EXTINF:%f,\n", en->duration);
271  if (hls->flags & HLS_SINGLE_FILE)
272  avio_printf(hls->pb, "#EXT-X-BYTERANGE:%"PRIi64"@%"PRIi64"\n",
273  en->size, en->pos);
274  if (hls->baseurl)
275  avio_printf(hls->pb, "%s", hls->baseurl);
276  avio_printf(hls->pb, "%s\n", en->filename);
277  }
278 
279  if (last)
280  avio_printf(hls->pb, "#EXT-X-ENDLIST\n");
281 
282 fail:
283  avio_closep(&hls->pb);
284  return ret;
285 }
286 
288 {
289  HLSContext *c = s->priv_data;
290  AVFormatContext *oc = c->avf;
291  int err = 0;
292 
293  if (c->flags & HLS_SINGLE_FILE)
294  av_strlcpy(oc->filename, c->basename,
295  sizeof(oc->filename));
296  else
297  if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
298  c->basename, c->wrap ? c->sequence % c->wrap : c->sequence) < 0) {
299  av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", c->basename);
300  return AVERROR(EINVAL);
301  }
302  c->number++;
303 
304  if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
305  &s->interrupt_callback, NULL)) < 0)
306  return err;
307 
308  if (oc->oformat->priv_class && oc->priv_data)
309  av_opt_set(oc->priv_data, "mpegts_flags", "resend_headers", 0);
310 
311  return 0;
312 }
313 
315 {
316  HLSContext *hls = s->priv_data;
317  int ret, i;
318  char *p;
319  const char *pattern = "%d.ts";
320  AVDictionary *options = NULL;
321  int basename_size;
322 
323  hls->sequence = hls->start_sequence;
324  hls->recording_time = hls->time * AV_TIME_BASE;
325  hls->start_pts = AV_NOPTS_VALUE;
326 
327  if (hls->format_options_str) {
328  ret = av_dict_parse_string(&hls->format_options, hls->format_options_str, "=", ":", 0);
329  if (ret < 0) {
330  av_log(s, AV_LOG_ERROR, "Could not parse format options list '%s'\n", hls->format_options_str);
331  goto fail;
332  }
333  }
334 
335  for (i = 0; i < s->nb_streams; i++)
336  hls->has_video +=
338 
339  if (hls->has_video > 1)
341  "More than a single video stream present, "
342  "expect issues decoding it.\n");
343 
344  hls->oformat = av_guess_format("mpegts", NULL, NULL);
345 
346  if (!hls->oformat) {
348  goto fail;
349  }
350 
351  if (hls->segment_filename) {
352  hls->basename = av_strdup(hls->segment_filename);
353  if (!hls->basename) {
354  ret = AVERROR(ENOMEM);
355  goto fail;
356  }
357  } else {
358  if (hls->flags & HLS_SINGLE_FILE)
359  pattern = ".ts";
360 
361  basename_size = strlen(s->filename) + strlen(pattern) + 1;
362  hls->basename = av_malloc(basename_size);
363  if (!hls->basename) {
364  ret = AVERROR(ENOMEM);
365  goto fail;
366  }
367 
368  av_strlcpy(hls->basename, s->filename, basename_size);
369 
370  p = strrchr(hls->basename, '.');
371  if (p)
372  *p = '\0';
373  av_strlcat(hls->basename, pattern, basename_size);
374  }
375 
376  if ((ret = hls_mux_init(s)) < 0)
377  goto fail;
378 
379  if ((ret = hls_start(s)) < 0)
380  goto fail;
381 
382  av_dict_copy(&options, hls->format_options, 0);
383  ret = avformat_write_header(hls->avf, &options);
384  if (av_dict_count(options)) {
385  av_log(s, AV_LOG_ERROR, "Some of provided format options in '%s' are not recognized\n", hls->format_options_str);
386  ret = AVERROR(EINVAL);
387  goto fail;
388  }
389  av_assert0(s->nb_streams == hls->avf->nb_streams);
390  for (i = 0; i < s->nb_streams; i++) {
391  AVStream *inner_st = hls->avf->streams[i];
392  AVStream *outer_st = s->streams[i];
393  avpriv_set_pts_info(outer_st, inner_st->pts_wrap_bits, inner_st->time_base.num, inner_st->time_base.den);
394  }
395 fail:
396 
397  av_dict_free(&options);
398  if (ret) {
399  av_freep(&hls->basename);
400  if (hls->avf)
402  }
403  return ret;
404 }
405 
407 {
408  HLSContext *hls = s->priv_data;
409  AVFormatContext *oc = hls->avf;
410  AVStream *st = s->streams[pkt->stream_index];
411  int64_t end_pts = hls->recording_time * hls->number;
412  int is_ref_pkt = 1;
413  int ret, can_split = 1;
414 
415  if (hls->start_pts == AV_NOPTS_VALUE) {
416  hls->start_pts = pkt->pts;
417  hls->end_pts = pkt->pts;
418  }
419 
420  if (hls->has_video) {
421  can_split = st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
422  pkt->flags & AV_PKT_FLAG_KEY;
423  is_ref_pkt = st->codec->codec_type == AVMEDIA_TYPE_VIDEO;
424  }
425  if (pkt->pts == AV_NOPTS_VALUE)
426  is_ref_pkt = can_split = 0;
427 
428  if (is_ref_pkt)
429  hls->duration = (double)(pkt->pts - hls->end_pts)
430  * st->time_base.num / st->time_base.den;
431 
432  if (can_split && av_compare_ts(pkt->pts - hls->start_pts, st->time_base,
433  end_pts, AV_TIME_BASE_Q) >= 0) {
434  int64_t new_start_pos;
435  av_write_frame(oc, NULL); /* Flush any buffered data */
436 
437  new_start_pos = avio_tell(hls->avf->pb);
438  hls->size = new_start_pos - hls->start_pos;
439  ret = hls_append_segment(hls, hls->duration, hls->start_pos, hls->size);
440  hls->start_pos = new_start_pos;
441  if (ret)
442  return ret;
443 
444  hls->end_pts = pkt->pts;
445  hls->duration = 0;
446 
447  if (hls->flags & HLS_SINGLE_FILE) {
448  if (hls->avf->oformat->priv_class && hls->avf->priv_data)
449  av_opt_set(hls->avf->priv_data, "mpegts_flags", "resend_headers", 0);
450  hls->number++;
451  } else {
452  avio_closep(&oc->pb);
453 
454  ret = hls_start(s);
455  }
456 
457  if (ret)
458  return ret;
459 
460  oc = hls->avf;
461 
462  if ((ret = hls_window(s, 0)) < 0)
463  return ret;
464  }
465 
466  ret = ff_write_chained(oc, pkt->stream_index, pkt, s, 0);
467 
468  return ret;
469 }
470 
472 {
473  HLSContext *hls = s->priv_data;
474  AVFormatContext *oc = hls->avf;
475 
476  av_write_trailer(oc);
477  if (oc->pb) {
478  hls->size = avio_tell(hls->avf->pb) - hls->start_pos;
479  avio_closep(&oc->pb);
480  hls_append_segment(hls, hls->duration, hls->start_pos, hls->size);
481  }
482  av_freep(&hls->basename);
484  hls->avf = NULL;
485  hls_window(s, 1);
486 
489  avio_close(hls->pb);
490  return 0;
491 }
492 
493 #define OFFSET(x) offsetof(HLSContext, x)
494 #define E AV_OPT_FLAG_ENCODING_PARAM
495 static const AVOption options[] = {
496  {"start_number", "set first number in the sequence", OFFSET(start_sequence),AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, E},
497  {"hls_time", "set segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E},
498  {"hls_list_size", "set maximum number of playlist entries", OFFSET(max_nb_segments), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E},
499  {"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},
500  {"hls_wrap", "set number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E},
501  {"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},
502  {"hls_base_url", "url to prepend to each playlist entry", OFFSET(baseurl), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
503  {"hls_segment_filename", "filename template for segment files", OFFSET(segment_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
504  {"hls_flags", "set flags affecting HLS playlist and media file generation", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0 }, 0, UINT_MAX, E, "flags"},
505  {"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"},
506  {"delete_segments", "delete segment files that are no longer part of the playlist", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_DELETE_SEGMENTS }, 0, UINT_MAX, E, "flags"},
507 
508  { NULL },
509 };
510 
511 static const AVClass hls_class = {
512  .class_name = "hls muxer",
513  .item_name = av_default_item_name,
514  .option = options,
515  .version = LIBAVUTIL_VERSION_INT,
516 };
517 
518 
520  .name = "hls",
521  .long_name = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
522  .extensions = "m3u8",
523  .priv_data_size = sizeof(HLSContext),
524  .audio_codec = AV_CODEC_ID_AAC,
525  .video_codec = AV_CODEC_ID_H264,
530  .priv_class = &hls_class,
531 };