Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 462 lines (424 sloc) 17.538 kb
c729c0f Importing existing code
Diego Dompe authored
1 /**
2 * ipiped/src/ipiped.vala
3 *
4 * Ipipe class - framework for controlling and configuring ipipe using D-Bus messages
5 *
6 * Copyright(c) 2010, RidgeRun
7 * All rights reserved.
8 */
9
10 using Posix,V4l2,ImpPrev,dm365aew;
11 #if (RRAEW)
12 using rraew;
13 #endif
14 [DBus(name = "com.ridgerun.ipiped.IpipeInterface", signals = "Error")]
15 public class Ipipe:GLib.Object {
16
17 /* Private data */
18 /** Flag to enable/disable debug information */
19 private static bool debug = false;
20 /** Flag to indicate if the previewer is initialized*/
21 private static bool initialized = false;
22 private string sensor = "";
23 private string video_processor = "";
24 public AbstcVideoProcessor video_processor_abstract;
25 public AbstcSensor sensor_abstract;
26 public signal void Error(string err_message);
27 #if (RRAEW)
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
28 /** Struct that contains auto white balance configuration*/
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
29 RraewAwbConfiguration awb_config;
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
30 /** Flag that indicates if auto white balance is configured or not*/
31 bool awb_configured;
32 /** Struct that contains auto exposure configuration*/
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
33 RraewAeConfiguration ae_config;
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
34 /** Flag that indicates if auto exposure's configuration status*/
35 bool ae_configured;
36 /** Struct that contains general aew configuration*/
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
37 RraewConfiguration aew_config;
c729c0f Importing existing code
Diego Dompe authored
38 /** Thread for aew*/
39 unowned Thread<void*> thread;
40 /** Flag to indicate if the aew thread is running*/
41 bool aew_running = false;
42 /** Indicates if we can close the execution thread wihtout failing **/
43 bool clean_close = false;
44 /** Pointer to aew structure*/
45 void *rraew = null;
46 /** Time between aew iterations*/
47 int wait_time=0;
48 #endif
49 /**
50 * Create a new instance of a Ipipe daemon
51 */
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
52 public Ipipe(string _sensor, string _video_processor,
53 AbstcVideoProcessor _video_processor_abstract,
c729c0f Importing existing code
Diego Dompe authored
54 AbstcSensor _sensor_abstract){
55 /* Initialize private variables */
56 initialized = false;
57 sensor =_sensor;
58 video_processor=_video_processor;
59 video_processor_abstract=_video_processor_abstract;
60 sensor_abstract=_sensor_abstract;
61 this.debug = false;
37a9ff1 @mmontero Fix initialization of ipiped, variables don't exist if librraew is not u...
mmontero authored
62 #if (RRAEW)
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
63 this.ae_configured = false;
64 this.awb_configured = false;
37a9ff1 @mmontero Fix initialization of ipiped, variables don't exist if librraew is not u...
mmontero authored
65 #endif
c729c0f Importing existing code
Diego Dompe authored
66 }
67 /**
68 * Destroy a instance of Ipipe
69 */
70 ~Ipipe(){
71 /* Destroy the ipipe instance */
72 }
73
74 /**
75 * Debug
76 * Enable/Disable the debug information in all the functions
77 * @param enable if true print debug information
78 */
79 public void enable_debug(bool enable) throws IOError{
80 this.debug = enable;
81 video_processor_abstract.debug = true;
82 sensor_abstract.debug = true;
83 }
84 /**
85 * Ping
86 * Show to the ipipe-client that an instance of the ipiped daemon is
87 * alive
88 */
89 public bool ping() throws IOError{
90 return true;
91 }
92
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
93 /**
94 * get_sensor
95 * Return the sensor that is being used
96 */
c729c0f Importing existing code
Diego Dompe authored
97 public string get_sensor() throws IOError{
98 return sensor;
99 }
100
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
101 /**
102 * get_video_processor
103 * Return the video processor that is being used*/
c729c0f Importing existing code
Diego Dompe authored
104 public string get_video_processor() throws IOError{
105 return video_processor;
106 }
107
108 #if (RRAEW)
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
109 /* aew_thread_func
110 * This function consists on a loop that iterates
111 * the execution of the rraew adjustments.
112 * */
c729c0f Importing existing code
Diego Dompe authored
113 private void* aew_thread_func(){
114 try {
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
115 /* When the flag aew_running is false
116 * stop aew*/
c729c0f Importing existing code
Diego Dompe authored
117 while (aew_running) {
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
118 /*Wait the configured time between aew iterations*/
c729c0f Importing existing code
Diego Dompe authored
119 Thread.usleep(wait_time);
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
120 if (rraew_run(rraew)<0){
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
121 /*When an error occurs close aew*/
c729c0f Importing existing code
Diego Dompe authored
122 clean_close = false;
123 close_aew();
124 }
125 }
126 }catch(IOError e) {
127 Posix.stderr.printf("Fail to execute:%s\n", e.message);
128 }
129 return null;
130 }
131
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
132 /**
133 * set_auto_exposure_configuration
134 * Initialize the ae struct for the auto exposure configuration of
135 * librraew.
136 * @param ae string that chooses the auto exposure algorithm, can be
137 * "electronic-centric" or "none" (not apply an auto exposure).
138 * @param meter string that chooses the metering system for the auto
139 * exposure algorithm, can be "partial", "center-weigthed", "segmented"
140 * or "average"
141 * @param rect_percentage defines the percentage of the image width
142 * and height to be used as the center size. Default: 40
143 * @param xrect x coordinate for the center point of the rectangle
144 * of interest.
145 * @param yrect y coordinate for the center point of the rectangle
146 * of interest.
147 */
148 public int set_auto_exposure_configuration(string ae, string meter,
149 int rect_percentage, int xrect, int yrect) throws IOError{
150
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
151 this.ae_config = RraewAeConfiguration();
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
152
153 /* Define the ae algorithm*/
154 if (ae == "electronic-centric")
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
155 ae_config.algorithm = RraewExposureAlgo.EC;
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
156 else if(ae == "none"){
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
157 ae_config.algorithm = RraewExposureAlgo.NONE;
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
158 this.ae_configured = true;
159 return 0;
160 } else {
161 Posix.stderr.printf("\nIpiped:Invalid auto exposure algorithm\n");
162 return -1;
163 }
164
165 /* Define the metering method*/
166 if (meter == "partial")
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
167 ae_config.meter_type = RraewMeteringType.PARTIAL_AREA;
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
168 else if (meter == "weighted")
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
169 ae_config.meter_type = RraewMeteringType.RECT_WEIGHTED;
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
170 else if (meter == "average")
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
171 ae_config.meter_type = RraewMeteringType.AVERAGE;
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
172 else if (meter == "segmented")
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
173 ae_config.meter_type = RraewMeteringType.SEGMENT;
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
174 else{
175 Posix.stderr.printf("\nIpiped:Invalid metering type\n");
176 return -1;
177 }
178 /* Only the metering systems partial, rectangular weighted and
179 * average require the definition of the rectangle of interest*/
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
180 if ((ae_config.meter_type == RraewMeteringType.PARTIAL_AREA) ||
181 (ae_config.meter_type == RraewMeteringType.RECT_WEIGHTED) ||
182 (ae_config.meter_type == RraewMeteringType.AVERAGE)){
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
183
184 /*Sets the center point*/
185 ae_config.rect_center_point.x = xrect;
186 ae_config.rect_center_point.y = yrect;
187
188 /* The point (-1, -1) es used as identifier for the
189 * center of the image. So the auto exposure is
190 * configured to center the rectangle*/
191 if ((xrect == -1) && (yrect == -1)){
192 ae_config.rect_center_point.centered = 1;
193 ae_config.rect_center_point.x = 0;
194 ae_config.rect_center_point.y = 0;
195 }
196 /*Check percentage limits*/
197 if ((rect_percentage > 100)|| (rect_percentage) < 1) {
198 Posix.stderr.printf("\nIpiped: rect_pecentage must be" +
199 " between 1 and 100\n");
200 return -1;
201 }
202 ae_config.rect_percentage = rect_percentage;
203 }
204 this.ae_configured = true;
205 return 0;
206 }
207
208 /**
209 * set_auto_white_balance_configuration
210 * Initialize the awb struct for the auto white balance configuration of
211 * librraew.
212 * @param wb string that chooses the auto white balance algorithm,
213 * can be "gray-world", "white-patch", "white-patch2" or "none" (not
214 * apply an auto white balance).
215 * @param g string that chooses the gain type, can be "sensor" for sensor
216 * gain or "digital" for digital gain on the SOC.
217 */
218 public int set_auto_white_balance_configuration(string wb, string g)
219 throws IOError{
220
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
221 this.awb_config = RraewAwbConfiguration();
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
222
223 /* Define the awb algorithm*/
224 if (wb == "white-patch")
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
225 awb_config.algorithm = RraewWhiteBalanceAlgo.WHITE_PATCH;
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
226 else if (wb == "white-patch2")
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
227 awb_config.algorithm = RraewWhiteBalanceAlgo.WHITE_PATCH_2;
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
228 else if (wb == "gray-world")
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
229 awb_config.algorithm = RraewWhiteBalanceAlgo.GRAY_WORLD;
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
230 else if (wb == "none") {
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
231 awb_config.algorithm = RraewWhiteBalanceAlgo.NONE;
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
232 this.awb_configured = true;
233 return 0;
234 } else{
235 Posix.stderr.printf("\nIpiped:Invalid white-balance algorithm\n");
236 return -1;
237 }
238
239 /* Define the gain module to be used*/
240 if (g == "sensor")
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
241 awb_config.gain_type = RraewGainType.SENSOR;
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
242 else if (g == "digital")
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
243 awb_config.gain_type = RraewGainType.DIGITAL;
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
244 else if (g == "default"){
245 /*Digital is the default gain*/
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
246 awb_config.gain_type = RraewGainType.DIGITAL;
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
247 } else {
248 Posix.stderr.printf("\nIpiped:Invalid gain type\n");
249 return -1;
250 }
251 this.awb_configured = true;
252 return 0;
253 }
254
255 /**
256 * get_auto_exposure_configuration
257 * Return current auto exposure's configuration
258 */
259 public int get_auto_exposure_configuration(out string ae, out string meter,
260 out int rect_percentage, out int xrect, out int yrect) throws IOError{
261
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
262 if (ae_config.algorithm == RraewExposureAlgo.EC)
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
263 ae = "electronic-centric";
264 else
265 ae = "none";
266
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
267 if ( ae_config.meter_type == RraewMeteringType.PARTIAL_AREA )
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
268 meter = "partial";
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
269 else if (ae_config.meter_type == RraewMeteringType.RECT_WEIGHTED)
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
270 meter = "weighted";
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
271 else if (ae_config.meter_type == RraewMeteringType.AVERAGE)
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
272 meter = "average";
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
273 else if (ae_config.meter_type == RraewMeteringType.SEGMENT)
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
274 meter = "segmented";
275
276 rect_percentage = ae_config.rect_percentage;
277
278 if (ae_config.rect_center_point.centered == 1){
279 xrect = -1;
280 yrect = -1;
281 } else {
282 xrect = (int)ae_config.rect_center_point.x;
283 yrect = (int)ae_config.rect_center_point.y;
284 }
285
286 if (!ae_configured)
287 return -1;
288 else
289 return 0;
290 }
291 /**
292 * get_auto_white_balance_configuration
293 * Return the current configuration of the awb
294 */
295 public int get_auto_white_balance_configuration(out string wb,
296 out string g) throws IOError{
297 if (!awb_configured){
298 wb = "";
299 g = "";
300 return -1;
301 }
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
302 if (awb_config.algorithm == RraewWhiteBalanceAlgo.WHITE_PATCH)
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
303 wb = "white-patch";
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
304 else if (awb_config.algorithm == RraewWhiteBalanceAlgo.WHITE_PATCH_2)
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
305 wb = "white-patch2";
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
306 else if (awb_config.algorithm == RraewWhiteBalanceAlgo.GRAY_WORLD)
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
307 wb = "gray-world";
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
308 else if (awb_config.algorithm == RraewWhiteBalanceAlgo.NONE )
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
309 wb = "none";
310
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
311 if (awb_config.gain_type == RraewGainType.SENSOR)
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
312 g = "sensor";
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
313 else if (awb_config.gain_type == RraewGainType.DIGITAL)
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
314 g = "digital";
315
316 return 0;
317 }
318
319 /**
320 * get_aew_status
321 * Return the current configuration of the rraew library
322 */
323 public bool get_aew_status(out bool awb_config, out bool ae_config,
324 out int time, out int width, out int height,
325 out int segment_factor) throws IOError{
326
327 awb_config = awb_configured;
328 ae_config = ae_configured;
329 time = wait_time;
330 width = aew_config.width;
331 height = aew_config.height;
332 segment_factor = aew_config.segmentation_factor;
333 return aew_running;
334 }
335
336 /**
337 * get_ae_rectangle_coordinates
338 * Get the rectangle of interest's coordinates, only if aew is running
339 * and return it.
340 */
341 public int get_ae_rectangle_coordinates(out uint right, out uint left,
342 out uint top, out uint bottom) throws IOError{
c729c0f Importing existing code
Diego Dompe authored
343
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
344 if (aew_running) {
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
345 if (rraew_get_rectangle_coordinates(rraew, &right, &left,
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
346 &top, &bottom) == 0){
347 return 0;
348 }
349 }
350 right = 0;
351 left = 0;
352 top = 0;
353 bottom = 0;
354 return -1;
355 }
c729c0f Importing existing code
Diego Dompe authored
356 /**
357 * AEW Algorithm initialization
358 * Uses the parameters given by the user to configure the aew library,
359 * sets the functions to access the sensor and the dm365 interface
360 * (vpfe ipipe). Creates an aew structure and a thread for the aew loop
361 * @param time it is the time between aew algorithms iterations
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
362 * @param width image width
363 * @param height image height
c729c0f Importing existing code
Diego Dompe authored
364 * @param segment_factor a percentage of the maximun image divisions
365 */
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
366 public int init_aew(int time, int width, int height,
367 int segment_factor) throws IOError
c729c0f Importing existing code
Diego Dompe authored
368 {
369 /** File descriptors information*/
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
370 RraewFileDescriptors fd = RraewFileDescriptors();
c729c0f Importing existing code
Diego Dompe authored
371 wait_time = time;
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
372 RraewSensor sensor = RraewSensor();
373 RraewInterface interf = RraewInterface();
374 aew_config = RraewConfiguration();
c729c0f Importing existing code
Diego Dompe authored
375
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
376 /* If an instance of librraew is running close it
377 * before create a new one */
c729c0f Importing existing code
Diego Dompe authored
378 if(aew_running) {
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
379 close_aew();
380 /*Wait for aew engine disable*/
381 usleep(300000);
c729c0f Importing existing code
Diego Dompe authored
382 }
383
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
384 /* Defining general rraew params */
385 aew_config.height = height;
386 aew_config.width = width;
387 aew_config.segmentation_factor = segment_factor;
388
389 /* Check params limits */
c729c0f Importing existing code
Diego Dompe authored
390 if (time > 1000000) {
391 Posix.stderr.printf("\nIpiped:Wait time is greater than the maximum\n");
392 return -1;
393 }
394
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
395 if ((segment_factor > 100)|| (segment_factor) < 1) {
c729c0f Importing existing code
Diego Dompe authored
396 Posix.stderr.printf("\nIpiped:Segmentation factor must be between 1 and 100\n");
397 return -1;
398 }
399
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
400 /* Get sensor information */
c729c0f Importing existing code
Diego Dompe authored
401 sensor_abstract.get_sensor_data(&sensor);
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
402 /* Get video processor information */
c729c0f Importing existing code
Diego Dompe authored
403 if (!video_processor_abstract.get_video_processor_data(&interf)) {
404 Posix.stderr.printf ("Can't get video processor data\n");
405 return -1;
406 }
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
407 /* Defining ipiped file descriptors */
c729c0f Importing existing code
Diego Dompe authored
408 fd.previewer_fd = video_processor_abstract.previewer_fd;
409 fd.owner_previewer_fd = video_processor_abstract.owner_previewer_fd;
410 fd.aew_fd = video_processor_abstract.aew_fd;
411 fd.owner_aew_fd = video_processor_abstract.owner_aew_fd;
412 fd.capture_fd = sensor_abstract.capture_fd;
413 fd.owner_capture_fd = sensor_abstract.owner_capture_fd;
414
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
415 /* Create rraew handler */
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
416 rraew = rraew_create(&awb_config, &ae_config, &aew_config,
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
417 &sensor, &interf, &fd);
418 if (rraew == null){
419 Posix.stderr.printf ("Can't create rraew handler\n");
420 return -1;
421 }
422 /* Enable aew flag */
c729c0f Importing existing code
Diego Dompe authored
423 aew_running = true;
424 clean_close = true;
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
425
426 /* Create a new thread for the aew loop*/
c729c0f Importing existing code
Diego Dompe authored
427 if (!Thread.supported ()) {
428 error ("Cannot run without thread support");
429 }
430 try {
431 thread = Thread.create<void*>(aew_thread_func, true);
432 } catch (ThreadError e) {
433 return -1;
434 }
435 return 0;
436 }
437
438 /**
439 * End AEW Algorithm, disable AEW engine
440 */
441 public void close_aew() throws IOError{
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
442 /*Clean the flag that indicates aew
443 * is running*/
c729c0f Importing existing code
Diego Dompe authored
444 aew_running = false;
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
445 /*Check if the aew wasn't close by
446 *librraew error*/
c729c0f Importing existing code
Diego Dompe authored
447 if(clean_close) {
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
448 /*join the thread*/
c729c0f Importing existing code
Diego Dompe authored
449 thread.join();
450 clean_close = false;
451 }
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored
452 /*If the rraew handler was initialized
453 *destroy it*/
c729c0f Importing existing code
Diego Dompe authored
454 if (rraew != null){
6dbe68e @mmontero Match typo to librraew vapi
mmontero authored
455 rraew_destroy(rraew);
c729c0f Importing existing code
Diego Dompe authored
456 rraew = null;
457 }
458 }
459 #endif
460 }
461
Something went wrong with that request. Please try again.