2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
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.
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.
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
20 #include "gnashconfig.h"
29 #include <gst/interfaces/propertyprobe.h>
32 //get rc file for webcam selection
33 gnash::RcInitFile
& rcfile
= gnash::RcInitFile::getDefaultInstance();
52 gint numDuplicates
= 0;
54 size_t findVidDevs(std::vector
<data
*>& vidVect
) {
59 element
= gst_element_factory_make ("videotestsrc", "vidtestsrc");
61 if (element
== NULL
) {
62 vidVect
.push_back(NULL
);
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;
73 GstPropertyProbe
*probe
;
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
) {
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
;
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
;
101 g_value_array_free (devarr
);
105 //video4linux2 source
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
) {
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
;
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;
143 g_value_array_free (devarr
);
149 //initialize gstreamer to probe for devs
150 gst_init(NULL
, NULL
);
152 std::vector
<data
*> vidVector
;
154 int fromrc
= rcfile
.getWebcamDevice();
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
173 << "Found " << (numdevs
- numDuplicates
)
174 << " video devices: " << std::endl
<< std::endl
;
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
;
182 } else if (i
== 0 && (vidVector
[i
] == 0)) {
183 std::cout
<< "no test video device available";
185 if (vidVector
[i
]->duplicate
!= true) {
186 std::cout
<< " " << counter
<< ". "
187 << vidVector
[i
]->deviceName
188 << " (" << vidVector
[i
]->deviceType
<< ")" << std::endl
;
193 //prompt user for device selection
198 std::cout
<< std::endl
199 << "Choose the device you would like to use (0-"
200 << (numdevs
- numDuplicates
- 1) << "): ";
202 if (fromCin
.size() != 1) {
204 } else if (fromCin
[0] == '0') {
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
;
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
;
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
;