From fba6a5d0f2e97f213327d2eacbe6cd8fcca8b4c5 Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Wed, 21 Oct 2026 13:57:24 -0400 Subject: [PATCH] feat: Remote Play host: PipeWire capture, hardware encoding, touch and game audio (#1139) * feat: run SteamOS PipeWire and WirePlumber for gamescope screen capture * feat: hardware Remote Play encoding through MediaCodec behind Steam's V4L2 stage * feat: deliver Remote Play touches to gamescope as touches * feat: route game audio through PulseAudio while a Remote Play client is connected * fix: keep the Remote Play marker when a forked child closes the encoder * fix: allow standalone preload tests without V4L2 encoder hook --- .github/workflows/build.yml | 11 + app/src/main/cpp/CMakeLists.txt | 4 + app/src/main/cpp/venc/venc_proto.h | 55 ++ app/src/main/cpp/venc/venc_server.cpp | 346 ++++++++++ .../launcher/session/SessionFiles.kt | 2 + .../launcher/session/SessionService.kt | 6 + .../launcher/session/VideoEncodeBridge.kt | 28 + tools/build_local.sh | 10 + .../overlay/usr/local/bin/droiddeck-pipewire | 62 ++ .../overlay/usr/local/bin/droiddeck-session | 10 +- .../overlay/usr/local/bin/steam-compatibility | 7 + tools/linuxfs/preload/fsync.c | 6 + tools/linuxfs/preload/netif.c | 2 + tools/linuxfs/preload/ntsync.c | 2 + tools/linuxfs/preload/opens.c | 3 + tools/linuxfs/preload/rptouch.c | 89 +++ tools/linuxfs/preload/venc.c | 633 ++++++++++++++++++ tools/pipewire/assemble.sh | 29 + tools/pipewire/packages.txt | 4 + tools/release/runtime_inputs.py | 6 +- tools/tests/test_pipewire_helper.py | 90 +++ tools/tests/test_venc_emulator.py | 227 +++++++ 22 files changed, 1629 insertions(+), 3 deletions(-) create mode 100644 app/src/main/cpp/venc/venc_proto.h create mode 100644 app/src/main/cpp/venc/venc_server.cpp create mode 100644 app/src/main/java/com/droiddeck/launcher/session/VideoEncodeBridge.kt create mode 100755 tools/linuxfs/overlay/usr/local/bin/droiddeck-pipewire create mode 100644 tools/linuxfs/preload/rptouch.c create mode 100644 tools/linuxfs/preload/venc.c create mode 100755 tools/pipewire/assemble.sh create mode 100644 tools/pipewire/packages.txt create mode 100644 tools/tests/test_pipewire_helper.py create mode 100644 tools/tests/test_venc_emulator.py diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b063bfb07f..24c5af98ca 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -321,6 +321,17 @@ jobs: install -Dm644 tools/mangoapp/mangoapp "$D/usr/local/bin/mangoapp" rm -rf mango-pkgs ls -l "$M" + mkdir -p pw-pkgs + grep -v '^#' tools/pipewire/packages.txt | while read -r sum url; do + [ -n "$url" ] || continue + curl -fsSL --retry 3 -o pw-pkgs/pkg.tar.zst "$url" + echo "$sum pw-pkgs/pkg.tar.zst" | sha256sum -c - + tar --use-compress-program=unzstd -xf pw-pkgs/pkg.tar.zst -C pw-pkgs + rm pw-pkgs/pkg.tar.zst + done + tools/pipewire/assemble.sh pw-pkgs "$D/usr/local/lib/droiddeck-pipewire.tzst" + rm -rf pw-pkgs + ls -l "$D/usr/local/lib/droiddeck-pipewire.tzst" T="$D/usr/local/lib/droiddeck-msitools" mkdir -p "$T" msi-pkgs grep -v '^#' tools/msitools/packages.txt | while read -r sum url; do diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 136e0f0e9d..ab06f6a56f 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -39,6 +39,10 @@ target_link_libraries(deviceinfo dl) add_library(termux SHARED termux/termux.c) +add_library(ddvenc SHARED venc/venc_server.cpp) +target_compile_features(ddvenc PRIVATE cxx_std_17) +target_link_libraries(ddvenc mediandk log) + # The optional proprietary SDK is supplied locally; its headers/libraries are not vendored. if(DROIDDECK_VITURE_SDK_DIR) add_library(droiddeck_viture SHARED viture/sdk_bridge.cpp) diff --git a/app/src/main/cpp/venc/venc_proto.h b/app/src/main/cpp/venc/venc_proto.h new file mode 100644 index 0000000000..9622577119 --- /dev/null +++ b/app/src/main/cpp/venc/venc_proto.h @@ -0,0 +1,55 @@ +#ifndef DROIDDECK_VENC_PROTO_H +#define DROIDDECK_VENC_PROTO_H + +#include + +/* SOCK_SEQPACKET between the guest's Remote Play hooks and the app: the emulated /dev/video-enc0 and + * MediaCodec encoder, and remote touches. A config carries the buffer memfds (inputs first) as SCM_RIGHTS; + * every frame gets one reply, touches get none. */ + +#define VENC_MAX_BUFFERS 16 + +enum venc_msg { VENC_CONFIG = 1, VENC_FRAME = 2, VENC_DONE = 3, VENC_BITRATE = 4, VENC_TOUCH = 5 }; +enum venc_touch_action { VENC_TOUCH_DOWN = 0, VENC_TOUCH_MOVE = 1, VENC_TOUCH_UP = 2 }; +enum venc_codec { VENC_H264 = 0, VENC_HEVC = 1 }; + +struct venc_config { + uint32_t type; + uint32_t codec; + uint32_t width, height, stride, rows; + uint32_t fps_num, fps_den; + uint32_t bitrate, peak_bitrate, vbr; + uint32_t gop; + int32_t profile, level; + uint32_t inputs, outputs; + uint32_t input_size, output_size; +}; + +struct venc_frame { + uint32_t type; + uint32_t input, output; + uint32_t bytes; + uint32_t keyframe; + int64_t pts_us; +}; + +struct venc_done { + uint32_t type; + int32_t status; + uint32_t bytes; + uint32_t keyframe; +}; + +struct venc_bitrate { + uint32_t type; + uint32_t bitrate; +}; + +/* Remote Play's touch-as-mouse turned back into a touch; x and y are fractions of the screen in 1/65535. */ +struct venc_touch { + uint32_t type; + uint32_t action; + uint32_t x, y; +}; + +#endif diff --git a/app/src/main/cpp/venc/venc_server.cpp b/app/src/main/cpp/venc/venc_server.cpp new file mode 100644 index 0000000000..be484c898d --- /dev/null +++ b/app/src/main/cpp/venc/venc_server.cpp @@ -0,0 +1,346 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "venc_proto.h" + +#define LOG(...) __android_log_print(ANDROID_LOG_INFO, "DroidDeckVenc", __VA_ARGS__) + +namespace { + +struct Mapping { + uint8_t *data = nullptr; + size_t size = 0; +}; + +constexpr int32_t kColorFormatYuv420SemiPlanar = 21; +constexpr uint32_t kBufferFlagKeyFrame = 1; +constexpr uint32_t kBufferFlagCodecConfig = 2; +constexpr int kTouchWidth = 1920, kTouchHeight = 1080; // InputSpace.kt +constexpr int kRemoteTouchId = 31; // the compositor's last touch slot (MAX_TOUCH_POINTS 32) + +int32_t avc_profile(int32_t v4l2) { + switch (v4l2) { + case 0: return 1; + case 1: return 0x10000; + case 2: return 2; + case 3: return 4; + case 4: return 8; + default: return 0; + } +} + +class Session { +public: + explicit Session(int fd) : fd_(fd) {} + ~Session() { + release(); + close(fd_); + } + + void run() { + for (;;) { + uint8_t buf[256]; + int fds[VENC_MAX_BUFFERS * 2]; + int nfds = 0; + ssize_t n = receive(buf, sizeof buf, fds, &nfds); + if (n <= 0) return; + uint32_t type; + memcpy(&type, buf, sizeof type); + if (type == VENC_CONFIG && n == (ssize_t)sizeof(venc_config)) { + venc_config c; + memcpy(&c, buf, sizeof c); + bool ok = configure(c, fds, nfds); + for (int i = 0; i < nfds; i++) close(fds[i]); + reply(ok ? 0 : -1, 0, false); + } else if (type == VENC_FRAME && n == (ssize_t)sizeof(venc_frame)) { + venc_frame f; + memcpy(&f, buf, sizeof f); + uint32_t bytes = 0; + bool key = false; + bool ok = encode(f, &bytes, &key); + reply(ok ? 0 : -1, bytes, key); + } else if (type == VENC_TOUCH && n == (ssize_t)sizeof(venc_touch)) { + venc_touch t; + memcpy(&t, buf, sizeof t); + touch(t); + for (int i = 0; i < nfds; i++) close(fds[i]); + } else if (type == VENC_BITRATE && n == (ssize_t)sizeof(venc_bitrate)) { + venc_bitrate b; + memcpy(&b, buf, sizeof b); + set_bitrate(b.bitrate); + for (int i = 0; i < nfds; i++) close(fds[i]); + } else { + LOG("unexpected message %u of %zd bytes; closing", type, n); + for (int i = 0; i < nfds; i++) close(fds[i]); + return; + } + } + } + +private: + ssize_t receive(uint8_t *buf, size_t len, int *fds, int *nfds) { + char control[CMSG_SPACE(sizeof(int) * VENC_MAX_BUFFERS * 2)]; + iovec iov = {buf, len}; + msghdr m = {}; + m.msg_iov = &iov; + m.msg_iovlen = 1; + m.msg_control = control; + m.msg_controllen = sizeof control; + ssize_t n = recvmsg(fd_, &m, MSG_CMSG_CLOEXEC); + for (cmsghdr *c = CMSG_FIRSTHDR(&m); n > 0 && c; c = CMSG_NXTHDR(&m, c)) { + if (c->cmsg_level != SOL_SOCKET || c->cmsg_type != SCM_RIGHTS) continue; + int count = (int)((c->cmsg_len - CMSG_LEN(0)) / sizeof(int)); + memcpy(fds, CMSG_DATA(c), sizeof(int) * std::min(count, VENC_MAX_BUFFERS * 2)); + *nfds = std::min(count, VENC_MAX_BUFFERS * 2); + } + return n; + } + + void reply(int32_t status, uint32_t bytes, bool key) { + venc_done d = {VENC_DONE, status, bytes, key ? 1u : 0u}; + send(fd_, &d, sizeof d, MSG_NOSIGNAL); + } + + void release() { + if (codec_) { + AMediaCodec_stop(codec_); + AMediaCodec_delete(codec_); + codec_ = nullptr; + } + for (auto &m : inputs_) munmap(m.data, m.size); + for (auto &m : outputs_) munmap(m.data, m.size); + inputs_.clear(); + outputs_.clear(); + header_.clear(); + } + + AMediaCodec *create(const venc_config &c, bool with_profile) { + const char *mime = c.codec == VENC_HEVC ? "video/hevc" : "video/avc"; + AMediaCodec *codec = AMediaCodec_createEncoderByType(mime); + if (!codec) return nullptr; + float fps = c.fps_num ? (float)c.fps_den / (float)c.fps_num : 60.0f; + AMediaFormat *f = AMediaFormat_new(); + AMediaFormat_setString(f, AMEDIAFORMAT_KEY_MIME, mime); + AMediaFormat_setInt32(f, AMEDIAFORMAT_KEY_WIDTH, (int32_t)c.width); + AMediaFormat_setInt32(f, AMEDIAFORMAT_KEY_HEIGHT, (int32_t)c.height); + AMediaFormat_setInt32(f, AMEDIAFORMAT_KEY_COLOR_FORMAT, kColorFormatYuv420SemiPlanar); + AMediaFormat_setInt32(f, AMEDIAFORMAT_KEY_BIT_RATE, (int32_t)std::max(c.bitrate, 500000u)); + AMediaFormat_setInt32(f, "bitrate-mode", c.vbr ? 1 : 2); + AMediaFormat_setFloat(f, AMEDIAFORMAT_KEY_FRAME_RATE, fps); + AMediaFormat_setFloat(f, "operating-rate", fps); + AMediaFormat_setInt32(f, "priority", 0); + AMediaFormat_setInt32(f, "latency", 1); + AMediaFormat_setInt32(f, "max-bframes", 0); + AMediaFormat_setInt32(f, "prepend-sps-pps-to-idr-frames", 1); + float gop_seconds = c.gop && c.gop < 0xffff ? (float)c.gop / fps : 3600.0f; + AMediaFormat_setFloat(f, AMEDIAFORMAT_KEY_I_FRAME_INTERVAL, std::max(gop_seconds, 0.1f)); + if (with_profile) { + int32_t profile = c.codec == VENC_HEVC ? (c.profile == 2 ? 2 : 1) : avc_profile(c.profile); + if (profile) AMediaFormat_setInt32(f, "profile", profile); + } + media_status_t s = AMediaCodec_configure(codec, f, nullptr, nullptr, AMEDIACODEC_CONFIGURE_FLAG_ENCODE); + AMediaFormat_delete(f); + if (s == AMEDIA_OK) s = AMediaCodec_start(codec); + if (s != AMEDIA_OK) { + AMediaCodec_delete(codec); + return nullptr; + } + return codec; + } + + bool configure(const venc_config &c, const int *fds, int nfds) { + release(); + if (c.inputs + c.outputs != (uint32_t)nfds || c.width < 64 || c.height < 64 || c.stride < c.width || + c.rows < c.height || (uint64_t)c.stride * c.rows * 3 / 2 > c.input_size) { + LOG("bad config: %u+%u buffers for %d fds, %ux%u stride %u", c.inputs, c.outputs, nfds, c.width, c.height, + c.stride); + return false; + } + for (int i = 0; i < nfds; i++) { + bool input = (uint32_t)i < c.inputs; + size_t size = input ? c.input_size : c.output_size; + void *p = mmap(nullptr, size, input ? PROT_READ : PROT_READ | PROT_WRITE, MAP_SHARED, fds[i], 0); + if (p == MAP_FAILED) { + LOG("could not map buffer %d: %s", i, strerror(errno)); + release(); + return false; + } + (input ? inputs_ : outputs_).push_back({(uint8_t *)p, size}); + } + codec_ = create(c, true); + if (!codec_) codec_ = create(c, false); + if (!codec_) { + LOG("no %s encoder for %ux%u", c.codec == VENC_HEVC ? "HEVC" : "H.264", c.width, c.height); + release(); + return false; + } + config_ = c; + dst_stride_ = (int32_t)c.width; + dst_rows_ = (int32_t)c.height; + using InputFormat = AMediaFormat *(*)(AMediaCodec *); + static auto input_format = (InputFormat)dlsym(RTLD_DEFAULT, "AMediaCodec_getInputFormat"); + if (AMediaFormat *in = input_format ? input_format(codec_) : nullptr) { + int32_t v; + if (AMediaFormat_getInt32(in, "stride", &v) && v >= (int32_t)c.width) dst_stride_ = v; + if (AMediaFormat_getInt32(in, "slice-height", &v) && v >= (int32_t)c.height) dst_rows_ = v; + AMediaFormat_delete(in); + } + LOG("%s %ux%u at %.2f fps, %u kbit/s %s, input stride %d rows %d", c.codec == VENC_HEVC ? "HEVC" : "H.264", + c.width, c.height, c.fps_num ? (double)c.fps_den / c.fps_num : 0.0, c.bitrate / 1000, + c.vbr ? "VBR" : "CBR", dst_stride_, dst_rows_); + return true; + } + + static void touch(const venc_touch &t) { + using SendTouch = void (*)(int, int, int, int); + static SendTouch send_touch = [] { + void *wayland = dlopen("libdroiddeckwayland.so", RTLD_NOW | RTLD_NOLOAD); + auto fn = wayland ? (SendTouch)dlsym(wayland, "droiddeck_wayland_send_touch") : nullptr; + if (!fn) LOG("remote touch unavailable: %s", dlerror()); + return fn; + }(); + if (!send_touch || t.action > VENC_TOUCH_UP) return; + int x = (int)((uint64_t)std::min(t.x, 65535u) * (kTouchWidth - 1) / 65535); + int y = (int)((uint64_t)std::min(t.y, 65535u) * (kTouchHeight - 1) / 65535); + send_touch((int)t.action, kRemoteTouchId, x, y); + } + + void set_bitrate(uint32_t bitrate) { + if (!codec_) return; + AMediaFormat *p = AMediaFormat_new(); + AMediaFormat_setInt32(p, "video-bitrate", (int32_t)std::max(bitrate, 500000u)); + AMediaCodec_setParameters(codec_, p); + AMediaFormat_delete(p); + } + + bool encode(const venc_frame &f, uint32_t *bytes, bool *key) { + if (!codec_ || f.input >= inputs_.size() || f.output >= outputs_.size()) return false; + if (f.keyframe) { + AMediaFormat *p = AMediaFormat_new(); + AMediaFormat_setInt32(p, "request-sync", 0); + AMediaCodec_setParameters(codec_, p); + AMediaFormat_delete(p); + } + ssize_t in = AMediaCodec_dequeueInputBuffer(codec_, 100000); + if (in < 0) return false; + size_t capacity = 0; + uint8_t *dst = AMediaCodec_getInputBuffer(codec_, (size_t)in, &capacity); + size_t need = (size_t)dst_stride_ * dst_rows_ + (size_t)dst_stride_ * ((config_.height + 1) / 2); + if (!dst || capacity < need) return false; + const uint8_t *src = inputs_[f.input].data; + for (uint32_t y = 0; y < config_.height; y++) + memcpy(dst + (size_t)y * dst_stride_, src + (size_t)y * config_.stride, config_.width); + const uint8_t *src_uv = src + (size_t)config_.stride * config_.rows; + uint8_t *dst_uv = dst + (size_t)dst_stride_ * dst_rows_; + for (uint32_t y = 0; y < (config_.height + 1) / 2; y++) + memcpy(dst_uv + (size_t)y * dst_stride_, src_uv + (size_t)y * config_.stride, config_.width); + if (AMediaCodec_queueInputBuffer(codec_, (size_t)in, 0, need, (uint64_t)std::max(f.pts_us, 0), 0) != + AMEDIA_OK) + return false; + Mapping &out = outputs_[f.output]; + for (int tries = 0; tries < 20; tries++) { + AMediaCodecBufferInfo info; + ssize_t o = AMediaCodec_dequeueOutputBuffer(codec_, &info, 20000); + if (o == AMEDIACODEC_INFO_TRY_AGAIN_LATER || o == AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED || + o == AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED) + continue; + if (o < 0) return false; + size_t size = 0; + uint8_t *data = AMediaCodec_getOutputBuffer(codec_, (size_t)o, &size); + const uint8_t *payload = data ? data + info.offset : nullptr; + size_t length = data ? (size_t)info.size : 0; + if (info.flags & kBufferFlagCodecConfig) { + header_.assign(payload, payload + length); + AMediaCodec_releaseOutputBuffer(codec_, (size_t)o, false); + continue; + } + *key = (info.flags & kBufferFlagKeyFrame) != 0; + bool prepend = *key && !header_.empty() && + (length < header_.size() || memcmp(payload, header_.data(), header_.size()) != 0); + size_t total = length + (prepend ? header_.size() : 0); + bool fits = payload && total <= out.size; + if (fits) { + if (prepend) memcpy(out.data, header_.data(), header_.size()); + memcpy(out.data + (prepend ? header_.size() : 0), payload, length); + *bytes = (uint32_t)total; + } + AMediaCodec_releaseOutputBuffer(codec_, (size_t)o, false); + return fits; + } + return false; + } + + int fd_; + AMediaCodec *codec_ = nullptr; + venc_config config_ = {}; + int32_t dst_stride_ = 0, dst_rows_ = 0; + std::vector inputs_, outputs_; + std::vector header_; +}; + +std::atomic g_listener{-1}; + +void serve(int listener) { + for (;;) { + int fd = accept4(listener, nullptr, nullptr, SOCK_CLOEXEC); + if (fd < 0) { + if (errno == EINTR) continue; + return; + } + std::thread([fd] { + Session s(fd); + s.run(); + }).detach(); + } +} + +} // namespace + +extern "C" JNIEXPORT jboolean JNICALL +Java_com_droiddeck_launcher_session_VideoEncodeBridge_nativeStart(JNIEnv *env, jobject, jstring jpath) { + if (g_listener.load() >= 0) return JNI_TRUE; + const char *path = env->GetStringUTFChars(jpath, nullptr); + sockaddr_un addr = {}; + addr.sun_family = AF_UNIX; + bool fits = strlen(path) < sizeof addr.sun_path; + if (fits) strcpy(addr.sun_path, path); + int fd = fits ? socket(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0) : -1; + if (fd >= 0) { + unlink(path); + if (bind(fd, (sockaddr *)&addr, sizeof addr) != 0 || listen(fd, 4) != 0) { + LOG("could not listen on %s: %s", path, strerror(errno)); + close(fd); + fd = -1; + } + } + env->ReleaseStringUTFChars(jpath, path); + if (fd < 0) return JNI_FALSE; + g_listener.store(fd); + std::thread(serve, fd).detach(); + return JNI_TRUE; +} + +extern "C" JNIEXPORT void JNICALL +Java_com_droiddeck_launcher_session_VideoEncodeBridge_nativeStop(JNIEnv *, jobject) { + int fd = g_listener.exchange(-1); + if (fd >= 0) { + shutdown(fd, SHUT_RDWR); + close(fd); + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt index 2883e799c7..d471f776e6 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt @@ -82,6 +82,7 @@ object SessionFiles { "usr/local/bin/droiddeck-x64-arm-webhelper" to "usr/local/bin/droiddeck-x64-arm-webhelper", "usr/local/bin/droiddeck-x64-webhelper" to "usr/local/bin/droiddeck-x64-webhelper", "usr/local/bin/droiddeck-steamvr" to "usr/local/bin/droiddeck-steamvr", + "usr/local/bin/droiddeck-pipewire" to "usr/local/bin/droiddeck-pipewire", "usr/local/bin/droiddeck-steamvr-settings" to "usr/local/bin/droiddeck-steamvr-settings", "usr/local/bin/droiddeck-steamvr-game" to "usr/local/bin/droiddeck-steamvr-game", "usr/local/bin/droiddeck-bwrap" to "usr/local/bin/droiddeck-bwrap", @@ -137,6 +138,7 @@ object SessionFiles { val optional = (arrayOf( "usr/local/bin/gamescope" to "usr/local/bin/gamescope", "usr/local/lib/droiddeck/uruntime" to "usr/local/lib/droiddeck/uruntime", + "usr/local/lib/droiddeck-pipewire.tzst" to "usr/local/lib/droiddeck-pipewire.tzst", "usr/local/share/licenses/uruntime/LICENSE" to "usr/local/share/licenses/uruntime/LICENSE", ) + mangoapp + msitools.map { it to it } + vr + fexPreloads).filter { (asset, _) -> val dir = asset.substringBeforeLast('/') diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt index f8602dba3f..9dde69517b 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -395,6 +395,12 @@ class SessionService : Service() { val fastPathAt = guest.size val pulse = startAudio(guest, sessionDir) + if (steamHere) { + val encoder = VideoEncodeBridge(File(filesDir, "venc/venc.sock")) + encoder.attach(this) + components.add(encoder) + guest.add("BL_VENC_SOCKET=" + encoder.socket.absolutePath) + } guest.add("BL_WIDTH=" + size.first) guest.add("BL_HEIGHT=" + size.second) diff --git a/app/src/main/java/com/droiddeck/launcher/session/VideoEncodeBridge.kt b/app/src/main/java/com/droiddeck/launcher/session/VideoEncodeBridge.kt new file mode 100644 index 0000000000..8e4b247307 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/session/VideoEncodeBridge.kt @@ -0,0 +1,28 @@ +package com.droiddeck.launcher.session + +import android.util.Log +import com.droiddeck.launcher.core.SessionPart +import java.io.File + +/** Steam's Remote Play V4L2 encoder in the guest talks to this MediaCodec encoder (venc.c, venc_server.cpp). */ +class VideoEncodeBridge(val socket: File) : SessionPart() { + override fun start() { + socket.parentFile?.mkdirs() + val ok = runCatching { System.loadLibrary("ddvenc"); nativeStart(socket.absolutePath) } + .onFailure { Log.w(TAG, "hardware video encoding unavailable", it) } + .getOrDefault(false) + Log.i(TAG, if (ok) "Remote Play hardware encoding on at $socket" else "Remote Play hardware encoding off") + } + + override fun stop() { + runCatching { nativeStop() } + socket.delete() + } + + private external fun nativeStart(path: String): Boolean + private external fun nativeStop() + + private companion object { + const val TAG = "VideoEncodeBridge" + } +} diff --git a/tools/build_local.sh b/tools/build_local.sh index 9f6026ab7d..065a98d65d 100755 --- a/tools/build_local.sh +++ b/tools/build_local.sh @@ -278,6 +278,16 @@ docker run --rm --platform linux/amd64 --user "$(id -u):$(id -g)" -v "${repo_roo mkdir -p "${linuxfs_dir}/usr/local/bin" install -m644 "${repo_root}/tools/mangoapp/mangoapp" "${linuxfs_dir}/usr/local/bin/mangoapp" +pw_pkgs="${staging_dir}/pw-pkgs" +mkdir -p "${pw_pkgs}" +while read -r package_sha256 package_url; do + [[ -n "${package_url}" ]] || continue + package_archive=$(cached "${package_sha256}" "$(basename "${package_url}")" \ + bash -c 'curl -fsSL --retry 3 -o "$out" "$0"' "${package_url}") + zstd -dc "${package_archive}" | tar -xf - -C "${pw_pkgs}" +done < <(grep -v '^#' "${repo_root}/tools/pipewire/packages.txt") +"${repo_root}/tools/pipewire/assemble.sh" "${pw_pkgs}" "${linuxfs_dir}/usr/local/lib/droiddeck-pipewire.tzst" + pa_source=${DROIDDECK_PA13_SOURCE_DIR:-"${staging_dir}/pulseaudio-13.0"} if [[ -z "${DROIDDECK_PA13_SOURCE_DIR:-}" ]]; then pa_tarball="${cache_dir}/pulseaudio-13.0.tar.gz" diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-pipewire b/tools/linuxfs/overlay/usr/local/bin/droiddeck-pipewire new file mode 100755 index 0000000000..11ed41fb23 --- /dev/null +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-pipewire @@ -0,0 +1,62 @@ +#!/bin/sh +# Audio stays on the session's PulseAudio, so WirePlumber runs its upstream video-only profile. +set -u + +bundle=/usr/local/lib/droiddeck-pipewire.tzst +dir=/usr/local/lib/droiddeck-pipewire +state=${XDG_RUNTIME_DIR:-/tmp}/droiddeck-pipewire + +client_env() { + echo "export PIPEWIRE_CONFIG_DIR=$dir/share/pipewire PIPEWIRE_MODULE_DIR=$dir/lib/pipewire-0.3 SPA_PLUGIN_DIR=$dir/lib/spa-0.2" +} + +unpack() { + [ -f "$bundle" ] || { echo "droiddeck-pipewire: $bundle is missing" >&2; return 1; } + sum=$(cksum < "$bundle") + [ "$(cat "$dir/.bundle" 2>/dev/null)" = "$sum" ] && return 0 + rm -rf "$dir.new" && mkdir -p "$dir.new" && + zstd -dc "$bundle" | tar -xf - -C "$dir.new" && + printf '%s\n' "$sum" > "$dir.new/.bundle" && + rm -rf "$dir" && mv "$dir.new" "$dir" || + { rm -rf "$dir.new"; echo "droiddeck-pipewire: could not unpack $bundle" >&2; return 1; } +} + +stop() { + for pid in $(cat "$state/pids" 2>/dev/null); do kill "$pid" 2>/dev/null; done + rm -f "$state/pids" +} + +case "${1:-}" in +env) + client_env + ;; +start) + unpack || exit 1 + stop + mkdir -p "$state" + eval "$(client_env)" + export LD_LIBRARY_PATH=$dir/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} + export WIREPLUMBER_MODULE_DIR=$dir/lib/wireplumber-0.5 WIREPLUMBER_CONFIG_DIR=$dir/share/wireplumber \ + WIREPLUMBER_DATA_DIR=$dir/share/wireplumber + socket=${XDG_RUNTIME_DIR:-/tmp}/pipewire-0 + rm -f "$socket" "$socket.lock" "$socket-manager" "$socket-manager.lock" + "$dir/bin/pipewire" < /dev/null & + echo $! > "$state/pids" + i=0 + while [ ! -S "$socket" ]; do + i=$((i + 1)) + [ "$i" -le 50 ] || { echo "droiddeck-pipewire: pipewire did not open $socket" >&2; stop; exit 1; } + sleep 0.1 + done + "$dir/bin/wireplumber" --profile video-only < /dev/null & + echo $! >> "$state/pids" + echo "droiddeck-pipewire: pipewire and wireplumber (video-only) up at $socket" + ;; +stop) + stop + ;; +*) + echo "usage: droiddeck-pipewire env|start|stop" >&2 + exit 2 + ;; +esac diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session index adfa3ba866..39ee3c8a61 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session @@ -134,6 +134,7 @@ export XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config} export XDG_CACHE_HOME=${XDG_CACHE_HOME:-$HOME/.cache} mkdir -p "$XDG_DATA_HOME" "$XDG_CONFIG_HOME" "$XDG_CACHE_HOME" rm -f "${XDG_RUNTIME_DIR:-/run/droiddeck}/droiddeck-steamvr/active" +rm -f "${XDG_RUNTIME_DIR:-/run/droiddeck}/droiddeck-remote-play" /usr/local/bin/droiddeck-steamvr unregister >/dev/null 2>&1 || true if [ "$mode" = lxqt ] || [ "$mode" = desktop ]; then /usr/local/bin/droiddeck-desktop-bookmarks || true @@ -251,8 +252,15 @@ if [ -z "${BL_INSIDE:-}" ]; then fi export BL_INSIDE=1 if [ "$mode" = steam ]; then + if /usr/local/bin/droiddeck-pipewire start; then + eval "$(/usr/local/bin/droiddeck-pipewire env)" + else + echo "== pipewire: not running; Remote Play and game recording have no picture" + fi run_gamescope "${args[@]}" -- "$0" "$mode" "$@" - exit $? + _bl_status=$? + /usr/local/bin/droiddeck-pipewire stop + exit $_bl_status fi exec gamescope "${args[@]}" -- "$0" "$mode" "$@" fi diff --git a/tools/linuxfs/overlay/usr/local/bin/steam-compatibility b/tools/linuxfs/overlay/usr/local/bin/steam-compatibility index 51bf2c287b..81018139f0 100755 --- a/tools/linuxfs/overlay/usr/local/bin/steam-compatibility +++ b/tools/linuxfs/overlay/usr/local/bin/steam-compatibility @@ -262,6 +262,13 @@ bl_directaudio() { echo "droiddeck-proton: DirectAudio skipped for Steam's install-script evaluator" >&2 return 0 fi + streamer=$(cat "${XDG_RUNTIME_DIR:-/tmp}/droiddeck-remote-play" 2>/dev/null) + if [ -n "$streamer" ] && kill -0 "$streamer" 2>/dev/null; then + WINE_AUDIO_DRIVER=pulse + export WINE_AUDIO_DRIVER + echo "droiddeck-proton: a Remote Play client is connected - game audio goes through PulseAudio so the stream carries it" >&2 + return 0 + fi version=$("$base/files/bin-arm64/wine" --version 2>/dev/null) major=${version#wine-} major=${major%%.*} diff --git a/tools/linuxfs/preload/fsync.c b/tools/linuxfs/preload/fsync.c index dfcb06979f..7a4529aa94 100644 --- a/tools/linuxfs/preload/fsync.c +++ b/tools/linuxfs/preload/fsync.c @@ -617,9 +617,13 @@ int shm_unlink(const char *name) { return r; } +int bl_venc_mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off, + void *(*real)(void *, size_t, int, int, int, off_t), void **out) __attribute__((weak, visibility("hidden"))); + void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off) { void *p; if (!fs_real_mmap) fs_real_mmap = (void *(*)(void *, size_t, int, int, int, off_t))dlsym(RTLD_NEXT, "mmap"); + if (bl_venc_mmap && bl_venc_mmap(addr, len, prot, flags, fd, off, fs_real_mmap, &p)) return p; p = fs_real_mmap(addr, len, prot, flags, fd, off); if (p == MAP_FAILED && bl_noexec_retry(prot, flags, fd)) p = fs_real_mmap(addr, len, prot & ~PROT_EXEC, flags, fd, off); if (p != MAP_FAILED && __builtin_expect(fs_fd >= 0, 0)) fs_track(p, len, flags, fd, off); @@ -629,6 +633,8 @@ void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off) { void *mmap64(void *addr, size_t len, int prot, int flags, int fd, off64_t off) { void *p; if (!fs_real_mmap64) fs_real_mmap64 = (void *(*)(void *, size_t, int, int, int, off64_t))dlsym(RTLD_NEXT, "mmap64"); + if (bl_venc_mmap && bl_venc_mmap(addr, len, prot, flags, fd, off, (void *(*)(void *, size_t, int, int, int, off_t))fs_real_mmap64, &p)) + return p; p = fs_real_mmap64(addr, len, prot, flags, fd, off); if (p == MAP_FAILED && bl_noexec_retry(prot, flags, fd)) p = fs_real_mmap64(addr, len, prot & ~PROT_EXEC, flags, fd, off); if (p != MAP_FAILED && __builtin_expect(fs_fd >= 0, 0)) fs_track(p, len, flags, fd, off); diff --git a/tools/linuxfs/preload/netif.c b/tools/linuxfs/preload/netif.c index 0223f0bb68..2fc9ebd42c 100644 --- a/tools/linuxfs/preload/netif.c +++ b/tools/linuxfs/preload/netif.c @@ -406,6 +406,7 @@ int bl_kgsl_poll(int (*real)(int, unsigned long, void *), int fd, unsigned long void *arg, int *rc) __attribute__((visibility("hidden"))); int bl_ntsync_ioctl(int fd, unsigned long request, void *arg, int *rc) __attribute__((visibility("hidden"))); int bl_drm_gem_close(int fd, unsigned long request, void *arg, int *rc) __attribute__((visibility("hidden"))); +int bl_venc_ioctl(int fd, unsigned long request, void *arg, int *rc) __attribute__((visibility("hidden"))); int bl_winebus_rumble_ioctl(int (*real)(int, unsigned long, void *), int fd, unsigned long request, void *arg, void *caller, int *rc) __attribute__((visibility("hidden"))); @@ -419,6 +420,7 @@ int ioctl(int fd, unsigned long request, ...) { int rc; if (bl_ntsync_ioctl(fd, request, arg, &rc)) return rc; + if (bl_venc_ioctl(fd, request, arg, &rc)) return rc; if (bl_drm_gem_close(fd, request, arg, &rc)) return rc; if (real == NULL) real = (ioctl_fn) dlsym(RTLD_NEXT, "ioctl"); if (bl_winebus_rumble_ioctl(real, fd, request, arg, __builtin_return_address(0), &rc)) return rc; diff --git a/tools/linuxfs/preload/ntsync.c b/tools/linuxfs/preload/ntsync.c index 69f40a1457..b920b49f92 100644 --- a/tools/linuxfs/preload/ntsync.c +++ b/tools/linuxfs/preload/ntsync.c @@ -1253,9 +1253,11 @@ ssize_t recvmsg(int fd, struct msghdr *msg, int flags) { } void bl_fsync_fds_closed(unsigned int first, unsigned int last) __attribute__((visibility("hidden"))); +void bl_venc_closed(int fd) __attribute__((visibility("hidden"))); int close(int fd) { if (fd >= 0) bl_fsync_fds_closed((unsigned int)fd, (unsigned int)fd); + bl_venc_closed(fd); uint64_t *t = __atomic_load_n(&ns_fdt, __ATOMIC_ACQUIRE); if (t && fd >= 0 && fd < NS_FDS && __atomic_load_n(&t[fd], __ATOMIC_ACQUIRE)) ns_forget(fd); return ns_close_fd(fd); diff --git a/tools/linuxfs/preload/opens.c b/tools/linuxfs/preload/opens.c index 3d1fdb35af..269f013913 100644 --- a/tools/linuxfs/preload/opens.c +++ b/tools/linuxfs/preload/opens.c @@ -14,6 +14,7 @@ int bl_status_without_tracer(const char *path, int flags, const void *caller) __attribute__((visibility("hidden"))); int bl_writable_retry(int dirfd, const char *path, int flags) __attribute__((visibility("hidden"))); int bl_ntsync_open(const char *path, int flags) __attribute__((visibility("hidden"))); +int bl_venc_open(const char *path, int flags) __attribute__((visibility("hidden"))); int bl_dircache_create(int dirfd, const char *path, int flags, mode_t mode, int (*real)(int, const char *, int, mode_t)) __attribute__((visibility("hidden"))); @@ -53,6 +54,7 @@ int open(const char *path, int flags, ...) { } if ((fd = bl_status_without_tracer(path, flags, __builtin_return_address(0))) >= 0) return fd; if ((fd = bl_ntsync_open(path, flags)) != -2) return fd; + if ((fd = bl_venc_open(path, flags)) != -2) return fd; if (real_open == NULL) real_open = dlsym(RTLD_NEXT, "open"); if (real_open == NULL) { errno = ENOSYS; @@ -78,6 +80,7 @@ int openat(int dirfd, const char *path, int flags, ...) { } if ((fd = bl_status_without_tracer(path, flags, __builtin_return_address(0))) >= 0) return fd; if ((fd = bl_ntsync_open(path, flags)) != -2) return fd; + if ((fd = bl_venc_open(path, flags)) != -2) return fd; if (real_openat == NULL) real_openat = dlsym(RTLD_NEXT, "openat"); if (real_openat == NULL) { errno = ENOSYS; diff --git a/tools/linuxfs/preload/rptouch.c b/tools/linuxfs/preload/rptouch.c new file mode 100644 index 0000000000..3d6f584edc --- /dev/null +++ b/tools/linuxfs/preload/rptouch.c @@ -0,0 +1,89 @@ +/* Steam's Linux Remote Play host has no touch injection: client touches arrive as an XTest mouse (absolute + * motion plus button 1). While a stream holds the encoder, those become touches into gamescope's own touch + * input, as a finger on the device screen would. */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#include "../../../app/src/main/cpp/venc/venc_proto.h" + +int bl_venc_streaming(void) __attribute__((visibility("hidden"))); +int bl_venc_connect(void) __attribute__((visibility("hidden"))); + +static pthread_mutex_t rt_lock = PTHREAD_MUTEX_INITIALIZER; +static int rt_fd = -1, rt_down, rt_x, rt_y, rt_screen; + +static int rt_enabled(void) { + static int enabled = -1; + if (enabled < 0) { + const char *v = getenv("BL_REMOTE_TOUCH"); + enabled = v == NULL || strcmp(v, "0") != 0; + } + return enabled && bl_venc_streaming(); +} + +static uint32_t rt_fraction(int value, int extent) { + if (extent <= 1 || value <= 0) return 0; + if (value >= extent - 1) return 65535; + return (uint32_t)((int64_t)value * 65535 / (extent - 1)); +} + +static void rt_send(void *dpy, uint32_t action) { + static int (*width)(void *, int), (*height)(void *, int); + if (!width) width = (int (*)(void *, int))dlsym(RTLD_DEFAULT, "XDisplayWidth"); + if (!height) height = (int (*)(void *, int))dlsym(RTLD_DEFAULT, "XDisplayHeight"); + if (!width || !height) return; + struct venc_touch m = { + .type = VENC_TOUCH, .action = action, + .x = rt_fraction(rt_x, width(dpy, rt_screen)), .y = rt_fraction(rt_y, height(dpy, rt_screen)), + }; + for (int attempt = 0; attempt < 2; attempt++) { + if (rt_fd < 0) rt_fd = bl_venc_connect(); + if (rt_fd < 0) return; + if (send(rt_fd, &m, sizeof m, MSG_NOSIGNAL) == (ssize_t)sizeof m) return; + close(rt_fd); + rt_fd = -1; + } +} + +int XTestFakeMotionEvent(void *dpy, int screen, int x, int y, unsigned long delay) { + static int (*real)(void *, int, int, int, unsigned long); + if (!real) real = (int (*)(void *, int, int, int, unsigned long))dlsym(RTLD_NEXT, "XTestFakeMotionEvent"); + if (rt_enabled()) { + pthread_mutex_lock(&rt_lock); + rt_x = x; + rt_y = y; + rt_screen = screen < 0 ? 0 : screen; + int down = rt_down; + if (down) rt_send(dpy, VENC_TOUCH_MOVE); + pthread_mutex_unlock(&rt_lock); + if (down) return 1; + } + return real ? real(dpy, screen, x, y, delay) : 0; +} + +int XTestFakeButtonEvent(void *dpy, unsigned int button, int press, unsigned long delay) { + static int (*real)(void *, unsigned int, int, unsigned long); + if (!real) real = (int (*)(void *, unsigned int, int, unsigned long))dlsym(RTLD_NEXT, "XTestFakeButtonEvent"); + if (button == 1) { + pthread_mutex_lock(&rt_lock); + int handled = 0; + if (press && rt_enabled()) { + rt_down = 1; + rt_send(dpy, VENC_TOUCH_DOWN); + handled = 1; + } else if (!press && rt_down) { + rt_down = 0; + rt_send(dpy, VENC_TOUCH_UP); + handled = 1; + } + pthread_mutex_unlock(&rt_lock); + if (handled) return 1; + } + return real ? real(dpy, button, press, delay) : 0; +} diff --git a/tools/linuxfs/preload/venc.c b/tools/linuxfs/preload/venc.c new file mode 100644 index 0000000000..ca610e85c3 --- /dev/null +++ b/tools/linuxfs/preload/venc.c @@ -0,0 +1,633 @@ +/* /dev/video-enc0 for Steam's Remote Play V4L2 encoder stage, backed by the app's MediaCodec encoder. */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../../../app/src/main/cpp/venc/venc_proto.h" + +#ifndef V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR +#define V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR (V4L2_CID_MPEG_BASE + 644) +#endif + +#define VENC_DEVICE "/dev/video-enc0" +#define VENC_INSTANCES 4 +#define VENC_OFFSET_STEP 0x1000000L +#define VENC_CAPTURE_OFFSET (VENC_OFFSET_STEP * 64) + +struct vq_buf { + int memfd; + uint32_t size; +}; + +struct vq_item { + uint32_t index, bytes, flags, sequence; + struct timeval timestamp; +}; + +struct vq { + struct vq_buf bufs[VENC_MAX_BUFFERS]; + uint32_t count; + struct vq_item queued[VENC_MAX_BUFFERS], done[VENC_MAX_BUFFERS]; + uint32_t nqueued, ndone; + int streaming; +}; + +struct venc { + int used, fd; + pid_t owner; + uint32_t width, height, stride, crop_w, crop_h, raw_size; + uint32_t coded_fourcc, coded_size; + uint32_t fps_num, fps_den; + uint32_t bitrate, peak, vbr, gop; + int32_t h264_profile, h264_level, hevc_profile; + int force_key, configured, sent_bitrate; + uint32_t sequence; + struct vq raw, coded; +}; + +static pthread_mutex_t vn_lock = PTHREAD_MUTEX_INITIALIZER; +static struct venc vn_all[VENC_INSTANCES]; +static int vn_open_count; + +static void vn_marker(int streaming) { + const char *dir = getenv("XDG_RUNTIME_DIR"); + char path[512]; + if (dir == NULL || snprintf(path, sizeof path, "%s/droiddeck-remote-play", dir) >= (int)sizeof path) return; + if (!streaming) { + unlink(path); + return; + } + FILE *f = fopen(path, "w"); + if (f == NULL) return; + fprintf(f, "%d\n", (int)getpid()); + fclose(f); +} + +static void vn_log(const char *fmt, ...) __attribute__((format(printf, 1, 2))); +static void vn_log(const char *fmt, ...) { + va_list ap; + char line[256]; + va_start(ap, fmt); + int n = vsnprintf(line, sizeof line - 1, fmt, ap); + va_end(ap); + if (n < 0) return; + if (n > (int)sizeof line - 2) n = sizeof line - 2; + line[n++] = '\n'; + if (write(2, "droiddeck-venc: ", 16) < 0 || write(2, line, n) < 0) return; +} + +static struct venc *vn_find(int fd) { + if (fd < 0 || __atomic_load_n(&vn_open_count, __ATOMIC_ACQUIRE) == 0) return NULL; + for (int i = 0; i < VENC_INSTANCES; i++) if (vn_all[i].used && vn_all[i].fd == fd) return &vn_all[i]; + return NULL; +} + +static int is_raw(uint32_t type) { return type == V4L2_BUF_TYPE_VIDEO_OUTPUT || type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; } +static int is_coded(uint32_t type) { return type == V4L2_BUF_TYPE_VIDEO_CAPTURE || type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; } +static int is_mplane(uint32_t type) { return type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE || type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; } +static struct vq *vn_queue(struct venc *v, uint32_t type) { return is_raw(type) ? &v->raw : is_coded(type) ? &v->coded : NULL; } + +static void vn_sizes(struct venc *v) { + if (v->stride < v->width) v->stride = v->width; + v->raw_size = v->stride * v->height * 3 / 2; + uint32_t coded = v->width * v->height * 3 / 2; + if (coded < 512 * 1024) coded = 512 * 1024; + if (v->coded_size < coded) v->coded_size = coded; +} + +static void vn_free_queue(struct vq *q) { + for (uint32_t i = 0; i < q->count; i++) if (q->bufs[i].memfd >= 0) close(q->bufs[i].memfd); + memset(q, 0, sizeof *q); +} + +__attribute__((visibility("hidden"))) int bl_venc_streaming(void) { + return __atomic_load_n(&vn_open_count, __ATOMIC_ACQUIRE) > 0; +} + +__attribute__((visibility("hidden"))) int bl_venc_connect(void); + +int bl_venc_connect(void) { + const char *path = getenv("BL_VENC_SOCKET"); + if (path == NULL || *path == '\0') return -1; + struct sockaddr_un addr = { .sun_family = AF_UNIX }; + if (strlen(path) >= sizeof addr.sun_path) return -1; + strcpy(addr.sun_path, path); + int fd = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0); + if (fd < 0) return -1; + if (connect(fd, (struct sockaddr *)&addr, sizeof addr) != 0) { + close(fd); + return -1; + } + struct timeval tv = { .tv_sec = 2 }; + setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv); + return fd; +} + +__attribute__((visibility("hidden"))) int bl_venc_open(const char *path, int flags) { + (void)flags; + if (path == NULL || strcmp(path, VENC_DEVICE) != 0) return -2; + int fd = bl_venc_connect(); + if (fd < 0) return -2; + pthread_mutex_lock(&vn_lock); + struct venc *v = NULL; + for (int i = 0; i < VENC_INSTANCES && v == NULL; i++) if (!vn_all[i].used) v = &vn_all[i]; + if (v == NULL) { + pthread_mutex_unlock(&vn_lock); + close(fd); + errno = EBUSY; + return -1; + } + memset(v, 0, sizeof *v); + v->used = 1; + v->fd = fd; + v->owner = getpid(); + v->width = v->crop_w = 1280; + v->height = v->crop_h = 720; + v->coded_fourcc = V4L2_PIX_FMT_H264; + v->fps_num = 1; + v->fps_den = 60; + v->bitrate = 20000000; + v->gop = 0xffff; + vn_sizes(v); + if (__atomic_add_fetch(&vn_open_count, 1, __ATOMIC_RELEASE) == 1) vn_marker(1); + pthread_mutex_unlock(&vn_lock); + vn_log("opened %s as fd %d (MediaCodec through the app)", path, fd); + return fd; +} + +static int vn_send(struct venc *v, const void *msg, size_t len, const int *fds, int nfds) { + struct iovec iov = { .iov_base = (void *)msg, .iov_len = len }; + char control[CMSG_SPACE(sizeof(int) * VENC_MAX_BUFFERS * 2)]; + struct msghdr m = { .msg_iov = &iov, .msg_iovlen = 1 }; + if (nfds > 0) { + memset(control, 0, sizeof control); + m.msg_control = control; + m.msg_controllen = CMSG_SPACE(sizeof(int) * nfds); + struct cmsghdr *c = CMSG_FIRSTHDR(&m); + c->cmsg_level = SOL_SOCKET; + c->cmsg_type = SCM_RIGHTS; + c->cmsg_len = CMSG_LEN(sizeof(int) * nfds); + memcpy(CMSG_DATA(c), fds, sizeof(int) * nfds); + } + return sendmsg(v->fd, &m, MSG_NOSIGNAL) == (ssize_t)len ? 0 : -1; +} + +static int vn_configure(struct venc *v) { + struct venc_config c = { + .type = VENC_CONFIG, + .codec = v->coded_fourcc == V4L2_PIX_FMT_HEVC ? VENC_HEVC : VENC_H264, + .width = v->crop_w, .height = v->crop_h, .stride = v->stride, .rows = v->height, + .fps_num = v->fps_num, .fps_den = v->fps_den, + .bitrate = v->bitrate, .peak_bitrate = v->peak, .vbr = v->vbr, .gop = v->gop, + .profile = v->coded_fourcc == V4L2_PIX_FMT_HEVC ? v->hevc_profile : v->h264_profile, + .level = v->h264_level, + .inputs = v->raw.count, .outputs = v->coded.count, + .input_size = v->raw_size, .output_size = v->coded_size, + }; + int fds[VENC_MAX_BUFFERS * 2], n = 0; + for (uint32_t i = 0; i < v->raw.count; i++) fds[n++] = v->raw.bufs[i].memfd; + for (uint32_t i = 0; i < v->coded.count; i++) fds[n++] = v->coded.bufs[i].memfd; + if (vn_send(v, &c, sizeof c, fds, n) != 0) return -1; + struct venc_done d; + if (recv(v->fd, &d, sizeof d, 0) != (ssize_t)sizeof d || d.type != VENC_DONE || d.status != 0) { + vn_log("the app's encoder refused %ux%u %s", c.width, c.height, c.codec == VENC_HEVC ? "HEVC" : "H.264"); + return -1; + } + vn_log("encoding %ux%u at %u/%u fps, %u kbit/s %s, %s", c.width, c.height, c.fps_den, c.fps_num, + c.bitrate / 1000, c.vbr ? "VBR" : "CBR", c.codec == VENC_HEVC ? "HEVC" : "H.264"); + v->configured = 1; + v->sent_bitrate = (int)v->bitrate; + return 0; +} + +static void vn_pump(struct venc *v) { + while (v->raw.streaming && v->coded.streaming && v->raw.nqueued > 0 && v->coded.nqueued > 0) { + struct vq_item in = v->raw.queued[0], out = v->coded.queued[0]; + memmove(v->raw.queued, v->raw.queued + 1, --v->raw.nqueued * sizeof in); + memmove(v->coded.queued, v->coded.queued + 1, --v->coded.nqueued * sizeof out); + int ok = v->configured || vn_configure(v) == 0; + if (ok && v->sent_bitrate != (int)v->bitrate) { + struct venc_bitrate b = { .type = VENC_BITRATE, .bitrate = v->bitrate }; + if (vn_send(v, &b, sizeof b, NULL, 0) == 0) v->sent_bitrate = (int)v->bitrate; + } + struct venc_frame f = { + .type = VENC_FRAME, .input = in.index, .output = out.index, .bytes = in.bytes, + .keyframe = (uint32_t)v->force_key, + .pts_us = (int64_t)in.timestamp.tv_sec * 1000000 + in.timestamp.tv_usec, + }; + struct venc_done d = { 0 }; + ok = ok && vn_send(v, &f, sizeof f, NULL, 0) == 0 && + recv(v->fd, &d, sizeof d, 0) == (ssize_t)sizeof d && d.type == VENC_DONE && d.status == 0; + if (!ok) vn_log("frame %u was not encoded (%s)", v->sequence, strerror(errno)); + v->force_key = 0; + in.flags = V4L2_BUF_FLAG_DONE; + out.bytes = ok ? d.bytes : 0; + out.flags = V4L2_BUF_FLAG_DONE | (ok ? (d.keyframe ? V4L2_BUF_FLAG_KEYFRAME : V4L2_BUF_FLAG_PFRAME) : V4L2_BUF_FLAG_ERROR); + out.timestamp = in.timestamp; + in.sequence = out.sequence = v->sequence++; + v->raw.done[v->raw.ndone++] = in; + v->coded.done[v->coded.ndone++] = out; + } +} + +static void vn_fill_format(struct venc *v, struct v4l2_format *f) { + if (is_mplane(f->type)) { + struct v4l2_pix_format_mplane *p = &f->fmt.pix_mp; + memset(p, 0, sizeof *p); + p->width = v->width; + p->height = v->height; + p->field = V4L2_FIELD_NONE; + p->num_planes = 1; + p->colorspace = V4L2_COLORSPACE_REC709; + if (is_raw(f->type)) { + p->pixelformat = V4L2_PIX_FMT_NV12; + p->plane_fmt[0].bytesperline = v->stride; + p->plane_fmt[0].sizeimage = v->raw_size; + } else { + p->pixelformat = v->coded_fourcc; + p->plane_fmt[0].sizeimage = v->coded_size; + } + } else { + struct v4l2_pix_format *p = &f->fmt.pix; + memset(p, 0, sizeof *p); + p->width = v->width; + p->height = v->height; + p->field = V4L2_FIELD_NONE; + p->colorspace = V4L2_COLORSPACE_REC709; + p->pixelformat = is_raw(f->type) ? V4L2_PIX_FMT_NV12 : v->coded_fourcc; + p->bytesperline = is_raw(f->type) ? v->stride : 0; + p->sizeimage = is_raw(f->type) ? v->raw_size : v->coded_size; + } +} + +static int vn_set_format(struct venc *v, struct v4l2_format *f, int apply) { + uint32_t w, h, fourcc, stride, size; + if (is_mplane(f->type)) { + w = f->fmt.pix_mp.width; h = f->fmt.pix_mp.height; fourcc = f->fmt.pix_mp.pixelformat; + stride = f->fmt.pix_mp.plane_fmt[0].bytesperline; size = f->fmt.pix_mp.plane_fmt[0].sizeimage; + } else if (is_raw(f->type) || is_coded(f->type)) { + w = f->fmt.pix.width; h = f->fmt.pix.height; fourcc = f->fmt.pix.pixelformat; + stride = f->fmt.pix.bytesperline; size = f->fmt.pix.sizeimage; + } else { + return -EINVAL; + } + struct venc t = *v; + if (w >= 64 && h >= 64 && w <= 4096 && h <= 4096) { + t.width = (w + 1) & ~1u; + t.height = (h + 1) & ~1u; + t.crop_w = t.width; + t.crop_h = t.height; + } + if (is_raw(f->type)) { + t.stride = stride; + } else { + t.coded_fourcc = fourcc == V4L2_PIX_FMT_HEVC ? V4L2_PIX_FMT_HEVC : V4L2_PIX_FMT_H264; + t.coded_size = size; + } + vn_sizes(&t); + if (apply) { + if (t.width != v->width || t.height != v->height || t.coded_fourcc != v->coded_fourcc) v->configured = 0; + *v = t; + } + vn_fill_format(&t, f); + return 0; +} + +static int vn_reqbufs(struct venc *v, struct v4l2_requestbuffers *r) { + struct vq *q = vn_queue(v, r->type); + if (q == NULL) return -EINVAL; + if (r->memory != V4L2_MEMORY_MMAP) return -EINVAL; + vn_free_queue(q); + v->configured = 0; + uint32_t n = r->count > VENC_MAX_BUFFERS ? VENC_MAX_BUFFERS : r->count; + uint32_t size = is_raw(r->type) ? v->raw_size : v->coded_size; + for (uint32_t i = 0; i < n; i++) { + int fd = memfd_create("droiddeck-venc", MFD_CLOEXEC); + if (fd < 0 || ftruncate(fd, size) != 0) { + if (fd >= 0) close(fd); + vn_free_queue(q); + return -ENOMEM; + } + q->bufs[i].memfd = fd; + q->bufs[i].size = size; + q->count = i + 1; + } + r->count = n; +#ifdef V4L2_BUF_CAP_SUPPORTS_MMAP + r->capabilities = V4L2_BUF_CAP_SUPPORTS_MMAP; +#endif + return 0; +} + +static long vn_offset(struct v4l2_buffer *b) { + return (is_raw(b->type) ? 0 : VENC_CAPTURE_OFFSET) + (long)(b->index + 1) * VENC_OFFSET_STEP; +} + +static void vn_describe(struct venc *v, struct v4l2_buffer *b, const struct vq_item *item) { + struct vq *q = vn_queue(v, b->type); + uint32_t size = q->bufs[b->index].size; + b->memory = V4L2_MEMORY_MMAP; + b->field = V4L2_FIELD_NONE; + b->flags = V4L2_BUF_FLAG_MAPPED | (item ? item->flags : 0); + if (item) { + b->timestamp = item->timestamp; + b->sequence = item->sequence; + } + if (is_mplane(b->type)) { + if (b->m.planes == NULL || b->length < 1) return; + b->length = 1; + b->m.planes[0].length = size; + b->m.planes[0].m.mem_offset = (uint32_t)vn_offset(b); + b->m.planes[0].data_offset = 0; + if (item) b->m.planes[0].bytesused = item->bytes; + } else { + b->length = size; + b->m.offset = (uint32_t)vn_offset(b); + if (item) b->bytesused = item->bytes; + } +} + +static int vn_qbuf(struct venc *v, struct v4l2_buffer *b) { + struct vq *q = vn_queue(v, b->type); + if (q == NULL || b->index >= q->count || q->nqueued >= VENC_MAX_BUFFERS) return -EINVAL; + struct vq_item item = { .index = b->index, .timestamp = b->timestamp }; + if (is_raw(b->type)) + item.bytes = is_mplane(b->type) ? (b->m.planes && b->length ? b->m.planes[0].bytesused : 0) : b->bytesused; + if (item.bytes == 0 || item.bytes > q->bufs[b->index].size) item.bytes = q->bufs[b->index].size; + q->queued[q->nqueued++] = item; + vn_describe(v, b, NULL); + b->flags |= V4L2_BUF_FLAG_QUEUED; + vn_pump(v); + return 0; +} + +static int vn_dqbuf(struct venc *v, struct v4l2_buffer *b) { + struct vq *q = vn_queue(v, b->type); + if (q == NULL) return -EINVAL; + if (q->ndone == 0) return -EAGAIN; + struct vq_item item = q->done[0]; + memmove(q->done, q->done + 1, --q->ndone * sizeof item); + b->index = item.index; + vn_describe(v, b, &item); + return 0; +} + +static void vn_set_control(struct venc *v, uint32_t id, int32_t value) { + switch (id) { + case V4L2_CID_MPEG_VIDEO_BITRATE: if (value > 0) v->bitrate = (uint32_t)value; break; + case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: if (value > 0) v->peak = (uint32_t)value; break; + case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: v->vbr = value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR; v->configured = 0; break; + case V4L2_CID_MPEG_VIDEO_GOP_SIZE: v->gop = (uint32_t)value; v->configured = 0; break; + case V4L2_CID_MPEG_VIDEO_H264_PROFILE: v->h264_profile = value; v->configured = 0; break; + case V4L2_CID_MPEG_VIDEO_H264_LEVEL: v->h264_level = value; v->configured = 0; break; + case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE: v->hevc_profile = value; v->configured = 0; break; + case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME: v->force_key = 1; break; + default: break; + } +} + +static int vn_get_control(struct venc *v, uint32_t id, int32_t *value) { + switch (id) { + case V4L2_CID_MPEG_VIDEO_BITRATE: *value = (int32_t)v->bitrate; return 0; + case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: *value = (int32_t)v->peak; return 0; + case V4L2_CID_MPEG_VIDEO_GOP_SIZE: *value = (int32_t)v->gop; return 0; + default: *value = 0; return 0; + } +} + +static int vn_ioctl(struct venc *v, unsigned long req, void *arg) { + switch (req) { + case VIDIOC_QUERYCAP: { + struct v4l2_capability *c = arg; + memset(c, 0, sizeof *c); + strcpy((char *)c->driver, "droiddeck_venc"); + strcpy((char *)c->card, "Android MediaCodec"); + strcpy((char *)c->bus_info, "platform:droiddeck-venc"); + c->version = 0x00060000; + c->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING; + c->capabilities = c->device_caps | V4L2_CAP_DEVICE_CAPS; + return 0; + } + case VIDIOC_ENUM_FMT: { + struct v4l2_fmtdesc *d = arg; + static const uint32_t coded[] = { V4L2_PIX_FMT_H264, V4L2_PIX_FMT_HEVC }; + if (is_raw(d->type) && d->index == 0) { + d->pixelformat = V4L2_PIX_FMT_NV12; + strcpy((char *)d->description, "Y/CbCr 4:2:0"); + d->flags = 0; + } else if (is_coded(d->type) && d->index < 2) { + d->pixelformat = coded[d->index]; + strcpy((char *)d->description, d->index ? "HEVC" : "H.264"); + d->flags = V4L2_FMT_FLAG_COMPRESSED; + } else { + return -EINVAL; + } + return 0; + } + case VIDIOC_ENUM_FRAMESIZES: { + struct v4l2_frmsizeenum *s = arg; + if (s->index != 0) return -EINVAL; + s->type = V4L2_FRMSIZE_TYPE_STEPWISE; + s->stepwise = (struct v4l2_frmsize_stepwise){ 64, 4096, 2, 64, 4096, 2 }; + return 0; + } + case VIDIOC_G_FMT: vn_fill_format(v, arg); return 0; + case VIDIOC_S_FMT: return vn_set_format(v, arg, 1); + case VIDIOC_TRY_FMT: return vn_set_format(v, arg, 0); + case VIDIOC_REQBUFS: return vn_reqbufs(v, arg); + case VIDIOC_QUERYBUF: { + struct v4l2_buffer *b = arg; + struct vq *q = vn_queue(v, b->type); + if (q == NULL || b->index >= q->count) return -EINVAL; + vn_describe(v, b, NULL); + return 0; + } + case VIDIOC_QBUF: return vn_qbuf(v, arg); + case VIDIOC_DQBUF: return vn_dqbuf(v, arg); + case VIDIOC_STREAMON: + case VIDIOC_STREAMOFF: { + struct vq *q = vn_queue(v, *(uint32_t *)arg); + if (q == NULL) return -EINVAL; + q->streaming = req == VIDIOC_STREAMON; + if (!q->streaming) q->nqueued = q->ndone = 0; + vn_pump(v); + return 0; + } + case VIDIOC_S_PARM: + case VIDIOC_G_PARM: { + struct v4l2_streamparm *p = arg; + if (!is_raw(p->type)) return req == VIDIOC_G_PARM ? 0 : -EINVAL; + struct v4l2_fract *t = &p->parm.output.timeperframe; + if (req == VIDIOC_S_PARM && t->numerator > 0 && t->denominator > 0) { + v->fps_num = t->numerator; + v->fps_den = t->denominator; + v->configured = 0; + } + p->parm.output.capability = V4L2_CAP_TIMEPERFRAME; + t->numerator = v->fps_num; + t->denominator = v->fps_den; + return 0; + } + case VIDIOC_S_CTRL: { + struct v4l2_control *c = arg; + vn_set_control(v, c->id, c->value); + return 0; + } + case VIDIOC_G_CTRL: { + struct v4l2_control *c = arg; + return vn_get_control(v, c->id, &c->value); + } + case VIDIOC_S_EXT_CTRLS: + case VIDIOC_TRY_EXT_CTRLS: + case VIDIOC_G_EXT_CTRLS: { + struct v4l2_ext_controls *e = arg; + for (uint32_t i = 0; i < e->count && e->controls; i++) { + if (req == VIDIOC_S_EXT_CTRLS) vn_set_control(v, e->controls[i].id, e->controls[i].value); + else if (req == VIDIOC_G_EXT_CTRLS) { + int32_t value; + vn_get_control(v, e->controls[i].id, &value); + e->controls[i].value = value; + } + } + return 0; + } + case VIDIOC_QUERYCTRL: { + struct v4l2_queryctrl *q = arg; + if (q->id & V4L2_CTRL_FLAG_NEXT_CTRL) return -EINVAL; + q->type = V4L2_CTRL_TYPE_INTEGER; + q->minimum = 0; + q->maximum = 0x7fffffff; + q->step = 1; + q->flags = 0; + return 0; + } + case VIDIOC_S_SELECTION: + case VIDIOC_G_SELECTION: { + struct v4l2_selection *s = arg; + if (req == VIDIOC_S_SELECTION && s->r.width >= 64 && s->r.height >= 64 && + s->r.width <= v->width && s->r.height <= v->height) { + v->crop_w = s->r.width & ~1u; + v->crop_h = s->r.height & ~1u; + v->configured = 0; + } + s->r = (struct v4l2_rect){ 0, 0, v->crop_w, v->crop_h }; + return 0; + } + case VIDIOC_SUBSCRIBE_EVENT: + case VIDIOC_UNSUBSCRIBE_EVENT: + case VIDIOC_ENCODER_CMD: + case VIDIOC_TRY_ENCODER_CMD: + return 0; + case VIDIOC_DQEVENT: + return -ENOENT; + default: { + static unsigned long logged[32]; + int seen = 0; + for (int i = 0; i < 32 && logged[i]; i++) seen |= logged[i] == req; + for (int i = 0; i < 32 && !seen; i++) if (!logged[i]) { logged[i] = req; break; } + if (!seen) vn_log("unhandled ioctl 0x%lx", req); + return -ENOTTY; + } + } +} + +__attribute__((visibility("hidden"))) int bl_venc_ioctl(int fd, unsigned long req, void *arg, int *rc) { + struct venc *v = vn_find(fd); + if (v == NULL) return 0; + pthread_mutex_lock(&vn_lock); + int r = arg ? vn_ioctl(v, req, arg) : -EFAULT; + pthread_mutex_unlock(&vn_lock); + if (r < 0) { + errno = -r; + *rc = -1; + } else { + *rc = 0; + } + return 1; +} + +__attribute__((visibility("hidden"))) int bl_venc_mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off, + void *(*real)(void *, size_t, int, int, int, off_t), void **out) { + struct venc *v = vn_find(fd); + if (v == NULL) return 0; + pthread_mutex_lock(&vn_lock); + struct vq *q = off >= VENC_CAPTURE_OFFSET ? &v->coded : &v->raw; + long index = (off - (off >= VENC_CAPTURE_OFFSET ? VENC_CAPTURE_OFFSET : 0)) / VENC_OFFSET_STEP - 1; + int memfd = index >= 0 && index < (long)q->count ? q->bufs[index].memfd : -1; + pthread_mutex_unlock(&vn_lock); + if (memfd < 0) { + errno = EINVAL; + *out = MAP_FAILED; + } else { + *out = real(addr, len, prot, (flags & ~MAP_PRIVATE) | MAP_SHARED, memfd, 0); + } + return 1; +} + +__attribute__((visibility("hidden"))) void bl_venc_closed(int fd) { + struct venc *v = vn_find(fd); + if (v == NULL) return; + pthread_mutex_lock(&vn_lock); + vn_free_queue(&v->raw); + vn_free_queue(&v->coded); + v->used = 0; + /* A forked child closing its inherited copy must not end the stream for the parent. */ + if (__atomic_sub_fetch(&vn_open_count, 1, __ATOMIC_RELEASE) == 0 && v->owner == getpid()) vn_marker(0); + pthread_mutex_unlock(&vn_lock); +} + +static short vn_ready(struct venc *v) { + pthread_mutex_lock(&vn_lock); + short r = (v->coded.ndone ? POLLIN | POLLRDNORM : 0) | (v->raw.ndone ? POLLOUT | POLLWRNORM : 0); + pthread_mutex_unlock(&vn_lock); + return r; +} + +static int vn_poll(struct pollfd *fds, nfds_t n, int timeout_ms, int (*real)(struct pollfd *, nfds_t, int)) { + int ours = 0, ready = 0; + int saved[64]; + if (n > 64) return real(fds, n, timeout_ms); + for (nfds_t i = 0; i < n; i++) { + saved[i] = fds[i].fd; + struct venc *v = vn_find(fds[i].fd); + if (v == NULL) continue; + ours++; + fds[i].revents = vn_ready(v) & (fds[i].events | POLLERR); + if (fds[i].revents) ready++; + fds[i].fd = -1; + } + if (ours == 0) return real(fds, n, timeout_ms); + if (ready || timeout_ms < 0 || timeout_ms > 100) timeout_ms = ready ? 0 : 100; + short keep[64]; + for (nfds_t i = 0; i < n; i++) keep[i] = fds[i].fd < 0 && saved[i] >= 0 ? fds[i].revents : 0; + int r = n - ours > 0 ? real(fds, n, timeout_ms) : (timeout_ms && !ready ? real(NULL, 0, timeout_ms) : 0); + if (r < 0) r = 0; + for (nfds_t i = 0; i < n; i++) { + if (fds[i].fd < 0 && saved[i] >= 0) fds[i].revents = keep[i]; + fds[i].fd = saved[i]; + } + return r + ready; +} + +int poll(struct pollfd *fds, nfds_t n, int timeout_ms) { + static int (*real)(struct pollfd *, nfds_t, int); + if (!real) real = (int (*)(struct pollfd *, nfds_t, int))dlsym(RTLD_NEXT, "poll"); + if (__atomic_load_n(&vn_open_count, __ATOMIC_ACQUIRE) == 0) return real(fds, n, timeout_ms); + return vn_poll(fds, n, timeout_ms, real); +} diff --git a/tools/pipewire/assemble.sh b/tools/pipewire/assemble.sh new file mode 100755 index 0000000000..a9adb6c26e --- /dev/null +++ b/tools/pipewire/assemble.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +# assemble.sh +# Packs the video side of SteamOS's PipeWire and WirePlumber: no audio plugins, no JACK. +set -euo pipefail +src=$1 +out=$2 +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +b=$work/bundle +mkdir -p "$b/bin" "$b/lib/pipewire-0.3" "$b/lib/spa-0.2" "$b/lib/wireplumber-0.5" "$b/share/licenses" + +for tool in pipewire wireplumber wpctl; do cp "$src/usr/bin/$tool" "$b/bin/$tool"; done +for library in libpipewire-0.3.so.0 libwireplumber-0.5.so.0; do cp -L "$src/usr/lib/$library" "$b/lib/$library"; done +for module in "$src"/usr/lib/pipewire-0.3/libpipewire-module-*.so; do + case ${module##*/} in *jack*) continue ;; esac + cp "$module" "$b/lib/pipewire-0.3/" +done +cp "$src/usr/lib/spa-0.2/libspa.so" "$b/lib/spa-0.2/" +for plugin in support control videoconvert v4l2; do cp -R "$src/usr/lib/spa-0.2/$plugin" "$b/lib/spa-0.2/$plugin"; done +cp "$src"/usr/lib/wireplumber-0.5/*.so "$b/lib/wireplumber-0.5/" +mkdir -p "$b/share/pipewire" +cp "$src/usr/share/pipewire/pipewire.conf" "$src/usr/share/pipewire/client.conf" "$b/share/pipewire/" +cp -R "$src/usr/share/wireplumber" "$b/share/wireplumber" +for package in pipewire libpipewire wireplumber libwireplumber; do + cp -R "$src/usr/share/licenses/$package" "$b/share/licenses/$package" +done + +mkdir -p "$(dirname "$out")" +(cd "$b" && find . -type f | LC_ALL=C sort | tar -cf - -T -) | zstd -19 -q -f -o "$out" diff --git a/tools/pipewire/packages.txt b/tools/pipewire/packages.txt new file mode 100644 index 0000000000..8df78c84d3 --- /dev/null +++ b/tools/pipewire/packages.txt @@ -0,0 +1,4 @@ +55c9705ff5721f10ca18c42bd15c14b0af88cb5352f5400fda04c944a8b224f1 https://holo-packages.steamos.cloud/archlinux-deckard-hotfixes/release/0.4.x/libpipewire-1%3A1.6.8-1.2-aarch64.pkg.tar.zst +8ea2d900326eaafdd2bbbfa47fa04f5d7515dbdcd9ede04213ba149f44f41da0 https://holo-packages.steamos.cloud/archlinux-deckard-hotfixes/release/0.4.x/pipewire-1%3A1.6.8-1.2-aarch64.pkg.tar.zst +900a4c59f87806d85abe200bb014d755f73a16b2ba4fea63d6f32ff64fcf5e34 https://holo-packages.steamos.cloud/archlinux-deckard-hotfixes/release/0.4.x/libwireplumber-0.5.14-1.3-aarch64.pkg.tar.zst +bf56f040cf09832e0a8135b56b0f6d2b9b29e2bddbd35be73d0bebe603eacafb https://holo-packages.steamos.cloud/archlinux-deckard-hotfixes/release/0.4.x/wireplumber-0.5.14-1.3-aarch64.pkg.tar.zst diff --git a/tools/release/runtime_inputs.py b/tools/release/runtime_inputs.py index 6b04616ef1..a0ac10bfdd 100644 --- a/tools/release/runtime_inputs.py +++ b/tools/release/runtime_inputs.py @@ -23,10 +23,11 @@ PACK_NAMES = ['proot', 'directaudio', 'tracking', 'xbox', 'comet'] SOURCE_PATHS = ['app/build.gradle', 'app/src/main/cpp/fakeinput_steam.cpp', 'app/src/main/assets/pulseaudio.tzst', 'tools/linuxfs', 'tools/proot', 'tools/aaudio-sink', 'tools/directaudio', 'tools/gamescope/release.env', 'tools/droiddeck-esync/release.env', - 'tools/mangoapp', 'tools/msitools', 'tools/comet', 'tools/xbox', 'tools/build_local.sh', + 'tools/mangoapp', 'tools/msitools', 'tools/pipewire', 'tools/comet', 'tools/xbox', 'tools/build_local.sh', 'tools/release/runtime_inputs.py', 'app/src/main/assets/desktop-removal', 'tools/quest', 'tools/vr/src', 'tools/vr/driver_droiddeck', 'tools/vr/layer', 'tools/vr/build.sh', 'app/src/main/cpp/waylandcomp/src/headpose_shm.h', + 'app/src/main/cpp/venc/venc_proto.h', 'app/src/main/cpp/waylandcomp/src/spatialpose_shm.h', 'app/src/main/cpp/adrenotools/src/hook/kgsl.h', 'app/src/main/assets/graphics_driver/adrenotools-turnip25.1.0.tzst'] @@ -75,7 +76,8 @@ def required(): for name in ['turnip26.3.0-a6xx', 'turnip26.3.0-quest2']] paths += ['assets/linuxfs/' + name for name in ['libfakeinput.so', 'libblsession.so', 'libblfastpath.so', 'libssbs.so', 'usr/local/bin/droiddeck-clipboard', - 'usr/local/bin/gamescope', 'usr/local/lib/droiddeck/uruntime']] + 'usr/local/bin/gamescope', 'usr/local/lib/droiddeck/uruntime', + 'usr/local/lib/droiddeck-pipewire.tzst']] for arch in ['x86_64', 'i386']: paths += ['assets/linuxfs/' + arch + '/' + lib for lib in ['libfakeinput.so', 'libblsession.so']] paths += ['assets/linuxfs/x86_64/' + lib for lib in diff --git a/tools/tests/test_pipewire_helper.py b/tools/tests/test_pipewire_helper.py new file mode 100644 index 0000000000..e8abd59fcc --- /dev/null +++ b/tools/tests/test_pipewire_helper.py @@ -0,0 +1,90 @@ +import os +import subprocess +import tarfile +import tempfile +import unittest +from pathlib import Path + +SOURCE = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin/droiddeck-pipewire' + + +class PipeWireHelperTest(unittest.TestCase): + def setUp(self): + self.temporary = tempfile.TemporaryDirectory() + self.addCleanup(self.temporary.cleanup) + self.root = Path(self.temporary.name) + self.lib = self.root / 'lib' + self.runtime = self.root / 'run' + self.lib.mkdir() + self.runtime.mkdir() + self.bundle = self.lib / 'droiddeck-pipewire.tzst' + self.dir = self.lib / 'droiddeck-pipewire' + text = SOURCE.read_text().replace('/usr/local/lib/droiddeck-pipewire', str(self.dir)) + self.helper = self.root / 'droiddeck-pipewire' + self.helper.write_text(text) + self.environment = dict(os.environ, XDG_RUNTIME_DIR=str(self.runtime)) + + def pack(self, daemon_body): + tree = self.root / 'tree' + for name, body in (('pipewire', daemon_body), ('wireplumber', 'exec sleep 30')): + path = tree / 'bin' / name + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text('#!/bin/sh\n' + body + '\n') + path.chmod(0o755) + tar = self.root / 'bundle.tar' + with tarfile.open(tar, 'w') as archive: + archive.add(tree, arcname='.') + subprocess.run(['zstd', '-q', '-f', str(tar), '-o', str(self.bundle)], check=True) + + def run_helper(self, *args): + with tempfile.TemporaryFile('w+') as out, tempfile.TemporaryFile('w+') as err: + done = subprocess.run(['sh', str(self.helper), *args], env=self.environment, stdout=out, stderr=err, + text=True, timeout=15) + out.seek(0) + err.seek(0) + return subprocess.CompletedProcess(done.args, done.returncode, out.read(), err.read()) + + def tearDown(self): + self.run_helper('stop') + + def test_env_points_clients_at_the_bundle(self): + out = self.run_helper('env').stdout + for part in ('PIPEWIRE_CONFIG_DIR=%s/share/pipewire', 'PIPEWIRE_MODULE_DIR=%s/lib/pipewire-0.3', + 'SPA_PLUGIN_DIR=%s/lib/spa-0.2'): + self.assertIn(part % self.dir, out) + + def test_start_waits_for_the_socket_and_stop_ends_both_daemons(self): + socket = self.runtime / 'pipewire-0' + self.pack('python3 -c "import socket,sys; s=socket.socket(socket.AF_UNIX); s.bind(sys.argv[1]); ' + 'import time; time.sleep(30)" "$XDG_RUNTIME_DIR/pipewire-0"') + result = self.run_helper('start') + self.assertEqual(result.returncode, 0, result.stderr) + self.assertTrue(socket.is_socket()) + pids = (self.runtime / 'droiddeck-pipewire/pids').read_text().split() + self.assertEqual(len(pids), 2) + self.run_helper('stop') + for pid in pids: + subprocess.run(['sh', '-c', 'for i in 1 2 3 4 5 6 7 8 9 10; do kill -0 %s 2>/dev/null || exit 0; ' + 'sleep 0.1; done; exit 1' % pid], check=True) + + def test_start_fails_cleanly_when_pipewire_never_listens(self): + self.pack('exec sleep 30') + result = self.run_helper('start') + self.assertNotEqual(result.returncode, 0) + self.assertIn('did not open', result.stderr) + self.assertFalse((self.runtime / 'droiddeck-pipewire/pids').exists()) + + def test_unchanged_bundle_is_not_unpacked_again(self): + self.pack('exec sleep 30') + self.run_helper('start') + marker = self.dir / 'kept' + marker.write_text('x') + self.run_helper('start') + self.assertTrue(marker.exists()) + self.pack('exec sleep 31') + self.run_helper('start') + self.assertFalse(marker.exists()) + + +if __name__ == '__main__': + unittest.main() diff --git a/tools/tests/test_venc_emulator.py b/tools/tests/test_venc_emulator.py new file mode 100644 index 0000000000..9fcf1f10de --- /dev/null +++ b/tools/tests/test_venc_emulator.py @@ -0,0 +1,227 @@ +import array +import mmap +import os +import socket +import struct +import subprocess +import tempfile +import threading +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[2] +PROTO = ROOT / 'app/src/main/cpp/venc/venc_proto.h' +VENC = ROOT / 'tools/linuxfs/preload/venc.c' +TOUCH = ROOT / 'tools/linuxfs/preload/rptouch.c' + +DRIVER = r''' +#include +#include +#include +#include +#include + +int bl_venc_open(const char *path, int flags); +int bl_venc_ioctl(int fd, unsigned long req, void *arg, int *rc); +int bl_venc_mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off, + void *(*real)(void *, size_t, int, int, int, off_t), void **out); +void bl_venc_closed(int fd); + +static int fd; +static int call(unsigned long req, void *arg) { + int rc; + if (!bl_venc_ioctl(fd, req, arg, &rc)) return -100; + return rc < 0 ? -errno : 0; +} + +int main(void) { + if (bl_venc_open("/dev/video0", 0) != -2) return 2; + fd = bl_venc_open("/dev/video-enc0", 0); + if (fd < 0) return 3; + struct v4l2_capability cap; + if (call(VIDIOC_QUERYCAP, &cap) || !(cap.device_caps & V4L2_CAP_VIDEO_M2M_MPLANE)) return 4; + struct v4l2_format f = { .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE }; + f.fmt.pix_mp.width = 640; f.fmt.pix_mp.height = 360; f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12; + if (call(VIDIOC_S_FMT, &f) || f.fmt.pix_mp.plane_fmt[0].sizeimage != 640 * 360 * 3 / 2) return 5; + f = (struct v4l2_format){ .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE }; + f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_HEVC; + if (call(VIDIOC_S_FMT, &f) || f.fmt.pix_mp.pixelformat != V4L2_PIX_FMT_HEVC) return 6; + struct v4l2_control c = { V4L2_CID_MPEG_VIDEO_BITRATE, 8000000 }; + if (call(VIDIOC_S_CTRL, &c)) return 7; + unsigned types[2] = { V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE }; + unsigned char *maps[2][2]; + for (int t = 0; t < 2; t++) { + struct v4l2_requestbuffers r = { .count = 2, .type = types[t], .memory = V4L2_MEMORY_MMAP }; + if (call(VIDIOC_REQBUFS, &r) || r.count != 2) return 8; + for (int i = 0; i < 2; i++) { + struct v4l2_plane p; + struct v4l2_buffer b = { .index = i, .type = types[t], .memory = V4L2_MEMORY_MMAP, .length = 1, .m.planes = &p }; + if (call(VIDIOC_QUERYBUF, &b)) return 9; + void *m; + if (!bl_venc_mmap(NULL, p.length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, p.m.mem_offset, mmap, &m) || + m == MAP_FAILED) return 10; + maps[t][i] = m; + if (t == 1 && call(VIDIOC_QBUF, &b)) return 11; + } + if (call(VIDIOC_STREAMON, &types[t])) return 12; + } + struct v4l2_plane cp; + struct v4l2_buffer cb = { .type = types[1], .memory = V4L2_MEMORY_MMAP, .length = 1, .m.planes = &cp }; + if (call(VIDIOC_DQBUF, &cb) != -EAGAIN) return 13; + for (int frame = 0; frame < 3; frame++) { + memset(maps[0][frame % 2], 'a' + frame, 640 * 360); + struct v4l2_plane p = { .bytesused = 640 * 360 * 3 / 2 }; + struct v4l2_buffer b = { .index = frame % 2, .type = types[0], .memory = V4L2_MEMORY_MMAP, .length = 1, .m.planes = &p }; + b.timestamp.tv_sec = frame; + if (frame == 1) { + struct v4l2_control k = { V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME, 1 }; + call(VIDIOC_S_CTRL, &k); + } + if (call(VIDIOC_QBUF, &b)) return 14; + if (call(VIDIOC_DQBUF, &cb)) return 15; + printf("%d %u %d %ld %.*s\n", frame, cp.bytesused, !!(cb.flags & V4L2_BUF_FLAG_KEYFRAME), + (long)cb.timestamp.tv_sec, (int)cp.bytesused, maps[1][cb.index]); + if (call(VIDIOC_DQBUF, &b)) return 16; + if (call(VIDIOC_QBUF, &cb)) return 17; + } + bl_venc_closed(fd); + return 0; +} +''' + +TOUCH_DRIVER = r''' +#include +#include +#include +#include + +int bl_venc_open(const char *path, int flags); +void bl_venc_closed(int fd); +int XTestFakeMotionEvent(void *dpy, int screen, int x, int y, unsigned long delay); +int XTestFakeButtonEvent(void *dpy, unsigned int button, int press, unsigned long delay); +int XDisplayWidth(void *dpy, int screen) { return 1281; } +int XDisplayHeight(void *dpy, int screen) { return 721; } + +int main(void) { + XTestFakeButtonEvent(NULL, 1, 1, 0); + XTestFakeButtonEvent(NULL, 1, 0, 0); + int fd = bl_venc_open("/dev/video-enc0", 0); + if (fd < 0) return 3; + XTestFakeMotionEvent(NULL, 0, 1280, 360, 0); + XTestFakeButtonEvent(NULL, 1, 1, 0); + XTestFakeMotionEvent(NULL, 0, 640, 0, 0); + XTestFakeButtonEvent(NULL, 3, 1, 0); + XTestFakeButtonEvent(NULL, 1, 0, 0); + char marker[512]; + snprintf(marker, sizeof marker, "%s/droiddeck-remote-play", getenv("XDG_RUNTIME_DIR")); + if (access(marker, F_OK) != 0) return 4; + pid_t child = fork(); + if (child == 0) { bl_venc_closed(fd); _exit(access(marker, F_OK) == 0 ? 0 : 1); } + int status; + waitpid(child, &status, 0); + if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) return 5; + bl_venc_closed(fd); + if (access(marker, F_OK) == 0) return 6; + XTestFakeButtonEvent(NULL, 1, 1, 0); + usleep(200000); + return 0; +} +''' + +CONFIG = struct.Struct('<' + 'I' * 12 + 'ii' + 'I' * 4) +FRAME = struct.Struct('