JulianAssmann / flutter_background

A flutter plugin to keep apps running in the background via foreground services. Android only.
https://pub.dev/packages/flutter_background
MIT License
89 stars 52 forks source link

[BUG] java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION #92

Closed iAbdulaziz93 closed 2 months ago

iAbdulaziz93 commented 2 months ago

Describe the bug I'm trying to share the screen on a real device, the below error message is showing without crashing the app, but the share function is not working.

To Reproduce I'm using the share feature that livekit_client package is offering by using flutter_background package. I followed both packages' steps/docs to enable this feature.

Expected behavior The share screen feature is working as expected without issues.

Smartphone:

Logs ``` [+11035 ms] E/RtgSchedIpcFile(26586): RtgSchedIpcFile failed to open /proc/26586/rtg [ ] I/RmeSchedManager(26586): init Rme, version is: v1.0 [ ] E/RtgSchedIpcFile(26586): setCommandByIoctl not opened [ ] D/RtgSched(26586): resetRtgSchedHandle failed enable:0 [ +7 ms] I/VRI[MainActivity](26586): send MSG_WINDOW_FOCUS_CHANGED msg [ +1 ms] I/PointerIcon(26586): Pointer cache recompute key = com.example [ ] D/DecorView(26586): showOrHideHighlightView: hasFocus=false; winMode=1; isMrgNull=true [ +31 ms] I/SurfaceControl(26586): SurfaceControl 0xb400007d6a93c220 [ ] I/SurfaceControl(26586): nativeRelease 0xb400007de00893c0 count: 2 [ ] I/SurfaceControl(26586): ~SurfaceControl 0xb400007de00893c0 [ ] I/SurfaceControl(26586): nativeRelease 0xb400007d7b881540 count: 2 [ ] I/SurfaceControl(26586): ~SurfaceControl 0xb400007d7b881540 [ ] I/SurfaceControl(26586): nativeRelease 0xb400007d7bae1220 count: 2 [ ] I/SurfaceControl(26586): ~SurfaceControl 0xb400007d7bae1220 [ +965 ms] D/ActivityThread(26586): Won't deliver top position change in state=4 [ +7 ms] I/SurfaceControl(26586): SurfaceControl 0xb400007d55839ae0 [ ] I/SurfaceControl(26586): SurfaceControl 0xb400007de008c660 [ ] I/SurfaceControl(26586): SurfaceControl 0xb400007d6a93ff60 [ +3 ms] I/DecorView[](26586): set decor visibility 0 [ +1 ms] I/SurfaceControl(26586): nativeRelease 0xb400007d6a93c220 count: 2 [ ] I/SurfaceControl(26586): ~SurfaceControl 0xb400007d6a93c220 [ +7 ms] I/org.webrtc.Logging(26586): EglBase14Impl: Using OpenGL ES version 2 [ +2 ms] I/BufferQueueConsumer(26586): [](id:67da00000004,api:0,p:-1,c:26586) connect: controlledByApp=true [ +3 ms] E/MethodChannel#FlutterWebRTC.Method(26586): Failed to handle method call [ ] E/MethodChannel#FlutterWebRTC.Method(26586): java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at android.os.Parcel.createExceptionOrNull(Parcel.java:3022) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at android.os.Parcel.createException(Parcel.java:3006) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at android.os.Parcel.readException(Parcel.java:2989) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at android.os.Parcel.readException(Parcel.java:2931) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at android.media.projection.IMediaProjection$Stub$Proxy.start(IMediaProjection.java:268) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at android.media.projection.MediaProjection.(MediaProjection.java:81) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at android.media.projection.MediaProjectionManager.getMediaProjection(MediaProjectionManager.java:134) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at com.cloudwebrtc.webrtc.OrientationAwareScreenCapturer.startCapture(OrientationAwareScreenCapturer.java:123) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at com.cloudwebrtc.webrtc.GetUserMediaImpl.getDisplayMedia(GetUserMediaImpl.java:560) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at com.cloudwebrtc.webrtc.GetUserMediaImpl.getDisplayMedia(GetUserMediaImpl.java:512) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at com.cloudwebrtc.webrtc.MethodCallHandlerImpl.getDisplayMedia(MethodCallHandlerImpl.java:1404) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at com.cloudwebrtc.webrtc.MethodCallHandlerImpl.onMethodCall(MethodCallHandlerImpl.java:634) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(D8$$SyntheticClass:0) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at android.os.Handler.handleCallback(Handler.java:971) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at android.os.Handler.dispatchMessage(Handler.java:107) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at android.os.Looper.loopOnce(Looper.java:206) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at android.os.Looper.loop(Looper.java:296) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at android.app.ActivityThread.main(ActivityThread.java:9205) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at java.lang.reflect.Method.invoke(Native Method) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:591) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1027) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): Caused by: android.os.RemoteException: Remote stack trace: [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at com.android.server.media.projection.MediaProjectionManagerService$MediaProjection.start(MediaProjectionManagerService.java:579) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at android.media.projection.IMediaProjection$Stub.onTransact(IMediaProjection.java:163) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at android.os.Binder.execTransactInternal(Binder.java:1318) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): at android.os.Binder.execTransact(Binder.java:1279) [ ] E/MethodChannel#FlutterWebRTC.Method(26586): callee: null 1782/4379 [ ] E/MethodChannel#FlutterWebRTC.Method(26586): [ ] I/VRI[MainActivity](26586): send MSG_WINDOW_FOCUS_CHANGED msg [ +3 ms] I/SurfaceControl(26586): SurfaceControl 0xb400007d6a93c220 [ ] I/SurfaceControl(26586): nativeRelease 0xb400007d6a93d940 count: 4 [ ] I/SurfaceControl(26586): SurfaceControl 0xb400007d7bae1220 [ ] I/SurfaceControl(26586): SurfaceControl 0xb400007dd6eadb00 [ ] I/SurfaceControl(26586): SurfaceControl 0xb400007d6a93d800 [ ] I/SurfaceControl(26586): nativeRelease 0xb400007d55839ae0 count: 2 [ ] I/SurfaceControl(26586): ~SurfaceControl 0xb400007d55839ae0 [ ] I/SurfaceControl(26586): nativeRelease 0xb400007d6a93ff60 count: 2 [ ] I/SurfaceControl(26586): ~SurfaceControl 0xb400007d6a93ff60 [ ] I/SurfaceControl(26586): nativeRelease 0xb400007de008c660 count: 2 [ ] I/SurfaceControl(26586): ~SurfaceControl 0xb400007de008c660 [ ] I/SurfaceControl(26586): ~SurfaceControl 0xb400007d6a93d940 [ +3 ms] E/RtgSchedIpcFile(26586): RtgSchedIpcFile failed to open /proc/26586/rtg [ ] I/RmeSchedManager(26586): init Rme, version is: v1.0 [ ] E/RtgSchedIpcFile(26586): setCommandByIoctl not opened [ ] D/RtgSched(26586): resetRtgSchedHandle failed enable:1 [ ] I/PointerIcon(26586): Pointer cache recompute key = com.example [ ] D/DecorView(26586): showOrHideHighlightView: hasFocus=true; winMode=1; isMrgNull=true [ ] W/InputMethodManager(26586): startInputReason = 1 ``` ``` [✓] Flutter (Channel stable, 3.24.1, on macOS 14.6.1 23G93 darwin-arm64, locale en-SA) • Flutter version 3.24.1 on channel stable at /Users/abdulaziz/Dev/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 5874a72aa4 (8 days ago), 2024-08-20 16:46:00 -0500 • Engine revision c9b9d5780d • Dart version 3.5.1 • DevTools version 2.37.2 [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/abdulaziz/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 15.4) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 15F31d • CocoaPods version 1.15.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2024.1) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314) [✓] VS Code (version 1.92.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension can be installed from: 🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter [✓] Connected device (4 available) • ELN L09 (mobile) • ADYX9X4205G01117 • android-arm64 • Android 13 (API 33) • macOS (desktop) • macos • darwin-arm64 • macOS 14.6.1 23G93 darwin-arm64 • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin • macOS 14.6.1 23G93 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 128.0.6613.85 ! Error: Browsing on the local area network for ‏iPhone ‏عز. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac. The device must be opted into Developer Mode to connect wirelessly. (code -27) ! Error: Browsing on the local area network for iPad. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac. The device must be opted into Developer Mode to connect wirelessly. (code -27) [✓] Network resources • All expected network resources are available. • No issues found! ```
JulianAssmann commented 2 months ago

