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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from ../gir-files
// DO NOT EDIT

use crate::Buffer;
use crate::Device;
use glib::object::Cast;
use glib::object::IsA;
use glib::signal::connect_raw;
use glib::signal::SignalHandlerId;
use glib::translate::*;
use glib::StaticType;
use std::boxed::Box as Box_;
use std::fmt;
use std::mem;
use std::mem::transmute;

glib::wrapper! {
	#[doc(alias = "ArvStream")]
	pub struct Stream(Object<ffi::ArvStream, ffi::ArvStreamClass>);

	match fn {
		type_ => || ffi::arv_stream_get_type(),
	}
}

unsafe impl Send for Stream {}

pub const NONE_STREAM: Option<&Stream> = None;

/// Trait containing all [`struct@Stream`] methods.
///
/// # Implementors
///
/// [`FakeStream`][struct@crate::FakeStream], [`GvStream`][struct@crate::GvStream], [`Stream`][struct@crate::Stream], [`UvStream`][struct@crate::UvStream]
pub trait StreamExt: 'static {
	/// Check if stream will emit its signals.
	///
	/// # Returns
	///
	/// [`true`] if `self` is emiting its signals.
	#[doc(alias = "arv_stream_get_emit_signals")]
	#[doc(alias = "get_emit_signals")]
	fn emits_signals(&self) -> bool;

	/// ## `id`
	/// info id
	///
	/// # Returns
	///
	/// the value of the corresponding stream information, as a double.
	#[cfg(any(feature = "v0_8_11", feature = "dox"))]
	#[cfg_attr(feature = "dox", doc(cfg(feature = "v0_8_11")))]
	#[doc(alias = "arv_stream_get_info_double")]
	#[doc(alias = "get_info_double")]
	fn info_double(&self, id: u32) -> f64;

	/// ## `name`
	/// info name
	///
	/// # Returns
	///
	/// the value of the corresponding stream information, as a double.
	#[cfg(any(feature = "v0_8_11", feature = "dox"))]
	#[cfg_attr(feature = "dox", doc(cfg(feature = "v0_8_11")))]
	#[doc(alias = "arv_stream_get_info_double_by_name")]
	#[doc(alias = "get_info_double_by_name")]
	fn info_double_by_name(&self, name: &str) -> f64;

	/// ## `id`
	/// info id
	///
	/// # Returns
	///
	/// the name of the corresponding stream information.
	#[cfg(any(feature = "v0_8_11", feature = "dox"))]
	#[cfg_attr(feature = "dox", doc(cfg(feature = "v0_8_11")))]
	#[doc(alias = "arv_stream_get_info_name")]
	#[doc(alias = "get_info_name")]
	fn info_name(&self, id: u32) -> Option<glib::GString>;

	/// ## `id`
	/// info id
	///
	/// # Returns
	///
	/// the `GType` of the corresponding stream information, which indicates what API to use to retrieve the
	/// information value ([`info_uint64()`][Self::info_uint64()] or [`info_double()`][Self::info_double()]).
	#[cfg(any(feature = "v0_8_11", feature = "dox"))]
	#[cfg_attr(feature = "dox", doc(cfg(feature = "v0_8_11")))]
	#[doc(alias = "arv_stream_get_info_type")]
	#[doc(alias = "get_info_type")]
	fn info_type(&self, id: u32) -> glib::types::Type;

	/// ## `id`
	/// info id
	///
	/// # Returns
	///
	/// the value of the corresponding stream information, as a 64 bit unsigned integer.
	#[cfg(any(feature = "v0_8_11", feature = "dox"))]
	#[cfg_attr(feature = "dox", doc(cfg(feature = "v0_8_11")))]
	#[doc(alias = "arv_stream_get_info_uint64")]
	#[doc(alias = "get_info_uint64")]
	fn info_uint64(&self, id: u32) -> u64;

	/// ## `name`
	/// info name
	///
	/// # Returns
	///
	/// the value of the corresponding stream information, as a 64 bit unsigned integer.
	#[cfg(any(feature = "v0_8_11", feature = "dox"))]
	#[cfg_attr(feature = "dox", doc(cfg(feature = "v0_8_11")))]
	#[doc(alias = "arv_stream_get_info_uint64_by_name")]
	#[doc(alias = "get_info_uint64_by_name")]
	fn info_uint64_by_name(&self, name: &str) -> u64;

	/// An accessor to the stream buffer queue lengths.
	///
	/// # Returns
	///
	///
	/// ## `n_input_buffers`
	/// input queue length
	///
	/// ## `n_output_buffers`
	/// output queue length
	#[doc(alias = "arv_stream_get_n_buffers")]
	#[doc(alias = "get_n_buffers")]
	fn n_buffers(&self) -> (i32, i32);

	///
	/// # Returns
	///
	/// the number of stream informations. These informations contain useful numbers about data transfer quality.
	#[cfg(any(feature = "v0_8_11", feature = "dox"))]
	#[cfg_attr(feature = "dox", doc(cfg(feature = "v0_8_11")))]
	#[doc(alias = "arv_stream_get_n_infos")]
	#[doc(alias = "get_n_infos")]
	fn n_infos(&self) -> u32;

	/// An accessor to the stream statistics.
	///
	/// # Returns
	///
	///
	/// ## `n_completed_buffers`
	/// number of complete received buffers
	///
	/// ## `n_failures`
	/// number of reception failures
	///
	/// ## `n_underruns`
	/// number of input buffer underruns
	#[doc(alias = "arv_stream_get_statistics")]
	#[doc(alias = "get_statistics")]
	fn statistics(&self) -> (u64, u64, u64);

	/// Pops a buffer from the output queue of `self`. The retrieved buffer
	/// may contain an invalid image. Caller should check the buffer status before using it.
	/// This function blocks until a buffer is available.
	///
	/// This method is thread safe.
	///
	/// # Returns
	///
	/// a [`Buffer`][crate::Buffer]
	#[doc(alias = "arv_stream_pop_buffer")]
	fn pop_buffer(&self) -> Option<Buffer>;

	/// Pushes a [`Buffer`][crate::Buffer] to the `self` thread. The `self` takes ownership of `buffer`,
	/// and will free all the buffers still in its queues when destroyed.
	///
	/// This method is thread safe.
	/// ## `buffer`
	/// buffer to push
	#[doc(alias = "arv_stream_push_buffer")]
	fn push_buffer(&self, buffer: &Buffer);

	/// Make `self` emit signals. This option is
	/// by default disabled because signal emission is expensive and unneeded when
	/// the application prefers to operate in pull mode.
	/// ## `emit_signals`
	/// the new state
	#[doc(alias = "arv_stream_set_emit_signals")]
	fn set_emit_signals(&self, emit_signals: bool);

	/// Start the stream receiving thread. The thread is automatically started when
	/// the [`Stream`][crate::Stream] object is instantiated, so this function is only useful if
	/// the thread was stopped using [`stop_thread()`][Self::stop_thread()].
	#[doc(alias = "arv_stream_start_thread")]
	fn start_thread(&self);

	/// Stop the stream receiving thread, and optionally delete all the [`Buffer`][crate::Buffer]
	/// stored in the stream object queues. Main use of this function is to be able
	/// to quickly change an acquisition parameter that changes the payload size,
	/// without deleting/recreating the stream object.
	/// ## `delete_buffers`
	/// enable buffer deletion
	///
	/// # Returns
	///
	/// the number of deleted buffers if `delete_buffers` == [`true`], 0 otherwise.
	#[doc(alias = "arv_stream_stop_thread")]
	fn stop_thread(&self, delete_buffers: bool) -> u32;

	/// Pops a buffer from the output queue of `self`, waiting no more than `timeout`. The retrieved buffer
	/// may contain an invalid image. Caller should check the buffer status before using it.
	///
	/// This method is thread safe.
	/// ## `timeout`
	/// timeout, in µs
	///
	/// # Returns
	///
	/// a [`Buffer`][crate::Buffer], NULL if no buffer is available until the timeout occurs.
	#[doc(alias = "arv_stream_timeout_pop_buffer")]
	fn timeout_pop_buffer(&self, timeout: u64) -> Option<Buffer>;

	/// Pops a buffer from the output queue of `self`. The retrieved buffer
	/// may contain an invalid image. Caller should check the buffer status before using it.
	/// This is the non blocking version of pop_buffer.
	///
	/// This method is thread safe.
	///
	/// # Returns
	///
	/// a [`Buffer`][crate::Buffer], NULL if no buffer is available.
	#[doc(alias = "arv_stream_try_pop_buffer")]
	fn try_pop_buffer(&self) -> Option<Buffer>;

	//fn callback(&self) -> /*Unimplemented*/Fundamental: Pointer;

	//#[doc(alias = "callback-data")]
	//fn callback_data(&self) -> /*Unimplemented*/Fundamental: Pointer;

	fn device(&self) -> Option<Device>;

	/// Signal that a new buffer is available.
	///
	/// This signal is emited from the stream receive thread and only when the
	/// "emit-signals" property is [`true`].
	///
	/// The new buffer can be retrieved with [`pop_buffer()`][Self::pop_buffer()].
	///
	/// Note that this signal is only emited when the "emit-signals" property is
	/// set to [`true`], which it is not by default for performance reasons.
	#[doc(alias = "new-buffer")]
	fn connect_new_buffer<F: Fn(&Self) + Send + 'static>(&self, f: F) -> SignalHandlerId;

	#[doc(alias = "emit-signals")]
	fn connect_emit_signals_notify<F: Fn(&Self) + Send + 'static>(&self, f: F) -> SignalHandlerId;
}

