Libav
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 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 
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  int64_t duration; // segment duration in AV_TIME_BASE units
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;
44  int64_t start_sequence;
47  float time; // Set by a private option.
48  int size; // Set by a private option.
49  int wrap; // Set by a private option.
50  int version; // Set by a private option.
52  int64_t recording_time;
53  int has_video;
54  // The following timestamps are in AV_TIME_BASE units.
55  int64_t start_pts;
56  int64_t end_pts;
57  int64_t duration; // last segment duration computed so far.
61  char *basename;
62  char *baseurl;
63 } HLSContext;
64 
66 {
67  HLSContext *hls = s->priv_data;
68  AVFormatContext *oc;
69  int i;
70 
71  hls->avf = oc = avformat_alloc_context();
72  if (!oc)
73  return AVERROR(ENOMEM);
74 
75  oc->oformat = hls->oformat;
77  oc->opaque = s->opaque;
78  oc->io_open = s->io_open;
79  oc->io_close = s->io_close;
80 
81  for (i = 0; i < s->nb_streams; i++) {
82  AVStream *st;
83  if (!(st = avformat_new_stream(oc, NULL)))
84  return AVERROR(ENOMEM);
87  st->time_base = s->streams[i]->time_base;
88  }
89 
90  return 0;
91 }
92 
93 static int append_entry(HLSContext *hls, int64_t duration)
94 {
95  ListEntry *en = av_malloc(sizeof(*en));
96 
97  if (!en)
98  return AVERROR(ENOMEM);
99 
100  av_strlcpy(en->name, av_basename(hls->avf->filename), sizeof(en->name));
101 
102  en->duration = duration;
103  en->next = NULL;
104 
105  if (!hls->list)
106  hls->list = en;
107  else
108  hls->end_list->next = en;
109 
110  hls->end_list = en;
111 
112  if (hls->nb_entries >= hls->size) {
113  en = hls->list;
114  hls->list = en->next;
115  av_free(en);
116  } else
117  hls->nb_entries++;
118 
119  hls->sequence++;
120 
121  return 0;
122 }
123 
124 static void free_entries(HLSContext *hls)
125 {
126  ListEntry *p = hls->list, *en;
127 
128  while(p) {
129  en = p;
130  p = p->next;
131  av_free(en);
132  }
133 }
134 
135 static int hls_window(AVFormatContext *s, int last)
136 {
137  HLSContext *hls = s->priv_data;
138  ListEntry *en;
139  int64_t target_duration = 0;
140  int ret = 0;
141  AVIOContext *out = NULL;
142  char temp_filename[1024];
143  int64_t sequence = FFMAX(hls->start_sequence, hls->sequence - hls->size);
144 
145  snprintf(temp_filename, sizeof(temp_filename), "%s.tmp", s->filename);
146  if ((ret = s->io_open(s, &out, temp_filename, AVIO_FLAG_WRITE, NULL)) < 0)
147  goto fail;
148 
149  for (en = hls->list; en; en = en->next) {
150  if (target_duration < en->duration)
151  target_duration = en->duration;
152  }
153 
154  avio_printf(out, "#EXTM3U\n");
155  avio_printf(out, "#EXT-X-VERSION:%d\n", hls->version);
156  if (hls->allowcache == 0 || hls->allowcache == 1) {
157  avio_printf(out, "#EXT-X-ALLOW-CACHE:%s\n", hls->allowcache == 0 ? "NO" : "YES");
158  }
159  avio_printf(out, "#EXT-X-TARGETDURATION:%"PRId64"\n",
160  av_rescale_rnd(target_duration, 1, AV_TIME_BASE,
161  AV_ROUND_UP));
162  avio_printf(out, "#EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
163 
164  av_log(s, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%"PRId64"\n",
165  sequence);
166 
167  for (en = hls->list; en; en = en->next) {
168  if (hls->version > 2)
169  avio_printf(out, "#EXTINF:%f\n",
170  (double)en->duration / AV_TIME_BASE);
171  else
172  avio_printf(out, "#EXTINF:%"PRId64",\n",
173  av_rescale(en->duration, 1, AV_TIME_BASE));
174  if (hls->baseurl)
175  avio_printf(out, "%s", hls->baseurl);
176  avio_printf(out, "%s\n", en->name);
177  }
178 
179  if (last)
180  avio_printf(out, "#EXT-X-ENDLIST\n");
181 
182 fail:
183  ff_format_io_close(s, &out);
184  if (ret >= 0)
185  ff_rename(temp_filename, s->filename);
186  return ret;
187 }
188 
190 {
191  HLSContext *c = s->priv_data;
192  AVFormatContext *oc = c->avf;
193  int err = 0;
194 
195  if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
196  c->basename, c->wrap ? c->sequence % c->wrap : c->sequence) < 0)
197  return AVERROR(EINVAL);
198  c->number++;
199 
200  if ((err = s->io_open(s, &oc->pb, oc->filename, AVIO_FLAG_WRITE, NULL)) < 0)
201  return err;
202 
203  if (oc->oformat->priv_class && oc->priv_data)
204  av_opt_set(oc->priv_data, "mpegts_flags", "resend_headers", 0);
205 
206  return 0;
207 }
208 
210 {
211  HLSContext *hls = s->priv_data;
212  int ret, i;
213  char *p;
214  const char *pattern = "%d.ts";
215  int basename_size = strlen(s->filename) + strlen(pattern) + 1;
216 
217  hls->sequence = hls->start_sequence;
218  hls->recording_time = hls->time * AV_TIME_BASE;
219  hls->start_pts = AV_NOPTS_VALUE;
220 
221  for (i = 0; i < s->nb_streams; i++)
222  hls->has_video +=
224 
225  if (hls->has_video > 1)
227  "More than a single video stream present, "
228  "expect issues decoding it.\n");
229 
230  hls->oformat = av_guess_format("mpegts", NULL, NULL);
231 
232  if (!hls->oformat) {
234  goto fail;
235  }
236 
237  hls->basename = av_malloc(basename_size);
238 
239  if (!hls->basename) {
240  ret = AVERROR(ENOMEM);
241  goto fail;
242  }
243 
244  strcpy(hls->basename, s->filename);
245 
246  p = strrchr(hls->basename, '.');
247 
248  if (p)
249  *p = '\0';
250 
251  av_strlcat(hls->basename, pattern, basename_size);
252 
253  if ((ret = hls_mux_init(s)) < 0)
254  goto fail;
255 
256  if ((ret = hls_start(s)) < 0)
257  goto fail;
258 
259  if ((ret = avformat_write_header(hls->avf, NULL)) < 0)
260  return ret;
261 
262 
263 fail:
264  if (ret) {
265  av_free(hls->basename);
266  if (hls->avf)
268  }
269  return ret;
270 }
271 
273 {
274  HLSContext *hls = s->priv_data;
275  AVFormatContext *oc = hls->avf;
276  AVStream *st = s->streams[pkt->stream_index];
277  int64_t end_pts = hls->recording_time * hls->number;
278  int64_t pts = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q);
279  int ret, can_split = 1;
280 
281  if (hls->start_pts == AV_NOPTS_VALUE) {
282  hls->start_pts = pts;
283  hls->end_pts = pts;
284  }
285 
286  if (hls->has_video) {
287  can_split = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
288  pkt->flags & AV_PKT_FLAG_KEY;
289  }
290  if (pkt->pts == AV_NOPTS_VALUE)
291  can_split = 0;
292  else
293  hls->duration = pts - hls->end_pts;
294 
295  if (can_split && pts - hls->start_pts >= end_pts) {
296  ret = append_entry(hls, hls->duration);
297  if (ret)
298  return ret;
299 
300  hls->end_pts = pts;
301  hls->duration = 0;
302 
303  av_write_frame(oc, NULL); /* Flush any buffered data */
304  ff_format_io_close(s, &oc->pb);
305 
306  ret = hls_start(s);
307 
308  if (ret)
309  return ret;
310 
311  oc = hls->avf;
312 
313  if ((ret = hls_window(s, 0)) < 0)
314  return ret;
315  }
316 
317  ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
318 
319  return ret;
320 }
321 
322 static int hls_write_trailer(struct AVFormatContext *s)
323 {
324  HLSContext *hls = s->priv_data;
325  AVFormatContext *oc = hls->avf;
326 
327  av_write_trailer(oc);
328  ff_format_io_close(s, &oc->pb);
330  av_free(hls->basename);
331  append_entry(hls, hls->duration);
332  hls_window(s, 1);
333 
334  free_entries(hls);
335  return 0;
336 }
337 
338 #define OFFSET(x) offsetof(HLSContext, x)
339 #define E AV_OPT_FLAG_ENCODING_PARAM
340 static const AVOption options[] = {
341  {"start_number", "first number in the sequence", OFFSET(start_sequence),AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, E},
342  {"hls_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E},
343  {"hls_list_size", "maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E},
344  {"hls_wrap", "number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E},
345  {"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},
346  {"hls_base_url", "url to prepend to each playlist entry", OFFSET(baseurl), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
347  {"hls_version", "protocol version", OFFSET(version), AV_OPT_TYPE_INT, {.i64 = 3}, 2, 3, E},
348  { NULL },
349 };
350 
351 static const AVClass hls_class = {
352  .class_name = "hls muxer",
353  .item_name = av_default_item_name,
354  .option = options,
355  .version = LIBAVUTIL_VERSION_INT,
356 };
357 
358 
360  .name = "hls",
361  .long_name = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
362  .extensions = "m3u8",
363  .priv_data_size = sizeof(HLSContext),
364  .audio_codec = AV_CODEC_ID_AAC,
365  .video_codec = AV_CODEC_ID_H264,
370  .priv_class = &hls_class,
371 };
float time
Definition: hlsenc.c:47
int(* io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **options)
A callback for opening new IO streams.
Definition: avformat.h:1270
int wrap
Definition: hlsenc.c:49
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
Bytestream IO Context.
Definition: avio.h:104
int size
char * basename
Definition: hlsenc.c:61
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1181
AVOption.
Definition: opt.h:234
struct ListEntry * next
Definition: hlsenc.c:37
static int hls_write_trailer(struct AVFormatContext *s)
Definition: hlsenc.c:322
int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
Definition: mux.c:252
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:470
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:40
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:770
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:369
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:429
static int hls_window(AVFormatContext *s, int last)
Definition: hlsenc.c:135
Format I/O context.
Definition: avformat.h:940
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:39
ListEntry * list
Definition: hlsenc.c:59
const char * av_basename(const char *path)
Thread safe basename.
Definition: avstring.c:177
Round toward +infinity.
Definition: mathematics.h:53
AVOptions.
int64_t end_pts
Definition: hlsenc.c:56
void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
A wrapper around AVFormatContext.io_close that should be used instead of calling the pointer directly...
Definition: utils.c:3317
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:136
static int flags
Definition: log.c:50
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:140
static int append_entry(HLSContext *hls, int64_t duration)
Definition: hlsenc.c:93
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:64
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:959
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
static int ff_rename(const char *oldpath, const char *newpath)
Wrap errno on rename() error.
Definition: internal.h:431
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:99
static const AVOption options[]
Definition: hlsenc.c:340
static int hls_write_header(AVFormatContext *s)
Definition: hlsenc.c:209
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: utils.c:2819
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
#define OFFSET(x)
Definition: hlsenc.c:338
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
#define wrap(func)
Definition: neontest.h:62
int has_video
Definition: hlsenc.c:53
int64_t recording_time
Definition: hlsenc.c:52
#define FFMAX(a, b)
Definition: common.h:64
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:81
#define fail()
Definition: checkasm.h:80
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
void * opaque
Arbitrary user data set by the caller.
Definition: avformat.h:1248
char * baseurl
Definition: hlsenc.c:62
int size
Definition: hlsenc.c:48
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:996
unsigned number
Definition: hlsenc.c:42
static void free_entries(HLSContext *hls)
Definition: hlsenc.c:124
char filename[1024]
input or output filename
Definition: avformat.h:1016
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:86
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:241
int64_t duration
Definition: hlsenc.c:36
int64_t start_pts
Definition: hlsenc.c:55
Definition: hls.c:95
static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: hlsenc.c:272
const char * name
Definition: avformat.h:450
#define E
Definition: hlsenc.c:339
AVOutputFormat * av_guess_format(const char *short_name, const char *filename, const char *mime_type)
Return the output format in the list of registered output formats which best matches the provided par...
Definition: format.c:104
const AVClass * priv_class
AVClass for the private context.
Definition: avformat.h:478
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
Return in &#39;buf&#39; the path with &#39;d&#39; replaced by a number.
Definition: utils.c:2817
if(ac->has_optimized_func)
Stream structure.
Definition: avformat.h:705
NULL
Definition: eval.c:55
int64_t start_sequence
Definition: hlsenc.c:44
version
Definition: ffv1enc.c:1091
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:247
int64_t duration
Definition: hlsenc.c:57
AVIOContext * pb
I/O context.
Definition: avformat.h:982
av_default_item_name
Definition: dnxhdenc.c:55
AVOutputFormat * oformat
Definition: hlsenc.c:45
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
Definition: avconv.c:278
void(* io_close)(struct AVFormatContext *s, AVIOContext *pb)
A callback for closing the streams opened with AVFormatContext.io_open().
Definition: avformat.h:1276
int allowcache
Definition: hlsenc.c:51
Describe the class of an AVClass context structure.
Definition: log.h:34
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:2594
int64_t sequence
Definition: hlsenc.c:43
char name[1024]
Definition: hlsenc.c:35
misc parsing utilities
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes...
Definition: avstring.c:91
static int64_t pts
Global timestamp for the audio frames.
static int hls_mux_init(AVFormatContext *s)
Definition: hlsenc.c:65
int nb_entries
Definition: hlsenc.c:58
Main libavformat public API header.
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:412
int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt, AVFormatContext *src)
Write a packet to another muxer than the one the user originally intended.
Definition: mux.c:755
ListEntry * end_list
Definition: hlsenc.c:60
static int hls_start(AVFormatContext *s)
Definition: hlsenc.c:189
void * priv_data
Format private data.
Definition: avformat.h:968
static const uint8_t start_sequence[]
Definition: rtpdec_h264.c:65
int version
Definition: hlsenc.c:50
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:373
AVFormatContext * avf
Definition: hlsenc.c:46
AVOutputFormat ff_hls_muxer
Definition: hlsenc.c:359
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:714
FILE * out
Definition: movenc.c:54
#define AVERROR_MUXER_NOT_FOUND
Muxer not found.
Definition: error.h:55
AVCodecParameters * codecpar
Definition: avformat.h:831
int stream_index
Definition: avcodec.h:1348
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:742
static const AVClass hls_class
Definition: hlsenc.c:351
This structure stores compressed data.
Definition: avcodec.h:1323
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
Definition: opt.c:247
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1339
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:235
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2