Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ abstract class AsyncBaseRecordController : RecordController {
@Volatile
private var startTs: Long = 0
private val scope = CoroutineScope(Dispatchers.IO)
@Volatile
private var muxerChannel: Channel<MediaFrame>? = null
private var muxerJob: Job? = null
private val bufferPool = BufferPool()
Expand Down Expand Up @@ -159,6 +160,7 @@ abstract class AsyncBaseRecordController : RecordController {
stopRecord()
throw e
}
startMuxerJob()
}

override fun startRecord(
Expand All @@ -173,26 +175,14 @@ abstract class AsyncBaseRecordController : RecordController {
stopRecord()
throw e
}
startMuxerJob()
}

private fun start(
listener: RecordController.Listener?,
tracks: RecordTracks
) {
clearTimestamp()
muxerChannel = Channel(CAPACITY)
muxerJob = scope.launch {
val channel = muxerChannel ?: return@launch
for (frame in channel) {
try {
if (!isActive) break
onWriteFrame(frame)
} finally {
bufferPool.release(frame.data)
onFrameRecycled()
}
}
}
this.tracks = tracks
this.listener = listener
recordStatus = RecordController.Status.STARTED
Expand All @@ -217,6 +207,22 @@ abstract class AsyncBaseRecordController : RecordController {
stopRecordImp()
}

private fun startMuxerJob() {
val channel = Channel<MediaFrame>(CAPACITY)
muxerJob = scope.launch {
for (frame in channel) {
try {
if (!isActive) break
onWriteFrame(frame)
} finally {
bufferPool.release(frame.data)
onFrameRecycled()
}
}
}
muxerChannel = channel
}

private fun clearTimestamp() {
pauseMoment = 0
pauseTime = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ class AacMuxerRecordController : AsyncBaseRecordController() {

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
override fun stopRecordImp() {
sampleRate = -1
channels = -1
try {
outputStream?.close()
} catch (_: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class AndroidMuxerRecordController : AsyncBaseRecordController() {
throw IOException("Unsupported VideoCodec: " + getVideoCodec().name)
}
mediaMuxer = MediaMuxer(path, outputFormat)
if (tracks == RecordTracks.AUDIO && audioFormat != null) init()
}

@RequiresApi(api = Build.VERSION_CODES.O)
Expand All @@ -72,7 +71,6 @@ class AndroidMuxerRecordController : AsyncBaseRecordController() {
throw IOException("Unsupported VideoCodec: " + getVideoCodec().name)
}
mediaMuxer = MediaMuxer(fd, outputFormat)
if (tracks == RecordTracks.AUDIO && audioFormat != null) init()
}

override fun stopRecordImp() {
Expand All @@ -91,19 +89,22 @@ class AndroidMuxerRecordController : AsyncBaseRecordController() {

override fun setAudioFormat(audioFormat: MediaFormat) {
this.audioFormat = audioFormat
if (tracks == RecordTracks.AUDIO && recordStatus == RecordController.Status.STARTED) {
init()
}
}

override fun resetFormats() {
videoFormat = null
audioFormat = null
}

/**
* Only called from onWriteFrame, so it always runs in the muxer thread and never twice at once.
*/
private fun init() {
if (tracks != RecordTracks.VIDEO) audioTrack = mediaMuxer?.addTrack(audioFormat!!) ?: -1
mediaMuxer?.start()
if (recordStatus != RecordController.Status.STARTED) return
val muxer = mediaMuxer ?: return
if (tracks != RecordTracks.AUDIO) videoTrack = muxer.addTrack(videoFormat!!)
if (tracks != RecordTracks.VIDEO) audioTrack = muxer.addTrack(audioFormat!!)
muxer.start()
recordStatus = RecordController.Status.RECORDING
listener?.onStatusChange(recordStatus)
}
Expand All @@ -121,10 +122,10 @@ class AndroidMuxerRecordController : AsyncBaseRecordController() {
override suspend fun onWriteFrame(frame: MediaFrame) {
when (frame.type) {
MediaFrame.Type.VIDEO -> {
if (recordStatus == RecordController.Status.STARTED && videoFormat != null && (audioFormat != null || tracks == RecordTracks.VIDEO)) {
if (recordStatus == RecordController.Status.STARTED && tracks != RecordTracks.AUDIO
&& videoFormat != null && (audioFormat != null || tracks == RecordTracks.VIDEO)) {
if (frame.info.isKeyFrame || isKeyFrame(frame.data)) {
myRequestKeyFrame = null
videoTrack = mediaMuxer?.addTrack(videoFormat!!) ?: -1
init()
} else if (myRequestKeyFrame != null) {
myRequestKeyFrame?.onRequestKeyFrame()
Expand All @@ -141,6 +142,14 @@ class AndroidMuxerRecordController : AsyncBaseRecordController() {
}
}
MediaFrame.Type.AUDIO -> {
if (tracks == RecordTracks.AUDIO) {
if (recordStatus == RecordController.Status.STARTED && audioFormat != null) {
init()
} else if (recordStatus == RecordController.Status.RESUMED) {
recordStatus = RecordController.Status.RECORDING
listener?.onStatusChange(recordStatus)
}
}
if (recordStatus == RecordController.Status.RECORDING && tracks != RecordTracks.VIDEO) {
write(audioTrack, frame)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class AndroidMuxerWebmRecordController : AsyncBaseRecordController() {
throw IOException("Unsupported VideoCodec: " + getVideoCodec().name)
}
mediaMuxer = MediaMuxer(path, outputFormat)
if (tracks == RecordTracks.AUDIO && audioFormat != null) init()
}

@RequiresApi(api = Build.VERSION_CODES.O)
Expand All @@ -72,7 +71,6 @@ class AndroidMuxerWebmRecordController : AsyncBaseRecordController() {
throw IOException("Unsupported VideoCodec: " + getVideoCodec().name)
}
mediaMuxer = MediaMuxer(fd, outputFormat)
if (tracks == RecordTracks.AUDIO && audioFormat != null) init()
}

override fun stopRecordImp() {
Expand All @@ -91,19 +89,22 @@ class AndroidMuxerWebmRecordController : AsyncBaseRecordController() {

override fun setAudioFormat(audioFormat: MediaFormat) {
this.audioFormat = audioFormat
if (tracks == RecordTracks.AUDIO && recordStatus == RecordController.Status.STARTED) {
init()
}
}

override fun resetFormats() {
videoFormat = null
audioFormat = null
}

/**
* Only called from onWriteFrame, so it always runs in the muxer thread and never twice at once.
*/
private fun init() {
if (tracks != RecordTracks.VIDEO) audioTrack = mediaMuxer?.addTrack(audioFormat!!) ?: -1
mediaMuxer?.start()
if (recordStatus != RecordController.Status.STARTED) return
val muxer = mediaMuxer ?: return
if (tracks != RecordTracks.AUDIO) videoTrack = muxer.addTrack(videoFormat!!)
if (tracks != RecordTracks.VIDEO) audioTrack = muxer.addTrack(audioFormat!!)
muxer.start()
recordStatus = RecordController.Status.RECORDING
listener?.onStatusChange(recordStatus)
}
Expand All @@ -121,10 +122,10 @@ class AndroidMuxerWebmRecordController : AsyncBaseRecordController() {
override suspend fun onWriteFrame(frame: MediaFrame) {
when (frame.type) {
MediaFrame.Type.VIDEO -> {
if (recordStatus == RecordController.Status.STARTED && videoFormat != null && (audioFormat != null || tracks == RecordTracks.VIDEO)) {
if (recordStatus == RecordController.Status.STARTED && tracks != RecordTracks.AUDIO
&& videoFormat != null && (audioFormat != null || tracks == RecordTracks.VIDEO)) {
if (frame.info.isKeyFrame || isKeyFrame(frame.data)) {
myRequestKeyFrame = null
videoTrack = mediaMuxer?.addTrack(videoFormat!!) ?: -1
init()
} else if (myRequestKeyFrame != null) {
myRequestKeyFrame?.onRequestKeyFrame()
Expand All @@ -141,6 +142,14 @@ class AndroidMuxerWebmRecordController : AsyncBaseRecordController() {
}
}
MediaFrame.Type.AUDIO -> {
if (tracks == RecordTracks.AUDIO) {
if (recordStatus == RecordController.Status.STARTED && audioFormat != null) {
init()
} else if (recordStatus == RecordController.Status.RESUMED) {
recordStatus = RecordController.Status.RECORDING
listener?.onStatusChange(recordStatus)
}
}
if (recordStatus == RecordController.Status.RECORDING && tracks != RecordTracks.VIDEO) {
write(audioTrack, frame)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import java.io.FileDescriptor
import java.nio.ByteBuffer
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger

/**
* The controller copies each frame into a pooled buffer before handing it to the muxer. These
Expand All @@ -22,15 +23,25 @@ class AsyncBaseRecordControllerTest {
private class FakeRecordController: AsyncBaseRecordController() {
val written = mutableListOf<Pair<ByteArray, Int>>()
var latch = CountDownLatch(1)
//what the real muxers do inside startRecordImp, like creating the MediaMuxer
var onStartRecordImp: () -> Unit = {}
@Volatile
private var startingRecord = false
val writtenWhileStarting = AtomicInteger(0)

override fun startRecordImp(fd: FileDescriptor, listener: RecordController.Listener?, tracks: RecordController.RecordTracks) {}
override fun startRecordImp(path: String, listener: RecordController.Listener?, tracks: RecordController.RecordTracks) {}
override fun startRecordImp(path: String, listener: RecordController.Listener?, tracks: RecordController.RecordTracks) {
startingRecord = true
onStartRecordImp()
startingRecord = false
}
override fun stopRecordImp() {}
override fun setVideoFormat(videoFormat: MediaFormat) {}
override fun setAudioFormat(audioFormat: MediaFormat) {}
override fun resetFormats() {}

override suspend fun onWriteFrame(frame: MediaFrame) {
if (startingRecord) writtenWhileStarting.incrementAndGet()
//keep the array identity and the capacity, the buffer itself is recycled right after
written.add(frame.data.array() to frame.data.capacity())
}
Expand Down Expand Up @@ -96,6 +107,30 @@ class AsyncBaseRecordControllerTest {
assertEquals(second.size, controller.written[1].second)
}

@Test
fun `GIVEN a frame sent while startRecordImp runs WHEN record THEN the muxer never gets it`() {
val controller = FakeRecordController()
controller.latch = CountDownLatch(1)
controller.onStartRecordImp = {
//the encoder keeps sending frames while the muxer is still being created
val early = byteArrayOf(0, 0, 0, 1, 101, 1, 2, 3)
controller.recordVideo(ByteBuffer.wrap(early), bufferInfoOf(early.size))
//time enough for the muxer thread to take it if the recorder already accepted frames
Thread.sleep(200)
}
controller.start()

val frame = byteArrayOf(0, 0, 0, 1, 101, 9, 8, 7)
controller.recordVideo(ByteBuffer.wrap(frame), bufferInfoOf(frame.size))
controller.awaitFrames(1)
controller.stopRecord()

assertEquals(0, controller.writtenWhileStarting.get())
assertEquals(1, controller.written.size)
val (array, capacity) = controller.written[0]
assertArrayEquals(frame, array.copyOfRange(0, capacity))
}

@Test
fun `GIVEN a frame WHEN record THEN the muxer receives the same bytes`() {
val controller = FakeRecordController()
Expand Down
Loading
Loading