Skip to content

Commit f2340cf

Browse files
committed
fix(Android): CSD ignored for flac recording.
fixes #540
1 parent 6b2dbbd commit f2340cf

File tree

10 files changed

+47
-25
lines changed

10 files changed

+47
-25
lines changed

record_android/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 1.4.2
2+
* fix: Flac recording failure by isolating CSD frame skipping.
3+
14
## 1.4.1
25
* fix: Calling stop never ends when not recording.
36
* fix: Update max amplitude when streaming.

record_android/android/src/main/kotlin/com/llfbandit/record/record/container/FlacContainer.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class FlacContainer(path: String) : IContainerWriter {
1717

1818
override fun start() {
1919
if (isStarted) {
20-
throw IllegalStateException("Container already started")
20+
throw Exception("Container already started")
2121
}
2222

2323
Os.lseek(file.fd, 0, OsConstants.SEEK_SET)
@@ -27,7 +27,9 @@ class FlacContainer(path: String) : IContainerWriter {
2727
}
2828

2929
override fun stop() {
30-
check(isStarted) { "Container not started" }
30+
if (!isStarted) {
31+
throw Exception("Container not started")
32+
}
3133

3234
isStarted = false
3335

@@ -46,9 +48,9 @@ class FlacContainer(path: String) : IContainerWriter {
4648

4749
override fun addTrack(mediaFormat: MediaFormat): Int {
4850
if (isStarted) {
49-
throw IllegalStateException("Container already started")
51+
throw Exception("Container already started")
5052
} else if (track >= 0) {
51-
throw IllegalStateException("Track already added")
53+
throw Exception("Track already added")
5254
}
5355

5456
track = 0
@@ -61,11 +63,11 @@ class FlacContainer(path: String) : IContainerWriter {
6163
bufferInfo: MediaCodec.BufferInfo
6264
) {
6365
if (!isStarted) {
64-
throw IllegalStateException("Container not started")
66+
throw Exception("Container not started")
6567
} else if (track < 0) {
66-
throw IllegalStateException("No track has been added")
68+
throw Exception("No track has been added")
6769
} else if (track != trackIndex) {
68-
throw IllegalStateException("Invalid track: $trackIndex")
70+
throw Exception("Invalid track: $trackIndex")
6971
}
7072

7173
Os.write(file.fd, byteBuffer)

record_android/android/src/main/kotlin/com/llfbandit/record/record/container/IContainerWriter.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,9 @@ interface IContainerWriter {
8282

8383
return out
8484
}
85+
86+
/**
87+
* If true, ignores samples flagged as [MediaCodec.BUFFER_FLAG_CODEC_CONFIG].
88+
*/
89+
fun ignoreCodecSpecificData(): Boolean = false
8590
}

record_android/android/src/main/kotlin/com/llfbandit/record/record/container/MuxerContainer.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ import java.util.concurrent.atomic.AtomicBoolean
1010
* Wrapper around [MediaMuxer].
1111
*
1212
* @param path Output file path.
13+
* @param ignoreCodecSpecificData If true, ignores samples flagged as [MediaCodec.BUFFER_FLAG_CODEC_CONFIG].
1314
* @param containerFormat A valid [MediaMuxer.OutputFormat] value for the output container format.
1415
*/
15-
class MuxerContainer(val path: String, private val containerFormat: Int) : IContainerWriter {
16+
class MuxerContainer(
17+
val path: String,
18+
val ignoreCodecSpecificData: Boolean,
19+
private val containerFormat: Int
20+
) : IContainerWriter {
1621
private var mMuxer: MediaMuxer? = null
1722
private var mStarted = AtomicBoolean(false)
1823
private var mStopped = AtomicBoolean(false)
@@ -58,4 +63,8 @@ class MuxerContainer(val path: String, private val containerFormat: Int) : ICont
5863
mMuxer?.release()
5964
mMuxer = null
6065
}
66+
67+
override fun ignoreCodecSpecificData(): Boolean {
68+
return ignoreCodecSpecificData
69+
}
6170
}

record_android/android/src/main/kotlin/com/llfbandit/record/record/encoder/MediaCodecEncoder.kt

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,21 @@ class MediaCodecEncoder(
155155

156156
private fun processOutputBuffer(codec: MediaCodec, index: Int, info: MediaCodec.BufferInfo) {
157157
try {
158-
// The CSD (Codec-specific Data) is passed to muxer in MediaFormat. So we ignore it.
159-
val isCodecData = info.flags and MediaCodec.BUFFER_FLAG_CODEC_CONFIG != 0
160-
161-
if (!isCodecData && info.size != 0) {
162-
val out = codec.getOutputBuffer(index)
163-
val container = mContainer
164-
165-
if (out != null && container != null) {
166-
if (container.isStream()) {
167-
listener.onEncoderStream(container.writeStream(mContainerTrack, out, info))
168-
} else {
169-
container.writeSampleData(mContainerTrack, out, info)
158+
val container = mContainer
159+
160+
if (container != null && info.size != 0) {
161+
// The CSD (Codec-specific Data) is passed to muxer in MediaFormat. So we ignore it.
162+
val ignoreSample = container.ignoreCodecSpecificData() && info.flags and MediaCodec.BUFFER_FLAG_CODEC_CONFIG != 0
163+
164+
if (!ignoreSample) {
165+
val out = codec.getOutputBuffer(index)
166+
167+
if (out != null) {
168+
if (container.isStream()) {
169+
listener.onEncoderStream(container.writeStream(mContainerTrack, out, info))
170+
} else {
171+
container.writeSampleData(mContainerTrack, out, info)
172+
}
170173
}
171174
}
172175
}

record_android/android/src/main/kotlin/com/llfbandit/record/record/format/AacFormat.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@ class AacFormat : Format() {
7070
return AdtsContainer(sampleRate, numChannels, aacProfile)
7171
}
7272

73-
return MuxerContainer(path, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4)
73+
return MuxerContainer(path, true, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4)
7474
}
7575
}

record_android/android/src/main/kotlin/com/llfbandit/record/record/format/AmrNbFormat.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ class AmrNbFormat : Format() {
3434
throw IllegalAccessException("AmrNb requires min API version: " + Build.VERSION_CODES.O)
3535
}
3636

37-
return MuxerContainer(path, MediaMuxer.OutputFormat.MUXER_OUTPUT_3GPP)
37+
return MuxerContainer(path, false, MediaMuxer.OutputFormat.MUXER_OUTPUT_3GPP)
3838
}
3939
}

record_android/android/src/main/kotlin/com/llfbandit/record/record/format/AmrWbFormat.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ class AmrWbFormat : Format() {
3434
throw IllegalAccessException("AmrWb requires min API version: " + Build.VERSION_CODES.O)
3535
}
3636

37-
return MuxerContainer(path, MediaMuxer.OutputFormat.MUXER_OUTPUT_3GPP)
37+
return MuxerContainer(path, false, MediaMuxer.OutputFormat.MUXER_OUTPUT_3GPP)
3838
}
3939
}

record_android/android/src/main/kotlin/com/llfbandit/record/record/format/OpusFormat.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ class OpusFormat : Format() {
3434
throw IllegalAccessException("Opus requires min API version: " + Build.VERSION_CODES.Q)
3535
}
3636

37-
return MuxerContainer(path, MediaMuxer.OutputFormat.MUXER_OUTPUT_OGG)
37+
return MuxerContainer(path, false, MediaMuxer.OutputFormat.MUXER_OUTPUT_OGG)
3838
}
3939
}

record_android/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: record_android
22
description: Android specific implementation for record package called by record_platform_interface.
3-
version: 1.4.1
3+
version: 1.4.2
44
homepage: https://github.com/llfbandit/record/tree/master/record_android
55

66
environment:

0 commit comments

Comments
 (0)