This seems to be an issue with your AndroidManifest.xml. Please try adding the required permission to it: https://stackoverflow.com/a/61277020/7686474

iAbdulaziz93 commented 2 months ago

This seems to be an issue with your AndroidManifest.xml. Please try adding the required permission to it: https://stackoverflow.com/a/61277020/7686474

@JulianAssmann I already did this as shown below.

<service
            android:name="de.julianassmann.flutter_background.IsolateHolderService"
            android:enabled="true"
            android:exported="false"
            android:foregroundServiceType="mediaProjection" />
JulianAssmann commented 2 months ago

As per https://github.com/livekit/client-sdk-flutter/issues/542#issuecomment-2203292204, the problem seems to be that the foreground service needs to be started only after receiving for the screenshare permission.

Have you tried the solution proposed in the comment?

   if (lkPlatformIs(PlatformType.android)) {
      // Android specific
      bool hasCapturePermission = await Helper.requestCapturePermission();
      if (!hasCapturePermission) {
        return;
      }

      requestBackgroundPermission([bool isRetry = false]) async {
        // 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);
          }
          if (hasPermissions &&
              !FlutterBackground.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 requestBackgroundPermission();
   }
iAbdulaziz93 commented 2 months ago

Hi, @JulianAssmann.

Sorry for the late reply.

I already checked for permission before proceeding with the screen sharing.

image

Do I need to do as the code above you shared or maybe open an issue with the LiveKit team?

iAbdulaziz93 commented 2 months ago

@JulianAssmann Solved the issue, solution: https://github.com/livekit/client-sdk-flutter/issues/582.