FFmpeg
Data Structures | Macros | Enumerations | Functions | Variables
hlsenc.c File Reference
#include "config.h"
#include "config_components.h"
#include <stdint.h>
#include <time.h>
#include "libavutil/avassert.h"
#include "libavutil/mathematics.h"
#include "libavutil/avstring.h"
#include "libavutil/bprint.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "libavutil/log.h"
#include "libavutil/random_seed.h"
#include "libavutil/time_internal.h"
#include "libavcodec/defs.h"
#include "avformat.h"
#include "avio_internal.h"
#include "avc.h"
#include "hlsplaylist.h"
#include "internal.h"
#include "nal.h"
#include "mux.h"
#include "os_support.h"
#include "url.h"

Go to the source code of this file.

Data Structures

struct  HLSSegment
 
struct  VariantStream
 
struct  ClosedCaptionsStream
 
struct  HLSContext
 

Macros

#define KEYSIZE   16
 
#define LINE_BUFFER_SIZE   MAX_URL_SIZE
 
#define HLS_MICROSECOND_UNIT   1000000
 
#define BUFSIZE   (16 * 1024)
 
#define POSTFIX_PATTERN   "_%d"
 
#define OFFSET(x)   offsetof(HLSContext, x)
 
#define E   AV_OPT_FLAG_ENCODING_PARAM
 

Enumerations

enum  StartSequenceSourceType {
  HLS_START_SEQUENCE_AS_START_NUMBER = 0, HLS_START_SEQUENCE_AS_SECONDS_SINCE_EPOCH = 1, HLS_START_SEQUENCE_AS_FORMATTED_DATETIME = 2, HLS_START_SEQUENCE_AS_MICROSECONDS_SINCE_EPOCH = 3,
  HLS_START_SEQUENCE_LAST
}
 
enum  CodecAttributeStatus { CODEC_ATTRIBUTE_WRITTEN = 0, CODEC_ATTRIBUTE_WILL_NOT_BE_WRITTEN }
 
enum  HLSFlags {
  HLS_SINGLE_FILE = (1 << 0), HLS_DELETE_SEGMENTS = (1 << 1), HLS_ROUND_DURATIONS = (1 << 2), HLS_DISCONT_START = (1 << 3),
  HLS_OMIT_ENDLIST = (1 << 4), HLS_SPLIT_BY_TIME = (1 << 5), HLS_APPEND_LIST = (1 << 6), HLS_PROGRAM_DATE_TIME = (1 << 7),
  HLS_SECOND_LEVEL_SEGMENT_INDEX = (1 << 8), HLS_SECOND_LEVEL_SEGMENT_DURATION = (1 << 9), HLS_SECOND_LEVEL_SEGMENT_SIZE = (1 << 10), HLS_TEMP_FILE = (1 << 11),
  HLS_PERIODIC_REKEY = (1 << 12), HLS_INDEPENDENT_SEGMENTS = (1 << 13), HLS_I_FRAMES_ONLY = (1 << 14)
}
 
enum  SegmentType {
  PALETTE_SEGMENT = 0x14, OBJECT_SEGMENT = 0x15, PRESENTATION_SEGMENT = 0x16, WINDOW_SEGMENT = 0x17,
  DISPLAY_SEGMENT = 0x80, SEGMENT_TYPE_AUTO = 0, SEGMENT_TYPE_MP4, SEGMENT_TYPE_WEBM,
  SEGMENT_TYPE_NB, SEGMENT_TYPE_MPEGTS, SEGMENT_TYPE_FMP4
}
 

Functions

static int strftime_expand (const char *fmt, char **dest)
 
static int hlsenc_io_open (AVFormatContext *s, AVIOContext **pb, const char *filename, AVDictionary **options)
 
static int hlsenc_io_close (AVFormatContext *s, AVIOContext **pb, char *filename)
 
static void set_http_options (AVFormatContext *s, AVDictionary **options, HLSContext *c)
 
