Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

Add Linux hardware video codecs

AS8148b646ea727d61
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index b0de011..66c40eb 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -169,7 +169,7 @@ jobs:
sudo apt-get update
sudo apt-get install -y --no-install-recommends \
libgl1-mesa-dev libxkbcommon-dev libvulkan-dev libx11-dev \
- ffmpeg libopenh264-dev
+ ffmpeg libavcodec-dev libavutil-dev libopenh264-dev
- name: Install Qt
uses: jurplel/install-qt-action@v4
with:
@@ -183,6 +183,12 @@ jobs:
run: cargo clippy --locked -p nectarlink-desktop -p nectarlink-vcam -p s1-qt-rust --all-targets -- -D warnings
- name: Tests
run: cargo test --locked -p nectarlink-desktop -p nectarlink-vcam
+ - name: Video adapter fallback
+ run: bash desktop/app/tests/linux_video_dispatch_test.sh
+ - name: Check without the optional FFmpeg backend
+ env:
+ LIBAVCODEC_NO_PKG_CONFIG: "1"
+ run: cargo check --locked -p nectarlink-desktop --tests
- name: Release build
run: cargo build --locked --release -p nectarlink-desktop -p nectarlink-vcam
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index d422127..44a6749 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -111,7 +111,9 @@ jobs:
sudo apt-get install -y --no-install-recommends \
build-essential pkg-config libgl1-mesa-dev libx11-dev libxcb1-dev \
libxkbcommon-dev libxkbcommon-x11-dev libwayland-dev libfontconfig1-dev \
- libfreetype-dev libdbus-1-dev libxtst-dev file desktop-file-utils appstream rpm shellcheck
+ libfreetype-dev libdbus-1-dev libxtst-dev libavcodec-dev libavutil-dev \
+ file desktop-file-utils appstream rpm shellcheck
+ pkg-config --exists libavcodec libavutil
- name: Install Qt
uses: jurplel/install-qt-action@v4
with:
@@ -141,16 +143,38 @@ jobs:
appstreamcli validate --no-net desktop/packaging/linux/org.nectarlink.desktop.metainfo.xml
- name: Check the install script
run: shellcheck -s sh desktop/packaging/linux/install.sh
+ - name: Cache FFmpeg source archives
+ uses: actions/cache@v4
+ with:
+ path: ${{ runner.temp }}/ffmpeg-sources
+ key: ffmpeg-sources-${{ hashFiles('desktop/packaging/linux/build-ffmpeg-backends.sh') }}
- name: Build and package (.deb, .rpm, AppImage, tarball)
env:
APPIMAGE_EXTRACT_AND_RUN: "1"
+ FFMPEG_SOURCE_CACHE: ${{ runner.temp }}/ffmpeg-sources
run: |
cargo build --release -p nectarlink-desktop
+ bash ./desktop/packaging/linux/build-ffmpeg-backends.sh
./desktop/packaging/linux/build-deb.sh
./desktop/packaging/linux/build-rpm.sh
./desktop/packaging/linux/build-appimage.sh
./desktop/packaging/linux/build-tarball.sh
cp desktop/packaging/linux/install.sh dist/install.sh
+ - name: Check video adapters in each package
+ env:
+ APPIMAGE_EXTRACT_AND_RUN: "1"
+ run: |
+ dpkg-deb -c dist/*.deb > /tmp/nectarlink-deb-files
+ rpm -qlp dist/*.rpm > /tmp/nectarlink-rpm-files
+ tar -tzf dist/*.tar.gz > /tmp/nectarlink-tar-files
+ (cd /tmp && "$GITHUB_WORKSPACE"/dist/*.AppImage --appimage-extract 'usr/bin/libnectarlink-video-ffmpeg*.so')
+ for major in 7 8 9; do
+ adapter="libnectarlink-video-ffmpeg${major}.so"
+ grep -F "$adapter" /tmp/nectarlink-deb-files
+ grep -F "$adapter" /tmp/nectarlink-rpm-files
+ grep -F "$adapter" /tmp/nectarlink-tar-files
+ test -f "/tmp/squashfs-root/usr/bin/$adapter"
+ done
- name: Check that each package starts
# A package that doesn't start must not reach a release. The .deb goes
# into a clean Ubuntu 24.04, the oldest system the build supports.
diff --git a/Cargo.lock b/Cargo.lock
index 83bd112..114aebd 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2482,6 +2482,7 @@ version = "0.1.4"
dependencies = [
"anyhow",
"blake3",
+ "cc",
"cxx",
"cxx-qt",
"cxx-qt-build",
@@ -2494,6 +2495,7 @@ dependencies = [
"material-colors",
"nectarlink-core",
"nectarlink-vcam",
+ "pkg-config",
"qrcode",
"serde",
"serde_json",
diff --git a/README.md b/README.md
index 17b2108..43ffe5b 100644
--- a/README.md
+++ b/README.md
@@ -268,6 +268,15 @@ docs/ Guide, technical docs, protocol, design system (CC B
| macOS app | macOS 14+, Xcode Command Line Tools, [Qt 6.12](https://www.qt.io/download-open-source) (or Homebrew Qt 6) |
| Android app | Android SDK (platform 37, NDK 30.0.16248370), JDK 17 or newer, `cargo-ndk` (see [android/README.md](android/README.md)) |
+For Linux hardware video encoding and decoding, install `libavcodec-dev` and
+`libavutil-dev` before building. The backend loads the matching FFmpeg runtime
+libraries when needed and tries available GPU drivers. Release packages support
+FFmpeg 6 through 9: the Ubuntu 24.04 release build includes an FFmpeg 6 backend,
+and separate adapters support versions 7, 8 and 9. Source builds use the
+matching runtime for their headers. Without matching libraries or a working
+hardware backend, the existing software H.264 path remains available. HEVC
+encoding requires hardware; if unavailable, screen sharing falls back to H.264.
+
```sh
cargo test -p nectarlink-protocol -p nectarlink-core -p nectarlink-ffi # core and tests
cargo run -p nectarlink-cli -- --help # command-line client
diff --git a/desktop/app/Cargo.toml b/desktop/app/Cargo.toml
index 50937db..ec737ef 100644
--- a/desktop/app/Cargo.toml
+++ b/desktop/app/Cargo.toml
@@ -121,6 +121,8 @@ libc = "0.2"
zbus.workspace = true
[build-dependencies]
+cc = "1"
+pkg-config = "0.3"
cxx-qt-build = { workspace = true, features = ["link_qt_object_files"] }
embed-resource.workspace = true
diff --git a/desktop/app/build.rs b/desktop/app/build.rs
index b5757ca..99612f8 100644
--- a/desktop/app/build.rs
+++ b/desktop/app/build.rs
@@ -352,6 +352,10 @@ fn compile_translations() {
}
fn main() {
+ println!("cargo::rustc-check-cfg=cfg(linux_ffmpeg_video)");
+ if std::env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("linux") {
+ build_linux_video_codec();
+ }
println!("cargo:rerun-if-changed=src/mark.rs");
for qml in QML.iter().chain(QML_SINGLETONS.iter()) {
println!("cargo:rerun-if-changed={qml}");
@@ -428,3 +432,26 @@ fn main() {
println!("cargo:rerun-if-changed=cpp/video_view.h");
println!("cargo:rerun-if-changed=cpp/video_view.cpp");
}
+
+fn build_linux_video_codec() {
+ println!("cargo::rerun-if-changed=cpp/linux_video_codec.h");
+ println!("cargo::rerun-if-changed=cpp/linux_video_codec.cpp");
+ println!("cargo::rerun-if-changed=cpp/linux_video_builtin.h");
+ println!("cargo::rerun-if-changed=cpp/linux_video_dispatch.cpp");
+ let libraries =
+ ["libavcodec", "libavutil"].map(|name| pkg_config::Config::new().cargo_metadata(false).probe(name));
+ let [Ok(codec), Ok(util)] = libraries else { return };
+ let mut build = cc::Build::new();
+ build.cpp(true).std("c++17").define("NECTARLINK_BUILTIN_VIDEO", None).file("cpp/linux_video_codec.cpp");
+ for path in codec.include_paths.iter().chain(&util.include_paths) {
+ build.include(path);
+ }
+ cc::Build::new()
+ .cpp(true)
+ .std("c++17")
+ .file("cpp/linux_video_dispatch.cpp")
+ .compile("nectarlink_linux_video_dispatch");
+ build.compile("nectarlink_linux_video_codec");
+ println!("cargo::rustc-link-lib=dl");
+ println!("cargo::rustc-cfg=linux_ffmpeg_video");
+}
diff --git a/desktop/app/cpp/linux_video_builtin.h b/desktop/app/cpp/linux_video_builtin.h
new file mode 100644
index 0000000..67ecf52
--- /dev/null
+++ b/desktop/app/cpp/linux_video_builtin.h
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+#pragma once
+
+#define nectarlink_video_decoder_create builtin_video_decoder_create
+#define nectarlink_video_decoder_destroy builtin_video_decoder_destroy
+#define nectarlink_video_decoder_next_device builtin_video_decoder_next_device
+#define nectarlink_video_decoder_backend builtin_video_decoder_backend
+#define nectarlink_video_decoder_send builtin_video_decoder_send
+#define nectarlink_video_decoder_receive builtin_video_decoder_receive
+#define nectarlink_video_decoder_flush builtin_video_decoder_flush
+#define nectarlink_video_encoder_create builtin_video_encoder_create
+#define nectarlink_video_encoder_destroy builtin_video_encoder_destroy
+#define nectarlink_video_encoder_next_device builtin_video_encoder_next_device
+#define nectarlink_video_encoder_backend builtin_video_encoder_backend
+#define nectarlink_video_encoder_send builtin_video_encoder_send
+#define nectarlink_video_encoder_receive builtin_video_encoder_receive
+#define nectarlink_video_encoder_request_keyframe builtin_video_encoder_request_keyframe
+#define nectarlink_video_encoder_set_bitrate builtin_video_encoder_set_bitrate
diff --git a/desktop/app/cpp/linux_video_codec.cpp b/desktop/app/cpp/linux_video_codec.cpp
new file mode 100644
index 0000000..12bf4ce
--- /dev/null
+++ b/desktop/app/cpp/linux_video_codec.cpp
@@ -0,0 +1,744 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+#ifdef NECTARLINK_BUILTIN_VIDEO
+#include "linux_video_builtin.h"
+#endif
+#include "linux_video_codec.h"
+
+extern "C" {
+#include <libavcodec/avcodec.h>
+#include <libavutil/dict.h>
+#include <libavutil/hwcontext.h>
+#include <libavutil/opt.h>
+}
+
+#include <algorithm>
+#include <array>
+#include <cstdio>
+#include <cstring>
+#include <dlfcn.h>
+#include <filesystem>
+#include <limits>
+#include <memory>
+#include <stdexcept>
+#include <string>
+#include <system_error>
+#include <utility>
+#include <vector>
+
+#define STRINGIFY_IMPL(value) #value
+#define STRINGIFY(value) STRINGIFY_IMPL(value)
+
+namespace {
+
+struct CloseLibrary
+{
+ void operator()(void* handle) const { dlclose(handle); }
+};
+using Library = std::unique_ptr<void, CloseLibrary>;
+
+template<typename T>
+T symbol(void* library, const char* name)
+{
+ auto function = reinterpret_cast<T>(dlsym(library, name));
+ if (!function) throw std::runtime_error(std::string("Missing symbol: ") + name);
+ return function;
+}
+
+// Compile against the installed headers and load only their matching ABI at runtime.
+struct Api
+{
+ Library codec{dlopen("libavcodec.so." STRINGIFY(LIBAVCODEC_VERSION_MAJOR), RTLD_NOW | RTLD_LOCAL)};
+ Library util{dlopen("libavutil.so." STRINGIFY(LIBAVUTIL_VERSION_MAJOR), RTLD_NOW | RTLD_LOCAL)};
+ decltype(&avcodec_find_encoder_by_name) find_encoder;
+ decltype(&avcodec_find_decoder_by_name) find_decoder;
+ decltype(&avcodec_get_hw_config) hw_config;
+ decltype(&avcodec_alloc_context3) alloc_context;
+ decltype(&avcodec_free_context) free_context;
+ decltype(&avcodec_open2) open;
+ decltype(&avcodec_send_frame) send;
+ decltype(&avcodec_receive_packet) receive;
+ decltype(&avcodec_send_packet) send_packet;
+ decltype(&avcodec_receive_frame) receive_frame;
+ decltype(&avcodec_flush_buffers) flush;
+ decltype(&av_packet_alloc) packet_alloc;
+ decltype(&av_packet_free) packet_free;
+ decltype(&av_packet_unref) packet_unref;
+ decltype(&av_new_packet) new_packet;
+ decltype(&av_frame_alloc) frame_alloc;
+ decltype(&av_frame_free) frame_free;
+ decltype(&av_frame_unref) frame_unref;
+ decltype(&av_frame_get_buffer) frame_buffer;
+ decltype(&av_frame_make_writable) frame_writable;
+ decltype(&av_frame_copy_props) copy_properties;
+ decltype(&av_frame_apply_cropping) crop;
+ decltype(&av_hwdevice_ctx_create) device_create;
+ decltype(&av_hwdevice_get_type_name) device_name;
+ decltype(&av_hwframe_ctx_alloc) frames_alloc;
+ decltype(&av_hwframe_ctx_init) frames_init;
+ decltype(&av_hwframe_get_buffer) hardware_buffer;
+ decltype(&av_hwframe_transfer_data) transfer;
+ decltype(&av_buffer_unref) buffer_unref;
+ decltype(&av_buffer_ref) buffer_ref;
+ decltype(&av_opt_set) option;
+ decltype(&av_opt_find) find_option;
+ decltype(&av_opt_set_int) integer_option;
+ decltype(&av_strerror) error_string;
+ decltype(&av_dict_set) dict_set;
+ decltype(&av_dict_free) dict_free;
+
+ Api()
+ {
+ if (!codec || !util) throw std::runtime_error("Matching FFmpeg runtime libraries are unavailable");
+#define LOAD_CODEC(member, function) member = symbol<decltype(member)>(codec.get(), #function)
+#define LOAD_UTIL(member, function) member = symbol<decltype(member)>(util.get(), #function)
+ LOAD_CODEC(find_encoder, avcodec_find_encoder_by_name);
+ LOAD_CODEC(find_decoder, avcodec_find_decoder_by_name);
+ LOAD_CODEC(hw_config, avcodec_get_hw_config);
+ LOAD_CODEC(alloc_context, avcodec_alloc_context3);
+ LOAD_CODEC(free_context, avcodec_free_context);
+ LOAD_CODEC(open, avcodec_open2);
+ LOAD_CODEC(send, avcodec_send_frame);
+ LOAD_CODEC(receive, avcodec_receive_packet);
+ LOAD_CODEC(send_packet, avcodec_send_packet);
+ LOAD_CODEC(receive_frame, avcodec_receive_frame);
+ LOAD_CODEC(flush, avcodec_flush_buffers);
+ LOAD_CODEC(packet_alloc, av_packet_alloc);
+ LOAD_CODEC(packet_free, av_packet_free);
+ LOAD_CODEC(packet_unref, av_packet_unref);
+ LOAD_CODEC(new_packet, av_new_packet);
+ LOAD_UTIL(frame_alloc, av_frame_alloc);
+ LOAD_UTIL(frame_free, av_frame_free);
+ LOAD_UTIL(frame_unref, av_frame_unref);
+ LOAD_UTIL(frame_buffer, av_frame_get_buffer);
+ LOAD_UTIL(frame_writable, av_frame_make_writable);
+ LOAD_UTIL(copy_properties, av_frame_copy_props);
+ LOAD_UTIL(crop, av_frame_apply_cropping);
+ LOAD_UTIL(device_create, av_hwdevice_ctx_create);
+ LOAD_UTIL(device_name, av_hwdevice_get_type_name);
+ LOAD_UTIL(frames_alloc, av_hwframe_ctx_alloc);
+ LOAD_UTIL(frames_init, av_hwframe_ctx_init);
+ LOAD_UTIL(hardware_buffer, av_hwframe_get_buffer);
+ LOAD_UTIL(transfer, av_hwframe_transfer_data);
+ LOAD_UTIL(buffer_unref, av_buffer_unref);
+ LOAD_UTIL(buffer_ref, av_buffer_ref);
+ LOAD_UTIL(option, av_opt_set);
+ LOAD_UTIL(find_option, av_opt_find);
+ LOAD_UTIL(integer_option, av_opt_set_int);
+ LOAD_UTIL(error_string, av_strerror);
+ LOAD_UTIL(dict_set, av_dict_set);
+ LOAD_UTIL(dict_free, av_dict_free);
+#undef LOAD_CODEC
+#undef LOAD_UTIL
+ }
+
+ void check(int result, const char* operation) const
+ {
+ if (result >= 0) return;
+ char message[AV_ERROR_MAX_STRING_SIZE]{};
+ error_string(result, message, sizeof(message));
+ throw std::runtime_error(std::string(operation) + ": " + message);
+ }
+
+ void set_optional_option(void* object, const char* name, const char* value) const
+ {
+ if (find_option(object, name, nullptr, 0, 0))
+ check(option(object, name, value, 0), name);
+ }
+};
+
+const Api& api()
+{
+ static const Api instance;
+ return instance;
+}
+
+std::vector<std::string> video_devices(AVHWDeviceType type)
+{
+ std::vector<std::string> devices;
+ if (type == AV_HWDEVICE_TYPE_CUDA) {
+ Library driver{dlopen("libcuda.so.1", RTLD_NOW | RTLD_LOCAL)};
+ if (driver) {
+ const auto initialize = symbol<int (*)(unsigned int)>(driver.get(), "cuInit");
+ const auto count_devices = symbol<int (*)(int*)>(driver.get(), "cuDeviceGetCount");
+ int count = 0;
+ if (initialize(0) == 0 && count_devices(&count) == 0)
+ for (int i = 0; i < count; ++i) devices.push_back(std::to_string(i));
+ }
+ } else if (type == AV_HWDEVICE_TYPE_VAAPI || type == AV_HWDEVICE_TYPE_QSV) {
+ std::error_code error;
+ std::filesystem::directory_iterator entries("/dev/dri", error), end;
+ for (; !error && entries != end; entries.increment(error)) {
+ const auto name = entries->path().filename().string();
+ if (name.rfind("renderD", 0) == 0 && name.size() > 7
+ && name.find_first_not_of("0123456789", 7) == std::string::npos)
+ devices.push_back(entries->path().string());
+ }
+ std::sort(devices.begin(), devices.end());
+ }
+ if (devices.empty()) devices.emplace_back();
+ return devices;
+}
+
+struct DeviceOptions
+{
+ const Api& functions;
+ AVDictionary* values = nullptr;
+ ~DeviceOptions() { functions.dict_free(&values); }
+ void set(const char* key, const char* value) { functions.check(functions.dict_set(&values, key, value, 0), key); }
+};
+
+void create_device(const Api& functions, AVHWDeviceType type, const std::string& id, AVBufferRef** device)
+{
+ DeviceOptions options{functions};
+ const char* selected = id.empty() ? nullptr : id.c_str();
+ if (type == AV_HWDEVICE_TYPE_QSV) {
+ options.set("child_device_type", "vaapi");
+ if (selected) options.set("child_device", selected);
+ selected = "hw_any";
+ }
+ functions.check(functions.device_create(device, type, selected, options.values, 0), "Create hardware device");
+}
+
+struct Encoder
+{
+ const Api& functions = api();
+ AVCodecContext* context = nullptr;
+ AVBufferRef* device = nullptr;
+ AVFrame* software = nullptr;
+ AVFrame* hardware = nullptr;
+ AVPacket* packet = nullptr;
+ AVHWDeviceType device_type = AV_HWDEVICE_TYPE_NONE;
+ bool keyframe_requested = true;
+
+ ~Encoder()
+ {
+ functions.packet_free(&packet);
+ functions.frame_free(&hardware);
+ functions.frame_free(&software);
+ functions.free_context(&context);
+ functions.buffer_unref(&device);
+ }
+
+ void configure_options()
+ {
+ functions.set_optional_option(context->priv_data, "async_depth", "1");
+ if (device_type == AV_HWDEVICE_TYPE_CUDA) {
+ functions.set_optional_option(context->priv_data, "forced-idr", "1");
+ functions.set_optional_option(context->priv_data, "preset", "p1");
+ functions.set_optional_option(context->priv_data, "tune", "ull");
+ functions.set_optional_option(context->priv_data, "zerolatency", "1");
+ functions.set_optional_option(context->priv_data, "delay", "0");
+ functions.set_optional_option(context->priv_data, "rc-lookahead", "0");
+ } else if (device_type == AV_HWDEVICE_TYPE_QSV) {
+ functions.set_optional_option(context->priv_data, "preset", "veryfast");
+ functions.set_optional_option(context->priv_data, "forced_idr", "1");
+ functions.set_optional_option(context->priv_data, "look_ahead", "0");
+ functions.set_optional_option(context->priv_data, "look_ahead_depth", "0");
+ }
+ }
+
+ void initialize(const AVCodec* codec, int width, int height, int fps, int bitrate, bool low_power = false,
+ const std::string& device_id = {})
+ {
+ const AVCodecHWConfig* configuration = nullptr;
+ for (int i = 0; (configuration = functions.hw_config(codec, i)); ++i) {
+ if (configuration->device_type == AV_HWDEVICE_TYPE_QSV) {
+ // FFmpeg handles QSV's surface alignment when supplied NV12 and a hardware device.
+ if (configuration->pix_fmt == AV_PIX_FMT_NV12
+ && (configuration->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)) break;
+ } else if (configuration->methods & AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX) {
+ break;
+ }
+ }
+ if (!configuration) throw std::runtime_error("No hardware encoder configuration");
+ device_type = configuration->device_type;
+ create_device(functions, device_type, device_id, &device);
+ context = functions.alloc_context(codec);
+ if (!context) throw std::bad_alloc();
+ context->width = width;
+ context->height = height;
+ context->time_base = AVRational{1, 1000000};
+ context->framerate = AVRational{fps, 1};
+ context->sample_aspect_ratio = AVRational{1, 1};
+ context->pix_fmt = configuration->pix_fmt;
+ context->bit_rate = bitrate;
+ context->rc_max_rate = bitrate;
+ context->rc_buffer_size = bitrate / 2;
+ context->gop_size = fps * 2;
+ context->max_b_frames = 0;
+ context->flags |= AV_CODEC_FLAG_LOW_DELAY;
+#if LIBAVCODEC_VERSION_MAJOR >= 61
+ context->profile = codec->id == AV_CODEC_ID_HEVC ? AV_PROFILE_HEVC_MAIN : AV_PROFILE_H264_CONSTRAINED_BASELINE;
+#else
+ context->profile = codec->id == AV_CODEC_ID_HEVC ? FF_PROFILE_HEVC_MAIN : FF_PROFILE_H264_CONSTRAINED_BASELINE;
+#endif
+ context->color_range = AVCOL_RANGE_MPEG;
+ context->colorspace = AVCOL_SPC_BT709;
+ context->color_primaries = AVCOL_PRI_BT709;
+ context->color_trc = AVCOL_TRC_BT709;
+
+ if (device_type == AV_HWDEVICE_TYPE_QSV) {
+ context->hw_device_ctx = functions.buffer_ref(device);
+ if (!context->hw_device_ctx) throw std::bad_alloc();
+ } else {
+ context->hw_frames_ctx = functions.frames_alloc(device);
+ if (!context->hw_frames_ctx) throw std::bad_alloc();
+ auto* frames = reinterpret_cast<AVHWFramesContext*>(context->hw_frames_ctx->data);
+ frames->format = context->pix_fmt;
+ frames->sw_format = AV_PIX_FMT_NV12;
+ frames->width = width;
+ frames->height = height;
+ // VAAPI uses a fixed pool; reserve space for reference and in-flight frames.
+ frames->initial_pool_size = device_type == AV_HWDEVICE_TYPE_VAAPI ? 20 : 4;
+ functions.check(functions.frames_init(context->hw_frames_ctx), "Create hardware frame pool");
+ }
+
+ configure_options();
+ if (configuration->device_type == AV_HWDEVICE_TYPE_VAAPI)
+ functions.set_optional_option(context->priv_data, "low_power", low_power ? "1" : "0");
+ functions.check(functions.open(context, codec, nullptr), "Open video encoder");
+
+ software = functions.frame_alloc();
+ hardware = functions.frame_alloc();
+ packet = functions.packet_alloc();
+ if (!software || !hardware || !packet) throw std::bad_alloc();
+ software->format = AV_PIX_FMT_NV12;
+ software->width = width;
+ software->height = height;
+ functions.check(functions.frame_buffer(software, 32), "Allocate NV12 frame");
+ }
+
+ void send(const uint8_t* bytes, size_t length, int64_t time_us)
+ {
+ const size_t width = context->width;
+ const size_t height = context->height;
+ if (length != width * height * 3 / 2) throw std::runtime_error("Invalid NV12 frame length");
+ functions.check(functions.frame_writable(software), "Make NV12 frame writable");
+ for (size_t y = 0; y < height; ++y)
+ std::memcpy(software->data[0] + y * software->linesize[0], bytes + y * width, width);
+ for (size_t y = 0; y < height / 2; ++y)
+ std::memcpy(software->data[1] + y * software->linesize[1], bytes + width * height + y * width, width);
+ AVFrame* input = software;
+ if (context->hw_frames_ctx) {
+ functions.frame_unref(hardware);
+ functions.check(functions.hardware_buffer(context->hw_frames_ctx, hardware, 0), "Allocate GPU frame");
+ functions.check(functions.transfer(hardware, software, 0), "Upload NV12 frame");
+ input = hardware;
+ }
+ input->pts = time_us;
+ input->pict_type = keyframe_requested ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_NONE;
+ functions.check(functions.send(context, input), "Encode video frame");
+ keyframe_requested = false;
+ }
+};
+
+void report(char* buffer, size_t capacity, const std::exception& error)
+{
+ if (capacity) std::snprintf(buffer, capacity, "%s", error.what());
+}
+
+struct EncoderCandidate
+{
+ const AVCodec* codec;
+ std::string device;
+ bool low_power;
+};
+
+struct EncoderBackend
+{
+ AVHWDeviceType device_type;
+ const char* h264;
+ const char* hevc;
+};
+
+constexpr std::array<EncoderBackend, 3> encoder_backends = {{
+ {AV_HWDEVICE_TYPE_CUDA, "h264_nvenc", "hevc_nvenc"},
+ {AV_HWDEVICE_TYPE_VAAPI, "h264_vaapi", "hevc_vaapi"},
+ {AV_HWDEVICE_TYPE_QSV, "h264_qsv", "hevc_qsv"},
+}};
+
+struct EncoderSelection
+{
+ const Api& functions = api();
+ int width;
+ int height;
+ int fps;
+ int bitrate;
+ std::vector<EncoderCandidate> candidates;
+ size_t next_candidate = 0;
+ std::unique_ptr<Encoder> current;
+
+ EncoderSelection(bool hevc, int width, int height, int fps, int bitrate)
+ : width(width), height(height), fps(fps), bitrate(bitrate)
+ {
+ for (const auto& backend : encoder_backends) {
+ const AVCodec* codec = functions.find_encoder(hevc ? backend.hevc : backend.h264);
+ if (!codec) continue;
+ for (const auto& device : video_devices(backend.device_type)) {
+ candidates.push_back({codec, device, false});
+ if (backend.device_type == AV_HWDEVICE_TYPE_VAAPI)
+ candidates.push_back({codec, device, true});
+ }
+ }
+ select_next();
+ }
+
+ void select_next()
+ {
+ current.reset();
+ std::string failures;
+ while (next_candidate < candidates.size()) {
+ const auto& candidate = candidates[next_candidate++];
+ try {
+ auto encoder = std::make_unique<Encoder>();
+ encoder->initialize(candidate.codec, width, height, fps, bitrate, candidate.low_power, candidate.device);
+ current = std::move(encoder);
+ return;
+ } catch (const std::exception& failure) {
+ failures += std::string(candidate.codec->name) + "[" + candidate.device + "]: " + failure.what() + "; ";
+ }
+ }
+ throw std::runtime_error(failures.empty() ? "No hardware encoder is available" : failures);
+ }
+
+ Encoder& active()
+ {
+ if (!current) throw std::runtime_error("Hardware encoding devices exhausted");
+ return *current;
+ }
+};
+
+struct Decoder
+{
+ const Api& functions = api();
+ AVCodecContext* context = nullptr;
+ AVBufferRef* device = nullptr;
+ AVFrame* software = nullptr;
+ AVFrame* hardware = nullptr;
+ AVPacket* packet = nullptr;
+ AVPixelFormat format = AV_PIX_FMT_NONE;
+ std::vector<uint8_t> interleaved_uv;
+
+ ~Decoder()
+ {
+ functions.packet_free(&packet);
+ functions.frame_free(&hardware);
+ functions.frame_free(&software);
+ functions.free_context(&context);
+ functions.buffer_unref(&device);
+ }
+
+ static AVPixelFormat choose_format(AVCodecContext* context, const AVPixelFormat* offered)
+ {
+ const auto wanted = static_cast<Decoder*>(context->opaque)->format;
+ for (; *offered != AV_PIX_FMT_NONE; ++offered)
+ if (*offered == wanted) return wanted;
+ return AV_PIX_FMT_NONE;
+ }
+
+ void open(const AVCodec* codec)
+ {
+ context->thread_count = 1;
+ context->flags |= AV_CODEC_FLAG_LOW_DELAY;
+ functions.check(functions.open(context, codec, nullptr), "Open video decoder");
+ software = functions.frame_alloc();
+ hardware = functions.frame_alloc();
+ packet = functions.packet_alloc();
+ if (!software || !hardware || !packet) throw std::bad_alloc();
+ }
+
+ void initialize(const AVCodec* codec, const AVCodecHWConfig& configuration, const std::string& device_id = {})
+ {
+ create_device(functions, configuration.device_type, device_id, &device);
+ context = functions.alloc_context(codec);
+ if (!context) throw std::bad_alloc();
+ context->hw_device_ctx = functions.buffer_ref(device);
+ if (!context->hw_device_ctx) throw std::bad_alloc();
+ format = configuration.pix_fmt;
+ context->opaque = this;
+ context->get_format = choose_format;
+ open(codec);
+ }
+
+ void initialize_software(const AVCodec* codec)
+ {
+ context = functions.alloc_context(codec);
+ if (!context) throw std::bad_alloc();
+ open(codec);
+ }
+};
+
+NectarlinkDecodedNv12 decoded_picture(Decoder& decoder, AVFrame* picture)
+{
+ decoder.functions.check(decoder.functions.crop(picture, 0), "Crop decoded picture");
+ const int width = picture->width & ~1;
+ const int height = picture->height & ~1;
+ if (width <= 0 || height <= 0 || !picture->data[0] || !picture->data[1] || picture->linesize[0] < width)
+ throw std::runtime_error("Unsupported decoded picture layout");
+ const uint8_t* uv = picture->data[1];
+ int uv_stride = picture->linesize[1];
+ if (picture->format == AV_PIX_FMT_YUV420P || picture->format == AV_PIX_FMT_YUVJ420P) {
+ const int half_width = width / 2;
+ if (!picture->data[2] || picture->linesize[1] < half_width || picture->linesize[2] < half_width)
+ throw std::runtime_error("Unsupported decoded picture layout");
+ decoder.interleaved_uv.resize(static_cast<size_t>(width) * (height / 2));
+ for (int row = 0; row < height / 2; ++row) {
+ const auto* u = picture->data[1] + static_cast<size_t>(row) * picture->linesize[1];
+ const auto* v = picture->data[2] + static_cast<size_t>(row) * picture->linesize[2];
+ auto* destination = decoder.interleaved_uv.data() + static_cast<size_t>(row) * width;
+ for (int x = 0; x < half_width; ++x) {
+ destination[2 * x] = u[x];
+ destination[2 * x + 1] = v[x];
+ }
+ }
+ uv = decoder.interleaved_uv.data();
+ uv_stride = width;
+ } else if (picture->format != AV_PIX_FMT_NV12 || uv_stride < width) {
+ throw std::runtime_error("Unsupported decoded picture layout");
+ }
+ return {picture->data[0], uv, width, height,
+ picture->linesize[0], uv_stride,
+ picture->colorspace == AVCOL_SPC_BT470BG || picture->colorspace == AVCOL_SPC_SMPTE170M,
+ picture->color_range == AVCOL_RANGE_JPEG || picture->format == AV_PIX_FMT_YUVJ420P};
+}
+
+struct DecoderCandidate
+{
+ AVCodecHWConfig configuration;
+ std::string device;
+};
+
+struct DecoderSelection
+{
+ const Api& functions = api();
+ const AVCodec* codec;
+ std::vector<DecoderCandidate> candidates;
+ size_t next_candidate = 0;
+ bool software_attempted = false;
+ std::unique_ptr<Decoder> current;
+
+ explicit DecoderSelection(bool hevc) : codec(functions.find_decoder(hevc ? "hevc" : "h264"))
+ {
+ if (!codec) throw std::runtime_error("Video decoder is unavailable");
+ for (int i = 0; const auto* configuration = functions.hw_config(codec, i); ++i) {
+ if (!(configuration->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)) continue;
+ if (configuration->device_type != AV_HWDEVICE_TYPE_CUDA
+ && configuration->device_type != AV_HWDEVICE_TYPE_VAAPI
+ && configuration->device_type != AV_HWDEVICE_TYPE_QSV) continue;
+ for (const auto& device : video_devices(configuration->device_type))
+ candidates.push_back({*configuration, device});
+ }
+ select_next(true);
+ }
+
+ void select_next(bool allow_software = false)
+ {
+ current.reset();
+ std::string failures;
+ while (next_candidate < candidates.size()) {
+ const auto& candidate = candidates[next_candidate++];
+ try {
+ auto decoder = std::make_unique<Decoder>();
+ decoder->initialize(codec, candidate.configuration, candidate.device);
+ current = std::move(decoder);
+ return;
+ } catch (const std::exception& failure) {
+ failures += candidate.device + ": " + failure.what() + "; ";
+ }
+ }
+ if (allow_software && !software_attempted) {
+ software_attempted = true;
+ auto decoder = std::make_unique<Decoder>();
+ decoder->initialize_software(codec);
+ current = std::move(decoder);
+ return;
+ }
+ throw std::runtime_error(failures.empty() ? "No hardware decoder is available" : failures);
+ }
+
+ Decoder& active()
+ {
+ if (!current) throw std::runtime_error("Video decoding options exhausted");
+ return *current;
+ }
+};
+} // namespace
+
+extern "C" void* nectarlink_video_encoder_create(bool hevc, int width, int height, int fps, int bitrate,
+ char* error, size_t capacity)
+{
+ try {
+ if (width <= 0 || height <= 0 || width % 2 || height % 2 || fps <= 0 || bitrate <= 0)
+ throw std::runtime_error("Invalid video encoder dimensions or rate");
+ return new EncoderSelection(hevc, width, height, fps, bitrate);
+ } catch (const std::exception& failure) {
+ report(error, capacity, failure);
+ return nullptr;
+ }
+}
+
+extern "C" void nectarlink_video_encoder_destroy(void* encoder) { delete static_cast<EncoderSelection*>(encoder); }
+extern "C" bool nectarlink_video_encoder_next_device(void* pointer, char* error, size_t capacity)
+{
+ try {
+ static_cast<EncoderSelection*>(pointer)->select_next();
+ return true;
+ } catch (const std::exception& failure) {
+ report(error, capacity, failure);
+ return false;
+ }
+}
+extern "C" const char* nectarlink_video_encoder_backend(const void* encoder)
+{
+ const auto& selected = *static_cast<const EncoderSelection*>(encoder);
+ return selected.current ? selected.current->context->codec->name : "unavailable";
+}
+
+extern "C" int nectarlink_video_encoder_send(void* encoder, const uint8_t* bytes, size_t length, int64_t time_us,
+ char* error, size_t capacity)
+{
+ try {
+ static_cast<EncoderSelection*>(encoder)->active().send(bytes, length, time_us);
+ return 0;
+ } catch (const std::exception& failure) {
+ report(error, capacity, failure);
+ return -1;
+ }
+}
+
+extern "C" int nectarlink_video_encoder_receive(void* pointer, const uint8_t** bytes, size_t* length, bool* keyframe,
+ char* error, size_t capacity)
+{
+ try {
+ auto& encoder = static_cast<EncoderSelection*>(pointer)->active();
+ encoder.functions.packet_unref(encoder.packet);
+ const int result = encoder.functions.receive(encoder.context, encoder.packet);
+ if (result == AVERROR(EAGAIN) || result == AVERROR_EOF) return 0;
+ encoder.functions.check(result, "Receive video packet");
+ *bytes = encoder.packet->data;
+ *length = encoder.packet->size;
+ *keyframe = (encoder.packet->flags & AV_PKT_FLAG_KEY) != 0;
+ return 1;
+ } catch (const std::exception& failure) {
+ report(error, capacity, failure);
+ return -1;
+ }
+}
+
+extern "C" void nectarlink_video_encoder_request_keyframe(void* pointer)
+{
+ auto& selected = *static_cast<EncoderSelection*>(pointer);
+ if (selected.current) selected.current->keyframe_requested = true;
+}
+
+extern "C" bool nectarlink_video_encoder_set_bitrate(void* pointer, int bitrate)
+{
+ auto& selected = *static_cast<EncoderSelection*>(pointer);
+ if (!selected.current) return false;
+ auto& encoder = *selected.current;
+ if (bitrate <= 0) return false;
+ // NVENC and QSV reconfigure these fields on the next submitted frame.
+ if (encoder.device_type == AV_HWDEVICE_TYPE_CUDA || encoder.device_type == AV_HWDEVICE_TYPE_QSV) {
+ encoder.context->bit_rate = bitrate;
+ encoder.context->rc_max_rate = bitrate;
+ encoder.context->rc_buffer_size = bitrate / 2;
+ selected.bitrate = bitrate;
+ return true;
+ }
+ const auto* option = encoder.functions.find_option(encoder.context, "b", nullptr, 0, 0);
+ const bool changed = option && (option->flags & AV_OPT_FLAG_RUNTIME_PARAM)
+ && encoder.functions.integer_option(encoder.context, "b", bitrate, 0) >= 0;
+ if (changed) selected.bitrate = bitrate;
+ return changed;
+}
+
+extern "C" void* nectarlink_video_decoder_create(bool hevc, char* error, size_t capacity)
+{
+ try {
+ return new DecoderSelection(hevc);
+ } catch (const std::exception& failure) {
+ report(error, capacity, failure);
+ return nullptr;
+ }
+}
+
+extern "C" void nectarlink_video_decoder_destroy(void* decoder) { delete static_cast<DecoderSelection*>(decoder); }
+
+extern "C" bool nectarlink_video_decoder_next_device(void* pointer, char* error, size_t capacity)
+{
+ try {
+ static_cast<DecoderSelection*>(pointer)->select_next(true);
+ return true;
+ } catch (const std::exception& failure) {
+ report(error, capacity, failure);
+ return false;
+ }
+}
+
+extern "C" const char* nectarlink_video_decoder_backend(const void* pointer)
+{
+ const auto& selected = *static_cast<const DecoderSelection*>(pointer);
+ if (!selected.current) return "unavailable";
+ const auto& decoder = *selected.current;
+ if (!decoder.device) return "software";
+ const auto* device = reinterpret_cast<const AVHWDeviceContext*>(decoder.device->data);
+ const char* name = decoder.functions.device_name(device->type);
+ return name ? name : "unknown";
+}
+
+extern "C" int nectarlink_video_decoder_send(void* pointer, const uint8_t* bytes, size_t length,
+ char* error, size_t capacity)
+{
+ try {
+ auto& decoder = static_cast<DecoderSelection*>(pointer)->active();
+ if (length > std::numeric_limits<int>::max()) throw std::runtime_error("Video packet is too large");
+ decoder.functions.packet_unref(decoder.packet);
+ decoder.functions.check(decoder.functions.new_packet(decoder.packet, static_cast<int>(length)),
+ "Allocate compressed packet");
+ std::memcpy(decoder.packet->data, bytes, length);
+ decoder.functions.check(decoder.functions.send_packet(decoder.context, decoder.packet),
+ "Submit compressed packet");
+ return 0;
+ } catch (const std::exception& failure) {
+ report(error, capacity, failure);
+ return -1;
+ }
+}
+
+extern "C" int nectarlink_video_decoder_receive(void* pointer, NectarlinkDecodedNv12* frame,
+ char* error, size_t capacity)
+{
+ try {
+ auto& decoder = static_cast<DecoderSelection*>(pointer)->active();
+ decoder.functions.frame_unref(decoder.hardware);
+ const int result = decoder.functions.receive_frame(decoder.context, decoder.hardware);
+ if (result == AVERROR(EAGAIN) || result == AVERROR_EOF) return 0;
+ decoder.functions.check(result, "Receive decoded picture");
+ AVFrame* picture = decoder.hardware;
+ if (decoder.device) {
+ decoder.functions.frame_unref(decoder.software);
+ decoder.software->format = AV_PIX_FMT_NV12;
+ decoder.functions.check(decoder.functions.transfer(decoder.software, decoder.hardware, 0),
+ "Download decoded picture");
+ decoder.functions.check(decoder.functions.copy_properties(decoder.software, decoder.hardware),
+ "Copy picture metadata");
+ picture = decoder.software;
+ }
+ *frame = decoded_picture(decoder, picture);
+ return 1;
+ } catch (const std::exception& failure) {
+ report(error, capacity, failure);
+ return -1;
+ }
+}
+
+extern "C" void nectarlink_video_decoder_flush(void* pointer)
+{
+ auto& selected = *static_cast<DecoderSelection*>(pointer);
+ if (!selected.current) return;
+ auto& decoder = *selected.current;
+ decoder.functions.flush(decoder.context);
+ decoder.functions.frame_unref(decoder.hardware);
+ decoder.functions.frame_unref(decoder.software);
+ decoder.functions.packet_unref(decoder.packet);
+}
diff --git a/desktop/app/cpp/linux_video_codec.h b/desktop/app/cpp/linux_video_codec.h
new file mode 100644
index 0000000..2a4f397
--- /dev/null
+++ b/desktop/app/cpp/linux_video_codec.h
@@ -0,0 +1,45 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+#pragma once
+
+#include <cstddef>
+#include <cstdint>
+
+extern "C" {
+struct NectarlinkDecodedNv12
+{
+ const uint8_t* y;
+ const uint8_t* uv;
+ int width;
+ int height;
+ int stride_y;
+ int stride_uv;
+ bool bt601;
+ bool full_range;
+};
+
+void* nectarlink_video_decoder_create(bool hevc, char* error, size_t capacity);
+void nectarlink_video_decoder_destroy(void* decoder);
+// After a successful switch, resume from a keyframe. False means all candidates failed.
+bool nectarlink_video_decoder_next_device(void* decoder, char* error, size_t capacity);
+const char* nectarlink_video_decoder_backend(const void* decoder);
+int nectarlink_video_decoder_send(void* decoder, const uint8_t* bytes, size_t length,
+ char* error, size_t capacity);
+// The returned planes remain valid until receive, flush, or next_device is called again.
+int nectarlink_video_decoder_receive(void* decoder, NectarlinkDecodedNv12* frame,
+ char* error, size_t capacity);
+void nectarlink_video_decoder_flush(void* decoder);
+
+void* nectarlink_video_encoder_create(bool hevc, int width, int height, int fps, int bitrate,
+ char* error, size_t capacity);
+void nectarlink_video_encoder_destroy(void* encoder);
+bool nectarlink_video_encoder_next_device(void* encoder, char* error, size_t capacity);
+const char* nectarlink_video_encoder_backend(const void* encoder);
+// Drain available output before sending another frame. Returns zero on success, negative on failure.
+int nectarlink_video_encoder_send(void* encoder, const uint8_t* nv12, size_t length, int64_t time_us,
+ char* error, size_t capacity);
+// Packet storage belongs to the encoder and remains valid until the next receive call.
+int nectarlink_video_encoder_receive(void* encoder, const uint8_t** bytes, size_t* length, bool* keyframe,
+ char* error, size_t capacity);
+void nectarlink_video_encoder_request_keyframe(void* encoder);
+bool nectarlink_video_encoder_set_bitrate(void* encoder, int bitrate);
+}
diff --git a/desktop/app/cpp/linux_video_dispatch.cpp b/desktop/app/cpp/linux_video_dispatch.cpp
new file mode 100644
index 0000000..487fbde
--- /dev/null
+++ b/desktop/app/cpp/linux_video_dispatch.cpp
@@ -0,0 +1,214 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+#include "linux_video_builtin.h"
+#include "linux_video_codec.h"
+
+#undef nectarlink_video_decoder_create
+#undef nectarlink_video_decoder_destroy
+#undef nectarlink_video_decoder_next_device
+#undef nectarlink_video_decoder_backend
+#undef nectarlink_video_decoder_send
+#undef nectarlink_video_decoder_receive
+#undef nectarlink_video_decoder_flush
+#undef nectarlink_video_encoder_create
+#undef nectarlink_video_encoder_destroy
+#undef nectarlink_video_encoder_next_device
+#undef nectarlink_video_encoder_backend
+#undef nectarlink_video_encoder_send
+#undef nectarlink_video_encoder_receive
+#undef nectarlink_video_encoder_request_keyframe
+#undef nectarlink_video_encoder_set_bitrate
+
+#include <array>
+#include <cstdio>
+#include <dlfcn.h>
+#include <exception>
+#include <filesystem>
+#include <memory>
+#include <new>
+#include <string>
+#include <system_error>
+
+namespace {
+struct Backend {
+ decltype(&builtin_video_decoder_create) decoder_create;
+ decltype(&builtin_video_decoder_destroy) decoder_destroy;
+ decltype(&builtin_video_decoder_next_device) decoder_next_device;
+ decltype(&builtin_video_decoder_backend) decoder_backend;
+ decltype(&builtin_video_decoder_send) decoder_send;
+ decltype(&builtin_video_decoder_receive) decoder_receive;
+ decltype(&builtin_video_decoder_flush) decoder_flush;
+ decltype(&builtin_video_encoder_create) encoder_create;
+ decltype(&builtin_video_encoder_destroy) encoder_destroy;
+ decltype(&builtin_video_encoder_next_device) encoder_next_device;
+ decltype(&builtin_video_encoder_backend) encoder_backend;
+ decltype(&builtin_video_encoder_send) encoder_send;
+ decltype(&builtin_video_encoder_receive) encoder_receive;
+ decltype(&builtin_video_encoder_request_keyframe) encoder_request_keyframe;
+ decltype(&builtin_video_encoder_set_bitrate) encoder_set_bitrate;
+};
+
+const Backend builtin = {
+ builtin_video_decoder_create, builtin_video_decoder_destroy, builtin_video_decoder_next_device,
+ builtin_video_decoder_backend, builtin_video_decoder_send, builtin_video_decoder_receive,
+ builtin_video_decoder_flush, builtin_video_encoder_create, builtin_video_encoder_destroy,
+ builtin_video_encoder_next_device, builtin_video_encoder_backend, builtin_video_encoder_send,
+ builtin_video_encoder_receive, builtin_video_encoder_request_keyframe, builtin_video_encoder_set_bitrate,
+};
+
+template<typename T> bool load_symbol(void* library, T& target, const char* name)
+{
+ target = reinterpret_cast<T>(dlsym(library, name));
+ return target != nullptr;
+}
+
+struct CloseLibrary {
+ void operator()(void* library) const { dlclose(library); }
+};
+
+std::unique_ptr<Backend> load_backend(const std::filesystem::path& path)
+{
+ std::unique_ptr<void, CloseLibrary> library(dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL));
+ if (!library) return {};
+ auto result = std::make_unique<Backend>();
+#define LOAD(name) load_symbol(library.get(), result->name, "nectarlink_video_" #name)
+ if (!(LOAD(decoder_create) && LOAD(decoder_destroy) && LOAD(decoder_next_device)
+ && LOAD(decoder_backend) && LOAD(decoder_send) && LOAD(decoder_receive) && LOAD(decoder_flush)
+ && LOAD(encoder_create) && LOAD(encoder_destroy) && LOAD(encoder_next_device)
+ && LOAD(encoder_backend) && LOAD(encoder_send) && LOAD(encoder_receive)
+ && LOAD(encoder_request_keyframe) && LOAD(encoder_set_bitrate))) {
+ return {};
+ }
+#undef LOAD
+ // Keep the library loaded for the lifetime of its function pointers.
+ library.release();
+ return result;
+}
+
+const auto& alternates()
+{
+ static const auto backends = [] {
+ std::array<std::unique_ptr<Backend>, 3> result;
+ std::error_code error;
+ const auto executable = std::filesystem::read_symlink("/proc/self/exe", error);
+ if (error) return result;
+ for (size_t i = 0; i < result.size(); ++i) {
+ const auto name = "libnectarlink-video-ffmpeg" + std::to_string(i + 7) + ".so";
+ result[i] = load_backend(executable.parent_path() / name);
+ }
+ return result;
+ }();
+ return backends;
+}
+
+struct Handle {
+ const Backend* backend;
+ void* object;
+};
+
+template<typename Create, typename Destroy>
+void* create(Create create, Destroy destroy, char* error, size_t capacity)
+{
+ auto attempt = [&](const Backend* backend) -> void* {
+ if (!backend) return nullptr;
+ void* object = create(*backend);
+ if (!object) return nullptr;
+ auto* handle = new (std::nothrow) Handle{backend, object};
+ if (handle) return handle;
+ destroy(*backend, object);
+ if (capacity) std::snprintf(error, capacity, "Out of memory");
+ return nullptr;
+ };
+ try {
+ const auto& candidates = alternates();
+ for (auto it = candidates.rbegin(); it != candidates.rend(); ++it) {
+ if (auto* handle = attempt(it->get())) return handle;
+ }
+ return attempt(&builtin);
+ } catch (const std::exception& failure) {
+ if (capacity) std::snprintf(error, capacity, "%s", failure.what());
+ }
+ return nullptr;
+}
+}
+
+extern "C" void* nectarlink_video_decoder_create(bool hevc, char* error, size_t capacity)
+{
+ return create([&](const Backend& b) { return b.decoder_create(hevc, error, capacity); },
+ [](const Backend& b, void* p) { b.decoder_destroy(p); }, error, capacity);
+}
+extern "C" void nectarlink_video_decoder_destroy(void* p)
+{
+ auto* h = static_cast<Handle*>(p);
+ h->backend->decoder_destroy(h->object);
+ delete h;
+}
+extern "C" bool nectarlink_video_decoder_next_device(void* p, char* error, size_t n)
+{
+ auto* h = static_cast<Handle*>(p);
+ return h->backend->decoder_next_device(h->object, error, n);
+}
+extern "C" const char* nectarlink_video_decoder_backend(const void* p)
+{
+ auto* h = static_cast<const Handle*>(p);
+ return h->backend->decoder_backend(h->object);
+}
+extern "C" int nectarlink_video_decoder_send(void* p, const uint8_t* data, size_t len, char* error, size_t n)
+{
+ auto* h = static_cast<Handle*>(p);
+ return h->backend->decoder_send(h->object, data, len, error, n);
+}
+extern "C" int nectarlink_video_decoder_receive(void* p, NectarlinkDecodedNv12* frame, char* error, size_t n)
+{
+ auto* h = static_cast<Handle*>(p);
+ return h->backend->decoder_receive(h->object, frame, error, n);
+}
+extern "C" void nectarlink_video_decoder_flush(void* p)
+{
+ auto* h = static_cast<Handle*>(p);
+ h->backend->decoder_flush(h->object);
+}
+
+extern "C" void* nectarlink_video_encoder_create(bool hevc, int width, int height, int fps, int bitrate,
+ char* error, size_t capacity)
+{
+ return create([&](const Backend& b) { return b.encoder_create(hevc, width, height, fps, bitrate, error, capacity); },
+ [](const Backend& b, void* p) { b.encoder_destroy(p); }, error, capacity);
+}
+extern "C" void nectarlink_video_encoder_destroy(void* p)
+{
+ auto* h = static_cast<Handle*>(p);
+ h->backend->encoder_destroy(h->object);
+ delete h;
+}
+extern "C" bool nectarlink_video_encoder_next_device(void* p, char* error, size_t n)
+{
+ auto* h = static_cast<Handle*>(p);
+ return h->backend->encoder_next_device(h->object, error, n);
+}
+extern "C" const char* nectarlink_video_encoder_backend(const void* p)
+{
+ auto* h = static_cast<const Handle*>(p);
+ return h->backend->encoder_backend(h->object);
+}
+extern "C" int nectarlink_video_encoder_send(void* p, const uint8_t* data, size_t len, int64_t time,
+ char* error, size_t n)
+{
+ auto* h = static_cast<Handle*>(p);
+ return h->backend->encoder_send(h->object, data, len, time, error, n);
+}
+extern "C" int nectarlink_video_encoder_receive(void* p, const uint8_t** data, size_t* len, bool* key,
+ char* error, size_t n)
+{
+ auto* h = static_cast<Handle*>(p);
+ return h->backend->encoder_receive(h->object, data, len, key, error, n);
+}
+extern "C" void nectarlink_video_encoder_request_keyframe(void* p)
+{
+ auto* h = static_cast<Handle*>(p);
+ h->backend->encoder_request_keyframe(h->object);
+}
+extern "C" bool nectarlink_video_encoder_set_bitrate(void* p, int bitrate)
+{
+ auto* h = static_cast<Handle*>(p);
+ return h->backend->encoder_set_bitrate(h->object, bitrate);
+}
diff --git a/desktop/app/src/core_host.rs b/desktop/app/src/core_host.rs
index 81edd2a..8a3cc15 100644
--- a/desktop/app/src/core_host.rs
+++ b/desktop/app/src/core_host.rs
@@ -135,6 +135,9 @@ pub fn compute_capabilities(
}
#[cfg(target_os = "linux")]
{
+ if crate::win::h264::supports_hevc_hardware() {
+ caps.push(nectarlink_core::MIRROR_HEVC.into());
+ }
if crate::win::vcam::dll_path().is_some() || crate::win::vcam::is_registered() {
caps.push(nectarlink_core::WEBCAM_VIRTUAL.into());
}
diff --git a/desktop/app/src/linux/h264.rs b/desktop/app/src/linux/h264.rs
index a56abaa..72173a8 100644
--- a/desktop/app/src/linux/h264.rs
+++ b/desktop/app/src/linux/h264.rs
@@ -182,8 +182,16 @@ fn openh264_api() -> Option<&'static OpenH264Api> {
}
enum Backend {
- AvCodec { ctx: *mut c_void, pkt: *mut c_void, frame: *mut c_void },
- OpenH264 { dec: *mut c_void },
+ #[cfg(linux_ffmpeg_video)]
+ Native(super::video_decoder::Decoder),
+ AvCodec {
+ ctx: *mut c_void,
+ pkt: *mut c_void,
+ frame: *mut c_void,
+ },
+ OpenH264 {
+ dec: *mut c_void,
+ },
}
// SAFETY: Decoder is owned and used exclusively by a single decode worker thread at a time.
@@ -192,6 +200,8 @@ unsafe impl Send for Backend {}
impl std::fmt::Debug for Backend {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
+ #[cfg(linux_ffmpeg_video)]
+ Backend::Native(_) => f.write_str("Backend::Native"),
Backend::AvCodec { .. } => f.write_str("Backend::AvCodec"),
Backend::OpenH264 { .. } => f.write_str("Backend::OpenH264"),
}
@@ -201,6 +211,8 @@ impl std::fmt::Debug for Backend {
impl Drop for Backend {
fn drop(&mut self) {
match self {
+ #[cfg(linux_ffmpeg_video)]
+ Backend::Native(_) => {}
Backend::AvCodec { ctx, pkt, frame } => {
if let Some(api) = avcodec_api() {
unsafe {
@@ -332,10 +344,25 @@ impl Decoder {
}
pub fn new_gpu(codec: &str) -> Result<Decoder> {
+ #[cfg(linux_ffmpeg_video)]
+ match super::video_decoder::Decoder::new(codec) {
+ Ok(decoder) => {
+ return Ok(Self {
+ codec_name: if codec == "h265" || codec == "hevc" { "hevc" } else { "h264" },
+ backend: Backend::Native(decoder),
+ stream_colors: None,
+ });
+ }
+ Err(error) => tracing::debug!(%error, codec, "Linux hardware decoder unavailable"),
+ }
Self::new_for_codec(codec)
}
pub fn on_gpu(&self) -> bool {
+ #[cfg(linux_ffmpeg_video)]
+ if let Backend::Native(decoder) = &self.backend {
+ return decoder.on_gpu();
+ }
false
}
@@ -353,7 +380,26 @@ impl Decoder {
self.stream_colors = Some(colors);
}
let stream_colors = self.stream_colors;
+ #[cfg(linux_ffmpeg_video)]
+ if let Backend::Native(decoder) = &mut self.backend {
+ match decoder.decode(data) {
+ Ok(pictures) => return Ok(pictures),
+ Err(error) => {
+ if decoder.next_device() {
+ return Err(error);
+ }
+ tracing::warn!(%error, codec = self.codec_name, "Video decoder failed; trying another software decoder");
+ if let Ok(mut software) = Self::new_for_codec(self.codec_name) {
+ software.stream_colors = stream_colors;
+ *self = software;
+ }
+ return Err(error);
+ }
+ }
+ }
match &mut self.backend {
+ #[cfg(linux_ffmpeg_video)]
+ Backend::Native(_) => unreachable!("native decoding handled above"),
Backend::OpenH264 { dec } => Self::decode_openh264(*dec, data, stream_colors),
Backend::AvCodec { ctx, pkt, frame } => {
Self::decode_avcodec(*ctx, *pkt, *frame, data, stream_colors)
@@ -459,6 +505,8 @@ impl Decoder {
pub fn flush(&mut self) {
match &mut self.backend {
+ #[cfg(linux_ffmpeg_video)]
+ Backend::Native(decoder) => decoder.flush(),
Backend::AvCodec { ctx, .. } => {
if let Some(api) = avcodec_api()
&& !ctx.is_null()
@@ -486,6 +534,11 @@ impl Decoder {
}
}
+pub(crate) fn supports_hevc_hardware() -> bool {
+ static SUPPORTED: OnceLock<bool> = OnceLock::new();
+ *SUPPORTED.get_or_init(|| Decoder::new_gpu("h265").is_ok_and(|decoder| decoder.on_gpu()))
+}
+
/// Split an Annex-B bitstream (`00 00 01` or `00 00 00 01` start codes) into individual
/// NAL units (each retaining its Annex-B start code prefix).
fn split_annexb_nals(data: &[u8]) -> Vec<&[u8]> {
diff --git a/desktop/app/src/linux/mod.rs b/desktop/app/src/linux/mod.rs
index 5b9e1cb..99f4009 100644
--- a/desktop/app/src/linux/mod.rs
+++ b/desktop/app/src/linux/mod.rs
@@ -33,6 +33,10 @@ pub mod storage;
pub mod toast;
pub mod tray;
pub mod vcam;
+#[cfg(linux_ffmpeg_video)]
+pub(crate) mod video_decoder;
+#[cfg(linux_ffmpeg_video)]
+pub(crate) mod video_encoder;
pub mod vmic;
pub mod wallpaper;
pub mod widget;
diff --git a/desktop/app/src/linux/video_decoder.rs b/desktop/app/src/linux/video_decoder.rs
new file mode 100644
index 0000000..224bd9d
--- /dev/null
+++ b/desktop/app/src/linux/video_decoder.rs
@@ -0,0 +1,202 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+//! FFmpeg video decoding with owned NV12 output.
+
+use std::{
+ ffi::{CStr, c_char, c_void},
+ io::{Error, ErrorKind, Result},
+ ptr::NonNull,
+};
+
+use super::h264::{Colors, Picture};
+
+#[repr(C)]
+#[derive(Default)]
+struct NativePicture {
+ y: *const u8,
+ uv: *const u8,
+ width: i32,
+ height: i32,
+ stride_y: i32,
+ stride_uv: i32,
+ bt601: bool,
+ full_range: bool,
+}
+
+impl NativePicture {
+ /// The planes must remain valid for the validated dimensions and strides until this copy completes.
+ unsafe fn copy_picture(&self) -> Result<Picture> {
+ let width = usize::try_from(self.width).map_err(|_| Error::other("Invalid picture width"))?;
+ let height = usize::try_from(self.height).map_err(|_| Error::other("Invalid picture height"))?;
+ let length = width
+ .checked_mul(height)
+ .and_then(|pixels| pixels.checked_add(pixels / 2))
+ .ok_or_else(|| Error::other("Picture size overflow"))?;
+ let mut nv12 = Vec::with_capacity(length);
+ for (plane, stride, rows) in
+ [(self.y, self.stride_y as usize, height), (self.uv, self.stride_uv as usize, height / 2)]
+ {
+ for row in 0..rows {
+ // SAFETY: the caller guarantees readable planes with these dimensions and strides.
+ let source = unsafe { std::slice::from_raw_parts(plane.add(row * stride), width) };
+ nv12.extend_from_slice(source);
+ }
+ }
+ Ok(Picture {
+ width: self.width as u32,
+ height: self.height as u32,
+ stride: width,
+ plane_rows: height,
+ nv12,
+ colors: Colors { bt601: self.bt601, full_range: self.full_range },
+ })
+ }
+}
+
+unsafe extern "C" {
+ fn nectarlink_video_decoder_create(hevc: bool, error: *mut c_char, capacity: usize) -> *mut c_void;
+ fn nectarlink_video_decoder_destroy(decoder: *mut c_void);
+ fn nectarlink_video_decoder_next_device(
+ decoder: *mut c_void,
+ error: *mut c_char,
+ capacity: usize,
+ ) -> bool;
+ fn nectarlink_video_decoder_backend(decoder: *const c_void) -> *const c_char;
+ fn nectarlink_video_decoder_send(
+ decoder: *mut c_void,
+ bytes: *const u8,
+ length: usize,
+ error: *mut c_char,
+ capacity: usize,
+ ) -> i32;
+ fn nectarlink_video_decoder_receive(
+ decoder: *mut c_void,
+ frame: *mut NativePicture,
+ error: *mut c_char,
+ capacity: usize,
+ ) -> i32;
+ fn nectarlink_video_decoder_flush(decoder: *mut c_void);
+}
+
+pub(crate) struct Decoder(NonNull<c_void>);
+
+impl Decoder {
+ pub(crate) fn on_gpu(&self) -> bool {
+ // SAFETY: the decoder owns a NUL-terminated backend name until it is dropped.
+ let backend = unsafe { CStr::from_ptr(nectarlink_video_decoder_backend(self.0.as_ptr())) };
+ backend != c"software" && backend != c"unavailable"
+ }
+
+ pub(crate) fn new(codec: &str) -> Result<Self> {
+ let hevc = match codec {
+ "h264" => false,
+ "h265" | "hevc" => true,
+ _ => return Err(Error::new(ErrorKind::Unsupported, "Hardware codec is unsupported")),
+ };
+ let mut error = [0 as c_char; 512];
+ // SAFETY: the factory writes only within the supplied error buffer.
+ let pointer = unsafe { nectarlink_video_decoder_create(hevc, error.as_mut_ptr(), error.len()) };
+ let pointer = NonNull::new(pointer).ok_or_else(|| native_error(&error))?;
+ // SAFETY: the live decoder owns a NUL-terminated hardware device name.
+ let backend = unsafe { CStr::from_ptr(nectarlink_video_decoder_backend(pointer.as_ptr())) };
+ tracing::info!(codec, backend = %backend.to_string_lossy(), "Linux video decoder initialized");
+ Ok(Self(pointer))
+ }
+
+ pub(crate) fn decode(&mut self, bytes: &[u8]) -> Result<Vec<Picture>> {
+ let mut pictures = Vec::new();
+ self.receive(&mut pictures)?;
+ let mut error = [0 as c_char; 512];
+ // SAFETY: the compressed data is copied by the native decoder before this call returns.
+ let result = unsafe {
+ nectarlink_video_decoder_send(
+ self.0.as_ptr(),
+ bytes.as_ptr(),
+ bytes.len(),
+ error.as_mut_ptr(),
+ error.len(),
+ )
+ };
+ if result < 0 {
+ return Err(native_error(&error));
+ }
+ self.receive(&mut pictures)?;
+ Ok(pictures)
+ }
+
+ pub(crate) fn next_device(&mut self) -> bool {
+ let mut error = [0 as c_char; 512];
+ // SAFETY: this worker owns the decoder; no native picture pointers escape receive().
+ let switched =
+ unsafe { nectarlink_video_decoder_next_device(self.0.as_ptr(), error.as_mut_ptr(), error.len()) };
+ if switched {
+ // SAFETY: a successful switch leaves a live decoder with a NUL-terminated backend name.
+ let backend = unsafe { CStr::from_ptr(nectarlink_video_decoder_backend(self.0.as_ptr())) };
+ tracing::info!(backend = %backend.to_string_lossy(), "Video decoder switched");
+ } else {
+ tracing::debug!(error = %native_error(&error), "Video decoding options exhausted");
+ }
+ switched
+ }
+
+ fn receive(&mut self, pictures: &mut Vec<Picture>) -> Result<()> {
+ loop {
+ let mut frame = NativePicture::default();
+ let mut error = [0 as c_char; 512];
+ // SAFETY: the output structures live through the call; native planes remain borrowed until receive is called again.
+ let result = unsafe {
+ nectarlink_video_decoder_receive(self.0.as_ptr(), &mut frame, error.as_mut_ptr(), error.len())
+ };
+ match result {
+ 0 => return Ok(()),
+ 1 => {
+ // SAFETY: a successful receive validates the layout; its planes remain live until the next call.
+ pictures.push(unsafe { frame.copy_picture() }?);
+ }
+ _ => return Err(native_error(&error)),
+ }
+ }
+ }
+
+ pub(crate) fn flush(&mut self) {
+ // SAFETY: this worker owns the decoder and no borrowed planes escape receive().
+ unsafe { nectarlink_video_decoder_flush(self.0.as_ptr()) };
+ }
+}
+
+impl Drop for Decoder {
+ fn drop(&mut self) {
+ // SAFETY: this is the sole owner of the native decoder.
+ unsafe { nectarlink_video_decoder_destroy(self.0.as_ptr()) };
+ }
+}
+
+fn native_error(buffer: &[c_char]) -> Error {
+ // SAFETY: buffers are zero-initialized and native errors are NUL-terminated.
+ Error::other(unsafe { CStr::from_ptr(buffer.as_ptr()) }.to_string_lossy().into_owned())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn copies_visible_nv12_rows_without_the_padding() {
+ let y = [1, 2, 99, 99, 3, 4, 99, 99];
+ let uv = [5, 6, 99, 99];
+ let frame = NativePicture {
+ y: y.as_ptr(),
+ uv: uv.as_ptr(),
+ width: 2,
+ height: 2,
+ stride_y: 4,
+ stride_uv: 4,
+ bt601: false,
+ full_range: true,
+ };
+ // SAFETY: both arrays stay alive and provide the rows described by the frame.
+ let picture = unsafe { frame.copy_picture() }.unwrap();
+ assert_eq!(picture.nv12, [1, 2, 3, 4, 5, 6]);
+ assert_eq!((picture.width, picture.height, picture.stride, picture.plane_rows), (2, 2, 2, 2));
+ assert_eq!(picture.colors, Colors { bt601: false, full_range: true });
+ }
+}
diff --git a/desktop/app/src/linux/video_encoder.rs b/desktop/app/src/linux/video_encoder.rs
new file mode 100644
index 0000000..cb3c71a
--- /dev/null
+++ b/desktop/app/src/linux/video_encoder.rs
@@ -0,0 +1,181 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+//! Hardware video encoding through the optional FFmpeg runtime libraries.
+
+use std::{
+ ffi::{CStr, c_char, c_void},
+ ptr::NonNull,
+ sync::atomic::{AtomicBool, Ordering},
+ time::{Duration, Instant},
+};
+
+unsafe extern "C" {
+ fn nectarlink_video_encoder_create(
+ hevc: bool,
+ width: i32,
+ height: i32,
+ fps: i32,
+ bitrate: i32,
+ error: *mut c_char,
+ capacity: usize,
+ ) -> *mut c_void;
+ fn nectarlink_video_encoder_destroy(encoder: *mut c_void);
+ fn nectarlink_video_encoder_next_device(
+ encoder: *mut c_void,
+ error: *mut c_char,
+ capacity: usize,
+ ) -> bool;
+ fn nectarlink_video_encoder_backend(encoder: *const c_void) -> *const c_char;
+ fn nectarlink_video_encoder_send(
+ encoder: *mut c_void,
+ bytes: *const u8,
+ length: usize,
+ time_us: i64,
+ error: *mut c_char,
+ capacity: usize,
+ ) -> i32;
+ fn nectarlink_video_encoder_receive(
+ encoder: *mut c_void,
+ bytes: *mut *const u8,
+ length: *mut usize,
+ keyframe: *mut bool,
+ error: *mut c_char,
+ capacity: usize,
+ ) -> i32;
+ fn nectarlink_video_encoder_request_keyframe(encoder: *mut c_void);
+ fn nectarlink_video_encoder_set_bitrate(encoder: *mut c_void, bitrate: i32) -> bool;
+}
+
+pub(crate) struct Encoder {
+ native: NonNull<c_void>,
+ last_output: Instant,
+}
+
+impl Encoder {
+ pub(crate) fn new(codec: &str, width: u32, height: u32, fps: u32, bitrate: u32) -> Result<Self, String> {
+ let hevc = match codec {
+ "h264" => false,
+ "h265" => true,
+ _ => return Err(format!("Unsupported hardware codec: {codec}")),
+ };
+ let number = |value: u32| i32::try_from(value).map_err(|_| "Encoder setting is too large".to_owned());
+ let mut error = [0 as c_char; 512];
+ // SAFETY: the factory copies these settings and writes only within the error buffer.
+ let native = unsafe {
+ nectarlink_video_encoder_create(
+ hevc,
+ number(width)?,
+ number(height)?,
+ number(fps)?,
+ number(bitrate)?,
+ error.as_mut_ptr(),
+ error.len(),
+ )
+ };
+ let native = NonNull::new(native).ok_or_else(|| error_message(&error))?;
+ // SAFETY: the live encoder owns a NUL-terminated backend name.
+ let backend = unsafe { CStr::from_ptr(nectarlink_video_encoder_backend(native.as_ptr())) };
+ tracing::info!(codec, backend = %backend.to_string_lossy(), "Linux hardware encoder initialized");
+ Ok(Self { native, last_output: Instant::now() })
+ }
+
+ pub(crate) fn request_keyframe(&self) -> bool {
+ // SAFETY: encoder controls and frame submission run on the capture thread.
+ unsafe { nectarlink_video_encoder_request_keyframe(self.native.as_ptr()) };
+ true
+ }
+
+ pub(crate) fn next_device(&mut self) -> bool {
+ let mut error = [0 as c_char; 512];
+ // SAFETY: this capture thread owns the encoder and its packet storage.
+ let switched = unsafe {
+ nectarlink_video_encoder_next_device(self.native.as_ptr(), error.as_mut_ptr(), error.len())
+ };
+ if switched {
+ self.last_output = Instant::now();
+ } else {
+ tracing::debug!(error = %error_message(&error), "Hardware encoding devices exhausted");
+ }
+ switched
+ }
+
+ pub(crate) fn set_bitrate(&self, bitrate: u32) -> bool {
+ let Ok(bitrate) = i32::try_from(bitrate) else { return false };
+ // SAFETY: the pointer stays valid until this encoder is dropped.
+ unsafe { nectarlink_video_encoder_set_bitrate(self.native.as_ptr(), bitrate) }
+ }
+
+ pub(crate) fn encode(
+ &mut self,
+ nv12: &[u8],
+ hns: i64,
+ stop: &AtomicBool,
+ ) -> Result<Vec<(bool, Vec<u8>)>, String> {
+ if stop.load(Ordering::Relaxed) {
+ return Ok(Vec::new());
+ }
+ let mut packets = Vec::new();
+ self.receive(&mut packets)?;
+ let mut error = [0 as c_char; 512];
+ // SAFETY: input is borrowed only for this call; the native encoder copies it into an owned frame.
+ let result = unsafe {
+ nectarlink_video_encoder_send(
+ self.native.as_ptr(),
+ nv12.as_ptr(),
+ nv12.len(),
+ hns / 10,
+ error.as_mut_ptr(),
+ error.len(),
+ )
+ };
+ if result < 0 {
+ return Err(error_message(&error));
+ }
+ self.receive(&mut packets)?;
+ if self.last_output.elapsed() >= Duration::from_secs(2) {
+ return Err("Hardware video encoder stopped producing output".into());
+ }
+ Ok(packets)
+ }
+
+ fn receive(&mut self, packets: &mut Vec<(bool, Vec<u8>)>) -> Result<(), String> {
+ loop {
+ let mut bytes = std::ptr::null();
+ let mut length = 0;
+ let mut keyframe = false;
+ let mut error = [0 as c_char; 512];
+ // SAFETY: output pointers refer to locals and the native encoder remains live.
+ let result = unsafe {
+ nectarlink_video_encoder_receive(
+ self.native.as_ptr(),
+ &mut bytes,
+ &mut length,
+ &mut keyframe,
+ error.as_mut_ptr(),
+ error.len(),
+ )
+ };
+ match result {
+ 0 => return Ok(()),
+ 1 if !bytes.is_null() && length > 0 => {
+ // SAFETY: packet storage remains valid until the next receive call; copy it now.
+ let owned = unsafe { std::slice::from_raw_parts(bytes, length) }.to_vec();
+ packets.push((keyframe, owned));
+ self.last_output = Instant::now();
+ }
+ _ => return Err(error_message(&error)),
+ }
+ }
+ }
+}
+
+impl Drop for Encoder {
+ fn drop(&mut self) {
+ // SAFETY: this is the sole owner; destruction releases codec, frames, packets, and device.
+ unsafe { nectarlink_video_encoder_destroy(self.native.as_ptr()) };
+ }
+}
+
+fn error_message(buffer: &[c_char]) -> String {
+ // SAFETY: buffers are zero-initialized and the native API always NUL-terminates errors.
+ unsafe { CStr::from_ptr(buffer.as_ptr()) }.to_string_lossy().into_owned()
+}
diff --git a/desktop/app/src/pc_mirror.rs b/desktop/app/src/pc_mirror.rs
index 24dff88..7a8677c 100644
--- a/desktop/app/src/pc_mirror.rs
+++ b/desktop/app/src/pc_mirror.rs
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-or-later
//! Shares the PC's display with a paired phone over `mirror` / `video` (session 0).
-//! Encodes H.264 or HEVC using Media Foundation on Windows, or H.264 using OpenH264
+//! Encodes H.264 or HEVC using Media Foundation on Windows and FFmpeg or OpenH264
//! on Linux, and handles remote touch, keyboard, and pointer input.
#[cfg(any(windows, target_os = "linux"))]
@@ -106,7 +106,15 @@ mod imp {
}
#[cfg(target_os = "linux")]
{
- if openh264_enc_api().is_none() {
+ let can_encode = openh264_enc_api().is_some();
+ #[cfg(linux_ffmpeg_video)]
+ let can_encode = can_encode || {
+ static HARDWARE_AVAILABLE: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
+ *HARDWARE_AVAILABLE.get_or_init(|| {
+ crate::win::video_encoder::Encoder::new("h264", 320, 240, 30, 1_000_000).is_ok()
+ })
+ };
+ if !can_encode {
return false;
}
let has_x11 = std::env::var_os("DISPLAY").is_some() && x11_api().is_some();
@@ -1102,7 +1110,7 @@ mod imp {
}
#[cfg(target_os = "linux")]
- pub(crate) struct ScreenEncoder {
+ pub(crate) struct OpenH264Encoder {
encoder: *mut std::ffi::c_void,
width: u32,
height: u32,
@@ -1112,7 +1120,7 @@ mod imp {
}
#[cfg(target_os = "linux")]
- impl Drop for ScreenEncoder {
+ impl Drop for OpenH264Encoder {
fn drop(&mut self) {
if !self.encoder.is_null() {
unsafe {
@@ -1129,20 +1137,7 @@ mod imp {
}
#[cfg(target_os = "linux")]
- impl ScreenEncoder {
- pub(crate) fn new_for_codec(
- codec: &str,
- width: u32,
- height: u32,
- fps: u32,
- bitrate: u32,
- ) -> Result<Self, String> {
- if codec != "h264" {
- return Err("Linux screen encoding supports H.264 only".into());
- }
- Self::new(width, height, fps, bitrate)
- }
-
+ impl OpenH264Encoder {
pub(crate) fn new(width: u32, height: u32, fps: u32, bitrate: u32) -> Result<Self, String> {
let api = openh264_enc_api().ok_or_else(|| "libopenh264 encoder unavailable".to_owned())?;
unsafe {
@@ -1394,6 +1389,83 @@ mod imp {
}
}
+ #[cfg(target_os = "linux")]
+ pub(crate) enum ScreenEncoder {
+ OpenH264(OpenH264Encoder),
+ #[cfg(linux_ffmpeg_video)]
+ Hardware(crate::win::video_encoder::Encoder),
+ }
+
+ #[cfg(target_os = "linux")]
+ impl ScreenEncoder {
+ pub(crate) fn new_for_codec(
+ codec: &str,
+ width: u32,
+ height: u32,
+ fps: u32,
+ bitrate: u32,
+ ) -> Result<Self, String> {
+ #[cfg(linux_ffmpeg_video)]
+ match crate::win::video_encoder::Encoder::new(codec, width, height, fps, bitrate) {
+ Ok(encoder) => return Ok(Self::Hardware(encoder)),
+ Err(error) if codec != "h264" => return Err(error),
+ Err(error) => tracing::debug!(%error, "Hardware H.264 unavailable; using OpenH264"),
+ }
+ if codec != "h264" {
+ return Err(
+ "HEVC hardware encoding requires FFmpeg development libraries at build time".into()
+ );
+ }
+ OpenH264Encoder::new(width, height, fps, bitrate).map(Self::OpenH264)
+ }
+
+ #[cfg(linux_ffmpeg_video)]
+ fn next_encoder(&mut self, codec: &str, width: u32, height: u32, fps: u32, bitrate: u32) -> bool {
+ if let Self::Hardware(encoder) = self {
+ if encoder.next_device() {
+ return true;
+ }
+ if codec == "h264"
+ && let Ok(software) = OpenH264Encoder::new(width, height, fps, bitrate)
+ {
+ *self = Self::OpenH264(software);
+ return true;
+ }
+ }
+ false
+ }
+
+ pub(crate) fn request_keyframe(&self) -> bool {
+ match self {
+ Self::OpenH264(encoder) => encoder.request_keyframe(),
+ #[cfg(linux_ffmpeg_video)]
+ Self::Hardware(encoder) => encoder.request_keyframe(),
+ }
+ }
+
+ pub(crate) fn set_bitrate(&self, bitrate: u32) -> bool {
+ match self {
+ Self::OpenH264(encoder) => encoder.set_bitrate(bitrate),
+ #[cfg(linux_ffmpeg_video)]
+ Self::Hardware(encoder) => encoder.set_bitrate(bitrate),
+ }
+ }
+
+ pub(crate) fn encode(
+ &mut self,
+ nv12: &[u8],
+ hns: i64,
+ dur_hns: i64,
+ stop: &AtomicBool,
+ ) -> Result<Vec<(bool, Vec<u8>)>, String> {
+ match self {
+ Self::OpenH264(encoder) => encoder.encode(nv12, hns, dur_hns, stop),
+ #[cfg(linux_ffmpeg_video)]
+ Self::Hardware(encoder) => encoder.encode(nv12, hns, stop),
+ }
+ }
+ }
+
#[cfg(windows)]
fn new_pc_encoder(
codec: &str,
@@ -2486,7 +2558,7 @@ mod imp {
if stop.load(Ordering::Relaxed) {
break;
}
- #[cfg(windows)]
+ #[cfg(any(windows, linux_ffmpeg_video))]
if enc.next_encoder(&codec, dst_w, dst_h, fps, bitrate) {
tracing::warn!(error = %e, codec, "Video encoding failed; trying another encoder");
stats.rebuilds += 1;
@@ -2810,7 +2882,8 @@ mod imp {
#[cfg(windows)]
let mut enc = new_pc_encoder("h264", w, h, 30, 2_000_000).expect("ScreenEncoder");
#[cfg(target_os = "linux")]
- let mut enc = ScreenEncoder::new(w, h, 30, 2_000_000).expect("OpenH264");
+ let mut enc =
+ ScreenEncoder::OpenH264(OpenH264Encoder::new(w, h, 30, 2_000_000).expect("OpenH264"));
let mut nv12 = Vec::new();
bgra_to_nv12(&vec![128u8; (w as usize) * (h as usize) * 4], w as usize, h as usize, &mut nv12);
let stop = AtomicBool::new(false);
diff --git a/desktop/app/tests/linux_video_dispatch_test.cpp b/desktop/app/tests/linux_video_dispatch_test.cpp
new file mode 100644
index 0000000..f882eab
--- /dev/null
+++ b/desktop/app/tests/linux_video_dispatch_test.cpp
@@ -0,0 +1,80 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+#if defined(TEST_BUILTIN) || defined(TEST_PLUGIN)
+#ifdef TEST_BUILTIN
+#include "../cpp/linux_video_builtin.h"
+#endif
+#include "../cpp/linux_video_codec.h"
+
+#ifndef TEST_LABEL
+#define TEST_LABEL "test"
+#endif
+namespace { int object; }
+
+extern "C" void* nectarlink_video_decoder_create(bool, char*, size_t)
+{
+#ifdef TEST_AVAILABLE
+ return &object;
+#else
+ return nullptr;
+#endif
+}
+extern "C" void nectarlink_video_decoder_destroy(void*) {}
+extern "C" bool nectarlink_video_decoder_next_device(void* p, char*, size_t) { return p == &object; }
+extern "C" const char* nectarlink_video_decoder_backend(const void*) { return TEST_LABEL; }
+extern "C" int nectarlink_video_decoder_send(void* p, const uint8_t*, size_t n, char*, size_t)
+{ return p == &object ? static_cast<int>(n) : -1; }
+extern "C" int nectarlink_video_decoder_receive(void*, NectarlinkDecodedNv12*, char*, size_t) { return 0; }
+extern "C" void nectarlink_video_decoder_flush(void*) {}
+extern "C" void* nectarlink_video_encoder_create(bool, int, int, int, int, char*, size_t)
+{
+#ifdef TEST_AVAILABLE
+ return &object;
+#else
+ return nullptr;
+#endif
+}
+extern "C" void nectarlink_video_encoder_destroy(void*) {}
+extern "C" bool nectarlink_video_encoder_next_device(void* p, char*, size_t) { return p == &object; }
+extern "C" const char* nectarlink_video_encoder_backend(const void*) { return TEST_LABEL; }
+extern "C" int nectarlink_video_encoder_send(void* p, const uint8_t*, size_t n, int64_t, char*, size_t)
+{ return p == &object ? static_cast<int>(n) : -1; }
+extern "C" int nectarlink_video_encoder_receive(void*, const uint8_t**, size_t*, bool*, char*, size_t) { return 0; }
+extern "C" void nectarlink_video_encoder_request_keyframe(void*) {}
+extern "C" bool nectarlink_video_encoder_set_bitrate(void* p, int bitrate) { return p == &object && bitrate == 123; }
+
+#else
+#include "../cpp/linux_video_codec.h"
+#include <cassert>
+#include <cstring>
+
+int main(int argc, char** argv)
+{
+ char error[128]{};
+ auto* encoder = nectarlink_video_encoder_create(false, 320, 240, 30, 123, error, sizeof(error));
+ auto* decoder = nectarlink_video_decoder_create(false, error, sizeof(error));
+ if (argc > 1 && std::strcmp(argv[1], "none") == 0) {
+ assert(!encoder && !decoder);
+ return 0;
+ }
+ assert(encoder && decoder);
+ const char* expected = argc > 1 ? argv[1] : "test";
+ assert(std::strcmp(nectarlink_video_encoder_backend(encoder), expected) == 0);
+ assert(std::strcmp(nectarlink_video_decoder_backend(decoder), expected) == 0);
+ const uint8_t data[] = {1, 2, 3};
+ assert(nectarlink_video_encoder_send(encoder, data, sizeof(data), 0, error, sizeof(error)) == 3);
+ assert(nectarlink_video_decoder_send(decoder, data, sizeof(data), error, sizeof(error)) == 3);
+ NectarlinkDecodedNv12 frame{};
+ assert(nectarlink_video_decoder_receive(decoder, &frame, error, sizeof(error)) == 0);
+ const uint8_t* packet = nullptr;
+ size_t length = 0;
+ bool keyframe = false;
+ assert(nectarlink_video_encoder_receive(encoder, &packet, &length, &keyframe, error, sizeof(error)) == 0);
+ assert(nectarlink_video_encoder_set_bitrate(encoder, 123));
+ assert(nectarlink_video_encoder_next_device(encoder, error, sizeof(error)));
+ assert(nectarlink_video_decoder_next_device(decoder, error, sizeof(error)));
+ nectarlink_video_encoder_request_keyframe(encoder);
+ nectarlink_video_decoder_flush(decoder);
+ nectarlink_video_encoder_destroy(encoder);
+ nectarlink_video_decoder_destroy(decoder);
+}
+#endif
diff --git a/desktop/app/tests/linux_video_dispatch_test.sh b/desktop/app/tests/linux_video_dispatch_test.sh
new file mode 100644
index 0000000..582804a
--- /dev/null
+++ b/desktop/app/tests/linux_video_dispatch_test.sh
@@ -0,0 +1,28 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: GPL-3.0-or-later
+set -euo pipefail
+root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
+stage="$(mktemp -d -t nectarlink-video-test-XXXXXX)"
+trap 'rm -rf "${stage}"' EXIT
+test_source="${root}/tests/linux_video_dispatch_test.cpp"
+
+g++ -std=c++17 -Wall -Wextra -Werror -DTEST_BUILTIN -c "${test_source}" -o "${stage}/builtin.o"
+g++ -std=c++17 -Wall -Wextra -Werror "${root}/cpp/linux_video_dispatch.cpp" \
+ "${test_source}" "${stage}/builtin.o" -ldl -o "${stage}/test"
+"${stage}/test" none
+g++ -std=c++17 -Wall -Wextra -Werror -fPIC -shared -DTEST_PLUGIN \
+ "${test_source}" -o "${stage}/libnectarlink-video-ffmpeg7.so"
+g++ -std=c++17 -Wall -Wextra -Werror -fPIC -shared -DTEST_PLUGIN -DTEST_AVAILABLE -DTEST_LABEL='"ffmpeg8"' \
+ "${test_source}" -o "${stage}/libnectarlink-video-ffmpeg8.so"
+"${stage}/test" ffmpeg8
+g++ -std=c++17 -Wall -Wextra -Werror -fPIC -shared -DTEST_PLUGIN -DTEST_AVAILABLE -DTEST_LABEL='"ffmpeg9"' \
+ "${test_source}" -o "${stage}/libnectarlink-video-ffmpeg9.so"
+"${stage}/test" ffmpeg9
+g++ -std=c++17 -Wall -Wextra -Werror -DTEST_BUILTIN -DTEST_AVAILABLE -DTEST_LABEL='"builtin"' \
+ -c "${test_source}" -o "${stage}/available-builtin.o"
+g++ -std=c++17 -Wall -Wextra -Werror "${root}/cpp/linux_video_dispatch.cpp" \
+ "${test_source}" "${stage}/available-builtin.o" -ldl -o "${stage}/test-with-builtin"
+"${stage}/test-with-builtin" ffmpeg9
+g++ -std=c++17 -Wall -Wextra -Werror "${root}/tests/linux_video_software_test.cpp" \
+ $(pkg-config --cflags libavcodec libavutil) -ldl -o "${stage}/software-test"
+"${stage}/software-test"
diff --git a/desktop/app/tests/linux_video_software_test.cpp b/desktop/app/tests/linux_video_software_test.cpp
new file mode 100644
index 0000000..f529973
--- /dev/null
+++ b/desktop/app/tests/linux_video_software_test.cpp
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+#include "../cpp/linux_video_codec.cpp"
+#include <cassert>
+#include <cstring>
+
+int main()
+{
+ const uint8_t keyframe[] = {
+ 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0xc0, 0x0a, 0xd9, 0x04, 0x26, 0xc0, 0x44, 0x00, 0x00, 0x03, 0x00,
+ 0x04, 0x00, 0x00, 0x03, 0x00, 0xc8, 0x3c, 0x48, 0x99, 0x20, 0x00, 0x00, 0x00, 0x01, 0x68, 0xcb, 0x83,
+ 0xcb, 0x20, 0x00, 0x00, 0x01, 0x65, 0x88, 0x84, 0x0a, 0xf1, 0x18, 0xa0, 0x00, 0x27, 0xa7, 0x1c, 0x00,
+ 0x04, 0xbe, 0x23, 0x80, 0x00, 0x82, 0xec, 0x9c, 0x9c, 0x9d, 0x75, 0xd7, 0x5d, 0x75, 0xd7, 0x5d, 0x75,
+ 0xd7, 0x5e,
+ };
+ Decoder decoder;
+ const AVCodec* codec = decoder.functions.find_decoder("h264");
+ assert(codec);
+ decoder.initialize_software(codec);
+ decoder.functions.check(decoder.functions.new_packet(decoder.packet, sizeof(keyframe)), "Allocate packet");
+ std::memcpy(decoder.packet->data, keyframe, sizeof(keyframe));
+ decoder.functions.check(decoder.functions.send_packet(decoder.context, decoder.packet), "Send packet");
+ decoder.functions.check(decoder.functions.receive_frame(decoder.context, decoder.hardware), "Receive picture");
+ const auto picture = decoded_picture(decoder, decoder.hardware);
+ assert(picture.width == 64 && picture.height == 64);
+ assert(picture.y && picture.uv && picture.stride_y >= 64 && picture.stride_uv >= 64);
+
+ AVFrame* planar = decoder.functions.frame_alloc();
+ assert(planar);
+ uint8_t y[] = {1, 2, 3, 4, 0, 0, 5, 6, 7, 8, 0, 0};
+ uint8_t u[] = {11, 12, 0};
+ uint8_t v[] = {21, 22, 0};
+ planar->width = 5;
+ planar->height = 3;
+ planar->format = AV_PIX_FMT_YUV420P;
+ planar->data[0] = y;
+ planar->data[1] = u;
+ planar->data[2] = v;
+ planar->linesize[0] = 6;
+ planar->linesize[1] = 3;
+ planar->linesize[2] = 3;
+ const auto converted = decoded_picture(decoder, planar);
+ assert(converted.width == 4 && converted.height == 2);
+ assert(converted.stride_y == 6 && converted.stride_uv == 4);
+ assert(converted.uv[0] == 11 && converted.uv[1] == 21);
+ assert(converted.uv[2] == 12 && converted.uv[3] == 22);
+ uint8_t nv12[] = {11, 21, 12, 22, 0, 0};
+ planar->format = AV_PIX_FMT_NV12;
+ planar->data[1] = nv12;
+ planar->data[2] = nullptr;
+ planar->linesize[1] = 6;
+ const auto packed = decoded_picture(decoder, planar);
+ assert(packed.width == 4 && packed.height == 2 && packed.stride_uv == 6);
+ assert(packed.uv == nv12);
+ decoder.functions.frame_free(&planar);
+}
diff --git a/desktop/packaging/linux/build-appimage.sh b/desktop/packaging/linux/build-appimage.sh
index 2bfb994..926a05f 100755
--- a/desktop/packaging/linux/build-appimage.sh
+++ b/desktop/packaging/linux/build-appimage.sh
@@ -52,6 +52,9 @@ mkdir -p \
"${APPDIR}/usr/share/icons/hicolor/scalable/apps"
install -m 0755 "${BIN_PATH}" "${APPDIR}/usr/bin/nectarlink-desktop"
+for adapter in "$(dirname "${BIN_PATH}")"/libnectarlink-video-ffmpeg*.so; do
+ [[ ! -f "${adapter}" ]] || install -m 0755 "${adapter}" "${APPDIR}/usr/bin/"
+done
for qm in "$(dirname "${BIN_PATH}")"/nectarlink_*.qm; do
if [[ -f "${qm}" ]]; then
diff --git a/desktop/packaging/linux/build-deb.sh b/desktop/packaging/linux/build-deb.sh
index 283faf8..d78bfe3 100755
--- a/desktop/packaging/linux/build-deb.sh
+++ b/desktop/packaging/linux/build-deb.sh
@@ -54,6 +54,9 @@ mkdir -p \
"${PKG_ROOT}/usr/share/icons/hicolor/scalable/apps"
install -m 0755 "${BIN_PATH}" "${PKG_ROOT}/usr/lib/nectarlink/nectarlink-desktop"
+for adapter in "$(dirname "${BIN_PATH}")"/libnectarlink-video-ffmpeg*.so; do
+ [[ ! -f "${adapter}" ]] || install -m 0755 "${adapter}" "${PKG_ROOT}/usr/lib/nectarlink/"
+done
cat > "${PKG_ROOT}/usr/bin/nectarlink-desktop" <<'EOF'
#!/usr/bin/env sh
if [ -d /usr/lib/nectarlink/lib ]; then
diff --git a/desktop/packaging/linux/build-ffmpeg-backends.sh b/desktop/packaging/linux/build-ffmpeg-backends.sh
new file mode 100644
index 0000000..d1f058b
--- /dev/null
+++ b/desktop/packaging/linux/build-ffmpeg-backends.sh
@@ -0,0 +1,34 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: GPL-3.0-or-later
+# Build ABI adapters for the FFmpeg versions used by supported distributions.
+set -euo pipefail
+
+root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)"
+stage="$(mktemp -d -t nectarlink-ffmpeg-XXXXXX)"
+trap 'rm -rf "${stage}"' EXIT
+out="${FFMPEG_BACKEND_DIR:-${root}/target/release}"
+mkdir -p "${out}"
+cache="${FFMPEG_SOURCE_CACHE:-${stage}}"
+mkdir -p "${cache}"
+
+build_adapter() {
+ local version="$1" checksum="$2" major="${1%%.*}"
+ local archive="${cache}/ffmpeg-${version}.tar.xz"
+ if [[ ! -f "${archive}" ]] || ! echo "${checksum} ${archive}" | sha256sum -c --status; then
+ curl -fsSL "https://ffmpeg.org/releases/ffmpeg-${version}.tar.xz" -o "${archive}.tmp"
+ echo "${checksum} ${archive}.tmp" | sha256sum -c -
+ mv "${archive}.tmp" "${archive}"
+ fi
+ tar -xf "${archive}" -C "${stage}"
+ (
+ cd "${stage}/ffmpeg-${version}"
+ ./configure --disable-everything --disable-x86asm --disable-programs --disable-doc >/dev/null
+ )
+ ${CXX:-c++} -std=c++17 -O2 -fPIC -shared -Wl,-Bsymbolic \
+ -I"${stage}/ffmpeg-${version}" "${root}/desktop/app/cpp/linux_video_codec.cpp" -ldl \
+ -o "${out}/libnectarlink-video-ffmpeg${major}.so"
+}
+
+build_adapter 7.1.1 733984395e0dbbe5c046abda2dc49a5544e7e0e1e2366bba849222ae9e3a03b1
+build_adapter 8.0.1 05ee0b03119b45c0bdb4df654b96802e909e0a752f72e4fe3794f487229e5a41
+build_adapter 9.0.2 8c3850283eb25fa026482078a04051e0be17347b09ef81a0849bec15a96e002e
diff --git a/desktop/packaging/linux/build-tarball.sh b/desktop/packaging/linux/build-tarball.sh
index a57ab21..9ca8bc2 100755
--- a/desktop/packaging/linux/build-tarball.sh
+++ b/desktop/packaging/linux/build-tarball.sh
@@ -41,6 +41,9 @@ BUNDLE_DIR="${STAGE_DIR}/${BUNDLE_NAME}"
mkdir -p "${BUNDLE_DIR}/share/applications" "${BUNDLE_DIR}/share/metainfo" "${BUNDLE_DIR}/share/icons" "${BUNDLE_DIR}/share/nectarlink"
install -m 0755 "${BIN_PATH}" "${BUNDLE_DIR}/nectarlink-desktop"
+for adapter in "$(dirname "${BIN_PATH}")"/libnectarlink-video-ffmpeg*.so; do
+ [[ ! -f "${adapter}" ]] || install -m 0755 "${adapter}" "${BUNDLE_DIR}/"
+done
printf '%s\n' "${VERSION}" > "${BUNDLE_DIR}/VERSION"
for qm in "$(dirname "${BIN_PATH}")"/nectarlink_*.qm; do
if [[ -f "${qm}" ]]; then
@@ -160,6 +163,9 @@ fi
mkdir -p "${PREFIX}" "$(dirname "${BIN_LINK}")" "${APPS_DIR}" "${META_DIR}" "${ICON_DIR}" "${PNG_ICON_DIR}"
install -m 0755 "${SELF_DIR}/nectarlink-desktop" "${PREFIX}/nectarlink-desktop"
+for adapter in "${SELF_DIR}"/libnectarlink-video-ffmpeg*.so; do
+ [[ ! -f "${adapter}" ]] || install -m 0755 "${adapter}" "${PREFIX}/"
+done
[[ -f "${SELF_DIR}/VERSION" ]] && install -m 0644 "${SELF_DIR}/VERSION" "${PREFIX}/VERSION"
[[ -f "${SELF_DIR}/qt.conf" ]] && install -m 0644 "${SELF_DIR}/qt.conf" "${PREFIX}/qt.conf"
for dir in lib plugins qml; do
diff --git a/docs/technical/screen-video.md b/docs/technical/screen-video.md
index d7ad662..8242a51 100644
--- a/docs/technical/screen-video.md
+++ b/docs/technical/screen-video.md
@@ -7,3 +7,9 @@ retain their producer's adapter LUID when passed to the virtual camera.
When decoding switches devices, the receiver requests a keyframe before
resuming.
+
+Linux tries FFmpeg's NVENC, VAAPI and QSV hardware encoders, with OpenH264 as
+the final H.264 fallback. It tries available CUDA devices and DRM render nodes
+for hardware decoding before falling back to software. Decoded pictures are
+downloaded as NV12 for the Qt renderer. Linux advertises `mirror.decode.hevc`
+when its hardware HEVC decoder initializes.