Few improvements to move to good.
[platform/upstream/gst-plugins-good.git] / tests / icles / v4l2src-test.c
1
2 #include <gst/gst.h>
3 #include <gst/interfaces/tuner.h>
4 #include <gst/interfaces/colorbalance.h>
5
6 GstElement *pipeline, *source, *sink;
7 GMainLoop *loop;
8 volatile int exit_read = 0;
9
10 void
11 print_options ()
12 {
13   printf
14       ("\nLaunch \"./v4l2src-test.c devname\" to choose the device (default: /dev/video0)\n");
15   printf ("\nf - to change the fequency\n");
16   printf ("i - to change the input\n");
17   printf ("c - list color balance\n");
18   printf ("e - to exit\n");
19 }
20
21 void
22 run_options (char opt)
23 {
24   switch (opt) {
25     case 'f':
26     {
27       GstTuner *tuner = GST_TUNER (source);
28       GstTunerChannel *channel = gst_tuner_get_channel (tuner);
29       guint freq;
30
31       printf ("type the new frequency (current = %lu) (-1 to cancel): ",
32           gst_tuner_get_frequency (tuner, channel));
33       scanf ("%u", &freq);
34       if (freq != -1)
35         gst_tuner_set_frequency (tuner, channel, freq);
36     }
37       break;
38     case 'i':
39     {
40       GstTuner *tuner = GST_TUNER (source);
41       const GList *item, *list;
42       const GstTunerChannel *current_channel;
43       GstTunerChannel *channel = NULL;
44       gint index, next_channel;
45
46       list = gst_tuner_list_channels (tuner);
47       current_channel = gst_tuner_get_channel (tuner);
48
49       printf ("\nlist of inputs:\n");
50       for (item = list, index = 0; item != NULL; item = item->next, ++index) {
51         channel = item->data;
52         if (current_channel == channel) {
53           printf (" * %u - %s\n", index, channel->label);
54         } else {
55           printf ("   %u - %s\n", index, channel->label);
56         }
57       }
58       printf ("\ntype the number of input you want (-1 to cancel): ");
59       scanf ("%d", &next_channel);
60       if (next_channel < 0 || index <= next_channel) {
61         break;
62       }
63       for (item = list, index = 0; item != NULL && index <= next_channel;
64           item = item->next, ++index) {
65         channel = item->data;
66       }
67       if (channel)
68         gst_tuner_set_channel (tuner, channel);
69     }
70       break;
71     case 'e':
72       gst_element_set_state (pipeline, GST_STATE_NULL);
73       g_main_loop_quit (loop);
74       printf ("Bye\n");
75       g_thread_exit (0);
76       break;
77     case 'c':
78     {
79       GstColorBalance *balance = GST_COLOR_BALANCE (source);
80       const GList *controls = gst_color_balance_list_channels (balance);
81       GstColorBalanceChannel *channel;
82       const GList *item;
83       gint index, new_value;
84
85       if (controls) {
86         printf ("\nlist of controls:\n");
87         for (item = controls, index = 0; item != NULL;
88             item = item->next, ++index) {
89           channel = item->data;
90           printf ("   %u - %s (%d - %d) = %d\n", index, channel->label,
91               channel->min_value, channel->max_value,
92               gst_color_balance_get_value (balance, channel));
93         }
94         printf ("\ntype the number of color control you want (-1 to cancel): ");
95         scanf ("%d", &new_value);
96         if (new_value == -1)
97           break;
98         for (item = controls, index = 0; item != NULL && index <= new_value;
99             item = item->next, ++index) {
100           channel = item->data;
101         }
102         printf ("   %u - %s (%d - %d) = %d, type the new value: ", index,
103             channel->label, channel->min_value, channel->max_value,
104             gst_color_balance_get_value (balance, channel));
105         scanf ("%d", &new_value);
106         if (new_value == -1)
107           break;
108         gst_color_balance_set_value (balance, channel, new_value);
109       }
110     }
111       break;
112     default:
113       if (opt != 10)
114         printf ("error: invalid option %c", opt);
115       break;
116   }
117
118 }
119
120 gpointer
121 read_user (gpointer data)
122 {
123
124   char opt;
125
126   while (!exit_read) {
127
128     print_options ();
129
130     opt = getchar ();
131     if (exit_read) {
132       break;
133     }
134
135     run_options (opt);
136
137   }
138
139   return NULL;
140
141 }
142
143 static gboolean
144 my_bus_callback (GstBus * bus, GstMessage * message, gpointer data)
145 {
146
147   switch (GST_MESSAGE_TYPE (message)) {
148     case GST_MESSAGE_ERROR:{
149       GError *err;
150       gchar *debug;
151
152       gst_message_parse_error (message, &err, &debug);
153       g_print ("Error: %s - element %s\n", err->message,
154           gst_element_get_name (message->src));
155       g_error_free (err);
156       g_free (debug);
157
158       printf ("presse any key to exit\n");
159       exit_read = 1;
160       g_main_loop_quit (loop);
161       break;
162     case GST_MESSAGE_EOS:
163       /* end-of-stream */
164       printf ("presse any key to exit\n");
165       exit_read = 1;
166       g_main_loop_quit (loop);
167       break;
168     default:
169       break;
170     }
171   }
172   return TRUE;
173 }
174
175 int
176 main (int argc, char *argv[])
177 {
178
179   GThread *input_thread;
180
181   /* init */
182   gst_init (&argc, &argv);
183
184   /* create elements */
185   if (!(pipeline = gst_pipeline_new ("my_pipeline"))) {
186     fprintf (stderr, "error: gst_pipeline_new return NULL");
187     return -1;
188   }
189
190   if (!(source = gst_element_factory_make ("v4l2src", NULL))) {
191     fprintf (stderr,
192         "error: gst_element_factory_make (\"v4l2src\", NULL) return NULL");
193     return -1;
194   }
195
196   if (!(sink = gst_element_factory_make ("xvimagesink", NULL))) {
197     fprintf (stderr,
198         "error: gst_element_factory_make (\"xvimagesink\", NULL) return NULL");
199     return -1;
200   }
201
202   if (argv < 2) {
203     g_object_set (source, "device", "/dev/video0", NULL);
204   } else {
205     g_object_set (source, "device", argv[1], NULL);
206   }
207
208   /* you would normally check that the elements were created properly */
209   gst_bus_add_watch (gst_pipeline_get_bus (GST_PIPELINE (pipeline)),
210       my_bus_callback, NULL);
211
212   /* put together a pipeline */
213   gst_bin_add_many (GST_BIN (pipeline), source, sink, NULL);
214   gst_element_link_pads (source, "src", sink, "sink");
215
216   /* start the pipeline */
217   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
218   loop = g_main_loop_new (NULL, FALSE);
219
220   if (!(input_thread = g_thread_create (read_user, source, TRUE, NULL))) {
221     fprintf (stderr, "error: g_thread_create return NULL");
222     return -1;
223   }
224
225   if (argv < 2)
226     printf
227         ("\nOpening /dev/video0. Launch ./v4l2src-test.c devname to try another one\n");
228
229
230   g_main_loop_run (loop);
231   g_thread_join (input_thread);
232
233   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
234
235   gst_object_unref (pipeline);
236
237   gst_deinit ();
238
239   return 0;
240
241 }