File size: 5,070 Bytes
1f3eae4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<!DOCTYPE html>
<html>

<head>
    <title>Camera Debug</title>
    <style>

        body {

            background: #000;

            color: #fff;

            font-family: monospace;

            padding: 20px;

        }



        .container {

            display: grid;

            grid-template-columns: 1fr 1fr;

            gap: 20px;

        }



        video,

        img {

            width: 100%;

            border: 2px solid #0f0;

        }



        .info {

            background: #222;

            padding: 10px;

            margin: 10px 0;

        }



        button {

            padding: 10px 20px;

            font-size: 16px;

            cursor: pointer;

        }

    </style>
</head>

<body>
    <h1>Camera Stream Debug</h1>
    <button onclick="startTest()">Start Camera Test</button>
    <button onclick="stopTest()">Stop Test</button>

    <div class="container">
        <div>
            <h3>Raw Camera Feed</h3>
            <video id="camera" autoplay playsinline muted></video>
            <div class="info" id="camera-info">Not started</div>
        </div>

        <div>
            <h3>Processed Stream (from server)</h3>
            <img id="processed" src="" alt="Waiting for processed frames...">
            <div class="info" id="processed-info">Not started</div>
        </div>
    </div>

    <div class="info">
        <h3>Console Log</h3>
        <div id="log" style="max-height: 300px; overflow-y: auto;"></div>
    </div>

    <canvas id="canvas" style="display:none;"></canvas>

    <script src="https://cdn.socket.io/4.5.4/socket.io.min.js"></script>
    <script>

        let stream = null;

        let socket = null;

        let capturing = false;



        const camera = document.getElementById('camera');

        const processed = document.getElementById('processed');

        const canvas = document.getElementById('canvas');

        const ctx = canvas.getContext('2d');

        const logDiv = document.getElementById('log');



        function log(msg) {

            const time = new Date().toLocaleTimeString();

            logDiv.innerHTML += `[${time}] ${msg}<br>`;

            logDiv.scrollTop = logDiv.scrollHeight;

            console.log(msg);

        }



        async function startTest() {

            try {

                log('Requesting camera access...');

                stream = await navigator.mediaDevices.getUserMedia({

                    video: { width: 640, height: 480 }

                });



                camera.srcObject = stream;

                document.getElementById('camera-info').textContent = 'Camera active';

                log('Camera started successfully');



                // Connect socket

                log('Connecting to Socket.IO...');

                socket = io();



                socket.on('connect', () => {

                    log('Socket connected: ' + socket.id);

                    socket.emit('start_camera_session', { session_id: 'debug_test' });

                });



                socket.on('camera_session_started', (data) => {

                    log('Session started: ' + data.session_id);

                    startCapture();

                });



                socket.on('processed_frame', (data) => {

                    log('Received processed frame, size: ' + data.frame.length);

                    processed.src = data.frame;

                    document.getElementById('processed-info').textContent = 'Last update: ' + new Date().toLocaleTimeString();

                });



                socket.on('error', (data) => {

                    log('ERROR: ' + data.message);

                });



            } catch (err) {

                log('ERROR: ' + err.message);

            }

        }



        function startCapture() {

            capturing = true;

            log('Starting frame capture...');

            captureFrame();

        }



        function captureFrame() {

            if (!capturing) return;



            if (camera.readyState < 2) {

                setTimeout(captureFrame, 100);

                return;

            }



            canvas.width = camera.videoWidth;

            canvas.height = camera.videoHeight;

            ctx.drawImage(camera, 0, 0);



            const frameData = canvas.toDataURL('image/jpeg', 0.8);



            if (socket && socket.connected) {

                socket.emit('camera_frame', { frame: frameData });

                log('Sent frame: ' + frameData.length + ' bytes');

            }



            setTimeout(captureFrame, 500); // 2 FPS for debugging

        }



        function stopTest() {

            capturing = false;

            if (stream) {

                stream.getTracks().forEach(track => track.stop());

                stream = null;

            }

            if (socket) {

                socket.disconnect();

                socket = null;

            }

            log('Test stopped');

        }

    </script>
</body>

</html>