Use embedded libopus encoder for devices that somehow lack the official one

This commit is contained in:
Jorge Martín 2022-09-05 12:43:37 +02:00 committed by Jorge Martin Espinosa
parent daacf35411
commit 0a1c7561bd
3 changed files with 17 additions and 4 deletions

1
changelog.d/7010.feature Normal file
View File

@ -0,0 +1 @@
Try to detect devices that lack Opus encoder support, use bundled libopus library for those.

View File

@ -48,7 +48,7 @@ class AudioMessageHelper @Inject constructor(
) {
private var mediaPlayer: MediaPlayer? = null
private var currentPlayingId: String? = null
private var voiceRecorder: VoiceRecorder = voiceRecorderProvider.provideVoiceRecorder()
private val voiceRecorder: VoiceRecorder by lazy { voiceRecorderProvider.provideVoiceRecorder() }
private val amplitudeList = mutableListOf<Int>()

View File

@ -17,6 +17,8 @@
package im.vector.app.features.voice
import android.content.Context
import android.media.MediaCodecList
import android.media.MediaFormat
import android.os.Build
import im.vector.app.features.VectorFeatures
import kotlinx.coroutines.Dispatchers
@ -27,10 +29,20 @@ class VoiceRecorderProvider @Inject constructor(
private val vectorFeatures: VectorFeatures,
) {
fun provideVoiceRecorder(): VoiceRecorder {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && vectorFeatures.forceUsageOfOpusEncoder().not()) {
VoiceRecorderQ(context)
} else {
return if (useFallbackRecorder()) {
VoiceRecorderL(context, Dispatchers.IO)
} else {
VoiceRecorderQ(context)
}
}
private fun useFallbackRecorder(): Boolean {
return Build.VERSION.SDK_INT < Build.VERSION_CODES.Q || !hasOpusEncoder() || vectorFeatures.forceUsageOfOpusEncoder()
}
private fun hasOpusEncoder(): Boolean {
val codecList = MediaCodecList(MediaCodecList.ALL_CODECS)
val format = MediaFormat.createAudioFormat(MediaFormat.MIMETYPE_AUDIO_OPUS, 48000, 1)
return codecList.findEncoderForFormat(format) != null
}
}