LCOV - code coverage report
Current view: top level - libavformat - webm_chunk.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 125 0.0 %
Date: 2017-12-15 02:19:58 Functions: 0 7 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2015, Vignesh Venkatasubramanian
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU Lesser General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2.1 of the License, or (at your option) any later version.
      10             :  *
      11             :  * FFmpeg is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with FFmpeg; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : /**
      22             :  * @file WebM Chunk Muxer
      23             :  * The chunk muxer enables writing WebM Live chunks where there is a header
      24             :  * chunk, followed by data chunks where each Cluster is written out as a Chunk.
      25             :  */
      26             : 
      27             : #include <float.h>
      28             : #include <time.h>
      29             : 
      30             : #include "avformat.h"
      31             : #include "avio.h"
      32             : #include "avio_internal.h"
      33             : #include "internal.h"
      34             : 
      35             : #include "libavutil/avassert.h"
      36             : #include "libavutil/log.h"
      37             : #include "libavutil/opt.h"
      38             : #include "libavutil/avstring.h"
      39             : #include "libavutil/parseutils.h"
      40             : #include "libavutil/mathematics.h"
      41             : #include "libavutil/time.h"
      42             : #include "libavutil/time_internal.h"
      43             : #include "libavutil/timestamp.h"
      44             : 
      45             : #define MAX_FILENAME_SIZE 1024
      46             : 
      47             : typedef struct WebMChunkContext {
      48             :     const AVClass *class;
      49             :     int chunk_start_index;
      50             :     char *header_filename;
      51             :     int chunk_duration;
      52             :     int chunk_index;
      53             :     char *http_method;
      54             :     uint64_t duration_written;
      55             :     int prev_pts;
      56             :     AVOutputFormat *oformat;
      57             :     AVFormatContext *avf;
      58             : } WebMChunkContext;
      59             : 
      60           0 : static int chunk_mux_init(AVFormatContext *s)
      61             : {
      62           0 :     WebMChunkContext *wc = s->priv_data;
      63             :     AVFormatContext *oc;
      64             :     int ret;
      65             : 
      66           0 :     ret = avformat_alloc_output_context2(&wc->avf, wc->oformat, NULL, NULL);
      67           0 :     if (ret < 0)
      68           0 :         return ret;
      69           0 :     oc = wc->avf;
      70             : 
      71           0 :     oc->interrupt_callback = s->interrupt_callback;
      72           0 :     oc->max_delay          = s->max_delay;
      73           0 :     av_dict_copy(&oc->metadata, s->metadata, 0);
      74             : 
      75           0 :     *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
      76           0 :     av_opt_set_defaults(oc->priv_data);
      77           0 :     av_opt_set_int(oc->priv_data, "dash", 1, 0);
      78           0 :     av_opt_set_int(oc->priv_data, "cluster_time_limit", wc->chunk_duration, 0);
      79           0 :     av_opt_set_int(oc->priv_data, "live", 1, 0);
      80             : 
      81           0 :     oc->streams = s->streams;
      82           0 :     oc->nb_streams = s->nb_streams;
      83             : 
      84           0 :     return 0;
      85             : }
      86             : 
      87           0 : static int get_chunk_filename(AVFormatContext *s, int is_header, char *filename)
      88             : {
      89           0 :     WebMChunkContext *wc = s->priv_data;
      90           0 :     AVFormatContext *oc = wc->avf;
      91           0 :     if (!filename) {
      92           0 :         return AVERROR(EINVAL);
      93             :     }
      94           0 :     if (is_header) {
      95           0 :         if (!wc->header_filename) {
      96           0 :             av_log(oc, AV_LOG_ERROR, "No header filename provided\n");
      97           0 :             return AVERROR(EINVAL);
      98             :         }
      99           0 :         av_strlcpy(filename, wc->header_filename, strlen(wc->header_filename) + 1);
     100             :     } else {
     101           0 :         if (av_get_frame_filename(filename, MAX_FILENAME_SIZE,
     102           0 :                                   s->filename, wc->chunk_index - 1) < 0) {
     103           0 :             av_log(oc, AV_LOG_ERROR, "Invalid chunk filename template '%s'\n", s->filename);
     104           0 :             return AVERROR(EINVAL);
     105             :         }
     106             :     }
     107           0 :     return 0;
     108             : }
     109             : 
     110           0 : static int webm_chunk_write_header(AVFormatContext *s)
     111             : {
     112           0 :     WebMChunkContext *wc = s->priv_data;
     113           0 :     AVFormatContext *oc = NULL;
     114             :     int ret;
     115             :     int i;
     116           0 :     AVDictionary *options = NULL;
     117             : 
     118             :     // DASH Streams can only have either one track per file.
     119           0 :     if (s->nb_streams != 1) { return AVERROR_INVALIDDATA; }
     120             : 
     121           0 :     wc->chunk_index = wc->chunk_start_index;
     122           0 :     wc->oformat = av_guess_format("webm", s->filename, "video/webm");
     123           0 :     if (!wc->oformat)
     124           0 :         return AVERROR_MUXER_NOT_FOUND;
     125             : 
     126           0 :     ret = chunk_mux_init(s);
     127           0 :     if (ret < 0)
     128           0 :         return ret;
     129           0 :     oc = wc->avf;
     130           0 :     ret = get_chunk_filename(s, 1, oc->filename);
     131           0 :     if (ret < 0)
     132           0 :         return ret;
     133           0 :     if (wc->http_method)
     134           0 :         av_dict_set(&options, "method", wc->http_method, 0);
     135           0 :     ret = s->io_open(s, &oc->pb, oc->filename, AVIO_FLAG_WRITE, &options);
     136           0 :     av_dict_free(&options);
     137           0 :     if (ret < 0)
     138           0 :         return ret;
     139             : 
     140           0 :     oc->pb->seekable = 0;
     141           0 :     ret = oc->oformat->write_header(oc);
     142           0 :     if (ret < 0)
     143           0 :         return ret;
     144           0 :     ff_format_io_close(s, &oc->pb);
     145           0 :     for (i = 0; i < s->nb_streams; i++) {
     146             :         // ms precision is the de-facto standard timescale for mkv files.
     147           0 :         avpriv_set_pts_info(s->streams[i], 64, 1, 1000);
     148             :     }
     149           0 :     return 0;
     150             : }
     151             : 
     152           0 : static int chunk_start(AVFormatContext *s)
     153             : {
     154           0 :     WebMChunkContext *wc = s->priv_data;
     155           0 :     AVFormatContext *oc = wc->avf;
     156             :     int ret;
     157             : 
     158           0 :     ret = avio_open_dyn_buf(&oc->pb);
     159           0 :     if (ret < 0)
     160           0 :         return ret;
     161           0 :     wc->chunk_index++;
     162           0 :     return 0;
     163             : }
     164             : 
     165           0 : static int chunk_end(AVFormatContext *s)
     166             : {
     167           0 :     WebMChunkContext *wc = s->priv_data;
     168           0 :     AVFormatContext *oc = wc->avf;
     169             :     int ret;
     170             :     int buffer_size;
     171             :     uint8_t *buffer;
     172             :     AVIOContext *pb;
     173             :     char filename[MAX_FILENAME_SIZE];
     174           0 :     AVDictionary *options = NULL;
     175             : 
     176           0 :     if (wc->chunk_start_index == wc->chunk_index)
     177           0 :         return 0;
     178             :     // Flush the cluster in WebM muxer.
     179           0 :     oc->oformat->write_packet(oc, NULL);
     180           0 :     buffer_size = avio_close_dyn_buf(oc->pb, &buffer);
     181           0 :     ret = get_chunk_filename(s, 0, filename);
     182           0 :     if (ret < 0)
     183           0 :         goto fail;
     184           0 :     if (wc->http_method)
     185           0 :         av_dict_set(&options, "method", wc->http_method, 0);
     186           0 :     ret = s->io_open(s, &pb, filename, AVIO_FLAG_WRITE, &options);
     187           0 :     if (ret < 0)
     188           0 :         goto fail;
     189           0 :     avio_write(pb, buffer, buffer_size);
     190           0 :     ff_format_io_close(s, &pb);
     191           0 :     oc->pb = NULL;
     192           0 : fail:
     193           0 :     av_dict_free(&options);
     194           0 :     av_free(buffer);
     195           0 :     return (ret < 0) ? ret : 0;
     196             : }
     197             : 
     198           0 : static int webm_chunk_write_packet(AVFormatContext *s, AVPacket *pkt)
     199             : {
     200           0 :     WebMChunkContext *wc = s->priv_data;
     201           0 :     AVFormatContext *oc = wc->avf;
     202           0 :     AVStream *st = s->streams[pkt->stream_index];
     203             :     int ret;
     204             : 
     205           0 :     if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
     206           0 :         wc->duration_written += av_rescale_q(pkt->pts - wc->prev_pts,
     207             :                                              st->time_base,
     208           0 :                                              (AVRational) {1, 1000});
     209           0 :         wc->prev_pts = pkt->pts;
     210             :     }
     211             : 
     212             :     // For video, a new chunk is started only on key frames. For audio, a new
     213             :     // chunk is started based on chunk_duration.
     214           0 :     if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
     215           0 :          (pkt->flags & AV_PKT_FLAG_KEY)) ||
     216           0 :         (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
     217           0 :          (pkt->pts == 0 || wc->duration_written >= wc->chunk_duration))) {
     218           0 :         wc->duration_written = 0;
     219           0 :         if ((ret = chunk_end(s)) < 0 || (ret = chunk_start(s)) < 0) {
     220             :             goto fail;
     221             :         }
     222             :     }
     223             : 
     224           0 :     ret = oc->oformat->write_packet(oc, pkt);
     225           0 :     if (ret < 0)
     226           0 :         goto fail;
     227             : 
     228           0 : fail:
     229           0 :     if (ret < 0) {
     230           0 :         oc->streams = NULL;
     231           0 :         oc->nb_streams = 0;
     232           0 :         avformat_free_context(oc);
     233             :     }
     234             : 
     235           0 :     return ret;
     236             : }
     237             : 
     238           0 : static int webm_chunk_write_trailer(AVFormatContext *s)
     239             : {
     240           0 :     WebMChunkContext *wc = s->priv_data;
     241           0 :     AVFormatContext *oc = wc->avf;
     242           0 :     oc->oformat->write_trailer(oc);
     243           0 :     chunk_end(s);
     244           0 :     oc->streams = NULL;
     245           0 :     oc->nb_streams = 0;
     246           0 :     avformat_free_context(oc);
     247           0 :     return 0;
     248             : }
     249             : 
     250             : #define OFFSET(x) offsetof(WebMChunkContext, x)
     251             : static const AVOption options[] = {
     252             :     { "chunk_start_index",  "start index of the chunk", OFFSET(chunk_start_index), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
     253             :     { "header", "filename of the header where the initialization data will be written", OFFSET(header_filename), AV_OPT_TYPE_STRING, { 0 }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
     254             :     { "audio_chunk_duration", "duration of each chunk in milliseconds", OFFSET(chunk_duration), AV_OPT_TYPE_INT, {.i64 = 5000}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
     255             :     { "method", "set the HTTP method", OFFSET(http_method), AV_OPT_TYPE_STRING, {.str = NULL},  0, 0, AV_OPT_FLAG_ENCODING_PARAM },
     256             :     { NULL },
     257             : };
     258             : 
     259             : #if CONFIG_WEBM_CHUNK_MUXER
     260             : static const AVClass webm_chunk_class = {
     261             :     .class_name = "WebM Chunk Muxer",
     262             :     .item_name  = av_default_item_name,
     263             :     .option     = options,
     264             :     .version    = LIBAVUTIL_VERSION_INT,
     265             : };
     266             : 
     267             : AVOutputFormat ff_webm_chunk_muxer = {
     268             :     .name           = "webm_chunk",
     269             :     .long_name      = NULL_IF_CONFIG_SMALL("WebM Chunk Muxer"),
     270             :     .mime_type      = "video/webm",
     271             :     .extensions     = "chk",
     272             :     .flags          = AVFMT_NOFILE | AVFMT_GLOBALHEADER | AVFMT_NEEDNUMBER |
     273             :                       AVFMT_TS_NONSTRICT,
     274             :     .priv_data_size = sizeof(WebMChunkContext),
     275             :     .write_header   = webm_chunk_write_header,
     276             :     .write_packet   = webm_chunk_write_packet,
     277             :     .write_trailer  = webm_chunk_write_trailer,
     278             :     .priv_class     = &webm_chunk_class,
     279             : };
     280             : #endif

Generated by: LCOV version 1.13