"High Definition Streaming" reproduction

This is where you talk about the EV3 software itself, installation issues, and programming talk.

Moderators: roger, gloomyandy, skoehler

clplaneguy
Novice
Posts: 29
Joined: Sat Feb 18, 2017 3:44 pm

"High Definition Streaming" reproduction

Postby clplaneguy » Wed Apr 25, 2018 12:45 am

I am trying to reproduce the results of the leJOS program in the post “High Definition Streaming”. It works great with my logitech webcam. The sample program assumes MJPG which works with the logitech. It supports two formats: YUYV (two resolutions) and MJPG (12 resolutions). But, I cannot get the program to work with my Insten Webcam. It has the same visual appearance as the Webcam in leJOS News. It supports one format: YUYV (two resolutions). Don’t pay much for it. It works fine on the computer. The camera is not broken. The program does not work for the Insten. It did work once and only once by accident on the EV3. I have no idea why. I have matched the program format (YUYV) and resolution (160 X 120) to the camera. The Lego is sending seven(7) frames per second but, the browser shows a broken image.

Does anyone have an idea what I can do next?


The Insten information and my program follow . . .


INSTEN
root@EV3:~# v4l2-ctl --list-formats
ioctl: VIDIOC_ENUM_FMT
Index : 0
Type : Video Capture
Pixel Format: 'YUYV'
Name : YUV 4:2:2 (YUYV)

root@EV3:~# v4l2-ctl --list-formats-ext
ioctl: VIDIOC_ENUM_FMT
Index : 0
Type : Video Capture
Pixel Format: 'YUYV'
Name : YUV 4:2:2 (YUYV)
Size: Discrete 320x240
Interval: Discrete 0.200 s (5.000 fps)
Size: Discrete 160x120
Interval: Discrete 0.100 s (10.000 fps)



Code: Select all

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import lejos.hardware.BrickFinder;
import lejos.hardware.Button;
import lejos.hardware.ev3.EV3;
import lejos.hardware.video.Video;

public class HttpStreamMJPEG {
   
    private static int WIDTH = 160;
    private static int HEIGHT = 120;
   
    public static void main(String[] args) throws IOException {
       System.out.println();
        System.out.println("4/23/2018");
        EV3 ev3 = (EV3) BrickFinder.getLocal();
        Video video = ev3.getVideo();
        System.out.println("format Video.PIX_FMT_MJPEG  " + Video.PIX_FMT_MJPEG);
        System.out.println("format Video.PIX_FMT_YUYV      " + Video.PIX_FMT_YUYV);
        System.out.println("format Video.PIX_FMT_JPEG     " + Video.PIX_FMT_JPEG);
       
        //video.open(WIDTH, HEIGHT, Video.PIX_FMT_MJPEG, video.FIELD_NONE, 30);byte[] frame = video.createFrame();
        //video.open(WIDTH, HEIGHT, Video.PIX_FMT_MJPEG, Video.FIELD_NONE, 30);
        //video.open(WIDTH, HEIGHT, format, field, fps);
        //video.open(WIDTH, HEIGHT, Video.PIX_FMT_MJPEG, Video.FIELD_NONE, 30);
        //video.open(WIDTH, HEIGHT, video.PIX_FMT_YUYV, video.FIELD_NONE, 30);
        //video.open(WIDTH, HEIGHT, Video.PIX_FMT_JPEG, Video.FIELD_NONE, 30);
        //video.open(WIDTH, HEIGHT, Video.PIX_FMT_JPEG, Video.FIELD_ANY, 15);
        //video.open(WIDTH, HEIGHT, Video.PIX_FMT_YUYV, Video.FIELD_ANY, 10);
        //video.open(WIDTH, HEIGHT, Video.PIX_FMT_YUYV, Video.FIELD_NONE, 10);
        video.open(WIDTH, HEIGHT);
        byte[] frame = video.createFrame();
        System.out.println("frame size " + frame.length);
        System.out.println("Waiting....");
        ServerSocket ss = new ServerSocket(8080);
        Socket sock = ss.accept();
        String boundary = "Thats it folks!";
        writeHeader(sock.getOutputStream(), boundary);
        long stime = System.currentTimeMillis();
        int cnt = 0;
        while (Button.ESCAPE.isUp()) {
            int len = video.grabFrame(frame);
            writeJpg(sock.getOutputStream(), frame, len, boundary);
            if (cnt++ >= 100)
            {
                long stop = System.currentTimeMillis();
                System.out.println("Frame rate: " + (cnt*1000/(stop - stime)));
                cnt = 0;
                stime = stop;
            }
        }   
        video.close();
       
        sock.close();
        ss.close();
    }
     
    private static void writeHeader(OutputStream stream, String boundary) throws IOException {
        stream.write(("HTTP/1.0 200 OK\r\n" +
                "Connection: close\r\n" +
                "Max-Age: 0\r\n" +
                "Expires: 0\r\n" +
                "Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0\r\n" +
                "Pragma: no-cache\r\n" +
                "Content-Type: multipart/x-mixed-replace; " +
                "boundary=" + boundary + "\r\n" +
                "\r\n" +
                "--" + boundary + "\r\n").getBytes());
    }
     
    private static void writeJpg(OutputStream stream, byte[] img, int len, String boundary) throws IOException {
        stream.write(("Content-type: image/jpeg\r\n" +
                "Content-Length: " + len + "\r\n" +
                "\r\n").getBytes());
        stream.write(img, 0, len);
        stream.write(("\r\n--" + boundary + "\r\n").getBytes());
    }
     

}

User avatar
gloomyandy
leJOS Team Member
Posts: 6047
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: "High Definition Streaming" reproduction

Postby gloomyandy » Wed Apr 25, 2018 5:26 am

Web browsers don't understand YUV format (especially when you tell them it is jpeg format. If you want to send a YUV image to a web browser you need to re-encode it into jpeg format first, when you use a camera that supports mjpeg then the images are encoded into jpeg my the camera already. To encode the image as jpeg see the code here:
https://lejosnews.wordpress.com/2015/02 ... streaming/
as it states in the article you will not get a very high framerate. There is a reason why the logitech camera costs more!
leJOS news https://lejosnews.wordpress.com/


Return to “EV3 Software”

Who is online

Users browsing this forum: No registered users and 1 guest