gst_plugins_base/tests/examples/volume/volume.c
changeset 2 5505e8908944
parent 0 0e761a78d257
equal deleted inserted replaced
1:4c282e7dd6d3 2:5505e8908944
       
     1 /* GStreamer
       
     2  *
       
     3  * volume.c: sample application to change the volume of a pipeline
       
     4  *
       
     5  * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
       
     6  *
       
     7  * This library is free software; you can redistribute it and/or
       
     8  * modify it under the terms of the GNU Library General Public
       
     9  * License as published by the Free Software Foundation; either
       
    10  * version 2 of the License, or (at your option) any later version.
       
    11  *
       
    12  * This library is distributed in the hope that it will be useful,
       
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    15  * Library General Public License for more details.
       
    16  *
       
    17  * You should have received a copy of the GNU Library General Public
       
    18  * License along with this library; if not, write to the
       
    19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    20  * Boston, MA 02111-1307, USA.
       
    21  */
       
    22 
       
    23 #ifdef HAVE_CONFIG_H
       
    24 #include "config.h"
       
    25 #endif
       
    26 
       
    27 #include <math.h>
       
    28 
       
    29 #include <gst/gst.h>
       
    30 #include <gtk/gtk.h>
       
    31 
       
    32 /* global pointer for the scale widget */
       
    33 GtkWidget *elapsed;
       
    34 GtkWidget *scale;
       
    35 
       
    36 #ifndef M_LN10
       
    37 #define M_LN10 (log(10.0))
       
    38 #endif
       
    39 
       
    40 static void
       
    41 value_changed_callback (GtkWidget * widget, GstElement * volume)
       
    42 {
       
    43   gdouble value;
       
    44   gdouble level;
       
    45 
       
    46   value = gtk_range_get_value (GTK_RANGE (widget));
       
    47   level = exp (value / 20.0 * M_LN10);
       
    48   g_print ("Value: %f dB, level: %f\n", value, level);
       
    49   g_object_set (volume, "volume", level, NULL);
       
    50 }
       
    51 
       
    52 static void
       
    53 setup_gui (GstElement * volume)
       
    54 {
       
    55   GtkWidget *window;
       
    56   GtkWidget *vbox;
       
    57   GtkWidget *label, *hbox;
       
    58 
       
    59   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
       
    60   g_signal_connect (window, "destroy", gtk_main_quit, NULL);
       
    61 
       
    62   vbox = gtk_vbox_new (TRUE, 0);
       
    63   gtk_container_add (GTK_CONTAINER (window), vbox);
       
    64 
       
    65   /* elapsed widget */
       
    66   hbox = gtk_hbox_new (TRUE, 0);
       
    67   label = gtk_label_new ("Elapsed");
       
    68   elapsed = gtk_label_new ("0.000");
       
    69   gtk_container_add (GTK_CONTAINER (hbox), label);
       
    70   gtk_container_add (GTK_CONTAINER (hbox), elapsed);
       
    71   gtk_container_add (GTK_CONTAINER (vbox), hbox);
       
    72 
       
    73   /* volume */
       
    74   hbox = gtk_hbox_new (TRUE, 0);
       
    75   label = gtk_label_new ("volume");
       
    76   gtk_container_add (GTK_CONTAINER (hbox), label);
       
    77   scale = gtk_hscale_new_with_range (-90.0, 10.0, 0.2);
       
    78   gtk_range_set_value (GTK_RANGE (scale), 0.0);
       
    79   gtk_widget_set_size_request (scale, 100, -1);
       
    80   gtk_container_add (GTK_CONTAINER (hbox), scale);
       
    81   gtk_container_add (GTK_CONTAINER (vbox), hbox);
       
    82   g_signal_connect (scale, "value-changed",
       
    83       G_CALLBACK (value_changed_callback), volume);
       
    84 
       
    85   gtk_widget_show_all (GTK_WIDGET (window));
       
    86 }
       
    87 
       
    88 static void
       
    89 message_received (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
       
    90 {
       
    91   const GstStructure *s;
       
    92 
       
    93   s = gst_message_get_structure (message);
       
    94   g_print ("message from \"%s\" (%s): ",
       
    95       GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
       
    96       gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
       
    97   if (s) {
       
    98     gchar *sstr;
       
    99 
       
   100     sstr = gst_structure_to_string (s);
       
   101     g_print ("%s\n", sstr);
       
   102     g_free (sstr);
       
   103   } else {
       
   104     g_print ("no message details\n");
       
   105   }
       
   106 }
       
   107 
       
   108 static void
       
   109 eos_message_received (GstBus * bus, GstMessage * message,
       
   110     GstPipeline * pipeline)
       
   111 {
       
   112   message_received (bus, message, pipeline);
       
   113   gtk_main_quit ();
       
   114 }
       
   115 
       
   116 int
       
   117 main (int argc, char *argv[])
       
   118 {
       
   119 
       
   120   GstElement *pipeline = NULL;
       
   121 
       
   122 #ifndef GST_DISABLE_PARSE
       
   123   GError *error = NULL;
       
   124 #endif
       
   125   GstElement *volume;
       
   126   GstBus *bus;
       
   127 
       
   128 #ifdef GST_DISABLE_PARSE
       
   129   g_print ("GStreamer was built without pipeline parsing capabilities.\n");
       
   130   g_print
       
   131       ("Please rebuild GStreamer with pipeline parsing capabilities activated to use this example.\n");
       
   132   return 1;
       
   133 #else
       
   134   gst_init (&argc, &argv);
       
   135   gtk_init (&argc, &argv);
       
   136 
       
   137   pipeline = gst_parse_launchv ((const gchar **) &argv[1], &error);
       
   138   if (error) {
       
   139     g_print ("pipeline could not be constructed: %s\n", error->message);
       
   140     g_print ("Please give a complete pipeline  with a 'volume' element.\n");
       
   141     g_print ("Example: audiotestsrc ! volume ! %s\n", DEFAULT_AUDIOSINK);
       
   142     g_error_free (error);
       
   143     return 1;
       
   144   }
       
   145 #endif
       
   146   volume = gst_bin_get_by_name (GST_BIN (pipeline), "volume0");
       
   147   if (volume == NULL) {
       
   148     g_print ("Please give a pipeline with a 'volume' element in it\n");
       
   149     return 1;
       
   150   }
       
   151 
       
   152   /* setup message handling */
       
   153   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
       
   154   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
       
   155   g_signal_connect (bus, "message::error", (GCallback) message_received,
       
   156       pipeline);
       
   157   g_signal_connect (bus, "message::warning", (GCallback) message_received,
       
   158       pipeline);
       
   159   g_signal_connect (bus, "message::eos", (GCallback) eos_message_received,
       
   160       pipeline);
       
   161 
       
   162   /* setup GUI */
       
   163   setup_gui (volume);
       
   164 
       
   165   /* go to main loop */
       
   166   gst_element_set_state (pipeline, GST_STATE_PLAYING);
       
   167   gtk_main ();
       
   168   gst_element_set_state (pipeline, GST_STATE_NULL);
       
   169   gst_object_unref (pipeline);
       
   170 
       
   171   return 0;
       
   172 }