Skip to content

Commit

Permalink
Disable device sources when exception occur
Browse files Browse the repository at this point in the history
Catch exceptions when failing to create a device track source in
WebcamSource and MicrophoneSource components, and disable the component
before re-throwing the exception to get Unity to log it. This ensures
that the components are disabled for consistency.

This is a clean-up loosely related to microsoft#335, though the code triggering
that issue has been changed so the issue does not occur anymore.
  • Loading branch information
djee-ms committed Jul 3, 2020
1 parent 2e70259 commit 2d8633a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
17 changes: 14 additions & 3 deletions libs/unity/library/Runtime/Scripts/Media/MicrophoneSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,21 @@ protected async void OnEnable()
{
AutoGainControl = _autoGainControl,
};
Source = await DeviceAudioTrackSource.CreateAsync(initConfig);
if (Source == null)
try
{
Source = await DeviceAudioTrackSource.CreateAsync(initConfig);
if (Source == null)
{
throw new Exception("DeviceAudioTrackSource.CreateAsync() returned a NULL source.");
}
}
catch (Exception ex)
{
throw new Exception("Failed to create microphone audio source.");
// Disable MicrophoneSource
Debug.LogError("Failed to create audio source for MicrophoneSource component; disabling it.");
enabled = false;
// Throw again to log the exception message with a callstack
throw ex;
}

IsStreaming = true;
Expand Down
17 changes: 14 additions & 3 deletions libs/unity/library/Runtime/Scripts/Media/WebcamSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,21 @@ protected async void OnEnable()
enableMrc = EnableMixedRealityCapture,
enableMrcRecordingIndicator = EnableMRCRecordingIndicator
};
Source = await DeviceVideoTrackSource.CreateAsync(deviceConfig);
if (Source == null)
try
{
Source = await DeviceVideoTrackSource.CreateAsync(deviceConfig);
if (Source == null)
{
throw new Exception("DeviceVideoTrackSource.CreateAsync() returned a NULL source.");
}
}
catch (Exception ex)
{
throw new Exception("Failed ot create webcam video source.");
// Disable WebcamSource
Debug.LogError("Failed to create video source for WebcamSource component; disabling it.");
enabled = false;
// Throw again to log the exception message with a callstack
throw ex;
}

IsStreaming = true;
Expand Down

0 comments on commit 2d8633a

Please sign in to comment.