summaryrefslogtreecommitdiff
path: root/profiles/mate-audio-profiles-test.c
blob: 444e9c62f7591a03fbedc2f35f8fcfb593c32b42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* mate-audio-profiles-test.c: */

/*
 * Copyright (C) 2003 Thomas Vander Stichele
 *
 * 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
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <gtk/gtk.h>
#include <mateconf/mateconf-client.h>
#include <gst/gst.h>

#include <profiles/mate-media-profiles.h>

static void
edit_clicked_cb (GtkButton *button, GtkWindow *window)
{
  GtkWidget *edit_dialog = NULL;
  edit_dialog = gm_audio_profiles_edit_new (mateconf_client_get_default (), window);
  g_assert (edit_dialog != NULL);
  gtk_widget_show_all (GTK_WIDGET (edit_dialog));
}

static void
test_clicked_cb (GtkButton *button, GtkWidget *combo)
{
  GstStateChangeReturn ret;
  gchar *partialpipe = NULL;
  gchar *extension = NULL;
  gchar *pipeline_desc;
  GError *error = NULL;
  GMAudioProfile *profile;
  GstElement *pipeline = NULL;
  GstMessage *msg = NULL;
  GstBus *bus = NULL;

  profile = gm_audio_profile_choose_get_active (combo);
  g_return_if_fail (profile != NULL);

  gtk_widget_set_sensitive (GTK_WIDGET (button), FALSE);

  extension = g_strdup (gm_audio_profile_get_extension (profile));
  partialpipe = g_strdup (gm_audio_profile_get_pipeline (profile));

  g_print ("You chose profile with name %s and pipeline %s\n",
           gm_audio_profile_get_name (profile),
           gm_audio_profile_get_pipeline (profile));

  pipeline_desc = g_strdup_printf ("audiotestsrc wave=sine num-buffers=4096 "
                                   " ! audioconvert "
                                   " ! %s "
                                   " ! filesink location=test.%s",
                                   partialpipe, extension);

  g_print ("Going to run pipeline %s\n", pipeline_desc);

  pipeline = gst_parse_launch (pipeline_desc, &error);
  if (error)
  {
    g_warning ("Error parsing pipeline: %s", error->message);
    goto done;
  }

  bus = gst_element_get_bus (pipeline);

  gst_element_set_state (pipeline, GST_STATE_PLAYING);

  /* wait for state change to complete or to have failed */
  ret = gst_element_get_state (pipeline, NULL, NULL, -1);
  if (ret == GST_STATE_CHANGE_FAILURE) {
    /* check if an error was posted on the bus */
    if ((msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0))) {
      gst_message_parse_error (msg, &error, NULL);
    }

    g_warning ("Error starting pipeline: %s",
        (error) ? error->message : "UNKNOWN ERROR");

    goto done;
  }

  g_print ("Writing test sound to test.%s ...\n", extension);

  /* wait for it finish (error or EOS), but no more than 30 secs */
  msg = gst_bus_poll (bus, GST_MESSAGE_ERROR | GST_MESSAGE_EOS, 30*GST_SECOND);

  if (msg) {
    switch (GST_MESSAGE_TYPE (msg)) {
      case GST_MESSAGE_EOS:
        g_print ("Test finished successfully.\n");
        break;
      case GST_MESSAGE_ERROR:
        gst_message_parse_error (msg, &error, NULL);
        g_warning ("Error starting pipeline: %s",
            (error) ? error->message : "UNKNOWN ERROR");
        break;
      default:
        g_assert_not_reached ();
    }
  } else {
    g_warning ("Test did not finish within 30 seconds!\n");
  }

done:

  g_print ("==============================================================\n");

  if (error)
    g_error_free (error);

  if (pipeline) {
    gst_element_set_state (pipeline, GST_STATE_NULL);
    gst_object_unref (pipeline);
  }

  if (msg)
    gst_message_unref (msg);

  if (bus)
    gst_object_unref (bus);

  g_free (pipeline_desc);
  g_free (partialpipe);
  g_free (extension);

  gtk_widget_set_sensitive (GTK_WIDGET (button), TRUE);
}

int
main (int argc, char **argv)
{
  GtkWidget *window, *hbox, *combo, *edit, *test;
  MateConfClient *mateconf;
  GOptionContext *context;
  GError *error = NULL;

  g_thread_init (NULL);
  context = g_option_context_new (NULL);
  g_option_context_add_group (context, gst_init_get_option_group ());
  g_option_context_add_group (context, gtk_get_option_group (TRUE));
  if (g_option_context_parse (context, &argc, &argv, &error) == FALSE) {
	  g_print ("%s\nRun '%s --help' to see a full list of available command line options.\n",
		   error->message, argv[0]);
	  g_error_free (error);
	  g_option_context_free (context);
	  exit (1);
  }
  g_option_context_free (context);

  mateconf = mateconf_client_get_default ();
  mate_media_profiles_init (mateconf);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  combo = gm_audio_profile_choose_new ();

  edit = gtk_button_new_with_mnemonic ("_Edit Profiles");
  test = gtk_button_new_with_mnemonic ("_Test");
  g_signal_connect (edit, "clicked", (GCallback) edit_clicked_cb, window);
  g_signal_connect (test, "clicked", (GCallback) test_clicked_cb, combo);
  g_signal_connect (edit, "destroy", (GCallback) gtk_main_quit, NULL);

  hbox = gtk_hbox_new (FALSE, 7);
  gtk_box_pack_start (GTK_BOX (hbox), combo, TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (hbox), test, FALSE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (hbox), edit, FALSE, TRUE, 0);
  gtk_container_add (GTK_CONTAINER (window), hbox);
  gtk_widget_show_all (window);
  gtk_main ();

  return 0;
}