]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/webm_chunk.c
avformat/rtp_mpegts: typedef MuxChain struct
[ffmpeg] / libavformat / webm_chunk.c
index af0d6b9d760ea676b5e866d74ca2a3c0480752d7..f1bee5fa9ff424b80a53d8415195b714715428b2 100644 (file)
  * chunk, followed by data chunks where each Cluster is written out as a Chunk.
  */
 
-#include <float.h>
-#include <time.h>
-
 #include "avformat.h"
 #include "avio.h"
 #include "avio_internal.h"
 #include "internal.h"
 
-#include "libavutil/avassert.h"
 #include "libavutil/log.h"
 #include "libavutil/opt.h"
-#include "libavutil/avstring.h"
-#include "libavutil/parseutils.h"
 #include "libavutil/mathematics.h"
-#include "libavutil/time.h"
-#include "libavutil/time_internal.h"
-#include "libavutil/timestamp.h"
 
 #define MAX_FILENAME_SIZE 1024
 
@@ -53,9 +44,10 @@ typedef struct WebMChunkContext {
     uint64_t duration_written;
     int64_t prev_pts;
     AVFormatContext *avf;
+    int header_written;
 } WebMChunkContext;
 
-static int chunk_mux_init(AVFormatContext *s)
+static int webm_chunk_init(AVFormatContext *s)
 {
     WebMChunkContext *wc = s->priv_data;
     ff_const59 AVOutputFormat *oformat;
@@ -64,11 +56,17 @@ static int chunk_mux_init(AVFormatContext *s)
     AVDictionary *dict = NULL;
     int ret;
 
+    // DASH Streams can only have one track per file.
+    if (s->nb_streams != 1)
+        return AVERROR(EINVAL);
+
     if (!wc->header_filename) {
         av_log(s, AV_LOG_ERROR, "No header filename provided\n");
         return AVERROR(EINVAL);
     }
 
+    wc->prev_pts = AV_NOPTS_VALUE;
+
     oformat = av_guess_format("webm", s->url, "video/webm");
     if (!oformat)
         return AVERROR_MUXER_NOT_FOUND;
@@ -81,15 +79,16 @@ static int chunk_mux_init(AVFormatContext *s)
     ff_format_set_url(oc, wc->header_filename);
     wc->header_filename = NULL;
 
-    oc->interrupt_callback = s->interrupt_callback;
-    oc->max_delay          = s->max_delay;
+    oc->interrupt_callback    = s->interrupt_callback;
+    oc->max_delay             = s->max_delay;
     oc->flags                 = s->flags & ~AVFMT_FLAG_FLUSH_PACKETS;
     oc->strict_std_compliance = s->strict_std_compliance;
     oc->avoid_negative_ts     = s->avoid_negative_ts;
 
     oc->flush_packets         = 0;
 
-    av_dict_copy(&oc->metadata, s->metadata, 0);
+    if ((ret = av_dict_copy(&oc->metadata, s->metadata, 0)) < 0)
+        return ret;
 
     if (!(st = avformat_new_stream(oc, NULL)))
         return AVERROR(ENOMEM);
@@ -103,11 +102,23 @@ static int chunk_mux_init(AVFormatContext *s)
     avpriv_set_pts_info(st, ost->pts_wrap_bits, ost->time_base.num,
                                                 ost->time_base.den);
 
-    av_dict_set_int(&dict, "dash", 1, 0);
-    av_dict_set_int(&dict, "cluster_time_limit", wc->chunk_duration, 0);
-    av_dict_set_int(&dict, "live", 1, 0);
+    if (wc->http_method)
+        if ((ret = av_dict_set(&dict, "method", wc->http_method, 0)) < 0)
+            return ret;
+    ret = s->io_open(s, &oc->pb, oc->url, AVIO_FLAG_WRITE, &dict);
+    av_dict_free(&dict);
+    if (ret < 0)
+        return ret;
+    oc->pb->seekable = 0;
+
+    if ((ret = av_dict_set_int(&dict, "dash", 1, 0))   < 0 ||
+        (ret = av_dict_set_int(&dict, "cluster_time_limit",
+                               wc->chunk_duration, 0)) < 0 ||
+        (ret = av_dict_set_int(&dict, "live", 1, 0))   < 0)
+        goto fail;
 
     ret = avformat_init_output(oc, &dict);
+fail:
     av_dict_free(&dict);
     if (ret < 0)
         return ret;
@@ -133,40 +144,23 @@ static int get_chunk_filename(AVFormatContext *s, char filename[MAX_FILENAME_SIZ
     if (!filename) {
         return AVERROR(EINVAL);
     }
-        if (av_get_frame_filename(filename, MAX_FILENAME_SIZE,
-                                  s->url, wc->chunk_index - 1) < 0) {
-            av_log(s, AV_LOG_ERROR, "Invalid chunk filename template '%s'\n", s->url);
-            return AVERROR(EINVAL);
-        }
+    if (av_get_frame_filename(filename, MAX_FILENAME_SIZE,
+                              s->url, wc->chunk_index - 1) < 0) {
+        av_log(s, AV_LOG_ERROR, "Invalid chunk filename template '%s'\n", s->url);
+        return AVERROR(EINVAL);
+    }
     return 0;
 }
 
 static int webm_chunk_write_header(AVFormatContext *s)
 {
     WebMChunkContext *wc = s->priv_data;
-    AVFormatContext *oc = NULL;
+    AVFormatContext *oc = wc->avf;
     int ret;
-    AVDictionary *options = NULL;
-
-    // DASH Streams can only have either one track per file.
-    if (s->nb_streams != 1) { return AVERROR_INVALIDDATA; }
-
-    wc->prev_pts = AV_NOPTS_VALUE;
 
-    ret = chunk_mux_init(s);
-    if (ret < 0)
-        return ret;
-    oc = wc->avf;
-    if (wc->http_method)
-        av_dict_set(&options, "method", wc->http_method, 0);
-    ret = s->io_open(s, &oc->pb, oc->url, AVIO_FLAG_WRITE, &options);
-    av_dict_free(&options);
-    if (ret < 0)
-        return ret;
-
-    oc->pb->seekable = 0;
     ret = avformat_write_header(oc, NULL);
     ff_format_io_close(s, &oc->pb);
+    wc->header_written = 1;
     if (ret < 0)
         return ret;
     return 0;
@@ -208,14 +202,15 @@ static int chunk_end(AVFormatContext *s, int flush)
     if (ret < 0)
         goto fail;
     if (wc->http_method)
-        av_dict_set(&options, "method", wc->http_method, 0);
+        if ((ret = av_dict_set(&options, "method", wc->http_method, 0)) < 0)
+            goto fail;
     ret = s->io_open(s, &pb, filename, AVIO_FLAG_WRITE, &options);
+    av_dict_free(&options);
     if (ret < 0)
         goto fail;
     avio_write(pb, buffer, buffer_size);
     ff_format_io_close(s, &pb);
 fail:
-    av_dict_free(&options);
     av_free(buffer);
     return (ret < 0) ? ret : 0;
 }
@@ -261,13 +256,27 @@ static int webm_chunk_write_trailer(AVFormatContext *s)
     if (!oc->pb) {
         ret = chunk_start(s);
         if (ret < 0)
-            goto fail;
+            return ret;
     }
-    av_write_trailer(oc);
-    ret = chunk_end(s, 0);
-fail:
-    avformat_free_context(oc);
-    return ret;
+    ret = av_write_trailer(oc);
+    if (ret < 0)
+        return ret;
+    return chunk_end(s, 0);
+}
+
+static void webm_chunk_deinit(AVFormatContext *s)
+{
+    WebMChunkContext *wc = s->priv_data;
+
+    if (!wc->avf)
+        return;
+
+    if (wc->header_written)
+        ffio_free_dyn_buf(&wc->avf->pb);
+    else
+        ff_format_io_close(s, &wc->avf->pb);
+    avformat_free_context(wc->avf);
+    wc->avf = NULL;
 }
 
 #define OFFSET(x) offsetof(WebMChunkContext, x)
@@ -279,7 +288,6 @@ static const AVOption options[] = {
     { NULL },
 };
 
-#if CONFIG_WEBM_CHUNK_MUXER
 static const AVClass webm_chunk_class = {
     .class_name = "WebM Chunk Muxer",
     .item_name  = av_default_item_name,
@@ -295,9 +303,10 @@ AVOutputFormat ff_webm_chunk_muxer = {
     .flags          = AVFMT_NOFILE | AVFMT_GLOBALHEADER | AVFMT_NEEDNUMBER |
                       AVFMT_TS_NONSTRICT,
     .priv_data_size = sizeof(WebMChunkContext),
+    .init           = webm_chunk_init,
     .write_header   = webm_chunk_write_header,
     .write_packet   = webm_chunk_write_packet,
     .write_trailer  = webm_chunk_write_trailer,
+    .deinit         = webm_chunk_deinit,
     .priv_class     = &webm_chunk_class,
 };
-#endif