livekit / client-sdk-flutter

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

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

Open kNoAPP opened 2 days ago

kNoAPP commented 2 days ago

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.

https://github.com/livekit/client-sdk-flutter/assets/23529044/2f86b067-45a1-4d89-817e-25cd6692728e

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

• No issues found!


* **Plugin version**: v2.2.0
* **Flutter target OS**: <!--  Android/iOS/Windows/Mac/Web [real device/simulator] -->
Android
* **Flutter target OS version**:
Android API 34+ (equivalent to Android 14)
* **Flutter console log**: <!-- optional -->
kNoAPP commented 2 days ago

Related: https://github.com/JulianAssmann/flutter_background/issues/85

kNoAPP commented 2 days ago

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

kNoAPP commented 2 days ago

https://github.com/JulianAssmann/flutter_background/blob/master/android/src/main/kotlin/de/julianassmann/flutter_background/IsolateHolderService.kt#L128C9-L128C41

Deprecated use: https://developer.android.com/reference/android/app/Service#startForeground(int,%20android.app.Notification)

Replace with: https://developer.android.com/reference/android/app/Service#startForeground(int,%20android.app.Notification,%20int)