43 #define LINE_BUFFER_SIZE 1024
133 float playlist_duration = 0.0f;
134 int ret = 0, path_size, sub_path_size;
135 char *dirname =
NULL, *p, *sub_path;
140 playlist_duration += segment->
duration;
141 segment = segment->
next;
146 playlist_duration -= segment->
duration;
147 previous_segment = segment;
148 segment = previous_segment->
next;
149 if (playlist_duration <= -previous_segment->
duration) {
172 path_size = strlen(dirname) + strlen(segment->
filename) + 1;
181 if (unlink(path) < 0) {
183 path, strerror(errno));
187 sub_path_size = strlen(dirname) + strlen(segment->
sub_filename) + 1;
196 if (unlink(sub_path) < 0) {
198 sub_path, strerror(errno));
203 previous_segment = segment;
204 segment = previous_segment->
next;
256 if (ret !=
sizeof(key)) {
317 int64_t pos, int64_t
size)
321 const char *pl_dir, *filename;
340 if (strstr(p, pl_dir) == p)
341 filename = hls->
avf->
filename + strlen(pl_dir) + 1;
412 int target_duration = 0;
416 char temp_filename[1024];
420 int use_rename = proto && !strcmp(proto,
"file");
421 static unsigned warned_non_file;
422 char *key_uri =
NULL;
423 char *iv_string =
NULL;
426 if (!use_rename && !warned_non_file++)
427 av_log(s,
AV_LOG_ERROR,
"Cannot use rename on non file protocol, this may lead to races and temporarly partial files\n");
430 snprintf(temp_filename,
sizeof(temp_filename), use_rename ?
"%s.tmp" :
"%s", s->
filename);
436 target_duration = ceil(en->
duration);
445 avio_printf(out,
"#EXT-X-TARGETDURATION:%d\n", target_duration);
446 avio_printf(out,
"#EXT-X-MEDIA-SEQUENCE:%"PRId64
"\n", sequence);
475 avio_printf(out,
"#EXT-X-BYTERANGE:%"PRIi64
"@%"PRIi64
"\n",
489 avio_printf(sub_out,
"#EXT-X-VERSION:%d\n", version);
493 avio_printf(sub_out,
"#EXT-X-TARGETDURATION:%d\n", target_duration);
494 avio_printf(sub_out,
"#EXT-X-MEDIA-SEQUENCE:%"PRId64
"\n", sequence);
502 avio_printf(sub_out,
"#EXT-X-BYTERANGE:%"PRIi64
"@%"PRIi64
"\n",
518 if (ret >= 0 && use_rename)
529 char *filename, iv_string[
KEYSIZE*2 + 1];
541 struct tm *tm, tmpbuf;
556 if (mkdir(dir, 0777) == -1 && errno != EEXIST) {
557 av_log(oc,
AV_LOG_ERROR,
"Could not create directory %s with use_localtime_mkdir\n", dir);
589 if ((err =
av_dict_set(&options,
"encryption_iv", iv_string, 0)) < 0)
616 snprintf(period,
sizeof(period),
"%d", (INT_MAX / 2) - 1);
641 const char *pattern =
"%d.ts";
642 const char *pattern_localtime_fmt =
"-%s.ts";
643 const char *vtt_pattern =
"%d.vtt";
646 int vtt_basename_size;
669 "More than a single video stream present, "
670 "expect issues decoding it.\n");
698 basename_size = strlen(s->
filename) + strlen(pattern_localtime_fmt) + 1;
700 basename_size = strlen(s->
filename) + strlen(pattern) + 1;
723 vtt_pattern =
".vtt";
724 vtt_basename_size = strlen(s->
filename) + strlen(vtt_pattern) + 1;
799 int ret, can_split = 1;
800 int stream_index = 0;
820 is_ref_pkt = can_split = 0;
828 int64_t new_start_pos;
906 #define OFFSET(x) offsetof(HLSContext, x)
907 #define E AV_OPT_FLAG_ENCODING_PARAM
911 {
"hls_list_size",
"set maximum number of playlist entries",
OFFSET(max_nb_segments),
AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX,
E},
912 {
"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},
913 {
"hls_vtt_options",
"set hls vtt list of options for the container format used for hls",
OFFSET(vtt_format_options_str),
AV_OPT_TYPE_STRING, {.str =
NULL}, 0, 0,
E},
915 {
"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},
920 {
"hls_flags",
"set flags affecting HLS playlist and media file generation",
OFFSET(
flags),
AV_OPT_TYPE_FLAGS, {.i64 = 0 }, 0, UINT_MAX,
E,
"flags"},
926 {
"use_localtime",
"set filename expansion with strftime at segment creation",
OFFSET(use_localtime),
AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1,
E },
927 {
"use_localtime_mkdir",
"create last directory component in strftime-generated filename",
OFFSET(use_localtime_mkdir),
AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1,
E },
947 .extensions =
"m3u8",
956 .priv_class = &hls_class,
char key_uri[LINE_BUFFER_SIZE+1]
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
static int hls_write_trailer(struct AVFormatContext *s)
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
char * vtt_format_options_str
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
int use_localtime_mkdir
flag to mkdir dirname in timebased filename
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
#define AVIO_FLAG_READ
read-only
#define AVIO_FLAG_WRITE
write-only
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
static int hls_window(AVFormatContext *s, int last)
static void set_http_options(AVDictionary **options, HLSContext *c)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
const char * av_basename(const char *path)
Thread safe basename.
static int hls_append_segment(struct AVFormatContext *s, HLSContext *hls, double duration, int64_t pos, int64_t size)
miscellaneous OS support macros and functions.
static void hls_free_segments(HLSSegment *p)
HLSSegment * old_segments
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
AVStream ** streams
A list of all streams in the file.
#define AVERROR_EOF
End of file.
char * format_options_str
#define AV_LOG_VERBOSE
Detailed information.
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
struct AVOutputFormat * oformat
The output container format.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
static const AVOption options[]
static int hls_write_header(AVFormatContext *s)
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
AVDictionary * metadata
Metadata that applies to the whole file.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
enum AVMediaType codec_type
General type of the encoded data.
AVDictionary * vtt_format_options
simple assert() macros that are a bit more flexible than ISO C assert().
char key_uri[LINE_BUFFER_SIZE+1]
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.
int flags
A combination of AV_PKT_FLAG values.
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare 2 timestamps each in its own timebases.
char * av_asprintf(const char *fmt,...)
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
char key_string[KEYSIZE *2+1]
char filename[1024]
input or output filename
#define AV_TIME_BASE
Internal time base represented as integer.
av_warn_unused_result int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
static struct tm * localtime_r(const time_t *clock, struct tm *result)
static int write_trailer(AVFormatContext *s1)
static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
HLSSegment * last_segment
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...
AVDictionary * format_options
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
Read a whole line of text from AVIOContext.
int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
Return in 'buf' the path with 'd' replaced by a number.
static int hls_delete_old_segments(HLSContext *hls)
int use_localtime
flag to expand filename with localtime
int av_dict_parse_string(AVDictionary **pm, const char *str, const char *key_val_sep, const char *pairs_sep, int flags)
Parse the key/value pairs list and add the parsed entries to a dictionary.
char iv_string[KEYSIZE *2+1]
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
char * av_strdup(const char *s)
Duplicate the string s.
AVIOContext * pb
I/O context.
static int hls_encryption_start(AVFormatContext *s)
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Describe the class of an AVClass context structure.
char iv_string[KEYSIZE *2+1]
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
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...
const char * avio_find_protocol_name(const char *url)
Return the name of the protocol that will handle the passed URL.
char key_file[LINE_BUFFER_SIZE+1]
static int hls_mux_init(AVFormatContext *s)
const char * av_dirname(char *path)
Thread safe dirname.
int pts_wrap_bits
number of bits in pts (used for wrapping control)
static int hls_start(AVFormatContext *s)
void * priv_data
Format private data.
static const uint8_t start_sequence[]
static void write_header(FFV1Context *f)
AVOutputFormat ff_hls_muxer
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
#define AVERROR_MUXER_NOT_FOUND
Muxer not found.
AVCodecParameters * codecpar
AVOutputFormat * vtt_oformat
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
static const AVClass hls_class
int(* io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **options)
This structure stores compressed data.
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
void(* io_close)(struct AVFormatContext *s, AVIOContext *pb)
A callback for closing the streams opened with AVFormatContext.io_open().
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define AV_NOPTS_VALUE
Undefined timestamp value.
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
AVFormatContext * vtt_avf