File size: 5,642 Bytes
ff9325e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
export function LCMLive(webcamVideo, liveImage) {
    let websocket: WebSocket;

    async function start() {
        return new Promise((resolve, reject) => {
            const websocketURL = `${window.location.protocol === "https:" ? "wss" : "ws"
                }:${window.location.host}/ws`;

            const socket = new WebSocket(websocketURL);
            socket.onopen = () => {
                console.log("Connected to websocket");
            };
            socket.onclose = () => {
                console.log("Disconnected from websocket");
                stop();
                resolve({ "status": "disconnected" });
            };
            socket.onerror = (err) => {
                console.error(err);
                reject(err);
            };
            socket.onmessage = (event) => {
                const data = JSON.parse(event.data);
                switch (data.status) {
                    case "success":
                        break;
                    case "start":
                        const userId = data.userId;
                        initVideoStream(userId);
                        break;
                    case "timeout":
                        stop();
                        resolve({ "status": "timeout" });
                    case "error":
                        stop();
                        reject(data.message);

                }
            };
            websocket = socket;
        })
    }
    function switchCamera() {
        const constraints = {
            audio: false,
            video: { width: 1024, height: 1024, deviceId: mediaDevices[webcamsEl.value].deviceId }
        };
        navigator.mediaDevices
            .getUserMedia(constraints)
            .then((mediaStream) => {
                webcamVideo.removeEventListener("timeupdate", videoTimeUpdateHandler);
                webcamVideo.srcObject = mediaStream;
                webcamVideo.onloadedmetadata = () => {
                    webcamVideo.play();
                    webcamVideo.addEventListener("timeupdate", videoTimeUpdateHandler);
                };
            })
            .catch((err) => {
                console.error(`${err.name}: ${err.message}`);
            });
    }

    async function videoTimeUpdateHandler() {
        const dimension = getValue("input[name=dimension]:checked");
        const [WIDTH, HEIGHT] = JSON.parse(dimension);

        const canvas = new OffscreenCanvas(WIDTH, HEIGHT);
        const videoW = webcamVideo.videoWidth;
        const videoH = webcamVideo.videoHeight;
        const aspectRatio = WIDTH / HEIGHT;

        const ctx = canvas.getContext("2d");
        ctx.drawImage(webcamVideo, videoW / 2 - videoH * aspectRatio / 2, 0, videoH * aspectRatio, videoH, 0, 0, WIDTH, HEIGHT)
        const blob = await canvas.convertToBlob({ type: "image/jpeg", quality: 1 });
        websocket.send(blob);
        websocket.send(JSON.stringify({
            "seed": getValue("#seed"),
            "prompt": getValue("#prompt"),
            "guidance_scale": getValue("#guidance-scale"),
            "strength": getValue("#strength"),
            "steps": getValue("#steps"),
            "lcm_steps": getValue("#lcm_steps"),
            "width": WIDTH,
            "height": HEIGHT,
            "controlnet_scale": getValue("#controlnet_scale"),
            "controlnet_start": getValue("#controlnet_start"),
            "controlnet_end": getValue("#controlnet_end"),
            "canny_low_threshold": getValue("#canny_low_threshold"),
            "canny_high_threshold": getValue("#canny_high_threshold"),
            "debug_canny": getValue("#debug_canny")
        }));
    }
    let mediaDevices = [];
    async function initVideoStream(userId) {
        liveImage.src = `/stream/${userId}`;
        await navigator.mediaDevices.enumerateDevices()
            .then(devices => {
                const cameras = devices.filter(device => device.kind === 'videoinput');
                mediaDevices = cameras;
                webcamsEl.innerHTML = "";
                cameras.forEach((camera, index) => {
                    const option = document.createElement("option");
                    option.value = index;
                    option.innerText = camera.label;
                    webcamsEl.appendChild(option);
                    option.selected = index === 0;
                });
                webcamsEl.addEventListener("change", switchCamera);
            })
            .catch(err => {
                console.error(err);
            });
        const constraints = {
            audio: false,
            video: { width: 1024, height: 1024, deviceId: mediaDevices[0].deviceId }
        };
        navigator.mediaDevices
            .getUserMedia(constraints)
            .then((mediaStream) => {
                webcamVideo.srcObject = mediaStream;
                webcamVideo.onloadedmetadata = () => {
                    webcamVideo.play();
                    webcamVideo.addEventListener("timeupdate", videoTimeUpdateHandler);
                };
            })
            .catch((err) => {
                console.error(`${err.name}: ${err.message}`);
            });
    }


    async function stop() {
        websocket.close();
        navigator.mediaDevices.getUserMedia({ video: true }).then((mediaStream) => {
            mediaStream.getTracks().forEach((track) => track.stop());
        });
        webcamVideo.removeEventListener("timeupdate", videoTimeUpdateHandler);
        webcamsEl.removeEventListener("change", switchCamera);
        webcamVideo.srcObject = null;
    }
    return {
        start,
        stop
    }
}