v4l2src-test.c 13.9 KB
Newer Older
Wim Taymans's avatar
Wim Taymans committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* GStreamer
 *
 * Copyright (C) 2006 Edgard Lima <edgard dot lima at indt dot org dot br>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
17 18
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
Wim Taymans's avatar
Wim Taymans committed
19 20 21
 */

#include <string.h>
22 23 24
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
25 26

#include <gst/gst.h>
Wim Taymans's avatar
Wim Taymans committed
27 28
#include <gst/video/colorbalance.h>
#include <gst/video/videoorientation.h>
29 30 31 32 33

GstElement *pipeline, *source, *sink;
GMainLoop *loop;
volatile int exit_read = 0;

34
static void
35
print_options (void)
36 37 38
{
  printf ("\nf - to change the fequency\n");
  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 43 44
  printf ("e - to exit\n");
}

45
static void
46 47
run_options (char opt)
{
Wim Taymans's avatar
Wim Taymans committed
48 49
  int res;

50
  switch (opt) {
51
#if 0
52 53 54
    case 'f':
    {
      GstTuner *tuner = GST_TUNER (source);
55
      GstTunerChannel *channel;
56 57
      guint freq;

58 59 60 61
      channel = gst_tuner_get_channel (tuner);

      freq = gst_tuner_get_frequency (tuner, channel);

62
      printf ("\ntype the new frequency (current = %u) (-1 to cancel): ", freq);
Wim Taymans's avatar
Wim Taymans committed
63 64
      res = scanf ("%u", &freq);
      if (res != 1 || freq != -1)
65 66 67
        gst_tuner_set_frequency (tuner, channel, freq);
    }
      break;
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    case 'n':
    {
      GstTuner *tuner = GST_TUNER (source);
      const GList *item, *list;
      const GstTunerNorm *current_norm;
      GstTunerNorm *norm = NULL;
      gint index, next_norm;


      list = gst_tuner_list_norms (tuner);

      current_norm = gst_tuner_get_norm (tuner);

      printf ("\nlist of norms:\n");
      for (item = list, index = 0; item != NULL; item = item->next, ++index) {
        norm = item->data;
        if (current_norm == norm) {
          printf (" * %u - %s\n", index, norm->label);
        } else {
          printf ("   %u - %s\n", index, norm->label);
        }
      }
      printf ("\ntype the number of norm you want (-1 to cancel): ");
Wim Taymans's avatar
Wim Taymans committed
91 92
      res = scanf ("%d", &next_norm);
      if (res != 1 || next_norm < 0) {
93 94 95 96 97 98 99 100 101 102 103 104 105 106
        break;
      }
      if (index <= next_norm) {
        printf ("Norm %d not available\n", next_norm);
        break;
      }
      for (item = list, index = 0; item != NULL && index <= next_norm;
          item = item->next, ++index) {
        norm = item->data;
      }
      if (norm)
        gst_tuner_set_norm (tuner, norm);
    }
      break;
107 108 109 110 111 112 113 114
    case 'i':
    {
      GstTuner *tuner = GST_TUNER (source);
      const GList *item, *list;
      const GstTunerChannel *current_channel;
      GstTunerChannel *channel = NULL;
      gint index, next_channel;

115

116
      list = gst_tuner_list_channels (tuner);
117

118 119 120 121 122 123 124 125 126 127 128 129
      current_channel = gst_tuner_get_channel (tuner);

      printf ("\nlist of inputs:\n");
      for (item = list, index = 0; item != NULL; item = item->next, ++index) {
        channel = item->data;
        if (current_channel == channel) {
          printf (" * %u - %s\n", index, channel->label);
        } else {
          printf ("   %u - %s\n", index, channel->label);
        }
      }
      printf ("\ntype the number of input you want (-1 to cancel): ");
Wim Taymans's avatar
Wim Taymans committed
130 131
      res = scanf ("%d", &next_channel);
      if (res != 1 || next_channel < 0) {
132 133 134 135
        break;
      }
      if (index <= next_channel) {
        printf ("Input %d not available\n", next_channel);
136 137 138 139 140 141 142 143 144 145
        break;
      }
      for (item = list, index = 0; item != NULL && index <= next_channel;
          item = item->next, ++index) {
        channel = item->data;
      }
      if (channel)
        gst_tuner_set_channel (tuner, channel);
    }
      break;
146
#endif
147 148 149 150 151 152 153 154 155
    case 'e':
      gst_element_set_state (pipeline, GST_STATE_NULL);
      g_main_loop_quit (loop);
      printf ("Bye\n");
      g_thread_exit (0);
      break;
    case 'c':
    {
      GstColorBalance *balance = GST_COLOR_BALANCE (source);
156
      const GList *controls;
157 158 159 160
      GstColorBalanceChannel *channel;
      const GList *item;
      gint index, new_value;

161 162
      controls = gst_color_balance_list_channels (balance);

163 164
      printf ("\n");

165 166 167 168 169
      if (controls == NULL) {
        printf ("There is no list of colorbalance controls\n");
        goto done;
      }

170
      if (controls) {
171
        printf ("list of controls:\n");
172 173 174 175 176 177 178 179
        for (item = controls, index = 0; item != NULL;
            item = item->next, ++index) {
          channel = item->data;
          printf ("   %u - %s (%d - %d) = %d\n", index, channel->label,
              channel->min_value, channel->max_value,
              gst_color_balance_get_value (balance, channel));
        }
        printf ("\ntype the number of color control you want (-1 to cancel): ");
Wim Taymans's avatar
Wim Taymans committed
180 181
        res = scanf ("%d", &new_value);
        if (res != 1 || new_value == -1)
182 183 184 185 186
          break;
        for (item = controls, index = 0; item != NULL && index <= new_value;
            item = item->next, ++index) {
          channel = item->data;
        }
187
        printf ("   %u - %s (%d - %d) = %d, type the new value: ", index - 1,
188 189
            channel->label, channel->min_value, channel->max_value,
            gst_color_balance_get_value (balance, channel));
Wim Taymans's avatar
Wim Taymans committed
190 191
        res = scanf ("%d", &new_value);
        if (res != 1 || new_value == -1)
192 193 194 195
          break;
        gst_color_balance_set_value (balance, channel, new_value);
      }
    }
196 197 198 199 200 201 202 203 204 205 206 207 208
    case 'v':
    {
      GstVideoOrientation *vidorient = GST_VIDEO_ORIENTATION (source);
      gboolean flip = FALSE;
      gint center = 0;

      printf ("\n");

      if (gst_video_orientation_get_hflip (vidorient, &flip)) {
        gint new_value;

        printf ("Horizontal flip is %s\n", flip ? "on" : "off");
        printf ("\ntype 1 to toggle (-1 to cancel): ");
Wim Taymans's avatar
Wim Taymans committed
209 210
        res = scanf ("%d", &new_value);
        if (res != 1 || new_value == 1) {
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
          flip = !flip;
          if (gst_video_orientation_set_hflip (vidorient, flip)) {
            gst_video_orientation_get_hflip (vidorient, &flip);
            printf ("Now horizontal flip is %s\n", flip ? "on" : "off");
          } else {
            printf ("Error toggling horizontal flip\n");
          }
        } else {
        }
      } else {
        printf ("Horizontal flip control not available\n");
      }

      if (gst_video_orientation_get_vflip (vidorient, &flip)) {
        gint new_value;

        printf ("\nVertical flip is %s\n", flip ? "on" : "off");
        printf ("\ntype 1 to toggle (-1 to cancel): ");
Wim Taymans's avatar
Wim Taymans committed
229 230
        res = scanf ("%d", &new_value);
        if (res != 1 || new_value == 1) {
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
          flip = !flip;
          if (gst_video_orientation_set_vflip (vidorient, flip)) {
            gst_video_orientation_get_vflip (vidorient, &flip);
            printf ("Now vertical flip is %s\n", flip ? "on" : "off");
          } else {
            printf ("Error toggling vertical flip\n");
          }
        } else {
        }
      } else {
        printf ("Vertical flip control not available\n");
      }

      if (gst_video_orientation_get_hcenter (vidorient, &center)) {
        printf ("Horizontal center is %d\n", center);
        printf ("\ntype the new horizontal center value (-1 to cancel): ");
Wim Taymans's avatar
Wim Taymans committed
247 248
        res = scanf ("%d", &center);
        if (res != 1 || center != -1) {
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
          if (gst_video_orientation_set_hcenter (vidorient, center)) {
            gst_video_orientation_get_hcenter (vidorient, &center);
            printf ("Now horizontal center is %d\n", center);
          } else {
            printf ("Error setting horizontal center\n");
          }
        } else {
        }
      } else {
        printf ("Horizontal center control not available\n");
      }

      if (gst_video_orientation_get_vcenter (vidorient, &center)) {
        printf ("Vertical center is %d\n", center);
        printf ("\ntype the new vertical center value (-1 to cancel): ");
Wim Taymans's avatar
Wim Taymans committed
264 265
        res = scanf ("%d", &center);
        if (res != 1 || center != -1) {
266 267 268 269 270 271 272 273 274 275 276 277 278 279
          if (gst_video_orientation_set_vcenter (vidorient, center)) {
            gst_video_orientation_get_vcenter (vidorient, &center);
            printf ("Now vertical center is %d\n", center);
          } else {
            printf ("Error setting vertical center\n");
          }
        } else {
        }
      } else {
        printf ("Vertical center control not available\n");
      }

    }
      break;
280 281 282 283 284 285 286
      break;
    default:
      if (opt != 10)
        printf ("error: invalid option %c", opt);
      break;
  }

287 288 289 290
done:

  return;

291 292
}

