Offload vaapi send data to secondary thread
This commit is contained in:
@@ -70,7 +70,8 @@ void VideoEncoderFFMPEG::Encode(bool idr, std::chrono::steady_clock::time_point
|
|||||||
int err = avcodec_receive_packet(encoder_ctx.get(), enc_pkt.get());
|
int err = avcodec_receive_packet(encoder_ctx.get(), enc_pkt.get());
|
||||||
if (err == 0)
|
if (err == 0)
|
||||||
{
|
{
|
||||||
SendData(std::span<uint8_t>(enc_pkt->data, enc_pkt->size), true);
|
enc_pkt.reset(packet_to_send.exchange(enc_pkt.release()));
|
||||||
|
packet_to_send.notify_all();
|
||||||
}
|
}
|
||||||
if (err == AVERROR(EAGAIN))
|
if (err == AVERROR(EAGAIN))
|
||||||
{
|
{
|
||||||
@@ -82,3 +83,24 @@ void VideoEncoderFFMPEG::Encode(bool idr, std::chrono::steady_clock::time_point
|
|||||||
throw std::runtime_error("frame encoding failed, code " + std::to_string(err));
|
throw std::runtime_error("frame encoding failed, code " + std::to_string(err));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VideoEncoderFFMPEG::VideoEncoderFFMPEG()
|
||||||
|
{
|
||||||
|
send_thread = std::jthread(
|
||||||
|
[this](std::stop_token stop) {
|
||||||
|
while (not stop.stop_requested())
|
||||||
|
{
|
||||||
|
packet_to_send.wait(nullptr);
|
||||||
|
av_packet_ptr packet(packet_to_send.exchange(nullptr));
|
||||||
|
if (packet)
|
||||||
|
SendData(std::span<uint8_t>(packet->data, packet->size), true);
|
||||||
|
}
|
||||||
|
av_packet_ptr packet(packet_to_send.exchange(nullptr));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
VideoEncoderFFMPEG::~VideoEncoderFFMPEG()
|
||||||
|
{
|
||||||
|
send_thread.request_stop();
|
||||||
|
packet_to_send.notify_all();
|
||||||
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
#include "encoder/video_encoder.h"
|
#include "encoder/video_encoder.h"
|
||||||
#include "ffmpeg_helper.h"
|
#include "ffmpeg_helper.h"
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
class VideoEncoderFFMPEG : public xrt::drivers::wivrn::VideoEncoder
|
class VideoEncoderFFMPEG : public xrt::drivers::wivrn::VideoEncoder
|
||||||
{
|
{
|
||||||
@@ -32,6 +33,8 @@ public:
|
|||||||
Encode(bool idr, std::chrono::steady_clock::time_point target_timestamp) override;
|
Encode(bool idr, std::chrono::steady_clock::time_point target_timestamp) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
VideoEncoderFFMPEG();
|
||||||
|
virtual ~VideoEncoderFFMPEG();
|
||||||
virtual void
|
virtual void
|
||||||
PushFrame(bool idr, std::chrono::steady_clock::time_point pts) = 0;
|
PushFrame(bool idr, std::chrono::steady_clock::time_point pts) = 0;
|
||||||
|
|
||||||
@@ -39,4 +42,6 @@ protected:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
static bool once;
|
static bool once;
|
||||||
|
std::atomic<AVPacket *> packet_to_send;
|
||||||
|
std::jthread send_thread;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -121,15 +121,19 @@ void VideoEncoder::Encode(wivrn_session & cnx,
|
|||||||
last_idr_frame = frame_index;
|
last_idr_frame = frame_index;
|
||||||
const char * extra = idr ? ",idr" : ",p";
|
const char * extra = idr ? ",idr" : ",p";
|
||||||
clock = cnx.get_offset();
|
clock = cnx.get_offset();
|
||||||
timing_info.encode_begin = clock.to_headset(os_monotonic_get_ns());
|
|
||||||
cnx.dump_time("encode_begin", frame_index, os_monotonic_get_ns(), stream_idx, extra);
|
|
||||||
|
|
||||||
// Prepare the video shard template
|
{
|
||||||
shard.stream_item_idx = stream_idx;
|
std::lock_guard lock(mutex);
|
||||||
shard.frame_idx = frame_index;
|
timing_info.encode_begin = clock.to_headset(os_monotonic_get_ns());
|
||||||
shard.shard_idx = 0;
|
cnx.dump_time("encode_begin", frame_index, os_monotonic_get_ns(), stream_idx, extra);
|
||||||
shard.view_info = view_info;
|
|
||||||
shard.timing_info.reset();
|
// Prepare the video shard template
|
||||||
|
shard.stream_item_idx = stream_idx;
|
||||||
|
shard.frame_idx = frame_index;
|
||||||
|
shard.shard_idx = 0;
|
||||||
|
shard.view_info = view_info;
|
||||||
|
shard.timing_info.reset();
|
||||||
|
}
|
||||||
|
|
||||||
Encode(idr, target_timestamp);
|
Encode(idr, target_timestamp);
|
||||||
cnx.dump_time("encode_end", frame_index, os_monotonic_get_ns(), stream_idx, extra);
|
cnx.dump_time("encode_end", frame_index, os_monotonic_get_ns(), stream_idx, extra);
|
||||||
|
|||||||
Reference in New Issue
Block a user