Update theme submodule
[platform/upstream/gstreamer.git] / examples / tutorials / playback-tutorial-5.c
1 #include <string.h>
2 #include <stdio.h>
3 #include <gst/gst.h>
4 #include <gst/video/colorbalance.h>
5
6 typedef struct _CustomData {
7   GstElement *pipeline;
8   GMainLoop *loop;
9 } CustomData;
10
11 /* Process a color balance command */
12 static void update_color_channel (const gchar *channel_name, gboolean increase, GstColorBalance *cb) {
13   gdouble step;
14   gint value;
15   GstColorBalanceChannel *channel = NULL;
16   const GList *channels, *l;
17
18   /* Retrieve the list of channels and locate the requested one */
19   channels = gst_color_balance_list_channels (cb);
20   for (l = channels; l != NULL; l = l->next) {
21     GstColorBalanceChannel *tmp = (GstColorBalanceChannel *)l->data;
22
23     if (g_strrstr (tmp->label, channel_name)) {
24       channel = tmp;
25       break;
26     }
27   }
28   if (!channel)
29     return;
30
31   /* Change the channel's value */
32   step = 0.1 * (channel->max_value - channel->min_value);
33   value = gst_color_balance_get_value (cb, channel);
34   if (increase) {
35     value = (gint)(value + step);
36     if (value > channel->max_value)
37       value = channel->max_value;
38   } else {
39     value = (gint)(value - step);
40     if (value < channel->min_value)
41       value = channel->min_value;
42   }
43   gst_color_balance_set_value (cb, channel, value);
44 }
45
46 /* Output the current values of all Color Balance channels */
47 static void print_current_values (GstElement *pipeline) {
48   const GList *channels, *l;
49
50   /* Output Color Balance values */
51   channels = gst_color_balance_list_channels (GST_COLOR_BALANCE (pipeline));
52   for (l = channels; l != NULL; l = l->next) {
53     GstColorBalanceChannel *channel = (GstColorBalanceChannel *)l->data;
54     gint value = gst_color_balance_get_value (GST_COLOR_BALANCE (pipeline), channel);
55     g_print ("%s: %3d%% ", channel->label,
56         100 * (value - channel->min_value) / (channel->max_value - channel->min_value));
57   }
58   g_print ("\n");
59 }
60
61 /* Process keyboard input */
62 static gboolean handle_keyboard (GIOChannel *source, GIOCondition cond, CustomData *data) {
63   gchar *str = NULL;
64
65   if (g_io_channel_read_line (source, &str, NULL, NULL, NULL) != G_IO_STATUS_NORMAL) {
66     return TRUE;
67   }
68
69   switch (g_ascii_tolower (str[0])) {
70   case 'c':
71     update_color_channel ("CONTRAST", g_ascii_isupper (str[0]), GST_COLOR_BALANCE (data->pipeline));
72     break;
73   case 'b':
74     update_color_channel ("BRIGHTNESS", g_ascii_isupper (str[0]), GST_COLOR_BALANCE (data->pipeline));
75     break;
76   case 'h':
77     update_color_channel ("HUE", g_ascii_isupper (str[0]), GST_COLOR_BALANCE (data->pipeline));
78     break;
79   case 's':
80     update_color_channel ("SATURATION", g_ascii_isupper (str[0]), GST_COLOR_BALANCE (data->pipeline));
81     break;
82   case 'q':
83     g_main_loop_quit (data->loop);
84     break;
85   default:
86     break;
87   }
88
89   g_free (str);
90
91   print_current_values (data->pipeline);
92
93   return TRUE;
94 }
95
96 int main(int argc, char *argv[]) {
97   CustomData data;
98   GstStateChangeReturn ret;
99   GIOChannel *io_stdin;
100
101   /* Initialize GStreamer */
102   gst_init (&argc, &argv);
103
104   /* Initialize our data structure */
105   memset (&data, 0, sizeof (data));
106
107   /* Print usage map */
108   g_print (
109     "USAGE: Choose one of the following options, then press enter:\n"
110     " 'C' to increase contrast, 'c' to decrease contrast\n"
111     " 'B' to increase brightness, 'b' to decrease brightness\n"
112     " 'H' to increase hue, 'h' to decrease hue\n"
113     " 'S' to increase saturation, 's' to decrease saturation\n"
114     " 'Q' to quit\n");
115
116   /* Build the pipeline */
117   data.pipeline = gst_parse_launch ("playbin uri=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm", NULL);
118
119   /* Add a keyboard watch so we get notified of keystrokes */
120 #ifdef G_OS_WIN32
121   io_stdin = g_io_channel_win32_new_fd (fileno (stdin));
122 #else
123   io_stdin = g_io_channel_unix_new (fileno (stdin));
124 #endif
125   g_io_add_watch (io_stdin, G_IO_IN, (GIOFunc)handle_keyboard, &data);
126
127   /* Start playing */
128   ret = gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
129   if (ret == GST_STATE_CHANGE_FAILURE) {
130     g_printerr ("Unable to set the pipeline to the playing state.\n");
131     gst_object_unref (data.pipeline);
132     return -1;
133   }
134   print_current_values (data.pipeline);
135
136   /* Create a GLib Main Loop and set it to run */
137   data.loop = g_main_loop_new (NULL, FALSE);
138   g_main_loop_run (data.loop);
139
140   /* Free resources */
141   g_main_loop_unref (data.loop);
142   g_io_channel_unref (io_stdin);
143   gst_element_set_state (data.pipeline, GST_STATE_NULL);
144   gst_object_unref (data.pipeline);
145   return 0;
146 }