42 #define LINE_BUFFER_SIZE 1024
121 float playlist_duration = 0.0f;
122 int ret = 0, path_size, sub_path_size;
123 char *dirname =
NULL, *p, *sub_path;
128 playlist_duration += segment->
duration;
129 segment = segment->
next;
134 playlist_duration -= segment->
duration;
135 previous_segment = segment;
136 segment = previous_segment->
next;
137 if (playlist_duration <= -previous_segment->
duration) {
160 path_size = strlen(dirname) + strlen(segment->
filename) + 1;
166 sub_path_size = strlen(dirname) + strlen(segment->
sub_filename) + 1;
175 if (unlink(path) < 0) {
177 path, strerror(errno));
182 if (unlink(sub_path) < 0) {
184 sub_path, strerror(errno));
188 previous_segment = segment;
189 segment = previous_segment->
next;
243 if (ret !=
sizeof(key)) {
365 int target_duration = 0;
369 char temp_filename[1024];
373 int use_rename = proto && !strcmp(proto,
"file");
374 static unsigned warned_non_file;
375 char *key_uri =
NULL;
376 char *iv_string =
NULL;
378 if (!use_rename && !warned_non_file++)
379 av_log(s,
AV_LOG_ERROR,
"Cannot use rename on non file protocol, this may lead to races and temporarly partial files\n");
381 snprintf(temp_filename,
sizeof(temp_filename), use_rename ?
"%s.tmp" :
"%s", s->
filename);
388 target_duration = ceil(en->
duration);
397 avio_printf(out,
"#EXT-X-TARGETDURATION:%d\n", target_duration);
398 avio_printf(out,
"#EXT-X-MEDIA-SEQUENCE:%"PRId64
"\n", sequence);
422 avio_printf(out,
"#EXT-X-BYTERANGE:%"PRIi64
"@%"PRIi64
"\n",
437 avio_printf(sub_out,
"#EXT-X-VERSION:%d\n", version);
441 avio_printf(sub_out,
"#EXT-X-TARGETDURATION:%d\n", target_duration);
442 avio_printf(sub_out,
"#EXT-X-MEDIA-SEQUENCE:%"PRId64
"\n", sequence);
450 avio_printf(sub_out,
"#EXT-X-BYTERANGE:%"PRIi64
"@%"PRIi64
"\n",
465 if (ret >= 0 && use_rename)
476 char *filename, iv_string[
KEYSIZE*2 + 1];
488 struct tm *tm, tmpbuf;
519 if ((err =
av_dict_set(&options,
"encryption_iv", iv_string, 0)) < 0)
557 const char *pattern =
"%d.ts";
558 const char *pattern_localtime_fmt =
"-%s.ts";
559 const char *vtt_pattern =
"%d.vtt";
562 int vtt_basename_size;
585 "More than a single video stream present, "
586 "expect issues decoding it.\n");
614 basename_size = strlen(s->
filename) + strlen(pattern_localtime_fmt) + 1;
616 basename_size = strlen(s->
filename) + strlen(pattern) + 1;
639 vtt_pattern =
".vtt";
640 vtt_basename_size = strlen(s->
filename) + strlen(vtt_pattern) + 1;
715 int ret, can_split = 1;
716 int stream_index = 0;
736 is_ref_pkt = can_split = 0;
744 int64_t new_start_pos;
822 #define OFFSET(x) offsetof(HLSContext, x)
823 #define E AV_OPT_FLAG_ENCODING_PARAM
827 {
"hls_list_size",
"set maximum number of playlist entries",
OFFSET(max_nb_segments),
AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX,
E},
828 {
"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},
829 {
"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},
831 {
"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},
836 {
"hls_flags",
"set flags affecting HLS playlist and media file generation",
OFFSET(
flags),
AV_OPT_TYPE_FLAGS, {.i64 = 0 }, 0, UINT_MAX,
E,
"flags"},
842 {
"use_localtime",
"set filename expansion with strftime at segment creation",
OFFSET(use_localtime),
AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1,
E },
858 .extensions =
"m3u8",
867 .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 avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
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 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)
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.
void av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
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. ...
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
#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().
static av_always_inline av_const double round(double x)
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.
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]
int avio_open2(AVIOContext **s, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create and initialize a AVIOContext for accessing the resource indicated by url.
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)
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
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
This structure stores compressed data.
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
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