2 * Apple HTTP Live Streaming segmenter
3 * Copyright (c) 2012, Luca Barbato
4 * Copyright (c) 2017 Akamai Technologies, Inc.
6 * This file is part of FFmpeg.
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.
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.
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
26 #include "libavutil/time_internal.h"
29 #include "hlsplaylist.h"
31 void ff_hls_write_playlist_version(AVIOContext *out, int version) {
34 avio_printf(out, "#EXTM3U\n");
35 avio_printf(out, "#EXT-X-VERSION:%d\n", version);
38 void ff_hls_write_audio_rendition(AVIOContext *out, char *agroup,
39 const char *filename, char *language, int name_id, int is_default) {
40 if (!out || !agroup || !filename)
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");
46 avio_printf(out, "LANGUAGE=\"%s\",", language);
48 avio_printf(out, "URI=\"%s\"\n", filename);
51 void ff_hls_write_subtitle_rendition(AVIOContext *out, char *sgroup,
52 const char *filename, char *language, int name_id, int is_default) {
53 if (!out || !filename)
56 avio_printf(out, "#EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID=\"%s\"", sgroup);
57 avio_printf(out, ",NAME=\"subtitle_%d\",DEFAULT=%s,", name_id, is_default ? "YES" : "NO");
59 avio_printf(out, "LANGUAGE=\"%s\",", language);
61 avio_printf(out, "URI=\"%s\"\n", filename);
64 void ff_hls_write_stream_info(AVStream *st, AVIOContext *out,
65 int bandwidth, const char *filename, char *agroup,
66 char *codecs, char *ccgroup, char *sgroup) {
68 if (!out || !filename)
72 av_log(NULL, AV_LOG_WARNING,
73 "Bandwidth info not available, set audio and video bitrates\n");
77 avio_printf(out, "#EXT-X-STREAM-INF:BANDWIDTH=%d", bandwidth);
78 if (st && st->codecpar->width > 0 && st->codecpar->height > 0)
79 avio_printf(out, ",RESOLUTION=%dx%d", st->codecpar->width,
80 st->codecpar->height);
81 if (codecs && codecs[0])
82 avio_printf(out, ",CODECS=\"%s\"", codecs);
83 if (agroup && agroup[0])
84 avio_printf(out, ",AUDIO=\"group_%s\"", agroup);
85 if (ccgroup && ccgroup[0])
86 avio_printf(out, ",CLOSED-CAPTIONS=\"%s\"", ccgroup);
87 if (sgroup && sgroup[0])
88 avio_printf(out, ",SUBTITLES=\"%s\"", sgroup);
89 avio_printf(out, "\n%s\n\n", filename);
92 void ff_hls_write_playlist_header(AVIOContext *out, int version, int allowcache,
93 int target_duration, int64_t sequence,
94 uint32_t playlist_type, int iframe_mode) {
97 ff_hls_write_playlist_version(out, version);
98 if (allowcache == 0 || allowcache == 1) {
99 avio_printf(out, "#EXT-X-ALLOW-CACHE:%s\n", allowcache == 0 ? "NO" : "YES");
101 avio_printf(out, "#EXT-X-TARGETDURATION:%d\n", target_duration);
102 avio_printf(out, "#EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
103 av_log(NULL, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
105 if (playlist_type == PLAYLIST_TYPE_EVENT) {
106 avio_printf(out, "#EXT-X-PLAYLIST-TYPE:EVENT\n");
107 } else if (playlist_type == PLAYLIST_TYPE_VOD) {
108 avio_printf(out, "#EXT-X-PLAYLIST-TYPE:VOD\n");
111 avio_printf(out, "#EXT-X-I-FRAMES-ONLY\n");
115 void ff_hls_write_init_file(AVIOContext *out, char *filename,
116 int byterange_mode, int64_t size, int64_t pos) {
117 avio_printf(out, "#EXT-X-MAP:URI=\"%s\"", filename);
118 if (byterange_mode) {
119 avio_printf(out, ",BYTERANGE=\"%"PRId64"@%"PRId64"\"", size, pos);
121 avio_printf(out, "\n");
124 int ff_hls_write_file_entry(AVIOContext *out, int insert_discont,
126 double duration, int round_duration,
127 int64_t size, int64_t pos, //Used only if HLS_SINGLE_FILE flag is set
128 char *baseurl, //Ignored if NULL
129 char *filename, double *prog_date_time,
130 int64_t video_keyframe_size, int64_t video_keyframe_pos, int iframe_mode) {
131 if (!out || !filename)
132 return AVERROR(EINVAL);
134 if (insert_discont) {
135 avio_printf(out, "#EXT-X-DISCONTINUITY\n");
138 avio_printf(out, "#EXTINF:%ld,\n", lrint(duration));
140 avio_printf(out, "#EXTINF:%f,\n", duration);
142 avio_printf(out, "#EXT-X-BYTERANGE:%"PRId64"@%"PRId64"\n", iframe_mode ? video_keyframe_size : size,
143 iframe_mode ? video_keyframe_pos : pos);
145 if (prog_date_time) {
146 time_t tt, wrongsecs;
148 struct tm *tm, tmpbuf;
149 char buf0[128], buf1[128];
150 tt = (int64_t)*prog_date_time;
151 milli = av_clip(lrint(1000*(*prog_date_time - tt)), 0, 999);
152 tm = localtime_r(&tt, &tmpbuf);
153 if (!strftime(buf0, sizeof(buf0), "%Y-%m-%dT%H:%M:%S", tm)) {
154 av_log(NULL, AV_LOG_DEBUG, "strftime error in ff_hls_write_file_entry\n");
155 return AVERROR_UNKNOWN;
157 if (!strftime(buf1, sizeof(buf1), "%z", tm) || buf1[1]<'0' ||buf1[1]>'2') {
158 int tz_min, dst = tm->tm_isdst;
159 tm = gmtime_r(&tt, &tmpbuf);
161 wrongsecs = mktime(tm);
162 tz_min = (FFABS(wrongsecs - tt) + 30) / 60;
163 snprintf(buf1, sizeof(buf1),
165 wrongsecs <= tt ? '+' : '-',
169 avio_printf(out, "#EXT-X-PROGRAM-DATE-TIME:%s.%03d%s\n", buf0, milli, buf1);
170 *prog_date_time += duration;
173 avio_printf(out, "%s", baseurl);
174 avio_printf(out, "%s\n", filename);
179 void ff_hls_write_end_list (AVIOContext *out) {
182 avio_printf(out, "#EXT-X-ENDLIST\n");