Whats wrong with my h264 MediaCodec implementation?

Viewed 31

I'm pulling video from a drone that encodes the video stream in h264. It sends each NAL unit via 1-5 UDP packets. I have code that creates a NAL unit out of those packets and removes the header. It then passes it to MediaCodec which then passes it to a Surface object

The output should be a video feed but for some reason, all I get is a black screen. I know that the surface is working as intended because if I futz with the NAL unit I get this green garbage that I think happens when MediaCodec doesn't know what it's looking at.

Anyways here's the section of code that handles the actual decoding. Is there anything actually wrong with it or am I looking for the issue in the wrong place?

    //This part initializes the decoder and generally sets up everything needed for the while loop down below
            encodedVideo = new ServerSocket(11111, 1460, false, 0);
            MediaFormat format = MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_AVC, 1920, 1080);
            format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 100000);
            try {
                m_codec = MediaCodec.createDecoderByType(MediaFormat.MIMETYPE_VIDEO_AVC);
                m_codec.configure(format, new Surface(droneSight.getSurfaceTexture()), null, 0);
            } catch (Exception e) {
                e.printStackTrace();
            }
            m_codec.start();
            running = true;
            initialFrame = Arrays.copyOf(encodedVideo.getPacketData(true,true),encodedVideo.getPacketData(true,false).length);

    //This section handles the actual grabbing and decoding of each NAL unit. Or it would, if it worked.
            while (running) {
                byte[] frame = this.getNALUnit();

                int inputIndex = m_codec.dequeueInputBuffer(-1);
                if (inputIndex >= 0) {
                    ByteBuffer buf = m_codec.getInputBuffer(inputIndex);
                    buf.put(frame);
                    m_codec.queueInputBuffer(inputIndex, 0, frame.length, 0, 0);
                }
                MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
                int outputIndex = m_codec.dequeueOutputBuffer(info, 0);
                if (outputIndex >= 0) {
                    m_codec.releaseOutputBuffer(outputIndex, true);
                }
            }
0 Answers
Related