]> git.sesse.net Git - nageru/blobdiff - mux.cpp
Add some asserts that will trigger if a driver gives very bogus fps values.
[nageru] / mux.cpp
diff --git a/mux.cpp b/mux.cpp
index e16943828fcccaa8f9d5e65ad9008cc9bb1456dd..cc67eb616b466b4a4e42f89fe511eeba8c081e11 100644 (file)
--- a/mux.cpp
+++ b/mux.cpp
@@ -1,5 +1,6 @@
 #include <assert.h>
 
+#include <algorithm>
 #include <mutex>
 #include <string>
 #include <vector>
 
 using namespace std;
 
-Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const string &video_extradata, const AVCodecContext *audio_ctx, int time_base, KeyFrameSignalReceiver *keyframe_signal_receiver)
-       : avctx(avctx), keyframe_signal_receiver(keyframe_signal_receiver)
+struct PacketBefore {
+       PacketBefore(const AVFormatContext *ctx) : ctx(ctx) {}
+
+       bool operator() (const AVPacket *a, const AVPacket *b) const {
+               int64_t a_dts = (a->dts == AV_NOPTS_VALUE ? a->pts : a->dts);
+               int64_t b_dts = (b->dts == AV_NOPTS_VALUE ? b->pts : b->dts);
+               AVRational a_timebase = ctx->streams[a->stream_index]->time_base;
+               AVRational b_timebase = ctx->streams[b->stream_index]->time_base;
+               if (av_compare_ts(a_dts, a_timebase, b_dts, b_timebase) != 0) {
+                       return av_compare_ts(a_dts, a_timebase, b_dts, b_timebase) < 0;
+               } else {
+                       return av_compare_ts(a->pts, a_timebase, b->pts, b_timebase) < 0;
+               }
+       }
+
+       const AVFormatContext * const ctx;
+};
+
+Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const string &video_extradata, const AVCodecContext *audio_ctx, int time_base)
+       : avctx(avctx)
 {
        AVCodec *codec_video = avcodec_find_encoder((video_codec == CODEC_H264) ? AV_CODEC_ID_H264 : AV_CODEC_ID_RAWVIDEO);
        avstream_video = avformat_new_stream(avctx, codec_video);
@@ -99,20 +118,46 @@ void Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts)
                assert(false);
        }
 
-       if (keyframe_signal_receiver) {
-               if (pkt.flags & AV_PKT_FLAG_KEY) {
-                       av_write_frame(avctx, nullptr);
-                       keyframe_signal_receiver->signal_keyframe();
-               }
-       }
-
        {
-               lock_guard<mutex> lock(ctx_mu);
-               if (av_interleaved_write_frame(avctx, &pkt_copy) < 0) {
-                       fprintf(stderr, "av_interleaved_write_frame() failed\n");
-                       exit(1);
+               lock_guard<mutex> lock(mu);
+               if (plug_count > 0) {
+                       plugged_packets.push_back(av_packet_clone(&pkt_copy));
+               } else {
+                       write_packet_or_die(pkt_copy);
                }
        }
 
        av_packet_unref(&pkt_copy);
 }
+
+void Mux::write_packet_or_die(const AVPacket &pkt)
+{
+       if (av_interleaved_write_frame(avctx, const_cast<AVPacket *>(&pkt)) < 0) {
+               fprintf(stderr, "av_interleaved_write_frame() failed\n");
+               exit(1);
+       }
+       avio_flush(avctx->pb);
+}
+
+void Mux::plug()
+{
+       lock_guard<mutex> lock(mu);
+       ++plug_count;
+}
+
+void Mux::unplug()
+{
+       lock_guard<mutex> lock(mu);
+       if (--plug_count > 0) {
+               return;
+       }
+       assert(plug_count >= 0);
+
+       sort(plugged_packets.begin(), plugged_packets.end(), PacketBefore(avctx));
+
+       for (AVPacket *pkt : plugged_packets) {
+               write_packet_or_die(*pkt);
+               av_packet_free(&pkt);
+       }
+       plugged_packets.clear();
+}