loonix / another_audio_recorder

(Null-Safe + V2) Audio Record Plugin that supports record, pause, resume, stop, and provide access to audio level metering properties average power peak power.
https://pub.dev/packages/another_audio_recorder
MIT License
5 stars 8 forks source link

crash when use aac format #6

Closed libinliu2013 closed 1 month ago

libinliu2013 commented 1 year ago

D/AudioRecord(19201): mAudioRecord->stop() E/AndroidRuntime(19201): FATAL EXCEPTION: Audio Processing Thread E/AndroidRuntime(19201): Process: com.alone.host, PID: 19201 E/AndroidRuntime(19201): java.lang.IllegalStateException E/AndroidRuntime(19201): at android.media.MediaCodec.releaseOutputBuffer(Native Method) E/AndroidRuntime(19201): at android.media.MediaCodec.releaseOutputBuffer(MediaCodec.java:2871) E/AndroidRuntime(19201): at com.loonix.another_audio_recorder.AACRecordThread.handleCodecOutput(AACRecordThread.java:226) E/AndroidRuntime(19201): at com.loonix.another_audio_recorder.AACRecordThread.run(AACRecordThread.java:150) E/AndroidRuntime(19201): at java.lang.Thread.run(Thread.java:919) I/Process (19201): Sending signal. PID: 19201 SIG: 9 Lost connection to device.

loonix commented 1 year ago

Hey, thanks for this but could you give me a bit more information like a sample app with that bug so I can quickly investigate it

loonix commented 1 year ago

I am in the process of upgrading to version 3 on my projects, so if I find it I will give it a fix.. but hard to see if I dont get it

squaredx commented 1 year ago

Hey I just wanted to add to this issue thread as we are encountering a similar issue on Android devices (iOS is fine). In our experience, the entire app crashes when the recording is stopped.

Here is the exception I'm getting on an Android (SDK level 33) simulator, however it can also be reproduced on a physical Pixel 7 pro.

