Use a single thread for all encoder async send
This commit is contained in:
@@ -63,44 +63,23 @@ bool set_log_level()
|
||||
|
||||
bool VideoEncoderFFMPEG::once = set_log_level();
|
||||
|
||||
void VideoEncoderFFMPEG::Encode(bool idr, std::chrono::steady_clock::time_point target_timestamp)
|
||||
std::optional<xrt::drivers::wivrn::VideoEncoder::data> VideoEncoderFFMPEG::encode(bool idr, std::chrono::steady_clock::time_point target_timestamp)
|
||||
{
|
||||
PushFrame(idr, target_timestamp);
|
||||
av_packet_ptr enc_pkt(av_packet_alloc());
|
||||
std::shared_ptr<AVPacket> enc_pkt(av_packet_alloc(), [](AVPacket * d) { av_packet_free(&d); });
|
||||
int err = avcodec_receive_packet(encoder_ctx.get(), enc_pkt.get());
|
||||
if (err == 0)
|
||||
{
|
||||
enc_pkt.reset(packet_to_send.exchange(enc_pkt.release()));
|
||||
packet_to_send.notify_all();
|
||||
return data{
|
||||
.encoder = this,
|
||||
.span = std::span(enc_pkt->data, enc_pkt->size),
|
||||
.mem = enc_pkt,
|
||||
};
|
||||
}
|
||||
if (err == AVERROR(EAGAIN))
|
||||
{
|
||||
U_LOG_W("EAGAIN in encoder %d", stream_idx);
|
||||
return;
|
||||
}
|
||||
if (err)
|
||||
{
|
||||
throw std::runtime_error("frame encoding failed, code " + std::to_string(err));
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
throw std::runtime_error("frame encoding failed, code " + std::to_string(err));
|
||||
}
|
||||
|
||||
@@ -22,19 +22,18 @@
|
||||
#include "encoder/video_encoder.h"
|
||||
#include "ffmpeg_helper.h"
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
class VideoEncoderFFMPEG : public xrt::drivers::wivrn::VideoEncoder
|
||||
{
|
||||
public:
|
||||
using Codec = xrt::drivers::wivrn::video_codec;
|
||||
|
||||
void
|
||||
Encode(bool idr, std::chrono::steady_clock::time_point target_timestamp) override;
|
||||
std::optional<data> encode(bool idr, std::chrono::steady_clock::time_point target_timestamp) override;
|
||||
|
||||
protected:
|
||||
VideoEncoderFFMPEG();
|
||||
virtual ~VideoEncoderFFMPEG();
|
||||
VideoEncoderFFMPEG() :
|
||||
xrt::drivers::wivrn::VideoEncoder(true) {}
|
||||
|
||||
virtual void
|
||||
PushFrame(bool idr, std::chrono::steady_clock::time_point pts) = 0;
|
||||
|
||||
@@ -42,6 +41,4 @@ protected:
|
||||
|
||||
private:
|
||||
static bool once;
|
||||
std::atomic<AVPacket *> packet_to_send;
|
||||
std::jthread send_thread;
|
||||
};
|
||||
|
||||
@@ -42,6 +42,62 @@
|
||||
namespace xrt::drivers::wivrn
|
||||
{
|
||||
|
||||
VideoEncoder::sender::sender() :
|
||||
thread([this](std::stop_token t) {
|
||||
while (not t.stop_requested())
|
||||
{
|
||||
data d;
|
||||
{
|
||||
std::unique_lock lock(mutex);
|
||||
if (pending.empty())
|
||||
{
|
||||
cv.wait_for(lock, std::chrono::milliseconds(100));
|
||||
}
|
||||
else
|
||||
{
|
||||
d = std::move(pending.front());
|
||||
pending.pop_front();
|
||||
if (pending.empty())
|
||||
cv.notify_all();
|
||||
}
|
||||
}
|
||||
if (not d.span.empty())
|
||||
d.encoder->SendData(d.span, true);
|
||||
}
|
||||
std::unique_lock lock(mutex);
|
||||
pending.clear();
|
||||
cv.notify_all();
|
||||
})
|
||||
{
|
||||
}
|
||||
|
||||
void VideoEncoder::sender::push(data && d)
|
||||
{
|
||||
std::unique_lock lock(mutex);
|
||||
pending.push_back(std::move(d));
|
||||
cv.notify_all();
|
||||
}
|
||||
|
||||
void VideoEncoder::sender::wait_idle()
|
||||
{
|
||||
std::unique_lock lock(mutex);
|
||||
while (not pending.empty())
|
||||
cv.wait_for(lock, std::chrono::milliseconds(100));
|
||||
}
|
||||
|
||||
std::shared_ptr<VideoEncoder::sender> VideoEncoder::sender::get()
|
||||
{
|
||||
static std::weak_ptr<VideoEncoder::sender> instance;
|
||||
static std::mutex m;
|
||||
std::unique_lock lock(m);
|
||||
auto s = instance.lock();
|
||||
if (s)
|
||||
return s;
|
||||
s.reset(new VideoEncoder::sender());
|
||||
instance = s;
|
||||
return s;
|
||||
}
|
||||
|
||||
std::unique_ptr<VideoEncoder> VideoEncoder::Create(
|
||||
wivrn_vk_bundle & wivrn_vk,
|
||||
encoder_settings & settings,
|
||||
@@ -95,8 +151,16 @@ std::unique_ptr<VideoEncoder> VideoEncoder::Create(
|
||||
|
||||
static const uint64_t idr_throttle = 100;
|
||||
|
||||
VideoEncoder::VideoEncoder() :
|
||||
last_idr_frame(-idr_throttle) {}
|
||||
VideoEncoder::VideoEncoder(bool async_send) :
|
||||
last_idr_frame(-idr_throttle),
|
||||
shared_sender(async_send ? sender::get() : nullptr)
|
||||
{}
|
||||
|
||||
VideoEncoder::~VideoEncoder()
|
||||
{
|
||||
if (shared_sender)
|
||||
shared_sender->wait_idle();
|
||||
}
|
||||
|
||||
void VideoEncoder::SyncNeeded()
|
||||
{
|
||||
@@ -107,6 +171,8 @@ void VideoEncoder::Encode(wivrn_session & cnx,
|
||||
const to_headset::video_stream_data_shard::view_info_t & view_info,
|
||||
uint64_t frame_index)
|
||||
{
|
||||
if (shared_sender)
|
||||
shared_sender->wait_idle();
|
||||
this->cnx = &cnx;
|
||||
auto target_timestamp = std::chrono::steady_clock::time_point(std::chrono::nanoseconds(view_info.display_time));
|
||||
bool idr = sync_needed.exchange(false);
|
||||
@@ -122,20 +188,22 @@ void VideoEncoder::Encode(wivrn_session & cnx,
|
||||
const char * extra = idr ? ",idr" : ",p";
|
||||
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;
|
||||
shard.frame_idx = frame_index;
|
||||
shard.shard_idx = 0;
|
||||
shard.view_info = view_info;
|
||||
shard.timing_info.reset();
|
||||
|
||||
auto data = encode(idr, target_timestamp);
|
||||
if (data)
|
||||
{
|
||||
std::lock_guard lock(mutex);
|
||||
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;
|
||||
shard.frame_idx = frame_index;
|
||||
shard.shard_idx = 0;
|
||||
shard.view_info = view_info;
|
||||
shard.timing_info.reset();
|
||||
assert(shared_sender);
|
||||
shared_sender->push(std::move(*data));
|
||||
}
|
||||
|
||||
Encode(idr, target_timestamp);
|
||||
cnx.dump_time("encode_end", frame_index, os_monotonic_get_ns(), stream_idx, extra);
|
||||
}
|
||||
|
||||
|
||||
@@ -22,9 +22,12 @@
|
||||
#include "driver/clock_offset.h"
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <deque>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
|
||||
#include "encoder_settings.h"
|
||||
@@ -44,12 +47,36 @@ inline const char * encoder_x264 = "x264";
|
||||
|
||||
class VideoEncoder
|
||||
{
|
||||
std::mutex mutex;
|
||||
protected:
|
||||
struct data
|
||||
{
|
||||
VideoEncoder * encoder;
|
||||
std::span<uint8_t> span;
|
||||
std::shared_ptr<void> mem;
|
||||
};
|
||||
|
||||
private:
|
||||
class sender
|
||||
{
|
||||
std::mutex mutex;
|
||||
std::condition_variable cv;
|
||||
std::deque<data> pending;
|
||||
std::jthread thread;
|
||||
void run();
|
||||
sender();
|
||||
|
||||
public:
|
||||
void push(data &&);
|
||||
static std::shared_ptr<sender> get();
|
||||
void wait_idle();
|
||||
};
|
||||
|
||||
protected:
|
||||
uint8_t stream_idx;
|
||||
|
||||
private:
|
||||
std::mutex mutex;
|
||||
|
||||
// temporary data
|
||||
wivrn_session * cnx;
|
||||
|
||||
@@ -64,6 +91,8 @@ private:
|
||||
|
||||
std::ofstream video_dump;
|
||||
|
||||
std::shared_ptr<sender> shared_sender;
|
||||
|
||||
public:
|
||||
static std::unique_ptr<VideoEncoder> Create(
|
||||
wivrn_vk_bundle &,
|
||||
@@ -73,8 +102,8 @@ public:
|
||||
int input_height,
|
||||
float fps);
|
||||
|
||||
VideoEncoder();
|
||||
virtual ~VideoEncoder() = default;
|
||||
VideoEncoder(bool async_send = false);
|
||||
virtual ~VideoEncoder();
|
||||
|
||||
// called on present to submit command buffers for the image.
|
||||
virtual void PresentImage(yuv_converter & src_yuv, vk::raii::CommandBuffer & cmd_buf) = 0;
|
||||
@@ -88,7 +117,7 @@ public:
|
||||
|
||||
protected:
|
||||
// called when command buffer finished executing
|
||||
virtual void Encode(bool idr, std::chrono::steady_clock::time_point target_timestamp) = 0;
|
||||
virtual std::optional<data> encode(bool idr, std::chrono::steady_clock::time_point target_timestamp) = 0;
|
||||
|
||||
void SendData(std::span<uint8_t> data, bool end_of_frame);
|
||||
};
|
||||
|
||||
@@ -357,7 +357,7 @@ void VideoEncoderNvenc::PresentImage(yuv_converter & src_yuv, vk::raii::CommandB
|
||||
return;
|
||||
}
|
||||
|
||||
void VideoEncoderNvenc::Encode(bool idr, std::chrono::steady_clock::time_point pts)
|
||||
std::optional<VideoEncoder::data> VideoEncoderNvenc::encode(bool idr, std::chrono::steady_clock::time_point pts)
|
||||
{
|
||||
CU_CHECK(cuda_fn->cuCtxPushCurrent(cuda));
|
||||
|
||||
@@ -397,6 +397,7 @@ void VideoEncoderNvenc::Encode(bool idr, std::chrono::steady_clock::time_point p
|
||||
NVENC_CHECK(fn.nvEncUnlockBitstream(session_handle, bitstreamBuffer));
|
||||
|
||||
CU_CHECK(cuda_fn->cuCtxPopCurrent(NULL));
|
||||
return {};
|
||||
}
|
||||
|
||||
std::array<int, 2> VideoEncoderNvenc::get_max_size(video_codec codec)
|
||||
|
||||
@@ -68,7 +68,7 @@ public:
|
||||
~VideoEncoderNvenc();
|
||||
|
||||
void PresentImage(yuv_converter & src_yuv, vk::raii::CommandBuffer & cmd_buf) override;
|
||||
void Encode(bool idr, std::chrono::steady_clock::time_point pts) override;
|
||||
std::optional<data> encode(bool idr, std::chrono::steady_clock::time_point pts) override;
|
||||
|
||||
static std::array<int, 2> get_max_size(video_codec);
|
||||
};
|
||||
|
||||
@@ -221,7 +221,7 @@ void VideoEncoderX264::PresentImage(yuv_converter & src_yuv, vk::raii::CommandBu
|
||||
}});
|
||||
}
|
||||
|
||||
void VideoEncoderX264::Encode(bool idr, std::chrono::steady_clock::time_point pts)
|
||||
std::optional<VideoEncoder::data> VideoEncoderX264::encode(bool idr, std::chrono::steady_clock::time_point pts)
|
||||
{
|
||||
int num_nal;
|
||||
x264_nal_t * nal;
|
||||
@@ -237,12 +237,8 @@ void VideoEncoderX264::Encode(bool idr, std::chrono::steady_clock::time_point pt
|
||||
if (size < 0)
|
||||
{
|
||||
U_LOG_W("x264_encoder_encode failed: %d", size);
|
||||
return;
|
||||
}
|
||||
if (size == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
VideoEncoderX264::~VideoEncoderX264()
|
||||
|
||||
@@ -61,7 +61,7 @@ public:
|
||||
|
||||
void PresentImage(yuv_converter & src_yuv, vk::raii::CommandBuffer & cmd_buf) override;
|
||||
|
||||
void Encode(bool idr, std::chrono::steady_clock::time_point pts) override;
|
||||
std::optional<data> encode(bool idr, std::chrono::steady_clock::time_point pts) override;
|
||||
|
||||
~VideoEncoderX264();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user