fix: Add null safety checks in ChunkedRecordingManager
Replace !! operators with proper null checks to prevent NullPointerExceptions when encodedFormat or muxerContext are null. This can happen if createNextMuxer is called before onOutputFormatChanged sets the format. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -105,6 +105,12 @@ class ChunkedRecordingManager(private val encoder: MediaCodec, private val outpu
|
||||
muxerContext?.finish()
|
||||
chunkIndex++
|
||||
|
||||
val format = this.encodedFormat
|
||||
if (format == null) {
|
||||
Log.e(TAG, "Cannot create muxer: encodedFormat is null (onOutputFormatChanged not called yet)")
|
||||
return
|
||||
}
|
||||
|
||||
val newFileName = "$chunkIndex.mp4"
|
||||
val newOutputFile = File(this.outputDirectory, newFileName)
|
||||
Log.i(TAG, "Creating new muxer for file: $newFileName")
|
||||
@@ -114,7 +120,7 @@ class ChunkedRecordingManager(private val encoder: MediaCodec, private val outpu
|
||||
)
|
||||
muxer.setOrientationHint(orientationHint)
|
||||
muxerContext = MuxerContext(
|
||||
muxer, newOutputFile, chunkIndex, bufferInfo.presentationTimeUs, this.encodedFormat!!, this.callbacks
|
||||
muxer, newOutputFile, chunkIndex, bufferInfo.presentationTimeUs, format, this.callbacks
|
||||
)
|
||||
}
|
||||
|
||||
@@ -123,7 +129,8 @@ class ChunkedRecordingManager(private val encoder: MediaCodec, private val outpu
|
||||
}
|
||||
|
||||
private fun chunkLengthUs(bufferInfo: BufferInfo): Long {
|
||||
return bufferInfo.presentationTimeUs - muxerContext!!.startTimeUs
|
||||
val context = muxerContext ?: return 0L
|
||||
return bufferInfo.presentationTimeUs - context.startTimeUs
|
||||
}
|
||||
|
||||
fun start() {
|
||||
@@ -155,7 +162,13 @@ class ChunkedRecordingManager(private val encoder: MediaCodec, private val outpu
|
||||
if (muxerContext == null || (atKeyframe(bufferInfo) && chunkLengthUs(bufferInfo) >= targetDurationUs)) {
|
||||
this.createNextMuxer(bufferInfo)
|
||||
}
|
||||
muxerContext!!.muxer.writeSampleData(muxerContext!!.videoTrack, encodedData, bufferInfo)
|
||||
val context = muxerContext
|
||||
if (context == null) {
|
||||
Log.e(TAG, "Cannot write sample data: muxerContext is null")
|
||||
encoder.releaseOutputBuffer(index, false)
|
||||
return
|
||||
}
|
||||
context.muxer.writeSampleData(context.videoTrack, encodedData, bufferInfo)
|
||||
encoder.releaseOutputBuffer(index, false)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user