davidliu
Committed by GitHub

Add example usage for ScreenAudioCapturer (#582)

1 /* 1 /*
2 - * Copyright 2024 LiveKit, Inc. 2 + * Copyright 2024-2025 LiveKit, Inc.
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
@@ -61,6 +61,10 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { @@ -61,6 +61,10 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
61 61
62 room.localParticipant.setScreenShareEnabled(true, data) 62 room.localParticipant.setScreenShareEnabled(true, data)
63 63
  64 + // Optionally disable the mic for screenshare audio only
  65 + // val javaAudioDeviceModule = (room.lkObjects.audioDeviceModule as? JavaAudioDeviceModule)
  66 + // javaAudioDeviceModule?.setAudioRecordEnabled(false)
  67 +
64 // Publish the audio track. 68 // Publish the audio track.
65 room.localParticipant.setMicrophoneEnabled(true) 69 room.localParticipant.setMicrophoneEnabled(true)
66 val screenCaptureTrack = room.localParticipant.getTrackPublication(Track.Source.SCREEN_SHARE)?.track as? LocalVideoTrack ?: return@launch 70 val screenCaptureTrack = room.localParticipant.getTrackPublication(Track.Source.SCREEN_SHARE)?.track as? LocalVideoTrack ?: return@launch
1 /* 1 /*
2 - * Copyright 2024 LiveKit, Inc. 2 + * Copyright 2024-2025 LiveKit, Inc.
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
@@ -61,7 +61,38 @@ private val DEFAULT_CONFIGURATOR: AudioPlaybackCaptureConfigurator = { builder - @@ -61,7 +61,38 @@ private val DEFAULT_CONFIGURATOR: AudioPlaybackCaptureConfigurator = { builder -
61 * 61 *
62 * Example usage: 62 * Example usage:
63 * ``` 63 * ```
  64 + * suspend fun startScreenCapture(data: Intent) {
  65 + * if (ActivityCompat.checkSelfPermission(getApplication(), Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
  66 + * return
  67 + * }
64 * 68 *
  69 + * // Publish the screen share video.
  70 + * room.localParticipant.setScreenShareEnabled(true, data)
  71 + *
  72 + * // Optionally disable the mic for screenshare audio only
  73 + * // val javaAudioDeviceModule = (room.lkObjects.audioDeviceModule as? JavaAudioDeviceModule)
  74 + * // javaAudioDeviceModule?.setAudioRecordEnabled(false)
  75 + *
  76 + * // Publish the audio track.
  77 + * room.localParticipant.setMicrophoneEnabled(true)
  78 + * val screenCaptureTrack = room.localParticipant.getTrackPublication(Track.Source.SCREEN_SHARE)?.track as? LocalVideoTrack ?: return
  79 + * val audioTrack = room.localParticipant.getTrackPublication(Track.Source.MICROPHONE)?.track as? LocalAudioTrack ?: return
  80 + *
  81 + * // Start capturing the screen share audio.
  82 + * val audioCapturer = ScreenAudioCapturer.createFromScreenShareTrack(screenCaptureTrack) ?: return
  83 + * audioCapturer.gain = 0.1f // Lower the volume so that mic can still be heard clearly.
  84 + * audioTrack.setAudioBufferCallback(audioCapturer)
  85 + * }
  86 + *
  87 + * suspend fun stopScreenCapture() {
  88 + * (room.localParticipant.getTrackPublication(Track.Source.MICROPHONE)?.track as? LocalAudioTrack)
  89 + * ?.setAudioBufferCallback(null)
  90 + * room.localParticipant.setMicrophoneEnabled(false)
  91 + * room.localParticipant.setScreenShareEnabled(false)
  92 + *
  93 + * // Remember to release when done capturing.
  94 + * audioCapturer?.releaseAudioResources()
  95 + * }
65 * ``` 96 * ```
66 */ 97 */
67 @RequiresApi(Build.VERSION_CODES.Q) 98 @RequiresApi(Build.VERSION_CODES.Q)
@@ -25,6 +25,7 @@ import com.vdurmont.semver4j.Semver @@ -25,6 +25,7 @@ import com.vdurmont.semver4j.Semver
25 import dagger.assisted.Assisted 25 import dagger.assisted.Assisted
26 import dagger.assisted.AssistedFactory 26 import dagger.assisted.AssistedFactory
27 import dagger.assisted.AssistedInject 27 import dagger.assisted.AssistedInject
  28 +import io.livekit.android.audio.ScreenAudioCapturer
28 import io.livekit.android.dagger.CapabilitiesGetter 29 import io.livekit.android.dagger.CapabilitiesGetter
29 import io.livekit.android.dagger.InjectionNames 30 import io.livekit.android.dagger.InjectionNames
30 import io.livekit.android.events.ParticipantEvent 31 import io.livekit.android.events.ParticipantEvent
@@ -271,11 +272,14 @@ internal constructor( @@ -271,11 +272,14 @@ internal constructor(
271 * 272 *
272 * This will use capture and publish default options from [Room]. 273 * This will use capture and publish default options from [Room].
273 * 274 *
  275 + * For screenshare audio, a [ScreenAudioCapturer] can be used.
  276 + *
274 * @param mediaProjectionPermissionResultData The resultData returned from launching 277 * @param mediaProjectionPermissionResultData The resultData returned from launching
275 * [MediaProjectionManager.createScreenCaptureIntent()](https://developer.android.com/reference/android/media/projection/MediaProjectionManager#createScreenCaptureIntent()). 278 * [MediaProjectionManager.createScreenCaptureIntent()](https://developer.android.com/reference/android/media/projection/MediaProjectionManager#createScreenCaptureIntent()).
276 * @throws IllegalArgumentException if attempting to enable screenshare without [mediaProjectionPermissionResultData] 279 * @throws IllegalArgumentException if attempting to enable screenshare without [mediaProjectionPermissionResultData]
277 * @see Room.screenShareTrackCaptureDefaults 280 * @see Room.screenShareTrackCaptureDefaults
278 * @see Room.screenShareTrackPublishDefaults 281 * @see Room.screenShareTrackPublishDefaults
  282 + * @see ScreenAudioCapturer
279 */ 283 */
280 suspend fun setScreenShareEnabled( 284 suspend fun setScreenShareEnabled(
281 enabled: Boolean, 285 enabled: Boolean,