- vaUnmapBuffer(va_dpy, gl_surfaces[display_order % SURFACE_NUM].coded_buf);
-
- AVPacket pkt;
- memset(&pkt, 0, sizeof(pkt));
- pkt.buf = nullptr;
- pkt.pts = av_rescale_q(display_order, AVRational{1, frame_rate}, avstream->time_base);
- pkt.dts = av_rescale_q(encode_order, AVRational{1, frame_rate}, avstream->time_base);
- pkt.data = reinterpret_cast<uint8_t *>(&data[0]);
- pkt.size = data.size();
- pkt.stream_index = 0;
- if (frame_type == FRAME_IDR || frame_type == FRAME_I) {
- pkt.flags = AV_PKT_FLAG_KEY;
- } else {
- pkt.flags = 0;
+ vaUnmapBuffer(va_dpy, gl_surfaces[task.display_order % SURFACE_NUM].coded_buf);
+
+ {
+ // Add video.
+ AVPacket pkt;
+ memset(&pkt, 0, sizeof(pkt));
+ pkt.buf = nullptr;
+ pkt.pts = av_rescale_q(task.pts + global_delay, AVRational{1, TIMEBASE}, avstream_video->time_base);
+ pkt.dts = av_rescale_q(task.dts + global_delay, AVRational{1, TIMEBASE}, avstream_video->time_base);
+ pkt.data = reinterpret_cast<uint8_t *>(&data[0]);
+ pkt.size = data.size();
+ pkt.stream_index = 0;
+ if (task.frame_type == FRAME_IDR || task.frame_type == FRAME_I) {
+ pkt.flags = AV_PKT_FLAG_KEY;
+ } else {
+ pkt.flags = 0;
+ }
+ //pkt.duration = 1;
+ httpd->add_packet(pkt);
+ av_interleaved_write_frame(avctx, &pkt);
+ }
+ // Encode and add all audio frames up to and including the pts of this video frame.
+ // (They can never be queued to us after the video frame they belong to, only before.)
+ for ( ;; ) {
+ int64_t audio_pts;
+ std::vector<float> audio;
+ {
+ unique_lock<mutex> lock(frame_queue_mutex);
+ if (pending_audio_frames.empty()) break;
+ auto it = pending_audio_frames.begin();
+ if (it->first > task.pts) break;
+ audio_pts = it->first;
+ audio = move(it->second);
+ pending_audio_frames.erase(it);
+ }
+ AVFrame *frame = avcodec_alloc_frame();
+ frame->nb_samples = audio.size() / 2;
+ frame->format = AV_SAMPLE_FMT_FLT;
+ frame->channel_layout = AV_CH_LAYOUT_STEREO;
+
+ unique_ptr<float[]> planar_samples(new float[audio.size()]);
+ avcodec_fill_audio_frame(frame, 2, AV_SAMPLE_FMT_FLTP, (const uint8_t*)planar_samples.get(), audio.size() * sizeof(float), 0);
+ for (int i = 0; i < frame->nb_samples; ++i) {
+ planar_samples[i] = audio[i * 2 + 0];
+ planar_samples[i + frame->nb_samples] = audio[i * 2 + 1];
+ }
+
+ AVPacket pkt;
+ av_init_packet(&pkt);
+ pkt.data = nullptr;
+ pkt.size = 0;
+ int got_output;
+ avcodec_encode_audio2(avstream_audio->codec, &pkt, frame, &got_output);
+ if (got_output) {
+ pkt.pts = av_rescale_q(audio_pts + global_delay, AVRational{1, TIMEBASE}, avstream_audio->time_base);
+ pkt.dts = pkt.pts;
+ pkt.stream_index = 1;
+ httpd->add_packet(pkt);
+ av_interleaved_write_frame(avctx, &pkt);
+ }
+ // TODO: Delayed frames.
+ avcodec_free_frame(&frame);