Skip to content

Commit 5d3cce3

Browse files
Merge pull request #10 from KristofferStrube/feature/dispose-of-mediadevices-from-service
Changed so that `MediaDevices` made from service are disposed.
2 parents 112a15c + 5b6a1da commit 5d3cce3

File tree

5 files changed

+9
-7
lines changed

5 files changed

+9
-7
lines changed

samples/KristofferStrube.Blazor.MediaCaptureStreams.WasmExample/Pages/Index.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ else
241241
{
242242
try
243243
{
244-
var mediaDevices = await MediaDevicesService.GetMediaDevicesAsync();
244+
await using var mediaDevices = await MediaDevicesService.GetMediaDevicesAsync();
245245
var mediaConstraints = new MediaStreamConstraints()
246246
{
247247
Video = new MediaTrackConstraints()

samples/KristofferStrube.Blazor.MediaCaptureStreams.WasmExample/Pages/Video.razor

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ else
140140
}
141141
videoStreamTrack = null;
142142
frameRate = null;
143+
if (mediaDevices is not null)
144+
await mediaDevices.DisposeAsync();
143145
}
144146

145147
public async ValueTask DisposeAsync()

src/KristofferStrube.Blazor.MediaCaptureStreams/MediaDevicesService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public MediaDevicesService(IJSRuntime jSRuntime)
2020
public async Task<MediaDevices> GetMediaDevicesAsync()
2121
{
2222
IJSObjectReference jSInstance = await jSRuntime.InvokeAsync<IJSObjectReference>("navigator.mediaDevices.valueOf");
23-
return await MediaDevices.CreateAsync(jSRuntime, jSInstance);
23+
return await MediaDevices.CreateAsync(jSRuntime, jSInstance, new() { DisposesJSReference = true });
2424
}
2525
}

tests/IntegrationTests/MediaDevicesNotSupportedTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public async Task GetUserMedia_Throws_NotSupportedErrorException_WhenNotSupporte
1212
// Arrange
1313
AfterRenderAsync = async () =>
1414
{
15-
MediaDevices mediaDevices = await EvaluationContext.MediaDevicesService.GetMediaDevicesAsync();
15+
await using MediaDevices mediaDevices = await EvaluationContext.MediaDevicesService.GetMediaDevicesAsync();
1616
MediaStream mediaStream = await mediaDevices.GetUserMediaAsync(new() { Audio = true });
1717
MediaStreamTrack[] videoTracks = await mediaStream.GetVideoTracksAsync();
1818
return videoTracks.Length;

tests/IntegrationTests/MediaDevicesTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public async Task GetUserMedia_ReturnsOneAudioTrack_WhenQueryingAudio()
1313
// Arrange
1414
AfterRenderAsync = async () =>
1515
{
16-
MediaDevices mediaDevices = await EvaluationContext.MediaDevicesService.GetMediaDevicesAsync();
16+
await using MediaDevices mediaDevices = await EvaluationContext.MediaDevicesService.GetMediaDevicesAsync();
1717
MediaStream mediaStream = await mediaDevices.GetUserMediaAsync(new() { Audio = true });
1818
MediaStreamTrack[] audioTracks = await mediaStream.GetAudioTracksAsync();
1919
return audioTracks.Length;
@@ -32,7 +32,7 @@ public async Task GetUserMedia_ReturnsOneVideoTrack_WhenQueryingVideo()
3232
// Arrange
3333
AfterRenderAsync = async () =>
3434
{
35-
MediaDevices mediaDevices = await EvaluationContext.MediaDevicesService.GetMediaDevicesAsync();
35+
await using MediaDevices mediaDevices = await EvaluationContext.MediaDevicesService.GetMediaDevicesAsync();
3636
MediaStream mediaStream = await mediaDevices.GetUserMediaAsync(new() { Video = true });
3737
MediaStreamTrack[] videoTracks = await mediaStream.GetVideoTracksAsync();
3838
return videoTracks.Length;
@@ -51,7 +51,7 @@ public async Task GetUserMedia_Throws_TypeErrorException_WhenNoConstraintsDefine
5151
// Arrange
5252
AfterRenderAsync = async () =>
5353
{
54-
MediaDevices mediaDevices = await EvaluationContext.MediaDevicesService.GetMediaDevicesAsync();
54+
await using MediaDevices mediaDevices = await EvaluationContext.MediaDevicesService.GetMediaDevicesAsync();
5555
MediaStream mediaStream = await mediaDevices.GetUserMediaAsync(new() { });
5656
MediaStreamTrack[] videoTracks = await mediaStream.GetVideoTracksAsync();
5757
return videoTracks.Length;
@@ -70,7 +70,7 @@ public async Task GetUserMedia_Throws_OverconstrainedErrorException_WhenOvercons
7070
// Arrange
7171
AfterRenderAsync = async () =>
7272
{
73-
MediaDevices mediaDevices = await EvaluationContext.MediaDevicesService.GetMediaDevicesAsync();
73+
await using MediaDevices mediaDevices = await EvaluationContext.MediaDevicesService.GetMediaDevicesAsync();
7474

7575
// Get video track
7676
MediaStream mediaStream = await mediaDevices.GetUserMediaAsync(new() { Video = true });

0 commit comments

Comments
 (0)