Skip to content

Commit

Permalink
Merge pull request #10 from KristofferStrube/feature/dispose-of-media…
Browse files Browse the repository at this point in the history
…devices-from-service

Changed so that `MediaDevices` made from service are disposed.
  • Loading branch information
KristofferStrube authored Oct 23, 2024
2 parents 112a15c + 5b6a1da commit 5d3cce3
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ else
{
try
{
var mediaDevices = await MediaDevicesService.GetMediaDevicesAsync();
await using var mediaDevices = await MediaDevicesService.GetMediaDevicesAsync();
var mediaConstraints = new MediaStreamConstraints()
{
Video = new MediaTrackConstraints()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ else
}
videoStreamTrack = null;
frameRate = null;
if (mediaDevices is not null)
await mediaDevices.DisposeAsync();
}

public async ValueTask DisposeAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public MediaDevicesService(IJSRuntime jSRuntime)
public async Task<MediaDevices> GetMediaDevicesAsync()
{
IJSObjectReference jSInstance = await jSRuntime.InvokeAsync<IJSObjectReference>("navigator.mediaDevices.valueOf");
return await MediaDevices.CreateAsync(jSRuntime, jSInstance);
return await MediaDevices.CreateAsync(jSRuntime, jSInstance, new() { DisposesJSReference = true });
}
}
2 changes: 1 addition & 1 deletion tests/IntegrationTests/MediaDevicesNotSupportedTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public async Task GetUserMedia_Throws_NotSupportedErrorException_WhenNotSupporte
// Arrange
AfterRenderAsync = async () =>
{
MediaDevices mediaDevices = await EvaluationContext.MediaDevicesService.GetMediaDevicesAsync();
await using MediaDevices mediaDevices = await EvaluationContext.MediaDevicesService.GetMediaDevicesAsync();
MediaStream mediaStream = await mediaDevices.GetUserMediaAsync(new() { Audio = true });
MediaStreamTrack[] videoTracks = await mediaStream.GetVideoTracksAsync();
return videoTracks.Length;
Expand Down
8 changes: 4 additions & 4 deletions tests/IntegrationTests/MediaDevicesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public async Task GetUserMedia_ReturnsOneAudioTrack_WhenQueryingAudio()
// Arrange
AfterRenderAsync = async () =>
{
MediaDevices mediaDevices = await EvaluationContext.MediaDevicesService.GetMediaDevicesAsync();
await using MediaDevices mediaDevices = await EvaluationContext.MediaDevicesService.GetMediaDevicesAsync();
MediaStream mediaStream = await mediaDevices.GetUserMediaAsync(new() { Audio = true });
MediaStreamTrack[] audioTracks = await mediaStream.GetAudioTracksAsync();
return audioTracks.Length;
Expand All @@ -32,7 +32,7 @@ public async Task GetUserMedia_ReturnsOneVideoTrack_WhenQueryingVideo()
// Arrange
AfterRenderAsync = async () =>
{
MediaDevices mediaDevices = await EvaluationContext.MediaDevicesService.GetMediaDevicesAsync();
await using MediaDevices mediaDevices = await EvaluationContext.MediaDevicesService.GetMediaDevicesAsync();
MediaStream mediaStream = await mediaDevices.GetUserMediaAsync(new() { Video = true });
MediaStreamTrack[] videoTracks = await mediaStream.GetVideoTracksAsync();
return videoTracks.Length;
Expand All @@ -51,7 +51,7 @@ public async Task GetUserMedia_Throws_TypeErrorException_WhenNoConstraintsDefine
// Arrange
AfterRenderAsync = async () =>
{
MediaDevices mediaDevices = await EvaluationContext.MediaDevicesService.GetMediaDevicesAsync();
await using MediaDevices mediaDevices = await EvaluationContext.MediaDevicesService.GetMediaDevicesAsync();
MediaStream mediaStream = await mediaDevices.GetUserMediaAsync(new() { });
MediaStreamTrack[] videoTracks = await mediaStream.GetVideoTracksAsync();
return videoTracks.Length;
Expand All @@ -70,7 +70,7 @@ public async Task GetUserMedia_Throws_OverconstrainedErrorException_WhenOvercons
// Arrange
AfterRenderAsync = async () =>
{
MediaDevices mediaDevices = await EvaluationContext.MediaDevicesService.GetMediaDevicesAsync();
await using MediaDevices mediaDevices = await EvaluationContext.MediaDevicesService.GetMediaDevicesAsync();

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

0 comments on commit 5d3cce3

Please sign in to comment.