FFmpeg
 All Data Structures 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 
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 
30 #include "avformat.h"
31 #include "internal.h"
32 
33 typedef struct ListEntry {
34  char name[1024];
35  int duration;
36  struct ListEntry *next;
37 } ListEntry;
38 
39 typedef struct HLSContext {
40  const AVClass *class; // Class for private options.
41  unsigned number;
42  int64_t sequence;
45  float time; // Set by a private option.
46  int size; // Set by a private option.
47  int wrap; // Set by a private option.
48  int64_t recording_time;
49  int has_video;
50  int64_t start_pts;
51  int64_t end_pts;
52  int64_t duration; ///< last segment duration computed so far, in seconds
56  char *basename;
58 } HLSContext;
59 
61 {
62  HLSContext *hls = s->priv_data;
63  AVFormatContext *oc;
64  int i;
65 
66  hls->avf = oc = avformat_alloc_context();
67  if (!oc)
68  return AVERROR(ENOMEM);
69 
70  oc->oformat = hls->oformat;
72 
73  for (i = 0; i < s->nb_streams; i++) {
74  AVStream *st;
75  if (!(st = avformat_new_stream(oc, NULL)))
76  return AVERROR(ENOMEM);
79  }
80 
81  return 0;
82 }
83 
84 static int append_entry(HLSContext *hls, uint64_t duration)
85 {
86  ListEntry *en = av_malloc(sizeof(*en));
87 
88  if (!en)
89  return AVERROR(ENOMEM);
90 
91  av_strlcpy(en->name, av_basename(hls->avf->filename), sizeof(en->name));
92 
93  en->duration = duration;
94  en->next = NULL;
95 
96  if (!hls->list)
97  hls->list = en;
98  else
99  hls->end_list->next = en;
100 
101  hls->end_list = en;
102 
103  if (hls->nb_entries >= hls->size) {
104  en = hls->list;
105  hls->list = en->next;
106  av_free(en);
107  } else
108  hls->nb_entries++;
109 
110  hls->sequence++;
111 
112  return 0;
113 }
114 
115 static void free_entries(HLSContext *hls)
116 {
117  ListEntry *p = hls->list, *en;
118 
119  while(p) {
120  en = p;
121  p = p->next;
122  av_free(en);
123  }
124 }
125 
126 static int hls_window(AVFormatContext *s, int last)
127 {
128  HLSContext *hls = s->priv_data;
129  ListEntry *en;
130  int target_duration = 0;
131  int ret = 0;
132 
133  if ((ret = avio_open2(&hls->pb, s->filename, AVIO_FLAG_WRITE,
134  &s->interrupt_callback, NULL)) < 0)
135  goto fail;
136 
137  for (en = hls->list; en; en = en->next) {
138  if (target_duration < en->duration)
139  target_duration = en->duration;
140  }
141 
142  avio_printf(hls->pb, "#EXTM3U\n");
143  avio_printf(hls->pb, "#EXT-X-VERSION:3\n");
144  avio_printf(hls->pb, "#EXT-X-TARGETDURATION:%d\n", target_duration);
145  avio_printf(hls->pb, "#EXT-X-MEDIA-SEQUENCE:%"PRId64"\n",
146  FFMAX(0, hls->sequence - hls->size));
147 
148  for (en = hls->list; en; en = en->next) {
149  avio_printf(hls->pb, "#EXTINF:%d,\n", en->duration);
150  avio_printf(hls->pb, "%s\n", en->name);
151  }
152 
153  if (last)
154  avio_printf(hls->pb, "#EXT-X-ENDLIST\n");
155 
156 fail:
157  avio_closep(&hls->pb);
158  return ret;
159 }
160 
162 {
163  HLSContext *c = s->priv_data;
164  AVFormatContext *oc = c->avf;
165  int err = 0;
166 
167  if (c->wrap)
168  c->number %= c->wrap;
169 
170  if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
171  c->basename, c->number++) < 0) {
172  av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", c->basename);
173  return AVERROR(EINVAL);
174  }
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 ret, is_ref_pkt = 0;
257 
258  if (hls->start_pts == AV_NOPTS_VALUE) {
259  hls->start_pts = pkt->pts;
260  hls->end_pts = pkt->pts;
261  }
262 
263  if ((hls->has_video && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
264  pkt->pts != AV_NOPTS_VALUE) {
265  is_ref_pkt = 1;
266  hls->duration = av_rescale(pkt->pts - hls->end_pts,
267  st->time_base.num, st->time_base.den);
268  }
269 
270  if (is_ref_pkt &&
271  av_compare_ts(pkt->pts - hls->start_pts, st->time_base,
272  end_pts, AV_TIME_BASE_Q) >= 0 &&
273  pkt->flags & AV_PKT_FLAG_KEY) {
274 
275  ret = append_entry(hls, hls->duration);
276  if (ret)
277  return ret;
278 
279  hls->end_pts = pkt->pts;
280  hls->duration = 0;
281 
282  av_write_frame(oc, NULL); /* Flush any buffered data */
283  avio_close(oc->pb);
284 
285  ret = hls_start(s);
286 
287  if (ret)
288  return ret;
289 
290  oc = hls->avf;
291 
292  if ((ret = hls_window(s, 0)) < 0)
293  return ret;
294  }
295 
296  ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
297 
298  return ret;
299 }
300 
301 static int hls_write_trailer(struct AVFormatContext *s)
302 {
303  HLSContext *hls = s->priv_data;
304  AVFormatContext *oc = hls->avf;
305 
306  av_write_trailer(oc);
307  avio_closep(&oc->pb);
309  av_free(hls->basename);
310  append_entry(hls, hls->duration);
311  hls_window(s, 1);
312 
313  free_entries(hls);
314  avio_close(hls->pb);
315  return 0;
316 }
317 
318 #define OFFSET(x) offsetof(HLSContext, x)
319 #define E AV_OPT_FLAG_ENCODING_PARAM
320 static const AVOption options[] = {
321  {"start_number", "set first number in the sequence", OFFSET(sequence),AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, E},
322  {"hls_time", "set segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E},
323  {"hls_list_size", "set maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E},
324  {"hls_wrap", "set number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E},
325  { NULL },
326 };
327 
328 static const AVClass hls_class = {
329  .class_name = "hls muxer",
330  .item_name = av_default_item_name,
331  .option = options,
332  .version = LIBAVUTIL_VERSION_INT,
333 };
334 
335 
337  .name = "hls",
338  .long_name = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
339  .extensions = "m3u8",
340  .priv_data_size = sizeof(HLSContext),
341  .audio_codec = AV_CODEC_ID_MP2,
342  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
347  .priv_class = &hls_class,
348 };