简体   繁体   中英

Incorrect decoding of H264 video stream on Nexus devices

I'm trying to use android MediaCodec class to decode h264 video stream of remote camera. My code is:

public class RemoteCamera {

public interface OnCameraListener {
    void onConnected();
    void onFailureConnection();
    void onDisconnected();
    void onReady();
}

private static final int MAX_NAL_LEN = 1024 * 1024;
private static final String TAG = "RemoteCamera";

private OutputThread mOutputThread;

private WebSocketManager mWebSocketManager;
private OnCameraListener mOnCameraListener;

private int mSearchState = 0;
private byte[] mNalData;
private int mNalDataPos;

private MediaCodec mDecoder;
private MediaFormat mFormat;
private SurfaceView mSurfaceView;
private MediaCodec.BufferInfo mInfo = new MediaCodec.BufferInfo();
private boolean mIsWaitingForSPS = true;

public RemoteCamera(final SurfaceView surfaceView, final String wss) {
    mSurfaceView = surfaceView;
    mWebSocketManager = new WebSocketManager(wss);
    mWebSocketManager.setWSListener(new WebSocketManager.OnWSListener() {
        @Override
        public void onOpen() {
            if (mOnCameraListener != null) {
                mOnCameraListener.onConnected();
            }
        }

        @Override
        public void onClosed() {
            if (mOnCameraListener != null) {
                mOnCameraListener.onDisconnected();
            }
        }

        @Override
        public void onFailure() {
            if (mOnCameraListener != null) {
                mOnCameraListener.onFailureConnection();
            }
        }

        @Override
        public synchronized void onMessage(final ByteString bytes) {
            final ByteBuffer bb = ByteBuffer.wrap(bytes.toByteArray());

            if (mIsWaitingForSPS) {
                if (isSPSUnit(bb)) {
                    mIsWaitingForSPS = false;
                    if (mOnCameraListener != null) {
                        mOnCameraListener.onReady();
                    }
                } else {
                    return;
                }
            }

            parseDatagram(bb.array(), bytes.size());
        }
    });

    mNalData = new byte[MAX_NAL_LEN];
    mNalDataPos = 0;
    try {
        mDecoder = MediaCodec.createDecoderByType("video/avc");
    } catch (Exception e) {
        Log.d(TAG, e.toString());
        return;
    }

    mFormat = MediaFormat.createVideoFormat("video/avc", 320, 240);
}

public void setOnCameraListener(final OnCameraListener cameraListener) {
    mOnCameraListener = cameraListener;
}

public void startStreaming() {
    mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder surfaceHolder) {
            try {
                mDecoder.configure(mFormat, mSurfaceView.getHolder().getSurface(), null, 0);
            } catch (Exception e) {
                Log.d(TAG, e.toString());
                return;
            }
            mWebSocketManager.wsRegister();
            mDecoder.start();
            mOutputThread = new OutputThread();
            mOutputThread.start();
        }

        @Override
        public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

        }
    });
}


private void feedDecoder(byte[] n, int len) {
    for (; ; ) {
        try {
            int inputBufferIndex = mDecoder.dequeueInputBuffer(0);
            if (inputBufferIndex >= 0) {
                final ByteBuffer inputBuffer = mDecoder.getInputBuffer(inputBufferIndex);
                inputBuffer.put(n, 0, len);
                mDecoder.queueInputBuffer(inputBufferIndex, 0, len, System.currentTimeMillis(), 0);
                break;
            }
        } catch (Exception e) {
            Log.d(TAG, e.toString());
        }
    }
}

private void parseDatagram(byte[] p, int plen) {
    try {
        for (int i = 0; i < plen; ++i) {
            mNalData[mNalDataPos++] = p[i];
            if (mNalDataPos == MAX_NAL_LEN - 1) {
                mNalDataPos = 0;
            }
            switch (mSearchState) {
                case 0:
                case 1:
                case 2:
                    if (p[i] == 0)
                        mSearchState++;
                    else
                        mSearchState = 0;
                    break;
                case 3:
                    if (p[i] == 1) {
                        mNalData[0] = 0;
                        mNalData[1] = 0;
                        mNalData[2] = 0;
                        mNalData[3] = 1;
                        feedDecoder(mNalData, mNalDataPos - 4);
                        mNalDataPos = 4;
                    }
                    mSearchState = 0;
                    break;
                default:
                    break;
            }
        }
    } catch (Exception e) {
        Log.d(TAG, e.toString());
    }
}

private boolean isSPSUnit(final ByteBuffer unit) {
    return unit.get(4) == 0x67;
}


private class OutputThread extends Thread {

    @Override
    public void run() {
        while (true) {
            try {
                int outputBufferIndex = mDecoder.dequeueOutputBuffer(mInfo, 10);
                if (outputBufferIndex >= 0) {
                    mDecoder.releaseOutputBuffer(outputBufferIndex, true);
                }
            } catch (Exception e) {
                Log.d(TAG, e.toString());
            }
        }
    }
}

I tested the code on Sony Xperia Z5 Compact and Yota Phone 2, and it works fine on these devices. The picture that I got from Sony is really good . Then I tried the video streamer on Nexus 9 and Nexus 7 devices, but it looks like a row moving from top to bottom. There is no correct output on Nexus devices: nexus results .

I know that it depends on native android media codec, but what should I do to resolve the problem and be able to show video on all devices?

不要将0x00 0x00 0x00 0x01 NALU起始代码传递给解码器。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM