MediaCodec在高质量流上崩溃

时间:2015-09-29 10:21:06

标签: android video video-streaming h.264 decoder

我正在使用以下代码(original guide)解码h264视频流:

public void configure(Surface surface, int width, int height, ByteBuffer csd0) {
        String VIDEO_FORMAT = "video/avc";
        if (mConfigured) {
            throw new IllegalStateException("Decoder is already configured");
        }
        MediaFormat format = MediaFormat.createVideoFormat(VIDEO_FORMAT, width, height);
        // little tricky here, csd-0 is required in order to configure the codec properly
        // it is basically the first sample from encoder with flag: BUFFER_FLAG_CODEC_CONFIG
        format.setByteBuffer("csd-0", csd0);
        try {
            mCodec = MediaCodec.createDecoderByType(VIDEO_FORMAT);
        } catch (IOException e) {
            throw new RuntimeException("Failed to create codec", e);
        }
        mCodec.configure(format, surface, null, 0);
        mCodec.start();
        mConfigured = true;
    }

    @SuppressWarnings("deprecation")
    public void decodeSample(byte[] data, int offset, int size, long presentationTimeUs, int flags) {
        if (mConfigured && mRunning) {
            int index = mCodec.dequeueInputBuffer(mTimeoutUs);
            if (index >= 0) {
                ByteBuffer buffer;
                // since API 21 we have new API to use
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                    buffer = mCodec.getInputBuffers()[index];
                    buffer.clear();
                } else {
                    buffer = mCodec.getInputBuffer(index);
                }
                if (buffer != null) {
                    buffer.put(data, offset, size);
                    mCodec.queueInputBuffer(index, 0, size, presentationTimeUs, flags);
                }
            }
        }
    }

    @Override
    public void run() {
        try {
            MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
            while (mRunning) {
                if (mConfigured) {
                    int index = mCodec.dequeueOutputBuffer(info, mTimeoutUs);
                    if (index >= 0) {
                        // setting true is telling system to render frame onto Surface
                        mCodec.releaseOutputBuffer(index, true);
                        if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) == MediaCodec.BUFFER_FLAG_END_OF_STREAM) {
                            break;
                        }
                    }
                } else {
                    // just waiting to be configured, then decode and render
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException ignore) {
                    }
                }
            }
        } finally {
            if (mConfigured) {
                mCodec.stop();
                mCodec.release();
            }
        }
    }

我可以在中低质量的Nexus 6(api 22)和三星galaxy core(api 16)上运行。然而,当我切换到高质量(720p)时,它在大约30帧后在三星上崩溃(但没有任何内容呈现在屏幕上)。

E/ACodec﹕ [OMX.qcom.video.decoder.avc] ERROR(0x8000100a)
E/MediaCodec﹕ Codec reported an error. (omx error 0x8000100a, internalError -2147483648)
[...]
W/System.err﹕ java.lang.IllegalStateException
W/System.err﹕ at android.media.MediaCodec.dequeueInputBuffer(Native Method)
W/System.err﹕ at com.test.stream.VideoDecoder$Worker.decodeSample(VideoDecoder.java:95)
W/System.err﹕ at com.test.stream.VideoDecoder.decodeSample(VideoDecoder.java:24)
W/System.err﹕ at com.test.stream.VideoThread.run(VideoThread.java:160)

上面的错误是出现的第一个错误,之后会在每个帧上抛出IllegalStateException。

我的问题是,这是一个设备特定的问题(因为:较旧的api /设备,功能较弱等)或实际上是错误的? 我该怎么处理这个?

1 个答案:

答案 0 :(得分:2)

对于我的Android h.264解码器,我的设置略有不同。我认为你使用更现代的api级别。但对我来说,它看起来更像是这样:

public void startDecoder() {
    // Initilize codec
    mediaCodec = MediaCodec.createDecoderByType("video/avc");
    mediaFormat = MediaFormat.createVideoFormat("video/avc", 0, 0);
    bufferInfo = new MediaCodec.BufferInfo();

    // STOPS unit-tests from crashing here from mocked out android
    if (mediaCodec != null) {
        mediaCodec.configure(mediaFormat, targetSurface, null, 0);
        mediaCodec.start();
        decoderThread = new Thread(this);
        decoderThread.start();
    }
}

//解码器线程引用此类进行解码器/渲染循环:

public void run() {
    //mediaCodec input + output dequeue timeouts
    long kInputBufferTimeoutMs = 50;
    long kOutputBufferTimeoutMs = 50;

    while (running && mediaCodec != null) {
        synchronized (mediaCodec) {
            // stop if not running.
            if (!running || mediaCodec == null)
                break;

            // Only push in new data if there is data available in the queue
            if (naluSegmentQueue.size() > 0) {
                int inputBufferIndex = mediaCodec.dequeueInputBuffer(kInputBufferTimeoutMs);
                if (inputBufferIndex >= 0) {
                    NaluSegment segment = naluSegmentQueue.poll();
                    codecInputBufferAvailable(segment, mediaCodec, inputBufferIndex);
                }
            }

            // always check if output is available.
            int outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, kOutputBufferTimeoutMs);
            if (outputBufferIndex >= 0) {
                // Try and render first
                codecOuputBufferAvailable(mediaCodec, outputBufferIndex, bufferInfo);
            } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
                // Subsequent data will conform to new format.
                // Can ignore if using getOutputFormat(outputBufferId)
                mediaFormat = mediaCodec.getOutputFormat();
            }
        }
    }
}

将数据放入包括参数的解码器中。我不想尝试使用csd-0/1网络流可以改变格式描述,而且更容易让它被动态拾取。

private void codecInputBufferAvailable(NaluSegment segment, MediaCodec codec, int index) {
    int flags = (segment.getType() == NaluType.SPS
            || segment.getType() == NaluType.PPS
            || segment.getType() == NaluType.SUPP_ENHANCEMENT) ?
            MediaCodec.BUFFER_FLAG_CODEC_CONFIG : MediaCodec.BUFFER_FLAG_SYNC_FRAME;

    ByteBuffer[] buffers = codec.getInputBuffers();
    ByteBuffer buffer = buffers[index];
    // Can throw buffer overflow exception when buffer sizes are too small.
    try {
        buffer.put(segment.getBuffer());
        codec.queueInputBuffer(index, 0, segment.getBufferSize(), 0, flags);
    } catch(Exception e) {
        Log.e(TAG, "Failed to push buffer to decoder");
    }
}

重要:buffer.put(segment.getBuffer()); getBuffer()在这里总是返回一个4字节 annexb 缓冲区。 android解码器不理解3字节nal单元。因此,如果你有一个3字节的nal单元把它变成4字节的魔术序列,长度为+ 1, 0x00,0x00,0x00,0x01 作为起始魔术序列,其余的缓冲区应该是&amp; buffer [headerLength]。

注意这里的try-catch这并没有给出编译器警告,但如果你的有效载荷非常大且字节缓冲区太小,它可能会抛出缓冲区溢出异常

只要你正确解析你的NAL单位,这对你有用。但对于我的情况,我注意到魔术标题的NAL单位可以是3或4个字节。

/**
 * H264 is comprised of NALU segments.
 *
 * XXXX Y ZZZZZZZZ -> XXXX Y ZZZZZZZZ -> XXXX Y ZZZZZZZZ
 *
 * Each segment is comprised of:
 *
 * XXXX   -> Magic byte header (0x00, 0x00, 0x00, 0x01) NOTE: this can be either 3 of 4 bytes
 * Y      -> The Nalu Type
 * ZZZ... -> The Payload
 *
 * Notice there is no nalu length specified. To parse an nalu, you must
 * read until the next magic-byte-sequence AKA the next segment to figure
 * out the full nalu length
 **/
public static List<NaluSegment> parseNaluSegments(byte[] buffer) throws NaluBufferException {
    List<NaluSegment> segmentList = new ArrayList<>();
    if (buffer.length < 6) {
        return segmentList;
    }

    int lastStartingOffset = -1;
    for (int i = 0; i < buffer.length - 10; ++i) {
        **if (buffer[i] == 0x00 && buffer[i+1] == 0x00 && buffer[i+2] == 0x01)** {
            int naluType = (buffer[i+3] & 0x1F);
            NaluSegment segment = new NaluSegment(naluType, 3, i);

            **if (i > 0 && buffer[i-1] == 0x00)** {
                // This is actually a 4 byte segment
                int currentSegmentOffset = segment.getOffset();
                segment.setHeaderSize(4);
                segment.setOffset(currentSegmentOffset - 1);
            }
...

创建自己的nalu-segment对象,不要忘记尾随的NAL。

我希望这会有所帮助。