static void write_codec_attr (AVStream *st, VariantStream *vs)
 
static int replace_str_data_in_filename (char **s, const char *filename, char placeholder, const char *datastring)
 
static int replace_int_data_in_filename (char **s, const char *filename, char placeholder, int64_t number)
 
static void write_styp (AVIOContext *pb)
 
static int flush_dynbuf (VariantStream *vs, int *range_length)
 
static void reflush_dynbuf (VariantStream *vs, int *range_length)
 
static int hls_delete_file (HLSContext *hls, AVFormatContext *avf, char *path, const char *proto)
 
static int hls_delete_old_segments (AVFormatContext *s, HLSContext *hls, VariantStream *vs)
 
static int do_encrypt (AVFormatContext *s, VariantStream *vs)
 
static int hls_encryption_start (AVFormatContext *s, VariantStream *vs)
 
static int hls_mux_init (AVFormatContext *s, VariantStream *vs)
 
static HLSSegmentfind_segment_by_filename (HLSSegment *segment, const char *filename)
 
static int sls_flags_filename_process (struct AVFormatContext *s, HLSContext *hls, VariantStream *vs, HLSSegment *en, double duration, int64_t pos, int64_t size)
 
static int sls_flag_check_duration_size_index (HLSContext *hls)
 
static int sls_flag_check_duration_size (HLSContext *hls, VariantStream *vs)
 
static void sls_flag_file_rename (HLSContext *hls, VariantStream *vs, char *old_filename)
 
static int sls_flag_use_localtime_filename (AVFormatContext *oc, HLSContext *c, VariantStream *vs)
 
static int hls_append_segment (struct AVFormatContext *s, HLSContext *hls, VariantStream *vs, double duration, int64_t pos, int64_t size)
 
static int extract_segment_number (const char *filename)
 
static int parse_playlist (AVFormatContext *s, const char *url, VariantStream *vs)
 
static void hls_free_segments (HLSSegment *p)
 
static int hls_rename_temp_file (AVFormatContext *s, AVFormatContext *oc)
 
static const char * get_relative_url (const char *master_url, const char *media_url)
 
static int64_t get_stream_bit_rate (AVStream *stream)
 
static int create_master_playlist (AVFormatContext *s, VariantStream *const input_vs, int final)
 
static int hls_window (AVFormatContext *s, int last, VariantStream *vs)
 
static int hls_start (AVFormatContext *s, VariantStream *vs)
 
static const char * get_default_pattern_localtime_fmt (AVFormatContext *s)
 
static int append_postfix (char *name, int name_buf_len, int i)
 
static int validate_name (int nb_vs, const char *fn)
 
static int format_name (const char *buf, char **s, int index, const char *varname)
 
static int get_nth_codec_stream_index (AVFormatContext *s, enum AVMediaType codec_type, int64_t stream_id)
 
static int parse_variant_stream_mapstring (AVFormatContext *s)
 
static int parse_cc_stream_mapstring (AVFormatContext *s)
 
static int update_variant_stream_info (AVFormatContext *s)
 
static int update_master_pl_info (AVFormatContext *s)
 
static int hls_write_header (AVFormatContext *s)
 
static int hls_init_file_resend (AVFormatContext *s, VariantStream *vs)
 
static int64_t append_single_file (AVFormatContext *s, VariantStream *vs)
 
static int hls_write_packet (AVFormatContext *s, AVPacket *pkt)
 
static void hls_deinit (AVFormatContext *s)
 
static int hls_write_trailer (struct AVFormatContext *s)
 
static int hls_init (AVFormatContext *s)
 

Variables

static const AVOption options []
 
static const AVClass hls_class
 
const FFOutputFormat ff_hls_muxer
 

Macro Definition Documentation

◆ KEYSIZE

#define KEYSIZE   16

Definition at line 71 of file hlsenc.c.

