6 #include <gst/interfaces/tuner.h>
7 #include <gst/interfaces/colorbalance.h>
8 #include <gst/interfaces/videoorientation.h>
10 GstElement *pipeline, *source, *sink;
12 volatile int exit_read = 0;
17 printf ("\nf - to change the fequency\n");
18 printf ("i - to change the input\n");
19 printf ("n - to change the norm\n");
20 printf ("c - list color balance\n");
21 printf ("v - change video orientarion\n");
22 printf ("e - to exit\n");
26 run_options (char opt)
31 GstTuner *tuner = GST_TUNER (source);
32 GstTunerChannel *channel;
35 channel = gst_tuner_get_channel (tuner);
37 freq = gst_tuner_get_frequency (tuner, channel);
39 printf ("\ntype the new frequency (current = %lu) (-1 to cancel): ",
43 gst_tuner_set_frequency (tuner, channel, freq);
48 GstTuner *tuner = GST_TUNER (source);
49 const GList *item, *list;
50 const GstTunerNorm *current_norm;
51 GstTunerNorm *norm = NULL;
52 gint index, next_norm;
55 list = gst_tuner_list_norms (tuner);
57 current_norm = gst_tuner_get_norm (tuner);
59 printf ("\nlist of norms:\n");
60 for (item = list, index = 0; item != NULL; item = item->next, ++index) {
62 if (current_norm == norm) {
63 printf (" * %u - %s\n", index, norm->label);
65 printf (" %u - %s\n", index, norm->label);
68 printf ("\ntype the number of norm you want (-1 to cancel): ");
69 scanf ("%d", &next_norm);
73 if (index <= next_norm) {
74 printf ("Norm %d not available\n", next_norm);
77 for (item = list, index = 0; item != NULL && index <= next_norm;
78 item = item->next, ++index) {
82 gst_tuner_set_norm (tuner, norm);
87 GstTuner *tuner = GST_TUNER (source);
88 const GList *item, *list;
89 const GstTunerChannel *current_channel;
90 GstTunerChannel *channel = NULL;
91 gint index, next_channel;
94 list = gst_tuner_list_channels (tuner);
96 current_channel = gst_tuner_get_channel (tuner);
98 printf ("\nlist of inputs:\n");
99 for (item = list, index = 0; item != NULL; item = item->next, ++index) {
100 channel = item->data;
101 if (current_channel == channel) {
102 printf (" * %u - %s\n", index, channel->label);
104 printf (" %u - %s\n", index, channel->label);
107 printf ("\ntype the number of input you want (-1 to cancel): ");
108 scanf ("%d", &next_channel);
109 if (next_channel < 0) {
112 if (index <= next_channel) {
113 printf ("Input %d not available\n", next_channel);
116 for (item = list, index = 0; item != NULL && index <= next_channel;
117 item = item->next, ++index) {
118 channel = item->data;
121 gst_tuner_set_channel (tuner, channel);
125 gst_element_set_state (pipeline, GST_STATE_NULL);
126 g_main_loop_quit (loop);
132 GstColorBalance *balance = GST_COLOR_BALANCE (source);
133 const GList *controls;
134 GstColorBalanceChannel *channel;
136 gint index, new_value;
138 controls = gst_color_balance_list_channels (balance);
142 if (controls == NULL) {
143 printf ("There is no list of colorbalance controls\n");
148 printf ("list of controls:\n");
149 for (item = controls, index = 0; item != NULL;
150 item = item->next, ++index) {
151 channel = item->data;
152 printf (" %u - %s (%d - %d) = %d\n", index, channel->label,
153 channel->min_value, channel->max_value,
154 gst_color_balance_get_value (balance, channel));
156 printf ("\ntype the number of color control you want (-1 to cancel): ");
157 scanf ("%d", &new_value);
160 for (item = controls, index = 0; item != NULL && index <= new_value;
161 item = item->next, ++index) {
162 channel = item->data;
164 printf (" %u - %s (%d - %d) = %d, type the new value: ", index - 1,
165 channel->label, channel->min_value, channel->max_value,
166 gst_color_balance_get_value (balance, channel));
167 scanf ("%d", &new_value);
170 gst_color_balance_set_value (balance, channel, new_value);
175 GstVideoOrientation *vidorient = GST_VIDEO_ORIENTATION (source);
176 gboolean flip = FALSE;
181 if (gst_video_orientation_get_hflip (vidorient, &flip)) {
184 printf ("Horizontal flip is %s\n", flip ? "on" : "off");
185 printf ("\ntype 1 to toggle (-1 to cancel): ");
186 scanf ("%d", &new_value);
187 if (new_value == 1) {
189 if (gst_video_orientation_set_hflip (vidorient, flip)) {
190 gst_video_orientation_get_hflip (vidorient, &flip);
191 printf ("Now horizontal flip is %s\n", flip ? "on" : "off");
193 printf ("Error toggling horizontal flip\n");
198 printf ("Horizontal flip control not available\n");
201 if (gst_video_orientation_get_vflip (vidorient, &flip)) {
204 printf ("\nVertical flip is %s\n", flip ? "on" : "off");
205 printf ("\ntype 1 to toggle (-1 to cancel): ");
206 scanf ("%d", &new_value);
207 if (new_value == 1) {
209 if (gst_video_orientation_set_vflip (vidorient, flip)) {
210 gst_video_orientation_get_vflip (vidorient, &flip);
211 printf ("Now vertical flip is %s\n", flip ? "on" : "off");
213 printf ("Error toggling vertical flip\n");
218 printf ("Vertical flip control not available\n");
221 if (gst_video_orientation_get_hcenter (vidorient, ¢er)) {
222 printf ("Horizontal center is %d\n", center);
223 printf ("\ntype the new horizontal center value (-1 to cancel): ");
224 scanf ("%d", ¢er);
226 if (gst_video_orientation_set_hcenter (vidorient, center)) {
227 gst_video_orientation_get_hcenter (vidorient, ¢er);
228 printf ("Now horizontal center is %d\n", center);
230 printf ("Error setting horizontal center\n");
235 printf ("Horizontal center control not available\n");
238 if (gst_video_orientation_get_vcenter (vidorient, ¢er)) {
239 printf ("Vertical center is %d\n", center);
240 printf ("\ntype the new vertical center value (-1 to cancel): ");
241 scanf ("%d", ¢er);
243 if (gst_video_orientation_set_vcenter (vidorient, center)) {
244 gst_video_orientation_get_vcenter (vidorient, ¢er);
245 printf ("Now vertical center is %d\n", center);
247 printf ("Error setting vertical center\n");
252 printf ("Vertical center control not available\n");
260 printf ("error: invalid option %c", opt);
271 read_user (gpointer data)
285 } while (opt == '\n');
296 my_bus_callback (GstBus * bus, GstMessage * message, gpointer data)
299 switch (GST_MESSAGE_TYPE (message)) {
300 case GST_MESSAGE_ERROR:{
304 gst_message_parse_error (message, &err, &debug);
305 g_print ("%s error: %s\n",
306 gst_element_get_name (message->src), err->message);
307 g_print ("Debug: %s\n", debug);
312 printf ("presse <ENTER> key to exit\n");
314 g_main_loop_quit (loop);
316 case GST_MESSAGE_EOS:
318 printf ("presse any key to exit\n");
320 g_main_loop_quit (loop);
330 main (int argc, char *argv[])
333 GThread *input_thread;
334 gint numbuffers = -1;
335 gchar device[128] = { '\0' };
336 gchar input[128] = { '\0' };
337 gulong frequency = 0;
338 gboolean nofixedfps = TRUE;
341 /* see for input option */
346 static char long_options_desc[][64] = {
347 {"Number of buffers to output before sending EOS"},
348 {"Device location. Common in /dev/video0"},
349 {"input/output (channel) to switch to"},
350 {"frequency to tune to (in Hz)"},
351 {"set use-fixed-fps to FALSE"},
354 static struct option long_options[] = {
355 {"numbuffers", 1, 0, 'n'},
356 {"device", 1, 0, 'd'},
357 {"input", 1, 0, 'i'},
358 {"frequency", 1, 0, 'f'},
359 {"nofixedfps", 0, 0, 's'},
362 /* getopt_long stores the option index here. */
363 int option_index = 0;
365 c = getopt_long (argc, argv, "n:d:i:f:sh:", long_options, &option_index);
367 /* Detect the end of the options. */
369 printf ("tip: use -h to see help message.\n");
375 /* If this option set a flag, do nothing else now. */
376 if (long_options[option_index].flag != 0)
378 printf ("option %s", long_options[option_index].name);
380 printf (" with arg %s", optarg);
385 numbuffers = atoi (optarg);
389 strncpy (device, optarg, sizeof (device) / sizeof (device[0]));
393 strncpy (input, optarg, sizeof (input) / sizeof (input[0]));
397 frequency = atol (optarg);
405 printf ("Usage: v4l2src-test [OPTION]...\n");
406 for (c = 0; long_options[c].name; ++c) {
407 printf ("-%c, --%s\r\t\t\t\t%s\n", long_options[c].val,
408 long_options[c].name, long_options_desc[c]);
414 /* getopt_long already printed an error message. */
415 printf ("Use -h to see help message.\n");
424 /* Print any remaining command line arguments (not options). */
426 printf ("Use -h to see help message.\n" "non-option ARGV-elements: ");
427 while (optind < argc)
428 printf ("%s ", argv[optind++]);
435 gst_init (&argc, &argv);
437 /* create elements */
438 if (!(pipeline = gst_pipeline_new ("my_pipeline"))) {
439 fprintf (stderr, "error: gst_pipeline_new return NULL");
443 if (!(source = gst_element_factory_make ("v4l2src", NULL))) {
445 "error: gst_element_factory_make (\"v4l2src\", NULL) return NULL");
449 if (!(sink = gst_element_factory_make ("xvimagesink", NULL))) {
451 "error: gst_element_factory_make (\"xvimagesink\", NULL) return NULL");
455 if (numbuffers > -1) {
456 g_object_set (source, "num-buffers", numbuffers, NULL);
459 g_object_set (source, "device", device, NULL);
462 g_object_set (source, "input", input, NULL);
465 g_object_set (source, "frequency", frequency, NULL);
468 g_object_set (source, "use-fixed-fps", nofixedfps, NULL);
471 /* you would normally check that the elements were created properly */
472 gst_bus_add_watch (gst_pipeline_get_bus (GST_PIPELINE (pipeline)),
473 my_bus_callback, NULL);
475 /* put together a pipeline */
476 gst_bin_add_many (GST_BIN (pipeline), source, sink, NULL);
477 gst_element_link_pads (source, "src", sink, "sink");
479 /* start the pipeline */
480 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
481 loop = g_main_loop_new (NULL, FALSE);
483 if (!(input_thread = g_thread_create (read_user, source, TRUE, NULL))) {
484 fprintf (stderr, "error: g_thread_create return NULL");
488 g_main_loop_run (loop);
489 g_thread_join (input_thread);
491 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
493 gst_object_unref (pipeline);