Skip to content

Commit

Permalink
[HDR Video] Fix the crash when MediaRecorder.stop is called immediate…
Browse files Browse the repository at this point in the history
…ly after start.

Bug: b/308345292
Test: Run on Samsung s23 Ultra and Pixel 8
Change-Id: I62595aaff1934061449d4ceff314c2c933b00c00
  • Loading branch information
kailianc committed Oct 30, 2023
1 parent 12a5686 commit f5852be
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,23 @@ class EncoderWrapper(width: Int,
* <p>
* Does not return until the encoder thread has stopped.
*/
public fun shutdown() {
public fun shutdown(): Boolean {
if (VERBOSE) Log.d(TAG, "releasing encoder objects")

if (mUseMediaRecorder) {
mMediaRecorder!!.stop()
try {
mMediaRecorder!!.stop()
} catch (e: RuntimeException) {
// https://developer.android.com/reference/android/media/MediaRecorder.html#stop()
// RuntimeException will be thrown if no valid audio/video data has been received
// when stop() is called, it usually happens when stop() is called immediately after
// start(). In this case the output file is not properly constructed ans should be
// deleted.
Log.d(TAG, "RuntimeException: stop() is called immediately after start()");
//noinspection ResultOfMethodCallIgnored
mOutputFile.delete()
return false
}
} else {
val handler = mEncoderThread!!.getHandler()
handler.sendMessage(handler.obtainMessage(EncoderThread.EncoderHandler.MSG_SHUTDOWN))
Expand All @@ -226,6 +238,7 @@ class EncoderWrapper(width: Int,
mEncoder!!.stop()
mEncoder!!.release()
}
return true
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,30 +369,38 @@ class PreviewFragment : Fragment() {
pipeline.cleanup()

Log.d(TAG, "Recording stopped. Output file: $outputFile")
encoder.shutdown()

// Broadcasts the media file to the rest of the system
MediaScannerConnection.scanFile(
requireView().context, arrayOf(outputFile.absolutePath), null, null)

// Launch external activity via intent to play video recorded using our provider
if (outputFile.exists()) {
startActivity(Intent().apply {
action = Intent.ACTION_VIEW
type = MimeTypeMap.getSingleton()
.getMimeTypeFromExtension(outputFile.extension)
val authority = "${BuildConfig.APPLICATION_ID}.provider"
data = FileProvider.getUriForFile(view.context, authority, outputFile)
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_ACTIVITY_CLEAR_TOP
})

if (encoder.shutdown()) {
// Broadcasts the media file to the rest of the system
MediaScannerConnection.scanFile(
requireView().context, arrayOf(outputFile.absolutePath), null, null)

if (outputFile.exists()) {
// Launch external activity via intent to play video recorded using our provider
startActivity(Intent().apply {
action = Intent.ACTION_VIEW
type = MimeTypeMap.getSingleton()
.getMimeTypeFromExtension(outputFile.extension)
val authority = "${BuildConfig.APPLICATION_ID}.provider"
data = FileProvider.getUriForFile(view.context, authority, outputFile)
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_ACTIVITY_CLEAR_TOP
})
} else {
// TODO:
// 1. Move the callback to ACTION_DOWN, activating it on the second press
// 2. Add an animation to the button before the user can press it again
Handler(Looper.getMainLooper()).post {
Toast.makeText(activity, R.string.error_file_not_found,
Toast.LENGTH_LONG).show()
}
}
} else {
Handler(Looper.getMainLooper()).post {
Toast.makeText(activity, R.string.error_file_not_found,
Toast.makeText(activity, R.string.recorder_shutdown_error,
Toast.LENGTH_LONG).show()
}
}

navController.popBackStack()
}
}
Expand Down
1 change: 1 addition & 0 deletions Camera2Video/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
<string name="app_name">Camera2Video</string>
<string name="capture">Capture</string>
<string name="error_file_not_found">Recorded file not found</string>
<string name="recorder_shutdown_error">Recording stops too fast, please press longer</string>
</resources>

0 comments on commit f5852be

Please sign in to comment.