blackuy / react-native-twilio-video-webrtc

Twilio Video (WebRTC) for React Native
https://www.twilio.com/docs/video
MIT License
606 stars 403 forks source link

Screen sharing on Android results in multiple video tracks being published. #720

Closed sidthidet-k closed 1 year ago

sidthidet-k commented 1 year ago

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch react-native-twilio-video-webrtc@3.2.1 for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-twilio-video-webrtc/android/src/main/java/com/twiliorn/library/CustomTwilioVideoView.java b/node_modules/react-native-twilio-video-webrtc/android/src/main/java/com/twiliorn/library/CustomTwilioVideoView.java
index 6a4c264..f5fd321 100644
--- a/node_modules/react-native-twilio-video-webrtc/android/src/main/java/com/twiliorn/library/CustomTwilioVideoView.java
+++ b/node_modules/react-native-twilio-video-webrtc/android/src/main/java/com/twiliorn/library/CustomTwilioVideoView.java
@@ -239,6 +239,9 @@ public class CustomTwilioVideoView extends View implements LifecycleEventListene
         @Override
         public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
             Log.d("RNTwilioScreenShare", "Got activity result " + requestCode + " " + resultCode);
+            if (localParticipant == null) {
+                return;
+            }
             super.onActivityResult(activity, requestCode, resultCode, data);
             if (requestCode == REQUEST_MEDIA_PROJECTION) {
                 Log.d("RNTwilioScreenShare", "Request for the screen capture permission");
@@ -795,14 +798,17 @@ public class CustomTwilioVideoView extends View implements LifecycleEventListene

         if (cameraCapturer != null && localVideoTrack != null) {
             localVideoTrack.enable(enabled);
-            publishLocalVideo(enabled);

-            if(!enabled) {
-                localVideoTrack.release();
-                localVideoTrack = null;
-                cameraCapturer = null;
+            if (enabled) {
+                publishLocalVideo(enabled);
             }

+            // if(!enabled) {
+            //     localVideoTrack.release();
+            //     localVideoTrack = null;
+            //     cameraCapturer = null;
+            // }
+
             WritableMap event = new WritableNativeMap();
             event.putBoolean("videoEnabled", enabled);
             pushEvent(CustomTwilioVideoView.this, ON_VIDEO_CHANGED, event);

This issue body was partially generated by patch-package.