Skip to content

Commit

Permalink
fix(android): race condition when calling stop recording.
Browse files Browse the repository at this point in the history
  • Loading branch information
llfbandit authored Oct 9, 2024
2 parents 8485404 + 01e6892 commit 833fa33
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ class MediaCodecEncoder(
processInputBuffer()
}
} else if (msg.what == MSG_ENCODE_INPUT) {
mQueue.addLast(msg.obj as Sample)
if (mInputBufferIndex >= 0) {
processInputBuffer()
if (!mStopped.get()) {
mQueue.addLast(msg.obj as Sample)
if (mInputBufferIndex >= 0) {
processInputBuffer()
}
}
}

Expand Down Expand Up @@ -124,6 +126,7 @@ class MediaCodecEncoder(
getPresentationTimestampUs(mInputBufferPosition),
MediaCodec.BUFFER_FLAG_END_OF_STREAM
)
mInputBufferIndex = -1 // Reset index after sending EOS
}
return
}
Expand Down Expand Up @@ -174,7 +177,6 @@ class MediaCodecEncoder(

private fun finish() {
stopAndRelease()
mStoppedCompleter?.release()
}

private fun stopAndRelease() {
Expand All @@ -185,6 +187,9 @@ class MediaCodecEncoder(
mContainer?.stop()
mContainer?.release()
mContainer = null

mStoppedCompleter?.release()
mStoppedCompleter = null
}

private fun calculateInputRate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,21 @@ class RecordThread(
}

private fun stopAndRelease() {
mPcmReader?.stop()
mPcmReader?.release()
mPcmReader = null
try {
mPcmReader?.stop()
mPcmReader?.release()
mPcmReader = null

mEncoder?.stopEncoding()
mEncoder = null
mEncoder?.stopEncoding()
mEncoder = null

if (mHasBeenCanceled) {
Utils.deleteFile(config.path)
if (mHasBeenCanceled) {
Utils.deleteFile(config.path)
}
recorderListener.onStop()
} catch (ex: Exception) {
recorderListener.onFailure(ex)
}

recorderListener.onStop()
}

private fun selectFormat(): Format {
Expand Down

0 comments on commit 833fa33

Please sign in to comment.