impl<O: IsA<Stream>> StreamExt for O {
	fn emits_signals(&self) -> bool {
		unsafe {
			from_glib(ffi::arv_stream_get_emit_signals(
				self.as_ref().to_glib_none().0,
			))
		}
	}

	#[cfg(any(feature = "v0_8_11", feature = "dox"))]
	#[cfg_attr(feature = "dox", doc(cfg(feature = "v0_8_11")))]
	fn info_double(&self, id: u32) -> f64 {
		unsafe { ffi::arv_stream_get_info_double(self.as_ref().to_glib_none().0, id) }
	}

	#[cfg(any(feature = "v0_8_11", feature = "dox"))]
	#[cfg_attr(feature = "dox", doc(cfg(feature = "v0_8_11")))]
	fn info_double_by_name(&self, name: &str) -> f64 {
		unsafe {
			ffi::arv_stream_get_info_double_by_name(
				self.as_ref().to_glib_none().0,
				name.to_glib_none().0,
			)
		}
	}

	#[cfg(any(feature = "v0_8_11", feature = "dox"))]
	#[cfg_attr(feature = "dox", doc(cfg(feature = "v0_8_11")))]
	fn info_name(&self, id: u32) -> Option<glib::GString> {
		unsafe {
			from_glib_none(ffi::arv_stream_get_info_name(
				self.as_ref().to_glib_none().0,
				id,
			))
		}
	}

