| 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 | 90 | void ff_hls_write_playlist_version(AVIOContext *out, int version) | |
| 33 | { | ||
| 34 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
|
90 | if (!out) |
| 35 | ✗ | return; | |
| 36 | 90 | avio_printf(out, "#EXTM3U\n"); | |
| 37 | 90 | 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 | 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 | ✗ | 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 | ✗ | if (!out || !filename) | |
| 85 | ✗ | return; | |
| 86 | |||
| 87 | ✗ | 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 | ✗ | avio_printf(out, "#EXT-X-STREAM-INF:BANDWIDTH=%d", bandwidth); | |
| 94 | ✗ | if (avg_bandwidth) | |
| 95 | ✗ | avio_printf(out, ",AVERAGE-BANDWIDTH=%d", avg_bandwidth); | |
| 96 | ✗ | if (st && st->codecpar->width > 0 && st->codecpar->height > 0) | |
| 97 | ✗ | avio_printf(out, ",RESOLUTION=%dx%d", st->codecpar->width, | |
| 98 | ✗ | st->codecpar->height); | |
| 99 | ✗ | if (codecs && codecs[0]) | |
| 100 | ✗ | avio_printf(out, ",CODECS=\"%s\"", codecs); | |
| 101 | ✗ | if (agroup && agroup[0]) | |
| 102 | ✗ | avio_printf(out, ",AUDIO=\"group_%s\"", agroup); | |
| 103 | ✗ | if (ccgroup && ccgroup[0]) | |
| 104 | ✗ | avio_printf(out, ",CLOSED-CAPTIONS=\"%s\"", ccgroup); | |
| 105 | ✗ | if (sgroup && sgroup[0]) | |
| 106 | ✗ | avio_printf(out, ",SUBTITLES=\"%s\"", sgroup); | |
| 107 | ✗ | avio_printf(out, "\n%s\n\n", filename); | |
| 108 | } | ||
| 109 | |||
| 110 | 90 | 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 90 times.
|
90 | if (!out) |
| 115 | ✗ | return; | |
| 116 | 90 | ff_hls_write_playlist_version(out, version); | |
| 117 |
2/4✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 90 times.
|
90 | if (allowcache == 0 || allowcache == 1) { |
| 118 | ✗ | avio_printf(out, "#EXT-X-ALLOW-CACHE:%s\n", allowcache == 0 ? "NO" : "YES"); | |
| 119 | } | ||
| 120 | 90 | avio_printf(out, "#EXT-X-TARGETDURATION:%d\n", target_duration); | |
| 121 | 90 | avio_printf(out, "#EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence); | |
| 122 | 90 | 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 80 times.
|
90 | 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 79 times.
|
80 | } else if (playlist_type == PLAYLIST_TYPE_VOD) { |
| 127 | 1 | avio_printf(out, "#EXT-X-PLAYLIST-TYPE:VOD\n"); | |
| 128 | } | ||
| 129 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
|
90 | if (iframe_mode) { |
| 130 | ✗ | avio_printf(out, "#EXT-X-I-FRAMES-ONLY\n"); | |
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | 6 | void ff_hls_write_init_file(AVIOContext *out, const char *filename, | |
| 135 | int byterange_mode, int64_t size, int64_t pos) | ||
| 136 | { | ||
| 137 | 6 | avio_printf(out, "#EXT-X-MAP:URI=\"%s\"", filename); | |
| 138 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (byterange_mode) { |
| 139 | ✗ | avio_printf(out, ",BYTERANGE=\"%"PRId64"@%"PRId64"\"", size, pos); | |
| 140 | } | ||
| 141 | 6 | avio_printf(out, "\n"); | |
| 142 | 6 | } | |
| 143 | |||
| 144 | 331 | 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 | int64_t video_keyframe_size, int64_t video_keyframe_pos, | ||
| 151 | int iframe_mode) | ||
| 152 | { | ||
| 153 |
2/4✓ Branch 0 taken 331 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 331 times.
|
331 | if (!out || !filename) |
| 154 | ✗ | return AVERROR(EINVAL); | |
| 155 | |||
| 156 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 329 times.
|
331 | if (insert_discont) { |
| 157 | 2 | avio_printf(out, "#EXT-X-DISCONTINUITY\n"); | |
| 158 | } | ||
| 159 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 325 times.
|
331 | if (round_duration) |
| 160 | 6 | avio_printf(out, "#EXTINF:%ld,\n", lrint(duration)); | |
| 161 | else | ||
| 162 | 325 | avio_printf(out, "#EXTINF:%f,\n", duration); | |
| 163 |
2/2✓ Branch 0 taken 110 times.
✓ Branch 1 taken 221 times.
|
331 | if (byterange_mode) |
| 164 |
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, |
| 165 | iframe_mode ? video_keyframe_pos : pos); | ||
| 166 | |||
| 167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 331 times.
|
331 | if (prog_date_time) { |
| 168 | time_t tt, wrongsecs; | ||
| 169 | int milli; | ||
| 170 | struct tm *tm, tmpbuf; | ||
| 171 | char buf0[128], buf1[128]; | ||
| 172 | ✗ | tt = (int64_t)*prog_date_time; | |
| 173 | ✗ | milli = av_clip(lrint(1000*(*prog_date_time - tt)), 0, 999); | |
| 174 | ✗ | tm = localtime_r(&tt, &tmpbuf); | |
| 175 | ✗ | if (!strftime(buf0, sizeof(buf0), "%Y-%m-%dT%H:%M:%S", tm)) { | |
| 176 | ✗ | av_log(NULL, AV_LOG_DEBUG, "strftime error in ff_hls_write_file_entry\n"); | |
| 177 | ✗ | return AVERROR_UNKNOWN; | |
| 178 | } | ||
| 179 | ✗ | if (!strftime(buf1, sizeof(buf1), "%z", tm) || buf1[1]<'0' ||buf1[1]>'2') { | |
| 180 | ✗ | int tz_min, dst = tm->tm_isdst; | |
| 181 | ✗ | tm = gmtime_r(&tt, &tmpbuf); | |
| 182 | ✗ | tm->tm_isdst = dst; | |
| 183 | ✗ | wrongsecs = mktime(tm); | |
| 184 | ✗ | tz_min = (FFABS(wrongsecs - tt) + 30) / 60; | |
| 185 | ✗ | snprintf(buf1, sizeof(buf1), | |
| 186 | "%c%02d%02d", | ||
| 187 | ✗ | wrongsecs <= tt ? '+' : '-', | |
| 188 | tz_min / 60, | ||
| 189 | tz_min % 60); | ||
| 190 | } | ||
| 191 | ✗ | avio_printf(out, "#EXT-X-PROGRAM-DATE-TIME:%s.%03d%s\n", buf0, milli, buf1); | |
| 192 | ✗ | *prog_date_time += duration; | |
| 193 | } | ||
| 194 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 331 times.
|
331 | if (baseurl) |
| 195 | ✗ | avio_printf(out, "%s", baseurl); | |
| 196 | 331 | avio_printf(out, "%s\n", filename); | |
| 197 | |||
| 198 | 331 | return 0; | |
| 199 | } | ||
| 200 | |||
| 201 | 16 | void ff_hls_write_end_list(AVIOContext *out) | |
| 202 | { | ||
| 203 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (!out) |
| 204 | ✗ | return; | |
| 205 | 16 | avio_printf(out, "#EXT-X-ENDLIST\n"); | |
| 206 | } | ||
| 207 |