◆ LINE_BUFFER_SIZE

#define LINE_BUFFER_SIZE   MAX_URL_SIZE

Definition at line 72 of file hlsenc.c.

◆ HLS_MICROSECOND_UNIT

#define HLS_MICROSECOND_UNIT   1000000

Definition at line 73 of file hlsenc.c.

◆ BUFSIZE

#define BUFSIZE   (16 * 1024)

Definition at line 74 of file hlsenc.c.

◆ POSTFIX_PATTERN

#define POSTFIX_PATTERN   "_%d"

Definition at line 75 of file hlsenc.c.

◆ OFFSET

#define OFFSET (   x)    offsetof(HLSContext, x)

Definition at line 3180 of file hlsenc.c.

◆ E

Definition at line 3181 of file hlsenc.c.

Enumeration Type Documentation

◆ StartSequenceSourceType

Enumerator
HLS_START_SEQUENCE_AS_START_NUMBER 
HLS_START_SEQUENCE_AS_SECONDS_SINCE_EPOCH 
HLS_START_SEQUENCE_AS_FORMATTED_DATETIME 
HLS_START_SEQUENCE_AS_MICROSECONDS_SINCE_EPOCH 
HLS_START_SEQUENCE_LAST 

Definition at line 58 of file hlsenc.c.

◆ CodecAttributeStatus

Enumerator
CODEC_ATTRIBUTE_WRITTEN 
CODEC_ATTRIBUTE_WILL_NOT_BE_WRITTEN 

Definition at line 66 of file hlsenc.c.

◆ HLSFlags

enum HLSFlags
Enumerator
HLS_SINGLE_FILE 
HLS_DELETE_SEGMENTS 
HLS_ROUND_DURATIONS 
HLS_DISCONT_START 
HLS_OMIT_ENDLIST 
HLS_SPLIT_BY_TIME 
HLS_APPEND_LIST 
HLS_PROGRAM_DATE_TIME 
HLS_SECOND_LEVEL_SEGMENT_INDEX 
HLS_SECOND_LEVEL_SEGMENT_DURATION 
HLS_SECOND_LEVEL_SEGMENT_SIZE 
HLS_TEMP_FILE 
HLS_PERIODIC_REKEY 
HLS_INDEPENDENT_SEGMENTS 
HLS_I_FRAMES_ONLY 

Definition at line 95 of file hlsenc.c.

◆ SegmentType

Enumerator
PALETTE_SEGMENT 
OBJECT_SEGMENT 
PRESENTATION_SEGMENT 
WINDOW_SEGMENT 
DISPLAY_SEGMENT 
SEGMENT_TYPE_AUTO 
SEGMENT_TYPE_MP4 
SEGMENT_TYPE_WEBM 
SEGMENT_TYPE_NB 
SEGMENT_TYPE_MPEGTS 
SEGMENT_TYPE_FMP4 

Definition at line 114 of file hlsenc.c.

Function Documentation

◆ strftime_expand()

static int strftime_expand ( const char *  fmt,
char **  dest 
)
static

Definition at line 268 of file hlsenc.c.

Referenced by hls_init(), and hls_start().

◆ hlsenc_io_open()

static int hlsenc_io_open ( AVFormatContext s,
AVIOContext **  pb,
const char *  filename,
AVDictionary **  options 
)
static

◆ hlsenc_io_close()

static int hlsenc_io_close ( AVFormatContext s,
AVIOContext **  pb,
char *  filename 
)
static

◆ set_http_options()

static void set_http_options ( AVFormatContext s,
AVDictionary **  options,
HLSContext c 
)
static

◆ write_codec_attr()

static void write_codec_attr ( AVStream st,
VariantStream vs 
)
static

Definition at line 351 of file hlsenc.c.

Referenced by hls_write_header().

◆ replace_str_data_in_filename()

static int replace_str_data_in_filename ( char **  s,
const char *  filename,
char  placeholder,
const char *  datastring 
)
static

