Skip to content

Commit

Permalink
guarantee AudioUtils stop executing after 3 seconds, sharp, regardles…
Browse files Browse the repository at this point in the history
…s of state of MediaCodec

Signed-off-by: rapterjet2004 <[email protected]>
  • Loading branch information
rapterjet2004 authored and backportbot-nextcloud[bot] committed Dec 22, 2023
1 parent cc17ff5 commit e16382d
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions app/src/main/java/com/nextcloud/talk/utils/AudioUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import kotlin.math.abs
object AudioUtils : DefaultLifecycleObserver {
private val TAG = AudioUtils::class.java.simpleName
private const val VALUE_10 = 10
private const val TIME_LIMIT = 5000
private const val TIME_LIMIT = 3000
private const val DEFAULT_SIZE = 500
private enum class LifeCycleFlag {
PAUSED,
Expand Down Expand Up @@ -115,21 +115,21 @@ object AudioUtils : DefaultLifecycleObserver {
mediaCodec.setCallback(object : MediaCodec.Callback() {
private var extractor: MediaExtractor? = null
val tempList = mutableListOf<Float>()
override fun onInputBufferAvailable(codec: MediaCodec, index: Int) {
// Setting up the extractor if not already done
if (extractor == null) {
extractor = MediaExtractor()
try {
extractor!!.setDataSource(path)
extractor!!.selectTrack(0)
} catch (e: IOException) {
e.printStackTrace()
}
init {
// Setting up the extractor to be guaranteed not null
extractor = MediaExtractor()
try {
extractor!!.setDataSource(path)
extractor!!.selectTrack(0)
} catch (e: IOException) {
e.printStackTrace()
}
}

override fun onInputBufferAvailable(codec: MediaCodec, index: Int) {
// Boiler plate, Extracts a buffer of encoded audio data to be sent to the codec for processing
val byteBuffer = codec.getInputBuffer(index)
if (byteBuffer != null) {
if (byteBuffer != null && extractor != null) {
val sampleSize = extractor!!.readSampleData(byteBuffer, 0)
if (sampleSize > 0) {
val isOver = !extractor!!.advance()
Expand Down Expand Up @@ -208,9 +208,14 @@ object AudioUtils : DefaultLifecycleObserver {
mediaCodec.configure(mediaFormat, null, null, 0)
mediaCodec.start()

// This runs until the codec finishes or the time limit is exceeded, or an error occurs
// If the time limit is exceed or an error occurs, the result should be null
while (result != null && result!!.size <= 0) {
// This runs until the codec finishes, the time limit is exceeded, or an error occurs
// If the time limit is exceed or an error occurs, the result should be null or empty
var currTime = SystemClock.elapsedRealtime() - startTime
while (result != null &&
result!!.size <= 0 &&
currTime < TIME_LIMIT // Guarantees Execution stops after 3 seconds
) {
currTime = SystemClock.elapsedRealtime() - startTime
continue
}

Expand Down

0 comments on commit e16382d

Please sign in to comment.