- any_frames_to_be_encoded.wait(lock, [this] { return !frames_to_be_encoded.empty() || should_quit; });
- if (should_quit) return;
- QueuedFrame qf = move(frames_to_be_encoded.front());
- frames_to_be_encoded.pop();
-
- vector<uint8_t> jpeg = encode_jpeg(qf);
-
- AVPacket pkt;
- memset(&pkt, 0, sizeof(pkt));
- pkt.buf = nullptr;
- pkt.data = &jpeg[0];
- pkt.size = jpeg.size();
- pkt.stream_index = qf.card_index;
- pkt.flags = AV_PKT_FLAG_KEY;
- pkt.pts = pkt.dts = qf.pts;
-
- if (av_write_frame(avctx.get(), &pkt) < 0) {
- fprintf(stderr, "av_write_frame() failed\n");
- exit(1);
+ QueuedFrame qf;
+ {
+ unique_lock<mutex> lock(mu);
+ any_frames_to_be_encoded.wait(lock, [this] { return !frames_to_be_encoded.empty() || should_quit; });
+ if (should_quit) break;
+ qf = move(frames_to_be_encoded.front());
+ frames_to_be_encoded.pop();
+ }
+
+ if (va_dpy != nullptr) {
+ // Will call back in the receiver thread.
+ encode_jpeg_va(move(qf));
+ } else {
+ // Encode synchronously, in the same thread.
+ vector<uint8_t> jpeg = encode_jpeg_libjpeg(qf);
+ write_mjpeg_packet(qf.pts, qf.card_index, jpeg);