293
static gpointer
294 295 296 297 298 299 300 301 302
read_user (gpointer data)
{

  char opt;

  while (!exit_read) {

    print_options ();

303 304 305 306 307 308
    do {
      opt = getchar ();
      if (exit_read) {
        break;
      }
    } while (opt == '\n');
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325

    run_options (opt);

  }

  return NULL;

}

static gboolean
my_bus_callback (GstBus * bus, GstMessage * message, gpointer data)
{

  switch (GST_MESSAGE_TYPE (message)) {
    case GST_MESSAGE_ERROR:{
      GError *err;
      gchar *debug;
Wim Taymans's avatar
Wim Taymans committed
326
      gchar *str;
327 328

      gst_message_parse_error (message, &err, &debug);
Wim Taymans's avatar
Wim Taymans committed
329 330 331
      str = gst_element_get_name (message->src);
      g_print ("%s error: %s\n", str, err->message);
      g_free (str);
332 333
      g_print ("Debug: %s\n", debug);

334 335 336
      g_error_free (err);
      g_free (debug);

337
      printf ("presse <ENTER> key to exit\n");
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
      exit_read = 1;
      g_main_loop_quit (loop);
      break;
    case GST_MESSAGE_EOS:
      /* end-of-stream */
      printf ("presse any key to exit\n");
      exit_read = 1;
      g_main_loop_quit (loop);
      break;
    default:
      break;
    }
  }
  return TRUE;
}

