43 #define LINE_BUFFER_SIZE 1024
124 float playlist_duration = 0.0f;
125 int ret = 0, path_size, sub_path_size;
126 char *dirname =
NULL, *p, *sub_path;
131 playlist_duration += segment->
duration;
132 segment = segment->
next;
137 playlist_duration -= segment->
duration;
138 previous_segment = segment;
139 segment = previous_segment->
next;
140 if (playlist_duration <= -previous_segment->
duration) {
163 path_size = strlen(dirname) + strlen(segment->
filename) + 1;
172 if (unlink(path) < 0) {
174 path, strerror(errno));
178 sub_path_size = strlen(dirname) + strlen(segment->
sub_filename) + 1;
187 if (unlink(sub_path) < 0) {
189 sub_path, strerror(errno));
194 previous_segment = segment;
195 segment = previous_segment->
next;
247 if (ret !=
sizeof(key)) {
380 int target_duration = 0;
384 char temp_filename[1024];
388 int use_rename = proto && !strcmp(proto,
"file");
389 static unsigned warned_non_file;
390 char *key_uri =
NULL;
391 char *iv_string =
NULL;
394 if (!use_rename && !warned_non_file++)
395 av_log(s,
AV_LOG_ERROR,
"Cannot use rename on non file protocol, this may lead to races and temporarly partial files\n");
398 snprintf(temp_filename,
sizeof(temp_filename), use_rename ?
"%s.tmp" :
"%s", s->
filename);
404 target_duration = ceil(en->
duration);
413 avio_printf(out,
"#EXT-X-TARGETDURATION:%d\n", target_duration);
414 avio_printf(out,
"#EXT-X-MEDIA-SEQUENCE:%"PRId64
"\n", sequence);
438 avio_printf(out,
"#EXT-X-BYTERANGE:%"PRIi64
"@%"PRIi64
"\n",
452 avio_printf(sub_out,
"#EXT-X-VERSION:%d\n", version);
456 avio_printf(sub_out,
"#EXT-X-TARGETDURATION:%d\n", target_duration);
457 avio_printf(sub_out,
"#EXT-X-MEDIA-SEQUENCE:%"PRId64
"\n", sequence);
465 avio_printf(sub_out,
"#EXT-X-BYTERANGE:%"PRIi64
"@%"PRIi64
"\n",
481 if (ret >= 0 && use_rename)
492 char *filename, iv_string[
KEYSIZE*2 + 1];
504 struct tm *tm, tmpbuf;
537 if ((err =
av_dict_set(&options,
"encryption_iv", iv_string, 0)) < 0)
564 snprintf(period,
sizeof(period),
"%d", (INT_MAX / 2) - 1);
589 const char *pattern =
"%d.ts";
590 const char *pattern_localtime_fmt =
"-%s.ts";
591 const char *vtt_pattern =
"%d.vtt";
594 int vtt_basename_size;
617 "More than a single video stream present, "
618 "expect issues decoding it.\n");
646 basename_size = strlen(s->
filename) + strlen(pattern_localtime_fmt) + 1;
648 basename_size = strlen(s->
filename) + strlen(pattern) + 1;
671 vtt_pattern =
".vtt";
672 vtt_basename_size = strlen(s->
filename) + strlen(vtt_pattern) + 1;
747 int ret, can_split = 1;
748 int stream_index = 0;
768 is_ref_pkt = can_split = 0;
776 int64_t new_start_pos;
854 #define OFFSET(x) offsetof(HLSContext, x)
855 #define E AV_OPT_FLAG_ENCODING_PARAM
859 {
"hls_list_size",
"set maximum number of playlist entries",
OFFSET(max_nb_segments),
AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX,
E},
860 {
"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},
861 {
"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},
863 {
"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},
868 {
"hls_flags",
"set flags affecting HLS playlist and media file generation",
OFFSET(
flags),
AV_OPT_TYPE_FLAGS, {.i64 = 0 }, 0, UINT_MAX,
E,
"flags"},
874 {
"use_localtime",
"set filename expansion with strftime at segment creation",
OFFSET(use_localtime),
AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1,
E },
891 .extensions =
"m3u8",
900 .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 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.
int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
Copy the settings of the source AVCodecContext into the destination AVCodecContext.
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.
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)
#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. ...
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.
AVCodecContext * codec
Codec context associated with this stream.
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]
enum AVMediaType codec_type
#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)
static int hls_append_segment(HLSContext *hls, double duration, int64_t pos, int64_t size)
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.
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