Skip to content

Commit e8ab31f

Browse files
Merge pull request #15 from KristofferStrube/fix/echo-cancellation-string-capability
Added converter that ignores strings in the echoCancellation array.
2 parents e5e4786 + af60cfe commit e8ab31f

3 files changed

Lines changed: 81 additions & 1 deletion

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
3+
4+
namespace KristofferStrube.Blazor.MediaCaptureStreams.Converters;
5+
6+
/// <summary>
7+
/// This is going to be removed, as it is only here to make sure that strings in the array of echoCancellation options doesn't break the wrapper.
8+
/// </summary>
9+
internal class NullableArrayOfBooleansIgnoringStringsConverter : JsonConverter<bool[]?>
10+
{
11+
public override bool[]? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
12+
{
13+
if (reader.TokenType is JsonTokenType.Null)
14+
{
15+
return null;
16+
}
17+
if (reader.TokenType is not JsonTokenType.StartArray)
18+
{
19+
throw new JsonException($"Tried to deserialize a nullable array of booleans, but encounted a token of type '{reader.TokenType}' which was unexpected. Expected a '{JsonTokenType.Null}' or {JsonTokenType.StartArray}");
20+
}
21+
22+
List<bool> entries = new();
23+
while (true)
24+
{
25+
reader.Read();
26+
if (reader.TokenType is JsonTokenType.EndArray)
27+
{
28+
break;
29+
}
30+
if (reader.TokenType is JsonTokenType.True)
31+
{
32+
entries.Add(true);
33+
}
34+
else if (reader.TokenType is JsonTokenType.False)
35+
{
36+
entries.Add(false);
37+
}
38+
}
39+
return entries.ToArray();
40+
}
41+
42+
public override void Write(Utf8JsonWriter writer, bool[]? value, JsonSerializerOptions options)
43+
{
44+
if (value is null)
45+
{
46+
writer.WriteNullValue();
47+
}
48+
else
49+
{
50+
writer.WriteStartArray();
51+
foreach (bool boolean in value)
52+
{
53+
writer.WriteBooleanValue(boolean);
54+
}
55+
writer.WriteEndArray();
56+
}
57+
}
58+
}

src/KristofferStrube.Blazor.MediaCaptureStreams/MediaStreamTrack/MediaTrackCapabilities.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text.Json.Serialization;
1+
using KristofferStrube.Blazor.MediaCaptureStreams.Converters;
2+
using System.Text.Json.Serialization;
23

34
namespace KristofferStrube.Blazor.MediaCaptureStreams;
45

@@ -69,6 +70,7 @@ public class MediaTrackCapabilities
6970
/// </summary>
7071
[JsonPropertyName("echoCancellation")]
7172
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
73+
[JsonConverter(typeof(NullableArrayOfBooleansIgnoringStringsConverter))]
7274
public bool[]? EchoCancellation { get; set; }
7375

7476
/// <summary>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace KristofferStrube.Blazor.MediaCaptureStreams.IntegrationTests;
2+
3+
public class MediaStreamTrackTests(string browserName) : BlazorTest(browserName)
4+
{
5+
[Test]
6+
public async Task GetCapabilities_ReturnsCapabilities()
7+
{
8+
// Arrange
9+
await using MediaDevices mediaDevices = await MediaDevicesService.GetMediaDevicesAsync();
10+
await using MediaStream mediaStream = await mediaDevices.GetUserMediaAsync(new() { Audio = true });
11+
MediaStreamTrack[] audioTracks = await mediaStream.GetAudioTracksAsync();
12+
MediaStreamTrack audioTrack = audioTracks.Single();
13+
14+
// Act
15+
var capabilities = await audioTrack.GetCapabilitiesAsync();
16+
17+
// Assert
18+
Assert.That(capabilities.EchoCancellation, Is.Not.Null);
19+
}
20+
}

0 commit comments

Comments
 (0)