int
main (int argc, char *argv[])
{

  GThread *input_thread;
359 360 361 362
  gint numbuffers = -1;
  gchar device[128] = { '\0' };
  gchar input[128] = { '\0' };
  gulong frequency = 0;
Wim Taymans's avatar
Wim Taymans committed
363
  GstBus *bus;
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386

  /* see for input option */

  int c;

  while (1) {
    static char long_options_desc[][64] = {
      {"Number of buffers to output before sending EOS"},
      {"Device location. Common in /dev/video0"},
      {"input/output (channel) to switch to"},
      {"frequency to tune to (in Hz)"},
      {0, 0, 0, 0}
    };
    static struct option long_options[] = {
      {"numbuffers", 1, 0, 'n'},
      {"device", 1, 0, 'd'},
      {"input", 1, 0, 'i'},
      {"frequency", 1, 0, 'f'},
      {0, 0, 0, 0}
    };
    /* getopt_long stores the option index here. */
    int option_index = 0;

387
    c = getopt_long (argc, argv, "n:d:i:f:h", long_options, &option_index);
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448

    /* Detect the end of the options. */
    if (c == -1) {
      printf ("tip: use -h to see help message.\n");
      break;
    }

    switch (c) {
      case 0:
        /* If this option set a flag, do nothing else now. */
        if (long_options[option_index].flag != 0)
          break;
        printf ("option %s", long_options[option_index].name);
        if (optarg)
          printf (" with arg %s", optarg);
        printf ("\n");
        break;

      case 'n':
        numbuffers = atoi (optarg);
        break;

      case 'd':
        strncpy (device, optarg, sizeof (device) / sizeof (device[0]));
        break;

      case 'i':
        strncpy (input, optarg, sizeof (input) / sizeof (input[0]));
        break;

      case 'f':
        frequency = atol (optarg);
        break;

      case 'h':
        printf ("Usage: v4l2src-test [OPTION]...\n");
        for (c = 0; long_options[c].name; ++c) {
          printf ("-%c, --%s\r\t\t\t\t%s\n", long_options[c].val,
              long_options[c].name, long_options_desc[c]);
        }
        exit (0);
        break;

      case '?':
        /* getopt_long already printed an error message. */
        printf ("Use -h to see help message.\n");
        break;

      default:
        abort ();
    }
  }

  /* Print any remaining command line arguments (not options). */
  if (optind < argc) {
    printf ("Use -h to see help message.\n" "non-option ARGV-elements: ");
    while (optind < argc)
      printf ("%s ", argv[optind++]);
    putchar ('\n');
  }

449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
  /* init */
  gst_init (&argc, &argv);

  /* create elements */
  if (!(pipeline = gst_pipeline_new ("my_pipeline"))) {
    fprintf (stderr, "error: gst_pipeline_new return NULL");
    return -1;
  }

  if (!(source = gst_element_factory_make ("v4l2src", NULL))) {
    fprintf (stderr,
        "error: gst_element_factory_make (\"v4l2src\", NULL) return NULL");
    return -1;
  }

  if (!(sink = gst_element_factory_make ("xvimagesink", NULL))) {
    fprintf (stderr,
        "error: gst_element_factory_make (\"xvimagesink\", NULL) return NULL");
    return -1;
  }

470 471 472 473 474 475 476 477 478 479 480 481
  if (numbuffers > -1) {
    g_object_set (source, "num-buffers", numbuffers, NULL);
  }
  if (device[0]) {
    g_object_set (source, "device", device, NULL);
  }
  if (input[0]) {
    g_object_set (source, "input", input, NULL);
  }
  if (frequency) {
    g_object_set (source, "frequency", frequency, NULL);
  }
482 483

  /* you would normally check that the elements were created properly */
Wim Taymans's avatar
Wim Taymans committed
484 485
  bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
  gst_bus_add_watch (bus, my_bus_callback, NULL);
486 487 488 489 490 491 492 493 494

  /* put together a pipeline */
  gst_bin_add_many (GST_BIN (pipeline), source, sink, NULL);
  gst_element_link_pads (source, "src", sink, "sink");

  /* start the pipeline */
  gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
  loop = g_main_loop_new (NULL, FALSE);

495 496 497
  input_thread = g_thread_try_new ("v4l2src-test", read_user, source, NULL);

  if (input_thread == NULL) {
498
    fprintf (stderr, "error: g_thread_try_new() failed");
499 500 501 502 503 504 505 506
    return -1;
  }

  g_main_loop_run (loop);
  g_thread_join (input_thread);

  gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);

Wim Taymans's avatar
Wim Taymans committed
507
  gst_object_unref (bus);
508 509 510 511 512 513 514
  gst_object_unref (pipeline);

  gst_deinit ();

  return 0;

}