Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] Screen Share on Android target API 34+ crashes (Media projections require a foreground service) #542

Closed
kNoAPP opened this issue Jun 28, 2024 · 9 comments · Fixed by #545

Comments

@kNoAPP
Copy link
Contributor

kNoAPP commented Jun 28, 2024

Describe the bug
After Android API level 34 (impacting Android 14), screen-share functionality according to the LiveKit documentation results in an app crash event.

Crash at 3:29 in video.

LiveKit.SS.Android.Bug-s.mp4

To Reproduce
Set the example app's build.gradle target SDK version to 34 or higher (currently 31).

Expected behavior
Android API level 34+ should have instructions documented to fix this issue. Otherwise, a general warning about targeting API 34+ should be added to the documentation.

Platform information

  • Flutter version:
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.22.2, on Microsoft Windows [Version 10.0.22631.3737], locale en-US)
[√] 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 Build Tools 2019 16.11.29)
[√] Android Studio (version 2022.3)
[√] VS Code (version 1.90.2)
[√] Connected device (4 available)
[√] Network resources

• No issues found!
  • Plugin version: v2.2.0
  • Flutter target OS:
    Android
  • Flutter target OS version:
    Android API 34+ (equivalent to Android 14)
  • Flutter console log:
@kNoAPP
Copy link
Contributor Author

kNoAPP commented Jun 29, 2024

Related: JulianAssmann/flutter_background#85

@kNoAPP
Copy link
Contributor Author

kNoAPP commented Jun 29, 2024

Media projection

Foreground service type to declare in manifest under
android:foregroundServiceType

mediaProjection

Permission to declare in your manifest
FOREGROUND_SERVICE_MEDIA_PROJECTION

Constant to pass to startForeground()
FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION

Runtime prerequisites
Call the createScreenCaptureIntent() method before starting the foreground service. Doing so shows a permission notification to the user; the user must grant the permission before you can create the service.

After you have created the foreground service, you can call MediaProjectionManager.getMediaProjection().

Description
Project content to non-primary display or external device using the MediaProjection APIs. This content doesn't have to be exclusively media content.

Alternatives
To stream media to another device, use the Google Cast SDK.

See: https://developer.android.com/about/versions/14/changes/fgs-types-required

@iAbdulaziz93
Copy link

Hi, all!

I have the same issue too. I'm using the latest livekit_client package version, and I added the required Android configurations, permissions and service.

flutter_webrtc version is 0.10.8.
Android 13 and 14.

@davidliu
Copy link
Contributor

davidliu commented Jul 2, 2024

Have you followed the screen sharing instructions, including adding the foreground service type in the service declaration? Calling startForeground without declaring the type will fallback to the service type declared in the manifest, meaning no change is needed in the flutter_background package.

@iAbdulaziz93
Copy link

@davidliu Yeah, I did add the permissions and service to the manifest file.

@davidliu
Copy link
Contributor

davidliu commented Jul 2, 2024

Oh, I see what the problem is here. The problem isn't with the foreground service type; calling startForeground without a fgs type will default to the type declared in the AndroidManifest.

The problem is that the foreground service needs to be started only after receiving for the screenshare permission, but before the screenshare is actually started. This can be handled by calling Helper.requestCapturePermission() from flutter_webrtc first, and then continuing only when it returns true.

I've added updates to the docs and example in #545.

@kNoAPP
Copy link
Contributor Author

kNoAPP commented Jul 2, 2024

@davidliu I'll give this a quick test here this morning and write back results. Looks promising! Thanks for taking a look at all this!

@kNoAPP
Copy link
Contributor Author

kNoAPP commented Jul 2, 2024

@davidliu
cc. @cloudwebrtc

Working amazingly both in the example app and my own. Thank you guys so much. I am super grateful! 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants