Fix test for bug #32625
[gnash.git] / utilities / findmicrophones.cpp
blobeb5218d67527ff1a8e0470762de61797c3136028
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
3 // Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifdef HAVE_CONFIG_H
20 #include "gnashconfig.h"
21 #endif
23 #include "rc.h"
24 #ifdef HAVE_GST_GST_H
25 #include "gst/gst.h"
26 #endif
28 #ifdef HAVE_GST_INTERFACES_PROBEPROBE_H
29 #include <gst/interfaces/propertyprobe.h>
30 #endif
32 #include <vector>
33 #include <string>
34 #include <iostream>
36 namespace {
37 //get rc file for webcam selection
38 gnash::RcInitFile& rcfile = gnash::RcInitFile::getDefaultInstance();
41 class data {
42 public:
43 data();
44 gchar* deviceName;
45 gchar* deviceType;
48 data::data() {
49 deviceName = NULL;
50 deviceType = NULL;
53 gint findAudioDevs(std::vector<data*>& audioVect) {
54 gint numdevs = 0;
56 //vid test source
57 GstElement *element;
58 element = gst_element_factory_make ("audiotestsrc", "audiotestsrc");
60 if (element == NULL) {
61 audioVect.push_back(NULL);
62 numdevs += 1;
63 } else {
64 audioVect.push_back(new data);
65 audioVect[numdevs]->deviceName = g_strdup_printf("audiotestsrc");
66 audioVect[numdevs]->deviceType = g_strdup_printf("audiotestsrc");
67 numdevs += 1;
70 //pulseaudio src
71 GstPropertyProbe *probe;
72 GValueArray *devarr;
73 element = NULL;
75 element = gst_element_factory_make ("pulsesrc", "pulsesrc");
76 probe = GST_PROPERTY_PROBE (element);
77 devarr = gst_property_probe_probe_and_get_values_name (probe, "device");
78 for (size_t i = 0; devarr != NULL && i < devarr->n_values; ++i) {
79 GValue *val;
80 gchar *dev_name = NULL;
82 val = g_value_array_get_nth (devarr, i);
83 g_object_set (element, "device", g_value_get_string (val), NULL);
84 gst_element_set_state (element, GST_STATE_PLAYING);
85 g_object_get (element, "device-name", &dev_name, NULL);
86 gst_element_set_state (element, GST_STATE_NULL);
87 if (strcmp(dev_name, "null") == 0) {
88 g_print("no pulse audio sources found\n");
90 else if ((strstr(dev_name, "Monitor") != NULL)) {
91 g_print("ignoring monitor (audio output)");
93 else {
94 audioVect.push_back(new data);
95 audioVect[numdevs]->deviceType = g_strdup_printf("pulsesrc");
96 audioVect[numdevs]->deviceName = dev_name;
97 numdevs += 1;
100 if (devarr) {
101 g_value_array_free (devarr);
103 return numdevs;
106 int main () {
107 //initialize gstreamer to probe for devs
108 gst_init(NULL, NULL);
109 gint numdevs = 0;
110 std::vector<data*> audioVector;
111 gint i;
113 int fromrc = rcfile.getAudioInputDevice();
115 if (fromrc == -1) {
116 g_print("Use this utility to set your desired default microphone device.\n");
117 numdevs = findAudioDevs(audioVector);
118 g_print("\nFound %d audio input devices: \n\n", numdevs);
119 for (i = 0; i < numdevs; ++i)
121 if (i == 0 && (audioVector[i] != 0)) {
122 g_print("%d. device[%d] = Audio Test Source (audiotestsrc)\n", i, i);
123 } else if (i == 0 && (audioVector[i] == 0)) {
124 g_print("no test audio device available\n");
125 } else {
126 g_print("%d. device[%d] = %s (%s)\n", i, i, audioVector[i]->deviceName,
127 audioVector[i]->deviceType);
130 //prompt user for device selection
131 int dev_select = -1;
132 std::string fromCin;
133 do {
134 dev_select = -1;
135 g_print("\nChoose the device you would like to use (0-%d): ",
136 (numdevs - 1));
137 std::cin >> fromCin;
138 if (fromCin.size() != 1) {
139 dev_select = -1;
140 } else if (fromCin[0] == '0') {
141 dev_select = 0;
142 } else {
143 dev_select = atoi(fromCin.c_str());
145 if ((dev_select < 0) || (dev_select > (numdevs - 1))) {
146 g_print("You must make a valid device selection\n");
148 } while ((dev_select < 0) || (dev_select > (numdevs - 1)));
149 g_print("\nTo select this microphone, add this line to your gnashrc file:\n");
150 g_print("set microphoneDevice %d\n", dev_select);
151 } else {
152 numdevs = findAudioDevs(audioVector);
153 if ((size_t)fromrc < audioVector.size()) {
154 g_print("\nThe gnashrc file reports default microphone is set to:\n");
155 g_print("%s (%s)\n", audioVector[fromrc]->deviceName,
156 audioVector[fromrc]->deviceType);
157 g_print("To change this setting, delete the 'set microphoneDevice' line\n");
158 g_print("from your gnashrc file and re-run this program.\n\n");
159 } else {
160 g_print("\nYou have an invalid microphone chosen in your gnashrc file.\n");
161 g_print("Try reattaching the device or deleting the value from gnashrc\n");
162 g_print("and running this program again\n");
165 return 1;