AgoraIO-Extensions / Agora-Flutter-SDK

Flutter plugin of Agora RTC SDK for Android/iOS/macOS/Windows
https://pub.dev/packages/agora_rtc_engine
MIT License
743 stars 385 forks source link

FOREGROUND_SERVICE_MEDIA_PROJECTION not found in io.agora.rtc2.extensions.MediaProjectionMgr$LocalScreenCaptureAssistantActivity on Android 14 #1733

Closed bhavinb98 closed 4 months ago

bhavinb98 commented 6 months ago

Version of the agora_rtc_engine

6.3.0

Platforms affected

Steps to reproduce

Flutter Version - 3.19.4 Android 14

These permissions have been specified in AndroidManifest

<uses-permission android:name="android.permission.CAPTURE_VIDEO_OUTPUT" />
<uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CAMERA" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION" />
<service
    android:name="com.pravera.flutter_foreground_task.service.ForegroundService"
    android:foregroundServiceType="mediaPlayback|camera|microphone|mediaProjection"
    android:stopWithTask="true"/>

I get this error when starting screen share

Java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1001, result=-1, data=Intent { (has extras) }} to activity {com.sprintstudio.conversations/io.agora.rtc2.extensions.MediaProjectionMgr$LocalScreenCaptureAssistantActivity}: java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION

Expected results

Screen share starts properly

Actual results

Get the above error

Code sample

Code sample ```dart [Paste your code here] ```

Screenshots or Video

Screenshots / Video demonstration [Upload media here]

Logs

Logs ```console [Paste your logs here] ```

Flutter Doctor output

Doctor output ```console [✓] Flutter (Channel stable, 3.19.4, on Microsoft Windows [Version 10.0.22631.3447], locale en-IN) [✓] Windows Version (Installed version of Windows is version 10 or higher) [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) [✓] Chrome - develop for the web [✗] Visual Studio - develop Windows apps ✗ Visual Studio not installed; this is necessary to develop Windows apps. Download at https://visualstudio.microsoft.com/downloads/. Please install the "Desktop development with C++" workload, including all of its default components [!] Android Studio (version 2021.3) ✗ Unable to determine bundled Java version. [✓] Android Studio (version 2023.2) [✓] VS Code (version 1.88.1) [✓] Connected device (3 available) [✓] Network resources ```
littleGnAl commented 6 months ago

I'm so sorry that we're in holiday until May 06, I will take a look this issue after holiday.

littleGnAl commented 6 months ago

I try our example and it works fine, can you check the configurations for reference? https://github.com/AgoraIO-Extensions/Agora-Flutter-SDK/blob/main/example/android/app/src/main/AndroidManifest.xml

I think you can also try removing the mediaProjection from the com.pravera.flutter_foreground_task.service.ForegroundService in your project to see if it works or not.

<service
    android:name="com.pravera.flutter_foreground_task.service.ForegroundService"
-   android:foregroundServiceType="mediaPlayback|camera|microphone|mediaProjection"
+   android:foregroundServiceType="mediaPlayback|camera|microphone"
    android:stopWithTask="true"/>
bhavinb98 commented 6 months ago

I did the changes and get this error when starting screen share -

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1001, result=-1, data=Intent { (has extras) }} to activity {com.sprintstudio.conversations/io.agora.rtc2.extensions.MediaProjectionMgr$LocalScreenCaptureAssistantActivity}: java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
bhavinb98 commented 6 months ago

It seems that I've ran into a weird problem. So I added the mediaProjection service back. I did a flutter clean and ran it again. It always gives me the above error during the first run, where it also crashes after the error. From 2nd run onwards, it runs fine.

I'm using this this plugin to enable foreground service in my app - https://pub.dev/packages/flutter_foreground_task, m not sure if this is the source of the issue. Also created an issue there - https://github.com/Dev-hwang/flutter_foreground_task/issues/212

Another doubt I have -

I try our example and it works fine, can you check the configurations for reference? https://github.com/AgoraIO-Extensions/Agora-Flutter-SDK/blob/main/example/android/app/src/main/AndroidManifest.xml

Android 14 needs foreground service permissions to be explicitly specified, but in your example, I don't see any declared, isn't the mediaProjection service is required for screen sharing?

littleGnAl commented 6 months ago

The permissions are declared inside the SDK https://github.com/AgoraIO-Extensions/Agora-Flutter-SDK/blob/0052cc78446244cfaa4cfa5fff53515256704abf/android/build.gradle#L62, so you do not need to config any permissions for the screen sharing feature on Android by default.

I'm not very sure if it is affected by the flutter_foreground_task package, but I think you can first try running our example on your phone and see if it works or not, if yes, you can try removing the configurations or codes related to the flutter_foreground_task package in your project and try again.

bhavinb98 commented 6 months ago

I ran the example and it ran fine.

Updated Agora plugin to version 6.3.1

I removed the flutter_foreground_task code from my app as well.

