]> git.sesse.net Git - ffmpeg/blob - libavformat/hlsenc.c
Merge commit '21c90d86d27c2143354c7d782050a779b0986eb1'
[ffmpeg] / libavformat / hlsenc.c
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 #define KEYSIZE 16
41 #define LINE_BUFFER_SIZE 1024
42
43 typedef struct HLSSegment {
44     char filename[1024];
45     double duration; /* in seconds */
46     int64_t pos;
47     int64_t size;
48
49     char key_uri[LINE_BUFFER_SIZE + 1];
50     char iv_string[KEYSIZE*2 + 1];
51
52     struct HLSSegment *next;
53 } HLSSegment;
54
55 typedef enum HLSFlags {
56     // Generate a single media file and use byte ranges in the playlist.
57     HLS_SINGLE_FILE = (1 << 0),
58     HLS_DELETE_SEGMENTS = (1 << 1),
59     HLS_ROUND_DURATIONS = (1 << 2),
60     HLS_DISCONT_START = (1 << 3),
61     HLS_OMIT_ENDLIST = (1 << 4),
62 } HLSFlags;
63
64 typedef struct HLSContext {
65     const AVClass *class;  // Class for private options.
66     unsigned number;
67     int64_t sequence;
68     int64_t start_sequence;
69     AVOutputFormat *oformat;
70
71     AVFormatContext *avf;
72
73     float time;            // Set by a private option.
74     int max_nb_segments;   // Set by a private option.
75     int  wrap;             // Set by a private option.
76     uint32_t flags;        // enum HLSFlags
77     char *segment_filename;
78
79     int allowcache;
80     int64_t recording_time;
81     int has_video;
82     int64_t start_pts;
83     int64_t end_pts;
84     double duration;      // last segment duration computed so far, in seconds
85     int64_t start_pos;    // last segment starting position
86     int64_t size;         // last segment size
87     int nb_entries;
88     int discontinuity_set;
89
90     HLSSegment *segments;
91     HLSSegment *last_segment;
92     HLSSegment *old_segments;
93
94     char *basename;
95     char *baseurl;
96     char *format_options_str;
97     AVDictionary *format_options;
98
99     char *key_info_file;
100     char key_file[LINE_BUFFER_SIZE + 1];
101     char key_uri[LINE_BUFFER_SIZE + 1];
102     char key_string[KEYSIZE*2 + 1];
103     char iv_string[KEYSIZE*2 + 1];
104 } HLSContext;
105
106 static int hls_delete_old_segments(HLSContext *hls) {
107
108     HLSSegment *segment, *previous_segment = NULL;
109     float playlist_duration = 0.0f;
110     int ret = 0, path_size;
111     char *dirname = NULL, *p, *path;
112
113     segment = hls->segments;
114     while (segment) {
115         playlist_duration += segment->duration;
116         segment = segment->next;
117     }
118
119     segment = hls->old_segments;
120     while (segment) {
121         playlist_duration -= segment->duration;
122         previous_segment = segment;
123         segment = previous_segment->next;
124         if (playlist_duration <= -previous_segment->duration) {
125             previous_segment->next = NULL;
126             break;
127         }
128     }
129
130     if (segment) {
131         if (hls->segment_filename) {
132             dirname = av_strdup(hls->segment_filename);
133         } else {
134             dirname = av_strdup(hls->avf->filename);
135         }
136         if (!dirname) {
137             ret = AVERROR(ENOMEM);
138             goto fail;
139         }
140         p = (char *)av_basename(dirname);
141         *p = '\0';
142     }
143
144     while (segment) {
145         av_log(hls, AV_LOG_DEBUG, "deleting old segment %s\n",
146                                   segment->filename);
147         path_size = strlen(dirname) + strlen(segment->filename) + 1;
148         path = av_malloc(path_size);
149         if (!path) {
150             ret = AVERROR(ENOMEM);
151             goto fail;
152         }
153         av_strlcpy(path, dirname, path_size);
154         av_strlcat(path, segment->filename, path_size);
155         if (unlink(path) < 0) {
156             av_log(hls, AV_LOG_ERROR, "failed to delete old segment %s: %s\n",
157                                      path, strerror(errno));
158         }
159         av_free(path);
160         previous_segment = segment;
161         segment = previous_segment->next;
162         av_free(previous_segment);
163     }
164
165 fail:
166     av_free(dirname);
167
168     return ret;
169 }
170
171 static int hls_encryption_start(AVFormatContext *s)
172 {
173     HLSContext *hls = s->priv_data;
174     int ret;
175     AVIOContext *pb;
176     uint8_t key[KEYSIZE];
177
178     if ((ret = avio_open2(&pb, hls->key_info_file, AVIO_FLAG_READ,
179                            &s->interrupt_callback, NULL)) < 0) {
180         av_log(hls, AV_LOG_ERROR,
181                 "error opening key info file %s\n", hls->key_info_file);
182         return ret;
183     }
184
185     ff_get_line(pb, hls->key_uri, sizeof(hls->key_uri));
186     hls->key_uri[strcspn(hls->key_uri, "\r\n")] = '\0';
187
188     ff_get_line(pb, hls->key_file, sizeof(hls->key_file));
189     hls->key_file[strcspn(hls->key_file, "\r\n")] = '\0';
190
191     ff_get_line(pb, hls->iv_string, sizeof(hls->iv_string));
192     hls->iv_string[strcspn(hls->iv_string, "\r\n")] = '\0';
193
194     avio_close(pb);
195
196     if (!*hls->key_uri) {
197         av_log(hls, AV_LOG_ERROR, "no key URI specified in key info file\n");
198         return AVERROR(EINVAL);
199     }
200
201     if (!*hls->key_file) {
202         av_log(hls, AV_LOG_ERROR, "no key file specified in key info file\n");
203         return AVERROR(EINVAL);
204     }
205
206     if ((ret = avio_open2(&pb, hls->key_file, AVIO_FLAG_READ,
207                            &s->interrupt_callback, NULL)) < 0) {
208         av_log(hls, AV_LOG_ERROR, "error opening key file %s\n", hls->key_file);
209         return ret;
210     }
211
212     ret = avio_read(pb, key, sizeof(key));
213     avio_close(pb);
214     if (ret != sizeof(key)) {
215         av_log(hls, AV_LOG_ERROR, "error reading key file %s\n", hls->key_file);
216         if (ret >= 0 || ret == AVERROR_EOF)
217             ret = AVERROR(EINVAL);
218         return ret;
219     }
220     ff_data_to_hex(hls->key_string, key, sizeof(key), 0);
221
222     return 0;
223 }
224
225 static int hls_mux_init(AVFormatContext *s)
226 {
227     HLSContext *hls = s->priv_data;
228     AVFormatContext *oc;
229     int i, ret;
230
231     ret = avformat_alloc_output_context2(&hls->avf, hls->oformat, NULL, NULL);
232     if (ret < 0)
233         return ret;
234     oc = hls->avf;
235
236     oc->oformat            = hls->oformat;
237     oc->interrupt_callback = s->interrupt_callback;
238     oc->max_delay          = s->max_delay;
239     av_dict_copy(&oc->metadata, s->metadata, 0);
240
241     for (i = 0; i < s->nb_streams; i++) {
242         AVStream *st;
243         if (!(st = avformat_new_stream(oc, NULL)))
244             return AVERROR(ENOMEM);
245         avcodec_copy_context(st->codec, s->streams[i]->codec);
246         st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
247         st->time_base = s->streams[i]->time_base;
248     }
249     hls->start_pos = 0;
250
251     return 0;
252 }
253
254 /* Create a new segment and append it to the segment list */
255 static int hls_append_segment(HLSContext *hls, double duration, int64_t pos,
256                               int64_t size)
257 {
258     HLSSegment *en = av_malloc(sizeof(*en));
259     int ret;
260
261     if (!en)
262         return AVERROR(ENOMEM);
263
264     av_strlcpy(en->filename, av_basename(hls->avf->filename), sizeof(en->filename));
265
266     en->duration = duration;
267     en->pos      = pos;
268     en->size     = size;
269     en->next     = NULL;
270
271     if (hls->key_info_file) {
272         av_strlcpy(en->key_uri, hls->key_uri, sizeof(en->key_uri));
273         av_strlcpy(en->iv_string, hls->iv_string, sizeof(en->iv_string));
274     }
275
276     if (!hls->segments)
277         hls->segments = en;
278     else
279         hls->last_segment->next = en;
280
281     hls->last_segment = en;
282
283     if (hls->max_nb_segments && hls->nb_entries >= hls->max_nb_segments) {
284         en = hls->segments;
285         hls->segments = en->next;
286         if (en && hls->flags & HLS_DELETE_SEGMENTS &&
287                 !(hls->flags & HLS_SINGLE_FILE || hls->wrap)) {
288             en->next = hls->old_segments;
289             hls->old_segments = en;
290             if ((ret = hls_delete_old_segments(hls)) < 0)
291                 return ret;
292         } else
293             av_free(en);
294     } else
295         hls->nb_entries++;
296
297     hls->sequence++;
298
299     return 0;
300 }
301
302 static void hls_free_segments(HLSSegment *p)
303 {
304     HLSSegment *en;
305
306     while(p) {
307         en = p;
308         p = p->next;
309         av_free(en);
310     }
311 }
312
313 static int hls_window(AVFormatContext *s, int last)
314 {
315     HLSContext *hls = s->priv_data;
316     HLSSegment *en;
317     int target_duration = 0;
318     int ret = 0;
319     AVIOContext *out = NULL;
320     char temp_filename[1024];
321     int64_t sequence = FFMAX(hls->start_sequence, hls->sequence - hls->nb_entries);
322     int version = hls->flags & HLS_SINGLE_FILE ? 4 : 3;
323     const char *proto = avio_find_protocol_name(s->filename);
324     int use_rename = proto && !strcmp(proto, "file");
325     static unsigned warned_non_file;
326     char *key_uri = NULL;
327     char *iv_string = NULL;
328
329     if (!use_rename && !warned_non_file++)
330         av_log(s, AV_LOG_ERROR, "Cannot use rename on non file protocol, this may lead to races and temporarly partial files\n");
331
332     snprintf(temp_filename, sizeof(temp_filename), use_rename ? "%s.tmp" : "%s", s->filename);
333     if ((ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE,
334                           &s->interrupt_callback, NULL)) < 0)
335         goto fail;
336
337     for (en = hls->segments; en; en = en->next) {
338         if (target_duration < en->duration)
339             target_duration = ceil(en->duration);
340     }
341
342     hls->discontinuity_set = 0;
343     avio_printf(out, "#EXTM3U\n");
344     avio_printf(out, "#EXT-X-VERSION:%d\n", version);
345     if (hls->allowcache == 0 || hls->allowcache == 1) {
346         avio_printf(out, "#EXT-X-ALLOW-CACHE:%s\n", hls->allowcache == 0 ? "NO" : "YES");
347     }
348     avio_printf(out, "#EXT-X-TARGETDURATION:%d\n", target_duration);
349     avio_printf(out, "#EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
350
351     av_log(s, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%"PRId64"\n",
352            sequence);
353     if((hls->flags & HLS_DISCONT_START) && sequence==hls->start_sequence && hls->discontinuity_set==0 ){
354         avio_printf(out, "#EXT-X-DISCONTINUITY\n");
355         hls->discontinuity_set = 1;
356     }
357     for (en = hls->segments; en; en = en->next) {
358         if (hls->key_info_file && (!key_uri || strcmp(en->key_uri, key_uri) ||
359                                     av_strcasecmp(en->iv_string, iv_string))) {
360             avio_printf(out, "#EXT-X-KEY:METHOD=AES-128,URI=\"%s\"", en->key_uri);
361             if (*en->iv_string)
362                 avio_printf(out, ",IV=0x%s", en->iv_string);
363             avio_printf(out, "\n");
364             key_uri = en->key_uri;
365             iv_string = en->iv_string;
366         }
367
368         if (hls->flags & HLS_ROUND_DURATIONS)
369             avio_printf(out, "#EXTINF:%d,\n",  (int)round(en->duration));
370         else
371             avio_printf(out, "#EXTINF:%f,\n", en->duration);
372         if (hls->flags & HLS_SINGLE_FILE)
373              avio_printf(out, "#EXT-X-BYTERANGE:%"PRIi64"@%"PRIi64"\n",
374                          en->size, en->pos);
375         if (hls->baseurl)
376             avio_printf(out, "%s", hls->baseurl);
377         avio_printf(out, "%s\n", en->filename);
378     }
379
380     if (last && (hls->flags & HLS_OMIT_ENDLIST)==0)
381         avio_printf(out, "#EXT-X-ENDLIST\n");
382
383 fail:
384     avio_closep(&out);
385     if (ret >= 0 && use_rename)
386         ff_rename(temp_filename, s->filename, s);
387     return ret;
388 }
389
390 static int hls_start(AVFormatContext *s)
391 {
392     HLSContext *c = s->priv_data;
393     AVFormatContext *oc = c->avf;
394     AVDictionary *options = NULL;
395     char *filename, iv_string[KEYSIZE*2 + 1];
396     int err = 0;
397
398     if (c->flags & HLS_SINGLE_FILE)
399         av_strlcpy(oc->filename, c->basename,
400                    sizeof(oc->filename));
401     else
402         if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
403                                   c->basename, c->wrap ? c->sequence % c->wrap : c->sequence) < 0) {
404             av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", c->basename);
405             return AVERROR(EINVAL);
406         }
407     c->number++;
408
409     if (c->key_info_file) {
410         if ((err = hls_encryption_start(s)) < 0)
411             return err;
412         if ((err = av_dict_set(&options, "encryption_key", c->key_string, 0))
413                 < 0)
414             return err;
415         err = av_strlcpy(iv_string, c->iv_string, sizeof(iv_string));
416         if (!err)
417             snprintf(iv_string, sizeof(iv_string), "%032"PRIx64, c->sequence);
418         if ((err = av_dict_set(&options, "encryption_iv", iv_string, 0)) < 0)
419             return err;
420
421         filename = av_asprintf("crypto:%s", oc->filename);
422         if (!filename) {
423             av_dict_free(&options);
424             return AVERROR(ENOMEM);
425         }
426         err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE,
427                          &s->interrupt_callback, &options);
428         av_free(filename);
429         av_dict_free(&options);
430         if (err < 0)
431             return err;
432     } else
433         if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
434                           &s->interrupt_callback, NULL)) < 0)
435             return err;
436
437     if (oc->oformat->priv_class && oc->priv_data)
438         av_opt_set(oc->priv_data, "mpegts_flags", "resend_headers", 0);
439
440     return 0;
441 }
442
443 static int hls_write_header(AVFormatContext *s)
444 {
445     HLSContext *hls = s->priv_data;
446     int ret, i;
447     char *p;
448     const char *pattern = "%d.ts";
449     AVDictionary *options = NULL;
450     int basename_size;
451
452     hls->sequence       = hls->start_sequence;
453     hls->recording_time = hls->time * AV_TIME_BASE;
454     hls->start_pts      = AV_NOPTS_VALUE;
455
456     if (hls->format_options_str) {
457         ret = av_dict_parse_string(&hls->format_options, hls->format_options_str, "=", ":", 0);
458         if (ret < 0) {
459             av_log(s, AV_LOG_ERROR, "Could not parse format options list '%s'\n", hls->format_options_str);
460             goto fail;
461         }
462     }
463
464     for (i = 0; i < s->nb_streams; i++)
465         hls->has_video +=
466             s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO;
467
468     if (hls->has_video > 1)
469         av_log(s, AV_LOG_WARNING,
470                "More than a single video stream present, "
471                "expect issues decoding it.\n");
472
473     hls->oformat = av_guess_format("mpegts", NULL, NULL);
474
475     if (!hls->oformat) {
476         ret = AVERROR_MUXER_NOT_FOUND;
477         goto fail;
478     }
479
480     if (hls->segment_filename) {
481         hls->basename = av_strdup(hls->segment_filename);
482         if (!hls->basename) {
483             ret = AVERROR(ENOMEM);
484             goto fail;
485         }
486     } else {
487         if (hls->flags & HLS_SINGLE_FILE)
488             pattern = ".ts";
489
490         basename_size = strlen(s->filename) + strlen(pattern) + 1;
491         hls->basename = av_malloc(basename_size);
492         if (!hls->basename) {
493             ret = AVERROR(ENOMEM);
494             goto fail;
495         }
496
497         av_strlcpy(hls->basename, s->filename, basename_size);
498
499         p = strrchr(hls->basename, '.');
500         if (p)
501             *p = '\0';
502         av_strlcat(hls->basename, pattern, basename_size);
503     }
504
505     if ((ret = hls_mux_init(s)) < 0)
506         goto fail;
507
508     if ((ret = hls_start(s)) < 0)
509         goto fail;
510
511     av_dict_copy(&options, hls->format_options, 0);
512     ret = avformat_write_header(hls->avf, &options);
513     if (av_dict_count(options)) {
514         av_log(s, AV_LOG_ERROR, "Some of provided format options in '%s' are not recognized\n", hls->format_options_str);
515         ret = AVERROR(EINVAL);
516         goto fail;
517     }
518     av_assert0(s->nb_streams == hls->avf->nb_streams);
519     for (i = 0; i < s->nb_streams; i++) {
520         AVStream *inner_st  = hls->avf->streams[i];
521         AVStream *outer_st = s->streams[i];
522         avpriv_set_pts_info(outer_st, inner_st->pts_wrap_bits, inner_st->time_base.num, inner_st->time_base.den);
523     }
524 fail:
525
526     av_dict_free(&options);
527     if (ret < 0) {
528         av_freep(&hls->basename);
529         if (hls->avf)
530             avformat_free_context(hls->avf);
531     }
532     return ret;
533 }
534
535 static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
536 {
537     HLSContext *hls = s->priv_data;
538     AVFormatContext *oc = hls->avf;
539     AVStream *st = s->streams[pkt->stream_index];
540     int64_t end_pts = hls->recording_time * hls->number;
541     int is_ref_pkt = 1;
542     int ret, can_split = 1;
543
544     if (hls->start_pts == AV_NOPTS_VALUE) {
545         hls->start_pts = pkt->pts;
546         hls->end_pts   = pkt->pts;
547     }
548
549     if (hls->has_video) {
550         can_split = st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
551                     pkt->flags & AV_PKT_FLAG_KEY;
552         is_ref_pkt = st->codec->codec_type == AVMEDIA_TYPE_VIDEO;
553     }
554     if (pkt->pts == AV_NOPTS_VALUE)
555         is_ref_pkt = can_split = 0;
556
557     if (is_ref_pkt)
558         hls->duration = (double)(pkt->pts - hls->end_pts)
559                                    * st->time_base.num / st->time_base.den;
560
561     if (can_split && av_compare_ts(pkt->pts - hls->start_pts, st->time_base,
562                                    end_pts, AV_TIME_BASE_Q) >= 0) {
563         int64_t new_start_pos;
564         av_write_frame(oc, NULL); /* Flush any buffered data */
565
566         new_start_pos = avio_tell(hls->avf->pb);
567         hls->size = new_start_pos - hls->start_pos;
568         ret = hls_append_segment(hls, hls->duration, hls->start_pos, hls->size);
569         hls->start_pos = new_start_pos;
570         if (ret < 0)
571             return ret;
572
573         hls->end_pts = pkt->pts;
574         hls->duration = 0;
575
576         if (hls->flags & HLS_SINGLE_FILE) {
577             if (hls->avf->oformat->priv_class && hls->avf->priv_data)
578                 av_opt_set(hls->avf->priv_data, "mpegts_flags", "resend_headers", 0);
579             hls->number++;
580         } else {
581             avio_closep(&oc->pb);
582
583             ret = hls_start(s);
584         }
585
586         if (ret < 0)
587             return ret;
588
589         oc = hls->avf;
590
591         if ((ret = hls_window(s, 0)) < 0)
592             return ret;
593     }
594
595     ret = ff_write_chained(oc, pkt->stream_index, pkt, s, 0);
596
597     return ret;
598 }
599
600 static int hls_write_trailer(struct AVFormatContext *s)
601 {
602     HLSContext *hls = s->priv_data;
603     AVFormatContext *oc = hls->avf;
604
605     av_write_trailer(oc);
606     if (oc->pb) {
607         hls->size = avio_tell(hls->avf->pb) - hls->start_pos;
608         avio_closep(&oc->pb);
609         hls_append_segment(hls, hls->duration, hls->start_pos, hls->size);
610     }
611     av_freep(&hls->basename);
612     avformat_free_context(oc);
613     hls->avf = NULL;
614     hls_window(s, 1);
615
616     hls_free_segments(hls->segments);
617     hls_free_segments(hls->old_segments);
618     return 0;
619 }
620
621 #define OFFSET(x) offsetof(HLSContext, x)
622 #define E AV_OPT_FLAG_ENCODING_PARAM
623 static const AVOption options[] = {
624     {"start_number",  "set first number in the sequence",        OFFSET(start_sequence),AV_OPT_TYPE_INT64,  {.i64 = 0},     0, INT64_MAX, E},
625     {"hls_time",      "set segment length in seconds",           OFFSET(time),    AV_OPT_TYPE_FLOAT,  {.dbl = 2},     0, FLT_MAX, E},
626     {"hls_list_size", "set maximum number of playlist entries",  OFFSET(max_nb_segments),    AV_OPT_TYPE_INT,    {.i64 = 5},     0, INT_MAX, E},
627     {"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},
628     {"hls_wrap",      "set number after which the index wraps",  OFFSET(wrap),    AV_OPT_TYPE_INT,    {.i64 = 0},     0, INT_MAX, E},
629     {"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},
630     {"hls_base_url",  "url to prepend to each playlist entry",   OFFSET(baseurl), AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       E},
631     {"hls_segment_filename", "filename template for segment files", OFFSET(segment_filename),   AV_OPT_TYPE_STRING, {.str = NULL},            0,       0,         E},
632     {"hls_key_info_file",    "file with key URI and key file path", OFFSET(key_info_file),      AV_OPT_TYPE_STRING, {.str = NULL},            0,       0,         E},
633     {"hls_flags",     "set flags affecting HLS playlist and media file generation", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0 }, 0, UINT_MAX, E, "flags"},
634     {"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"},
635     {"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"},
636     {"round_durations", "round durations in m3u8 to whole numbers", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_ROUND_DURATIONS }, 0, UINT_MAX,   E, "flags"},
637     {"discont_start", "start the playlist with a discontinuity tag", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_DISCONT_START }, 0, UINT_MAX,   E, "flags"},
638     {"omit_endlist", "Do not append an endlist when ending stream", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_OMIT_ENDLIST }, 0, UINT_MAX,   E, "flags"},
639
640     { NULL },
641 };
642
643 static const AVClass hls_class = {
644     .class_name = "hls muxer",
645     .item_name  = av_default_item_name,
646     .option     = options,
647     .version    = LIBAVUTIL_VERSION_INT,
648 };
649
650
651 AVOutputFormat ff_hls_muxer = {
652     .name           = "hls",
653     .long_name      = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
654     .extensions     = "m3u8",
655     .priv_data_size = sizeof(HLSContext),
656     .audio_codec    = AV_CODEC_ID_AAC,
657     .video_codec    = AV_CODEC_ID_H264,
658     .flags          = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
659     .write_header   = hls_write_header,
660     .write_packet   = hls_write_packet,
661     .write_trailer  = hls_write_trailer,
662     .priv_class     = &hls_class,
663 };