wip: set bitrate at runtime
This commit is contained in:
@@ -40,6 +40,9 @@
|
||||
<arg type="s" name="Pin" direction="out"/>
|
||||
</method>
|
||||
<method name="DisablePairing"/>
|
||||
<method name="SetBitrate">
|
||||
<arg type="i" name="BitrateBps"/>
|
||||
</method>
|
||||
|
||||
<property name="HeadsetConnected" type="b" access="read"/>
|
||||
<property name="SteamCommand" type="s" access="read"/>
|
||||
|
||||
@@ -30,6 +30,11 @@ static void handle_event_from_main_loop(to_monado::disconnect)
|
||||
// Ignore disconnect request when no headset is connected
|
||||
}
|
||||
|
||||
static void handle_event_from_main_loop(to_monado::set_bitrate)
|
||||
{
|
||||
// Ignore bitrate request when no headset is connected
|
||||
}
|
||||
|
||||
std::unique_ptr<wivrn::TCP> wivrn::accept_connection(int watch_fd, std::function<bool()> quit)
|
||||
{
|
||||
wivrn_ipc_socket_monado->send(from_monado::headset_disconnected{});
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
#include "driver/wivrn_session.h"
|
||||
#include "encoder/video_encoder.h"
|
||||
#include "util/u_logging.h"
|
||||
#include "utils/scoped_lock.h"
|
||||
#include "wivrn_foveation.h"
|
||||
|
||||
@@ -784,6 +785,17 @@ void wivrn_comp_target::reset_encoders()
|
||||
cnx.send_control(to_headset::video_stream_description{desc});
|
||||
}
|
||||
|
||||
void wivrn_comp_target::set_bitrate(int bitrate_bps)
|
||||
{
|
||||
for (auto & encoder: encoders)
|
||||
{
|
||||
// Alpha will have multiplier of 0 and will be left unchanged.
|
||||
auto encoder_bps = (int)(bitrate_bps * encoder->bitrate_multiplier);
|
||||
U_LOG_D("Encoder %d bitrate: %d", encoder->stream_idx, encoder_bps);
|
||||
encoder->set_bitrate(encoder_bps);
|
||||
}
|
||||
}
|
||||
|
||||
void wivrn_comp_target::render_dynamic_foveation(std::array<to_headset::foveation_parameter, 2> foveation)
|
||||
{
|
||||
assert(foveation_renderer);
|
||||
|
||||
@@ -105,6 +105,7 @@ struct wivrn_comp_target : public comp_target
|
||||
|
||||
void on_feedback(const from_headset::feedback &, const clock_offset &);
|
||||
void reset_encoders();
|
||||
void set_bitrate(int bitrate_bps);
|
||||
|
||||
void render_dynamic_foveation(std::array<to_headset::foveation_parameter, 2> foveation);
|
||||
|
||||
|
||||
@@ -38,6 +38,11 @@ static void handle_event_from_main_loop(to_monado::disconnect)
|
||||
// Ignore disconnect request when no headset is connected
|
||||
}
|
||||
|
||||
static void handle_event_from_main_loop(to_monado::set_bitrate)
|
||||
{
|
||||
// Ignore bitrate request when no headset is connected
|
||||
}
|
||||
|
||||
static std::string clean_key(std::string key)
|
||||
{
|
||||
static const std::regex header{"^-+BEGIN .*-+$", std::regex_constants::multiline};
|
||||
|
||||
@@ -449,6 +449,11 @@ void wivrn_session::operator()(to_monado::disconnect &&)
|
||||
throw std::runtime_error("Disconnecting as requested by main loop");
|
||||
}
|
||||
|
||||
void wivrn_session::operator()(to_monado::set_bitrate && data)
|
||||
{
|
||||
comp_target->set_bitrate(data.bitrate_bps);
|
||||
}
|
||||
|
||||
void wivrn_session::run(std::stop_token stop)
|
||||
{
|
||||
while (not stop.stop_requested())
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "wivrn_connection.h"
|
||||
#include "wivrn_controller.h"
|
||||
#include "wivrn_hmd.h"
|
||||
#include "wivrn_ipc.h"
|
||||
#include "wivrn_packets.h"
|
||||
#include "xrt/xrt_results.h"
|
||||
#include "xrt/xrt_system.h"
|
||||
@@ -164,6 +165,7 @@ public:
|
||||
void operator()(audio_data &&);
|
||||
|
||||
void operator()(to_monado::disconnect &&);
|
||||
void operator()(to_monado::set_bitrate &&);
|
||||
|
||||
template <typename T>
|
||||
void send_stream(T && packet)
|
||||
|
||||
@@ -76,7 +76,8 @@ static void split_bitrate(std::vector<wivrn::encoder_settings> & encoders, uint6
|
||||
|
||||
for (auto & encoder: encoders)
|
||||
{
|
||||
encoder.bitrate = encoder.bitrate * bitrate / total_weight;
|
||||
encoder.bitrate_multiplier = encoder.bitrate / total_weight;
|
||||
encoder.bitrate = encoder.bitrate_multiplier * bitrate;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ struct encoder_settings : public to_headset::video_stream_description::item
|
||||
// encoder identifier, such as nvenc, vaapi or x264
|
||||
std::string encoder_name;
|
||||
uint64_t bitrate; // bit/s
|
||||
double bitrate_multiplier; // encoder bitrate / global bitrate
|
||||
std::map<std::string, std::string> options; // additional encoder-specific configuration
|
||||
// encoders in the same group are executed in sequence
|
||||
int group = 0;
|
||||
|
||||
@@ -68,6 +68,14 @@ bool video_encoder_ffmpeg::once = set_log_level();
|
||||
|
||||
std::optional<wivrn::video_encoder::data> video_encoder_ffmpeg::encode(bool idr, std::chrono::steady_clock::time_point target_timestamp, uint8_t slot)
|
||||
{
|
||||
if (auto bitrate = pending_bitrate.exchange(BITRATE_UNCHANGED))
|
||||
{
|
||||
U_LOG_E("Reconfigure FFMPEG encoder");
|
||||
idr = true;
|
||||
encoder_ctx->bit_rate = bitrate;
|
||||
encoder_ctx->rc_max_rate = bitrate;
|
||||
}
|
||||
|
||||
push_frame(idr, target_timestamp, slot);
|
||||
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());
|
||||
|
||||
@@ -38,8 +38,8 @@ public:
|
||||
};
|
||||
|
||||
protected:
|
||||
video_encoder_ffmpeg(uint8_t stream_idx, to_headset::video_stream_description::channels_t channels) :
|
||||
wivrn::video_encoder(stream_idx, channels, true) {}
|
||||
video_encoder_ffmpeg(uint8_t stream_idx, to_headset::video_stream_description::channels_t channels, double bitrate_multiplier) :
|
||||
wivrn::video_encoder(stream_idx, channels, bitrate_multiplier, true) {}
|
||||
|
||||
virtual void push_frame(bool idr, std::chrono::steady_clock::time_point pts, uint8_t slot) = 0;
|
||||
|
||||
|
||||
@@ -135,7 +135,7 @@ video_encoder_va::video_encoder_va(wivrn_vk_bundle & vk,
|
||||
wivrn::encoder_settings & settings,
|
||||
float fps,
|
||||
uint8_t stream_idx) :
|
||||
video_encoder_ffmpeg(stream_idx, settings.channels),
|
||||
video_encoder_ffmpeg(stream_idx, settings.channels, settings.bitrate_multiplier),
|
||||
synchronization2(vk.vk.features.synchronization_2)
|
||||
{
|
||||
auto drm_hw_ctx = make_drm_hw_ctx(vk.physical_device, settings.device);
|
||||
|
||||
@@ -208,9 +208,10 @@ std::pair<std::vector<vk::VideoProfileInfoKHR>, vk::ImageUsageFlags> video_encod
|
||||
|
||||
static const uint64_t idr_throttle = 100;
|
||||
|
||||
video_encoder::video_encoder(uint8_t stream_idx, to_headset::video_stream_description::channels_t channels, bool async_send) :
|
||||
video_encoder::video_encoder(uint8_t stream_idx, to_headset::video_stream_description::channels_t channels, double bitrate_multiplier, bool async_send) :
|
||||
stream_idx(stream_idx),
|
||||
channels(channels),
|
||||
bitrate_multiplier(bitrate_multiplier),
|
||||
last_idr_frame(-idr_throttle),
|
||||
shared_sender(async_send ? sender::get() : nullptr)
|
||||
{}
|
||||
@@ -232,6 +233,11 @@ void video_encoder::reset()
|
||||
sync_needed = true;
|
||||
}
|
||||
|
||||
void video_encoder::set_bitrate(int bitrate_bps)
|
||||
{
|
||||
pending_bitrate = bitrate_bps;
|
||||
}
|
||||
|
||||
std::pair<bool, vk::Semaphore> video_encoder::present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, uint64_t frame_index)
|
||||
{
|
||||
// Wait for encoder to be done
|
||||
|
||||
@@ -33,6 +33,8 @@
|
||||
#include <thread>
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
|
||||
#define BITRATE_UNCHANGED 0
|
||||
|
||||
namespace wivrn
|
||||
{
|
||||
|
||||
@@ -75,6 +77,7 @@ public:
|
||||
const uint8_t stream_idx;
|
||||
const to_headset::video_stream_description::channels_t channels;
|
||||
static const uint8_t num_slots = 2;
|
||||
const double bitrate_multiplier;
|
||||
|
||||
private:
|
||||
std::mutex mutex;
|
||||
@@ -111,7 +114,7 @@ public:
|
||||
static std::pair<std::vector<vk::VideoProfileInfoKHR>, vk::ImageUsageFlags> get_create_image_info(const std::vector<encoder_settings> &);
|
||||
#endif
|
||||
|
||||
video_encoder(uint8_t stream_idx, to_headset::video_stream_description::channels_t channels, bool async_send);
|
||||
video_encoder(uint8_t stream_idx, to_headset::video_stream_description::channels_t channels, double bitrate_multiplier, bool async_send);
|
||||
virtual ~video_encoder();
|
||||
|
||||
// return value: true if image should be transitioned to queue and layout for vulkan video encode
|
||||
@@ -121,12 +124,15 @@ public:
|
||||
|
||||
virtual void on_feedback(const from_headset::feedback &);
|
||||
virtual void reset();
|
||||
virtual void set_bitrate(int bitrate_bps);
|
||||
|
||||
void encode(wivrn_session & cnx,
|
||||
const to_headset::video_stream_data_shard::view_info_t & view_info,
|
||||
uint64_t frame_index);
|
||||
|
||||
protected:
|
||||
std::atomic_int pending_bitrate;
|
||||
|
||||
// called on present to submit command buffers for the image.
|
||||
virtual std::pair<bool, vk::Semaphore> present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, uint8_t slot, uint64_t frame_index) = 0;
|
||||
// called after command buffer passed in present_image was submitted
|
||||
|
||||
@@ -139,7 +139,7 @@ video_encoder_nvenc::video_encoder_nvenc(
|
||||
encoder_settings & settings,
|
||||
float fps,
|
||||
uint8_t stream_idx) :
|
||||
video_encoder(stream_idx, settings.channels, true),
|
||||
video_encoder(stream_idx, settings.channels, settings.bitrate_multiplier, true),
|
||||
vk(vk),
|
||||
fps(fps),
|
||||
bitrate(settings.bitrate)
|
||||
|
||||
@@ -59,7 +59,7 @@ wivrn::video_encoder_vulkan::video_encoder_vulkan(
|
||||
float fps,
|
||||
uint8_t stream_idx,
|
||||
const encoder_settings & settings) :
|
||||
video_encoder(stream_idx, settings.channels, true), vk(vk), encode_caps(patch_capabilities(in_encode_caps)), rect(rect), fps(fps)
|
||||
video_encoder(stream_idx, settings.channels, settings.bitrate_multiplier, true), vk(vk), encode_caps(patch_capabilities(in_encode_caps)), rect(rect), fps(fps)
|
||||
{
|
||||
// Initialize Rate control
|
||||
U_LOG_D("Supported rate control modes: %s", vk::to_string(encode_caps.rateControlModes).c_str());
|
||||
@@ -693,6 +693,27 @@ std::pair<bool, vk::Semaphore> wivrn::video_encoder_vulkan::present_image(vk::Im
|
||||
session_initialized = true;
|
||||
}
|
||||
|
||||
if (auto bitrate = pending_bitrate.exchange(BITRATE_UNCHANGED); bitrate and rate_control)
|
||||
{
|
||||
rate_control_layer.averageBitrate = std::min<uint64_t>(bitrate, encode_caps.maxBitrate);
|
||||
switch (rate_control->rateControlMode)
|
||||
{
|
||||
case vk::VideoEncodeRateControlModeFlagBitsKHR::eCbr:
|
||||
rate_control_layer.maxBitrate = rate_control_layer.averageBitrate;
|
||||
break;
|
||||
case vk::VideoEncodeRateControlModeFlagBitsKHR::eVbr:
|
||||
rate_control_layer.maxBitrate = std::min(2 * rate_control_layer.averageBitrate, encode_caps.maxBitrate);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
video_cmd_buf.controlVideoCodingKHR(
|
||||
vk::VideoCodingControlInfoKHR{
|
||||
.pNext = &rate_control.value(),
|
||||
.flags = vk::VideoCodingControlFlagBitsKHR::eEncodeRateControl,
|
||||
});
|
||||
}
|
||||
|
||||
vk::VideoEncodeInfoKHR encode_info{
|
||||
.pNext = encode_info_next(frame_num, slot_index, ref_slot ? std::make_optional(ref_slot->info.slotIndex) : std::nullopt),
|
||||
.dstBuffer = slot_item.output_buffer,
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
#include "video_encoder_x264.h"
|
||||
|
||||
#include "encoder/video_encoder.h"
|
||||
#include "encoder_settings.h"
|
||||
#include "util/u_logging.h"
|
||||
#include "utils/wivrn_vk_bundle.h"
|
||||
@@ -90,7 +91,7 @@ video_encoder_x264::video_encoder_x264(
|
||||
encoder_settings & settings,
|
||||
float fps,
|
||||
uint8_t stream_idx) :
|
||||
video_encoder(stream_idx, settings.channels, false)
|
||||
video_encoder(stream_idx, settings.channels, settings.bitrate_multiplier, false)
|
||||
{
|
||||
if (settings.codec != h264)
|
||||
{
|
||||
@@ -230,6 +231,13 @@ std::pair<bool, vk::Semaphore> video_encoder_x264::present_image(vk::Image y_cbc
|
||||
|
||||
std::optional<video_encoder::data> video_encoder_x264::encode(bool idr, std::chrono::steady_clock::time_point pts, uint8_t slot)
|
||||
{
|
||||
if (auto bitrate = pending_bitrate.exchange(BITRATE_UNCHANGED))
|
||||
{
|
||||
U_LOG_E("Reconfigure X264 encoder");
|
||||
idr = true;
|
||||
param.rc.i_bitrate = bitrate / 1000;
|
||||
x264_encoder_reconfig(enc, ¶m);
|
||||
}
|
||||
int num_nal;
|
||||
x264_nal_t * nal;
|
||||
auto & pic = in[slot].pic;
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include <CLI/CLI.hpp>
|
||||
#include <avahi-glib/glib-watch.h>
|
||||
#include <chrono> // IWYU pragma: keep
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
@@ -674,6 +675,22 @@ gboolean on_handle_rename_key(WivrnServer * skeleton, GDBusMethodInvocation * in
|
||||
return G_SOURCE_CONTINUE;
|
||||
}
|
||||
|
||||
gboolean on_handle_set_bitrate(WivrnServer * skeleton, GDBusMethodInvocation * invocation, gpointer user_data)
|
||||
{
|
||||
GVariant * args = g_dbus_method_invocation_get_parameters(invocation);
|
||||
|
||||
int32_t bitrate_bps;
|
||||
g_variant_get_child(args, 0, "i", &bitrate_bps);
|
||||
|
||||
if (bitrate_bps > 0)
|
||||
{
|
||||
wivrn_ipc_socket_main_loop->send(to_monado::set_bitrate{bitrate_bps});
|
||||
}
|
||||
|
||||
g_dbus_method_invocation_return_value(invocation, nullptr);
|
||||
return G_SOURCE_CONTINUE;
|
||||
}
|
||||
|
||||
gboolean on_handle_enable_pairing(WivrnServer * skeleton, GDBusMethodInvocation * invocation, gpointer user_data)
|
||||
{
|
||||
set_encryption_state(wivrn_connection::encryption_state::pairing);
|
||||
@@ -812,6 +829,11 @@ void on_name_acquired(GDBusConnection * connection, const gchar * name, gpointer
|
||||
G_CALLBACK(on_handle_rename_key),
|
||||
NULL);
|
||||
|
||||
g_signal_connect(dbus_server,
|
||||
"handle-set-bitrate",
|
||||
G_CALLBACK(on_handle_set_bitrate),
|
||||
NULL);
|
||||
|
||||
if (enc_state != wivrn_connection::encryption_state::disabled)
|
||||
{
|
||||
g_signal_connect(dbus_server,
|
||||
|
||||
+6
-1
@@ -50,7 +50,12 @@ namespace to_monado
|
||||
struct disconnect
|
||||
{};
|
||||
|
||||
using packets = std::variant<disconnect>;
|
||||
struct set_bitrate
|
||||
{
|
||||
int32_t bitrate_bps;
|
||||
};
|
||||
|
||||
using packets = std::variant<disconnect, set_bitrate>;
|
||||
} // namespace to_monado
|
||||
|
||||
extern std::optional<wivrn::typed_socket<wivrn::UnixDatagram, to_monado::packets, from_monado::packets>> wivrn_ipc_socket_monado;
|
||||
|
||||
@@ -222,6 +222,47 @@ void disconnect()
|
||||
call_method(get_user_bus(), "Disconnect", "");
|
||||
}
|
||||
|
||||
void set_bitrate(std::string bitrate_str)
|
||||
{
|
||||
auto suffix = bitrate_str.back();
|
||||
|
||||
int32_t bitrate = -1;
|
||||
size_t str_len = bitrate_str.size();
|
||||
|
||||
switch (std::tolower(suffix))
|
||||
{
|
||||
case 'm':
|
||||
bitrate = 1'000'000;
|
||||
str_len = bitrate_str.size() - 1;
|
||||
break;
|
||||
case 'k':
|
||||
bitrate = 1'000;
|
||||
str_len = bitrate_str.size() - 1;
|
||||
break;
|
||||
default:
|
||||
if (std::isdigit(suffix))
|
||||
bitrate = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
bitrate = bitrate * std::stoi(bitrate_str.substr(0, str_len));
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
bitrate = -1;
|
||||
}
|
||||
|
||||
if (bitrate < 0)
|
||||
{
|
||||
std::cerr << "Invalid bitrate value. Valid examples: 50M, 50000K, 50000000" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
call_method(get_user_bus(), "SetBitrate", "i", bitrate);
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
CLI::App app;
|
||||
@@ -269,6 +310,14 @@ int main(int argc, char ** argv)
|
||||
app.add_subcommand("disconnect", "Disconnect headset")
|
||||
->callback(disconnect);
|
||||
|
||||
std::string bitrate_str;
|
||||
auto bitrate_command = app.add_subcommand("set-bitrate", "Set encoding bitrate");
|
||||
bitrate_command->add_option("BITRATE", bitrate_str, "Desired total bitrate, e.g. 100M")->required();
|
||||
|
||||
bitrate_command->callback([&]() {
|
||||
return set_bitrate(bitrate_str);
|
||||
});
|
||||
|
||||
try
|
||||
{
|
||||
CLI11_PARSE(app, argc, argv);
|
||||
|
||||
Reference in New Issue
Block a user