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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! This crate defines a trait, [AsyncCamera], whose [AsyncCamera::frames]
//! method returns a [futures::Stream] for asynchronous usage.
//!
//! It also provides a struct, [ThreadedAsyncCameraModule] which will take a
//! camera module implementing the [ci2::CameraModule] trait and wrap it into a
//! new struct that also implements the [ci2::CameraModule] in addition to
//! returning a [ThreadedAsyncCamera] which implements the [AsyncCamera] trait.
//!
//! For [ThreadedAsyncCameraModule] to work, it requires that the wrapped camera
//! type `C` implements the [ci2::Camera] and [Send] traits. It operates by
//! serializing access to the camera by wrapping `Arc<Mutex<C>>`. The
//! [AsyncCamera::frames] method spawns a thread on on which an infinite loop is
//! used to grab frames from the camera. Therefore other camera access happens
//! only between frame acquisitions. Thus, when image exposure times are on the
//! order of 10 msec, this calls to access the camera (e.g. to chance exposure
//! time) may block for about 10 msec.
//!
//! The structs [ThreadedAsyncCameraModule] and [ThreadedAsyncCamera] here are a
//! generic implementation that can be used at the cost of spawning a new
//! thread.
//!
//! It would be possible for an upstream camera backend module to directly
//! implement the [AsyncCamera] trait. Such a camera-specific backend could
//! implement [AsyncCamera] without serializing access to the camera but rather
//! by taking advantage of functionality in most camera drivers.

#[macro_use]
extern crate log;

use futures::Stream;

use basic_frame::DynamicFrame;
use machine_vision_formats as formats;

use ci2::Result;
use parking_lot::Mutex;
use std::sync::Arc;

pub enum FrameResult {
    Frame(DynamicFrame),
    SingleFrameError(String),
}

/// Defines a method to return a stream of frames.
pub trait AsyncCamera {
    /// asynchronous frame acquisition, get an infinite stream of frames
    fn frames(
        &mut self,
        bufsize: usize,
    ) -> Result<Box<dyn Stream<Item = FrameResult> + Send + Unpin>>;
}

pub struct ThreadedAsyncCamera<C> {
    camera: Arc<Mutex<C>>,
    name: String,
    serial: String,
    model: String,
    vendor: String,
    /// When acquiring, has value of Some, else None.
    control_and_join_handle: Option<(thread_control::Control, std::thread::JoinHandle<()>)>,
}

fn _test_camera_is_send() {
    // Compile-time test to ensure ThreadedAsyncCamera implements Send trait.
    fn implements<T: Send>() {}
    implements::<ThreadedAsyncCamera<i8>>();
}

pub struct ThreadedAsyncCameraModule<M, C, G>
where
    M: Send,
    G: Send,
{
    cam_module: M,
    name: String,
    camera_type: std::marker::PhantomData<C>,
    guard_type: std::marker::PhantomData<G>,
}

impl<C: 'static> ThreadedAsyncCamera<C>
where
    C: ci2::Camera + Send,
{
    pub fn control_and_join_handle(
        self,
    ) -> Option<(thread_control::Control, std::thread::JoinHandle<()>)> {
        self.control_and_join_handle
    }
}

impl<C> AsyncCamera for ThreadedAsyncCamera<C>
where
    C: 'static + ci2::Camera + Send,
{
    fn frames(
        &mut self,
        bufsize: usize,
    ) -> Result<Box<dyn Stream<Item = FrameResult> + Send + Unpin>> {
        if self.control_and_join_handle.is_some() {
            return Err(ci2::Error::from("already launched thread"));
        }

        let (mut tx, rx) = futures::channel::mpsc::channel(bufsize);

        let (flag, control) = thread_control::make_pair();

        let thread_builder =
            std::thread::Builder::new().name(format!("ThreadedAsyncCamera-{}", self.name));
        let cam_arc = self.camera.clone();
        let join_handle: std::thread::JoinHandle<()> = thread_builder.spawn(move || {
            while flag.is_alive() {
                // We need to release and re-acquire the lock every cycle to
                // allow other threads the chance to grab the lock.
                {
                    let mut cam = cam_arc.lock();
                    let msg = match cam.next_frame() {
                        Ok(frame) => FrameResult::Frame(frame),
                        Err(ci2::Error::SingleFrameError(s)) => FrameResult::SingleFrameError(s),
                        Err(e) => {
                            error!(
                                "fatal error acquiring frames: {} {:?} {}:{}",
                                e,
                                e,
                                file!(),
                                line!()
                            );
                            return;
                        }
                    };

                    match tx.try_send(msg) {
                        Ok(()) => {} // message put in channel ok
                        Err(e) => {
                            if e.is_full() {
                                // channel was full
                                error!("dropping message due to backpressure");
                            }
                            if e.is_disconnected() {
                                debug!("ThreadedAsyncCamera listener disconnected");
                                return;
                            }
                        }
                    };
                }
            }
            debug!(
                "closing thread {:?} ({:?}) in {}:{}",
                std::thread::current().name(),
                std::thread::current().id(),
                file!(),
                line!()
            );
        })?;

        self.control_and_join_handle = Some((control, join_handle));

        Ok(Box::new(rx))
    }
}

