FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hlsplaylist.c
Go to the documentation of this file.
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 
27 
28 #include "avformat.h"
29 #include "hlsplaylist.h"
30 
32  if (!out)
33  return;
34  avio_printf(out, "#EXTM3U\n");
35  avio_printf(out, "#EXT-X-VERSION:%d\n", version);
36 }
37 
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 
49  int bandwidth, char *filename, char *agroup,
50  char *codecs, char *ccgroup) {
51 
52  if (!out || !filename)
53  return;
54 
55  if (!bandwidth) {
57  "Bandwidth info not available, set audio and video bitrates\n");
58  return;
59  }
60 
61  avio_printf(out, "#EXT-X-STREAM-INF:BANDWIDTH=%d", bandwidth);
62  if (st && st->codecpar->width > 0 && st->codecpar->height > 0)
63  avio_printf(out, ",RESOLUTION=%dx%d", st->codecpar->width,
64  st->codecpar->height);
65  if (codecs && strlen(codecs) > 0)
66  avio_printf(out, ",CODECS=\"%s\"", codecs);
67  if (agroup && strlen(agroup) > 0)
68  avio_printf(out, ",AUDIO=\"group_%s\"", agroup);
69  if (ccgroup && strlen(ccgroup) > 0)
70  avio_printf(out, ",CLOSED-CAPTIONS=\"%s\"", ccgroup);
71  avio_printf(out, "\n%s\n\n", filename);
72 }
73 
75  int target_duration, int64_t sequence,
76  uint32_t playlist_type) {
77  if (!out)
78  return;
79  ff_hls_write_playlist_version(out, version);
80  if (allowcache == 0 || allowcache == 1) {
81  avio_printf(out, "#EXT-X-ALLOW-CACHE:%s\n", allowcache == 0 ? "NO" : "YES");
82  }
83  avio_printf(out, "#EXT-X-TARGETDURATION:%d\n", target_duration);
84  avio_printf(out, "#EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
85  av_log(NULL, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
86 
87  if (playlist_type == PLAYLIST_TYPE_EVENT) {
88  avio_printf(out, "#EXT-X-PLAYLIST-TYPE:EVENT\n");
89  } else if (playlist_type == PLAYLIST_TYPE_VOD) {
90  avio_printf(out, "#EXT-X-PLAYLIST-TYPE:VOD\n");
91  }
92 }
93 
94 void ff_hls_write_init_file(AVIOContext *out, char *filename,
95  int byterange_mode, int64_t size, int64_t pos) {
96  avio_printf(out, "#EXT-X-MAP:URI=\"%s\"", filename);
97  if (byterange_mode) {
98  avio_printf(out, ",BYTERANGE=\"%"PRId64"@%"PRId64"\"", size, pos);
99  }
100  avio_printf(out, "\n");
101 }
102 
103 int ff_hls_write_file_entry(AVIOContext *out, int insert_discont,
104  int byterange_mode,
105  double duration, int round_duration,
106  int64_t size, int64_t pos, //Used only if HLS_SINGLE_FILE flag is set
107  char *baseurl, //Ignored if NULL
108  char *filename, double *prog_date_time) {
109  if (!out || !filename)
110  return AVERROR(EINVAL);
111 
112  if (insert_discont) {
113  avio_printf(out, "#EXT-X-DISCONTINUITY\n");
114  }
115  if (round_duration)
116  avio_printf(out, "#EXTINF:%ld,\n", lrint(duration));
117  else
118  avio_printf(out, "#EXTINF:%f,\n", duration);
119  if (byterange_mode)
120  avio_printf(out, "#EXT-X-BYTERANGE:%"PRId64"@%"PRId64"\n", size, pos);
121 
122  if (prog_date_time) {
123  time_t tt, wrongsecs;
124  int milli;
125  struct tm *tm, tmpbuf;
126  char buf0[128], buf1[128];
127  tt = (int64_t)*prog_date_time;
128  milli = av_clip(lrint(1000*(*prog_date_time - tt)), 0, 999);
129  tm = localtime_r(&tt, &tmpbuf);
130  if (!strftime(buf0, sizeof(buf0), "%Y-%m-%dT%H:%M:%S", tm)) {
131  av_log(NULL, AV_LOG_DEBUG, "strftime error in ff_hls_write_file_entry\n");
132  return AVERROR_UNKNOWN;
133  }
134  if (!strftime(buf1, sizeof(buf1), "%z", tm) || buf1[1]<'0' ||buf1[1]>'2') {
135  int tz_min, dst = tm->tm_isdst;
136  tm = gmtime_r(&tt, &tmpbuf);
137  tm->tm_isdst = dst;
138  wrongsecs = mktime(tm);
139  tz_min = (FFABS(wrongsecs - tt) + 30) / 60;
140  snprintf(buf1, sizeof(buf1),
141  "%c%02d%02d",
142  wrongsecs <= tt ? '+' : '-',
143  tz_min / 60,
144  tz_min % 60);
145  }
146  avio_printf(out, "#EXT-X-PROGRAM-DATE-TIME:%s.%03d%s\n", buf0, milli, buf1);
147  *prog_date_time += duration;
148  }
149  if (baseurl)
150  avio_printf(out, "%s", baseurl);
151  avio_printf(out, "%s\n", filename);
152 
153  return 0;
154 }
155 
157  if (!out)
158  return;
159  avio_printf(out, "#EXT-X-ENDLIST\n");
160 }
161 
#define NULL
Definition: coverity.c:32
Bytestream IO Context.
Definition: avio.h:161
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int ff_hls_write_file_entry(AVIOContext *out, int insert_discont, int byterange_mode, double duration, int round_duration, int64_t size, int64_t pos, char *baseurl, char *filename, double *prog_date_time)
Definition: hlsplaylist.c:103
int version
Definition: avisynth_c.h:766
static struct codec_string codecs[]
int width
Video only.
Definition: avcodec.h:3950
int64_t duration
Definition: movenc.c:63
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
void ff_hls_write_audio_rendition(AVIOContext *out, char *agroup, char *filename, int name_id, int is_default)
Definition: hlsplaylist.c:38
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
static struct tm * gmtime_r(const time_t *clock, struct tm *result)
Definition: time_internal.h:26
void ff_hls_write_playlist_header(AVIOContext *out, int version, int allowcache, int target_duration, int64_t sequence, uint32_t playlist_type)
Definition: hlsplaylist.c:74
static struct tm * localtime_r(const time_t *clock, struct tm *result)
Definition: time_internal.h:37
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
Stream structure.
Definition: avformat.h:873
void ff_hls_write_playlist_version(AVIOContext *out, int version)
Definition: hlsplaylist.c:31
#define snprintf
Definition: snprintf.h:34
void ff_hls_write_stream_info(AVStream *st, AVIOContext *out, int bandwidth, char *filename, char *agroup, char *codecs, char *ccgroup)
Definition: hlsplaylist.c:48
Main libavformat public API header.
void ff_hls_write_init_file(AVIOContext *out, char *filename, int byterange_mode, int64_t size, int64_t pos)
Definition: hlsplaylist.c:94
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
#define lrint
Definition: tablegen.h:53
void ff_hls_write_end_list(AVIOContext *out)
Definition: hlsplaylist.c:156
FILE * out
Definition: movenc.c:54
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1020
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2