Definition at line 455 of file hlsenc.c.

Referenced by format_name(), and hls_delete_old_segments().

◆ replace_int_data_in_filename()

static int replace_int_data_in_filename ( char **  s,
const char *  filename,
char  placeholder,
int64_t  number 
)
static

◆ write_styp()

static void write_styp ( AVIOContext pb)
static

Definition at line 543 of file hlsenc.c.

Referenced by hls_write_packet(), and hls_write_trailer().

◆ flush_dynbuf()

static int flush_dynbuf ( VariantStream vs,
int *  range_length 
)
static

Definition at line 553 of file hlsenc.c.

Referenced by hls_write_packet(), and hls_write_trailer().

◆ reflush_dynbuf()

static void reflush_dynbuf ( VariantStream vs,
int *  range_length 
)
static

Definition at line 574 of file hlsenc.c.

Referenced by hls_write_packet(), and hls_write_trailer().

◆ hls_delete_file()

static int hls_delete_file ( HLSContext hls,
AVFormatContext avf,
char *  path,
const char *  proto 
)
static

Definition at line 580 of file hlsenc.c.

Referenced by hls_delete_old_segments().

◆ hls_delete_old_segments()

static int hls_delete_old_segments ( AVFormatContext s,
HLSContext hls,
VariantStream vs 
)
static

Definition at line 604 of file hlsenc.c.

Referenced by hls_append_segment().

◆ do_encrypt()

static int do_encrypt ( AVFormatContext s,
VariantStream vs 
)
static

Definition at line 714 of file hlsenc.c.

Referenced by hls_start().

◆ hls_encryption_start()

static int hls_encryption_start ( AVFormatContext s,
VariantStream vs 
)
static

Definition at line 787 of file hlsenc.c.

Referenced by hls_start().

◆ hls_mux_init()

static int hls_mux_init ( AVFormatContext s,
VariantStream vs 
)
static

Definition at line 846 of file hlsenc.c.

Referenced by hls_init().

◆ find_segment_by_filename()

static HLSSegment* find_segment_by_filename ( HLSSegment segment,
const char *  filename 
)
static

Definition at line 963 of file hlsenc.c.

Referenced by hls_append_segment().

◆ sls_flags_filename_process()

static int sls_flags_filename_process ( struct AVFormatContext s,
HLSContext hls,
VariantStream vs,
HLSSegment en,
double  duration,
int64_t  pos,
int64_t  size 
)
static

Definition at line 973 of file hlsenc.c.

Referenced by hls_append_segment().

◆ sls_flag_check_duration_size_index()

static int sls_flag_check_duration_size_index ( HLSContext hls)
static

Definition at line 1013 of file hlsenc.c.

Referenced by hls_init().

◆ sls_flag_check_duration_size()

static int sls_flag_check_duration_size ( HLSContext hls,
VariantStream vs 
)
static

Definition at line 1036 of file hlsenc.c.

Referenced by hls_init().

◆ sls_flag_file_rename()

static void sls_flag_file_rename ( HLSContext hls,
VariantStream vs,
char *  old_filename 
)
static

Definition at line 1056 of file hlsenc.c.

Referenced by hls_write_packet(), and hls_write_trailer().

◆ sls_flag_use_localtime_filename()

static int sls_flag_use_localtime_filename ( AVFormatContext oc,
HLSContext c,
VariantStream vs 
)
static

Definition at line 1063 of file hlsenc.c.

Referenced by hls_start().

◆ hls_append_segment()

static int hls_append_segment ( struct AVFormatContext s,
HLSContext hls,
VariantStream vs,
double  duration,
int64_t  pos,
int64_t  size 
)
static

Definition at line 1107 of file hlsenc.c.

Referenced by hls_write_packet(), hls_write_trailer(), and parse_playlist().

◆ extract_segment_number()

static int extract_segment_number ( const char *  filename)
static

