FFmpeg coverage


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