gstreamer/examples/tutorials/playback-tutorial-5.c

166 lines
4.5 KiB
C
Raw Normal View History

2012-06-22 10:57:24 +00:00
#include <string.h>
2013-10-31 09:13:31 +00:00
#include <stdio.h>
2012-06-22 10:57:24 +00:00
#include <gst/gst.h>
2013-10-31 09:13:31 +00:00
#include <gst/video/colorbalance.h>
2019-02-07 19:32:58 +00:00
typedef struct _CustomData
{
2012-06-21 14:08:19 +00:00
GstElement *pipeline;
GMainLoop *loop;
2012-06-22 10:57:24 +00:00
} CustomData;
2012-06-22 10:57:24 +00:00
/* Process a color balance command */
2019-02-07 19:32:58 +00:00
static void
update_color_channel (const gchar * channel_name, gboolean increase,
GstColorBalance * cb)
{
2012-06-22 10:57:24 +00:00
gdouble step;
gint value;
GstColorBalanceChannel *channel = NULL;
const GList *channels, *l;
2012-06-22 10:57:24 +00:00
/* Retrieve the list of channels and locate the requested one */
channels = gst_color_balance_list_channels (cb);
for (l = channels; l != NULL; l = l->next) {
2019-02-07 19:32:58 +00:00
GstColorBalanceChannel *tmp = (GstColorBalanceChannel *) l->data;
2012-06-22 10:57:24 +00:00
if (g_strrstr (tmp->label, channel_name)) {
channel = tmp;
break;
}
}
if (!channel)
return;
2012-06-22 10:57:24 +00:00
/* Change the channel's value */
step = 0.1 * (channel->max_value - channel->min_value);
value = gst_color_balance_get_value (cb, channel);
if (increase) {
2019-02-07 19:32:58 +00:00
value = (gint) (value + step);
2012-06-22 10:57:24 +00:00
if (value > channel->max_value)
value = channel->max_value;
2012-06-22 10:57:24 +00:00
} else {
2019-02-07 19:32:58 +00:00
value = (gint) (value - step);
2012-06-22 10:57:24 +00:00
if (value < channel->min_value)
value = channel->min_value;
2012-06-22 10:57:24 +00:00
}
gst_color_balance_set_value (cb, channel, value);
}
/* Output the current values of all Color Balance channels */
2019-02-07 19:32:58 +00:00
static void
print_current_values (GstElement * pipeline)
{
2012-06-22 10:57:24 +00:00
const GList *channels, *l;
2012-06-22 10:57:24 +00:00
/* Output Color Balance values */
channels = gst_color_balance_list_channels (GST_COLOR_BALANCE (pipeline));
for (l = channels; l != NULL; l = l->next) {
2019-02-07 19:32:58 +00:00
GstColorBalanceChannel *channel = (GstColorBalanceChannel *) l->data;
gint value =
gst_color_balance_get_value (GST_COLOR_BALANCE (pipeline), channel);
2012-06-22 10:57:24 +00:00
g_print ("%s: %3d%% ", channel->label,
2019-02-07 19:32:58 +00:00
100 * (value - channel->min_value) / (channel->max_value -
channel->min_value));
2012-06-22 10:57:24 +00:00
}
g_print ("\n");
2012-06-22 10:57:24 +00:00
}
2012-06-22 10:57:24 +00:00
/* Process keyboard input */
2019-02-07 19:32:58 +00:00
static gboolean
handle_keyboard (GIOChannel * source, GIOCondition cond, CustomData * data)
{
2012-06-22 10:57:24 +00:00
gchar *str = NULL;
2019-02-07 19:32:58 +00:00
if (g_io_channel_read_line (source, &str, NULL, NULL,
NULL) != G_IO_STATUS_NORMAL) {
2012-06-22 10:57:24 +00:00
return TRUE;
}
2012-06-22 10:57:24 +00:00
switch (g_ascii_tolower (str[0])) {
2019-02-07 19:32:58 +00:00
case 'c':
update_color_channel ("CONTRAST", g_ascii_isupper (str[0]),
GST_COLOR_BALANCE (data->pipeline));
break;
case 'b':
update_color_channel ("BRIGHTNESS", g_ascii_isupper (str[0]),
GST_COLOR_BALANCE (data->pipeline));
break;
case 'h':
update_color_channel ("HUE", g_ascii_isupper (str[0]),
GST_COLOR_BALANCE (data->pipeline));
break;
case 's':
update_color_channel ("SATURATION", g_ascii_isupper (str[0]),
GST_COLOR_BALANCE (data->pipeline));
break;
case 'q':
g_main_loop_quit (data->loop);
break;
default:
break;
2012-06-22 10:57:24 +00:00
}
2012-06-22 10:57:24 +00:00
g_free (str);
2012-06-22 10:57:24 +00:00
print_current_values (data->pipeline);
2012-06-22 10:57:24 +00:00
return TRUE;
}
2019-02-07 19:32:58 +00:00
int
main (int argc, char *argv[])
{
2012-06-22 10:57:24 +00:00
CustomData data;
GstStateChangeReturn ret;
GIOChannel *io_stdin;
2012-06-22 10:57:24 +00:00
/* Initialize GStreamer */
gst_init (&argc, &argv);
2012-06-22 10:57:24 +00:00
/* Initialize our data structure */
memset (&data, 0, sizeof (data));
2012-06-22 10:57:24 +00:00
/* Print usage map */
2019-02-07 19:32:58 +00:00
g_print ("USAGE: Choose one of the following options, then press enter:\n"
" 'C' to increase contrast, 'c' to decrease contrast\n"
" 'B' to increase brightness, 'b' to decrease brightness\n"
" 'H' to increase hue, 'h' to decrease hue\n"
" 'S' to increase saturation, 's' to decrease saturation\n"
" 'Q' to quit\n");
2012-06-22 10:57:24 +00:00
/* Build the pipeline */
2019-02-07 19:32:58 +00:00
data.pipeline =
gst_parse_launch
("playbin uri=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm",
NULL);
2012-06-22 10:57:24 +00:00
/* Add a keyboard watch so we get notified of keystrokes */
2013-10-31 09:13:31 +00:00
#ifdef G_OS_WIN32
2012-06-22 10:57:24 +00:00
io_stdin = g_io_channel_win32_new_fd (fileno (stdin));
#else
io_stdin = g_io_channel_unix_new (fileno (stdin));
#endif
2019-02-07 19:32:58 +00:00
g_io_add_watch (io_stdin, G_IO_IN, (GIOFunc) handle_keyboard, &data);
2012-06-22 10:57:24 +00:00
/* Start playing */
ret = gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
if (ret == GST_STATE_CHANGE_FAILURE) {
g_printerr ("Unable to set the pipeline to the playing state.\n");
gst_object_unref (data.pipeline);
return -1;
}
print_current_values (data.pipeline);
2012-06-22 10:57:24 +00:00
/* Create a GLib Main Loop and set it to run */
data.loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (data.loop);
2012-06-22 10:57:24 +00:00
/* Free resources */
g_main_loop_unref (data.loop);
g_io_channel_unref (io_stdin);
gst_element_set_state (data.pipeline, GST_STATE_NULL);
gst_object_unref (data.pipeline);
return 0;
}