7

I get the IllegalStateException on MediaCodec.configure() line, I'm trying to record audio using MediaCodec. This only occur on some phones, on tabs everything is fine. This particular crash example is from Samsung Galaxy S4. Exception traces:

01-22 17:33:38.379: V/ACodec(16541): [OMX.google.aac.decoder] Now Loaded
01-22 17:33:38.379: V/ACodec(16541): onConfigureComponent
01-22 17:33:38.379: W/ACodec(16541): [OMX.google.aac.decoder] Failed to set standard component role 'audio_encoder.aac'.
01-22 17:33:38.379: E/ACodec(16541): [OMX.google.aac.decoder] configureCodec returning error -2147483648
01-22 17:33:38.379: E/MediaCodec(16541): Codec reported an error. (omx error 0x80001001, internalError -2147483648)
01-22 17:33:38.384: D/AndroidRuntime(16541): Shutting down VM
01-22 17:33:38.384: W/dalvikvm(16541): threadid=1: thread exiting with uncaught exception (group=0x418d0700)
01-22 17:33:38.414: W/BugSenseHandler(16541): Transmitting crash Exception Unable to resolve host "bugsense.appspot.com": No address associated with hostname
01-22 17:33:41.404: E/AndroidRuntime(16541): FATAL EXCEPTION: main
01-22 17:33:41.404: E/AndroidRuntime(16541): java.lang.IllegalStateException
01-22 17:33:41.404: E/AndroidRuntime(16541):    at android.media.MediaCodec.native_configure(Native Method)
01-22 17:33:41.404: E/AndroidRuntime(16541):    at android.media.MediaCodec.configure(MediaCodec.java:259)
01-22 17:33:41.404: E/AndroidRuntime(16541):    at com.example.poc.MyRenderer.startRecordPressed(MyRenderer.java:344)

Audio format declaration:

    MediaFormat format = new MediaFormat();
    format.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
    format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
    format.setInteger(MediaFormat.KEY_SAMPLE_RATE, 44100);
    format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
    format.setInteger(MediaFormat.KEY_BIT_RATE, 64000);

Audio encoder initialization:

        mAudioEncoder = MediaCodec.createEncoderByType("audio/mp4a-latm");
        mAudioEncoder.configure(mAudioFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); //<-This line fails
        mAudioEncoder.start();

Does anyone have any idea what that might be? What's strange is that it only happens on some devices. Any suggestions would be welcome!

fadden
  • 51,356
  • 5
  • 116
  • 166
Alexey
  • 287
  • 1
  • 4
  • 9
  • The setup looks fine. One odd thing about your code is that you declare a local `MediaFormat format`, but later on you appear to be using a member variable `mAudioEncoder`. Can you confirm that the right format is getting used? Maybe log the value of `mAudioFormat` right before the call to `configure()`. – fadden Jan 22 '14 at 18:21
  • That's just an extract, mAudioFormat is initialized fine, I double checked. Do you have any idea what the problem might be? And how to debug this further? – Alexey Jan 23 '14 at 08:08
  • The CTS EncoderTest does what your code does, so it should absolutely work on all devices. Can you add the output from the `MediaFormat` log to your question? It's curious that the failures are coming out of `OMX.google.aac.decoder` for an encoder, but maybe that's normal. (I haven't worked with audio much.) – fadden Jan 23 '14 at 15:35
  • @fadden, Yeah similarly i got error from `decoder component` when trying to configure encoder for `video/avc` – nmxprime Jan 24 '14 at 05:27
  • 1
    @fadded, How do I collect logs from MediaFormat? – Alexey Jan 24 '14 at 14:34
  • Are you trying to create more than one instance of same Encoder? Many devices do not support multiple instances of same Encoder. One possible reason could be this. – Gagan Mar 20 '14 at 03:01
  • Hey Alex , did you find any solution/workaround to this , I am facing same issue in only Galaxy s4 ! – Parikh Vaibhav Jul 26 '14 at 12:43
  • Parikh, unfortunately no. I haven't found any solution or workaround to this. I blame S4 native codec software for this error. – Alexey Jul 27 '14 at 13:29

1 Answers1

10

I see this same error when trying to configure a video codec on certain Samsung devices running Jellybean (4.1.2). In many cases, setting KEY_MAX_INPUT_SIZE to 0 (before calling configure) in the format parameters will fix it:

mVideoFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 0);

I found this solution when researching a similar bug (https://stackoverflow.com/questions/15105843/mediacodec-jelly-bean#=), and have been surprised at how many codec configuration bugs this fixes. That said, I haven't tried it on Audio codecs so no guarantee it will work in your case :/

Community
  • 1
  • 1
Tim
  • 875
  • 10
  • 15
  • 5
    What happens when `KEY_MAX_INPUT_SIZE` is set to 0? I wasn't able to find any docs – Mike Rapadas Jun 27 '15 at 02:09
  • 2
    Same issue with rockchip video decoders, setting it to 0 solves it. – Arpan Feb 02 '16 at 05:13
  • This also fixed issues on Galaxy Nexus with Android 4.3 (playing some videos with the ExoPlayer demo app made it crash). Do you know what kind of software/hardware this bug is limited to? – Stan May 04 '17 at 13:16