3 * Copyright (C) 2006 Edgard Lima <edgard dot lima at indt dot org dot br>
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.
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.
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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
27 #include <gst/interfaces/tuner.h>
28 #include <gst/video/colorbalance.h>
29 #include <gst/video/videoorientation.h>
31 GstElement *pipeline, *source, *sink;
33 volatile int exit_read = 0;
38 printf ("\nf - to change the fequency\n");
39 printf ("i - to change the input\n");
40 printf ("n - to change the norm\n");
41 printf ("c - list color balance\n");
42 printf ("v - change video orientarion\n");
43 printf ("e - to exit\n");
47 run_options (char opt)
54 GstTuner *tuner = GST_TUNER (source);
55 GstTunerChannel *channel;
58 channel = gst_tuner_get_channel (tuner);
60 freq = gst_tuner_get_frequency (tuner, channel);
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);
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;
77 list = gst_tuner_list_norms (tuner);
79 current_norm = gst_tuner_get_norm (tuner);
81 printf ("\nlist of norms:\n");
82 for (item = list, index = 0; item != NULL; item = item->next, ++index) {
84 if (current_norm == norm) {
85 printf (" * %u - %s\n", index, norm->label);
87 printf (" %u - %s\n", index, norm->label);
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) {
95 if (index <= next_norm) {
96 printf ("Norm %d not available\n", next_norm);
99 for (item = list, index = 0; item != NULL && index <= next_norm;
100 item = item->next, ++index) {
104 gst_tuner_set_norm (tuner, norm);
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;
116 list = gst_tuner_list_channels (tuner);
118 current_channel = gst_tuner_get_channel (tuner);
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);
126 printf (" %u - %s\n", index, channel->label);
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) {
134 if (index <= next_channel) {
135 printf ("Input %d not available\n", next_channel);
138 for (item = list, index = 0; item != NULL && index <= next_channel;
139 item = item->next, ++index) {
140 channel = item->data;
143 gst_tuner_set_channel (tuner, channel);
147 gst_element_set_state (pipeline, GST_STATE_NULL);
148 g_main_loop_quit (loop);
154 GstColorBalance *balance = GST_COLOR_BALANCE (source);
155 const GList *controls;
156 GstColorBalanceChannel *channel;
158 gint index, new_value;
160 controls = gst_color_balance_list_channels (balance);
164 if (controls == NULL) {
165 printf ("There is no list of colorbalance controls\n");
170 printf ("list of controls:\n");
171 for (item = controls, index = 0; item != NULL;
172 item = item->next, ++index) {
173 channel = item->data;
174 printf (" %u - %s (%d - %d) = %d\n", index, channel->label,
175 channel->min_value, channel->max_value,
176 gst_color_balance_get_value (balance, channel));
178 printf ("\ntype the number of color control you want (-1 to cancel): ");
179 res = scanf ("%d", &new_value);
180 if (res != 1 || new_value == -1)
182 for (item = controls, index = 0; item != NULL && index <= new_value;
183 item = item->next, ++index) {
184 channel = item->data;
186 printf (" %u - %s (%d - %d) = %d, type the new value: ", index - 1,
187 channel->label, channel->min_value, channel->max_value,
188 gst_color_balance_get_value (balance, channel));
189 res = scanf ("%d", &new_value);
190 if (res != 1 || new_value == -1)
192 gst_color_balance_set_value (balance, channel, new_value);
197 GstVideoOrientation *vidorient = GST_VIDEO_ORIENTATION (source);
198 gboolean flip = FALSE;
203 if (gst_video_orientation_get_hflip (vidorient, &flip)) {
206 printf ("Horizontal flip is %s\n", flip ? "on" : "off");
207 printf ("\ntype 1 to toggle (-1 to cancel): ");
208 res = scanf ("%d", &new_value);
209 if (res != 1 || new_value == 1) {
211 if (gst_video_orientation_set_hflip (vidorient, flip)) {
212 gst_video_orientation_get_hflip (vidorient, &flip);
213 printf ("Now horizontal flip is %s\n", flip ? "on" : "off");
215 printf ("Error toggling horizontal flip\n");
220 printf ("Horizontal flip control not available\n");
223 if (gst_video_orientation_get_vflip (vidorient, &flip)) {
226 printf ("\nVertical flip is %s\n", flip ? "on" : "off");
227 printf ("\ntype 1 to toggle (-1 to cancel): ");
228 res = scanf ("%d", &new_value);
229 if (res != 1 || new_value == 1) {
231 if (gst_video_orientation_set_vflip (vidorient, flip)) {
232 gst_video_orientation_get_vflip (vidorient, &flip);
233 printf ("Now vertical flip is %s\n", flip ? "on" : "off");
235 printf ("Error toggling vertical flip\n");
240 printf ("Vertical flip control not available\n");
243 if (gst_video_orientation_get_hcenter (vidorient, ¢er)) {
244 printf ("Horizontal center is %d\n", center);
245 printf ("\ntype the new horizontal center value (-1 to cancel): ");
246 res = scanf ("%d", ¢er);
247 if (res != 1 || center != -1) {
248 if (gst_video_orientation_set_hcenter (vidorient, center)) {
249 gst_video_orientation_get_hcenter (vidorient, ¢er);
250 printf ("Now horizontal center is %d\n", center);
252 printf ("Error setting horizontal center\n");
257 printf ("Horizontal center control not available\n");
260 if (gst_video_orientation_get_vcenter (vidorient, ¢er)) {
261 printf ("Vertical center is %d\n", center);
262 printf ("\ntype the new vertical center value (-1 to cancel): ");
263 res = scanf ("%d", ¢er);
264 if (res != 1 || center != -1) {
265 if (gst_video_orientation_set_vcenter (vidorient, center)) {
266 gst_video_orientation_get_vcenter (vidorient, ¢er);
267 printf ("Now vertical center is %d\n", center);
269 printf ("Error setting vertical center\n");
274 printf ("Vertical center control not available\n");
282 printf ("error: invalid option %c", opt);
293 read_user (gpointer data)
307 } while (opt == '\n');
318 my_bus_callback (GstBus * bus, GstMessage * message, gpointer data)
321 switch (GST_MESSAGE_TYPE (message)) {
322 case GST_MESSAGE_ERROR:{
327 gst_message_parse_error (message, &err, &debug);
328 str = gst_element_get_name (message->src);
329 g_print ("%s error: %s\n", str, err->message);
331 g_print ("Debug: %s\n", debug);
336 printf ("presse <ENTER> key to exit\n");
338 g_main_loop_quit (loop);
340 case GST_MESSAGE_EOS:
342 printf ("presse any key to exit\n");
344 g_main_loop_quit (loop);
354 main (int argc, char *argv[])
357 GThread *input_thread;
358 gint numbuffers = -1;
359 gchar device[128] = { '\0' };
360 gchar input[128] = { '\0' };
361 gulong frequency = 0;
364 /* see for input option */
369 static char long_options_desc[][64] = {
370 {"Number of buffers to output before sending EOS"},
371 {"Device location. Common in /dev/video0"},
372 {"input/output (channel) to switch to"},
373 {"frequency to tune to (in Hz)"},
376 static struct option long_options[] = {
377 {"numbuffers", 1, 0, 'n'},
378 {"device", 1, 0, 'd'},
379 {"input", 1, 0, 'i'},
380 {"frequency", 1, 0, 'f'},
383 /* getopt_long stores the option index here. */
384 int option_index = 0;
386 c = getopt_long (argc, argv, "n:d:i:f:h", long_options, &option_index);
388 /* Detect the end of the options. */
390 printf ("tip: use -h to see help message.\n");
396 /* If this option set a flag, do nothing else now. */
397 if (long_options[option_index].flag != 0)
399 printf ("option %s", long_options[option_index].name);
401 printf (" with arg %s", optarg);
406 numbuffers = atoi (optarg);
410 strncpy (device, optarg, sizeof (device) / sizeof (device[0]));
414 strncpy (input, optarg, sizeof (input) / sizeof (input[0]));
418 frequency = atol (optarg);
422 printf ("Usage: v4l2src-test [OPTION]...\n");
423 for (c = 0; long_options[c].name; ++c) {
424 printf ("-%c, --%s\r\t\t\t\t%s\n", long_options[c].val,
425 long_options[c].name, long_options_desc[c]);
431 /* getopt_long already printed an error message. */
432 printf ("Use -h to see help message.\n");
440 /* Print any remaining command line arguments (not options). */
442 printf ("Use -h to see help message.\n" "non-option ARGV-elements: ");
443 while (optind < argc)
444 printf ("%s ", argv[optind++]);
449 gst_init (&argc, &argv);
451 /* create elements */
452 if (!(pipeline = gst_pipeline_new ("my_pipeline"))) {
453 fprintf (stderr, "error: gst_pipeline_new return NULL");
457 if (!(source = gst_element_factory_make ("v4l2src", NULL))) {
459 "error: gst_element_factory_make (\"v4l2src\", NULL) return NULL");
463 if (!(sink = gst_element_factory_make ("xvimagesink", NULL))) {
465 "error: gst_element_factory_make (\"xvimagesink\", NULL) return NULL");
469 if (numbuffers > -1) {
470 g_object_set (source, "num-buffers", numbuffers, NULL);
473 g_object_set (source, "device", device, NULL);
476 g_object_set (source, "input", input, NULL);
479 g_object_set (source, "frequency", frequency, NULL);
482 /* you would normally check that the elements were created properly */
483 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
484 gst_bus_add_watch (bus, my_bus_callback, NULL);
486 /* put together a pipeline */
487 gst_bin_add_many (GST_BIN (pipeline), source, sink, NULL);
488 gst_element_link_pads (source, "src", sink, "sink");
490 /* start the pipeline */
491 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
492 loop = g_main_loop_new (NULL, FALSE);
494 if (!(input_thread = g_thread_create (read_user, source, TRUE, NULL))) {
495 fprintf (stderr, "error: g_thread_create return NULL");
499 g_main_loop_run (loop);
500 g_thread_join (input_thread);
502 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
504 gst_object_unref (bus);
505 gst_object_unref (pipeline);