1 | /* $Id: HostVideoInputDeviceImpl.cpp 48607 2013-09-20 15:47:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * Host video capture device implementation.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2013 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include "HostVideoInputDeviceImpl.h"
|
---|
20 | #include "Logging.h"
|
---|
21 |
|
---|
22 | #ifdef RT_OS_WINDOWS
|
---|
23 | #include <dshow.h>
|
---|
24 | #endif
|
---|
25 |
|
---|
26 | /*
|
---|
27 | * HostVideoInputDevice implementation.
|
---|
28 | */
|
---|
29 | DEFINE_EMPTY_CTOR_DTOR(HostVideoInputDevice)
|
---|
30 |
|
---|
31 | HRESULT HostVideoInputDevice::FinalConstruct()
|
---|
32 | {
|
---|
33 | return BaseFinalConstruct();
|
---|
34 | }
|
---|
35 |
|
---|
36 | void HostVideoInputDevice::FinalRelease()
|
---|
37 | {
|
---|
38 | uninit();
|
---|
39 |
|
---|
40 | BaseFinalRelease();
|
---|
41 | }
|
---|
42 |
|
---|
43 | /*
|
---|
44 | * Initializes the instance.
|
---|
45 | */
|
---|
46 | HRESULT HostVideoInputDevice::init(const com::Utf8Str &name, const com::Utf8Str &path, const com::Utf8Str &alias)
|
---|
47 | {
|
---|
48 | LogFlowThisFunc(("\n"));
|
---|
49 |
|
---|
50 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
51 | AutoInitSpan autoInitSpan(this);
|
---|
52 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
53 |
|
---|
54 | m.name = name;
|
---|
55 | m.path = path;
|
---|
56 | m.alias = alias;
|
---|
57 |
|
---|
58 | /* Confirm a successful initialization */
|
---|
59 | autoInitSpan.setSucceeded();
|
---|
60 |
|
---|
61 | return S_OK;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /*
|
---|
65 | * Uninitializes the instance.
|
---|
66 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
67 | */
|
---|
68 | void HostVideoInputDevice::uninit()
|
---|
69 | {
|
---|
70 | LogFlowThisFunc(("\n"));
|
---|
71 |
|
---|
72 | m.name.setNull();
|
---|
73 | m.path.setNull();
|
---|
74 | m.alias.setNull();
|
---|
75 |
|
---|
76 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
77 | AutoUninitSpan autoUninitSpan(this);
|
---|
78 | if (autoUninitSpan.uninitDone())
|
---|
79 | return;
|
---|
80 | }
|
---|
81 |
|
---|
82 | static HRESULT hostVideoInputDeviceAdd(HostVideoInputDeviceList *pList,
|
---|
83 | const com::Utf8Str &name,
|
---|
84 | const com::Utf8Str &path,
|
---|
85 | const com::Utf8Str &alias)
|
---|
86 | {
|
---|
87 | ComObjPtr<HostVideoInputDevice> obj;
|
---|
88 | HRESULT hr = obj.createObject();
|
---|
89 | if (SUCCEEDED(hr))
|
---|
90 | {
|
---|
91 | hr = obj->init(name, path, alias);
|
---|
92 | if (SUCCEEDED(hr))
|
---|
93 | pList->push_back(obj);
|
---|
94 | }
|
---|
95 | return hr;
|
---|
96 | }
|
---|
97 |
|
---|
98 | #ifdef RT_OS_WINDOWS
|
---|
99 | static HRESULT hvcdCreateEnumerator(IEnumMoniker **ppEnumMoniker)
|
---|
100 | {
|
---|
101 | ICreateDevEnum *pCreateDevEnum = NULL;
|
---|
102 | HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
|
---|
103 | IID_PPV_ARGS(&pCreateDevEnum));
|
---|
104 | if (SUCCEEDED(hr))
|
---|
105 | {
|
---|
106 | hr = pCreateDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, ppEnumMoniker, 0);
|
---|
107 | pCreateDevEnum->Release();
|
---|
108 | }
|
---|
109 | return hr;
|
---|
110 | }
|
---|
111 |
|
---|
112 | static HRESULT hvcdFillList(HostVideoInputDeviceList *pList, IEnumMoniker *pEnumMoniker)
|
---|
113 | {
|
---|
114 | int iDevice = 0;
|
---|
115 | IMoniker *pMoniker = NULL;
|
---|
116 | while (pEnumMoniker->Next(1, &pMoniker, NULL) == S_OK)
|
---|
117 | {
|
---|
118 | IPropertyBag *pPropBag = NULL;
|
---|
119 | HRESULT hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag));
|
---|
120 | if (FAILED(hr))
|
---|
121 | {
|
---|
122 | pMoniker->Release();
|
---|
123 | continue;
|
---|
124 | }
|
---|
125 |
|
---|
126 | VARIANT var;
|
---|
127 | VariantInit(&var);
|
---|
128 |
|
---|
129 | hr = pPropBag->Read(L"DevicePath", &var, 0);
|
---|
130 | if (FAILED(hr))
|
---|
131 | {
|
---|
132 | /* Can't use a device without path. */
|
---|
133 | pMoniker->Release();
|
---|
134 | continue;
|
---|
135 | }
|
---|
136 |
|
---|
137 | ++iDevice;
|
---|
138 |
|
---|
139 | com::Utf8Str path = var.bstrVal;
|
---|
140 | VariantClear(&var);
|
---|
141 |
|
---|
142 | hr = pPropBag->Read(L"FriendlyName", &var, 0);
|
---|
143 | if (FAILED(hr))
|
---|
144 | {
|
---|
145 | hr = pPropBag->Read(L"Description", &var, 0);
|
---|
146 | }
|
---|
147 |
|
---|
148 | com::Utf8Str name;
|
---|
149 | if (SUCCEEDED(hr))
|
---|
150 | {
|
---|
151 | name = var.bstrVal;
|
---|
152 | VariantClear(&var);
|
---|
153 | }
|
---|
154 | else
|
---|
155 | {
|
---|
156 | name = com::Utf8StrFmt("Video Input Device #%d", iDevice);
|
---|
157 | }
|
---|
158 |
|
---|
159 | com::Utf8Str alias = com::Utf8StrFmt(".%d", iDevice);
|
---|
160 |
|
---|
161 | hr = hostVideoInputDeviceAdd(pList, name, path, alias);
|
---|
162 |
|
---|
163 | pPropBag->Release();
|
---|
164 | pMoniker->Release();
|
---|
165 |
|
---|
166 | if (FAILED(hr))
|
---|
167 | return hr;
|
---|
168 | }
|
---|
169 |
|
---|
170 | return S_OK;
|
---|
171 | }
|
---|
172 |
|
---|
173 | static HRESULT fillDeviceList(HostVideoInputDeviceList *pList)
|
---|
174 | {
|
---|
175 | IEnumMoniker *pEnumMoniker = NULL;
|
---|
176 | HRESULT hr = hvcdCreateEnumerator(&pEnumMoniker);
|
---|
177 | if (SUCCEEDED(hr))
|
---|
178 | {
|
---|
179 | if (hr != S_FALSE)
|
---|
180 | {
|
---|
181 | /* List not empty */
|
---|
182 | hr = hvcdFillList(pList, pEnumMoniker);
|
---|
183 | pEnumMoniker->Release();
|
---|
184 | }
|
---|
185 | else
|
---|
186 | {
|
---|
187 | hr = S_OK; /* Return empty list. */
|
---|
188 | }
|
---|
189 | }
|
---|
190 | return hr;
|
---|
191 | }
|
---|
192 | #else
|
---|
193 | static HRESULT fillDeviceList(HostVideoInputDeviceList *pList)
|
---|
194 | {
|
---|
195 | NOREF(pList);
|
---|
196 | return E_NOTIMPL;
|
---|
197 | }
|
---|
198 | #endif /* RT_OS_WINDOWS */
|
---|
199 |
|
---|
200 | /* static */ HRESULT HostVideoInputDevice::queryHostDevices(HostVideoInputDeviceList *pList)
|
---|
201 | {
|
---|
202 | HRESULT hr = fillDeviceList(pList);
|
---|
203 |
|
---|
204 | if (FAILED(hr))
|
---|
205 | {
|
---|
206 | pList->clear();
|
---|
207 | }
|
---|
208 |
|
---|
209 | return hr;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|