v4l2: Fix unknown type name ‘off_t’ error
[platform/upstream/gst-plugins-good.git] / tests / icles / v4l2src-test.c
1 /* GStreamer
2  *
3  * Copyright (C) 2006 Edgard Lima <edgard dot lima at indt dot org dot br>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include <string.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <getopt.h>
25
26 #include <gst/gst.h>
27 #include <gst/video/colorbalance.h>
28 #include <gst/video/videoorientation.h>
29
30 GstElement *pipeline, *source, *sink;
31 GMainLoop *loop;
32 volatile int exit_read = 0;
33
34 static void
35 print_options (void)
36 {
37   printf ("\nf - to change the fequency\n");
38   printf ("i - to change the input\n");
39   printf ("n - to change the norm\n");
40   printf ("c - list color balance\n");
41   printf ("v - change video orientarion\n");
42   printf ("e - to exit\n");
43 }
44
45 static void
46 run_options (char opt)
47 {
48   int res;
49
50   switch (opt) {
51 #if 0
52     case 'f':
53     {
54       GstTuner *tuner = GST_TUNER (source);
55       GstTunerChannel *channel;
56       guint freq;
57
58       channel = gst_tuner_get_channel (tuner);
59
60       freq = gst_tuner_get_frequency (tuner, channel);
61
62       printf ("\ntype the new frequency (current = %u) (-1 to cancel): ", freq);
63       res = scanf ("%u", &freq);
64       if (res != 1 || freq != -1)
65         gst_tuner_set_frequency (tuner, channel, freq);
66     }
67       break;
68     case 'n':
69     {
70       GstTuner *tuner = GST_TUNER (source);
71       const GList *item, *list;
72       const GstTunerNorm *current_norm;
73       GstTunerNorm *norm = NULL;
74       gint index, next_norm;
75
76
77       list = gst_tuner_list_norms (tuner);
78
79       current_norm = gst_tuner_get_norm (tuner);
80
81       printf ("\nlist of norms:\n");
82       for (item = list, index = 0; item != NULL; item = item->next, ++index) {
83         norm = item->data;
84         if (current_norm == norm) {
85           printf (" * %u - %s\n", index, norm->label);
86         } else {
87           printf ("   %u - %s\n", index, norm->label);
88         }
89       }
90       printf ("\ntype the number of norm you want (-1 to cancel): ");
91       res = scanf ("%d", &next_norm);
92       if (res != 1 || next_norm < 0) {
93         break;
94       }
95       if (index <= next_norm) {
96         printf ("Norm %d not available\n", next_norm);
97         break;
98       }
99       for (item = list, index = 0; item != NULL && index <= next_norm;
100           item = item->next, ++index) {
101         norm = item->data;
102       }
103       if (norm)
104         gst_tuner_set_norm (tuner, norm);
105     }
106       break;
107     case 'i':
108     {
109       GstTuner *tuner = GST_TUNER (source);
110       const GList *item, *list;
111       const GstTunerChannel *current_channel;
112       GstTunerChannel *channel = NULL;
113       gint index, next_channel;
114
115
116       list = gst_tuner_list_channels (tuner);
117
118       current_channel = gst_tuner_get_channel (tuner);
119
120       printf ("\nlist of inputs:\n");
121       for (item = list, index = 0; item != NULL; item = item->next, ++index) {
122         channel = item->data;
123         if (current_channel == channel) {
124           printf (" * %u - %s\n", index, channel->label);
125         } else {
126           printf ("   %u - %s\n", index, channel->label);
127         }
128       }
129       printf ("\ntype the number of input you want (-1 to cancel): ");
130       res = scanf ("%d", &next_channel);
131       if (res != 1 || next_channel < 0) {
132         break;
133       }
134       if (index <= next_channel) {
135         printf ("Input %d not available\n", next_channel);
136         break;
137       }
138       for (item = list, index = 0; item != NULL && index <= next_channel;
139           item = item->next, ++index) {
140         channel = item->data;
141       }
142       if (channel)
143         gst_tuner_set_channel (tuner, channel);
144     }
145       break;
146 #endif
147     case 'e':
148       gst_element_set_state (pipeline, GST_STATE_NULL);
149       g_main_loop_quit (loop);
150       printf ("Bye\n");
151       g_thread_exit (0);
152       break;
153     case 'c':
154     {
155       GstColorBalance *balance = GST_COLOR_BALANCE (source);
156       const GList *controls;
157       GstColorBalanceChannel *channel;
158       const GList *item;
159       gint index, new_value;
160
161       controls = gst_color_balance_list_channels (balance);
162
163       printf ("\n");
164
165       if (controls == NULL) {
166         printf ("There is no list of colorbalance controls\n");
167         goto done;
168       }
169
170       if (controls) {
171         printf ("list of controls:\n");
172         for (item = controls, index = 0; item != NULL;
173             item = item->next, ++index) {
174           channel = item->data;
175           printf ("   %u - %s (%d - %d) = %d\n", index, channel->label,
176               channel->min_value, channel->max_value,
177               gst_color_balance_get_value (balance, channel));
178         }
179         printf ("\ntype the number of color control you want (-1 to cancel): ");
180         res = scanf ("%d", &new_value);
181         if (res != 1 || new_value == -1)
182           break;
183         for (item = controls, index = 0; item != NULL && index <= new_value;
184             item = item->next, ++index) {
185           channel = item->data;
186         }
187         printf ("   %u - %s (%d - %d) = %d, type the new value: ", index - 1,
188             channel->label, channel->min_value, channel->max_value,
189             gst_color_balance_get_value (balance, channel));
190         res = scanf ("%d", &new_value);
191         if (res != 1 || new_value == -1)
192           break;
193         gst_color_balance_set_value (balance, channel, new_value);
194       }
195     }
196     case 'v':
197     {
198       GstVideoOrientation *vidorient = GST_VIDEO_ORIENTATION (source);
199       gboolean flip = FALSE;
200       gint center = 0;
201
202       printf ("\n");
203
204       if (gst_video_orientation_get_hflip (vidorient, &flip)) {
205         gint new_value;
206
207         printf ("Horizontal flip is %s\n", flip ? "on" : "off");
208         printf ("\ntype 1 to toggle (-1 to cancel): ");
209         res = scanf ("%d", &new_value);
210         if (res != 1 || new_value == 1) {
211           flip = !flip;
212           if (gst_video_orientation_set_hflip (vidorient, flip)) {
213             gst_video_orientation_get_hflip (vidorient, &flip);
214             printf ("Now horizontal flip is %s\n", flip ? "on" : "off");
215           } else {
216             printf ("Error toggling horizontal flip\n");
217           }
218         } else {
219         }
220       } else {
221         printf ("Horizontal flip control not available\n");
222       }
223
224       if (gst_video_orientation_get_vflip (vidorient, &flip)) {
225         gint new_value;
226
227         printf ("\nVertical flip is %s\n", flip ? "on" : "off");
228         printf ("\ntype 1 to toggle (-1 to cancel): ");
229         res = scanf ("%d", &new_value);
230         if (res != 1 || new_value == 1) {
231           flip = !flip;
232           if (gst_video_orientation_set_vflip (vidorient, flip)) {
233             gst_video_orientation_get_vflip (vidorient, &flip);
234             printf ("Now vertical flip is %s\n", flip ? "on" : "off");
235           } else {
236             printf ("Error toggling vertical flip\n");
237           }
238         } else {
239         }
240       } else {
241         printf ("Vertical flip control not available\n");
242       }
243
244       if (gst_video_orientation_get_hcenter (vidorient, &center)) {
245         printf ("Horizontal center is %d\n", center);
246         printf ("\ntype the new horizontal center value (-1 to cancel): ");
247         res = scanf ("%d", &center);
248         if (res != 1 || center != -1) {
249           if (gst_video_orientation_set_hcenter (vidorient, center)) {
250             gst_video_orientation_get_hcenter (vidorient, &center);
251             printf ("Now horizontal center is %d\n", center);
252           } else {
253             printf ("Error setting horizontal center\n");
254           }
255         } else {
256         }
257       } else {
258         printf ("Horizontal center control not available\n");
259       }
260
261       if (gst_video_orientation_get_vcenter (vidorient, &center)) {
262         printf ("Vertical center is %d\n", center);
263         printf ("\ntype the new vertical center value (-1 to cancel): ");
264         res = scanf ("%d", &center);
265         if (res != 1 || center != -1) {
266           if (gst_video_orientation_set_vcenter (vidorient, center)) {
267             gst_video_orientation_get_vcenter (vidorient, &center);
268             printf ("Now vertical center is %d\n", center);
269           } else {
270             printf ("Error setting vertical center\n");
271           }
272         } else {
273         }
274       } else {
275         printf ("Vertical center control not available\n");
276       }
277
278     }
279       break;
280       break;
281     default:
282       if (opt != 10)
283         printf ("error: invalid option %c", opt);
284       break;
285   }
286
287 done:
288
289   return;
290
291 }
292
293 static gpointer
294 read_user (gpointer data)
295 {
296
297   char opt;
298
299   while (!exit_read) {
300
301     print_options ();
302
303     do {
304       opt = getchar ();
305       if (exit_read) {
306         break;
307       }
308     } while (opt == '\n');
309
310     run_options (opt);
311
312   }
313
314   return NULL;
315
316 }
317
318 static gboolean
319 my_bus_callback (GstBus * bus, GstMessage * message, gpointer data)
320 {
321
322   switch (GST_MESSAGE_TYPE (message)) {
323     case GST_MESSAGE_ERROR:{
324       GError *err;
325       gchar *debug;
326       gchar *str;
327
328       gst_message_parse_error (message, &err, &debug);
329       str = gst_element_get_name (message->src);
330       g_print ("%s error: %s\n", str, err->message);
331       g_free (str);
332       g_print ("Debug: %s\n", debug);
333
334       g_error_free (err);
335       g_free (debug);
336
337       printf ("presse <ENTER> key to exit\n");
338       exit_read = 1;
339       g_main_loop_quit (loop);
340       break;
341     case GST_MESSAGE_EOS:
342       /* end-of-stream */
343       printf ("presse any key to exit\n");
344       exit_read = 1;
345       g_main_loop_quit (loop);
346       break;
347     default:
348       break;
349     }
350   }
351   return TRUE;
352 }
353
354 int
355 main (int argc, char *argv[])
356 {
357
358   GThread *input_thread;
359   gint numbuffers = -1;
360   gchar device[128] = { '\0' };
361   gchar input[128] = { '\0' };
362   gulong frequency = 0;
363   GstBus *bus;
364
365   /* see for input option */
366
367   int c;
368
369   while (1) {
370     static char long_options_desc[][64] = {
371       {"Number of buffers to output before sending EOS"},
372       {"Device location. Common in /dev/video0"},
373       {"input/output (channel) to switch to"},
374       {"frequency to tune to (in Hz)"},
375       {0, 0, 0, 0}
376     };
377     static struct option long_options[] = {
378       {"numbuffers", 1, 0, 'n'},
379       {"device", 1, 0, 'd'},
380       {"input", 1, 0, 'i'},
381       {"frequency", 1, 0, 'f'},
382       {0, 0, 0, 0}
383     };
384     /* getopt_long stores the option index here. */
385     int option_index = 0;
386
387     c = getopt_long (argc, argv, "n:d:i:f:h", long_options, &option_index);
388
389     /* Detect the end of the options. */
390     if (c == -1) {
391       printf ("tip: use -h to see help message.\n");
392       break;
393     }
394
395     switch (c) {
396       case 0:
397         /* If this option set a flag, do nothing else now. */
398         if (long_options[option_index].flag != 0)
399           break;
400         printf ("option %s", long_options[option_index].name);
401         if (optarg)
402           printf (" with arg %s", optarg);
403         printf ("\n");
404         break;
405
406       case 'n':
407         numbuffers = atoi (optarg);
408         break;
409
410       case 'd':
411         strncpy (device, optarg, sizeof (device) / sizeof (device[0]));
412         break;
413
414       case 'i':
415         strncpy (input, optarg, sizeof (input) / sizeof (input[0]));
416         break;
417
418       case 'f':
419         frequency = atol (optarg);
420         break;
421
422       case 'h':
423         printf ("Usage: v4l2src-test [OPTION]...\n");
424         for (c = 0; long_options[c].name; ++c) {
425           printf ("-%c, --%s\r\t\t\t\t%s\n", long_options[c].val,
426               long_options[c].name, long_options_desc[c]);
427         }
428         exit (0);
429         break;
430
431       case '?':
432         /* getopt_long already printed an error message. */
433         printf ("Use -h to see help message.\n");
434         break;
435
436       default:
437         abort ();
438     }
439   }
440
441   /* Print any remaining command line arguments (not options). */
442   if (optind < argc) {
443     printf ("Use -h to see help message.\n" "non-option ARGV-elements: ");
444     while (optind < argc)
445       printf ("%s ", argv[optind++]);
446     putchar ('\n');
447   }
448
449   /* init */
450   gst_init (&argc, &argv);
451
452   /* create elements */
453   if (!(pipeline = gst_pipeline_new ("my_pipeline"))) {
454     fprintf (stderr, "error: gst_pipeline_new return NULL");
455     return -1;
456   }
457
458   if (!(source = gst_element_factory_make ("v4l2src", NULL))) {
459     fprintf (stderr,
460         "error: gst_element_factory_make (\"v4l2src\", NULL) return NULL");
461     return -1;
462   }
463
464   if (!(sink = gst_element_factory_make ("xvimagesink", NULL))) {
465     fprintf (stderr,
466         "error: gst_element_factory_make (\"xvimagesink\", NULL) return NULL");
467     return -1;
468   }
469
470   if (numbuffers > -1) {
471     g_object_set (source, "num-buffers", numbuffers, NULL);
472   }
473   if (device[0]) {
474     g_object_set (source, "device", device, NULL);
475   }
476   if (input[0]) {
477     g_object_set (source, "input", input, NULL);
478   }
479   if (frequency) {
480     g_object_set (source, "frequency", frequency, NULL);
481   }
482
483   /* you would normally check that the elements were created properly */
484   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
485   gst_bus_add_watch (bus, my_bus_callback, NULL);
486
487   /* put together a pipeline */
488   gst_bin_add_many (GST_BIN (pipeline), source, sink, NULL);
489   gst_element_link_pads (source, "src", sink, "sink");
490
491   /* start the pipeline */
492   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
493   loop = g_main_loop_new (NULL, FALSE);
494
495   input_thread = g_thread_try_new ("v4l2src-test", read_user, source, NULL);
496
497   if (input_thread == NULL) {
498     fprintf (stderr, "error: g_thread_try_new() failed");
499     return -1;
500   }
501
502   g_main_loop_run (loop);
503   g_thread_join (input_thread);
504
505   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
506
507   gst_object_unref (bus);
508   gst_object_unref (pipeline);
509
510   gst_deinit ();
511
512   return 0;
513
514 }