	#[cfg(any(feature = "v0_8_11", feature = "dox"))]
	#[cfg_attr(feature = "dox", doc(cfg(feature = "v0_8_11")))]
	fn info_type(&self, id: u32) -> glib::types::Type {
		unsafe {
			from_glib(ffi::arv_stream_get_info_type(
				self.as_ref().to_glib_none().0,
				id,
			))
		}
	}

	#[cfg(any(feature = "v0_8_11", feature = "dox"))]
	#[cfg_attr(feature = "dox", doc(cfg(feature = "v0_8_11")))]
	fn info_uint64(&self, id: u32) -> u64 {
		unsafe { ffi::arv_stream_get_info_uint64(self.as_ref().to_glib_none().0, id) }
	}

	#[cfg(any(feature = "v0_8_11", feature = "dox"))]
	#[cfg_attr(feature = "dox", doc(cfg(feature = "v0_8_11")))]
	fn info_uint64_by_name(&self, name: &str) -> u64 {
		unsafe {
			ffi::arv_stream_get_info_uint64_by_name(
				self.as_ref().to_glib_none().0,
				name.to_glib_none().0,
			)
		}
	}

	fn n_buffers(&self) -> (i32, i32) {
		unsafe {
			let mut n_input_buffers = mem::MaybeUninit::uninit();
			let mut n_output_buffers = mem::MaybeUninit::uninit();
			ffi::arv_stream_get_n_buffers(
				self.as_ref().to_glib_none().0,
				n_input_buffers.as_mut_ptr(),
				n_output_buffers.as_mut_ptr(),
			);
			let n_input_buffers = n_input_buffers.assume_init();
			let n_output_buffers = n_output_buffers.assume_init();
			(n_input_buffers, n_output_buffers)
		}
	}

	#[cfg(any(feature = "v0_8_11", feature = "dox"))]
	#[cfg_attr(feature = "dox", doc(cfg(feature = "v0_8_11")))]
	fn n_infos(&self) -> u32 {
		unsafe { ffi::arv_stream_get_n_infos(self.as_ref().to_glib_none().0) }
	}

	fn statistics(&self) -> (u64, u64, u64) {
		unsafe {
			let mut n_completed_buffers = mem::MaybeUninit::uninit();
			let mut n_failures = mem::MaybeUninit::uninit();
			let mut n_underruns = mem::MaybeUninit::uninit();
			ffi::arv_stream_get_statistics(
				self.as_ref().to_glib_none().0,
				n_completed_buffers.as_mut_ptr(),
				n_failures.as_mut_ptr(),
				n_underruns.as_mut_ptr(),
			);
			let n_completed_buffers = n_completed_buffers.assume_init();
			let n_failures = n_failures.assume_init();
			let n_underruns = n_underruns.assume_init();
			(n_completed_buffers, n_failures, n_underruns)
		}
	}

	fn pop_buffer(&self) -> Option<Buffer> {
		unsafe { from_glib_full(ffi::arv_stream_pop_buffer(self.as_ref().to_glib_none().0)) }
	}

