]> git.sesse.net Git - nageru/blobdiff - mux.cpp
Write 1.4.0 changelog.
[nageru] / mux.cpp
diff --git a/mux.cpp b/mux.cpp
index 8dd969a3ec8a95b4a21d21a4e7eae906ce584f6d..a7b3a7f838461a9af78f54a2ae93902c26d11c43 100644 (file)
--- a/mux.cpp
+++ b/mux.cpp
@@ -1,12 +1,27 @@
-#include <assert.h>
+#include "mux.h"
 
+#include <assert.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <algorithm>
 #include <mutex>
 #include <string>
+#include <utility>
 #include <vector>
 
+extern "C" {
+#include <libavformat/avio.h>
+#include <libavutil/avutil.h>
+#include <libavutil/dict.h>
+#include <libavutil/mathematics.h>
+#include <libavutil/mem.h>
+#include <libavutil/pixfmt.h>
+#include <libavutil/rational.h>
+}
+
 #include "defs.h"
-#include "mux.h"
 #include "timebase.h"
 
 using namespace std;
@@ -29,8 +44,8 @@ struct PacketBefore {
        const AVFormatContext * const ctx;
 };
 
-Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const string &video_extradata, const AVCodecParameters *audio_codecpar, int time_base)
-       : avctx(avctx)
+Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const string &video_extradata, const AVCodecParameters *audio_codecpar, int time_base, std::function<void(int64_t)> write_callback)
+       : avctx(avctx), write_callback(write_callback)
 {
        avstream_video = avformat_new_stream(avctx, nullptr);
        if (avstream_video == nullptr) {
@@ -94,8 +109,10 @@ Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const
 Mux::~Mux()
 {
        av_write_trailer(avctx);
-       av_free(avctx->pb->buffer);
-       av_free(avctx->pb);
+       if (!(avctx->oformat->flags & AVFMT_NOFILE) &&
+           !(avctx->flags & AVFMT_FLAG_CUSTOM_IO)) {
+               avio_closep(&avctx->pb);
+       }
        avformat_free_context(avctx);
 }
 
@@ -128,6 +145,14 @@ void Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts)
        }
 
        av_packet_unref(&pkt_copy);
+
+       // Note: This will be wrong in the case of plugged packets, but that only happens
+       // for network streams, not for files, and write callbacks are only really relevant
+       // for files. (We don't want to do this from write_packet_or_die, as it only has
+       // the rescaled pts, which is unsuitable for callback.)
+       if (pkt.stream_index == 0 && write_callback != nullptr) {
+               write_callback(pts);
+       }
 }
 
 void Mux::write_packet_or_die(const AVPacket &pkt)