-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera-sdl-capture-and-playback.js
More file actions
220 lines (178 loc) · 5.56 KB
/
camera-sdl-capture-and-playback.js
File metadata and controls
220 lines (178 loc) · 5.56 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const sdl = require('bare-sdl')
class CameraCapture {
constructor() {
const cameras = sdl.Camera.getCameras()
if (!cameras?.length) {
throw new Error('No cameras found')
}
for (const camera of cameras) {
console.log(` ${camera.index}: ${camera.name} (${camera.id}))`, camera)
if (camera.index === 0) {
const formats = sdl.Camera.getSupportedFormats(camera.id)
console.log(` Supported formats: ${formats.length}`)
for (const format of formats.slice(0, 3)) {
const fps = format.framerateNumerator / format.framerateDenominator
console.log(` - ${format.width}x${format.height} @ ${fps}fps`)
}
}
}
console.log('\nOpening first camera...')
const requestedSpec =
Bare.platform === 'darwin'
? {
format: sdl.constants.SDL_PIXELFORMAT_UYVY,
width: 1280,
height: 720,
framerateNumerator: 30,
framerateDenominator: 1
}
: null
this.camera = new sdl.Camera(cameras[0], requestedSpec)
const spec = this.camera.spec
{
console.log(`Camera format: ${spec.width}x${spec.height} @ ${spec.fps}fps`)
console.log(`Pixel format: 0x${spec.format.toString(16)}`)
let formatName
switch (spec.format) {
case sdl.constants.SDL_PIXELFORMAT_YUY2:
formatName = 'YUY2'
break
case sdl.constants.SDL_PIXELFORMAT_UYVY:
formatName = 'UYVY'
break
case sdl.constants.SDL_PIXELFORMAT_YVYU:
formatName = 'YVYU'
break
case sdl.constants.SDL_PIXELFORMAT_NV12:
formatName = 'NV12'
break
case sdl.constants.SDL_PIXELFORMAT_NV21:
formatName = 'NV21'
break
case sdl.constants.SDL_PIXELFORMAT_RGB24:
formatName = 'RGB24'
break
case sdl.constants.SDL_PIXELFORMAT_BGR24:
formatName = 'BGR24'
break
default:
formatName = `Unknown (0x${spec.format.toString(16)})`
break
}
console.log(`Format name: ${formatName}`, spec.format, spec.toJSON())
}
this.win = new sdl.Window('Camera Capture', spec.width || 640, spec.height || 480)
this.ren = new sdl.Renderer(this.win)
const pixelFormat = spec.format
console.log(`Creating texture with format: 0x${pixelFormat.toString(16)}`)
this.tex = new sdl.Texture(
this.ren,
spec.width || 640,
spec.height || 480,
pixelFormat,
sdl.constants.SDL_TEXTUREACCESS_STREAMING
)
this.poller = new sdl.Poller()
this.frameCount = 0
this.lastFrameTime = Date.now()
}
checkPermission() {
const state = this.camera.permissionState
if (state === 0) {
console.log('Waiting for camera permission...')
return false
} else if (state === -1) {
console.log('Camera permission denied!')
return false
} else if (state === 1) {
if (!this.permissionGranted) {
console.log('Camera permission granted!')
this.permissionGranted = true
}
return true
}
return false
}
captureFrame() {
if (!this.checkPermission()) {
return false
}
const frame = this.camera.acquireFrame()
if (frame.valid) {
this.frameCount++
if (!this.firstFrameLogged) {
console.log(`First frame format: 0x${frame.format.toString(16)}`)
this.firstFrameLogged = true
}
const now = Date.now()
if (now - this.lastFrameTime > 1000) {
const fps = this.frameCount / ((now - this.lastFrameTime) / 1000)
console.log(`Capturing: ${frame.width}x${frame.height}, ${fps.toFixed(1)} fps`)
this.frameCount = 0
this.lastFrameTime = now
}
const pixels = frame.pixels
if (pixels) {
const pixelArray = new Uint8Array(pixels)
if (!this.pitchLogged) {
console.log(`Frame pitch: ${frame.pitch} bytes`)
this.pitchLogged = true
}
this.tex.update(pixelArray, frame.pitch)
this.ren.clear()
this.ren.texture(this.tex)
this.ren.present()
}
frame.release()
return true
}
return false
}
pollEvents() {
const event = new sdl.Event()
if (this.poller.poll(event)) {
const type = event.type
if (
type === sdl.constants.SDL_EVENT_QUIT ||
type === sdl.constants.SDL_EVENT_WINDOW_CLOSE_REQUESTED
) {
return false
}
if (type === sdl.constants.SDL_EVENT_CAMERA_DEVICE_APPROVED) {
console.log('Camera approved by system')
} else if (type === sdl.constants.SDL_EVENT_CAMERA_DEVICE_DENIED) {
console.log('Camera denied by system')
return false
} else if (type === sdl.constants.SDL_EVENT_CAMERA_DEVICE_ADDED) {
console.log('Camera device added')
} else if (type === sdl.constants.SDL_EVENT_CAMERA_DEVICE_REMOVED) {
console.log('Camera device removed')
}
if (type === sdl.constants.SDL_EVENT_KEY_DOWN) {
const key = event.key
if (key.scancode === sdl.constants.SDL_SCANCODE_ESCAPE) {
return false
}
}
}
return true
}
destroy() {
this.camera.destroy()
this.tex.destroy()
this.ren.destroy()
this.win.destroy()
}
run() {
console.log('\nCamera capture started. Press ESC to quit.')
let running = true
while (running) {
running = this.pollEvents()
this.captureFrame()
}
console.log('\nShutting down...')
}
}
const capture = new CameraCapture()
capture.run()
capture.destroy()