-
Notifications
You must be signed in to change notification settings - Fork 45
feat: screen share audio #2157
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
Merged
Merged
feat: screen share audio #2157
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1321032
feat: added audio capture for screen sharing for Android
greenfrvr bca81f0
feat: added in app screen sharing
greenfrvr 5c60bd0
feat: added ios screen share audio capturing
greenfrvr d95572b
chore: ios moved to mixer node implementation
greenfrvr 6d7943a
chore: extended media usage types for Android
greenfrvr 9431910
chore: code cleanup
greenfrvr 4dcdc16
chote: made mixer initialization lazy
greenfrvr 3f6e739
chore: audio capture improvement
greenfrvr ae0a08d
chore: added early exit for missing permissions branch
greenfrvr a04243f
chore: pr comment
greenfrvr b35bbc1
chore: small tweak
greenfrvr 13c1589
chore: bumped webrtc version
greenfrvr 1976dec
Merge branch 'main' into feat/screen-share-audio
greenfrvr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
...e-sdk/android/src/main/java/com/streamvideo/reactnative/screenshare/ScreenAudioCapture.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| package com.streamvideo.reactnative.screenshare | ||
|
|
||
| import android.annotation.SuppressLint | ||
| import android.media.AudioAttributes | ||
| import android.media.AudioFormat | ||
| import android.media.AudioPlaybackCaptureConfiguration | ||
| import android.media.AudioRecord | ||
| import android.media.projection.MediaProjection | ||
| import android.os.Build | ||
| import android.util.Log | ||
| import androidx.annotation.RequiresApi | ||
| import java.nio.ByteBuffer | ||
|
|
||
| /** | ||
| * Captures system media audio using [AudioPlaybackCaptureConfiguration]. | ||
| * | ||
| * Uses the given [MediaProjection] to set up an [AudioRecord] that captures | ||
| * audio from media playback, games, and other apps (USAGE_MEDIA, USAGE_GAME, | ||
| * USAGE_UNKNOWN) but not notifications, alarms, or system sounds. | ||
| * | ||
| * Audio is captured in a pull-based manner via [getScreenAudioBytes], which | ||
| * reads exactly the requested number of bytes using [AudioRecord.READ_BLOCKING]. | ||
| * This is designed to be called from the WebRTC audio processing thread. | ||
| * | ||
| * Format: 48kHz, mono, PCM 16-bit (matching WebRTC's audio pipeline). | ||
| * | ||
| * Requires Android 10 (API 29+). | ||
| */ | ||
| @RequiresApi(Build.VERSION_CODES.Q) | ||
| class ScreenAudioCapture(private val mediaProjection: MediaProjection) { | ||
|
|
||
| private var audioRecord: AudioRecord? = null | ||
| private var screenAudioBuffer: ByteBuffer? = null | ||
|
|
||
| companion object { | ||
| private const val TAG = "ScreenAudioCapture" | ||
| const val SAMPLE_RATE = 48000 | ||
| private const val CHANNEL_CONFIG = AudioFormat.CHANNEL_IN_MONO | ||
| private const val AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT | ||
| } | ||
|
|
||
| @SuppressLint("MissingPermission") | ||
| fun start() { | ||
| val playbackConfig = AudioPlaybackCaptureConfiguration.Builder(mediaProjection) | ||
| .addMatchingUsage(AudioAttributes.USAGE_MEDIA) | ||
| .addMatchingUsage(AudioAttributes.USAGE_GAME) | ||
| .addMatchingUsage(AudioAttributes.USAGE_UNKNOWN) | ||
| .build() | ||
|
|
||
| val audioFormat = AudioFormat.Builder() | ||
| .setSampleRate(SAMPLE_RATE) | ||
| .setChannelMask(CHANNEL_CONFIG) | ||
| .setEncoding(AUDIO_FORMAT) | ||
| .build() | ||
|
|
||
| audioRecord = AudioRecord.Builder() | ||
| .setAudioFormat(audioFormat) | ||
| .setAudioPlaybackCaptureConfig(playbackConfig) | ||
| .build() | ||
|
|
||
| if (audioRecord?.state != AudioRecord.STATE_INITIALIZED) { | ||
| Log.e(TAG, "AudioRecord failed to initialize") | ||
| audioRecord?.release() | ||
| audioRecord = null | ||
| return | ||
| } | ||
|
|
||
| audioRecord?.startRecording() | ||
| Log.d(TAG, "Screen audio capture started") | ||
| } | ||
|
|
||
| /** | ||
| * Pull-based read: returns a [ByteBuffer] containing exactly [bytesRequested] bytes | ||
| * of captured screen audio. | ||
| * | ||
| * Called from the WebRTC audio processing thread. Uses [AudioRecord.READ_BLOCKING] | ||
| * so it will block until the requested bytes are available. | ||
| * | ||
| * @return A [ByteBuffer] with screen audio data, or `null` if capture is not active. | ||
| */ | ||
| fun getScreenAudioBytes(bytesRequested: Int): ByteBuffer? { | ||
| val record = audioRecord ?: return null | ||
| if (bytesRequested <= 0) return null | ||
|
|
||
| val buffer = screenAudioBuffer?.takeIf { it.capacity() >= bytesRequested } | ||
| ?: ByteBuffer.allocateDirect(bytesRequested).also { screenAudioBuffer = it } | ||
|
|
||
| buffer.clear() | ||
| buffer.limit(bytesRequested) | ||
|
|
||
| val bytesRead = record.read(buffer, bytesRequested, AudioRecord.READ_BLOCKING) | ||
| if (bytesRead > 0) { | ||
| buffer.position(0) | ||
| buffer.limit(bytesRead) | ||
| return buffer | ||
| } | ||
| return null | ||
| } | ||
|
|
||
| fun stop() { | ||
| try { | ||
| audioRecord?.stop() | ||
| } catch (e: Exception) { | ||
| Log.w(TAG, "Error stopping AudioRecord: ${e.message}") | ||
| } | ||
| audioRecord?.release() | ||
| audioRecord = null | ||
| screenAudioBuffer = null | ||
| Log.d(TAG, "Screen audio capture stopped") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.