Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
Expand Down Expand Up @@ -154,7 +155,7 @@ public List<String> getScalabilityModes() {
* @return The codec MIME type.
*/
public String getMimeType() {
return mediaType.toString().toLowerCase() + "/" + name;
return mediaType.toString().toLowerCase(Locale.ROOT) + "/" + name;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ void releaseCamera(SharedCamera camera) {
camera.source.dispose();
}

private static VideoCaptureCapability nearest(List<VideoCaptureCapability> capabilities,
static VideoCaptureCapability nearest(List<VideoCaptureCapability> capabilities,
CameraOptions options) {
if (capabilities == null || capabilities.isEmpty()) {
return null;
Expand All @@ -435,11 +435,11 @@ private static VideoCaptureCapability nearest(List<VideoCaptureCapability> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]",
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Loading