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 <float.h>
23 #include <stdint.h>
24 
25 #include "libavutil/mathematics.h"
26 #include "libavutil/parseutils.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/log.h"
30 
31 #include "avformat.h"
32 #include "internal.h"
33 
34 typedef struct ListEntry {
35  char name[1024];
36  int duration;
37  struct ListEntry *next;
38 } ListEntry;
39 
40 typedef struct HLSContext {
41  const AVClass *class; // Class for private options.
42  unsigned number;
43  int64_t sequence;
46  float time; // Set by a private option.
47  int size; // Set by a private option.
48  int wrap; // Set by a private option.
49  int64_t recording_time;
50  int has_video;
51  int64_t start_pts;
52  int64_t end_pts;
53  int64_t duration; // last segment duration computed so far, in seconds
57  char *basename;
59 } HLSContext;
60 
62 {
63  HLSContext *hls = s->priv_data;
64  AVFormatContext *oc;
65  int i;
66 
67  hls->avf = oc = avformat_alloc_context();
68  if (!oc)
69  return AVERROR(ENOMEM);
70 
71  oc->oformat = hls->oformat;
73  av_dict_copy(&oc->metadata, s->metadata, 0);
74 
75  for (i = 0; i < s->nb_streams; i++) {
76  AVStream *st;
77  if (!(st = avformat_new_stream(oc, NULL)))
78  return AVERROR(ENOMEM);
81  }
82 
83  return 0;
84 }
85 
86 static int append_entry(HLSContext *hls, uint64_t duration)
87 {
88  ListEntry *en = av_malloc(sizeof(*en));
89 
90  if (!en)
91  return AVERROR(ENOMEM);
92 
93  av_strlcpy(en->name, av_basename(hls->avf->filename), sizeof(en->name));
94 
95  en->duration = duration;
96  en->next = NULL;
97 
98  if (!hls->list)
99  hls->list = en;
100  else
101  hls->end_list->next = en;
102 
103  hls->end_list = en;
104 
105  if (hls->nb_entries >= hls->size) {
106  en = hls->list;
107  hls->list = en->next;
108  av_free(en);
109  } else
110  hls->nb_entries++;
111 
112  hls->sequence++;
113 
114  return 0;
115 }
116 
117 static void free_entries(HLSContext *hls)
118 {
119  ListEntry *p = hls->list, *en;
120 
121  while(p) {
122  en = p;
123  p = p->next;
124  av_free(en);
125  }
126 }
127 
128 static int hls_window(AVFormatContext *s, int last)
129 {
130  HLSContext *hls = s->priv_data;
131  ListEntry *en;
132  int target_duration = 0;
133  int ret = 0;
134 
135  if ((ret = avio_open2(&hls->pb, s->filename, AVIO_FLAG_WRITE,
136  &s->interrupt_callback, NULL)) < 0)
137  goto fail;
138 
139  for (en = hls->list; en; en = en->next) {
140  if (target_duration < en->duration)
141  target_duration = en->duration;
142  }
143 
144  avio_printf(hls->pb, "#EXTM3U\n");
145  avio_printf(hls->pb, "#EXT-X-VERSION:3\n");
146  avio_printf(hls->pb, "#EXT-X-TARGETDURATION:%d\n", target_duration);
147  avio_printf(hls->pb, "#EXT-X-MEDIA-SEQUENCE:%"PRId64"\n",
148  FFMAX(0, hls->sequence - hls->size));
149 
150  for (en = hls->list; en; en = en->next) {
151  avio_printf(hls->pb, "#EXTINF:%d,\n", en->duration);
152  avio_printf(hls->pb, "%s\n", en->name);
153  }
154 
155  if (last)
156  avio_printf(hls->pb, "#EXT-X-ENDLIST\n");
157 
158 fail:
159  avio_closep(&hls->pb);
160  return ret;
161 }
162 
164 {
165  HLSContext *c = s->priv_data;
166  AVFormatContext *oc = c->avf;
167  int err = 0;
168 
169  if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
170  c->basename, c->wrap ? c->number % c->wrap : c->number) < 0) {
171  av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", c->basename);
172  return AVERROR(EINVAL);
173  }
174  c->number++;
175 
176  if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
177  &s->interrupt_callback, NULL)) < 0)
178  return err;
179 
180  if (oc->oformat->priv_class && oc->priv_data)
181  av_opt_set(oc->priv_data, "mpegts_flags", "resend_headers", 0);
182 
183  return 0;
184 }
185 
187 {
188  HLSContext *hls = s->priv_data;
189  int ret, i;
190  char *p;
191  const char *pattern = "%d.ts";
192  int basename_size = strlen(s->filename) + strlen(pattern) + 1;
193 
194  hls->number = 0;
195 
196  hls->recording_time = hls->time * AV_TIME_BASE;
197  hls->start_pts = AV_NOPTS_VALUE;
198 
199  for (i = 0; i < s->nb_streams; i++)
200  hls->has_video +=
202 
203  if (hls->has_video > 1)
205  "More than a single video stream present, "
206  "expect issues decoding it.\n");
207 
208  hls->oformat = av_guess_format("mpegts", NULL, NULL);
209 
210  if (!hls->oformat) {
212  goto fail;
213  }
214 
215  hls->basename = av_malloc(basename_size);
216 
217  if (!hls->basename) {
218  ret = AVERROR(ENOMEM);
219  goto fail;
220  }
221 
222  strcpy(hls->basename, s->filename);
223 
224  p = strrchr(hls->basename, '.');
225 
226  if (p)
227  *p = '\0';
228 
229  av_strlcat(hls->basename, pattern, basename_size);
230 
231  if ((ret = hls_mux_init(s)) < 0)
232  goto fail;
233 
234  if ((ret = hls_start(s)) < 0)
235  goto fail;
236 
237  if ((ret = avformat_write_header(hls->avf, NULL)) < 0)
238  return ret;
239 
240 
241 fail:
242  if (ret) {
243  av_free(hls->basename);
244  if (hls->avf)
246  }
247  return ret;
248 }
249 
251 {
252  HLSContext *hls = s->priv_data;
253  AVFormatContext *oc = hls->avf;
254  AVStream *st = s->streams[pkt->stream_index];
255  int64_t end_pts = hls->recording_time * hls->number;
256  int is_ref_pkt = 1;
257  int ret, can_split = 1;
258 
259  if (hls->start_pts == AV_NOPTS_VALUE) {
260  hls->start_pts = pkt->pts;
261  hls->end_pts = pkt->pts;
262  }
263 
264  if (hls->has_video) {
265  can_split = st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
266  pkt->flags & AV_PKT_FLAG_KEY;
267  is_ref_pkt = st->codec->codec_type == AVMEDIA_TYPE_VIDEO;
268  }
269  if (pkt->pts == AV_NOPTS_VALUE)
270  is_ref_pkt = can_split = 0;
271 
272  if (is_ref_pkt)
273  hls->duration = av_rescale(pkt->pts - hls->end_pts,
274  st->time_base.num, st->time_base.den);
275 
276  if (can_split && av_compare_ts(pkt->pts - hls->start_pts, st->time_base,
277  end_pts, AV_TIME_BASE_Q) >= 0) {
278  ret = append_entry(hls, hls->duration);
279  if (ret)
280  return ret;
281 
282  hls->end_pts = pkt->pts;
283  hls->duration = 0;
284 
285  av_write_frame(oc, NULL); /* Flush any buffered data */
286  avio_close(oc->pb);
287 
288  ret = hls_start(s);
289 
290  if (ret)
291  return ret;
292 
293  oc = hls->avf;
294 
295  if ((ret = hls_window(s, 0)) < 0)
296  return ret;
297  }
298 
299  ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
300 
301  return ret;
302 }
303 
305 {
306  HLSContext *hls = s->priv_data;
307  AVFormatContext *oc = hls->avf;
308 
309  av_write_trailer(oc);
310  avio_closep(&oc->pb);
312  av_free(hls->basename);
313  append_entry(hls, hls->duration);
314  hls_window(s, 1);
315 
316  free_entries(hls);
317  avio_close(hls->pb);
318  return 0;
319 }
320 
321 #define OFFSET(x) offsetof(HLSContext, x)
322 #define E AV_OPT_FLAG_ENCODING_PARAM
323 static const AVOption options[] = {
324  {"start_number", "set first number in the sequence", OFFSET(sequence),AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, E},
325  {"hls_time", "set segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E},
326  {"hls_list_size", "set maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E},
327  {"hls_wrap", "set number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E},
328  { NULL },
329 };
330 
331 static const AVClass hls_class = {
332  .class_name = "hls muxer",
333  .item_name = av_default_item_name,
334  .option = options,
335  .version = LIBAVUTIL_VERSION_INT,
336 };
337 
338 
340  .name = "hls",
341  .long_name = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
342  .extensions = "m3u8",
343  .priv_data_size = sizeof(HLSContext),
344  .audio_codec = AV_CODEC_ID_MP2,
345  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
350  .priv_class = &hls_class,
351 };