From ba242a0d60cc22df78fa4fc367a16493685efc32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Teunis?= Date: Thu, 12 Oct 2023 13:11:16 +0200 Subject: [PATCH 1/2] Make sure that the sample does not crash on an iOS simulator and only add a video track when there is a camera --- .../The49.Twilio.Video.Maui.csproj | 26 +++++----- .../VideoChatPage.xaml.cs | 48 ++++++++++++------- 2 files changed, 45 insertions(+), 29 deletions(-) diff --git a/The49.Twilio.Video.Maui/The49.Twilio.Video.Maui.csproj b/The49.Twilio.Video.Maui/The49.Twilio.Video.Maui.csproj index a00915c..c3dccc4 100644 --- a/The49.Twilio.Video.Maui/The49.Twilio.Video.Maui.csproj +++ b/The49.Twilio.Video.Maui/The49.Twilio.Video.Maui.csproj @@ -33,18 +33,18 @@ - - - True - \ - - - True - \ - - - True - \ - + + + True + \ + + + True + \ + + + True + \ + \ No newline at end of file diff --git a/The49.Twilio.Video.Sample/VideoChatPage.xaml.cs b/The49.Twilio.Video.Sample/VideoChatPage.xaml.cs index e58340a..1e0394d 100644 --- a/The49.Twilio.Video.Sample/VideoChatPage.xaml.cs +++ b/The49.Twilio.Video.Sample/VideoChatPage.xaml.cs @@ -45,21 +45,31 @@ protected async override void OnAppearing() // Create the local audio track _localAudioTrack = LocalAudioTrack.Create(true); - // Capture the video from the camera. We pick the front facing camera for now - var cameraCapturer = CameraCapturer.Create(CameraPosition.Front); + // Check if we have a virtual device. The iOS simulator does not have a camera + if (DeviceInfo.Current.DeviceType == DeviceType.Physical) + { + // Capture the video from the camera. We pick the front facing camera for now + var cameraCapturer = CameraCapturer.Create(CameraPosition.Front); + + // Create the local video track from the camera capturer + _localVideoTrack = LocalVideoTrack.Create(true, cameraCapturer); + + // Display the local video in the video view + _localVideoTrack.AddSink(localVideoView); + + } - // Create the local video track from the camera capturer - _localVideoTrack = LocalVideoTrack.Create(true, cameraCapturer); + // Configure the connection options. Pass the token and the tracks we want to send to remote participants + var builder = new ConnectOptions.Builder(token) + .RoomName(RoomName); - // Display the local video in the video view - _localVideoTrack.AddSink(localVideoView); + if (_localVideoTrack != null) + builder.VideoTracks(new List { _localVideoTrack }); - // Configure the connection options. Pass the token and the tracks we want to send to remote participants - var options = new ConnectOptions.Builder(token) - .RoomName(RoomName) - .VideoTracks(new List { _localVideoTrack }) - .AudioTracks(new List { _localAudioTrack }) - .Build(); + if (_localAudioTrack != null) + builder.AudioTracks(new List { _localAudioTrack }); + + var options = builder.Build(); _room = TwilioVideoService.Default.Connect(options); @@ -71,10 +81,16 @@ protected async override void OnAppearing() protected override void OnDisappearing() { - base.OnDisappearing(); - - _localVideoTrack.Release(); - _localAudioTrack.Release(); + base.OnDisappearing(); + + // remove events so we don't leak anything + _room.ConnectFailure -= OnRoomConnectFailure; + _room.Connected -= OnRoomConnected; + _room.ParticipantConnected -= OnParticipantConnected; + + // null check + _localVideoTrack?.Release(); + _localAudioTrack?.Release(); _room.Disconnect(); } From f9db97c57229e2594a3d121235945d3006e5bce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Teunis?= Date: Thu, 12 Oct 2023 13:24:27 +0200 Subject: [PATCH 2/2] Check for physical device or any Android device for camera capturer --- The49.Twilio.Video.Sample/VideoChatPage.xaml.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/The49.Twilio.Video.Sample/VideoChatPage.xaml.cs b/The49.Twilio.Video.Sample/VideoChatPage.xaml.cs index 1e0394d..87b9843 100644 --- a/The49.Twilio.Video.Sample/VideoChatPage.xaml.cs +++ b/The49.Twilio.Video.Sample/VideoChatPage.xaml.cs @@ -46,7 +46,8 @@ protected async override void OnAppearing() _localAudioTrack = LocalAudioTrack.Create(true); // Check if we have a virtual device. The iOS simulator does not have a camera - if (DeviceInfo.Current.DeviceType == DeviceType.Physical) + if (DeviceInfo.Current.DeviceType == DeviceType.Physical + || DeviceInfo.Current.Platform == DevicePlatform.Android) { // Capture the video from the camera. We pick the front facing camera for now var cameraCapturer = CameraCapturer.Create(CameraPosition.Front); @@ -57,6 +58,11 @@ protected async override void OnAppearing() // Display the local video in the video view _localVideoTrack.AddSink(localVideoView); + } else + { + _ = DisplayAlert("No Camera", + "iOS Simulator does not provide a camera. You will still see incoming video.", + "OK"); } // Configure the connection options. Pass the token and the tracks we want to send to remote participants