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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
27 #include <gst/video/colorbalance.h>
28 #include <gst/video/videoorientation.h>
30 GstElement *pipeline, *source, *sink;
32 volatile int exit_read = 0;
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");
46 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);
148 gst_element_set_state (pipeline, GST_STATE_NULL);
149 g_main_loop_quit (loop);
155 GstColorBalance *balance = GST_COLOR_BALANCE (source);
156 const GList *controls;
157 GstColorBalanceChannel *channel;
159 gint index, new_value;
161 controls = gst_color_balance_list_channels (balance);
165 if (controls == NULL) {
166 printf ("There is no list of colorbalance controls\n");
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));
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)
183 for (item = controls, index = 0; item != NULL && index <= new_value;
184 item = item->next, ++index) {
185 channel = item->data;
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)
193 gst_color_balance_set_value (balance, channel, new_value);
198 GstVideoOrientation *vidorient = GST_VIDEO_ORIENTATION (source);
199 gboolean flip = FALSE;
204 if (gst_video_orientation_get_hflip (vidorient, &flip)) {
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) {
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");
216 printf ("Error toggling horizontal flip\n");
221 printf ("Horizontal flip control not available\n");
224 if (gst_video_orientation_get_vflip (vidorient, &flip)) {
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) {
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");
236 printf ("Error toggling vertical flip\n");
241 printf ("Vertical flip control not available\n");
244 if (gst_video_orientation_get_hcenter (vidorient, ¢er)) {
245 printf ("Horizontal center is %d\n", center);
246 printf ("\ntype the new horizontal center value (-1 to cancel): ");
247 res = scanf ("%d", ¢er);
248 if (res != 1 || center != -1) {
249 if (gst_video_orientation_set_hcenter (vidorient, center)) {
250 gst_video_orientation_get_hcenter (vidorient, ¢er);
251 printf ("Now horizontal center is %d\n", center);
253 printf ("Error setting horizontal center\n");
258 printf ("Horizontal center control not available\n");
261 if (gst_video_orientation_get_vcenter (vidorient, ¢er)) {
262 printf ("Vertical center is %d\n", center);
263 printf ("\ntype the new vertical center value (-1 to cancel): ");
264 res = scanf ("%d", ¢er);
265 if (res != 1 || center != -1) {
266 if (gst_video_orientation_set_vcenter (vidorient, center)) {
267 gst_video_orientation_get_vcenter (vidorient, ¢er);
268 printf ("Now vertical center is %d\n", center);
270 printf ("Error setting vertical center\n");
275 printf ("Vertical center control not available\n");
283 printf ("error: invalid option %c", opt);
294 read_user (gpointer data)
308 } while (opt == '\n');
319 my_bus_callback (GstBus * bus, GstMessage * message, gpointer data)
322 switch (GST_MESSAGE_TYPE (message)) {
323 case GST_MESSAGE_ERROR:{
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);
332 g_print ("Debug: %s\n", debug);
337 printf ("presse <ENTER> key to exit\n");
339 g_main_loop_quit (loop);
341 case GST_MESSAGE_EOS:
343 printf ("presse any key to exit\n");
345 g_main_loop_quit (loop);
355 main (int argc, char *argv[])
358 GThread *input_thread;
359 gint numbuffers = -1;
360 gchar device[128] = { '\0' };
361 gchar input[128] = { '\0' };
362 gulong frequency = 0;
365 /* see for input option */
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)"},
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'},
384 /* getopt_long stores the option index here. */
385 int option_index = 0;
387 c = getopt_long (argc, argv, "n:d:i:f:h", long_options, &option_index);
389 /* Detect the end of the options. */
391 printf ("tip: use -h to see help message.\n");
397 /* If this option set a flag, do nothing else now. */
398 if (long_options[option_index].flag != 0)
400 printf ("option %s", long_options[option_index].name);
402 printf (" with arg %s", optarg);
407 numbuffers = atoi (optarg);
411 strncpy (device, optarg, sizeof (device) / sizeof (device[0]));
415 strncpy (input, optarg, sizeof (input) / sizeof (input[0]));
419 frequency = atol (optarg);
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]);
432 /* getopt_long already printed an error message. */
433 printf ("Use -h to see help message.\n");
441 /* Print any remaining command line arguments (not options). */
443 printf ("Use -h to see help message.\n" "non-option ARGV-elements: ");
444 while (optind < argc)
445 printf ("%s ", argv[optind++]);
450 gst_init (&argc, &argv);
452 /* create elements */
453 if (!(pipeline = gst_pipeline_new ("my_pipeline"))) {
454 fprintf (stderr, "error: gst_pipeline_new return NULL");
458 if (!(source = gst_element_factory_make ("v4l2src", NULL))) {
460 "error: gst_element_factory_make (\"v4l2src\", NULL) return NULL");
464 if (!(sink = gst_element_factory_make ("xvimagesink", NULL))) {
466 "error: gst_element_factory_make (\"xvimagesink\", NULL) return NULL");
470 if (numbuffers > -1) {
471 g_object_set (source, "num-buffers", numbuffers, NULL);
474 g_object_set (source, "device", device, NULL);
477 g_object_set (source, "input", input, NULL);
480 g_object_set (source, "frequency", frequency, NULL);
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);
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");
491 /* start the pipeline */
492 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
493 loop = g_main_loop_new (NULL, FALSE);
495 input_thread = g_thread_try_new ("v4l2src-test", read_user, source, NULL);
497 if (input_thread == NULL) {
498 fprintf (stderr, "error: g_thread_try_new() failed");
502 g_main_loop_run (loop);
503 g_thread_join (input_thread);
505 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
507 gst_object_unref (bus);
508 gst_object_unref (pipeline);