LCOV - code coverage report
Current view: top level - libavformat - hlsproto.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 158 0.0 %
Date: 2018-05-20 11:54:08 Functions: 0 7 0.0 %

          Line data    Source code
       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;
      50             :     char url[MAX_URL_SIZE];
      51             : };
      52             : 
      53             : struct variant {
      54             :     int bandwidth;
      55             :     char url[MAX_URL_SIZE];
      56             : };
      57             : 
      58             : typedef struct HLSContext {
      59             :     char playlisturl[MAX_URL_SIZE];
      60             :     int64_t target_duration;
      61             :     int start_seq_no;
      62             :     int finished;
      63             :     int n_segments;
      64             :     struct segment **segments;
      65             :     int n_variants;
      66             :     struct variant **variants;
      67             :     int cur_seq_no;
      68             :     URLContext *seg_hd;
      69             :     int64_t last_load_time;
      70             : } HLSContext;
      71             : 
      72           0 : static void free_segment_list(HLSContext *s)
      73             : {
      74             :     int i;
      75           0 :     for (i = 0; i < s->n_segments; i++)
      76           0 :         av_freep(&s->segments[i]);
      77           0 :     av_freep(&s->segments);
      78           0 :     s->n_segments = 0;
      79           0 : }
      80             : 
      81           0 : static void free_variant_list(HLSContext *s)
      82             : {
      83             :     int i;
      84           0 :     for (i = 0; i < s->n_variants; i++)
      85           0 :         av_freep(&s->variants[i]);
      86           0 :     av_freep(&s->variants);
      87           0 :     s->n_variants = 0;
      88           0 : }
      89             : 
      90             : struct variant_info {
      91             :     char bandwidth[20];
      92             : };
      93             : 
      94           0 : static void handle_variant_args(struct variant_info *info, const char *key,
      95             :                                 int key_len, char **dest, int *dest_len)
      96             : {
      97           0 :     if (!strncmp(key, "BANDWIDTH=", key_len)) {
      98           0 :         *dest     =        info->bandwidth;
      99           0 :         *dest_len = sizeof(info->bandwidth);
     100             :     }
     101           0 : }
     102             : 
     103           0 : static int parse_playlist(URLContext *h, const char *url)
     104             : {
     105           0 :     HLSContext *s = h->priv_data;
     106             :     AVIOContext *in;
     107           0 :     int ret = 0, is_segment = 0, is_variant = 0, bandwidth = 0;
     108           0 :     int64_t duration = 0;
     109             :     char line[1024];
     110             :     const char *ptr;
     111             : 
     112           0 :     if ((ret = ffio_open_whitelist(&in, url, AVIO_FLAG_READ,
     113           0 :                                    &h->interrupt_callback, NULL,
     114             :                                    h->protocol_whitelist, h->protocol_blacklist)) < 0)
     115           0 :         return ret;
     116             : 
     117           0 :     ff_get_chomp_line(in, line, sizeof(line));
     118           0 :     if (strcmp(line, "#EXTM3U")) {
     119           0 :         ret = AVERROR_INVALIDDATA;
     120           0 :         goto fail;
     121             :     }
     122             : 
     123           0 :     free_segment_list(s);
     124           0 :     s->finished = 0;
     125           0 :     while (!avio_feof(in)) {
     126           0 :         ff_get_chomp_line(in, line, sizeof(line));
     127           0 :         if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
     128           0 :             struct variant_info info = {{0}};
     129           0 :             is_variant = 1;
     130           0 :             ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args,
     131             :                                &info);
     132           0 :             bandwidth = atoi(info.bandwidth);
     133           0 :         } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
     134           0 :             s->target_duration = atoi(ptr) * AV_TIME_BASE;
     135           0 :         } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
     136           0 :             s->start_seq_no = atoi(ptr);
     137           0 :         } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
     138           0 :             s->finished = 1;
     139           0 :         } else if (av_strstart(line, "#EXTINF:", &ptr)) {
     140           0 :             is_segment = 1;
     141           0 :             duration = atof(ptr) * AV_TIME_BASE;
     142           0 :         } else if (av_strstart(line, "#", NULL)) {
     143           0 :             continue;
     144           0 :         } else if (line[0]) {
     145           0 :             if (is_segment) {
     146           0 :                 struct segment *seg = av_malloc(sizeof(struct segment));
     147           0 :                 if (!seg) {
     148           0 :                     ret = AVERROR(ENOMEM);
     149           0 :                     goto fail;
     150             :                 }
     151           0 :                 seg->duration = duration;
     152           0 :                 ff_make_absolute_url(seg->url, sizeof(seg->url), url, line);
     153           0 :                 dynarray_add(&s->segments, &s->n_segments, seg);
     154           0 :                 is_segment = 0;
     155           0 :             } else if (is_variant) {
     156           0 :                 struct variant *var = av_malloc(sizeof(struct variant));
     157           0 :                 if (!var) {
     158           0 :                     ret = AVERROR(ENOMEM);
     159           0 :                     goto fail;
     160             :                 }
     161           0 :                 var->bandwidth = bandwidth;
     162           0 :                 ff_make_absolute_url(var->url, sizeof(var->url), url, line);
     163           0 :                 dynarray_add(&s->variants, &s->n_variants, var);
     164           0 :                 is_variant = 0;
     165             :             }
     166             :         }
     167             :     }
     168           0 :     s->last_load_time = av_gettime_relative();
     169             : 
     170           0 : fail:
     171           0 :     avio_close(in);
     172           0 :     return ret;
     173             : }
     174             : 
     175           0 : static int hls_close(URLContext *h)
     176             : {
     177           0 :     HLSContext *s = h->priv_data;
     178             : 
     179           0 :     free_segment_list(s);
     180           0 :     free_variant_list(s);
     181           0 :     ffurl_close(s->seg_hd);
     182           0 :     return 0;
     183             : }
     184             : 
     185           0 : static int hls_open(URLContext *h, const char *uri, int flags)
     186             : {
     187           0 :     HLSContext *s = h->priv_data;
     188             :     int ret, i;
     189             :     const char *nested_url;
     190             : 
     191           0 :     if (flags & AVIO_FLAG_WRITE)
     192           0 :         return AVERROR(ENOSYS);
     193             : 
     194           0 :     h->is_streamed = 1;
     195             : 
     196           0 :     if (av_strstart(uri, "hls+", &nested_url)) {
     197           0 :         av_strlcpy(s->playlisturl, nested_url, sizeof(s->playlisturl));
     198           0 :     } else if (av_strstart(uri, "hls://", &nested_url)) {
     199           0 :         av_log(h, AV_LOG_ERROR,
     200             :                "No nested protocol specified. Specify e.g. hls+http://%s\n",
     201             :                nested_url);
     202           0 :         ret = AVERROR(EINVAL);
     203           0 :         goto fail;
     204             :     } else {
     205           0 :         av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri);
     206           0 :         ret = AVERROR(EINVAL);
     207           0 :         goto fail;
     208             :     }
     209           0 :     av_log(h, AV_LOG_WARNING,
     210             :            "Using the hls protocol is discouraged, please try using the "
     211             :            "hls demuxer instead. The hls demuxer should be more complete "
     212             :            "and work as well as the protocol implementation. (If not, "
     213             :            "please report it.) To use the demuxer, simply use %s as url.\n",
     214           0 :            s->playlisturl);
     215             : 
     216           0 :     if ((ret = parse_playlist(h, s->playlisturl)) < 0)
     217           0 :         goto fail;
     218             : 
     219           0 :     if (s->n_segments == 0 && s->n_variants > 0) {
     220           0 :         int max_bandwidth = 0, maxvar = -1;
     221           0 :         for (i = 0; i < s->n_variants; i++) {
     222           0 :             if (s->variants[i]->bandwidth > max_bandwidth || i == 0) {
     223           0 :                 max_bandwidth = s->variants[i]->bandwidth;
     224           0 :                 maxvar = i;
     225             :             }
     226             :         }
     227           0 :         av_strlcpy(s->playlisturl, s->variants[maxvar]->url,
     228             :                    sizeof(s->playlisturl));
     229           0 :         if ((ret = parse_playlist(h, s->playlisturl)) < 0)
     230           0 :             goto fail;
     231             :     }
     232             : 
     233           0 :     if (s->n_segments == 0) {
     234           0 :         av_log(h, AV_LOG_WARNING, "Empty playlist\n");
     235           0 :         ret = AVERROR(EIO);
     236           0 :         goto fail;
     237             :     }
     238           0 :     s->cur_seq_no = s->start_seq_no;
     239           0 :     if (!s->finished && s->n_segments >= 3)
     240           0 :         s->cur_seq_no = s->start_seq_no + s->n_segments - 3;
     241             : 
     242           0 :     return 0;
     243             : 
     244           0 : fail:
     245           0 :     hls_close(h);
     246           0 :     return ret;
     247             : }
     248             : 
     249           0 : static int hls_read(URLContext *h, uint8_t *buf, int size)
     250             : {
     251           0 :     HLSContext *s = h->priv_data;
     252             :     const char *url;
     253             :     int ret;
     254             :     int64_t reload_interval;
     255             : 
     256           0 : start:
     257           0 :     if (s->seg_hd) {
     258           0 :         ret = ffurl_read(s->seg_hd, buf, size);
     259           0 :         if (ret > 0)
     260           0 :             return ret;
     261             :     }
     262           0 :     if (s->seg_hd) {
     263           0 :         ffurl_close(s->seg_hd);
     264           0 :         s->seg_hd = NULL;
     265           0 :         s->cur_seq_no++;
     266             :     }
     267           0 :     reload_interval = s->n_segments > 0 ?
     268           0 :                       s->segments[s->n_segments - 1]->duration :
     269             :                       s->target_duration;
     270           0 : retry:
     271           0 :     if (!s->finished) {
     272           0 :         int64_t now = av_gettime_relative();
     273           0 :         if (now - s->last_load_time >= reload_interval) {
     274           0 :             if ((ret = parse_playlist(h, s->playlisturl)) < 0)
     275           0 :                 return ret;
     276             :             /* If we need to reload the playlist again below (if
     277             :              * there's still no more segments), switch to a reload
     278             :              * interval of half the target duration. */
     279           0 :             reload_interval = s->target_duration / 2;
     280             :         }
     281             :     }
     282           0 :     if (s->cur_seq_no < s->start_seq_no) {
     283           0 :         av_log(h, AV_LOG_WARNING,
     284             :                "skipping %d segments ahead, expired from playlist\n",
     285           0 :                s->start_seq_no - s->cur_seq_no);
     286           0 :         s->cur_seq_no = s->start_seq_no;
     287             :     }
     288           0 :     if (s->cur_seq_no - s->start_seq_no >= s->n_segments) {
     289           0 :         if (s->finished)
     290           0 :             return AVERROR_EOF;
     291           0 :         while (av_gettime_relative() - s->last_load_time < reload_interval) {
     292           0 :             if (ff_check_interrupt(&h->interrupt_callback))
     293           0 :                 return AVERROR_EXIT;
     294           0 :             av_usleep(100*1000);
     295             :         }
     296           0 :         goto retry;
     297             :     }
     298           0 :     url = s->segments[s->cur_seq_no - s->start_seq_no]->url,
     299           0 :     av_log(h, AV_LOG_DEBUG, "opening %s\n", url);
     300           0 :     ret = ffurl_open_whitelist(&s->seg_hd, url, AVIO_FLAG_READ,
     301           0 :                                &h->interrupt_callback, NULL,
     302             :                                h->protocol_whitelist, h->protocol_blacklist, h);
     303           0 :     if (ret < 0) {
     304           0 :         if (ff_check_interrupt(&h->interrupt_callback))
     305           0 :             return AVERROR_EXIT;
     306           0 :         av_log(h, AV_LOG_WARNING, "Unable to open %s\n", url);
     307           0 :         s->cur_seq_no++;
     308           0 :         goto retry;
     309             :     }
     310           0 :     goto start;
     311             : }
     312             : 
     313             : const URLProtocol ff_hls_protocol = {
     314             :     .name           = "hls",
     315             :     .url_open       = hls_open,
     316             :     .url_read       = hls_read,
     317             :     .url_close      = hls_close,
     318             :     .flags          = URL_PROTOCOL_FLAG_NESTED_SCHEME,
     319             :     .priv_data_size = sizeof(HLSContext),
     320             : };

Generated by: LCOV version 1.13