livekit / client-sdk-flutter

Flutter Client SDK for LiveKit
https://docs.livekit.io
Apache License 2.0
257 stars 127 forks source link

webrtc screenshare crashes app on Android #297

Open omofolarin opened 1 year ago

omofolarin commented 1 year ago

Describe the bug W/System.err( 9765): java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException W/System.err( 9765): at java.util.concurrent.FutureTask.report(FutureTask.java:122) W/System.err( 9765): at java.util.concurrent.FutureTask.get(FutureTask.java:191) W/System.err( 9765): at com.cloudwebrtc.webrtc.SimulcastVideoEncoderFactoryWrapper$StreamEncoderWrapper.initEncode(SimulcastVideoEncoderFactoryWrapper.kt:111) W/System.err( 9765): Caused by: java.lang.IllegalArgumentException W/System.err( 9765): at android.media.MediaCodec.native_configure(Native Method) W/System.err( 9765): at android.media.MediaCodec.configure(MediaCodec.java:2214) W/System.err( 9765): at android.media.MediaCodec.configure(MediaCodec.java:2130) W/System.err( 9765): at org.webrtc.MediaCodecWrapperFactoryImpl$MediaCodecWrapperImpl.configure(MediaCodecWrapperFactoryImpl.java:36) W/System.err( 9765): at org.webrtc.HardwareVideoEncoder.initEncodeInternal(HardwareVideoEncoder.java:273) W/System.err( 9765): at org.webrtc.HardwareVideoEncoder.initEncode(HardwareVideoEncoder.java:231) W/System.err( 9765): at com.cloudwebrtc.webrtc.SimulcastVideoEncoderFactoryWrapper$StreamEncoderWrapper.initEncode$lambda$0(SimulcastVideoEncoderFactoryWrapper.kt:109) W/System.err( 9765): at com.cloudwebrtc.webrtc.SimulcastVideoEncoderFactoryWrapper$StreamEncoderWrapper.$r8$lambda$4_8OOhnixrzHpnCh49psWgv_jfs(Unknown Source:0) W/System.err( 9765): at com.cloudwebrtc.webrtc.SimulcastVideoEncoderFactoryWrapper$StreamEncoderWrapper$$ExternalSyntheticLambda10.call(Unknown Source:6) W/System.err( 9765): at java.util.concurrent.FutureTask.run(FutureTask.java:264) W/System.err( 9765): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137) W/System.err( 9765): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637) W/System.err( 9765): at java.lang.Thread.run(Thread.java:1012) E/rtc ( 9765): # To Reproduce

Expected behavior

    // Required for android screenshare.
        try {
          bool hasPermissions = await FlutterBackground.hasPermissions;
          if (!isRetry) {
            const androidConfig = FlutterBackgroundAndroidConfig(
              notificationTitle: 'Screen Sharing',
              notificationText: 'LiveKit Example is sharing the screen.',
              notificationImportance: AndroidNotificationImportance.Default,
              notificationIcon: AndroidResource(
                name: 'livekit_ic_launcher',
                defType: 'mipmap',
              ),
            );
            hasPermissions = await FlutterBackground.initialize(
              androidConfig: androidConfig,
            );
          }

          final isBackgroundExecutionEnabled =
              FlutterBackground.isBackgroundExecutionEnabled;

          if (hasPermissions && !isBackgroundExecutionEnabled) {
            await FlutterBackground.enableBackgroundExecution();
          }
        } catch (e) {
          if (!isRetry) {
            return await Future<void>.delayed(const Duration(seconds: 1),
                () => requestBackgroundPermission(true));
          }
          print('could not publish video: $e');
        }

  await widget.room.localParticipant?.setScreenShareEnabled(
          true,
          captureScreenAudio: true,
        );

Platform information

cloudwebrtc commented 1 year ago

hey @omofolarin, Need some additional info, is this after the first app launch Or it crashes every time? can you tell me the screen resolution of your phone?

omofolarin commented 1 year ago

Hi @cloudwebrtc , I have screen share button. That calls

 await FlutterBackground.initialize(androidConfig: androidConfig);
 await FlutterBackground.enableBackgroundExecution();
  await widget.room.localParticipant?.setScreenShareEnabled(
          true,
          captureScreenAudio: true,
        );

The app crashes every time I press the screen share button. I think it has something to do with the SimulcastVideoEncoderFactoryWrapper.

cloudwebrtc commented 1 year ago

hey @omofolarin, What is the model of the device you are using? I can't seem to reproduce the issue you mentioned, using the Samsung s20u and Xiaomi k40 If I turn off the simulcast button before connecting, will the error still occur?

omofolarin commented 1 year ago

@cloudwebrtc I'm using Redmi Note 12, MIUI Global 14.0.7 Android 13 TKQ1.221114.001 The error occurs only when the simulcast button is turned on for a call. If the simulcast is not used in the call the app doesn't crash.

cloudwebrtc commented 1 year ago

This may be a bug in MediaTek HW Encoder for simulcast support.

Since I don't have the same model of Android device, can you try to use Android Studio to load your Flutter project and locate the breakpoint of the native code when it crashes? Maybe I can help with this issue.

image
AlfaizCeras commented 1 year ago

Screen share crashing in SDK example also.

Goden4you commented 1 year ago

Same on Windows, but without any error. I found a solution. The reason was that maxFrameRate was null instead of default value. Try to add maxFrameRate param to ScreenShareCaptureOptions in setScreenShareEnabled method:

await room.localParticipant?.setScreenShareEnabled(true,
                   screenShareCaptureOptions:  ScreenShareCaptureOptions(
                    sourceId: event.source?.id, // optional, may be useless on mobile
                    maxFrameRate: maxFrameRate, // not NULL value. In my case it was 30
                  ),
);