This is the error I get when I start screen share on the first run.

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1001, result=-1, data=Intent { (has extras) }} to activity {com.sprintstudio.conversations/io.agora.rtc2.extensions.MediaProjectionMgr$LocalScreenCaptureAssistantActivity}: java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
E/AndroidRuntime(16677):    at android.app.ActivityThread.deliverResults(ActivityThread.java:5527)
E/AndroidRuntime(16677):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:5566)
E/AndroidRuntime(16677):    at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:67)
E/AndroidRuntime(16677):    at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
E/AndroidRuntime(16677):    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139)
E/AndroidRuntime(16677):    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96)
E/AndroidRuntime(16677):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443)
E/AndroidRuntime(16677):    at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime(16677):    at android.os.Looper.loopOnce(Looper.java:205)
E/AndroidRuntime(16677):    at android.os.Looper.loop(Looper.java:294)
E/AndroidRuntime(16677):    at android.app.ActivityThread.main(ActivityThread.java:8177)
E/AndroidRuntime(16677):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(16677):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
E/AndroidRuntime(16677):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
E/AndroidRuntime(16677): Caused by: java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
E/AndroidRuntime(16677):    at android.os.Parcel.createExceptionOrNull(Parcel.java:3057)
E/AndroidRuntime(16677):    at android.os.Parcel.createException(Parcel.java:3041)
E/AndroidRuntime(16677):    at android.os.Parcel.readException(Parcel.java:3024)
E/AndroidRuntime(16677):    at android.os.Parcel.readException(Parcel.java:2966)
E/AndroidRuntime(16677):    at android.media.projection.IMediaProjection$Stub$Proxy.start(IMediaProjection.java:313)
E/AndroidRuntime(16677):    at android.media.projection.MediaProjection.<init>(MediaProjection.java:84)
E/AndroidRuntime(16677):    at android.media.projection.MediaProjection.<init>(MediaProjection.java:75)
E/AndroidRuntime(16677):    at android.media.projection.MediaProjectionManager.getMediaProjection(MediaProjectionManager.java:236)
E/AndroidRuntime(16677):    at io.agora.rtc2.extensions.MediaProjectionMgr.onRequestResult(MediaProjectionMgr.java:172)
E/AndroidRuntime(16677):    at io.agora.rtc2.extensions.MediaProjectionMgr$LocalScreenCaptureAssistantActivity.onActivityResult(MediaProjectionMgr.java:257)
E/AndroidRuntime(16677):    at android.app.Activity.dispatchActivityResult(Activity.java:8943)
E/AndroidRuntime(16677):    at android.app.ActivityThread.deliverResults(ActivityThread.java:5520)
E/AndroidRuntime(16677):    ... 13 more
E/AndroidRuntime(16677): Caused by: android.os.RemoteException: Remote stack trace:
E/AndroidRuntime(16677):    at com.android.server.media.projection.MediaProjectionManagerService$MediaProjection.start(MediaProjectionManagerService.java:940)
E/AndroidRuntime(16677):    at android.media.projection.IMediaProjection$Stub.onTransact(IMediaProjection.java:192)
E/AndroidRuntime(16677):    at android.os.Binder.execTransactInternal(Binder.java:1339)
E/AndroidRuntime(16677):    at android.os.Binder.execTransact(Binder.java:1275)
E/AndroidRuntime(16677): 

The subsequent runs after the first run work fine.

I also tried adding these permissions to my manifest to fix the first run issue but the problems remains

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CAMERA" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION" />
littleGnAl commented 5 months ago

Is it possible to provide a mini reproducible demo for this issue which should be useful for us to investigate it?

costaaaaaa commented 5 months ago

Ignore this update. I'm doing some tests. Sorry about it

costaaaaaa commented 5 months ago

@UnluckyY1 Comment how relevant is your link, please.

UnluckyY1 commented 5 months ago

@costaaaaaa Sorry, I made a mistake. I was supposed to post my comment on another issue.

bhavinb98 commented 5 months ago

Is it possible to provide a mini reproducible demo for this issue which should be useful for us to investigate it?

@littleGnAl Here

Steps to reproduce -

  1. Run first time, start screen share. (results in a crash).
  2. Run again, works fine.
  3. Do a flutter clean and also uninstall the app from emulator/device.
costaaaaaa commented 5 months ago

Guys, I made this changes to build and send to play console: Link to fork

Basically I added tools:node="remove" param in two permissions on AndroidManifest (from example folder) and set the replacement on Service tag.

I removed permissions because it I do not need this behavior at this moment.

Hope this workaround help someone.

bhavinb98 commented 5 months ago

@costaaaaaa Not sure if it works. I tried your forked branch on my demo project-

agora_rtc_engine:
    git:
      url: https://github.com/costaaaaaa/Agora-Flutter-SDK.git
      ref: fix_foreground_media_service

The issue persists.

@littleGnAl Did you get the chance to look at my demo project?

littleGnAl commented 5 months ago

@bhavinb98 I'm so sorry that I just got back from vacation, I will investigate the demo in the next few days.

littleGnAl commented 5 months ago

@bhavinb98 I found that it's related to the targetSdk, I changed the targetSdk it works fine

image

I'm so sorry that I'm a little busy this week, it still needs more time for me to investigate this issue next week. For a workaround you can set the targetSdk to 33 at this time.

littleGnAl commented 5 months ago

Okay, I finally found that it's a bug of our native SDK, it will be fixed on the next version, still, for a workaround you can set the targetSdk to 33 at this time.

bhavinb98 commented 5 months ago

Any timeline for the next version release?

littleGnAl commented 5 months ago

@bhavinb98 Probably in early July.

littleGnAl commented 5 months ago

The new version agora_rtc_engine: ^6.3.2 is out, please try upgrading it to see if it works.

bhavinb98 commented 5 months ago

It's working. Thanks

github-actions[bot] commented 4 months ago

Without additional information, we are unfortunately not sure how to resolve this issue. We are therefore reluctantly going to close this bug for now. If you find this problem please file a new issue with the same description, what happens, logs and the output. All system setups can be slightly different so it's always better to open new issues and reference the related ones. Thanks for your contribution.

github-actions[bot] commented 4 months ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please raise a new issue.