Definition at line 1208 of file hlsenc.c.

Referenced by parse_playlist().

◆ parse_playlist()

static int parse_playlist ( AVFormatContext s,
const char *  url,
VariantStream vs 
)
static

Definition at line 1224 of file hlsenc.c.

Referenced by hls_init().

◆ hls_free_segments()

static void hls_free_segments ( HLSSegment p)
static

Definition at line 1351 of file hlsenc.c.

Referenced by hls_deinit().

◆ hls_rename_temp_file()

static int hls_rename_temp_file ( AVFormatContext s,
AVFormatContext oc 
)
static

Definition at line 1362 of file hlsenc.c.

Referenced by hls_write_packet(), and hls_write_trailer().

◆ get_relative_url()

static const char* get_relative_url ( const char *  master_url,
const char *  media_url 
)
static

Definition at line 1377 of file hlsenc.c.

Referenced by create_master_playlist().

◆ get_stream_bit_rate()

static int64_t get_stream_bit_rate ( AVStream stream)
static

Definition at line 1397 of file hlsenc.c.

Referenced by create_master_playlist().

◆ create_master_playlist()

static int create_master_playlist ( AVFormatContext s,
VariantStream *const  input_vs,
int  final 
)
static

Traverse through the list of audio only rendition streams and find the rendition which has highest bitrate in the same audio group

Definition at line 1414 of file hlsenc.c.

Referenced by hls_window().

◆ hls_window()

static int hls_window ( AVFormatContext s,
int  last,
VariantStream vs 
)
static

Definition at line 1593 of file hlsenc.c.

Referenced by hls_write_packet(), and hls_write_trailer().

◆ hls_start()

static int hls_start ( AVFormatContext s,
VariantStream vs 
)
static

Definition at line 1737 of file hlsenc.c.

Referenced by hls_init(), and hls_write_packet().

◆ get_default_pattern_localtime_fmt()

static const char* get_default_pattern_localtime_fmt ( AVFormatContext s)
static

Definition at line 1922 of file hlsenc.c.

Referenced by hls_init().

◆ append_postfix()

static int append_postfix ( char *  name,
int  name_buf_len,
int  i 
)
static

Definition at line 1943 of file hlsenc.c.

Referenced by hls_init().

◆ validate_name()

static int validate_name ( int  nb_vs,
const char *  fn 
)
static

Definition at line 1962 of file hlsenc.c.

Referenced by hls_init().

◆ format_name()

static int format_name ( const char *  buf,
char **  s,
int  index,
const char *  varname 
)
static

Definition at line 1996 of file hlsenc.c.

Referenced by hls_init().

◆ get_nth_codec_stream_index()

static int get_nth_codec_stream_index ( AVFormatContext s,
enum AVMediaType  codec_type,
int64_t  stream_id 
)
static

Definition at line 2042 of file hlsenc.c.

Referenced by parse_variant_stream_mapstring().

◆ parse_variant_stream_mapstring()

static int parse_variant_stream_mapstring ( AVFormatContext s)
static

Expected format for var_stream_map string is as below: "a:0,v:0 a:1,v:1" "a:0,agroup:a0,default:1,language:ENG a:1,agroup:a1,default:0 v:0,agroup:a0 v:1,agroup:a1" This string specifies how to group the audio, video and subtitle streams into different variant streams. The variant stream groups are separated by space.

a:, v:, s: are keys to specify audio, video and subtitle streams respectively. Allowed values are 0 to 9 digits (limited just based on practical usage)

agroup: is key to specify audio group. A string can be given as value. sgroup: is key to specify subtitle group. A string can be given as value.

Definition at line 2060 of file hlsenc.c.

Referenced by update_variant_stream_info().

◆ parse_cc_stream_mapstring()

static int parse_cc_stream_mapstring ( AVFormatContext s)
static

Definition at line 2206 of file hlsenc.c.

Referenced by update_variant_stream_info().

