subtitle_stream_idx = streams.size() - 1;
}
- AVDictionary *options = NULL;
- vector<pair<string, string>> opts = MUX_OPTS;
- for (pair<string, string> opt : opts) {
- av_dict_set(&options, opt.first.c_str(), opt.second.c_str(), 0);
- }
- int err = avformat_write_header(avctx, &options);
- if (err < 0) {
- char errbuf[AV_ERROR_MAX_STRING_SIZE];
- av_strerror(err, errbuf, sizeof(errbuf));
- fprintf(stderr, "avformat_write_header() failed: %s\n", errbuf);
- exit(EXIT_FAILURE);
- }
- for (MuxMetrics *metric : metrics) {
- metric->metric_written_bytes += avctx->pb->pos;
- }
-
- // Make sure the header is written before the constructor exits.
- avio_flush(avctx->pb);
-
if (write_strategy == WRITE_BACKGROUND) {
writer_thread = thread(&Mux::thread_func, this);
+ } else {
+ write_header();
}
}
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();
{
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();
void Mux::plug()
{
lock_guard<mutex> lock(mu);
+ assert(!drained);
++plug_count;
}
}
}
+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");
+ write_header();
+
unique_lock<mutex> lock(mu);
for ( ;; ) {
packet_queue_ready.wait(lock, [this]() {
}
}
+void Mux::write_header()
+{
+ AVDictionary *options = NULL;
+ vector<pair<string, string>> opts = MUX_OPTS;
+ for (pair<string, string> opt : opts) {
+ av_dict_set(&options, opt.first.c_str(), opt.second.c_str(), 0);
+ }
+
+ int err = avformat_write_header(avctx, &options);
+ if (err < 0) {
+ char errbuf[AV_ERROR_MAX_STRING_SIZE];
+ av_strerror(err, errbuf, sizeof(errbuf));
+ fprintf(stderr, "avformat_write_header() failed: %s\n", errbuf);
+ exit(EXIT_FAILURE);
+ }
+ for (MuxMetrics *metric : metrics) {
+ metric->metric_written_bytes += avctx->pb->pos;
+ }
+
+ // 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)
{
vector<pair<string, string>> labels_video = labels;