• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/applehttpproto.c

Go to the documentation of this file.
00001 /*
00002  * Apple HTTP Live Streaming Protocol Handler
00003  * Copyright (c) 2010 Martin Storsjo
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00028 #include "libavutil/avstring.h"
00029 #include "avformat.h"
00030 #include "internal.h"
00031 #include "url.h"
00032 #include <unistd.h>
00033 
00034 /*
00035  * An apple http stream consists of a playlist with media segment files,
00036  * played sequentially. There may be several playlists with the same
00037  * video content, in different bandwidth variants, that are played in
00038  * parallel (preferrably only one bandwidth variant at a time). In this case,
00039  * the user supplied the url to a main playlist that only lists the variant
00040  * playlists.
00041  *
00042  * If the main playlist doesn't point at any variants, we still create
00043  * one anonymous toplevel variant for this, to maintain the structure.
00044  */
00045 
00046 struct segment {
00047     int duration;
00048     char url[MAX_URL_SIZE];
00049 };
00050 
00051 struct variant {
00052     int bandwidth;
00053     char url[MAX_URL_SIZE];
00054 };
00055 
00056 typedef struct AppleHTTPContext {
00057     char playlisturl[MAX_URL_SIZE];
00058     int target_duration;
00059     int start_seq_no;
00060     int finished;
00061     int n_segments;
00062     struct segment **segments;
00063     int n_variants;
00064     struct variant **variants;
00065     int cur_seq_no;
00066     URLContext *seg_hd;
00067     int64_t last_load_time;
00068 } AppleHTTPContext;
00069 
00070 static int read_chomp_line(AVIOContext *s, char *buf, int maxlen)
00071 {
00072     int len = ff_get_line(s, buf, maxlen);
00073     while (len > 0 && isspace(buf[len - 1]))
00074         buf[--len] = '\0';
00075     return len;
00076 }
00077 
00078 static void free_segment_list(AppleHTTPContext *s)
00079 {
00080     int i;
00081     for (i = 0; i < s->n_segments; i++)
00082         av_free(s->segments[i]);
00083     av_freep(&s->segments);
00084     s->n_segments = 0;
00085 }
00086 
00087 static void free_variant_list(AppleHTTPContext *s)
00088 {
00089     int i;
00090     for (i = 0; i < s->n_variants; i++)
00091         av_free(s->variants[i]);
00092     av_freep(&s->variants);
00093     s->n_variants = 0;
00094 }
00095 
00096 struct variant_info {
00097     char bandwidth[20];
00098 };
00099 
00100 static void handle_variant_args(struct variant_info *info, const char *key,
00101                                 int key_len, char **dest, int *dest_len)
00102 {
00103     if (!strncmp(key, "BANDWIDTH=", key_len)) {
00104         *dest     =        info->bandwidth;
00105         *dest_len = sizeof(info->bandwidth);
00106     }
00107 }
00108 
00109 static int parse_playlist(URLContext *h, const char *url)
00110 {
00111     AppleHTTPContext *s = h->priv_data;
00112     AVIOContext *in;
00113     int ret = 0, duration = 0, is_segment = 0, is_variant = 0, bandwidth = 0;
00114     char line[1024];
00115     const char *ptr;
00116 
00117     if ((ret = avio_open2(&in, url, AVIO_FLAG_READ,
00118                           &h->interrupt_callback, NULL)) < 0)
00119         return ret;
00120 
00121     read_chomp_line(in, line, sizeof(line));
00122     if (strcmp(line, "#EXTM3U"))
00123         return AVERROR_INVALIDDATA;
00124 
00125     free_segment_list(s);
00126     s->finished = 0;
00127     while (!in->eof_reached) {
00128         read_chomp_line(in, line, sizeof(line));
00129         if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
00130             struct variant_info info = {{0}};
00131             is_variant = 1;
00132             ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args,
00133                                &info);
00134             bandwidth = atoi(info.bandwidth);
00135         } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
00136             s->target_duration = atoi(ptr);
00137         } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
00138             s->start_seq_no = atoi(ptr);
00139         } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
00140             s->finished = 1;
00141         } else if (av_strstart(line, "#EXTINF:", &ptr)) {
00142             is_segment = 1;
00143             duration = atoi(ptr);
00144         } else if (av_strstart(line, "#", NULL)) {
00145             continue;
00146         } else if (line[0]) {
00147             if (is_segment) {
00148                 struct segment *seg = av_malloc(sizeof(struct segment));
00149                 if (!seg) {
00150                     ret = AVERROR(ENOMEM);
00151                     goto fail;
00152                 }
00153                 seg->duration = duration;
00154                 ff_make_absolute_url(seg->url, sizeof(seg->url), url, line);
00155                 dynarray_add(&s->segments, &s->n_segments, seg);
00156                 is_segment = 0;
00157             } else if (is_variant) {
00158                 struct variant *var = av_malloc(sizeof(struct variant));
00159                 if (!var) {
00160                     ret = AVERROR(ENOMEM);
00161                     goto fail;
00162                 }
00163                 var->bandwidth = bandwidth;
00164                 ff_make_absolute_url(var->url, sizeof(var->url), url, line);
00165                 dynarray_add(&s->variants, &s->n_variants, var);
00166                 is_variant = 0;
00167             }
00168         }
00169     }
00170     s->last_load_time = av_gettime();
00171 
00172 fail:
00173     avio_close(in);
00174     return ret;
00175 }
00176 
00177 static int applehttp_close(URLContext *h)
00178 {
00179     AppleHTTPContext *s = h->priv_data;
00180 
00181     free_segment_list(s);
00182     free_variant_list(s);
00183     ffurl_close(s->seg_hd);
00184     return 0;
00185 }
00186 
00187 static int applehttp_open(URLContext *h, const char *uri, int flags)
00188 {
00189     AppleHTTPContext *s = h->priv_data;
00190     int ret, i;
00191     const char *nested_url;
00192 
00193     if (flags & AVIO_FLAG_WRITE)
00194         return AVERROR(ENOSYS);
00195 
00196     h->is_streamed = 1;
00197 
00198     if (av_strstart(uri, "applehttp+", &nested_url)) {
00199         av_strlcpy(s->playlisturl, nested_url, sizeof(s->playlisturl));
00200     } else if (av_strstart(uri, "applehttp://", &nested_url)) {
00201         av_strlcpy(s->playlisturl, "http://", sizeof(s->playlisturl));
00202         av_strlcat(s->playlisturl, nested_url, sizeof(s->playlisturl));
00203     } else {
00204         av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri);
00205         ret = AVERROR(EINVAL);
00206         goto fail;
00207     }
00208 
00209     if ((ret = parse_playlist(h, s->playlisturl)) < 0)
00210         goto fail;
00211 
00212     if (s->n_segments == 0 && s->n_variants > 0) {
00213         int max_bandwidth = 0, maxvar = -1;
00214         for (i = 0; i < s->n_variants; i++) {
00215             if (s->variants[i]->bandwidth > max_bandwidth || i == 0) {
00216                 max_bandwidth = s->variants[i]->bandwidth;
00217                 maxvar = i;
00218             }
00219         }
00220         av_strlcpy(s->playlisturl, s->variants[maxvar]->url,
00221                    sizeof(s->playlisturl));
00222         if ((ret = parse_playlist(h, s->playlisturl)) < 0)
00223             goto fail;
00224     }
00225 
00226     if (s->n_segments == 0) {
00227         av_log(h, AV_LOG_WARNING, "Empty playlist\n");
00228         ret = AVERROR(EIO);
00229         goto fail;
00230     }
00231     s->cur_seq_no = s->start_seq_no;
00232     if (!s->finished && s->n_segments >= 3)
00233         s->cur_seq_no = s->start_seq_no + s->n_segments - 3;
00234 
00235     return 0;
00236 
00237 fail:
00238     applehttp_close(h);
00239     return ret;
00240 }
00241 
00242 static int applehttp_read(URLContext *h, uint8_t *buf, int size)
00243 {
00244     AppleHTTPContext *s = h->priv_data;
00245     const char *url;
00246     int ret;
00247     int64_t reload_interval;
00248 
00249 start:
00250     if (s->seg_hd) {
00251         ret = ffurl_read(s->seg_hd, buf, size);
00252         if (ret > 0)
00253             return ret;
00254     }
00255     if (s->seg_hd) {
00256         ffurl_close(s->seg_hd);
00257         s->seg_hd = NULL;
00258         s->cur_seq_no++;
00259     }
00260     reload_interval = s->n_segments > 0 ?
00261                       s->segments[s->n_segments - 1]->duration :
00262                       s->target_duration;
00263     reload_interval *= 1000000;
00264 retry:
00265     if (!s->finished) {
00266         int64_t now = av_gettime();
00267         if (now - s->last_load_time >= reload_interval) {
00268             if ((ret = parse_playlist(h, s->playlisturl)) < 0)
00269                 return ret;
00270             /* If we need to reload the playlist again below (if
00271              * there's still no more segments), switch to a reload
00272              * interval of half the target duration. */
00273             reload_interval = s->target_duration * 500000;
00274         }
00275     }
00276     if (s->cur_seq_no < s->start_seq_no) {
00277         av_log(h, AV_LOG_WARNING,
00278                "skipping %d segments ahead, expired from playlist\n",
00279                s->start_seq_no - s->cur_seq_no);
00280         s->cur_seq_no = s->start_seq_no;
00281     }
00282     if (s->cur_seq_no - s->start_seq_no >= s->n_segments) {
00283         if (s->finished)
00284             return AVERROR_EOF;
00285         while (av_gettime() - s->last_load_time < reload_interval) {
00286             if (ff_check_interrupt(&h->interrupt_callback))
00287                 return AVERROR_EXIT;
00288             usleep(100*1000);
00289         }
00290         goto retry;
00291     }
00292     url = s->segments[s->cur_seq_no - s->start_seq_no]->url,
00293     av_log(h, AV_LOG_DEBUG, "opening %s\n", url);
00294     ret = ffurl_open(&s->seg_hd, url, AVIO_FLAG_READ,
00295                      &h->interrupt_callback, NULL);
00296     if (ret < 0) {
00297         if (ff_check_interrupt(&h->interrupt_callback))
00298             return AVERROR_EXIT;
00299         av_log(h, AV_LOG_WARNING, "Unable to open %s\n", url);
00300         s->cur_seq_no++;
00301         goto retry;
00302     }
00303     goto start;
00304 }
00305 
00306 URLProtocol ff_applehttp_protocol = {
00307     .name           = "applehttp",
00308     .url_open       = applehttp_open,
00309     .url_read       = applehttp_read,
00310     .url_close      = applehttp_close,
00311     .flags          = URL_PROTOCOL_FLAG_NESTED_SCHEME,
00312     .priv_data_size = sizeof(AppleHTTPContext),
00313 };
Generated on Sat Mar 17 2012 12:57:53 for Libav by doxygen 1.7.1