FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hlsproto.c
Go to the documentation of this file.
1 /*
2  * Apple HTTP Live Streaming Protocol Handler
3  * Copyright (c) 2010 Martin Storsjo
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Apple HTTP Live Streaming Protocol Handler
25  * http://tools.ietf.org/html/draft-pantos-http-live-streaming
26  */
27 
28 #include "libavutil/avstring.h"
29 #include "libavutil/time.h"
30 #include "avformat.h"
31 #include "avio_internal.h"
32 #include "internal.h"
33 #include "url.h"
34 #include "version.h"
35 
36 /*
37  * An apple http stream consists of a playlist with media segment files,
38  * played sequentially. There may be several playlists with the same
39  * video content, in different bandwidth variants, that are played in
40  * parallel (preferably only one bandwidth variant at a time). In this case,
41  * the user supplied the url to a main playlist that only lists the variant
42  * playlists.
43  *
44  * If the main playlist doesn't point at any variants, we still create
45  * one anonymous toplevel variant for this, to maintain the structure.
46  */
47 
48 struct segment {
49  int64_t duration;
51 };
52 
53 struct variant {
54  int bandwidth;
56 };
57 
58 typedef struct HLSContext {
60  int64_t target_duration;
62  int finished;
64  struct segment **segments;
65  int n_variants;
66  struct variant **variants;
67  int cur_seq_no;
69  int64_t last_load_time;
70 } HLSContext;
71 
72 static int read_chomp_line(AVIOContext *s, char *buf, int maxlen)
73 {
74  int len = ff_get_line(s, buf, maxlen);
75  while (len > 0 && av_isspace(buf[len - 1]))
76  buf[--len] = '\0';
77  return len;
78 }
79 
81 {
82  int i;
83  for (i = 0; i < s->n_segments; i++)
84  av_freep(&s->segments[i]);
85  av_freep(&s->segments);
86  s->n_segments = 0;
87 }
88 
90 {
91  int i;
92  for (i = 0; i < s->n_variants; i++)
93  av_freep(&s->variants[i]);
94  av_freep(&s->variants);
95  s->n_variants = 0;
96 }
97 
98 struct variant_info {
99  char bandwidth[20];
100 };
101 
102 static void handle_variant_args(struct variant_info *info, const char *key,
103  int key_len, char **dest, int *dest_len)
104 {
105  if (!strncmp(key, "BANDWIDTH=", key_len)) {
106  *dest = info->bandwidth;
107  *dest_len = sizeof(info->bandwidth);
108  }
109 }
110 
111 static int parse_playlist(URLContext *h, const char *url)
112 {
113  HLSContext *s = h->priv_data;
114  AVIOContext *in;
115  int ret = 0, is_segment = 0, is_variant = 0, bandwidth = 0;
116  int64_t duration = 0;
117  char line[1024];
118  const char *ptr;
119 
120  if ((ret = ffio_open_whitelist(&in, url, AVIO_FLAG_READ,
123  return ret;
124 
125  read_chomp_line(in, line, sizeof(line));
126  if (strcmp(line, "#EXTM3U")) {
127  ret = AVERROR_INVALIDDATA;
128  goto fail;
129  }
130 
132  s->finished = 0;
133  while (!avio_feof(in)) {
134  read_chomp_line(in, line, sizeof(line));
135  if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
136  struct variant_info info = {{0}};
137  is_variant = 1;
139  &info);
140  bandwidth = atoi(info.bandwidth);
141  } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
142  s->target_duration = atoi(ptr) * AV_TIME_BASE;
143  } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
144  s->start_seq_no = atoi(ptr);
145  } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
146  s->finished = 1;
147  } else if (av_strstart(line, "#EXTINF:", &ptr)) {
148  is_segment = 1;
149  duration = atof(ptr) * AV_TIME_BASE;
150  } else if (av_strstart(line, "#", NULL)) {
151  continue;
152  } else if (line[0]) {
153  if (is_segment) {
154  struct segment *seg = av_malloc(sizeof(struct segment));
155  if (!seg) {
156  ret = AVERROR(ENOMEM);
157  goto fail;
158  }
159  seg->duration = duration;
160  ff_make_absolute_url(seg->url, sizeof(seg->url), url, line);
161  dynarray_add(&s->segments, &s->n_segments, seg);
162  is_segment = 0;
163  } else if (is_variant) {
164  struct variant *var = av_malloc(sizeof(struct variant));
165  if (!var) {
166  ret = AVERROR(ENOMEM);
167  goto fail;
168  }
169  var->bandwidth = bandwidth;
170  ff_make_absolute_url(var->url, sizeof(var->url), url, line);
171  dynarray_add(&s->variants, &s->n_variants, var);
172  is_variant = 0;
173  }
174  }
175  }
177 
178 fail:
179  avio_close(in);
180  return ret;
181 }
182 
183 static int hls_close(URLContext *h)
184 {
185  HLSContext *s = h->priv_data;
186 
189  ffurl_close(s->seg_hd);
190  return 0;
191 }
192 
193 static int hls_open(URLContext *h, const char *uri, int flags)
194 {
195  HLSContext *s = h->priv_data;
196  int ret, i;
197  const char *nested_url;
198 
199  if (flags & AVIO_FLAG_WRITE)
200  return AVERROR(ENOSYS);
201 
202  h->is_streamed = 1;
203 
204  if (av_strstart(uri, "hls+", &nested_url)) {
205  av_strlcpy(s->playlisturl, nested_url, sizeof(s->playlisturl));
206  } else if (av_strstart(uri, "hls://", &nested_url)) {
207  av_log(h, AV_LOG_ERROR,
208  "No nested protocol specified. Specify e.g. hls+http://%s\n",
209  nested_url);
210  ret = AVERROR(EINVAL);
211  goto fail;
212  } else {
213  av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri);
214  ret = AVERROR(EINVAL);
215  goto fail;
216  }
218  "Using the hls protocol is discouraged, please try using the "
219  "hls demuxer instead. The hls demuxer should be more complete "
220  "and work as well as the protocol implementation. (If not, "
221  "please report it.) To use the demuxer, simply use %s as url.\n",
222  s->playlisturl);
223 
224  if ((ret = parse_playlist(h, s->playlisturl)) < 0)
225  goto fail;
226 
227  if (s->n_segments == 0 && s->n_variants > 0) {
228  int max_bandwidth = 0, maxvar = -1;
229  for (i = 0; i < s->n_variants; i++) {
230  if (s->variants[i]->bandwidth > max_bandwidth || i == 0) {
231  max_bandwidth = s->variants[i]->bandwidth;
232  maxvar = i;
233  }
234  }
235  av_strlcpy(s->playlisturl, s->variants[maxvar]->url,
236  sizeof(s->playlisturl));
237  if ((ret = parse_playlist(h, s->playlisturl)) < 0)
238  goto fail;
239  }
240 
241  if (s->n_segments == 0) {
242  av_log(h, AV_LOG_WARNING, "Empty playlist\n");
243  ret = AVERROR(EIO);
244  goto fail;
245  }
246  s->cur_seq_no = s->start_seq_no;
247  if (!s->finished && s->n_segments >= 3)
248  s->cur_seq_no = s->start_seq_no + s->n_segments - 3;
249 
250  return 0;
251 
252 fail:
253  hls_close(h);
254  return ret;
255 }
256 
257 static int hls_read(URLContext *h, uint8_t *buf, int size)
258 {
259  HLSContext *s = h->priv_data;
260  const char *url;
261  int ret;
262  int64_t reload_interval;
263 
264 start:
265  if (s->seg_hd) {
266  ret = ffurl_read(s->seg_hd, buf, size);
267  if (ret > 0)
268  return ret;
269  }
270  if (s->seg_hd) {
271  ffurl_close(s->seg_hd);
272  s->seg_hd = NULL;
273  s->cur_seq_no++;
274  }
275  reload_interval = s->n_segments > 0 ?
276  s->segments[s->n_segments - 1]->duration :
277  s->target_duration;
278 retry:
279  if (!s->finished) {
280  int64_t now = av_gettime_relative();
281  if (now - s->last_load_time >= reload_interval) {
282  if ((ret = parse_playlist(h, s->playlisturl)) < 0)
283  return ret;
284  /* If we need to reload the playlist again below (if
285  * there's still no more segments), switch to a reload
286  * interval of half the target duration. */
287  reload_interval = s->target_duration / 2;
288  }
289  }
290  if (s->cur_seq_no < s->start_seq_no) {
292  "skipping %d segments ahead, expired from playlist\n",
293  s->start_seq_no - s->cur_seq_no);
294  s->cur_seq_no = s->start_seq_no;
295  }
296  if (s->cur_seq_no - s->start_seq_no >= s->n_segments) {
297  if (s->finished)
298  return AVERROR_EOF;
299  while (av_gettime_relative() - s->last_load_time < reload_interval) {
301  return AVERROR_EXIT;
302  av_usleep(100*1000);
303  }
304  goto retry;
305  }
306  url = s->segments[s->cur_seq_no - s->start_seq_no]->url,
307  av_log(h, AV_LOG_DEBUG, "opening %s\n", url);
311  if (ret < 0) {
313  return AVERROR_EXIT;
314  av_log(h, AV_LOG_WARNING, "Unable to open %s\n", url);
315  s->cur_seq_no++;
316  goto retry;
317  }
318  goto start;
319 }
320 
322  .name = "hls",
323  .url_open = hls_open,
324  .url_read = hls_read,
325  .url_close = hls_close,
327  .priv_data_size = sizeof(HLSContext),
328 };
#define NULL
Definition: coverity.c:32
void ff_make_absolute_url(char *buf, int size, const char *base, const char *rel)
Convert a relative url into an absolute url, given a base url.
Definition: url.c:80
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int bandwidth
Definition: hls.c:174
int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist, URLContext *parent)
Create an URLContext for accessing to the resource indicated by url, and open it. ...
Definition: avio.c:307
void(* ff_parse_key_val_cb)(void *context, const char *key, int key_len, char **dest, int *dest_len)
Callback function type for ff_parse_key_value.
Definition: internal.h:318
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
double duration
Definition: hlsenc.c:63
int is_streamed
true if streamed (no seek possible), default = false
Definition: url.h:45
AVIOInterruptCB interrupt_callback
Definition: url.h:47
#define AVIO_FLAG_READ
read-only
Definition: avio.h:660
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition: avstring.h:222
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:661
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:84
char * url
Definition: hls.c:71
#define MAX_URL_SIZE
Definition: internal.h:30
uint8_t
int n_variants
Definition: hls.c:188
#define av_malloc(s)
int64_t duration
Definition: movenc.c:63
static int flags
Definition: log.c:57
#define AVERROR_EOF
End of file.
Definition: error.h:55
ptrdiff_t size
Definition: opengl_enc.c:101
static void free_variant_list(HLSContext *s)
Definition: hlsproto.c:89
char playlisturl[MAX_URL_SIZE]
Definition: hlsproto.c:59
#define av_log(a,...)
struct variant ** variants
Definition: hls.c:189
int n_segments
Definition: hlsproto.c:63
URLContext * seg_hd
Definition: hlsproto.c:68
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
char bandwidth[20]
Definition: hls.c:322
const char * protocol_whitelist
Definition: url.h:49
#define AVERROR(e)
Definition: error.h:43
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:1112
static int parse_playlist(URLContext *h, const char *url)
Definition: hlsproto.c:111
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
#define URL_PROTOCOL_FLAG_NESTED_SCHEME
Definition: url.h:33
Definition: graph2dot.c:48
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:83
#define fail()
Definition: checkasm.h:109
Definition: hls.c:67
#define dynarray_add(tab, nb_ptr, elem)
Definition: internal.h:202
static void handle_variant_args(struct variant_info *info, const char *key, int key_len, char **dest, int *dest_len)
Definition: hlsproto.c:102
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
static int hls_read(URLContext *h, uint8_t *buf, int size)
Definition: hlsproto.c:257
HLSSegment * segments
Definition: hlsenc.c:149
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
Read a whole line of text from AVIOContext.
Definition: aviobuf.c:798
Libavformat version macros.
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:56
const char * protocol_blacklist
Definition: url.h:50
int ff_check_interrupt(AVIOInterruptCB *cb)
Check if the user has requested to interrupt a blocking function associated with cb.
Definition: avio.c:660
static int read_chomp_line(AVIOContext *s, char *buf, int maxlen)
Definition: hlsproto.c:72
char url[MAX_URL_SIZE]
Definition: hlsproto.c:55
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
void * buf
Definition: avisynth_c.h:690
Definition: url.h:38
void * priv_data
Definition: url.h:41
int64_t last_load_time
Definition: hlsproto.c:69
int ffio_open_whitelist(AVIOContext **s, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist)
Definition: aviobuf.c:1081
const char * name
Definition: url.h:55
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition: time.c:56
int ffurl_close(URLContext *h)
Definition: avio.c:465
void ff_parse_key_value(const char *str, ff_parse_key_val_cb callback_get_buf, void *context)
Parse a string with comma-separated key=value pairs.
Definition: utils.c:4767
int start_seq_no
Definition: hlsproto.c:61
int64_t target_duration
Definition: hlsproto.c:60
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:34
Main libavformat public API header.
struct segment ** segments
Definition: hlsproto.c:64
int cur_seq_no
Definition: hls.c:195
static void free_segment_list(HLSContext *s)
Definition: hlsproto.c:80
int len
static int hls_open(URLContext *h, const char *uri, int flags)
Definition: hlsproto.c:193
const URLProtocol ff_hls_protocol
Definition: hlsproto.c:321
static int hls_close(URLContext *h)
Definition: hlsproto.c:183
Definition: hls.c:173
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:690
unbuffered private I/O API
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:356
int finished
Definition: hlsproto.c:62
int64_t duration
Definition: hls.c:68
int ffurl_read(URLContext *h, unsigned char *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf...
Definition: avio.c:405