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 "internal.h"
32 #include "url.h"
33 #include "version.h"
34 
35 /*
36  * An apple http stream consists of a playlist with media segment files,
37  * played sequentially. There may be several playlists with the same
38  * video content, in different bandwidth variants, that are played in
39  * parallel (preferably only one bandwidth variant at a time). In this case,
40  * the user supplied the url to a main playlist that only lists the variant
41  * playlists.
42  *
43  * If the main playlist doesn't point at any variants, we still create
44  * one anonymous toplevel variant for this, to maintain the structure.
45  */
46 
47 struct segment {
48  int64_t duration;
50 };
51 
52 struct variant {
53  int bandwidth;
55 };
56 
57 typedef struct HLSContext {
59  int64_t target_duration;
61  int finished;
63  struct segment **segments;
64  int n_variants;
65  struct variant **variants;
66  int cur_seq_no;
68  int64_t last_load_time;
69 } HLSContext;
70 
71 static int read_chomp_line(AVIOContext *s, char *buf, int maxlen)
72 {
73  int len = ff_get_line(s, buf, maxlen);
74  while (len > 0 && av_isspace(buf[len - 1]))
75  buf[--len] = '\0';
76  return len;
77 }
78 
80 {
81  int i;
82  for (i = 0; i < s->n_segments; i++)
83  av_freep(&s->segments[i]);
84  av_freep(&s->segments);
85  s->n_segments = 0;
86 }
87 
89 {
90  int i;
91  for (i = 0; i < s->n_variants; i++)
92  av_freep(&s->variants[i]);
93  av_freep(&s->variants);
94  s->n_variants = 0;
95 }
96 
97 struct variant_info {
98  char bandwidth[20];
99 };
100 
101 static void handle_variant_args(struct variant_info *info, const char *key,
102  int key_len, char **dest, int *dest_len)
103 {
104  if (!strncmp(key, "BANDWIDTH=", key_len)) {
105  *dest = info->bandwidth;
106  *dest_len = sizeof(info->bandwidth);
107  }
108 }
109 
110 static int parse_playlist(URLContext *h, const char *url)
111 {
112  HLSContext *s = h->priv_data;
113  AVIOContext *in;
114  int ret = 0, is_segment = 0, is_variant = 0, bandwidth = 0;
115  int64_t duration = 0;
116  char line[1024];
117  const char *ptr;
118 
119  if ((ret = avio_open2(&in, url, AVIO_FLAG_READ,
120  &h->interrupt_callback, NULL)) < 0)
121  return ret;
122 
123  read_chomp_line(in, line, sizeof(line));
124  if (strcmp(line, "#EXTM3U")) {
125  ret = AVERROR_INVALIDDATA;
126  goto fail;
127  }
128 
130  s->finished = 0;
131  while (!avio_feof(in)) {
132  read_chomp_line(in, line, sizeof(line));
133  if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
134  struct variant_info info = {{0}};
135  is_variant = 1;
137  &info);
138  bandwidth = atoi(info.bandwidth);
139  } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
140  s->target_duration = atoi(ptr) * AV_TIME_BASE;
141  } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
142  s->start_seq_no = atoi(ptr);
143  } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
144  s->finished = 1;
145  } else if (av_strstart(line, "#EXTINF:", &ptr)) {
146  is_segment = 1;
147  duration = atof(ptr) * AV_TIME_BASE;
148  } else if (av_strstart(line, "#", NULL)) {
149  continue;
150  } else if (line[0]) {
151  if (is_segment) {
152  struct segment *seg = av_malloc(sizeof(struct segment));
153  if (!seg) {
154  ret = AVERROR(ENOMEM);
155  goto fail;
156  }
157  seg->duration = duration;
158  ff_make_absolute_url(seg->url, sizeof(seg->url), url, line);
159  dynarray_add(&s->segments, &s->n_segments, seg);
160  is_segment = 0;
161  } else if (is_variant) {
162  struct variant *var = av_malloc(sizeof(struct variant));
163  if (!var) {
164  ret = AVERROR(ENOMEM);
165  goto fail;
166  }
167  var->bandwidth = bandwidth;
168  ff_make_absolute_url(var->url, sizeof(var->url), url, line);
169  dynarray_add(&s->variants, &s->n_variants, var);
170  is_variant = 0;
171  }
172  }
173  }
175 
176 fail:
177  avio_close(in);
178  return ret;
179 }
180 
181 static int hls_close(URLContext *h)
182 {
183  HLSContext *s = h->priv_data;
184 
187  ffurl_close(s->seg_hd);
188  return 0;
189 }
190 
191 static int hls_open(URLContext *h, const char *uri, int flags)
192 {
193  HLSContext *s = h->priv_data;
194  int ret, i;
195  const char *nested_url;
196 
197  if (flags & AVIO_FLAG_WRITE)
198  return AVERROR(ENOSYS);
199 
200  h->is_streamed = 1;
201 
202  if (av_strstart(uri, "hls+", &nested_url)) {
203  av_strlcpy(s->playlisturl, nested_url, sizeof(s->playlisturl));
204  } else if (av_strstart(uri, "hls://", &nested_url)) {
205  av_log(h, AV_LOG_ERROR,
206  "No nested protocol specified. Specify e.g. hls+http://%s\n",
207  nested_url);
208  ret = AVERROR(EINVAL);
209  goto fail;
210  } else {
211  av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri);
212  ret = AVERROR(EINVAL);
213  goto fail;
214  }
216  "Using the hls protocol is discouraged, please try using the "
217  "hls demuxer instead. The hls demuxer should be more complete "
218  "and work as well as the protocol implementation. (If not, "
219  "please report it.) To use the demuxer, simply use %s as url.\n",
220  s->playlisturl);
221 
222  if ((ret = parse_playlist(h, s->playlisturl)) < 0)
223  goto fail;
224 
225  if (s->n_segments == 0 && s->n_variants > 0) {
226  int max_bandwidth = 0, maxvar = -1;
227  for (i = 0; i < s->n_variants; i++) {
228  if (s->variants[i]->bandwidth > max_bandwidth || i == 0) {
229  max_bandwidth = s->variants[i]->bandwidth;
230  maxvar = i;
231  }
232  }
233  av_strlcpy(s->playlisturl, s->variants[maxvar]->url,
234  sizeof(s->playlisturl));
235  if ((ret = parse_playlist(h, s->playlisturl)) < 0)
236  goto fail;
237  }
238 
239  if (s->n_segments == 0) {
240  av_log(h, AV_LOG_WARNING, "Empty playlist\n");
241  ret = AVERROR(EIO);
242  goto fail;
243  }
244  s->cur_seq_no = s->start_seq_no;
245  if (!s->finished && s->n_segments >= 3)
246  s->cur_seq_no = s->start_seq_no + s->n_segments - 3;
247 
248  return 0;
249 
250 fail:
251  hls_close(h);
252  return ret;
253 }
254 
255 static int hls_read(URLContext *h, uint8_t *buf, int size)
256 {
257  HLSContext *s = h->priv_data;
258  const char *url;
259  int ret;
260  int64_t reload_interval;
261 
262 start:
263  if (s->seg_hd) {
264  ret = ffurl_read(s->seg_hd, buf, size);
265  if (ret > 0)
266  return ret;
267  }
268  if (s->seg_hd) {
269  ffurl_close(s->seg_hd);
270  s->seg_hd = NULL;
271  s->cur_seq_no++;
272  }
273  reload_interval = s->n_segments > 0 ?
274  s->segments[s->n_segments - 1]->duration :
275  s->target_duration;
276 retry:
277  if (!s->finished) {
278  int64_t now = av_gettime_relative();
279  if (now - s->last_load_time >= reload_interval) {
280  if ((ret = parse_playlist(h, s->playlisturl)) < 0)
281  return ret;
282  /* If we need to reload the playlist again below (if
283  * there's still no more segments), switch to a reload
284  * interval of half the target duration. */
285  reload_interval = s->target_duration / 2;
286  }
287  }
288  if (s->cur_seq_no < s->start_seq_no) {
290  "skipping %d segments ahead, expired from playlist\n",
291  s->start_seq_no - s->cur_seq_no);
292  s->cur_seq_no = s->start_seq_no;
293  }
294  if (s->cur_seq_no - s->start_seq_no >= s->n_segments) {
295  if (s->finished)
296  return AVERROR_EOF;
297  while (av_gettime_relative() - s->last_load_time < reload_interval) {
299  return AVERROR_EXIT;
300  av_usleep(100*1000);
301  }
302  goto retry;
303  }
304  url = s->segments[s->cur_seq_no - s->start_seq_no]->url,
305  av_log(h, AV_LOG_DEBUG, "opening %s\n", url);
306  ret = ffurl_open(&s->seg_hd, url, AVIO_FLAG_READ,
307  &h->interrupt_callback, NULL);
308  if (ret < 0) {
310  return AVERROR_EXIT;
311  av_log(h, AV_LOG_WARNING, "Unable to open %s\n", url);
312  s->cur_seq_no++;
313  goto retry;
314  }
315  goto start;
316 }
317 
319  .name = "hls",
320  .url_open = hls_open,
321  .url_read = hls_read,
322  .url_close = hls_close,
324  .priv_data_size = sizeof(HLSContext),
325 };
#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:631
Bytestream IO Context.
Definition: avio.h:111
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int bandwidth
Definition: hls.c:156
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:223
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
double duration
Definition: hlsenc.c:42
int is_streamed
true if streamed (no seek possible), default = false
Definition: url.h:46
AVIOInterruptCB interrupt_callback
Definition: url.h:48
#define AVIO_FLAG_READ
read-only
Definition: avio.h:460
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:461
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:76
char * url
Definition: hls.c:72
#define MAX_URL_SIZE
Definition: internal.h:28
uint8_t
int n_variants
Definition: hls.c:169
#define av_malloc(s)
int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition: avstring.c:330
#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:88
static int64_t duration
Definition: ffplay.c:321
char playlisturl[MAX_URL_SIZE]
Definition: hlsproto.c:58
#define av_log(a,...)
struct variant ** variants
Definition: hls.c:170
int n_segments
Definition: hlsproto.c:62
URLContext * seg_hd
Definition: hlsproto.c:67
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
char bandwidth[20]
Definition: hls.c:282
#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:937
static int parse_playlist(URLContext *h, const char *url)
Definition: hlsproto.c:110
#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:34
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
Definition: hls.c:68
#define dynarray_add(tab, nb_ptr, elem)
Definition: internal.h:105
static void handle_variant_args(struct variant_info *info, const char *key, int key_len, char **dest, int *dest_len)
Definition: hlsproto.c:101
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:247
URLProtocol ff_hls_protocol
Definition: hlsproto.c:318
static int hls_read(URLContext *h, uint8_t *buf, int size)
Definition: hlsproto.c:255
ret
Definition: avfilter.c:974
HLSSegment * segments
Definition: hlsenc.c:84
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
Read a whole line of text from AVIOContext.
Definition: aviobuf.c:697
Libavformat version macros.
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:56
int ff_check_interrupt(AVIOInterruptCB *cb)
Check if the user has requested to interrup a blocking function associated with cb.
Definition: avio.c:541
static int read_chomp_line(AVIOContext *s, char *buf, int maxlen)
Definition: hlsproto.c:71
char url[MAX_URL_SIZE]
Definition: hlsproto.c:54
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:553
Definition: url.h:39
void * priv_data
Definition: url.h:42
int64_t last_load_time
Definition: hlsproto.c:68
int avio_open2(AVIOContext **s, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:914
const char * name
Definition: url.h:53
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition: time.c:56
static int flags
Definition: cpu.c:47
int ffurl_close(URLContext *h)
Definition: avio.c:392
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:4031
int start_seq_no
Definition: hlsproto.c:60
int64_t target_duration
Definition: hlsproto.c:59
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:63
int ffurl_open(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create an URLContext for accessing to the resource indicated by url, and open it. ...
Definition: avio.c:272
int cur_seq_no
Definition: hls.c:176
static void free_segment_list(HLSContext *s)
Definition: hlsproto.c:79
int len
static int hls_open(URLContext *h, const char *uri, int flags)
Definition: hlsproto.c:191
static int hls_close(URLContext *h)
Definition: hlsproto.c:181
Definition: hls.c:155
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:553
unbuffered private I/O API
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:300
int finished
Definition: hlsproto.c:61
int64_t duration
Definition: hls.c:69
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:333