FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/hlsplaylist.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 34 98 34.7%
Functions: 5 8 62.5%
Branches: 20 96 20.8%

Line Branch Exec Source
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 #include <time.h>
26
27 #include "libavutil/time_internal.h"
28
29 #include "avformat.h"
30 #include "hlsplaylist.h"
31
32 66 void ff_hls_write_playlist_version(AVIOContext *out, int version)
33 {
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (!out)
35 return;
36 66 avio_printf(out, "#EXTM3U\n");
37 66 avio_printf(out, "#EXT-X-VERSION:%d\n", version);
38 }
39
40 void ff_hls_write_audio_rendition(AVIOContext *out, const char *agroup,
41 const char *filename, const char *language,
42 int name_id, int is_default, int nb_channels)
43 {
44 if (!out || !agroup || !filename)
45 return;
46
47 avio_printf(out, "#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID=\"group_%s\"", agroup);
48 avio_printf(out, ",NAME=\"audio_%d\",DEFAULT=%s,", name_id, is_default ? "YES" : "NO");
49 if (language) {
50 avio_printf(out, "LANGUAGE=\"%s\",", language);
51 }
52 if (nb_channels) {
53 avio_printf(out, "CHANNELS=\"%d\",", nb_channels);
54 }
55 avio_printf(out, "URI=\"%s\"\n", filename);
56 }
57
58 void ff_hls_write_subtitle_rendition(AVIOContext *out, const char *sgroup,
59 const char *filename, const char *language,
60 int name_id, int is_default)
61 {
62 if (!out || !filename)
63 return;
64
65 avio_printf(out, "#EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID=\"%s\"", sgroup);
66 avio_printf(out, ",NAME=\"subtitle_%d\",DEFAULT=%s,", name_id, is_default ? "YES" : "NO");
67 if (language) {
68 avio_printf(out, "LANGUAGE=\"%s\",", language);
69 }
70 avio_printf(out, "URI=\"%s\"\n", filename);
71 }
72
73 void ff_hls_write_stream_info(AVStream *st, AVIOContext *out, int bandwidth,
74 const char *filename, const char *agroup,
75 const char *codecs, const char *ccgroup,
76 const char *sgroup)
77 {
78 if (!out || !filename)
79 return;
80
81 if (!bandwidth) {
82 av_log(NULL, AV_LOG_WARNING,
83 "Bandwidth info not available, set audio and video bitrates\n");
84 return;
85 }
86
87 avio_printf(out, "#EXT-X-STREAM-INF:BANDWIDTH=%d", bandwidth);
88 if (st && st->codecpar->width > 0 && st->codecpar->height > 0)
89 avio_printf(out, ",RESOLUTION=%dx%d", st->codecpar->width,
90 st->codecpar->height);
91 if (codecs && codecs[0])
92 avio_printf(out, ",CODECS=\"%s\"", codecs);
93 if (agroup && agroup[0])
94 avio_printf(out, ",AUDIO=\"group_%s\"", agroup);
95 if (ccgroup && ccgroup[0])
96 avio_printf(out, ",CLOSED-CAPTIONS=\"%s\"", ccgroup);
97 if (sgroup && sgroup[0])
98 avio_printf(out, ",SUBTITLES=\"%s\"", sgroup);
99 avio_printf(out, "\n%s\n\n", filename);
100 }
101
102 66 void ff_hls_write_playlist_header(AVIOContext *out, int version, int allowcache,
103 int target_duration, int64_t sequence,
104 uint32_t playlist_type, int iframe_mode)
105 {
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (!out)
107 return;
108 66 ff_hls_write_playlist_version(out, version);
109
2/4
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 66 times.
66 if (allowcache == 0 || allowcache == 1) {
110 avio_printf(out, "#EXT-X-ALLOW-CACHE:%s\n", allowcache == 0 ? "NO" : "YES");
111 }
112 66 avio_printf(out, "#EXT-X-TARGETDURATION:%d\n", target_duration);
113 66 avio_printf(out, "#EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
114 66 av_log(NULL, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
115
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (playlist_type == PLAYLIST_TYPE_EVENT) {
117 avio_printf(out, "#EXT-X-PLAYLIST-TYPE:EVENT\n");
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 } else if (playlist_type == PLAYLIST_TYPE_VOD) {
119 avio_printf(out, "#EXT-X-PLAYLIST-TYPE:VOD\n");
120 }
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (iframe_mode) {
122 avio_printf(out, "#EXT-X-I-FRAMES-ONLY\n");
123 }
124 }
125
126 5 void ff_hls_write_init_file(AVIOContext *out, const char *filename,
127 int byterange_mode, int64_t size, int64_t pos)
128 {
129 5 avio_printf(out, "#EXT-X-MAP:URI=\"%s\"", filename);
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (byterange_mode) {
131 avio_printf(out, ",BYTERANGE=\"%"PRId64"@%"PRId64"\"", size, pos);
132 }
133 5 avio_printf(out, "\n");
134 5 }
135
136 269 int ff_hls_write_file_entry(AVIOContext *out, int insert_discont,
137 int byterange_mode, double duration,
138 int round_duration, int64_t size,
139 int64_t pos /* Used only if HLS_SINGLE_FILE flag is set */,
140 const char *baseurl /* Ignored if NULL */,
141 const char *filename, double *prog_date_time,
142 int64_t video_keyframe_size, int64_t video_keyframe_pos,
143 int iframe_mode)
144 {
145
2/4
✓ Branch 0 taken 269 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 269 times.
269 if (!out || !filename)
146 return AVERROR(EINVAL);
147
148
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 267 times.
269 if (insert_discont) {
149 2 avio_printf(out, "#EXT-X-DISCONTINUITY\n");
150 }
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 269 times.
269 if (round_duration)
152 avio_printf(out, "#EXTINF:%ld,\n", lrint(duration));
153 else
154 269 avio_printf(out, "#EXTINF:%f,\n", duration);
155
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 159 times.
269 if (byterange_mode)
156
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 110 times.
110 avio_printf(out, "#EXT-X-BYTERANGE:%"PRId64"@%"PRId64"\n", iframe_mode ? video_keyframe_size : size,
157 iframe_mode ? video_keyframe_pos : pos);
158
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 269 times.
269 if (prog_date_time) {
160 time_t tt, wrongsecs;
161 int milli;
162 struct tm *tm, tmpbuf;
163 char buf0[128], buf1[128];
164 tt = (int64_t)*prog_date_time;
165 milli = av_clip(lrint(1000*(*prog_date_time - tt)), 0, 999);
166 tm = localtime_r(&tt, &tmpbuf);
167 if (!strftime(buf0, sizeof(buf0), "%Y-%m-%dT%H:%M:%S", tm)) {
168 av_log(NULL, AV_LOG_DEBUG, "strftime error in ff_hls_write_file_entry\n");
169 return AVERROR_UNKNOWN;
170 }
171 if (!strftime(buf1, sizeof(buf1), "%z", tm) || buf1[1]<'0' ||buf1[1]>'2') {
172 int tz_min, dst = tm->tm_isdst;
173 tm = gmtime_r(&tt, &tmpbuf);
174 tm->tm_isdst = dst;
175 wrongsecs = mktime(tm);
176 tz_min = (FFABS(wrongsecs - tt) + 30) / 60;
177 snprintf(buf1, sizeof(buf1),
178 "%c%02d%02d",
179 wrongsecs <= tt ? '+' : '-',
180 tz_min / 60,
181 tz_min % 60);
182 }
183 avio_printf(out, "#EXT-X-PROGRAM-DATE-TIME:%s.%03d%s\n", buf0, milli, buf1);
184 *prog_date_time += duration;
185 }
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 269 times.
269 if (baseurl)
187 avio_printf(out, "%s", baseurl);
188 269 avio_printf(out, "%s\n", filename);
189
190 269 return 0;
191 }
192
193 9 void ff_hls_write_end_list(AVIOContext *out)
194 {
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!out)
196 return;
197 9 avio_printf(out, "#EXT-X-ENDLIST\n");
198 }
199
200