Hello Tello Pilot!
Join our DJI Tello community & remove this banner.
Sign up

[Java] Receive Tello's video stream and decode using JCodec library

nvplus

New member
Joined
Mar 11, 2020
Messages
2
Reaction score
0
Hello guys,
this is my first test in Java to receive DJI Tello's video stream and decode it using JCodec.

This is my code:
Code:
public void run() {
        logger.info("Thread started");
        try {
            socket = new DatagramSocket(new Integer(11111));
        } catch (SocketException e) {
            e.printStackTrace();
            return;
        }
        byte[] frame = null;
        while (isStreamOn) {

            buffer = new byte[2048];
            try {
                DatagramPacket receivePacket = new DatagramPacket(buffer, buffer.length);
                socket.receive(receivePacket);
                byte[] data = receivePacket.getData();
                if (frame == null) {
                    frame = data;
                } else {
                    byte[] temp = new byte[frame.length + data.length];
                    ByteBuffer bb = ByteBuffer.wrap(temp);
                    bb.put(frame);
                    bb.put(data);
                    frame = bb.array();
                }
                System.out.println("data size " + new String(data).trim().length());
                if (new String(data).trim().length() < 1460) {
                    System.out.println(">>>frame complete");
                    decode(frame);
                    frame = null;
                }

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        socket.close();
        logger.info("Thread stopped");

    }

        }
I think this is so far correct.

But now I am struggling to decode the received byte[] to a BufferedImage.
This is my try using JCodec library, but it just throws exceptions:

Code:
private H264Decoder decoder = new H264Decoder();
    private ByteBuffer bb = null;
    private Picture out = Picture.create(960, 720, ColorSpace.YUV420);

    private void decode(byte[] data) {

        System.out.println("decoding");
        bb = ByteBuffer.wrap(data);
        Picture real = decoder.decodeFrame(bb, out.getData());
        BufferedImage bi = AWTUtil.toBufferedImage(real);
        
        }
Has anyone tried using this library to decode before or are there better librarys for my needs?
Help appreciated.

nvplus
 

New Threads

Members online

Forum statistics

Threads
4,188
Messages
31,784
Members
10,724
Latest member
Ajkari