client-sdk-flutter icon indicating copy to clipboard operation
client-sdk-flutter copied to clipboard

webrtc screenshare crashes app on Android

Open omofolarin opened this issue 2 years ago • 7 comments

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

  • Flutter version: Flutter (Channel stable, 3.10.3, on macOS 13.3.1 22E772610a darwin-x64, locale en-NG
  • Plugin version:
  • flutter_webrtc: ^0.9.33
  • livekit_client: ^1.3.4
  • OS: Android
  • OS version: Android 13

omofolarin avatar Jun 11 '23 20:06 omofolarin

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?

cloudwebrtc avatar Jun 12 '23 01:06 cloudwebrtc

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.

omofolarin avatar Jun 12 '23 11:06 omofolarin

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?

cloudwebrtc avatar Jun 16 '23 03:06 cloudwebrtc

@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.

omofolarin avatar Jun 16 '23 06:06 omofolarin

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

cloudwebrtc avatar Jun 16 '23 12:06 cloudwebrtc

Screen share crashing in SDK example also.

AlfaizCeras avatar Aug 23 '23 09:08 AlfaizCeras

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
                  ),
);

Goden4you avatar Sep 14 '23 04:09 Goden4you