From 5b352ca8b5f68ab3e4c21ba6b415be6249f56157 Mon Sep 17 00:00:00 2001 From: Alex Andres Date: Thu, 5 Nov 2026 14:38:47 +0100 Subject: [PATCH 1/2] fix: match AudioCodecInfo hash to equals and avoid camera score overflow --- .../onvoid/webrtc/client/LocalMediaImpl.java | 8 +- .../media/audio/codec/AudioCodecInfo.java | 14 ++- .../webrtc/client/NearestCapabilityTests.java | 55 +++++++++++ .../audio/codec/AudioCodecInfoTests.java | 94 +++++++++++++++++++ 4 files changed, 166 insertions(+), 5 deletions(-) create mode 100644 webrtc/src/test/java/dev/onvoid/webrtc/client/NearestCapabilityTests.java create mode 100644 webrtc/src/test/java/dev/onvoid/webrtc/media/audio/codec/AudioCodecInfoTests.java diff --git a/webrtc/src/main/java/dev/onvoid/webrtc/client/LocalMediaImpl.java b/webrtc/src/main/java/dev/onvoid/webrtc/client/LocalMediaImpl.java index eea46fa33..fbb20616b 100644 --- a/webrtc/src/main/java/dev/onvoid/webrtc/client/LocalMediaImpl.java +++ b/webrtc/src/main/java/dev/onvoid/webrtc/client/LocalMediaImpl.java @@ -422,7 +422,7 @@ void releaseCamera(SharedCamera camera) { camera.source.dispose(); } - private static VideoCaptureCapability nearest(List capabilities, + static VideoCaptureCapability nearest(List capabilities, CameraOptions options) { if (capabilities == null || capabilities.isEmpty()) { return null; @@ -435,11 +435,11 @@ private static VideoCaptureCapability nearest(List capab long score = 0; if (options.width > 0) { - score += 1000L * (Math.abs(capability.width - options.width) - + Math.abs(capability.height - options.height)); + score += 1000L * (Math.abs((long) capability.width - options.width) + + Math.abs((long) capability.height - options.height)); } if (options.frameRate > 0) { - score += Math.abs(capability.frameRate - options.frameRate); + score += Math.abs((long) capability.frameRate - options.frameRate); } if (score < bestScore) { best = capability; diff --git a/webrtc/src/main/java/dev/onvoid/webrtc/media/audio/codec/AudioCodecInfo.java b/webrtc/src/main/java/dev/onvoid/webrtc/media/audio/codec/AudioCodecInfo.java index 93793a776..3b2cfa0b9 100644 --- a/webrtc/src/main/java/dev/onvoid/webrtc/media/audio/codec/AudioCodecInfo.java +++ b/webrtc/src/main/java/dev/onvoid/webrtc/media/audio/codec/AudioCodecInfo.java @@ -16,6 +16,7 @@ package dev.onvoid.webrtc.media.audio.codec; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -104,7 +105,18 @@ public boolean equals(Object obj) { @Override public int hashCode() { - return Objects.hash(name.toLowerCase(), clockRate, channels, parameters); + return Objects.hash(foldedNameHash(), clockRate, channels, parameters); + } + + /* + * Folds each character as equalsIgnoreCase does. Casing the whole name + * would not match it, not even with Locale.ROOT: "I" and the dotless i are + * equal, but keep different lower cases. + */ + private int foldedNameHash() { + return Arrays.hashCode(name.codePoints() + .map(c -> Character.toLowerCase(Character.toUpperCase(c))) + .toArray()); } @Override diff --git a/webrtc/src/test/java/dev/onvoid/webrtc/client/NearestCapabilityTests.java b/webrtc/src/test/java/dev/onvoid/webrtc/client/NearestCapabilityTests.java new file mode 100644 index 000000000..3ee863466 --- /dev/null +++ b/webrtc/src/test/java/dev/onvoid/webrtc/client/NearestCapabilityTests.java @@ -0,0 +1,55 @@ +/* + * Copyright 2026 Alex Andres + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.onvoid.webrtc.client; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.List; + +import dev.onvoid.webrtc.media.video.VideoCaptureCapability; + +import org.junit.jupiter.api.Test; + +class NearestCapabilityTests { + + private static final VideoCaptureCapability SMALL = new VideoCaptureCapability(640, 480, 30); + + private static final VideoCaptureCapability LARGE = new VideoCaptureCapability(1920, 1080, 30); + + + @Test + void picksNearestResolution() { + assertEquals(SMALL, nearest(800, 600, SMALL, LARGE)); + assertEquals(LARGE, nearest(1600, 900, SMALL, LARGE)); + } + + /* + * The width alone is close to overflowing an int, the distance of the small + * format, width and height together, is past it. + */ + @Test + void hugeResolutionPicksNearest() { + assertEquals(LARGE, nearest(Integer.MAX_VALUE, 2160, SMALL, LARGE)); + assertEquals(LARGE, nearest(Integer.MAX_VALUE, 2160, LARGE, SMALL)); + } + + private static VideoCaptureCapability nearest(int width, int height, VideoCaptureCapability... capabilities) { + CameraOptions options = CameraOptions.builder().resolution(width, height).build(); + + return LocalMediaImpl.nearest(List.of(capabilities), options); + } +} diff --git a/webrtc/src/test/java/dev/onvoid/webrtc/media/audio/codec/AudioCodecInfoTests.java b/webrtc/src/test/java/dev/onvoid/webrtc/media/audio/codec/AudioCodecInfoTests.java new file mode 100644 index 000000000..068e8dfb7 --- /dev/null +++ b/webrtc/src/test/java/dev/onvoid/webrtc/media/audio/codec/AudioCodecInfoTests.java @@ -0,0 +1,94 @@ +/* + * Copyright 2026 Alex Andres + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.onvoid.webrtc.media.audio.codec; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.HashSet; +import java.util.Locale; +import java.util.Map; +import java.util.Set; + +import org.junit.jupiter.api.Test; + +class AudioCodecInfoTests { + + @Test + void hashIgnoresTurkishLocale() { + Locale locale = Locale.getDefault(); + + try { + Locale.setDefault(Locale.forLanguageTag("tr-TR")); + + AudioCodecInfo upper = codec("ISAC"); + AudioCodecInfo lower = codec("isac"); + + assertEquals(upper, lower); + assertEquals(upper.hashCode(), lower.hashCode()); + assertTrue(new HashSet<>(Set.of(upper)).contains(lower)); + } + finally { + Locale.setDefault(locale); + } + } + + /* + * Each pair is equal to equalsIgnoreCase, while lower or upper casing the + * whole name tells it apart even with Locale.ROOT. + */ + @Test + void hashMatchesEquals() { + String[][] pairs = { + { "I", "ı" }, // Dotless i. + { "i", "İ" }, // Capital I with a dot. + { "s", "ſ" }, // Long s. + { "k", "K" }, // Kelvin sign. + }; + + for (String[] pair : pairs) { + AudioCodecInfo first = codec(pair[0]); + AudioCodecInfo second = codec(pair[1]); + + assertEquals(first, second, pair[1]); + assertEquals(first.hashCode(), second.hashCode(), pair[1]); + } + } + + /* + * Whether these two are equal depends on the Java version, code points are + * compared from Java 16 on, so that their hash codes have to match anyway. + */ + @Test + void hashFoldsSupplementaryLetters() { + assertEquals(codec("𐐀").hashCode(), codec("𐐨").hashCode()); + } + + @Test + void hashCoversOtherFields() { + AudioCodecInfo codec = codec("opus"); + + assertNotEquals(codec.hashCode(), new AudioCodecInfo("opus", 16000, 2, Map.of()).hashCode()); + assertNotEquals(codec.hashCode(), new AudioCodecInfo("opus", 48000, 1, Map.of()).hashCode()); + assertNotEquals(codec.hashCode(), codec("g722").hashCode()); + } + + private static AudioCodecInfo codec(String name) { + return new AudioCodecInfo(name, 48000, 2, Map.of()); + } +} From 67d7b041c9417477c00251ba16ef73b8450b9ecb Mon Sep 17 00:00:00 2001 From: Alex Andres Date: Thu, 5 Nov 2026 14:42:12 +0100 Subject: [PATCH 2/2] fix: keep codec MIME type and VideoCodecInfo hash independent of the locale --- .../onvoid/webrtc/RTCRtpCodecCapability.java | 3 +- .../media/video/codec/VideoCodecInfo.java | 13 +++- .../webrtc/RTCRtpCodecCapabilityTests.java | 48 ++++++++++++++ .../video/codec/VideoCodecInfoTests.java | 62 +++++++++++++++++++ 4 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 webrtc/src/test/java/dev/onvoid/webrtc/RTCRtpCodecCapabilityTests.java create mode 100644 webrtc/src/test/java/dev/onvoid/webrtc/media/video/codec/VideoCodecInfoTests.java diff --git a/webrtc/src/main/java/dev/onvoid/webrtc/RTCRtpCodecCapability.java b/webrtc/src/main/java/dev/onvoid/webrtc/RTCRtpCodecCapability.java index 800ab6480..52c1888c6 100644 --- a/webrtc/src/main/java/dev/onvoid/webrtc/RTCRtpCodecCapability.java +++ b/webrtc/src/main/java/dev/onvoid/webrtc/RTCRtpCodecCapability.java @@ -21,6 +21,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Locale; import java.util.Map; /** @@ -154,7 +155,7 @@ public List getScalabilityModes() { * @return The codec MIME type. */ public String getMimeType() { - return mediaType.toString().toLowerCase() + "/" + name; + return mediaType.toString().toLowerCase(Locale.ROOT) + "/" + name; } @Override diff --git a/webrtc/src/main/java/dev/onvoid/webrtc/media/video/codec/VideoCodecInfo.java b/webrtc/src/main/java/dev/onvoid/webrtc/media/video/codec/VideoCodecInfo.java index c8179dee9..1ae651d00 100644 --- a/webrtc/src/main/java/dev/onvoid/webrtc/media/video/codec/VideoCodecInfo.java +++ b/webrtc/src/main/java/dev/onvoid/webrtc/media/video/codec/VideoCodecInfo.java @@ -145,10 +145,21 @@ public boolean equals(Object obj) { @Override public int hashCode() { - return Objects.hash(name.toUpperCase(java.util.Locale.ROOT), parameters, + return Objects.hash(foldedNameHash(), parameters, Arrays.hashCode(scalabilityModes)); } + /* + * Folds each character as equalsIgnoreCase does. Casing the whole name + * would not match it, not even with Locale.ROOT: "k" and the Kelvin sign + * are equal, but keep different upper cases. + */ + private int foldedNameHash() { + return Arrays.hashCode(name.codePoints() + .map(c -> Character.toLowerCase(Character.toUpperCase(c))) + .toArray()); + } + @Override public String toString() { return String.format("%s@%d [name=%s, parameters=%s, scalabilityModes=%s]", diff --git a/webrtc/src/test/java/dev/onvoid/webrtc/RTCRtpCodecCapabilityTests.java b/webrtc/src/test/java/dev/onvoid/webrtc/RTCRtpCodecCapabilityTests.java new file mode 100644 index 000000000..194187946 --- /dev/null +++ b/webrtc/src/test/java/dev/onvoid/webrtc/RTCRtpCodecCapabilityTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 2026 Alex Andres + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.onvoid.webrtc; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Locale; +import java.util.Map; + +import dev.onvoid.webrtc.media.MediaType; + +import org.junit.jupiter.api.Test; + +class RTCRtpCodecCapabilityTests { + + @Test + void mimeTypeIgnoresTurkishLocale() { + Locale locale = Locale.getDefault(); + + try { + Locale.setDefault(Locale.forLanguageTag("tr-TR")); + + assertEquals("video/VP8", capability(MediaType.VIDEO, "VP8").getMimeType()); + assertEquals("audio/opus", capability(MediaType.AUDIO, "opus").getMimeType()); + } + finally { + Locale.setDefault(locale); + } + } + + private static RTCRtpCodecCapability capability(MediaType type, String name) { + return new RTCRtpCodecCapability(type, name, 48000, 2, Map.of()); + } +} diff --git a/webrtc/src/test/java/dev/onvoid/webrtc/media/video/codec/VideoCodecInfoTests.java b/webrtc/src/test/java/dev/onvoid/webrtc/media/video/codec/VideoCodecInfoTests.java new file mode 100644 index 000000000..825d7497a --- /dev/null +++ b/webrtc/src/test/java/dev/onvoid/webrtc/media/video/codec/VideoCodecInfoTests.java @@ -0,0 +1,62 @@ +/* + * Copyright 2026 Alex Andres + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.onvoid.webrtc.media.video.codec; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import java.util.List; +import java.util.Map; + +import org.junit.jupiter.api.Test; + +class VideoCodecInfoTests { + + /* + * Each pair is equal to equalsIgnoreCase, while upper casing the whole name + * tells it apart even with Locale.ROOT. + */ + @Test + void hashMatchesEquals() { + String[][] pairs = { + { "i", "İ" }, // Capital I with a dot. + { "k", "K" }, // Kelvin sign. + }; + + for (String[] pair : pairs) { + VideoCodecInfo first = new VideoCodecInfo(pair[0]); + VideoCodecInfo second = new VideoCodecInfo(pair[1]); + + assertEquals(first, second, pair[1]); + assertEquals(first.hashCode(), second.hashCode(), pair[1]); + } + } + + @Test + void hashIgnoresNameCase() { + assertEquals(new VideoCodecInfo("vp8").hashCode(), new VideoCodecInfo("VP8").hashCode()); + } + + @Test + void hashCoversOtherFields() { + VideoCodecInfo codec = new VideoCodecInfo("VP9"); + + assertNotEquals(codec.hashCode(), new VideoCodecInfo("VP8").hashCode()); + assertNotEquals(codec.hashCode(), new VideoCodecInfo("VP9", Map.of("a", "b")).hashCode()); + assertNotEquals(codec.hashCode(), new VideoCodecInfo("VP9", Map.of(), List.of("L1T3")).hashCode()); + } +}