Statistics
| Branch: | Revision:

ffmpeg / libavformat / applehttpproto.c @ 2912e87a

History | View | Annotate | Download (8.94 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 <unistd.h>
33

    
34
/*
35
 * An apple http stream consists of a playlist with media segment files,
36
 * played sequentially. There may be several playlists with the same
37
 * video content, in different bandwidth variants, that are played in
38
 * parallel (preferrably only one bandwidth variant at a time). In this case,
39
 * the user supplied the url to a main playlist that only lists the variant
40
 * playlists.
41
 *
42
 * If the main playlist doesn't point at any variants, we still create
43
 * one anonymous toplevel variant for this, to maintain the structure.
44
 */
45

    
46
struct segment {
47
    int duration;
48
    char url[MAX_URL_SIZE];
49
};
50

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

    
56
typedef struct AppleHTTPContext {
57
    char playlisturl[MAX_URL_SIZE];
58
    int target_duration;
59
    int start_seq_no;
60
    int finished;
61
    int n_segments;
62
    struct segment **segments;
63
    int n_variants;
64
    struct variant **variants;
65
    int cur_seq_no;
66
    URLContext *seg_hd;
67
    int64_t last_load_time;
68
} AppleHTTPContext;
69

    
70
static int read_chomp_line(AVIOContext *s, char *buf, int maxlen)
71
{
72
    int len = ff_get_line(s, buf, maxlen);
73
    while (len > 0 && isspace(buf[len - 1]))
74
        buf[--len] = '\0';
75
    return len;
76
}
77

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

    
87
static void free_variant_list(AppleHTTPContext *s)
88
{
89
    int i;
90
    for (i = 0; i < s->n_variants; i++)
91
        av_free(s->variants[i]);
92
    av_freep(&s->variants);
93
    s->n_variants = 0;
94
}
95

    
96
struct variant_info {
97
    char bandwidth[20];
98
};
99

    
100
static void handle_variant_args(struct variant_info *info, const char *key,
101
                                int key_len, char **dest, int *dest_len)
102
{
103
    if (!strncmp(key, "BANDWIDTH=", key_len)) {
104
        *dest     =        info->bandwidth;
105
        *dest_len = sizeof(info->bandwidth);
106
    }
107
}
108

    
109
static int parse_playlist(URLContext *h, const char *url)
110
{
111
    AppleHTTPContext *s = h->priv_data;
112
    AVIOContext *in;
113
    int ret = 0, duration = 0, is_segment = 0, is_variant = 0, bandwidth = 0;
114
    char line[1024];
115
    const char *ptr;
116

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

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

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

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

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

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

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

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

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

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

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

    
228
    return 0;
229

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

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

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

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

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

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