package org.aohe.webcam;

import com.github.sarxos.webcam.util.ImageUtils;
import lombok.extern.slf4j.Slf4j;
import org.aohe.constant.Control;
import org.aohe.constant.SocketEnum;
import org.aohe.web.SocketUtils;

/**
 * 二进制流传输
 */
@Slf4j
public class WebcamWebTask extends Thread {
    @Override
    public void run() {
        while (true) {
            try {
                while (Control.SHOW_CAMERA) {
                    WebCamUtils.open();
                    SocketUtils.get(SocketEnum.CAM_SOCKET.getName())
                            .broadcastNew(ImageUtils.toByteArray(WebCamUtils.getImage(), "jpg"));
                    //等待0.09秒
                    Thread.sleep(90);
                }
                //等待0.1秒
                Thread.sleep(100);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    }
}
