2

I am trying to run the example code of Media Codec API with H264 Encoder on 4.3 explained in following link of bigflake

http://bigflake.com/mediacodec/CameraToMpegTest.java.txt

I have faced following problem. -> In H264 encoder code the color format,height and width are not getting updated because there is problem in getpatameter implemetation. So i applied this patch (https://code.google.com/p/android/issues/detail?id=58834). -> After applying the patch,also encoder does not encode -> I have seen the observation like D/CameraToMpegTest( 3421): encoder output format changed: {csd-1=java.nio.ByteArrayBuffer[position=0,limit=8,capacity=8], height=144, mime=video/avc, csd-0=java.nio.ByteArrayBuffer[position=0,limit=12,capacity=12], what=1869968451, width=176}

SO why this value is getting changed, No idea... After that we always see encoder gives status of queueOutputBuffer as INFO_TRY_AGAIN_LATER. So it creates the file but it does not encode anything and it stops as

I/MPEG4Writer( 3421): Received total/0-length (0/0) buffers and encoded 0 frames. - video

D/MPEG4Writer( 3421): Stopping Video track

D/MPEG4Writer( 3421): Stopping Video track source

D/MPEG4Writer( 3421): Video track stopped

D/MPEG4Writer( 3421): Stopping writer thread

D/MPEG4Writer( 3421): 0 chunks are written in the last batch

D/MPEG4Writer( 3421): Writer thread stopped

So in my understanding it should work but looks like still encoder is not getting configured properly...

Please guide on this... Thanks

Nehal

Nehal Shah
  • 93
  • 2
  • 11

3 Answers3

3

The "encoder output format changed" message is normal in Android 4.3. That's how the encoder gives you a MediaFormat with csd-0/csd-1 keys, needed by MediaMuxer#addTrack().

Bug 58834 is for the VP8 software encoder; those patches shouldn't be needed for the hardware AVC codec.

The most common reason for INFO_TRY_AGAIN_LATER is lack of input. The encoder may queue up a fair number of input frames before producing any output, so you can't just submit one frame and then wait for output to appear. Turn on the VERBOSE flag and make sure that frames are being submitted.

fadden
  • 51,356
  • 5
  • 116
  • 166
  • Thanks for the info fadden. However, csd-0/csd-1 message shows the diffrenet height and width and bitrate then what we set. Also here i am trying on software AVC so do you know any other fixes required. Regarding the frame queue up i will check and keep you posted. It will be also great if you have working example code of Media Codec, to share . – Nehal Shah Sep 18 '13 at 03:48
  • The code on bigflake.com is mine. The size and color format of the encoder are set when the format is passed to `configure()`; changing the `MediaFormat` object after that point won't have any effect. If you log the format immediately before calling `MediaCodec#configure()`, you should see the same dimensions that you see later on. – fadden Sep 18 '13 at 05:40
  • Hi Fadeen i am trying this example on Emulator. Hereby i have pasted my code. I have changed egl parameters as it does not work directly on emulator..However, i can see the frame coming from camera but onfrmaeavailable is not getting called...Also if just ignore this fact, assuming there is blank frame availale to encoder still it does not encoding...Please find the code below – Nehal Shah Sep 18 '13 at 06:08
  • I have taken changes regarding EGL config from http://stackoverflow.com/questions/11372863/egl-bad-config-error-opengl-es-2-0-with-egl-1-4-creating-an-off-screen-rendering – Nehal Shah Sep 18 '13 at 06:15
  • I strongly recommend using a physical device for media stuff. I have not tried any of the samples on the emulator. – fadden Sep 18 '13 at 17:56
1

I have tried running CameraToMpegTest sample on Android 4.3 emulator. As you'd have realized by now, it's not going to work as it is, and some fixes are required.

  1. Implement getparameter properly in SoftAVCEncoder (in case of MIME type - "video/avc") for parameters like width, height, colour format. Otherwise your MediaFormat is not configured properly, and createInputSurface would fail. (I am not sure why this doesn't cause any problem when running H.264 encoding using Mediarecorder)

  2. Fix the EGL attributes

  3. Most importantly, if you're trying to execute this code in Activity context, make sure you don't block onFrameAvailable callback (final void join() Blocks the current Thread (Thread.currentThread()) until the receiver finishes its execution and dies.)

Sumit Agrawal
  • 113
  • 1
  • 7
0

As the code snippet, you should remove th.join();

    /** Entry point. */
    public static void runTest(CameraToMpegTest obj) throws Throwable {
        CameraToMpegWrapper wrapper = new CameraToMpegWrapper(obj);
        Thread th = new Thread(wrapper, "codec test");
        th.start();
    //    th.join();
        if (wrapper.mThrowable != null) {
            throw wrapper.mThrowable;
        }
    }

It works well for me.

Jerikc XIONG
  • 3,517
  • 5
  • 42
  • 71