-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement LocalAudioTrack.addSink (#516)
* Implement LocalAudioTrack.addSink * test fix * fix tests * fix tests
- Loading branch information
Showing
15 changed files
with
223 additions
and
113 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
"client-sdk-android": minor | ||
--- | ||
|
||
Implement LocalAudioTrack.addSink to receive audio data from local mic |
70 changes: 70 additions & 0 deletions
70
livekit-android-sdk/src/main/java/io/livekit/android/audio/AudioRecordSamplesDispatcher.kt
This file contains 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,70 @@ | ||
/* | ||
* Copyright 2024 LiveKit, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.livekit.android.audio | ||
|
||
import android.media.AudioFormat | ||
import android.os.SystemClock | ||
import livekit.org.webrtc.AudioTrackSink | ||
import livekit.org.webrtc.audio.JavaAudioDeviceModule | ||
import livekit.org.webrtc.audio.JavaAudioDeviceModule.SamplesReadyCallback | ||
import java.nio.ByteBuffer | ||
|
||
class AudioRecordSamplesDispatcher : SamplesReadyCallback { | ||
|
||
private val sinks = mutableSetOf<AudioTrackSink>() | ||
|
||
@Synchronized | ||
fun registerSink(sink: AudioTrackSink) { | ||
sinks.add(sink) | ||
} | ||
|
||
@Synchronized | ||
fun unregisterSink(sink: AudioTrackSink) { | ||
sinks.remove(sink) | ||
} | ||
|
||
// Reference from Android code, AudioFormat.getBytesPerSample. BitPerSample / 8 | ||
// Default audio data format is PCM 16 bits per sample. | ||
// Guaranteed to be supported by all devices | ||
private fun getBytesPerSample(audioFormat: Int): Int { | ||
return when (audioFormat) { | ||
AudioFormat.ENCODING_PCM_8BIT -> 1 | ||
AudioFormat.ENCODING_PCM_16BIT, AudioFormat.ENCODING_IEC61937, AudioFormat.ENCODING_DEFAULT -> 2 | ||
AudioFormat.ENCODING_PCM_FLOAT -> 4 | ||
AudioFormat.ENCODING_INVALID -> throw IllegalArgumentException("Bad audio format $audioFormat") | ||
else -> throw IllegalArgumentException("Bad audio format $audioFormat") | ||
} | ||
} | ||
|
||
@Synchronized | ||
override fun onWebRtcAudioRecordSamplesReady(samples: JavaAudioDeviceModule.AudioSamples) { | ||
val bitsPerSample = getBytesPerSample(samples.audioFormat) * 8 | ||
val numFrames = samples.sampleRate / 100 // 10ms worth of samples. | ||
val timestamp = SystemClock.elapsedRealtime() | ||
for (sink in sinks) { | ||
val byteBuffer = ByteBuffer.wrap(samples.data) | ||
sink.onData( | ||
byteBuffer, | ||
bitsPerSample, | ||
samples.sampleRate, | ||
samples.channelCount, | ||
numFrames, | ||
timestamp, | ||
) | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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
48 changes: 48 additions & 0 deletions
48
...android-test/src/main/java/io/livekit/android/test/mock/room/track/MockLocalAudioTrack.kt
This file contains 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,48 @@ | ||
/* | ||
* Copyright 2024 LiveKit, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.livekit.android.test.mock.room.track | ||
|
||
import io.livekit.android.audio.AudioProcessingController | ||
import io.livekit.android.audio.AudioRecordSamplesDispatcher | ||
import io.livekit.android.room.track.LocalAudioTrack | ||
import io.livekit.android.room.track.LocalAudioTrackOptions | ||
import io.livekit.android.test.MockE2ETest | ||
import io.livekit.android.test.mock.MockAudioProcessingController | ||
import io.livekit.android.test.mock.MockAudioStreamTrack | ||
import io.livekit.android.test.mock.TestData | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import livekit.org.webrtc.AudioTrack | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
fun MockE2ETest.createMockLocalAudioTrack( | ||
name: String = "", | ||
mediaTrack: AudioTrack = MockAudioStreamTrack(id = TestData.LOCAL_TRACK_PUBLISHED.trackPublished.cid), | ||
options: LocalAudioTrackOptions = LocalAudioTrackOptions(), | ||
audioProcessingController: AudioProcessingController = MockAudioProcessingController(), | ||
dispatcher: CoroutineDispatcher = coroutineRule.dispatcher, | ||
audioRecordSamplesDispatcher: AudioRecordSamplesDispatcher = AudioRecordSamplesDispatcher(), | ||
): LocalAudioTrack { | ||
return LocalAudioTrack( | ||
name = name, | ||
mediaTrack = mediaTrack, | ||
options = options, | ||
audioProcessingController = audioProcessingController, | ||
dispatcher = dispatcher, | ||
audioRecordSamplesDispatcher = audioRecordSamplesDispatcher, | ||
) | ||
} |
This file contains 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.