D/EGL_emulation( 5794): app_time_stats: avg=500.09ms min=493.17ms max=506.69ms count=3
5
D/CCodecBufferChannel( 5794): [c2.android.aac.encoder#219] MediaCodec discarded an unknown buffer
E/AndroidRuntime( 5794): FATAL EXCEPTION: Audio Processing Thread
E/AndroidRuntime( 5794): Process: com.**********.app, PID: 5794
E/AndroidRuntime( 5794): java.lang.IllegalStateException
E/AndroidRuntime( 5794):    at android.media.MediaCodec.native_dequeueInputBuffer(Native Method)
E/AndroidRuntime( 5794):    at android.media.MediaCodec.dequeueInputBuffer(MediaCodec.java:2975)
E/AndroidRuntime( 5794):    at com.loonix.another_audio_recorder.AACRecordThread.handleCodecInput(AACRecordThread.java:187)
E/AndroidRuntime( 5794):    at com.loonix.another_audio_recorder.AACRecordThread.run(AACRecordThread.java:147)
E/AndroidRuntime( 5794):    at java.lang.Thread.run(Thread.java:1012)
I/hw-BpHwBinder( 5794): onLastStrongRef automatically unlinking death recipients
W/m.*********.app( 5794): No such thread for suspend: 0x6cd65127d8:AudioRecord
W/m.*********.app( 5794): No such thread for suspend: 0x6cd65127d8:MediaCodec_looper
D/MediaCodecList( 5794): codecHandlesFormat: no format, so no extra checks
D/CCodec  ( 5794): allocate(c2.android.aac.decoder)
I/CCodec  ( 5794): setting up 'default' as default (vendor) store
I/CCodec  ( 5794): Created component [c2.android.aac.decoder]
D/CCodecConfig( 5794): read media type: audio/mp4a-latm
D/ReflectedParamUpdater( 5794): extent() != 1 for single value type: algo.buffers.max-count.values
D/ReflectedParamUpdater( 5794): extent() != 1 for single value type: output.subscribed-indices.values
D/ReflectedParamUpdater( 5794): extent() != 1 for single value type: input.buffers.allocator-ids.values
D/ReflectedParamUpdater( 5794): extent() != 1 for single value type: output.buffers.allocator-ids.values
D/ReflectedParamUpdater( 5794): extent() != 1 for single value type: algo.buffers.allocator-ids.values
D/ReflectedParamUpdater( 5794): extent() != 1 for single value type: output.buffers.pool-ids.values
D/ReflectedParamUpdater( 5794): extent() != 1 for single value type: algo.buffers.pool-ids.values
I/CCodecConfig( 5794): query failed after returning 20 values (BAD_INDEX)
D/CCodecConfig( 5794): c2 config diff is Dict {
D/CCodecConfig( 5794):   c2::u32 coded.aac-packaging.value = 0
D/CCodecConfig( 5794):   c2::u32 coded.bitrate.value = 64000
D/CCodecConfig( 5794):   c2::u32 coded.pl.level = 0
D/CCodecConfig( 5794):   c2::u32 coded.pl.profile = 8192
D/CCodecConfig( 5794):   c2::i32 coding.drc.album-mode.value = 0
D/CCodecConfig( 5794):   c2::float coding.drc.attenuation-factor.value = 1
D/CCodecConfig( 5794):   c2::float coding.drc.boost-factor.value = 1
D/CCodecConfig( 5794):   c2::i32 coding.drc.compression-mode.value = 3
D/CCodecConfig( 5794):   c2::i32 coding.drc.effect-type.value = 3
D/CCodecConfig( 5794):   c2::float coding.drc.encoded-level.value = 0.25
D/CCodecConfig( 5794):   c2::float coding.drc.reference-level.value = -16
D/CCodecConfig( 5794):   c2::u32 input.buffers.max-size.value = 8192
D/CCodecConfig( 5794):   c2::u32 input.delay.value = 0
D/CCodecConfig( 5794):   string input.media-type.value = "audio/mp4a-latm"
D/CCodecConfig( 5794):   c2::u32 output.delay.value = 2
D/CCodecConfig( 5794):   c2::float output.drc.output-loudness.value = 0.25
D/CCodecConfig( 5794):   string output.media-type.value = "audio/raw"
D/CCodecConfig( 5794):   c2::u32 raw.channel-count.value = 1
D/CCodecConfig( 5794):   c2::u32 raw.channel-mask.value = 0
D/CCodecConfig( 5794):   c2::u32 raw.max-channel-count.value = 8
D/CCodecConfig( 5794):   c2::u32 raw.sample-rate.value = 44100
D/CCodecConfig( 5794): }
D/CCodec  ( 5794): [c2.android.aac.decoder] buffers are bound to CCodec for this session
D/CCodecConfig( 5794): no c2 equivalents for track-id
D/CCodecConfig( 5794): no c2 equivalents for durationUs
D/CCodecConfig( 5794): no c2 equivalents for flags
W/Codec2Client( 5794): query -- param skipped: index = 1107298332.
D/CCodec  ( 5794): encoding statistics level = 0
D/CCodec  ( 5794): setup formats input: AMessage(what = 0x00000000) = {
D/CCodec  ( 5794):   int32_t aac-drc-album-mode = 0
D/CCodec  ( 5794):   int32_t aac-drc-boost-level = 127
D/CCodec  ( 5794):   int32_t aac-drc-cut-level = 127
D/CCodec  ( 5794):   int32_t aac-drc-effect-type = 3
D/CCodec  ( 5794):   int32_t aac-encoded-target-level = -1
D/CCodec  ( 5794):   int32_t aac-max-output-channel_count = 8
D/CCodec  ( 5794):   int32_t aac-target-ref-level = 64
D/CCodec  ( 5794):   int32_t bitrate = 64000
D/CCodec  ( 5794):   int32_t channel-count = 1
D/CCodec  ( 5794):   int32_t channel-mask = 0
D/CCodec  ( 5794):   int32_t level = 0
D/CCodec  ( 5794):   int32_t max-input-size = 8192
D/CCodec  ( 5794):   int32_t max-output-channel-count = 8
D/CCodec  ( 5794):   string mime = "audio/mp4a-latm"
D/CCodec  ( 5794):   int32_t profile = 2
D/CCodec  ( 5794):   int32_t sample-rate = 44100
D/CCodec  ( 5794): }
D/CCodec  ( 5794): setup formats output: AMessage(what = 0x00000000) = {
D/CCodec  ( 5794):   int32_t aac-drc-album-mode = 0
D/CCodec  ( 5794):   int32_t aac-drc-boost-level = 127
D/CCodec  ( 5794):   int32_t aac-drc-cut-level = 127
D/CCodec  ( 5794):   int32_t aac-drc-effect-type = 3
D/CCodec  ( 5794):   int32_t aac-drc-output-loudness = -1
D/CCodec  ( 5794):   int32_t aac-encoded-target-level = -1
D/CCodec  ( 5794):   int32_t aac-max-output-channel_count = 8
D/CCodec  ( 5794):   int32_t aac-target-ref-level = 64
D/CCodec  ( 5794):   int32_t channel-count = 1
D/CCodec  ( 5794):   int32_t channel-mask = 0
D/CCodec  ( 5794):   int32_t max-output-channel-count = 8
D/CCodec  ( 5794):   string mime = "audio/raw"
D/CCodec  ( 5794):   int32_t sample-rate = 44100
D/CCodec  ( 5794):   int32_t android._config-pcm-encoding = 2
D/CCodec  ( 5794): }
I/CCodecConfig( 5794): query failed after returning 20 values (BAD_INDEX)
W/Codec2Client( 5794): query -- param skipped: index = 1342179345.
W/Codec2Client( 5794): query -- param skipped: index = 2415921170.
W/Codec2Client( 5794): query -- param skipped: index = 1610614798.
D/CCodecBufferChannel( 5794): [c2.android.aac.decoder#196] Created input block pool with allocatorID 16 => poolID 18 - OK (0)
I/CCodecBufferChannel( 5794): [c2.android.aac.decoder#196] Created output block pool with allocatorID 16 => poolID 27 - OK
D/CCodecBufferChannel( 5794): [c2.android.aac.decoder#196] Configured output block pool ids 27 => OK
D/CCodecConfig( 5794): c2 config diff is   c2::u32 raw.channel-mask.value = 4
D/CCodecBuffers( 5794): [c2.android.aac.decoder#196:Output[N]] popFromStashAndRegister: at 0us, output format changed to AMessage(what = 0x00000000) = {
D/CCodecBuffers( 5794):   int32_t aac-drc-album-mode = 0
D/CCodecBuffers( 5794):   int32_t aac-drc-boost-level = 127
D/CCodecBuffers( 5794):   int32_t aac-drc-cut-level = 127
D/CCodecBuffers( 5794):   int32_t aac-drc-effect-type = 3
D/CCodecBuffers( 5794):   int32_t aac-drc-output-loudness = -1
D/CCodecBuffers( 5794):   int32_t aac-encoded-target-level = -1
D/CCodecBuffers( 5794):   int32_t aac-max-output-channel_count = 8
D/CCodecBuffers( 5794):   int32_t aac-target-ref-level = 64
D/CCodecBuffers( 5794):   int32_t channel-count = 1
D/CCodecBuffers( 5794):   int32_t channel-mask = 4
D/CCodecBuffers( 5794):   int32_t max-output-channel-count = 8
D/CCodecBuffers( 5794):   string mime = "audio/raw"
D/CCodecBuffers( 5794):   int32_t sample-rate = 44100
D/CCodecBuffers( 5794):   int32_t android._config-pcm-encoding = 2
D/CCodecBuffers( 5794): }
I/System.out( 5794): decoderOutputBufferIndex = -2 (INFO_OUTPUT_FORMAT_CHANGED)
D/PipelineWatcher( 5794): onInputBufferReleased: frameIndex not found (17); ignored
loonix commented 1 year ago

Ok, so when you press stop it just crashes? or gives an error and then crashes?

squaredx commented 1 year ago

Yes I have created a demo that replicates the issue it's based on the example provided in this repo but upgraded to work on the latest version of flutter (with a few hacks).

When the recording uses the WAV codec it works as expected, but when the recording is stopped with AAC codec selected it hard crashes the app. I got that error from the android logcat for the emulator.

I think there might be some sort of race condition happening? Its weird because sometimes it crashes on the first attempt, however sometimes it take 2, 3, or 4 recordings for it to crash.

Example 1: https://github.com/loonix/another_audio_recorder/assets/5732781/93716993-bcc7-463a-afe4-559fcadbc7cf

Example 2: https://github.com/loonix/another_audio_recorder/assets/5732781/ee0dbcd4-bcf5-42d3-9179-5c0a961c1fb1

loonix commented 10 months ago

Does this only happen on the emulator? or does it happen on the device as well?

squaredx commented 10 months ago

I was able to replicate the issue both in the emulator and on a physical device (Pixel 7 Pro with Android 14).

Also, I was poking around in the source code to see if I could find the source of the issue but it's been a while since I've done Android development. My suspicion is that there might be a race condition occurring when stopping the recording specifically when using the AAC format.

^ This suspicion comes from the fact that WAV has no issues recording and this issue is only for AAC files-- which require the use of mediaCodec

I wasn't able to narrow it down and provide a fix in a reasonable amount of time but I just thought I would leave my finding here in case it helps with debugging! 😊

loonix commented 10 months ago

I've updated the version to latest. Let see if you get the same issue on the latest version...

squaredx commented 9 months ago

Hmm yeah I updated our application and we're still getting the same issue with version 1.1.0

loonix commented 9 months ago

ok, thanks can you use wav for now while this issue occurs? I will need to test the aac, I've never used it...