impl<M, C, G> ThreadedAsyncCameraModule<M, C, G>
where
    M: ci2::CameraModule<CameraType = C, Guard = G>,
    C: ci2::Camera,
    G: Send,
{
    pub fn threaded_async_camera(&mut self, name: &str) -> Result<ThreadedAsyncCamera<C>> {
        let camera = self.cam_module.camera(name)?;
        let name = camera.name().into();
        let model = camera.name().into();
        let serial = camera.serial().into();
        let vendor = camera.vendor().into();

        Ok(ThreadedAsyncCamera {
            camera: Arc::new(Mutex::new(camera)),
            name,
            model,
            vendor,
            serial,
            control_and_join_handle: None,
        })
    }
}

pub fn into_threaded_async<M, C, G>(cam_module: M, _guard: &G) -> ThreadedAsyncCameraModule<M, C, G>
where
    M: ci2::CameraModule<CameraType = C, Guard = G>,
    C: ci2::Camera,
    G: Send,
{
    let name = format!("async-{}", cam_module.name());

    ThreadedAsyncCameraModule {
        cam_module,
        name,
        camera_type: std::marker::PhantomData,
        guard_type: std::marker::PhantomData,
    }
}

// ----

impl<C> ci2::CameraInfo for ThreadedAsyncCamera<C>
where
    C: ci2::CameraInfo,
{
    fn name(&self) -> &str {
        &self.name
    }
    fn serial(&self) -> &str {
        &self.serial
    }
    fn model(&self) -> &str {
        &self.model
    }
    fn vendor(&self) -> &str {
        &self.vendor
    }
}

