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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
|
/* Copyright (C) 2011, 2012 Matthias Vogelgesang <matthias.vogelgesang@kit.edu>
(Karlsruhe Institute of Technology)
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc., 51
Franklin St, Fifth Floor, Boston, MA 02110, USA */
/**
* SECTION:uca-camera
* @Short_description: Base class representing a camera
* @Title: UcaCamera
*
* UcaCamera is the base camera from which a real hardware camera derives from.
*/
#include <glib.h>
#include <string.h>
#include "config.h"
#include "compat.h"
#include "uca-camera.h"
#include "uca-ring-buffer.h"
#include "uca-enums.h"
#define UCA_CAMERA_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), UCA_TYPE_CAMERA, UcaCameraPrivate))
G_DEFINE_TYPE(UcaCamera, uca_camera, G_TYPE_OBJECT)
/**
* UcaCameraTrigger:
* @UCA_CAMERA_TRIGGER_AUTO: Trigger automatically
* @UCA_CAMERA_TRIGGER_EXTERNAL: Trigger from an external source
* @UCA_CAMERA_TRIGGER_SOFTWARE: Trigger from software using
* #uca_camera_trigger
*/
/**
* UcaCameraError:
* @UCA_CAMERA_ERROR_NOT_FOUND: Camera type is unknown
* @UCA_CAMERA_ERROR_RECORDING: Camera is already recording
* @UCA_CAMERA_ERROR_NOT_RECORDING: Camera is not recording
* @UCA_CAMERA_ERROR_NO_GRAB_FUNC: No grab callback was set
* @UCA_CAMERA_ERROR_NOT_IMPLEMENTED: Virtual function is not implemented
* @UCA_CAMERA_ERROR_END_OF_STREAM: Data stream has ended.
*/
GQuark uca_camera_error_quark()
{
return g_quark_from_static_string ("uca-camera-error-quark");
}
/**
* UcaUnit:
* @UCA_UNIT_NA: Not applicable
* @UCA_UNIT_METER: Length in SI meter
* @UCA_UNIT_SECOND: Time in SI second
* @UCA_UNIT_PIXEL: Number of pixels in one dimension
* @UCA_UNIT_DEGREE_CELSIUS: Temperature in degree Celsius
* @UCA_UNIT_COUNT: Generic number
*
* Units should be registered by camera implementations using
* uca_camera_register_unit() and can be queried by client programs with
* uca_camera_get_unit().
*/
GQuark uca_unit_quark ()
{
return g_quark_from_static_string ("uca-unit-quark");
}
enum {
LAST_SIGNAL
};
/*
* These strings must UNDER ALL CIRCUMSTANCES match the property id's in the
* public header! Everyone relies on this relationship.
*/
const gchar *uca_camera_props[N_BASE_PROPERTIES] = {
NULL,
"name",
"sensor-width",
"sensor-height",
"sensor-pixel-width",
"sensor-pixel-height",
"sensor-bitdepth",
"sensor-horizontal-binning",
"sensor-horizontal-binnings",
"sensor-vertical-binning",
"sensor-vertical-binnings",
"sensor-max-frame-rate",
"trigger-mode",
"exposure-time",
"frames-per-second",
"roi-x0",
"roi-y0",
"roi-width",
"roi-height",
"roi-width-multiplier",
"roi-height-multiplier",
"has-streaming",
"has-camram-recording",
"recorded-frames",
"transfer-asynchronously",
"is-recording",
"is-readout",
"buffered",
"num-buffers",
};
static GParamSpec *camera_properties[N_BASE_PROPERTIES] = { NULL, };
static GStaticMutex access_lock = G_STATIC_MUTEX_INIT;
struct _UcaCameraPrivate {
gboolean is_recording;
gboolean is_readout;
gboolean transfer_async;
gboolean buffered;
guint num_buffers;
GThread *read_thread;
UcaRingBuffer *ring_buffer;
UcaCameraTrigger trigger;
GValueArray *h_binnings;
GValueArray *v_binnings;
};
static void
uca_camera_set_property_unit (GParamSpec *pspec, UcaUnit unit)
{
g_param_spec_set_qdata (pspec, UCA_UNIT_QUARK, GINT_TO_POINTER (unit));
}
static void
uca_camera_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
{
UcaCameraPrivate *priv = UCA_CAMERA_GET_PRIVATE(object);
if (priv->is_recording) {
g_warning("You cannot change properties during data acquisition");
return;
}
switch (property_id) {
case PROP_TRANSFER_ASYNCHRONOUSLY:
priv->transfer_async = g_value_get_boolean(value);
break;
case PROP_FRAMES_PER_SECOND:
{
gdouble frames_per_second;
frames_per_second = g_value_get_double (value);
g_object_set (object, "exposure-time", 1. / frames_per_second, NULL);
}
break;
case PROP_TRIGGER_MODE:
priv->trigger = g_value_get_enum (value);
break;
case PROP_BUFFERED:
priv->buffered = g_value_get_boolean (value);
break;
case PROP_NUM_BUFFERS:
priv->num_buffers = g_value_get_uint (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
}
}
static void
uca_camera_get_property(GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
{
UcaCameraPrivate *priv = UCA_CAMERA_GET_PRIVATE (object);
switch (property_id) {
case PROP_IS_RECORDING:
g_value_set_boolean (value, priv->is_recording);
break;
case PROP_IS_READOUT:
g_value_set_boolean (value, priv->is_readout);
break;
case PROP_TRANSFER_ASYNCHRONOUSLY:
g_value_set_boolean (value, priv->transfer_async);
break;
case PROP_TRIGGER_MODE:
g_value_set_enum (value, priv->trigger);
break;
case PROP_FRAMES_PER_SECOND:
{
gdouble exposure_time;
g_object_get (object, "exposure-time", &exposure_time, NULL);
g_value_set_double (value, 1. / exposure_time);
}
break;
case PROP_RECORDED_FRAMES:
g_value_set_uint (value, 0);
break;
case PROP_SENSOR_PIXEL_WIDTH:
/* 10um is an arbitrary default, cameras should definitely override
* this. */
g_value_set_double (value, 10e-6);
break;
case PROP_SENSOR_PIXEL_HEIGHT:
g_value_set_double (value, 10e-6);
break;
case PROP_SENSOR_HORIZONTAL_BINNING:
g_value_set_uint (value, 1);
break;
case PROP_SENSOR_VERTICAL_BINNING:
g_value_set_uint (value, 1);
break;
case PROP_SENSOR_HORIZONTAL_BINNINGS:
g_value_set_boxed (value, priv->h_binnings);
break;
case PROP_SENSOR_VERTICAL_BINNINGS:
g_value_set_boxed (value, priv->v_binnings);
break;
case PROP_ROI_WIDTH_MULTIPLIER:
g_value_set_uint (value, 1);
break;
case PROP_ROI_HEIGHT_MULTIPLIER:
g_value_set_uint (value, 1);
break;
case PROP_BUFFERED:
g_value_set_boolean (value, priv->buffered);
break;
case PROP_NUM_BUFFERS:
g_value_set_uint (value, priv->num_buffers);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
}
}
static void
uca_camera_dispose (GObject *object)
{
UcaCameraPrivate *priv;
priv = UCA_CAMERA_GET_PRIVATE (object);
if (priv->is_recording) {
GError *error;
uca_camera_stop_recording (UCA_CAMERA (object), &error);
g_warning ("Could not stop recording: %s", error->message);
g_error_free (error);
}
if (priv->ring_buffer != NULL) {
g_object_unref (priv->ring_buffer);
priv->ring_buffer = NULL;
}
G_OBJECT_CLASS (uca_camera_parent_class)->dispose (object);
}
static void
uca_camera_finalize (GObject *object)
{
UcaCameraPrivate *priv;
priv = UCA_CAMERA_GET_PRIVATE (object);
g_value_array_free (priv->h_binnings);
g_value_array_free (priv->v_binnings);
G_OBJECT_CLASS (uca_camera_parent_class)->finalize (object);
}
static void
uca_camera_class_init (UcaCameraClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
gobject_class->set_property = uca_camera_set_property;
gobject_class->get_property = uca_camera_get_property;
gobject_class->dispose = uca_camera_dispose;
gobject_class->finalize = uca_camera_finalize;
klass->start_recording = NULL;
klass->stop_recording = NULL;
klass->grab = NULL;
camera_properties[PROP_NAME] =
g_param_spec_string("name",
"Name of the camera",
"Name of the camera",
"", G_PARAM_READABLE);
camera_properties[PROP_SENSOR_WIDTH] =
g_param_spec_uint(uca_camera_props[PROP_SENSOR_WIDTH],
"Width of sensor",
"Width of the sensor in pixels",
1, G_MAXUINT, 1,
G_PARAM_READABLE);
camera_properties[PROP_SENSOR_HEIGHT] =
g_param_spec_uint(uca_camera_props[PROP_SENSOR_HEIGHT],
"Height of sensor",
"Height of the sensor in pixels",
1, G_MAXUINT, 1,
G_PARAM_READABLE);
camera_properties[PROP_SENSOR_PIXEL_WIDTH] =
g_param_spec_double (uca_camera_props[PROP_SENSOR_PIXEL_WIDTH],
"Width of sensor pixel in meters",
"Width of sensor pixel in meters",
G_MINDOUBLE, G_MAXDOUBLE, 10e-6,
G_PARAM_READABLE);
camera_properties[PROP_SENSOR_PIXEL_HEIGHT] =
g_param_spec_double (uca_camera_props[PROP_SENSOR_PIXEL_HEIGHT],
"Height of sensor pixel in meters",
"Height of sensor pixel in meters",
G_MINDOUBLE, G_MAXDOUBLE, 10e-6,
G_PARAM_READABLE);
camera_properties[PROP_SENSOR_BITDEPTH] =
g_param_spec_uint(uca_camera_props[PROP_SENSOR_BITDEPTH],
"Number of bits per pixel",
"Number of bits per pixel",
1, 32, 1,
G_PARAM_READABLE);
camera_properties[PROP_SENSOR_HORIZONTAL_BINNING] =
g_param_spec_uint(uca_camera_props[PROP_SENSOR_HORIZONTAL_BINNING],
"Horizontal binning",
"Number of sensor ADCs that are combined to one pixel in horizontal direction",
1, G_MAXUINT, 1,
G_PARAM_READWRITE);
camera_properties[PROP_SENSOR_HORIZONTAL_BINNINGS] =
g_param_spec_value_array(uca_camera_props[PROP_SENSOR_HORIZONTAL_BINNINGS],
"Array of possible binnings",
"Array of possible binnings in horizontal direction",
g_param_spec_uint(
uca_camera_props[PROP_SENSOR_HORIZONTAL_BINNING],
"Number of ADCs",
"Number of ADCs that make up one pixel",
1, G_MAXUINT, 1,
G_PARAM_READABLE), G_PARAM_READABLE);
camera_properties[PROP_SENSOR_VERTICAL_BINNING] =
g_param_spec_uint(uca_camera_props[PROP_SENSOR_VERTICAL_BINNING],
"Vertical binning",
"Number of sensor ADCs that are combined to one pixel in vertical direction",
1, G_MAXUINT, 1,
G_PARAM_READWRITE);
camera_properties[PROP_SENSOR_VERTICAL_BINNINGS] =
g_param_spec_value_array(uca_camera_props[PROP_SENSOR_VERTICAL_BINNINGS],
"Array of possible binnings",
"Array of possible binnings in vertical direction",
g_param_spec_uint(
uca_camera_props[PROP_SENSOR_VERTICAL_BINNING],
"Number of ADCs",
"Number of ADCs that make up one pixel",
1, G_MAXUINT, 1,
G_PARAM_READABLE), G_PARAM_READABLE);
camera_properties[PROP_SENSOR_MAX_FRAME_RATE] =
g_param_spec_float(uca_camera_props[PROP_SENSOR_MAX_FRAME_RATE],
"Maximum frame rate",
"Maximum frame rate at full frame resolution",
0.0f, G_MAXFLOAT, 1.0f,
G_PARAM_READABLE);
camera_properties[PROP_TRIGGER_MODE] =
g_param_spec_enum("trigger-mode",
"Trigger mode",
"Trigger mode",
UCA_TYPE_CAMERA_TRIGGER, UCA_CAMERA_TRIGGER_AUTO,
G_PARAM_READWRITE);
camera_properties[PROP_ROI_X] =
g_param_spec_uint(uca_camera_props[PROP_ROI_X],
"Horizontal coordinate",
"Horizontal coordinate",
0, G_MAXUINT, 0,
G_PARAM_READWRITE);
camera_properties[PROP_ROI_Y] =
g_param_spec_uint(uca_camera_props[PROP_ROI_Y],
"Vertical coordinate",
"Vertical coordinate",
0, G_MAXUINT, 0,
G_PARAM_READWRITE);
camera_properties[PROP_ROI_WIDTH] =
g_param_spec_uint(uca_camera_props[PROP_ROI_WIDTH],
"Width",
"Width of the region of interest",
1, G_MAXUINT, 1,
G_PARAM_READWRITE);
camera_properties[PROP_ROI_HEIGHT] =
g_param_spec_uint(uca_camera_props[PROP_ROI_HEIGHT],
"Height",
"Height of the region of interest",
1, G_MAXUINT, 1,
G_PARAM_READWRITE);
camera_properties[PROP_ROI_WIDTH_MULTIPLIER] =
g_param_spec_uint(uca_camera_props[PROP_ROI_WIDTH_MULTIPLIER],
"Horizontal ROI multiplier",
"Minimum possible step size of horizontal ROI",
1, G_MAXUINT, 1,
G_PARAM_READABLE);
camera_properties[PROP_ROI_HEIGHT_MULTIPLIER] =
g_param_spec_uint(uca_camera_props[PROP_ROI_HEIGHT_MULTIPLIER],
"Vertical ROI multiplier",
"Minimum possible step size of vertical ROI",
1, G_MAXUINT, 1,
G_PARAM_READABLE);
camera_properties[PROP_EXPOSURE_TIME] =
g_param_spec_double(uca_camera_props[PROP_EXPOSURE_TIME],
"Exposure time in seconds",
"Exposure time in seconds",
0.0, G_MAXDOUBLE, 1.0,
G_PARAM_READWRITE);
camera_properties[PROP_FRAMES_PER_SECOND] =
g_param_spec_double(uca_camera_props[PROP_FRAMES_PER_SECOND],
"Frames per second",
"Frames per second",
0.0, G_MAXDOUBLE, 1.0,
G_PARAM_READWRITE);
camera_properties[PROP_HAS_STREAMING] =
g_param_spec_boolean(uca_camera_props[PROP_HAS_STREAMING],
"Streaming capability",
"Is the camera able to stream the data",
TRUE, G_PARAM_READABLE);
camera_properties[PROP_HAS_CAMRAM_RECORDING] =
g_param_spec_boolean(uca_camera_props[PROP_HAS_CAMRAM_RECORDING],
"Cam-RAM capability",
"Is the camera able to record the data in-camera",
FALSE, G_PARAM_READABLE);
/**
* UcaCamera:recorded-frames
*
* Number of frames that are recorded into internal camera memory.
*
* Since: 1.1
*/
camera_properties[PROP_RECORDED_FRAMES] =
g_param_spec_uint(uca_camera_props[PROP_RECORDED_FRAMES],
"Number of frames recorded into internal camera memory",
"Number of frames recorded into internal camera memory",
0, G_MAXUINT, 0,
G_PARAM_READABLE);
camera_properties[PROP_TRANSFER_ASYNCHRONOUSLY] =
g_param_spec_boolean(uca_camera_props[PROP_TRANSFER_ASYNCHRONOUSLY],
"Specify whether data should be transfered asynchronously",
"Specify whether data should be transfered asynchronously using a specified callback",
FALSE, G_PARAM_READWRITE);
camera_properties[PROP_IS_RECORDING] =
g_param_spec_boolean(uca_camera_props[PROP_IS_RECORDING],
"Is camera recording",
"Is the camera currently recording",
FALSE, G_PARAM_READABLE);
camera_properties[PROP_IS_READOUT] =
g_param_spec_boolean(uca_camera_props[PROP_IS_READOUT],
"Is camera in readout mode",
"Is camera in readout mode",
FALSE, G_PARAM_READABLE);
camera_properties[PROP_BUFFERED] =
g_param_spec_boolean(uca_camera_props[PROP_BUFFERED],
"TRUE if libuca should buffer frames",
"TRUE if libuca should buffer frames",
FALSE, G_PARAM_READWRITE);
camera_properties[PROP_NUM_BUFFERS] =
g_param_spec_uint(uca_camera_props[PROP_NUM_BUFFERS],
"Number of frame buffers in the ring buffer ",
"Number of frame buffers in the ring buffer ",
0, G_MAXUINT, 4,
G_PARAM_READWRITE);
for (guint id = PROP_0 + 1; id < N_BASE_PROPERTIES; id++)
g_object_class_install_property(gobject_class, id, camera_properties[id]);
g_type_class_add_private(klass, sizeof(UcaCameraPrivate));
}
static void
uca_camera_init (UcaCamera *camera)
{
GValue val = {0};
camera->grab_func = NULL;
camera->priv = UCA_CAMERA_GET_PRIVATE(camera);
camera->priv->is_recording = FALSE;
camera->priv->is_readout = FALSE;
camera->priv->transfer_async = FALSE;
camera->priv->trigger = UCA_CAMERA_TRIGGER_AUTO;
camera->priv->h_binnings = g_value_array_new (1);
camera->priv->v_binnings = g_value_array_new (1);
camera->priv->buffered = FALSE;
camera->priv->num_buffers = 4;
camera->priv->ring_buffer = NULL;
g_value_init (&val, G_TYPE_UINT);
g_value_set_uint (&val, 1);
g_value_array_append (camera->priv->h_binnings, &val);
g_value_array_append (camera->priv->v_binnings, &val);
uca_camera_set_property_unit (camera_properties[PROP_SENSOR_WIDTH], UCA_UNIT_PIXEL);
uca_camera_set_property_unit (camera_properties[PROP_SENSOR_HEIGHT], UCA_UNIT_PIXEL);
uca_camera_set_property_unit (camera_properties[PROP_SENSOR_PIXEL_WIDTH], UCA_UNIT_METER);
uca_camera_set_property_unit (camera_properties[PROP_SENSOR_PIXEL_HEIGHT], UCA_UNIT_METER);
uca_camera_set_property_unit (camera_properties[PROP_SENSOR_BITDEPTH], UCA_UNIT_COUNT);
uca_camera_set_property_unit (camera_properties[PROP_SENSOR_HORIZONTAL_BINNING], UCA_UNIT_PIXEL);
uca_camera_set_property_unit (camera_properties[PROP_SENSOR_VERTICAL_BINNING], UCA_UNIT_PIXEL);
uca_camera_set_property_unit (camera_properties[PROP_SENSOR_MAX_FRAME_RATE], UCA_UNIT_COUNT);
uca_camera_set_property_unit (camera_properties[PROP_EXPOSURE_TIME], UCA_UNIT_SECOND);
uca_camera_set_property_unit (camera_properties[PROP_FRAMES_PER_SECOND], UCA_UNIT_COUNT);
uca_camera_set_property_unit (camera_properties[PROP_ROI_X], UCA_UNIT_PIXEL);
uca_camera_set_property_unit (camera_properties[PROP_ROI_Y], UCA_UNIT_PIXEL);
uca_camera_set_property_unit (camera_properties[PROP_ROI_WIDTH], UCA_UNIT_PIXEL);
uca_camera_set_property_unit (camera_properties[PROP_ROI_HEIGHT], UCA_UNIT_PIXEL);
uca_camera_set_property_unit (camera_properties[PROP_ROI_WIDTH_MULTIPLIER], UCA_UNIT_COUNT);
uca_camera_set_property_unit (camera_properties[PROP_ROI_HEIGHT_MULTIPLIER], UCA_UNIT_COUNT);
uca_camera_set_property_unit (camera_properties[PROP_RECORDED_FRAMES], UCA_UNIT_COUNT);
}
static gpointer
buffer_thread (UcaCamera *camera)
{
UcaCameraClass *klass;
GError *error = NULL;
klass = UCA_CAMERA_GET_CLASS (camera);
while (camera->priv->is_recording) {
gpointer buffer;
buffer = uca_ring_buffer_get_write_pointer (camera->priv->ring_buffer);
if (!(*klass->grab) (camera, buffer, &error))
break;
uca_ring_buffer_write_advance (camera->priv->ring_buffer);
}
return error;
}
/**
* uca_camera_start_recording:
* @camera: A #UcaCamera object
* @error: Location to store a #UcaCameraError error or %NULL
*
* Initiate recording of frames. If UcaCamera:grab-asynchronous is %TRUE and a
* #UcaCameraGrabFunc callback is set, frames are automatically transfered to
* the client program, otherwise you must use uca_camera_grab().
*/
void
uca_camera_start_recording (UcaCamera *camera, GError **error)
{
UcaCameraClass *klass;
UcaCameraPrivate *priv;
GError *tmp_error = NULL;
static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
g_return_if_fail (UCA_IS_CAMERA (camera));
klass = UCA_CAMERA_GET_CLASS (camera);
g_return_if_fail (klass != NULL);
g_return_if_fail (klass->start_recording != NULL);
priv = camera->priv;
g_static_mutex_lock (&mutex);
if (priv->is_recording) {
g_set_error (error, UCA_CAMERA_ERROR, UCA_CAMERA_ERROR_RECORDING,
"Camera is already recording");
goto start_recording_unlock;
}
if (priv->transfer_async && (camera->grab_func == NULL)) {
g_set_error (error, UCA_CAMERA_ERROR, UCA_CAMERA_ERROR_NO_GRAB_FUNC,
"No grab callback function set");
goto start_recording_unlock;
}
g_static_mutex_lock (&access_lock);
(*klass->start_recording)(camera, &tmp_error);
g_static_mutex_unlock (&access_lock);
if (tmp_error == NULL) {
priv->is_readout = FALSE;
priv->is_recording = TRUE;
/* TODO: we should depend on GLib 2.26 and use g_object_notify_by_pspec */
g_object_notify (G_OBJECT (camera), "is-recording");
}
else
g_propagate_error (error, tmp_error);
if (priv->buffered) {
guint width, height, bitdepth;
guint pixel_size;
g_object_get (camera,
"roi-width", &width,
"roi-height", &height,
"sensor-bitdepth", &bitdepth,
NULL);
pixel_size = bitdepth <= 8 ? 1 : 2;
priv->ring_buffer = uca_ring_buffer_new (width * height * pixel_size,
priv->num_buffers);
/* Let's read out the frames from another thread */
priv->read_thread = g_thread_new ("read-thread", (GThreadFunc) buffer_thread, camera);
}
start_recording_unlock:
g_static_mutex_unlock (&mutex);
}
/**
* uca_camera_stop_recording:
* @camera: A #UcaCamera object
* @error: Location to store a #UcaCameraError error or %NULL
*
* Stop recording.
*/
void
uca_camera_stop_recording (UcaCamera *camera, GError **error)
{
UcaCameraClass *klass;
UcaCameraPrivate *priv;
GError *tmp_error = NULL;
static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
g_return_if_fail (UCA_IS_CAMERA (camera));
klass = UCA_CAMERA_GET_CLASS (camera);
g_return_if_fail (klass != NULL);
g_return_if_fail (klass->stop_recording != NULL);
priv = camera->priv;
g_static_mutex_lock (&mutex);
if (!priv->is_recording) {
g_set_error (error, UCA_CAMERA_ERROR, UCA_CAMERA_ERROR_NOT_RECORDING,
"Camera is not recording");
goto error_stop_recording;
}
g_static_mutex_lock (&access_lock);
(*klass->stop_recording)(camera, &tmp_error);
g_static_mutex_unlock (&access_lock);
if (tmp_error == NULL) {
priv->is_readout = FALSE;
priv->is_recording = FALSE;
/* TODO: we should depend on GLib 2.26 and use g_object_notify_by_pspec */
g_object_notify (G_OBJECT (camera), "is-recording");
}
else
g_propagate_error (error, tmp_error);
if (priv->buffered) {
g_thread_join (priv->read_thread);
priv->read_thread = NULL;
}
if (camera->priv->ring_buffer != NULL) {
g_object_unref (camera->priv->ring_buffer);
camera->priv->ring_buffer = NULL;
}
error_stop_recording:
g_static_mutex_unlock (&mutex);
}
/**
* uca_camera_is_recording:
* @camera: A #UcaCamera object
*
* Convenience function to ask the current recording status
*
* Return value: %TRUE if recording is ongoing
* Since: 1.5
*/
gboolean
uca_camera_is_recording (UcaCamera *camera)
{
g_return_val_if_fail (UCA_IS_CAMERA (camera), FALSE);
return camera->priv->is_recording;
}
/**
* uca_camera_start_readout:
* @camera: A #UcaCamera object
* @error: Location to store a #UcaCameraError error or %NULL
*
* Initiate readout mode for cameras that support
* #UcaCamera:has-camram-recording after calling
* uca_camera_stop_recording(). Frames have to be picked up with
* ufo_camera_grab().
*/
void
uca_camera_start_readout (UcaCamera *camera, GError **error)
{
UcaCameraClass *klass;
static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
g_return_if_fail (UCA_IS_CAMERA(camera));
klass = UCA_CAMERA_GET_CLASS(camera);
g_return_if_fail (klass != NULL);
g_return_if_fail (klass->start_readout != NULL);
g_static_mutex_lock (&mutex);
if (camera->priv->is_recording) {
g_set_error (error, UCA_CAMERA_ERROR, UCA_CAMERA_ERROR_RECORDING,
"Camera is still recording");
}
else {
GError *tmp_error = NULL;
g_static_mutex_lock (&access_lock);
(*klass->start_readout) (camera, &tmp_error);
g_static_mutex_unlock (&access_lock);
if (tmp_error == NULL) {
camera->priv->is_readout = TRUE;
/* TODO: we should depend on GLib 2.26 and use g_object_notify_by_pspec */
g_object_notify (G_OBJECT (camera), "is-readout");
}
else
g_propagate_error (error, tmp_error);
}
g_static_mutex_unlock (&mutex);
}
/**
* uca_camera_stop_readout:
* @camera: A #UcaCamera object
* @error: Location to store a #UcaCameraError error or %NULL
*
* Stop reading out frames.
*
* Since: 1.1
*/
void
uca_camera_stop_readout (UcaCamera *camera, GError **error)
{
UcaCameraClass *klass;
static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
g_return_if_fail (UCA_IS_CAMERA(camera));
klass = UCA_CAMERA_GET_CLASS(camera);
g_return_if_fail (klass != NULL);
g_return_if_fail (klass->start_readout != NULL);
g_static_mutex_lock (&mutex);
if (camera->priv->is_recording) {
g_set_error (error, UCA_CAMERA_ERROR, UCA_CAMERA_ERROR_RECORDING,
"Camera is still recording");
}
else {
GError *tmp_error = NULL;
g_static_mutex_lock (&access_lock);
(*klass->stop_readout) (camera, &tmp_error);
g_static_mutex_unlock (&access_lock);
if (tmp_error == NULL) {
camera->priv->is_readout = FALSE;
g_object_notify (G_OBJECT (camera), "is-readout");
}
else
g_propagate_error (error, tmp_error);
}
g_static_mutex_unlock (&mutex);
}
/**
* uca_camera_set_grab_func:
* @camera: A #UcaCamera object
* @func: (scope call): A #UcaCameraGrabFunc callback function
* @user_data: (closure): Data that is passed on to #func
*
* Set the grab function that is called whenever a frame is readily transfered.
*/
void
uca_camera_set_grab_func(UcaCamera *camera, UcaCameraGrabFunc func, gpointer user_data)
{
camera->grab_func = func;
camera->user_data = user_data;
}
/**
* uca_camera_trigger:
* @camera: A #UcaCamera object
* @error: Location to store a #UcaCameraError error or %NULL
*
* Trigger from software if supported by camera.
*
* You must have called uca_camera_start_recording() before, otherwise you will
* get a #UCA_CAMERA_ERROR_NOT_RECORDING error.
*/
void
uca_camera_trigger (UcaCamera *camera, GError **error)
{
UcaCameraClass *klass;
static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
g_return_if_fail (UCA_IS_CAMERA (camera));
klass = UCA_CAMERA_GET_CLASS (camera);
g_return_if_fail (klass != NULL);
g_return_if_fail (klass->trigger != NULL);
g_static_mutex_lock (&mutex);
if (!camera->priv->is_recording)
g_set_error (error, UCA_CAMERA_ERROR, UCA_CAMERA_ERROR_NOT_RECORDING, "Camera is not recording");
else {
g_static_mutex_lock (&access_lock);
(*klass->trigger) (camera, error);
g_static_mutex_unlock (&access_lock);
}
g_static_mutex_unlock (&mutex);
}
/**
* uca_camera_grab:
* @camera: A #UcaCamera object
* @data: (type gulong): Pointer to suitably sized data buffer. Must not be
* %NULL.
* @error: Location to store a #UcaCameraError error or %NULL
*
* Grab a frame a single frame and store the result in @data.
*
* You must have called uca_camera_start_recording() before, otherwise you will
* get a #UCA_CAMERA_ERROR_NOT_RECORDING error.
*/
gboolean
uca_camera_grab (UcaCamera *camera, gpointer data, GError **error)
{
UcaCameraClass *klass;
gboolean result = FALSE;
/* FIXME: this prevents accessing two independent cameras simultanously. */
static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
g_return_val_if_fail (UCA_IS_CAMERA(camera), FALSE);
klass = UCA_CAMERA_GET_CLASS (camera);
g_return_val_if_fail (klass != NULL, FALSE);
g_return_val_if_fail (klass->grab != NULL, FALSE);
g_return_val_if_fail (data != NULL, FALSE);
if (!camera->priv->buffered) {
g_static_mutex_lock (&mutex);
if (!camera->priv->is_recording && !camera->priv->is_readout) {
g_set_error (error, UCA_CAMERA_ERROR, UCA_CAMERA_ERROR_NOT_RECORDING,
"Camera is neither recording nor in readout mode");
}
else {
g_static_mutex_lock (&access_lock);
result = (*klass->grab) (camera, data, error);
g_static_mutex_unlock (&access_lock);
}
g_static_mutex_unlock (&mutex);
}
else {
gpointer buffer;
/*
* Spin-lock until we can read something. This shouldn't happen to
* often, as buffering is usually used in those cases when the camera is
* faster than the software.
*/
while (!uca_ring_buffer_available (camera->priv->ring_buffer))
;
buffer = uca_ring_buffer_get_read_pointer (camera->priv->ring_buffer);
if (buffer == NULL) {
g_set_error (error, UCA_CAMERA_ERROR, UCA_CAMERA_ERROR_END_OF_STREAM,
"Ring buffer is empty");
}
else {
memcpy (data, buffer, uca_ring_buffer_get_block_size (camera->priv->ring_buffer));
result = TRUE;
}
}
return result;
}
/**
* uca_camera_register_unit:
* @camera: A #UcaCamera object
* @prop_name: Name of a property
* @unit: #UcaUnit
*
* Associates @prop_name of @camera with a specific @unit.
*
* Since: 1.1
*/
void
uca_camera_register_unit (UcaCamera *camera,
const gchar *prop_name,
UcaUnit unit)
{
UcaCameraClass *klass;
GObjectClass *oclass;
GParamSpec *pspec;
klass = UCA_CAMERA_GET_CLASS (camera);
oclass = G_OBJECT_CLASS (klass);
pspec = g_object_class_find_property (oclass, prop_name);
if (pspec == NULL) {
g_warning ("Camera does not have property `%s'", prop_name);
return;
}
uca_camera_set_property_unit (pspec, unit);
}
/**
* uca_camera_get_unit:
* @camera: A #UcaCamera object
* @prop_name: Name of a property
*
* Returns the unit associated with @prop_name of @camera.
*
* Returns: A #UcaUnit value associated with @prop_name. If there is none, the
* value will be #UCA_UNIT_NA.
* Since: 1.1
*/
UcaUnit
uca_camera_get_unit (UcaCamera *camera,
const gchar *prop_name)
{
UcaCameraClass *klass;
GObjectClass *oclass;
GParamSpec *pspec;
gpointer data;
klass = UCA_CAMERA_GET_CLASS (camera);
oclass = G_OBJECT_CLASS (klass);
pspec = g_object_class_find_property (oclass, prop_name);
if (pspec == NULL) {
g_warning ("Camera does not have property `%s'", prop_name);
return UCA_UNIT_NA;
}
data = g_param_spec_get_qdata (pspec, UCA_UNIT_QUARK);
return data == NULL ? UCA_UNIT_NA : GPOINTER_TO_INT (data);
}
|