]> git.sesse.net Git - nageru/blobdiff - shared/mux.cpp
Drop buffered SRT data if the connection is down for too long.
[nageru] / shared / mux.cpp
index 46f727c8a1636c9cb2869aa862e3d88e34a23c1f..a52f21fbdab35b18bae0161cab70b9bd5d6013dd 100644 (file)
@@ -157,7 +157,6 @@ void Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts, AVRational t
        assert(pts >= dts);
 
        AVPacket pkt_copy;
-       av_init_packet(&pkt_copy);
        if (av_packet_ref(&pkt_copy, &pkt) < 0) {
                fprintf(stderr, "av_copy_packet() failed\n");
                abort();
@@ -173,7 +172,9 @@ void Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts, AVRational t
 
        {
                lock_guard<mutex> lock(mu);
-               if (write_strategy == WriteStrategy::WRITE_BACKGROUND) {
+               if (drained) {
+                       // Just drop the packet on the floor.
+               } else if (write_strategy == WriteStrategy::WRITE_BACKGROUND) {
                        packet_queue.push_back(QueuedPacket{ av_packet_clone(&pkt_copy), pts });
                        if (plug_count == 0)
                                packet_queue_ready.notify_all();
@@ -219,6 +220,7 @@ void Mux::write_packet_or_die(const AVPacket &pkt, int64_t unscaled_pts)
 void Mux::plug()
 {
        lock_guard<mutex> lock(mu);
+       assert(!drained);
        ++plug_count;
 }
 
@@ -243,6 +245,25 @@ void Mux::unplug()
        }
 }
 
+void Mux::drain()
+{
+       lock_guard<mutex> lock(mu);
+       assert(!drained);
+       assert(plug_count == 0);
+       for (QueuedPacket &qp : packet_queue) {
+               av_packet_free(&qp.pkt);
+       }
+       packet_queue.clear();
+       drained = true;
+}
+
+void Mux::undrain()
+{
+       lock_guard<mutex> lock(mu);
+       assert(drained);
+       drained = false;
+}
+
 void Mux::thread_func()
 {
        pthread_setname_np(pthread_self(), "Mux");
@@ -294,6 +315,7 @@ void Mux::write_header()
        // Make sure the header is written before the constructor exits
        // (assuming we are in WRITE_FOREGROUND mode).
        avio_flush(avctx->pb);
+
 }
 
 void MuxMetrics::init(const vector<pair<string, string>> &labels)