impl<C> ci2::Camera for ThreadedAsyncCamera<C>
where
    C: ci2::Camera,
{
    // ----- start: weakly typed but easier to implement API -----

    // fn feature_access_query(&self, name: &str) -> ci2::Result<ci2::AccessQueryResult> {
    //     let c = self.camera.lock();
    //     c.feature_access_query(name)
    // }

    fn command_execute(&self, name: &str, verify: bool) -> ci2::Result<()> {
        let c = self.camera.lock();
        c.command_execute(name, verify)
    }

    fn feature_bool(&self, name: &str) -> ci2::Result<bool> {
        let c = self.camera.lock();
        c.feature_bool(name)
    }

    fn feature_bool_set(&self, name: &str, value: bool) -> ci2::Result<()> {
        let c = self.camera.lock();
        c.feature_bool_set(name, value)
    }

    fn feature_enum(&self, name: &str) -> ci2::Result<String> {
        let c = self.camera.lock();
        c.feature_enum(name)
    }

    fn feature_enum_set(&self, name: &str, value: &str) -> ci2::Result<()> {
        let c = self.camera.lock();
        c.feature_enum_set(name, value)
    }

    fn feature_float(&self, name: &str) -> ci2::Result<f64> {
        let c = self.camera.lock();
        c.feature_float(name)
    }

    fn feature_float_set(&self, name: &str, value: f64) -> ci2::Result<()> {
        let c = self.camera.lock();
        c.feature_float_set(name, value)
    }

    fn feature_int(&self, name: &str) -> ci2::Result<i64> {
        let c = self.camera.lock();
        c.feature_int(name)
    }

    fn feature_int_set(&self, name: &str, value: i64) -> ci2::Result<()> {
        let c = self.camera.lock();
        c.feature_int_set(name, value)
    }

    // ----- end: weakly typed but easier to implement API -----

    fn node_map_load(&self, settings: &str) -> Result<()> {
        let c = self.camera.lock();
        c.node_map_load(settings)
    }
    fn node_map_save(&self) -> Result<String> {
        let c = self.camera.lock();
        c.node_map_save()
    }

    fn width(&self) -> ci2::Result<u32> {
        let c = self.camera.lock();
        c.width()
    }
    fn height(&self) -> ci2::Result<u32> {
        let c = self.camera.lock();
        c.height()
    }
    fn pixel_format(&self) -> ci2::Result<formats::PixFmt> {
        let c = self.camera.lock();
        c.pixel_format()
    }
    fn possible_pixel_formats(&self) -> ci2::Result<Vec<formats::PixFmt>> {
        let c = self.camera.lock();
        c.possible_pixel_formats()
    }
    fn set_pixel_format(&mut self, pixel_format: formats::PixFmt) -> ci2::Result<()> {
        let mut c = self.camera.lock();
        c.set_pixel_format(pixel_format)
    }
    fn exposure_time(&self) -> ci2::Result<f64> {
        let c = self.camera.lock();
        c.exposure_time()
    }
    fn exposure_time_range(&self) -> ci2::Result<(f64, f64)> {
        let c = self.camera.lock();
        c.exposure_time_range()
    }
    fn set_exposure_time(&mut self, value: f64) -> ci2::Result<()> {
        let mut c = self.camera.lock();
        c.set_exposure_time(value)
    }
    fn gain(&self) -> ci2::Result<f64> {
        let c = self.camera.lock();
        c.gain()
    }
    fn gain_range(&self) -> ci2::Result<(f64, f64)> {
        let c = self.camera.lock();
        c.gain_range()
    }
    fn set_gain(&mut self, value: f64) -> ci2::Result<()> {
        let mut c = self.camera.lock();
        c.set_gain(value)
    }
    fn exposure_auto(&self) -> ci2::Result<ci2::AutoMode> {
        let c = self.camera.lock();
        c.exposure_auto()
    }
    fn set_exposure_auto(&mut self, value: ci2::AutoMode) -> ci2::Result<()> {
        let mut c = self.camera.lock();
        c.set_exposure_auto(value)
    }
    fn gain_auto(&self) -> ci2::Result<ci2::AutoMode> {
        let c = self.camera.lock();
        c.gain_auto()
    }
    fn set_gain_auto(&mut self, value: ci2::AutoMode) -> ci2::Result<()> {
        let mut c = self.camera.lock();
        c.set_gain_auto(value)
    }

    fn start_default_external_triggering(&mut self) -> ci2::Result<()> {
        let mut c = self.camera.lock();
        c.start_default_external_triggering()
    }

    fn set_software_frame_rate_limit(&mut self, fps_limit: f64) -> ci2::Result<()> {
        let mut c = self.camera.lock();
        c.set_software_frame_rate_limit(fps_limit)
    }

    fn trigger_mode(&self) -> ci2::Result<ci2::TriggerMode> {
        let c = self.camera.lock();
        c.trigger_mode()
    }
    fn set_trigger_mode(&mut self, value: ci2::TriggerMode) -> ci2::Result<()> {
        let mut c = self.camera.lock();
        c.set_trigger_mode(value)
    }

    fn acquisition_frame_rate_enable(&self) -> ci2::Result<bool> {
        let c = self.camera.lock();
        c.acquisition_frame_rate_enable()
    }
    fn set_acquisition_frame_rate_enable(&mut self, value: bool) -> ci2::Result<()> {
        let mut c = self.camera.lock();
        c.set_acquisition_frame_rate_enable(value)
    }

    fn acquisition_frame_rate(&self) -> ci2::Result<f64> {
        let c = self.camera.lock();
        c.acquisition_frame_rate()
    }
    fn acquisition_frame_rate_range(&self) -> ci2::Result<(f64, f64)> {
        let c = self.camera.lock();
        c.acquisition_frame_rate_range()
    }
    fn set_acquisition_frame_rate(&mut self, value: f64) -> ci2::Result<()> {
        let mut c = self.camera.lock();
        c.set_acquisition_frame_rate(value)
    }

    fn trigger_selector(&self) -> ci2::Result<ci2::TriggerSelector> {
        let c = self.camera.lock();
        c.trigger_selector()
    }
    fn set_trigger_selector(&mut self, value: ci2::TriggerSelector) -> ci2::Result<()> {
        let mut c = self.camera.lock();
        c.set_trigger_selector(value)
    }

    fn acquisition_mode(&self) -> ci2::Result<ci2::AcquisitionMode> {
        let c = self.camera.lock();
        c.acquisition_mode()
    }
    fn set_acquisition_mode(&mut self, value: ci2::AcquisitionMode) -> ci2::Result<()> {
        let mut c = self.camera.lock();
        c.set_acquisition_mode(value)
    }

    fn acquisition_start(&mut self) -> ci2::Result<()> {
        let mut c = self.camera.lock();
        c.acquisition_start()
    }
    fn acquisition_stop(&mut self) -> ci2::Result<()> {
        let mut c = self.camera.lock();
        c.acquisition_stop()
    }

    /// blocks forever.
    fn next_frame(&mut self) -> ci2::Result<DynamicFrame> {
        let mut c = self.camera.lock();
        c.next_frame()
    }
}

impl<M, C, G> ci2::CameraModule for ThreadedAsyncCameraModule<M, C, G>
where
    M: ci2::CameraModule<CameraType = C, Guard = G>,
    C: ci2::Camera,
    G: Send,
{
    type CameraType = C;
    type Guard = G;

    fn name(&self) -> &str {
        self.name.as_ref()
    }
    fn camera_infos(&self) -> Result<Vec<Box<dyn ci2::CameraInfo>>> {
        self.cam_module.camera_infos()
    }
    fn camera(&mut self, name: &str) -> Result<C> {
        self.cam_module.camera(name)
    }

    fn settings_file_extension(&self) -> &str {
        self.cam_module.settings_file_extension()
    }

    fn frame_info_extractor(&self) -> &'static dyn ci2::ExtractFrameInfo {
        self.cam_module.frame_info_extractor()
    }
}