Skip to content

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

Trim webcam test and production scaffolding

AS81487cac91dfeb0b
diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts
index b3052a1..13345c8 100644
--- a/android/app/build.gradle.kts
+++ b/android/app/build.gradle.kts
@@ -25,11 +25,6 @@ android {
targetSdk = 37
versionCode = appVersionCode
versionName = appVersion
- manifestPlaceholders["appLabel"] = "@string/app_name"
- if (providers.gradleProperty("nectarlink.hevcTest").orNull == "true") {
- applicationId = "app.nectarlink.hevctest"
- manifestPlaceholders["appLabel"] = "NectarLink HEVC test"
- }
}
// Only the languages the app offers (res/xml/locales_config.xml) go into the APK: a
diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml
index 14d36f9..34a7162 100644
--- a/android/app/src/main/AndroidManifest.xml
+++ b/android/app/src/main/AndroidManifest.xml
@@ -133,7 +133,7 @@
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
- android:label="${appLabel}"
+ android:label="@string/app_name"
android:localeConfig="@xml/locales_config"
android:supportsRtl="true"
android:theme="@style/Theme.Nectarlink">
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/webcam/FrameQuad.kt b/android/app/src/main/kotlin/app/nectarlink/android/webcam/FrameQuad.kt
index 4fac92a..7c59e2b 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/webcam/FrameQuad.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/webcam/FrameQuad.kt
@@ -5,7 +5,7 @@ import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.FloatBuffer
-/** GL-thread-owned, reusable position/UV data for the upright FIT_CENTER image. */
+/** Caches rotated quad vertices for the encoder fit and UI crop. */
internal class FrameQuad(
private val srcWidth: Int,
private val srcHeight: Int,
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/webcam/WebcamPacket.kt b/android/app/src/main/kotlin/app/nectarlink/android/webcam/WebcamPacket.kt
index 6991151..11f348f 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/webcam/WebcamPacket.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/webcam/WebcamPacket.kt
@@ -44,6 +44,6 @@ internal object WebcamPacket {
return end
}
- internal val AVC_AUD_NAL = byteArrayOf(0, 0, 0, 1, 9, 0xF0.toByte())
- internal val HEVC_AUD_NAL = byteArrayOf(0, 0, 0, 1, 0x46, 1, 0x50)
+ private val AVC_AUD_NAL = byteArrayOf(0, 0, 0, 1, 9, 0xF0.toByte())
+ private val HEVC_AUD_NAL = byteArrayOf(0, 0, 0, 1, 0x46, 1, 0x50)
}
diff --git a/android/app/src/test/kotlin/app/nectarlink/android/webcam/WebcamPacketTest.kt b/android/app/src/test/kotlin/app/nectarlink/android/webcam/WebcamPacketTest.kt
index ee13522..2ce0679 100644
--- a/android/app/src/test/kotlin/app/nectarlink/android/webcam/WebcamPacketTest.kt
+++ b/android/app/src/test/kotlin/app/nectarlink/android/webcam/WebcamPacketTest.kt
@@ -7,6 +7,9 @@ import org.junit.Assert.assertEquals
import org.junit.Test
class WebcamPacketTest {
+ private val avcAud = byteArrayOf(0, 0, 0, 1, 9, 0xF0.toByte())
+ private val hevcAud = byteArrayOf(0, 0, 0, 1, 0x46, 1, 0x50)
+
private data class CodecFixture(
val codec: String,
val aud: ByteArray,
@@ -22,7 +25,7 @@ class WebcamPacketTest {
)
val h264Keyframe = byteArrayOf(0, 0, 0, 1, 0x65, 0x55)
assertArrayEquals(
- WebcamPacket.AVC_AUD_NAL + h264Parameters + h264Keyframe,
+ avcAud + h264Parameters + h264Keyframe,
WebcamPacket.frame(ByteBuffer.wrap(h264Keyframe), 0, h264Keyframe.size, h264Parameters, true, "h264"),
)
@@ -33,7 +36,7 @@ class WebcamPacketTest {
)
val hevcKeyframe = byteArrayOf(0, 0, 0, 1, 0x26, 1, 0x55)
assertArrayEquals(
- WebcamPacket.HEVC_AUD_NAL + hevcParameters + hevcKeyframe,
+ hevcAud + hevcParameters + hevcKeyframe,
WebcamPacket.frame(ByteBuffer.wrap(hevcKeyframe), 0, hevcKeyframe.size, hevcParameters, true, "h265"),
)
}
@@ -49,35 +52,61 @@ class WebcamPacketTest {
source.position(2)
val readOnly = source.asReadOnlyBuffer()
val packet = WebcamPacket.frame(readOnly, 7, payload.size, byteArrayOf(), false, "h265")
- assertArrayEquals(WebcamPacket.HEVC_AUD_NAL + payload, packet)
+ assertArrayEquals(hevcAud + payload, packet)
assertEquals(2, readOnly.position())
assertEquals(30, readOnly.limit())
source.put(7, 0x7E)
- assertArrayEquals(WebcamPacket.HEVC_AUD_NAL + payload, packet)
+ assertArrayEquals(hevcAud + payload, packet)
}
@Test
fun `strips only leading AUDs for both codecs including nonzero offsets`() {
for (fixture in listOf(
- CodecFixture("h264", WebcamPacket.AVC_AUD_NAL, byteArrayOf(0, 0, 1, 0x65, 0x22), byteArrayOf(0, 0, 1, 0x67, 0x44)),
- CodecFixture("h265", WebcamPacket.HEVC_AUD_NAL, byteArrayOf(0, 0, 1, 0x26, 1, 0x22), byteArrayOf(0, 0, 1, 0x40, 1, 0x44)),
+ CodecFixture(
+ "h264", avcAud,
+ byteArrayOf(0, 0, 1, 0x65, 0x22),
+ byteArrayOf(0, 0, 1, 0x67, 0x44),
+ ),
+ CodecFixture(
+ "h265", hevcAud,
+ byteArrayOf(0, 0, 1, 0x26, 1, 0x22),
+ byteArrayOf(0, 0, 1, 0x40, 1, 0x44),
+ ),
)) {
val shortAud = fixture.aud.copyOfRange(1, fixture.aud.size)
val source = ByteBuffer.wrap(byteArrayOf(99, 98) + shortAud + fixture.slice + byteArrayOf(97))
assertArrayEquals(
fixture.aud + fixture.parameters + fixture.slice,
WebcamPacket.frame(
- source, 2, shortAud.size + fixture.slice.size,
- fixture.aud + fixture.parameters, true, fixture.codec,
+ source,
+ 2,
+ shortAud.size + fixture.slice.size,
+ fixture.aud + fixture.parameters,
+ true,
+ fixture.codec,
),
)
assertArrayEquals(
fixture.aud + fixture.slice,
- WebcamPacket.frame(source, 2, shortAud.size + fixture.slice.size, fixture.parameters, false, fixture.codec),
+ WebcamPacket.frame(
+ source,
+ 2,
+ shortAud.size + fixture.slice.size,
+ fixture.parameters,
+ false,
+ fixture.codec,
+ ),
)
assertArrayEquals(
fixture.aud,
- WebcamPacket.frame(ByteBuffer.wrap(fixture.aud), 0, fixture.aud.size, byteArrayOf(), false, fixture.codec),
+ WebcamPacket.frame(
+ ByteBuffer.wrap(fixture.aud),
+ 0,
+ fixture.aud.size,
+ byteArrayOf(),
+ false,
+ fixture.codec,
+ ),
)
}
}
@@ -86,8 +115,8 @@ class WebcamPacketTest {
fun `supports empty input and sliced buffers`() {
val slice = byteArrayOf(0, 0, 0, 1, 0x41, 0x55)
val source = ByteBuffer.wrap(byteArrayOf(99, 98) + slice).apply { position(2) }.slice()
- assertArrayEquals(WebcamPacket.AVC_AUD_NAL + slice, WebcamPacket.frame(source, 0, slice.size, byteArrayOf(), false, "h264"))
- assertArrayEquals(WebcamPacket.AVC_AUD_NAL, WebcamPacket.frame(source, source.limit(), 0, byteArrayOf(), false, "h264"))
+ assertArrayEquals(avcAud + slice, WebcamPacket.frame(source, 0, slice.size, byteArrayOf(), false, "h264"))
+ assertArrayEquals(avcAud, WebcamPacket.frame(source, source.limit(), 0, byteArrayOf(), false, "h264"))
}
@Test(expected = IllegalArgumentException::class)
diff --git a/desktop/app/src/webcam.rs b/desktop/app/src/webcam.rs
index a8ec677..ee85bc8 100644
--- a/desktop/app/src/webcam.rs
+++ b/desktop/app/src/webcam.rs
@@ -93,6 +93,8 @@ static HEIGHT: AtomicU32 = AtomicU32::new(720);
static UHD: AtomicBool = AtomicBool::new(true);
static PREVIEW_ACTIVE: AtomicBool = AtomicBool::new(false);
struct PreviewRequests(AtomicU32);
+const HOME_PREVIEW: u32 = 1 << 0;
+const SETTINGS_PREVIEW: u32 = 1 << 1;
impl PreviewRequests {
fn set(&self, bit: u32, requested: bool) -> bool {
let mask = if requested {
@@ -459,8 +461,8 @@ fn refresh_idle_frame() {
/// preview. Window visibility participates in the QML demand expression.
pub fn set_preview_visible(owner: &str, active: bool) {
let bit = match owner {
- "home" => 2,
- "settings" => 4,
+ "home" => HOME_PREVIEW,
+ "settings" => SETTINGS_PREVIEW,
_ => return,
};
set_preview_demand(bit, active);
@@ -895,13 +897,13 @@ mod tests {
#[test]
fn hiding_one_preview_does_not_disable_another_visible_view() {
let requests = PreviewRequests(AtomicU32::new(0));
- assert!(requests.set(2, true));
- assert!(requests.set(4, true));
- assert!(requests.set(2, false));
- assert!(!requests.set(4, false));
- assert!(requests.set(2, true));
+ assert!(requests.set(HOME_PREVIEW, true));
+ assert!(requests.set(SETTINGS_PREVIEW, true));
+ assert!(requests.set(HOME_PREVIEW, false));
+ assert!(!requests.set(SETTINGS_PREVIEW, false));
+ assert!(requests.set(HOME_PREVIEW, true));
requests.clear();
- assert!(!requests.set(4, false));
+ assert!(!requests.set(SETTINGS_PREVIEW, false));
}
#[test]
diff --git a/desktop/app/src/win/h264.rs b/desktop/app/src/win/h264.rs
index 6f50df9..430e02f 100644
--- a/desktop/app/src/win/h264.rs
+++ b/desktop/app/src/win/h264.rs
@@ -763,7 +763,6 @@ mod tests {
}
}
assert_eq!(count, 200);
- assert_eq!(publisher.stats(), Some((200, 0)));
assert!(decoder.staging.is_none(), "publication must not download the decoded frames");
eprintln!("{codec}: published {count} GPU frames with zero CPU downloads");
}
diff --git a/desktop/vcam/src/gpu.rs b/desktop/vcam/src/gpu.rs
index b09f319..cfd6e43 100644
--- a/desktop/vcam/src/gpu.rs
+++ b/desktop/vcam/src/gpu.rs
@@ -235,14 +235,10 @@ impl GpuPublisher {
pub fn disable(&mut self) {
self.unavailable = true;
}
- /// Counts observed by consumers, useful for verifying which allocator path
- /// Windows selected without logging per-frame data or saving camera images.
+ #[cfg(test)]
pub fn stats(&self) -> Option<(u32, u32)> {
self.mapping.as_ref().map(|m| m.control_stats())
}
- pub fn failure_code(&self) -> Option<i32> {
- self.mapping.as_ref().map(|m| m.control_code()).filter(|code| *code != 0)
- }
/// Returns false to request the existing CPU/RGB fallback. No camera client
/// (or an old DLL) is a normal case, not a fatal decoder error.
#[allow(unsafe_code)]
@@ -625,6 +621,7 @@ impl GpuReader {
}
context.Unmap(staging, 0);
self.cpu_frame = Some(d.frame);
+ #[cfg(test)]
self.mapping.count_download();
Ok(())
}
@@ -690,6 +687,7 @@ impl GpuReader {
)
.is_ok()
{
+ #[cfg(test)]
self.mapping.count_gpu_sample();
return Ok(true);
}
@@ -708,6 +706,7 @@ impl GpuReader {
Some(&region),
);
context.Flush();
+ #[cfg(test)]
self.mapping.count_gpu_sample();
return Ok(true);
}
@@ -848,17 +847,12 @@ mod tests {
};
static TEST_ID: AtomicU64 = AtomicU64::new(0);
- fn pair() -> (GpuPublisher, GpuReader) {
- let name = format!(
- "Local\\NectarlinkGpuTest_{}_{}",
- std::process::id(),
- TEST_ID.fetch_add(1, Ordering::Relaxed)
- );
- let mapping = SharedFrameMapping::open_or_create_control(&name).unwrap();
- let mut publisher = GpuPublisher::new();
- publisher.mapping = Some(SharedFrameMapping::open_existing_control(&name).unwrap());
- publisher.mapping_global = true;
- let reader = GpuReader {
+ fn reader_with_mapping(name: &str) -> GpuReader {
+ let mapping = SharedFrameMapping::open_or_create_control(name).unwrap();
+ unsafe {
+ MFStartup(MF_VERSION, MFSTARTUP_NOSOCKET).unwrap();
+ }
+ GpuReader {
mapping,
device: None,
unshareable_device: None,
@@ -872,10 +866,17 @@ mod tests {
scaled_key: None,
scaled: Vec::new(),
scaler: None,
- };
- unsafe {
- MFStartup(MF_VERSION, MFSTARTUP_NOSOCKET).unwrap();
}
+ }
+
+ fn pair() -> (GpuPublisher, GpuReader) {
+ let name = format!(
+ "Local\\NectarlinkGpuTest_{}_{}",
+ std::process::id(),
+ TEST_ID.fetch_add(1, Ordering::Relaxed)
+ );
+ let reader = reader_with_mapping(&name);
+ let publisher = GpuPublisher::new_with_mapping(&name).unwrap();
(publisher, reader)
}
fn texture(device: &ID3D11Device, w: u32, h: u32, y: u8) -> (ID3D11Texture2D, IMFSample) {
@@ -1076,12 +1077,8 @@ mod tests {
#[test]
fn separate_process_opens_and_verifies_shared_gpu_frames() {
- let (publisher, mut reader) = pair();
- // pair uses a unique local mapping; derive its test name independently
- // by allocating a fresh control mapping for this cross-process test.
- drop(publisher);
let name = format!("Local\\NectarlinkGpuCrossProcess_{}", std::process::id());
- reader.mapping = SharedFrameMapping::open_or_create_control(&name).unwrap();
+ let mut reader = reader_with_mapping(&name);
let mut child = std::process::Command::new(std::env::current_exe().unwrap())
.creation_flags(0x08000000)
.args(["--exact", "gpu::tests::cross_process_gpu_producer", "--nocapture"])
diff --git a/desktop/vcam/src/shm.rs b/desktop/vcam/src/shm.rs
index 4118111..a2a9685 100644
--- a/desktop/vcam/src/shm.rs
+++ b/desktop/vcam/src/shm.rs
@@ -144,43 +144,38 @@ fn unix_shm_path(name: &str) -> std::path::PathBuf {
#[cfg(windows)]
impl SharedFrameMapping {
- #[cfg(windows)]
pub(crate) fn control_failed(&self) -> bool {
self.header()._pad.load(Ordering::Acquire) != 0
}
- #[cfg(windows)]
pub(crate) fn fail_control(&self) {
self.header()._pad.store(1, Ordering::Release);
}
- #[cfg(windows)]
pub(crate) fn fail_control_code(&self, code: i32) {
self.header()._pad.store(code as u32, Ordering::Release);
}
- #[cfg(windows)]
pub(crate) fn control_code(&self) -> i32 {
self.header()._pad.load(Ordering::Acquire) as i32
}
- #[cfg(windows)]
+ #[cfg(test)]
pub(crate) fn control_stats(&self) -> (u32, u32) {
(self.header().height.load(Ordering::Relaxed), self.header().stride.load(Ordering::Relaxed))
}
- #[cfg(windows)]
+ #[cfg(test)]
pub(crate) fn count_gpu_sample(&self) {
self.header().height.fetch_add(1, Ordering::Relaxed);
}
- #[cfg(windows)]
+ #[cfg(test)]
pub(crate) fn count_download(&self) {
self.header().stride.fetch_add(1, Ordering::Relaxed);
}
/// Binary control messages use their own mapping name, never the RGB mapping.
/// A busy writer is distinct from idle so GPU readers can retain a snapshot.
- #[cfg(windows)]
#[allow(unsafe_code)]
pub(crate) fn read_control(&self) -> Option<(u64, Vec<u8>)> {
let h = self.header();
@@ -205,7 +200,6 @@ impl SharedFrameMapping {
None
}
- #[cfg(windows)]
#[allow(unsafe_code)]
pub(crate) fn write_control(&self, bytes: &[u8]) {
assert!(bytes.len() <= 256);