FFmpeg
hlsplaylist.c
Go to the documentation of this file.
1 /*
2  * Apple HTTP Live Streaming segmenter
3  * Copyright (c) 2012, Luca Barbato
4  * Copyright (c) 2017 Akamai Technologies, Inc.
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "config.h"
24 #include <stdint.h>
25 
27 
28 #include "avformat.h"
29 #include "hlsplaylist.h"
30 
32  if (!out)
33  return;
34  avio_printf(out, "#EXTM3U\n");
35  avio_printf(out, "#EXT-X-VERSION:%d\n", version);
36 }
37 
39  char *filename, char *language, int name_id, int is_default) {
40  if (!out || !agroup || !filename)
41  return;
42 
43  avio_printf(out, "#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID=\"group_%s\"", agroup);
44  avio_printf(out, ",NAME=\"audio_%d\",DEFAULT=%s,", name_id, is_default ? "YES" : "NO");
45  if (language) {
46  avio_printf(out, "LANGUAGE=\"%s\",", language);
47  }
48  avio_printf(out, "URI=\"%s\"\n", filename);
49 }
50 
52  int bandwidth, char *filename, char *agroup,
53  char *codecs, char *ccgroup) {
54 
55  if (!out || !filename)
56  return;
57 
58  if (!bandwidth) {
60  "Bandwidth info not available, set audio and video bitrates\n");
61  return;
62  }
63 
64  avio_printf(out, "#EXT-X-STREAM-INF:BANDWIDTH=%d", bandwidth);
65  if (st && st->codecpar->width > 0 && st->codecpar->height > 0)
66  avio_printf(out, ",RESOLUTION=%dx%d", st->codecpar->width,
67  st->codecpar->height);
68  if (codecs && strlen(codecs) > 0)
69  avio_printf(out, ",CODECS=\"%s\"", codecs);
70  if (agroup && strlen(agroup) > 0)
71  avio_printf(out, ",AUDIO=\"group_%s\"", agroup);
72  if (ccgroup && strlen(ccgroup) > 0)
73  avio_printf(out, ",CLOSED-CAPTIONS=\"%s\"", ccgroup);
74  avio_printf(out, "\n%s\n\n", filename);
75 }
76 
78  int target_duration, int64_t sequence,
79  uint32_t playlist_type, int iframe_mode) {
80  if (!out)
81  return;
83  if (allowcache == 0 || allowcache == 1) {
84  avio_printf(out, "#EXT-X-ALLOW-CACHE:%s\n", allowcache == 0 ? "NO" : "YES");
85  }
86  avio_printf(out, "#EXT-X-TARGETDURATION:%d\n", target_duration);
87  avio_printf(out, "#EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
88  av_log(NULL, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
89 
90  if (playlist_type == PLAYLIST_TYPE_EVENT) {
91  avio_printf(out, "#EXT-X-PLAYLIST-TYPE:EVENT\n");
92  } else if (playlist_type == PLAYLIST_TYPE_VOD) {
93  avio_printf(out, "#EXT-X-PLAYLIST-TYPE:VOD\n");
94  }
95  if (iframe_mode) {
96  avio_printf(out, "#EXT-X-I-FRAMES-ONLY\n");
97  }
98 }
99 
100 void ff_hls_write_init_file(AVIOContext *out, char *filename,
101  int byterange_mode, int64_t size, int64_t pos) {
102  avio_printf(out, "#EXT-X-MAP:URI=\"%s\"", filename);
103  if (byterange_mode) {
104  avio_printf(out, ",BYTERANGE=\"%"PRId64"@%"PRId64"\"", size, pos);
105  }
106  avio_printf(out, "\n");
107 }
108 
109 int ff_hls_write_file_entry(AVIOContext *out, int insert_discont,
110  int byterange_mode,
111  double duration, int round_duration,
112  int64_t size, int64_t pos, //Used only if HLS_SINGLE_FILE flag is set
113  char *baseurl, //Ignored if NULL
114  char *filename, double *prog_date_time,
115  int64_t video_keyframe_size, int64_t video_keyframe_pos, int iframe_mode) {
116  if (!out || !filename)
117  return AVERROR(EINVAL);
118 
119  if (insert_discont) {
120  avio_printf(out, "#EXT-X-DISCONTINUITY\n");
121  }
122  if (round_duration)
123  avio_printf(out, "#EXTINF:%ld,\n", lrint(duration));
124  else
125  avio_printf(out, "#EXTINF:%f,\n", duration);
126  if (byterange_mode)
127  avio_printf(out, "#EXT-X-BYTERANGE:%"PRId64"@%"PRId64"\n", iframe_mode ? video_keyframe_size : size,
128  iframe_mode ? video_keyframe_pos : pos);
129 
130  if (prog_date_time) {
131  time_t tt, wrongsecs;
132  int milli;
133  struct tm *tm, tmpbuf;
134  char buf0[128], buf1[128];
135  tt = (int64_t)*prog_date_time;
136  milli = av_clip(lrint(1000*(*prog_date_time - tt)), 0, 999);
137  tm = localtime_r(&tt, &tmpbuf);
138  if (!strftime(buf0, sizeof(buf0), "%Y-%m-%dT%H:%M:%S", tm)) {
139  av_log(NULL, AV_LOG_DEBUG, "strftime error in ff_hls_write_file_entry\n");
140  return AVERROR_UNKNOWN;
141  }
142  if (!strftime(buf1, sizeof(buf1), "%z", tm) || buf1[1]<'0' ||buf1[1]>'2') {
143  int tz_min, dst = tm->tm_isdst;
144  tm = gmtime_r(&tt, &tmpbuf);
145  tm->tm_isdst = dst;
146  wrongsecs = mktime(tm);
147  tz_min = (FFABS(wrongsecs - tt) + 30) / 60;
148  snprintf(buf1, sizeof(buf1),
149  "%c%02d%02d",
150  wrongsecs <= tt ? '+' : '-',
151  tz_min / 60,
152  tz_min % 60);
153  }
154  avio_printf(out, "#EXT-X-PROGRAM-DATE-TIME:%s.%03d%s\n", buf0, milli, buf1);
155  *prog_date_time += duration;
156  }
157  if (baseurl)
158  avio_printf(out, "%s", baseurl);
159  avio_printf(out, "%s\n", filename);
160 
161  return 0;
162 }
163 
165  if (!out)
166  return;
167  avio_printf(out, "#EXT-X-ENDLIST\n");
168 }
169 
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
out
FILE * out
Definition: movenc.c:54
ff_hls_write_end_list
void ff_hls_write_end_list(AVIOContext *out)
Definition: hlsplaylist.c:164
PLAYLIST_TYPE_VOD
@ PLAYLIST_TYPE_VOD
Definition: hlsplaylist.h:35
codecs
static struct codec_string codecs[]
PLAYLIST_TYPE_EVENT
@ PLAYLIST_TYPE_EVENT
Definition: hlsplaylist.h:34
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
hlsplaylist.h
ff_hls_write_playlist_header
void ff_hls_write_playlist_header(AVIOContext *out, int version, int allowcache, int target_duration, int64_t sequence, uint32_t playlist_type, int iframe_mode)
Definition: hlsplaylist.c:77
gmtime_r
#define gmtime_r
Definition: time_internal.h:34
ff_hls_write_audio_rendition
void ff_hls_write_audio_rendition(AVIOContext *out, char *agroup, char *filename, char *language, int name_id, int is_default)
Definition: hlsplaylist.c:38
lrint
#define lrint
Definition: tablegen.h:53
duration
int64_t duration
Definition: movenc.c:63
AVCodecParameters::width
int width
Video only.
Definition: avcodec.h:4023
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
version
int version
Definition: avisynth_c.h:858
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
time_internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1017
NULL
#define NULL
Definition: coverity.c:32
ff_hls_write_playlist_version
void ff_hls_write_playlist_version(AVIOContext *out, int version)
Definition: hlsplaylist.c:31
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
localtime_r
#define localtime_r
Definition: time_internal.h:46
size
int size
Definition: twinvq_data.h:11134
AVCodecParameters::height
int height
Definition: avcodec.h:4024
language
Undefined Behavior In the C language
Definition: undefined.txt:3
AVStream
Stream structure.
Definition: avformat.h:870
avformat.h
avio_printf
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
config.h
ff_hls_write_file_entry
int ff_hls_write_file_entry(AVIOContext *out, int insert_discont, int byterange_mode, double duration, int round_duration, int64_t size, int64_t pos, char *baseurl, char *filename, double *prog_date_time, int64_t video_keyframe_size, int64_t video_keyframe_pos, int iframe_mode)
Definition: hlsplaylist.c:109
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
snprintf
#define snprintf
Definition: snprintf.h:34
ff_hls_write_init_file
void ff_hls_write_init_file(AVIOContext *out, char *filename, int byterange_mode, int64_t size, int64_t pos)
Definition: hlsplaylist.c:100
ff_hls_write_stream_info
void ff_hls_write_stream_info(AVStream *st, AVIOContext *out, int bandwidth, char *filename, char *agroup, char *codecs, char *ccgroup)
Definition: hlsplaylist.c:51