srctree

Robin Linden parent 2d85f2ac 95902493
Get rid of unnecessary null checks in audio recording

inlinesplit
domain/src/main/kotlin/av/AudioCapture.kt added: 19, removed: 15, total 4
@@ -43,15 +43,24 @@ private fun findAudioRecord(sampleRate: Int, channels: Int): AudioRecord? {
return null
}
 
class AudioCapture(private val sampleRate: Int, private val channels: Int) {
private val audioRecord = findAudioRecord(sampleRate, channels)
fun isOk() = audioRecord != null
fun start() = audioRecord?.startRecording()
fun stop() = audioRecord?.stop()
fun release() = audioRecord?.release()
class AudioCapture private constructor(
private val sampleRate: Int,
private val channels: Int,
private val audioRecord: AudioRecord,
) {
fun start() = audioRecord.startRecording()
fun stop() = audioRecord.stop()
fun release() = audioRecord.release()
fun read(): ShortArray {
val bytes = ShortArray((sampleRate * channels * 0.1).toInt()) // E.g. 16-bit, 48kHz, 1 channel, 100ms
audioRecord?.read(bytes, 0, bytes.size)
audioRecord.read(bytes, 0, bytes.size)
return bytes
}
 
companion object {
fun create(sampleRate: Int, channels: Int): AudioCapture? {
val audioRecord = findAudioRecord(sampleRate, channels) ?: return null
return AudioCapture(sampleRate, channels, audioRecord)
}
}
}
 
domain/src/main/kotlin/feature/CallManager.kt added: 19, removed: 15, total 4
@@ -71,12 +71,7 @@ class CallManager @Inject constructor(
 
fun startSendingAudio(): Boolean {
val to = (inCall.value as CallState.InCall?)?.publicKey ?: return false
 
val recorder = AudioCapture(AUDIO_SAMPLING_RATE_HZ, AUDIO_CHANNELS)
if (!recorder.isOk()) {
return false
}
 
val recorder = AudioCapture.create(AUDIO_SAMPLING_RATE_HZ, AUDIO_CHANNELS) ?: return false
startAudioSender(recorder, to)
return true
}