LCOV - code coverage report
Current view: top level - libavformat - hlsplaylist.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 27 70 38.6 %
Date: 2017-12-11 04:34:20 Functions: 4 6 66.7 %

          Line data    Source code
       1             : /*
       2             :  * Apple HTTP Live Streaming segmenter
       3             :  * Copyright (c) 2012, Luca Barbato
       4             :  * Copyright (c) 2017 Akamai Technologies, Inc.
       5             :  *
       6             :  * This file is part of FFmpeg.
       7             :  *
       8             :  * FFmpeg is free software; you can redistribute it and/or
       9             :  * modify it under the terms of the GNU Lesser General Public
      10             :  * License as published by the Free Software Foundation; either
      11             :  * version 2.1 of the License, or (at your option) any later version.
      12             :  *
      13             :  * FFmpeg is distributed in the hope that it will be useful,
      14             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16             :  * Lesser General Public License for more details.
      17             :  *
      18             :  * You should have received a copy of the GNU Lesser General Public
      19             :  * License along with FFmpeg; if not, write to the Free Software
      20             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      21             :  */
      22             : 
      23             : #include "config.h"
      24             : #include <stdint.h>
      25             : 
      26             : #include "libavutil/time_internal.h"
      27             : 
      28             : #include "avformat.h"
      29             : #include "hlsplaylist.h"
      30             : 
      31           2 : void ff_hls_write_playlist_version(AVIOContext *out, int version) {
      32           2 :     if (!out)
      33           0 :         return;
      34           2 :     avio_printf(out, "#EXTM3U\n");
      35           2 :     avio_printf(out, "#EXT-X-VERSION:%d\n", version);
      36             : }
      37             : 
      38           0 : void ff_hls_write_stream_info(AVStream *st, AVIOContext *out,
      39             :                               int bandwidth, char *filename) {
      40           0 :     if (!out || !filename)
      41           0 :         return;
      42             : 
      43           0 :     if (!bandwidth) {
      44           0 :         av_log(NULL, AV_LOG_WARNING,
      45             :                 "Bandwidth info not available, set audio and video bitrates\n");
      46           0 :         return;
      47             :     }
      48             : 
      49           0 :     avio_printf(out, "#EXT-X-STREAM-INF:BANDWIDTH=%d", bandwidth);
      50           0 :     if (st && st->codecpar->width > 0 && st->codecpar->height > 0)
      51           0 :         avio_printf(out, ",RESOLUTION=%dx%d", st->codecpar->width,
      52           0 :                 st->codecpar->height);
      53           0 :     avio_printf(out, "\n%s\n\n", filename);
      54             : }
      55             : 
      56           2 : void ff_hls_write_playlist_header(AVIOContext *out, int version, int allowcache,
      57             :                                   int target_duration, int64_t sequence,
      58             :                                   uint32_t playlist_type) {
      59           2 :     if (!out)
      60           0 :         return;
      61           2 :     ff_hls_write_playlist_version(out, version);
      62           2 :     if (allowcache == 0 || allowcache == 1) {
      63           0 :         avio_printf(out, "#EXT-X-ALLOW-CACHE:%s\n", allowcache == 0 ? "NO" : "YES");
      64             :     }
      65           2 :     avio_printf(out, "#EXT-X-TARGETDURATION:%d\n", target_duration);
      66           2 :     avio_printf(out, "#EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
      67           2 :     av_log(NULL, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
      68             : 
      69           2 :     if (playlist_type == PLAYLIST_TYPE_EVENT) {
      70           0 :         avio_printf(out, "#EXT-X-PLAYLIST-TYPE:EVENT\n");
      71           2 :     } else if (playlist_type == PLAYLIST_TYPE_VOD) {
      72           0 :         avio_printf(out, "#EXT-X-PLAYLIST-TYPE:VOD\n");
      73             :     }
      74             : }
      75             : 
      76           0 : void ff_hls_write_init_file(AVIOContext *out, char *filename,
      77             :                             int byterange_mode, int64_t size, int64_t pos) {
      78           0 :     avio_printf(out, "#EXT-X-MAP:URI=\"%s\"", filename);
      79           0 :     if (byterange_mode) {
      80           0 :         avio_printf(out, ",BYTERANGE=\"%"PRId64"@%"PRId64"\"", size, pos);
      81             :     }
      82           0 :     avio_printf(out, "\n");
      83           0 : }
      84             : 
      85           7 : int ff_hls_write_file_entry(AVIOContext *out, int insert_discont,
      86             :                              int byterange_mode,
      87             :                              double duration, int round_duration,
      88             :                              int64_t size, int64_t pos, //Used only if HLS_SINGLE_FILE flag is set
      89             :                              char *baseurl, //Ignored if NULL
      90             :                              char *filename, double *prog_date_time) {
      91           7 :     if (!out || !filename)
      92           0 :         return AVERROR(EINVAL);
      93             : 
      94           7 :     if (insert_discont) {
      95           2 :         avio_printf(out, "#EXT-X-DISCONTINUITY\n");
      96             :     }
      97           7 :     if (round_duration)
      98           0 :         avio_printf(out, "#EXTINF:%ld,\n",  lrint(duration));
      99             :     else
     100           7 :         avio_printf(out, "#EXTINF:%f,\n", duration);
     101           7 :     if (byterange_mode)
     102           0 :         avio_printf(out, "#EXT-X-BYTERANGE:%"PRId64"@%"PRId64"\n", size, pos);
     103             : 
     104           7 :     if (prog_date_time) {
     105             :         time_t tt, wrongsecs;
     106             :         int milli;
     107             :         struct tm *tm, tmpbuf;
     108             :         char buf0[128], buf1[128];
     109           0 :         tt = (int64_t)*prog_date_time;
     110           0 :         milli = av_clip(lrint(1000*(*prog_date_time - tt)), 0, 999);
     111           0 :         tm = localtime_r(&tt, &tmpbuf);
     112           0 :         if (!strftime(buf0, sizeof(buf0), "%Y-%m-%dT%H:%M:%S", tm)) {
     113           0 :             av_log(NULL, AV_LOG_DEBUG, "strftime error in ff_hls_write_file_entry\n");
     114           0 :             return AVERROR_UNKNOWN;
     115             :         }
     116           0 :         if (!strftime(buf1, sizeof(buf1), "%z", tm) || buf1[1]<'0' ||buf1[1]>'2') {
     117           0 :             int tz_min, dst = tm->tm_isdst;
     118           0 :             tm = gmtime_r(&tt, &tmpbuf);
     119           0 :             tm->tm_isdst = dst;
     120           0 :             wrongsecs = mktime(tm);
     121           0 :             tz_min = (FFABS(wrongsecs - tt) + 30) / 60;
     122           0 :             snprintf(buf1, sizeof(buf1),
     123             :                      "%c%02d%02d",
     124           0 :                      wrongsecs <= tt ? '+' : '-',
     125             :                      tz_min / 60,
     126             :                      tz_min % 60);
     127             :         }
     128           0 :         avio_printf(out, "#EXT-X-PROGRAM-DATE-TIME:%s.%03d%s\n", buf0, milli, buf1);
     129           0 :         *prog_date_time += duration;
     130             :     }
     131           7 :     if (baseurl)
     132           0 :         avio_printf(out, "%s", baseurl);
     133           7 :     avio_printf(out, "%s\n", filename);
     134             : 
     135           7 :     return 0;
     136             : }
     137             : 
     138           1 : void ff_hls_write_end_list (AVIOContext *out) {
     139           1 :     if (!out)
     140           0 :         return;
     141           1 :     avio_printf(out, "#EXT-X-ENDLIST\n");
     142             : }
     143             : 

Generated by: LCOV version 1.13