◆ update_variant_stream_info()

static int update_variant_stream_info ( AVFormatContext s)
static

Definition at line 2283 of file hlsenc.c.

Referenced by hls_init().

◆ update_master_pl_info()

static int update_master_pl_info ( AVFormatContext s)
static

if output file's directory has v, variants are created in sub-directories then master is created at the sub-directories level

Definition at line 2321 of file hlsenc.c.

Referenced by hls_init().

◆ hls_write_header()

static int hls_write_header ( AVFormatContext s)
static

Definition at line 2363 of file hlsenc.c.

◆ hls_init_file_resend()

static int hls_init_file_resend ( AVFormatContext s,
VariantStream vs 
)
static

Definition at line 2424 of file hlsenc.c.

Referenced by hls_write_packet().

◆ append_single_file()

static int64_t append_single_file ( AVFormatContext s,
VariantStream vs 
)
static

Definition at line 2441 of file hlsenc.c.

Referenced by hls_write_packet(), and hls_write_trailer().

◆ hls_write_packet()

static int hls_write_packet ( AVFormatContext s,
AVPacket pkt 
)
static

Definition at line 2472 of file hlsenc.c.

◆ hls_deinit()

static void hls_deinit ( AVFormatContext s)
static

Definition at line 2755 of file hlsenc.c.

◆ hls_write_trailer()

static int hls_write_trailer ( struct AVFormatContext s)
static

Definition at line 2789 of file hlsenc.c.

◆ hls_init()

static int hls_init ( AVFormatContext s)
static

Definition at line 2929 of file hlsenc.c.

Variable Documentation

◆ options

const AVOption options[]
static

Definition at line 3182 of file hlsenc.c.

◆ hls_class

const AVClass hls_class
static
Initial value:
= {
.class_name = "hls muxer",
.item_name = av_default_item_name,
.option = options,
}

Definition at line 3244 of file hlsenc.c.

◆ ff_hls_muxer

const FFOutputFormat ff_hls_muxer
Initial value:
= {
.p.name = "hls",
.p.long_name = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
.p.extensions = "m3u8",
.p.audio_codec = AV_CODEC_ID_AAC,
.p.video_codec = AV_CODEC_ID_H264,
.p.subtitle_codec = AV_CODEC_ID_WEBVTT,
.p.priv_class = &hls_class,
.flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
.priv_data_size = sizeof(HLSContext),
}

Definition at line 3252 of file hlsenc.c.

hls_write_header
static int hls_write_header(AVFormatContext *s)
Definition: hlsenc.c:2363
AVFMT_NODIMENSIONS
#define AVFMT_NODIMENSIONS
Format does not need width/height.
Definition: avformat.h:483
deinit
static void deinit(AVFormatContext *s)
Definition: chromaprint.c:52
hls_write_trailer
static int hls_write_trailer(struct AVFormatContext *s)
Definition: hlsenc.c:2789
hls_deinit
static void hls_deinit(AVFormatContext *s)
Definition: hlsenc.c:2755
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
AV_CODEC_ID_WEBVTT
@ AV_CODEC_ID_WEBVTT
Definition: codec_id.h:574
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
write_trailer
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:101
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:448
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
FF_OFMT_FLAG_ALLOW_FLUSH
#define FF_OFMT_FLAG_ALLOW_FLUSH
This flag indicates that the muxer stores data internally and supports flushing it.
Definition: mux.h:38
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:468
hls_init
static int hls_init(AVFormatContext *s)
Definition: hlsenc.c:2929
hls_write_packet
static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: hlsenc.c:2472
AVFMT_GLOBALHEADER
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:478
HLSContext
Definition: hls.c:205
options
static const AVOption options[]
Definition: hlsenc.c:3182
write_packet
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
Definition: ffmpeg_mux.c:209
write_header
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:348
hls_class
static const AVClass hls_class
Definition: hlsenc.c:3244