	fn push_buffer(&self, buffer: &Buffer) {
		unsafe {
			ffi::arv_stream_push_buffer(self.as_ref().to_glib_none().0, buffer.to_glib_full());
		}
	}

	fn set_emit_signals(&self, emit_signals: bool) {
		unsafe {
			ffi::arv_stream_set_emit_signals(
				self.as_ref().to_glib_none().0,
				emit_signals.into_glib(),
			);
		}
	}

	fn start_thread(&self) {
		unsafe {
			ffi::arv_stream_start_thread(self.as_ref().to_glib_none().0);
		}
	}

	fn stop_thread(&self, delete_buffers: bool) -> u32 {
		unsafe {
			ffi::arv_stream_stop_thread(self.as_ref().to_glib_none().0, delete_buffers.into_glib())
		}
	}

	fn timeout_pop_buffer(&self, timeout: u64) -> Option<Buffer> {
		unsafe {
			from_glib_full(ffi::arv_stream_timeout_pop_buffer(
				self.as_ref().to_glib_none().0,
				timeout,
			))
		}
	}

	fn try_pop_buffer(&self) -> Option<Buffer> {
		unsafe {
			from_glib_full(ffi::arv_stream_try_pop_buffer(
				self.as_ref().to_glib_none().0,
			))
		}
	}

	//fn callback(&self) -> /*Unimplemented*/Fundamental: Pointer {
	//    unsafe {
	//        let mut value = glib::Value::from_type(</*Unknown type*/ as StaticType>::static_type());
	//        glib::gobject_ffi::g_object_get_property(self.to_glib_none().0 as *mut glib::gobject_ffi::GObject, b"callback\0".as_ptr() as *const _, value.to_glib_none_mut().0);
	//        value.get().expect("Return Value for property `callback` getter")
	//    }
	//}

	//fn callback_data(&self) -> /*Unimplemented*/Fundamental: Pointer {
	//    unsafe {
	//        let mut value = glib::Value::from_type(</*Unknown type*/ as StaticType>::static_type());
	//        glib::gobject_ffi::g_object_get_property(self.to_glib_none().0 as *mut glib::gobject_ffi::GObject, b"callback-data\0".as_ptr() as *const _, value.to_glib_none_mut().0);
	//        value.get().expect("Return Value for property `callback-data` getter")
	//    }
	//}

	fn device(&self) -> Option<Device> {
		unsafe {
			let mut value = glib::Value::from_type(<Device as StaticType>::static_type());
			glib::gobject_ffi::g_object_get_property(
				self.to_glib_none().0 as *mut glib::gobject_ffi::GObject,
				b"device\0".as_ptr() as *const _,
				value.to_glib_none_mut().0,
			);
			value
				.get()
				.expect("Return Value for property `device` getter")
		}
	}

	fn connect_new_buffer<F: Fn(&Self) + Send + 'static>(&self, f: F) -> SignalHandlerId {
		unsafe extern "C" fn new_buffer_trampoline<P: IsA<Stream>, F: Fn(&P) + Send + 'static>(
			this: *mut ffi::ArvStream,
			f: glib::ffi::gpointer,
		) {
			let f: &F = &*(f as *const F);
			f(Stream::from_glib_borrow(this).unsafe_cast_ref())
		}
		unsafe {
			let f: Box_<F> = Box_::new(f);
			connect_raw(
				self.as_ptr() as *mut _,
				b"new-buffer\0".as_ptr() as *const _,
				Some(transmute::<_, unsafe extern "C" fn()>(
					new_buffer_trampoline::<Self, F> as *const (),
				)),
				Box_::into_raw(f),
			)
		}
	}

	fn connect_emit_signals_notify<F: Fn(&Self) + Send + 'static>(&self, f: F) -> SignalHandlerId {
		unsafe extern "C" fn notify_emit_signals_trampoline<
			P: IsA<Stream>,
			F: Fn(&P) + Send + 'static,
		>(
			this: *mut ffi::ArvStream,
			_param_spec: glib::ffi::gpointer,
			f: glib::ffi::gpointer,
		) {
			let f: &F = &*(f as *const F);
			f(Stream::from_glib_borrow(this).unsafe_cast_ref())
		}
		unsafe {
			let f: Box_<F> = Box_::new(f);
			connect_raw(
				self.as_ptr() as *mut _,
				b"notify::emit-signals\0".as_ptr() as *const _,
				Some(transmute::<_, unsafe extern "C" fn()>(
					notify_emit_signals_trampoline::<Self, F> as *const (),
				)),
				Box_::into_raw(f),
			)
		}
	}
}

impl fmt::Display for Stream {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		f.write_str("Stream")
	}
}