]> git.sesse.net Git - ffmpeg/blob - libavformat/hlsplaylist.c
Merge commit 'ca44fa5d7fda7e954f3ebfeb5b0d6d1be55fcaa3'
[ffmpeg] / libavformat / hlsplaylist.c
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
26 #include "libavutil/time_internal.h"
27
28 #include "avformat.h"
29 #include "hlsplaylist.h"
30
31 void ff_hls_write_playlist_version(AVIOContext *out, int version) {
32     if (!out)
33         return;
34     avio_printf(out, "#EXTM3U\n");
35     avio_printf(out, "#EXT-X-VERSION:%d\n", version);
36 }
37
38 void ff_hls_write_audio_rendition(AVIOContext *out, char *agroup,
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
51 void ff_hls_write_stream_info(AVStream *st, AVIOContext *out,
52                               int bandwidth, char *filename, char *agroup,
53                               char *codecs, char *ccgroup) {
54
55     if (!out || !filename)
56         return;
57
58     if (!bandwidth) {
59         av_log(NULL, AV_LOG_WARNING,
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
77 void ff_hls_write_playlist_header(AVIOContext *out, int version, int allowcache,
78                                   int target_duration, int64_t sequence,
79                                   uint32_t playlist_type) {
80     if (!out)
81         return;
82     ff_hls_write_playlist_version(out, version);
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 }
96
97 void ff_hls_write_init_file(AVIOContext *out, char *filename,
98                             int byterange_mode, int64_t size, int64_t pos) {
99     avio_printf(out, "#EXT-X-MAP:URI=\"%s\"", filename);
100     if (byterange_mode) {
101         avio_printf(out, ",BYTERANGE=\"%"PRId64"@%"PRId64"\"", size, pos);
102     }
103     avio_printf(out, "\n");
104 }
105
106 int ff_hls_write_file_entry(AVIOContext *out, int insert_discont,
107                              int byterange_mode,
108                              double duration, int round_duration,
109                              int64_t size, int64_t pos, //Used only if HLS_SINGLE_FILE flag is set
110                              char *baseurl, //Ignored if NULL
111                              char *filename, double *prog_date_time) {
112     if (!out || !filename)
113         return AVERROR(EINVAL);
114
115     if (insert_discont) {
116         avio_printf(out, "#EXT-X-DISCONTINUITY\n");
117     }
118     if (round_duration)
119         avio_printf(out, "#EXTINF:%ld,\n",  lrint(duration));
120     else
121         avio_printf(out, "#EXTINF:%f,\n", duration);
122     if (byterange_mode)
123         avio_printf(out, "#EXT-X-BYTERANGE:%"PRId64"@%"PRId64"\n", size, pos);
124
125     if (prog_date_time) {
126         time_t tt, wrongsecs;
127         int milli;
128         struct tm *tm, tmpbuf;
129         char buf0[128], buf1[128];
130         tt = (int64_t)*prog_date_time;
131         milli = av_clip(lrint(1000*(*prog_date_time - tt)), 0, 999);
132         tm = localtime_r(&tt, &tmpbuf);
133         if (!strftime(buf0, sizeof(buf0), "%Y-%m-%dT%H:%M:%S", tm)) {
134             av_log(NULL, AV_LOG_DEBUG, "strftime error in ff_hls_write_file_entry\n");
135             return AVERROR_UNKNOWN;
136         }
137         if (!strftime(buf1, sizeof(buf1), "%z", tm) || buf1[1]<'0' ||buf1[1]>'2') {
138             int tz_min, dst = tm->tm_isdst;
139             tm = gmtime_r(&tt, &tmpbuf);
140             tm->tm_isdst = dst;
141             wrongsecs = mktime(tm);
142             tz_min = (FFABS(wrongsecs - tt) + 30) / 60;
143             snprintf(buf1, sizeof(buf1),
144                      "%c%02d%02d",
145                      wrongsecs <= tt ? '+' : '-',
146                      tz_min / 60,
147                      tz_min % 60);
148         }
149         avio_printf(out, "#EXT-X-PROGRAM-DATE-TIME:%s.%03d%s\n", buf0, milli, buf1);
150         *prog_date_time += duration;
151     }
152     if (baseurl)
153         avio_printf(out, "%s", baseurl);
154     avio_printf(out, "%s\n", filename);
155
156     return 0;
157 }
158
159 void ff_hls_write_end_list (AVIOContext *out) {
160     if (!out)
161         return;
162     avio_printf(out, "#EXT-X-ENDLIST\n");
163 }
164