mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-22 06:15:58 +00:00
feat: add immersive Quest SteamVR to the standard app (#910)
This commit is contained in:
1 parent
d21466be6b
commit
2847910f22
73 files changed
+5916
-289
No files matched your search
+103
-1
@@ -5,13 +5,18 @@ from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import base64
|
||||
import gzip
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import struct
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
import uuid
|
||||
import zlib
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
||||
@@ -382,8 +387,101 @@ def input_request(args: argparse.Namespace) -> dict:
|
||||
raise ControlError(f"Unknown input type {kind}", "INVALID_INPUT")
|
||||
|
||||
|
||||
def xr_ppm_to_png(data: bytes, linear: bool = False) -> bytes:
|
||||
header = re.match(rb"P6\n([0-9]+) ([0-9]+)\n255\n", data)
|
||||
if not header:
|
||||
raise ControlError("Invalid XR image header", "XR_CAPTURE_FAILED")
|
||||
width, height = map(int, header.groups())
|
||||
pixels = data[header.end():]
|
||||
if not (0 < width <= 16384 and 0 < height <= 16384) or len(pixels) != width * height * 3:
|
||||
raise ControlError("Invalid XR image dimensions or incomplete pixels", "XR_CAPTURE_FAILED")
|
||||
def chunk(kind: bytes, value: bytes) -> bytes:
|
||||
return struct.pack('>I', len(value)) + kind + value + struct.pack('>I', zlib.crc32(kind + value))
|
||||
stride = width * 3
|
||||
rows = b''.join(b'\0' + pixels[start:start + stride] for start in range(0, len(pixels), stride))
|
||||
transfer = chunk(b'gAMA', struct.pack('>I', 100000)) if linear else chunk(b'sRGB', b'\0')
|
||||
return (b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('>IIBBBBB', width, height, 8, 2, 0, 0, 0)) + transfer +
|
||||
chunk(b'IDAT', zlib.compress(rows)) + chunk(b'IEND', b''))
|
||||
|
||||
|
||||
def xr_capture(adb: str, serial: str, destination: Path, timeout: float) -> dict:
|
||||
if not 0 < timeout <= 120:
|
||||
raise ControlError("XR capture timeout must be between 0 and 120 seconds", "INVALID_TIMEOUT")
|
||||
remote = f'/sdcard/Android/data/{PACKAGE}/files/xr-capture'
|
||||
token = uuid.uuid4().hex
|
||||
with tempfile.TemporaryDirectory() as temporary:
|
||||
request = Path(temporary) / 'request'
|
||||
request.write_text(token, encoding='ascii')
|
||||
pushed = run_adb(adb, serial, 'push', str(request), remote + '.request.tmp')
|
||||
if pushed.returncode:
|
||||
raise ControlError(pushed.stderr.decode(errors='replace'), 'XR_CAPTURE_FAILED')
|
||||
moved = run_adb(adb, serial, 'shell', 'mv', remote + '.request.tmp', remote + '.request')
|
||||
if moved.returncode:
|
||||
raise ControlError(moved.stderr.decode(errors='replace'), 'XR_CAPTURE_FAILED')
|
||||
deadline = time.monotonic() + timeout
|
||||
metadata = None
|
||||
while time.monotonic() < deadline:
|
||||
remaining = deadline - time.monotonic()
|
||||
if remaining <= 0:
|
||||
break
|
||||
try:
|
||||
result = run_adb(adb, serial, 'shell', 'cat', remote + '.json', timeout=remaining)
|
||||
except ControlError as error:
|
||||
if not isinstance(error.__cause__, subprocess.TimeoutExpired):
|
||||
raise
|
||||
break
|
||||
try:
|
||||
answer = json.loads(result.stdout)
|
||||
if answer.get('id') == token:
|
||||
metadata = answer
|
||||
break
|
||||
except (ValueError, AttributeError):
|
||||
pass
|
||||
time.sleep(0.2)
|
||||
if metadata is None:
|
||||
try:
|
||||
run_adb(adb, serial, 'shell', 'rm', '-f', remote + '.request', timeout=1)
|
||||
except ControlError:
|
||||
pass
|
||||
raise ControlError('No fresh XR capture. Enter SteamVR using a build with XR capture support.', 'TIMEOUT')
|
||||
if not metadata.get('ok'):
|
||||
detail = 'No valid XR projection layer is being submitted' if not metadata.get('validLayer') else 'GPU readback or image write failed'
|
||||
raise ControlError(detail, 'XR_CAPTURE_FAILED')
|
||||
images = {}
|
||||
for eye in (('left', 'right', 'source') if metadata.get('sourceImage') else ('left', 'right')):
|
||||
pulled = run_adb(adb, serial, 'exec-out', 'gzip', '-c', remote + '-' + eye + '.ppm', timeout=120)
|
||||
if pulled.returncode:
|
||||
raise ControlError(pulled.stderr.decode(errors='replace'), 'XR_CAPTURE_FAILED')
|
||||
try:
|
||||
pixels = gzip.decompress(pulled.stdout)
|
||||
except (OSError, EOFError, zlib.error) as error:
|
||||
raise ControlError('Incomplete compressed XR image', 'XR_CAPTURE_FAILED') from error
|
||||
images[eye] = xr_ppm_to_png(pixels, linear=eye == 'source' and metadata.get('sourceColorSpace') == 'linear')
|
||||
# Detect overlapping captures before publishing images with another request's metadata.
|
||||
check = run_adb(adb, serial, 'shell', 'cat', remote + '.json')
|
||||
try:
|
||||
unchanged = json.loads(check.stdout).get('id') == token
|
||||
except (ValueError, AttributeError):
|
||||
unchanged = False
|
||||
if not unchanged:
|
||||
raise ControlError('XR capture was replaced by another request; retry', 'XR_CAPTURE_FAILED')
|
||||
try:
|
||||
destination.mkdir(parents=True, exist_ok=True)
|
||||
for eye, image in images.items():
|
||||
(destination / (eye + '.png')).write_bytes(image)
|
||||
(destination / 'capture.json').write_text(json.dumps(metadata, indent=2), encoding='utf-8')
|
||||
except OSError as error:
|
||||
raise ControlError(str(error), 'FILE_IO') from error
|
||||
return {'ok': True, 'command': 'xr-frame', 'deviceSerial': serial, 'capture': metadata,
|
||||
'left': str((destination / 'left.png').resolve()), 'right': str((destination / 'right.png').resolve()),
|
||||
**({'source': str((destination / 'source.png').resolve())} if 'source' in images else {}),
|
||||
'scope': 'App eye textures before headset compositor reprojection; excludes Meta system overlays'}
|
||||
|
||||
|
||||
def execute(args: argparse.Namespace, adb: str, serial: str) -> dict:
|
||||
command = args.command
|
||||
if command == 'xr-frame':
|
||||
return xr_capture(adb, serial, Path(args.path), args.timeout)
|
||||
|
||||
if command == "state":
|
||||
return add_serial(get_state(adb, serial), serial)
|
||||
@@ -717,6 +815,10 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
shot.add_argument("path")
|
||||
shot.add_argument("--display", type=int, help="logical display id (see displays); default the default display")
|
||||
|
||||
xr = commands.add_parser('xr-frame', help='capture both submitted XR eyes and pose metadata')
|
||||
xr.add_argument('path', help='output directory for left.png, right.png and capture.json')
|
||||
xr.add_argument('--timeout', type=float, default=15)
|
||||
|
||||
rec = commands.add_parser("record", help="record the screen to an mp4")
|
||||
rec.add_argument("path")
|
||||
rec.add_argument("--seconds", type=float, default=10)
|
||||
@@ -738,7 +840,7 @@ EXIT_SESSION = {
|
||||
}
|
||||
EXIT_TIMEOUT = {"TIMEOUT", "ARTIFACT_TIMEOUT"}
|
||||
EXIT_FILE = {"NO_SESSION_LOGS", "LOG_PULL_FAILED", "SCREENSHOT_FAILED", "FILE_IO", "RECORD_FAILED", "PUSH_FAILED",
|
||||
"PACING_FAILED"}
|
||||
"PACING_FAILED", "XR_CAPTURE_FAILED"}
|
||||
EXIT_DENIED = {"AGENT_COMMANDS_DISABLED", "PROTECTED_PREFS", "PROTECTED_CONTROL"}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user