linkedin / LiTr

Lightweight hardware accelerated video/audio transcoder for Android.
BSD 2-Clause "Simplified" License
598 stars 83 forks source link

MediaCodec$CodecException: Error 0xffffec77 When trying to record video via CameraX while LiTr compressing #254

Open LiveInParadise opened 1 year ago

LiveInParadise commented 1 year ago

This error happens in CameraX only when LiTr library doing compress

Receiving this error -

androidx.camera.video.internal.ResourceCreationException: androidx.camera.video.internal.encoder.InvalidConfigException: android.media.MediaCodec$CodecException: Error 0xffffec77

Here is a full log stack

androidx.camera.video.internal.ResourceCreationException: androidx.camera.video.internal.encoder.InvalidConfigException: android.media.MediaCodec$CodecException: Error 0xffffec77
    at androidx.camera.video.Recorder.setupVideo(Recorder.java:1333)
    at androidx.camera.video.Recorder.initializeInternal(Recorder.java:1012)
    at androidx.camera.video.Recorder.lambda$onSurfaceRequested$0$androidx-camera-video-Recorder(Recorder.java:427)
    at androidx.camera.video.Recorder$$ExternalSyntheticLambda16.run(Unknown Source:4)
    at androidx.camera.core.impl.utils.executor.SequentialExecutor$1.run(SequentialExecutor.java:111)
    at androidx.camera.core.impl.utils.executor.SequentialExecutor$QueueWorker.workOnQueue(SequentialExecutor.java:231)
    at androidx.camera.core.impl.utils.executor.SequentialExecutor$QueueWorker.run(SequentialExecutor.java:173)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
    at java.lang.Thread.run(Thread.java:764)
Caused by: androidx.camera.video.internal.encoder.InvalidConfigException: android.media.MediaCodec$CodecException: Error 0xffffec77
    at androidx.camera.video.internal.encoder.EncoderImpl.<init>(EncoderImpl.java:234)
    at androidx.camera.video.Recorder$$ExternalSyntheticLambda15.createEncoder(Unknown Source:2)
    at androidx.camera.video.Recorder.setupVideo(Recorder.java:1330)
    ... 9 more
Caused by: android.media.MediaCodec$CodecException: Error 0xffffec77
    at android.media.MediaCodec.native_configure(Native Method)
    at android.media.MediaCodec.configure(MediaCodec.java:1943)
    at android.media.MediaCodec.configure(MediaCodec.java:1872)
    at androidx.camera.video.internal.encoder.EncoderImpl.reset(EncoderImpl.java:262)
    at androidx.camera.video.internal.encoder.EncoderImpl.<init>(EncoderImpl.java:232)

This is how I'm creating MediaFormat

private fun createVideoMediaFormat(trackFormat: VideoTrackFormat): MediaFormat {
        return MediaFormat().apply {
            setString(MediaFormat.KEY_MIME, "video/avc")
            setInteger(MediaFormat.KEY_WIDTH, trackFormat.width)
            setInteger(MediaFormat.KEY_HEIGHT, trackFormat.height)
            setInteger(MediaFormat.KEY_BIT_RATE, trackFormat.bitrate)
            setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, trackFormat.keyFrameInterval)
            setInteger(MediaFormat.KEY_FRAME_RATE, trackFormat.frameRate)
            setLong(MediaFormat.KEY_DURATION, trackFormat.duration)
            setInteger(KEY_ROTATION, trackFormat.rotation)
        }
    }

CameraX Recorder class has simple create method where I set highest supported quality -

 val recorder = Recorder.Builder().apply {
                        getVideoQuality()?.let {
                            setQualitySelector(QualitySelector.from(it))
                        }
                    }.build()

Can reproduce this on Samsung SM-J710FQ with Android 8

litr version - 1.5.4 CameraX - 1.2.1

What problem can it be?

izzytwosheds commented 1 year ago

I don't think we experimented with CameraX yet. @IanDBird can you take a look when you get a moment?

IanDBird commented 1 year ago

Hey 👋 Curious how you're integrating LiTr + CameraX. The current changes I landed recently really only support Camera2 as the API is more flexible about where the camera is rendering too. I have looked into proper CameraX support and was considering moving it to a separate module (since it requires a fair amount of dependencies).

If you're able to share how you're attaching CameraX, then happy to see if I can suggest a short term workaround until native support is ready?