-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathglium.rs
More file actions
240 lines (206 loc) · 7.08 KB
/
Copy pathglium.rs
File metadata and controls
240 lines (206 loc) · 7.08 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use std::io;
use std::sync::{mpsc, RwLock};
use std::thread;
use std::time::Instant;
use glium::index::PrimitiveType;
use glium::Surface;
use glium::{implement_vertex, program, uniform};
use jpeg_decoder as jpeg;
use v4l::buffer::Type;
use v4l::io::traits::CaptureStream;
use v4l::prelude::*;
use v4l::video::capture::Parameters;
use v4l::video::Capture;
use v4l::{Format, FourCC};
use winit::application::ApplicationHandler;
use winit::event::WindowEvent;
use winit::event_loop::ActiveEventLoop;
use winit::window::WindowId;
#[derive(Debug, Clone, Copy)]
enum UserEvent {
WakeUp,
}
fn main() -> io::Result<()> {
let path = "/dev/video0";
println!("Using device: {}\n", path);
// Allocate 4 buffers by default
let buffer_count = 4;
let mut format: Format;
let params: Parameters;
let dev = RwLock::new(Device::with_path(path)?);
{
let dev = dev.read().unwrap();
format = dev.format()?;
params = dev.params()?;
// try RGB3 first
format.fourcc = FourCC::new(b"RGB3");
format = dev.set_format(&format)?;
if format.fourcc != FourCC::new(b"RGB3") {
// fallback to Motion-JPEG
format.fourcc = FourCC::new(b"MJPG");
format = dev.set_format(&format)?;
if format.fourcc != FourCC::new(b"MJPG") {
return Err(io::Error::new(
io::ErrorKind::Other,
"neither RGB3 nor MJPG supported by the device, but required by this example!",
));
}
}
}
println!("Active format:\n{}", format);
println!("Active parameters:\n{}", params);
// Setup the GL display stuff
let event_loop = winit::event_loop::EventLoop::<UserEvent>::with_user_event()
.build()
.map_err(io::Error::other)?;
let event_loop_proxy = event_loop.create_proxy();
let (_window, display) = glium::backend::glutin::SimpleWindowBuilder::new()
.with_inner_size(format.width, format.height)
.build(&event_loop);
// building the vertex buffer, which contains all the vertices that we will draw
let vertex_buffer = {
#[derive(Copy, Clone)]
struct Vertex {
position: [f32; 2],
tex_coords: [f32; 2],
}
implement_vertex!(Vertex, position, tex_coords);
glium::VertexBuffer::new(
&display,
&[
Vertex {
position: [-1.0, -1.0],
tex_coords: [0.0, 0.0],
},
Vertex {
position: [-1.0, 1.0],
tex_coords: [0.0, 1.0],
},
Vertex {
position: [1.0, 1.0],
tex_coords: [1.0, 1.0],
},
Vertex {
position: [1.0, -1.0],
tex_coords: [1.0, 0.0],
},
],
)
.unwrap()
};
// building the index buffer
let index_buffer =
glium::IndexBuffer::new(&display, PrimitiveType::TriangleStrip, &[1u16, 2, 0, 3]).unwrap();
// compiling shaders and linking them together
let program = program!(&display,
140 => {
vertex: "
#version 140
uniform mat4 matrix;
in vec2 position;
in vec2 tex_coords;
out vec2 v_tex_coords;
void main() {
gl_Position = matrix * vec4(position, 0.0, 1.0);
v_tex_coords = tex_coords;
}
",
fragment: "
#version 140
uniform sampler2D tex;
in vec2 v_tex_coords;
out vec4 f_color;
void main() {
f_color = texture(tex, v_tex_coords);
}
"
},
)
.unwrap();
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let dev = dev.read().unwrap();
// Setup a buffer stream
let mut stream = MmapStream::with_buffers(&dev, Type::VideoCapture, buffer_count).unwrap();
loop {
let (buf, _) = stream.next().unwrap();
let data = match &format.fourcc.repr {
b"RGB3" => buf.to_vec(),
b"MJPG" => {
// Decode the JPEG frame to RGB
let mut decoder = jpeg::Decoder::new(buf);
decoder.decode().expect("failed to decode JPEG")
}
_ => panic!("invalid buffer pixelformat"),
};
let _ = event_loop_proxy.send_event(UserEvent::WakeUp);
tx.send(data).unwrap();
}
});
struct LoopHandler<F> {
user_event: F,
}
impl<F: Fn(UserEvent)> ApplicationHandler<UserEvent> for LoopHandler<F> {
fn resumed(&mut self, _event_loop: &ActiveEventLoop) {}
fn window_event(
&mut self,
event_loop: &ActiveEventLoop,
_window_id: WindowId,
event: WindowEvent,
) {
// polling and handling the events received by the window
if let winit::event::WindowEvent::CloseRequested = event {
event_loop.exit();
}
}
fn user_event(&mut self, _event_loop: &ActiveEventLoop, event: UserEvent) {
(self.user_event)(event)
}
}
event_loop
.run_app(&mut LoopHandler {
user_event: move |_event| {
let t0 = Instant::now();
let mut data = rx.recv().unwrap();
while let Ok(frame) = rx.try_recv() {
data = frame;
}
let data = data;
let t1 = Instant::now();
let image = glium::texture::RawImage2d::from_raw_rgb_reversed(
&data,
(format.width, format.height),
);
let opengl_texture = glium::texture::Texture2d::new(&display, image).unwrap();
// building the uniforms
let uniforms = uniform! {
matrix: [
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0f32]
],
tex: &opengl_texture
};
// drawing a frame
let mut target = display.draw();
target.clear_color(0.0, 0.0, 0.0, 0.0);
target
.draw(
&vertex_buffer,
&index_buffer,
&program,
&uniforms,
&Default::default(),
)
.unwrap();
target.finish().unwrap();
print!(
"\rms: {}\t (buffer) + {}\t (UI)",
t1.duration_since(t0).as_millis(),
t1.elapsed().as_millis(),
);
},
})
.map_err(io::Error::other)
}