Statistics
| Branch: | Revision:

ffmpeg / libavformat / applehttpproto.c @ bc371aca

History | View | Annotate | Download (8.96 KB)

1
/*
2
 * Apple HTTP Live Streaming Protocol Handler
3
 * Copyright (c) 2010 Martin Storsjo
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav 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
 * Libav 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 Libav; 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
#define _XOPEN_SOURCE 600
29
#include "libavutil/avstring.h"
30
#include "avformat.h"
31
#include "internal.h"
32
#include "url.h"
33
#include <unistd.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 (preferrably 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
    int duration;
49
    char url[MAX_URL_SIZE];
50
};
51

    
52
struct variant {
53
    int bandwidth;
54
    char url[MAX_URL_SIZE];
55
};
56

    
57
typedef struct AppleHTTPContext {
58
    char playlisturl[MAX_URL_SIZE];
59
    int target_duration;
60
    int start_seq_no;
61
    int finished;
62
    int n_segments;
63
    struct segment **segments;
64
    int n_variants;
65
    struct variant **variants;
66
    int cur_seq_no;
67
    URLContext *seg_hd;
68
    int64_t last_load_time;
69
} AppleHTTPContext;
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 && isspace(buf[len - 1]))
75
        buf[--len] = '\0';
76
    return len;
77
}
78

    
79
static void free_segment_list(AppleHTTPContext *s)
80
{
81
    int i;
82
    for (i = 0; i < s->n_segments; i++)
83
        av_free(s->segments[i]);
84
    av_freep(&s->segments);
85
    s->n_segments = 0;
86
}
87

    
88
static void free_variant_list(AppleHTTPContext *s)
89
{
90
    int i;
91
    for (i = 0; i < s->n_variants; i++)
92
        av_free(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
    AppleHTTPContext *s = h->priv_data;
113
    AVIOContext *in;
114
    int ret = 0, duration = 0, is_segment = 0, is_variant = 0, bandwidth = 0;
115
    char line[1024];
116
    const char *ptr;
117

    
118
    if ((ret = avio_open(&in, url, URL_RDONLY)) < 0)
119
        return ret;
120

    
121
    read_chomp_line(in, line, sizeof(line));
122
    if (strcmp(line, "#EXTM3U"))
123
        return AVERROR_INVALIDDATA;
124

    
125
    free_segment_list(s);
126
    s->finished = 0;
127
    while (!in->eof_reached) {
128
        read_chomp_line(in, line, sizeof(line));
129
        if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
130
            struct variant_info info = {{0}};
131
            is_variant = 1;
132
            ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args,
133
                               &info);
134
            bandwidth = atoi(info.bandwidth);
135
        } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
136
            s->target_duration = atoi(ptr);
137
        } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
138
            s->start_seq_no = atoi(ptr);
139
        } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
140
            s->finished = 1;
141
        } else if (av_strstart(line, "#EXTINF:", &ptr)) {
142
            is_segment = 1;
143
            duration = atoi(ptr);
144
        } else if (av_strstart(line, "#", NULL)) {
145
            continue;
146
        } else if (line[0]) {
147
            if (is_segment) {
148
                struct segment *seg = av_malloc(sizeof(struct segment));
149
                if (!seg) {
150
                    ret = AVERROR(ENOMEM);
151
                    goto fail;
152
                }
153
                seg->duration = duration;
154
                ff_make_absolute_url(seg->url, sizeof(seg->url), url, line);
155
                dynarray_add(&s->segments, &s->n_segments, seg);
156
                is_segment = 0;
157
            } else if (is_variant) {
158
                struct variant *var = av_malloc(sizeof(struct variant));
159
                if (!var) {
160
                    ret = AVERROR(ENOMEM);
161
                    goto fail;
162
                }
163
                var->bandwidth = bandwidth;
164
                ff_make_absolute_url(var->url, sizeof(var->url), url, line);
165
                dynarray_add(&s->variants, &s->n_variants, var);
166
                is_variant = 0;
167
            }
168
        }
169
    }
170
    s->last_load_time = av_gettime();
171

    
172
fail:
173
    avio_close(in);
174
    return ret;
175
}
176

    
177
static int applehttp_open(URLContext *h, const char *uri, int flags)
178
{
179
    AppleHTTPContext *s;
180
    int ret, i;
181
    const char *nested_url;
182

    
183
    if (flags & (URL_WRONLY | URL_RDWR))
184
        return AVERROR_NOTSUPP;
185

    
186
    s = av_mallocz(sizeof(AppleHTTPContext));
187
    if (!s)
188
        return AVERROR(ENOMEM);
189
    h->priv_data = s;
190
    h->is_streamed = 1;
191

    
192
    if (av_strstart(uri, "applehttp+", &nested_url)) {
193
        av_strlcpy(s->playlisturl, nested_url, sizeof(s->playlisturl));
194
    } else if (av_strstart(uri, "applehttp://", &nested_url)) {
195
        av_strlcpy(s->playlisturl, "http://", sizeof(s->playlisturl));
196
        av_strlcat(s->playlisturl, nested_url, sizeof(s->playlisturl));
197
    } else {
198
        av_log(NULL, AV_LOG_ERROR, "Unsupported url %s\n", uri);
199
        ret = AVERROR(EINVAL);
200
        goto fail;
201
    }
202

    
203
    if ((ret = parse_playlist(h, s->playlisturl)) < 0)
204
        goto fail;
205

    
206
    if (s->n_segments == 0 && s->n_variants > 0) {
207
        int max_bandwidth = 0, maxvar = -1;
208
        for (i = 0; i < s->n_variants; i++) {
209
            if (s->variants[i]->bandwidth > max_bandwidth || i == 0) {
210
                max_bandwidth = s->variants[i]->bandwidth;
211
                maxvar = i;
212
            }
213
        }
214
        av_strlcpy(s->playlisturl, s->variants[maxvar]->url,
215
                   sizeof(s->playlisturl));
216
        if ((ret = parse_playlist(h, s->playlisturl)) < 0)
217
            goto fail;
218
    }
219

    
220
    if (s->n_segments == 0) {
221
        av_log(NULL, AV_LOG_WARNING, "Empty playlist\n");
222
        ret = AVERROR(EIO);
223
        goto fail;
224
    }
225
    s->cur_seq_no = s->start_seq_no;
226
    if (!s->finished && s->n_segments >= 3)
227
        s->cur_seq_no = s->start_seq_no + s->n_segments - 3;
228

    
229
    return 0;
230

    
231
fail:
232
    av_free(s);
233
    return ret;
234
}
235

    
236
static int applehttp_read(URLContext *h, uint8_t *buf, int size)
237
{
238
    AppleHTTPContext *s = h->priv_data;
239
    const char *url;
240
    int ret;
241

    
242
start:
243
    if (s->seg_hd) {
244
        ret = ffurl_read(s->seg_hd, buf, size);
245
        if (ret > 0)
246
            return ret;
247
    }
248
    if (s->seg_hd) {
249
        url_close(s->seg_hd);
250
        s->seg_hd = NULL;
251
        s->cur_seq_no++;
252
    }
253
retry:
254
    if (!s->finished) {
255
        int64_t now = av_gettime();
256
        if (now - s->last_load_time >= s->target_duration*1000000)
257
            if ((ret = parse_playlist(h, s->playlisturl)) < 0)
258
                return ret;
259
    }
260
    if (s->cur_seq_no < s->start_seq_no) {
261
        av_log(NULL, AV_LOG_WARNING,
262
               "skipping %d segments ahead, expired from playlist\n",
263
               s->start_seq_no - s->cur_seq_no);
264
        s->cur_seq_no = s->start_seq_no;
265
    }
266
    if (s->cur_seq_no - s->start_seq_no >= s->n_segments) {
267
        if (s->finished)
268
            return AVERROR_EOF;
269
        while (av_gettime() - s->last_load_time < s->target_duration*1000000) {
270
            if (url_interrupt_cb())
271
                return AVERROR_EXIT;
272
            usleep(100*1000);
273
        }
274
        goto retry;
275
    }
276
    url = s->segments[s->cur_seq_no - s->start_seq_no]->url,
277
    av_log(NULL, AV_LOG_DEBUG, "opening %s\n", url);
278
    ret = ffurl_open(&s->seg_hd, url, URL_RDONLY);
279
    if (ret < 0) {
280
        if (url_interrupt_cb())
281
            return AVERROR_EXIT;
282
        av_log(NULL, AV_LOG_WARNING, "Unable to open %s\n", url);
283
        s->cur_seq_no++;
284
        goto retry;
285
    }
286
    goto start;
287
}
288

    
289
static int applehttp_close(URLContext *h)
290
{
291
    AppleHTTPContext *s = h->priv_data;
292

    
293
    free_segment_list(s);
294
    free_variant_list(s);
295
    url_close(s->seg_hd);
296
    av_free(s);
297
    return 0;
298
}
299

    
300
URLProtocol ff_applehttp_protocol = {
301
    "applehttp",
302
    applehttp_open,
303
    applehttp_read,
304
    NULL, /* write */
305
    NULL, /* seek */
306
    applehttp_close,
307
    .flags = URL_PROTOCOL_FLAG_NESTED_SCHEME,
308
};