avformat/http: return EINVAL if ff_http_do_new_request is called with non-http URLContext
[ffmpeg.git] / 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, 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,URI=\"%s\"\n", name_id,
45                      is_default ? "YES" : "NO", filename);
46 }
47
48 void ff_hls_write_stream_info(AVStream *st, AVIOContext *out,
49                               int bandwidth, char *filename, char *agroup) {
50     if (!out || !filename)
51         return;
52
53     if (!bandwidth) {
54         av_log(NULL, AV_LOG_WARNING,
55                 "Bandwidth info not available, set audio and video bitrates\n");
56         return;
57     }
58
59     avio_printf(out, "#EXT-X-STREAM-INF:BANDWIDTH=%d", bandwidth);
60     if (st && st->codecpar->width > 0 && st->codecpar->height > 0)
61         avio_printf(out, ",RESOLUTION=%dx%d", st->codecpar->width,
62                 st->codecpar->height);
63     if (agroup && strlen(agroup) > 0)
64         avio_printf(out, ",AUDIO=\"group_%s\"", agroup);
65     avio_printf(out, "\n%s\n\n", filename);
66 }
67
68 void ff_hls_write_playlist_header(AVIOContext *out, int version, int allowcache,
69                                   int target_duration, int64_t sequence,
70                                   uint32_t playlist_type) {
71     if (!out)
72         return;
73     ff_hls_write_playlist_version(out, version);
74     if (allowcache == 0 || allowcache == 1) {
75         avio_printf(out, "#EXT-X-ALLOW-CACHE:%s\n", allowcache == 0 ? "NO" : "YES");
76     }
77     avio_printf(out, "#EXT-X-TARGETDURATION:%d\n", target_duration);
78     avio_printf(out, "#EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
79     av_log(NULL, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
80
81     if (playlist_type == PLAYLIST_TYPE_EVENT) {
82         avio_printf(out, "#EXT-X-PLAYLIST-TYPE:EVENT\n");
83     } else if (playlist_type == PLAYLIST_TYPE_VOD) {
84         avio_printf(out, "#EXT-X-PLAYLIST-TYPE:VOD\n");
85     }
86 }
87
88 void ff_hls_write_init_file(AVIOContext *out, char *filename,
89                             int byterange_mode, int64_t size, int64_t pos) {
90     avio_printf(out, "#EXT-X-MAP:URI=\"%s\"", filename);
91     if (byterange_mode) {
92         avio_printf(out, ",BYTERANGE=\"%"PRId64"@%"PRId64"\"", size, pos);
93     }
94     avio_printf(out, "\n");
95 }
96
97 int ff_hls_write_file_entry(AVIOContext *out, int insert_discont,
98                              int byterange_mode,
99                              double duration, int round_duration,
100                              int64_t size, int64_t pos, //Used only if HLS_SINGLE_FILE flag is set
101                              char *baseurl, //Ignored if NULL
102                              char *filename, double *prog_date_time) {
103     if (!out || !filename)
104         return AVERROR(EINVAL);
105
106     if (insert_discont) {
107         avio_printf(out, "#EXT-X-DISCONTINUITY\n");
108     }
109     if (round_duration)
110         avio_printf(out, "#EXTINF:%ld,\n",  lrint(duration));
111     else
112         avio_printf(out, "#EXTINF:%f,\n", duration);
113     if (byterange_mode)
114         avio_printf(out, "#EXT-X-BYTERANGE:%"PRId64"@%"PRId64"\n", size, pos);
115
116     if (prog_date_time) {
117         time_t tt, wrongsecs;
118         int milli;
119         struct tm *tm, tmpbuf;
120         char buf0[128], buf1[128];
121         tt = (int64_t)*prog_date_time;
122         milli = av_clip(lrint(1000*(*prog_date_time - tt)), 0, 999);
123         tm = localtime_r(&tt, &tmpbuf);
124         if (!strftime(buf0, sizeof(buf0), "%Y-%m-%dT%H:%M:%S", tm)) {
125             av_log(NULL, AV_LOG_DEBUG, "strftime error in ff_hls_write_file_entry\n");
126             return AVERROR_UNKNOWN;
127         }
128         if (!strftime(buf1, sizeof(buf1), "%z", tm) || buf1[1]<'0' ||buf1[1]>'2') {
129             int tz_min, dst = tm->tm_isdst;
130             tm = gmtime_r(&tt, &tmpbuf);
131             tm->tm_isdst = dst;
132             wrongsecs = mktime(tm);
133             tz_min = (FFABS(wrongsecs - tt) + 30) / 60;
134             snprintf(buf1, sizeof(buf1),
135                      "%c%02d%02d",
136                      wrongsecs <= tt ? '+' : '-',
137                      tz_min / 60,
138                      tz_min % 60);
139         }
140         avio_printf(out, "#EXT-X-PROGRAM-DATE-TIME:%s.%03d%s\n", buf0, milli, buf1);
141         *prog_date_time += duration;
142     }
143     if (baseurl)
144         avio_printf(out, "%s", baseurl);
145     avio_printf(out, "%s\n", filename);
146
147     return 0;
148 }
149
150 void ff_hls_write_end_list (AVIOContext *out) {
151     if (!out)
152         return;
153     avio_printf(out, "#EXT-X-ENDLIST\n");
154 }
155