enable docbook, don't gzip man pages
[gnash.git] / utilities / findwebcams.cpp
blob8acc433620842d324325a3e5044761cc437135d0
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software 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 <vector>
24 #include <string>
25 #include <iostream>
27 #include "rc.h"
28 #include "gst/gst.h"
29 #include <gst/interfaces/propertyprobe.h>
31 namespace {
32 //get rc file for webcam selection
33 gnash::RcInitFile& rcfile = gnash::RcInitFile::getDefaultInstance();
36 class data {
37 public:
38 data();
39 gchar* deviceName;
40 gchar* deviceType;
41 gint deviceNumber;
42 gboolean duplicate;
45 data::data() {
46 deviceName = NULL;
47 deviceType = NULL;
48 deviceNumber = -1;
49 duplicate = false;
52 gint numDuplicates = 0;
54 size_t findVidDevs(std::vector<data*>& vidVect) {
55 gint numdevs = 0;
57 //vid test source
58 GstElement *element;
59 element = gst_element_factory_make ("videotestsrc", "vidtestsrc");
61 if (element == NULL) {
62 vidVect.push_back(NULL);
63 numdevs += 1;
64 } else {
65 vidVect.push_back(new data);
66 vidVect[numdevs]->deviceName = g_strdup_printf("videotestsrc");
67 vidVect[numdevs]->deviceType = g_strdup_printf("videotestsrc");
68 vidVect[numdevs]->deviceNumber = 0;
69 numdevs += 1;
72 //video4linux source
73 GstPropertyProbe *probe;
74 GValueArray *devarr;
75 element = NULL;
77 element = gst_element_factory_make ("v4lsrc", "v4lvidsrc");
78 probe = GST_PROPERTY_PROBE (element);
79 devarr = gst_property_probe_probe_and_get_values_name (probe, "device");
80 for (size_t i = 0; devarr != NULL && i < devarr->n_values; ++i) {
81 GValue *val;
82 gchar *dev_name = NULL;
84 val = g_value_array_get_nth (devarr, i);
85 g_object_set (element, "device", g_value_get_string (val), NULL);
86 gst_element_set_state (element, GST_STATE_PLAYING);
87 g_object_get (element, "device-name", &dev_name, NULL);
88 gst_element_set_state (element, GST_STATE_NULL);
89 if (strcmp(dev_name, "null") == 0) {
90 std::cout << "no v4l video sources found" << std::endl;
92 else {
93 vidVect.push_back(new data);
94 vidVect[numdevs]->deviceType = g_strdup_printf("v4lsrc");
95 vidVect[numdevs]->deviceName = dev_name;
96 vidVect[numdevs]->deviceNumber = numdevs;
97 numdevs += 1;
100 if (devarr) {
101 g_value_array_free (devarr);
105 //video4linux2 source
106 probe = NULL;
107 element = NULL;
108 devarr = NULL;
110 element = gst_element_factory_make ("v4l2src", "v4l2vidsrc");
111 probe = GST_PROPERTY_PROBE (element);
112 devarr = gst_property_probe_probe_and_get_values_name (probe, "device");
113 for (size_t i = 0; devarr != NULL && i < devarr->n_values; ++i) {
114 GValue *val;
115 gchar *dev_name = NULL;
117 val = g_value_array_get_nth (devarr, i);
118 g_object_set (element, "device", g_value_get_string (val), NULL);
119 gst_element_set_state (element, GST_STATE_PLAYING);
120 g_object_get (element, "device-name", &dev_name, NULL);
121 gst_element_set_state (element, GST_STATE_NULL);
122 if (strcmp(dev_name, "null") == 0) {
123 std::cout << "no v4l2 video sources found." << std::endl;
125 else {
126 vidVect.push_back(new data);
127 vidVect[numdevs]->deviceType = g_strdup_printf("v4l2src");
128 vidVect[numdevs]->deviceName = dev_name;
129 vidVect[numdevs]->deviceNumber = numdevs;
130 //mark duplicates (we like v4l2 sources more than v4l, so if
131 //they're both detected, mark the v4l source as a duplicate)
132 for (size_t g=1; g < (vidVect.size()-1); g++) {
133 if (strcmp(vidVect[numdevs]->deviceName,
134 vidVect[g]->deviceName) == 0) {
135 vidVect[g]->duplicate = true;
136 numDuplicates += 1;
139 numdevs += 1;
142 if (devarr) {
143 g_value_array_free (devarr);
145 return numdevs;
148 int main () {
149 //initialize gstreamer to probe for devs
150 gst_init(NULL, NULL);
151 size_t numdevs = 0;
152 std::vector<data*> vidVector;
154 int fromrc = rcfile.getWebcamDevice();
156 if (fromrc == -1) {
157 std::cout << std::endl
158 << "Use this utility to set your desired default webcam device." << std::endl;
159 numdevs = findVidDevs(vidVector);
160 std::cout << std::endl
161 << "INFO: these devices were ignored because they are supported by both" << std::endl
162 << "video4linux and video4linux2:" << std::endl << std::endl;
163 for (size_t i = 0; i < numdevs; ++i) {
164 if (vidVector[i]->duplicate == true) {
165 std::cout << " " << vidVector[i]->deviceName
166 << " (" << vidVector[i]->deviceType << ")" << std::endl;
169 std::cout << std::endl
170 << "Gnash interacts with v4l2 sources better than v4l sources, thus the" << std::endl
171 << "v4l sources will not be printed in the list below." << std::endl
172 << std::endl
173 << "Found " << (numdevs - numDuplicates)
174 << " video devices: " << std::endl << std::endl;
175 gint counter = 0;
176 for (size_t i = 0; i < numdevs; ++i)
178 if (i == 0 && (vidVector[i] != 0)) {
179 std::cout << " " << i
180 << ". Video Test Source (videotestsrc)" << std::endl;
181 counter++;
182 } else if (i == 0 && (vidVector[i] == 0)) {
183 std::cout << "no test video device available";
184 } else {
185 if (vidVector[i]->duplicate != true) {
186 std::cout << " " << counter << ". "
187 << vidVector[i]->deviceName
188 << " (" << vidVector[i]->deviceType << ")" << std::endl;
189 counter++;
193 //prompt user for device selection
194 int dev_select = -1;
195 std::string fromCin;
196 do {
197 dev_select = -1;
198 std::cout << std::endl
199 << "Choose the device you would like to use (0-"
200 << (numdevs - numDuplicates - 1) << "): ";
201 std::cin >> fromCin;
202 if (fromCin.size() != 1) {
203 dev_select = -1;
204 } else if (fromCin[0] == '0') {
205 dev_select = 0;
206 } else {
207 dev_select = atoi(fromCin.c_str());
209 if ((dev_select < 0) || (dev_select > ((int)numdevs - numDuplicates - 1))) {
210 std::cout << "You must make a valid device selection" << std::endl;
212 } while ((dev_select < 0) || (dev_select > ((int)numdevs - numDuplicates - 1)));
213 std::cout << std::endl
214 << "To select this camera, add this line to your gnashrc file:" << std::endl
215 << "set webcamDevice "
216 << (vidVector[dev_select + numDuplicates]->deviceNumber) << std::endl;
217 } else {
218 numdevs = findVidDevs(vidVector);
219 if ((size_t)fromrc <= (vidVector.size() - 1)) {
220 std::cout << std::endl
221 << "The gnashrc file reports default webcam is set to:" << std::endl
222 << vidVector[fromrc]->deviceName
223 << " (" << vidVector[fromrc]->deviceType << ")" << std::endl
224 << "To change this setting, delete the 'set webcamDevice' line" << std::endl
225 << "from your gnashrc file and re-run this program." << std::endl << std::endl;
226 } else {
227 std::cout << std::endl
228 << "You have an invalid webcam chosen in your gnashrc file." << std::endl
229 << "Try reattaching the device or deleting the value from gnashrc" << std::endl
230 << "and running this program again" << std::endl;
233 return 0;