VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/UsbWebcamInterface.cpp@ 108641

Last change on this file since 108641 was 107576, checked in by vboxsync, 3 months ago

src/VBox/Main/src-client/UsbWebcamInterface.cpp: Fixed warnings found by Parfait (assignment unused). jiraref:VBP-1424

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.4 KB
Line 
1/* $Id: UsbWebcamInterface.cpp 107576 2025-01-09 09:47:37Z vboxsync $ */
2/** @file
3 * UsbWebcamInterface - Driver Interface for USB Webcam emulation.
4 */
5
6/*
7 * Copyright (C) 2011-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29#define LOG_GROUP LOG_GROUP_USB_WEBCAM
30#include "LoggingNew.h"
31
32#include "UsbWebcamInterface.h"
33#include "ConsoleImpl.h"
34#include "ConsoleVRDPServer.h"
35#include "EmulatedUSBImpl.h"
36
37#include <VBox/vmm/pdmwebcaminfs.h>
38#include <VBox/err.h>
39
40
41typedef struct EMWEBCAMREMOTE
42{
43 EmWebcam *pEmWebcam;
44
45 VRDEVIDEOINDEVICEHANDLE deviceHandle; /* The remote identifier. */
46
47 /* Received from the remote client. */
48 uint32_t u32Version; /* VRDE_VIDEOIN_NEGOTIATE_VERSION */
49 uint32_t fu32Capabilities; /* VRDE_VIDEOIN_NEGOTIATE_CAP_* */
50 VRDEVIDEOINDEVICEDESC *pDeviceDesc;
51 uint32_t cbDeviceDesc;
52
53 /* The device identifier for the PDM device.*/
54 uint64_t u64DeviceId;
55} EMWEBCAMREMOTE;
56
57typedef struct EMWEBCAMDRV
58{
59 EMWEBCAMREMOTE *pRemote;
60 PPDMIWEBCAMDEV pIWebcamUp;
61 PDMIWEBCAMDRV IWebcamDrv;
62} EMWEBCAMDRV, *PEMWEBCAMDRV;
63
64typedef struct EMWEBCAMREQCTX
65{
66 EMWEBCAMREMOTE *pRemote;
67 void *pvUser;
68} EMWEBCAMREQCTX;
69
70
71static DECLCALLBACK(void) drvEmWebcamReady(PPDMIWEBCAMDRV pInterface,
72 bool fReady)
73{
74 NOREF(fReady);
75
76 PEMWEBCAMDRV pThis = RT_FROM_MEMBER(pInterface, EMWEBCAMDRV, IWebcamDrv);
77 EMWEBCAMREMOTE *pRemote = pThis->pRemote;
78
79 LogFlowFunc(("pRemote:%p\n", pThis->pRemote));
80
81 if (pThis->pIWebcamUp)
82 {
83 pThis->pIWebcamUp->pfnAttached(pThis->pIWebcamUp,
84 pRemote->u64DeviceId,
85 pRemote->pDeviceDesc,
86 pRemote->cbDeviceDesc,
87 pRemote->u32Version,
88 pRemote->fu32Capabilities);
89 }
90}
91
92static DECLCALLBACK(int) drvEmWebcamControl(PPDMIWEBCAMDRV pInterface,
93 void *pvUser,
94 uint64_t u64DeviceId,
95 const struct VRDEVIDEOINCTRLHDR *pCtrl,
96 uint32_t cbCtrl)
97{
98 PEMWEBCAMDRV pThis = RT_FROM_MEMBER(pInterface, EMWEBCAMDRV, IWebcamDrv);
99 EMWEBCAMREMOTE *pRemote = pThis->pRemote;
100
101 LogFlowFunc(("pRemote:%p, u64DeviceId %lld\n", pRemote, u64DeviceId));
102
103 return pRemote->pEmWebcam->SendControl(pThis, pvUser, u64DeviceId, pCtrl, cbCtrl);
104}
105
106
107EmWebcam::EmWebcam(ConsoleVRDPServer *pServer)
108 :
109 mParent(pServer),
110 mpDrv(NULL),
111 mpRemote(NULL),
112 mu64DeviceIdSrc(0)
113{
114}
115
116EmWebcam::~EmWebcam()
117{
118 if (mpDrv)
119 {
120 mpDrv->pRemote = NULL;
121 mpDrv = NULL;
122 }
123}
124
125void EmWebcam::EmWebcamConstruct(EMWEBCAMDRV *pDrv)
126{
127 AssertReturnVoid(mpDrv == NULL);
128
129 mpDrv = pDrv;
130}
131
132void EmWebcam::EmWebcamDestruct(EMWEBCAMDRV *pDrv)
133{
134 AssertReturnVoid(pDrv == mpDrv);
135
136 if (mpRemote)
137 {
138 mParent->VideoInDeviceDetach(&mpRemote->deviceHandle);
139
140 RTMemFree(mpRemote->pDeviceDesc);
141 mpRemote->pDeviceDesc = NULL;
142 mpRemote->cbDeviceDesc = 0;
143
144 RTMemFree(mpRemote);
145 mpRemote = NULL;
146 }
147
148 mpDrv->pRemote = NULL;
149 mpDrv = NULL;
150}
151
152void EmWebcam::EmWebcamCbNotify(uint32_t u32Id, const void *pvData, uint32_t cbData)
153{
154 switch (u32Id)
155 {
156 case VRDE_VIDEOIN_NOTIFY_ID_ATTACH:
157 {
158 VRDEVIDEOINNOTIFYATTACH *p = (VRDEVIDEOINNOTIFYATTACH *)pvData;
159
160 /* Older versions did not report u32Version and fu32Capabilities. */
161 uint32_t u32Version = 1;
162 uint32_t fu32Capabilities = VRDE_VIDEOIN_NEGOTIATE_CAP_VOID;
163
164 if (cbData >= RT_UOFFSETOF(VRDEVIDEOINNOTIFYATTACH, u32Version) + sizeof(p->u32Version))
165 u32Version = p->u32Version;
166
167 if (cbData >= RT_UOFFSETOF(VRDEVIDEOINNOTIFYATTACH, fu32Capabilities) + sizeof(p->fu32Capabilities))
168 fu32Capabilities = p->fu32Capabilities;
169
170 LogFlowFunc(("ATTACH[%d,%d] version %d, caps 0x%08X\n",
171 p->deviceHandle.u32ClientId, p->deviceHandle.u32DeviceId,
172 u32Version, fu32Capabilities));
173
174 /* Currently only one device is allowed. */
175 if (mpRemote)
176 {
177 AssertFailed();
178 break;
179 }
180
181 EMWEBCAMREMOTE *pRemote = (EMWEBCAMREMOTE *)RTMemAllocZ(sizeof(EMWEBCAMREMOTE));
182 if (pRemote == NULL)
183 break;
184
185 pRemote->pEmWebcam = this;
186 pRemote->deviceHandle = p->deviceHandle;
187 pRemote->u32Version = u32Version;
188 pRemote->fu32Capabilities = fu32Capabilities;
189 pRemote->pDeviceDesc = NULL;
190 pRemote->cbDeviceDesc = 0;
191 pRemote->u64DeviceId = ASMAtomicIncU64(&mu64DeviceIdSrc);
192
193 mpRemote = pRemote;
194
195 /* Tell the server that this webcam will be used. */
196 int vrc = mParent->VideoInDeviceAttach(&mpRemote->deviceHandle, mpRemote);
197 if (RT_FAILURE(vrc))
198 {
199 RTMemFree(mpRemote);
200 mpRemote = NULL;
201 break;
202 }
203
204 /* Get the device description. */
205 vrc = mParent->VideoInGetDeviceDesc(NULL, &mpRemote->deviceHandle);
206 if (RT_FAILURE(vrc))
207 {
208 mParent->VideoInDeviceDetach(&mpRemote->deviceHandle);
209 RTMemFree(mpRemote);
210 mpRemote = NULL;
211 break;
212 }
213
214 LogFlowFunc(("sent DeviceDesc\n"));
215 break;
216 }
217
218 case VRDE_VIDEOIN_NOTIFY_ID_DETACH:
219 {
220 VRDEVIDEOINNOTIFYDETACH *p = (VRDEVIDEOINNOTIFYDETACH *)pvData; NOREF(p);
221 Assert(cbData == sizeof(VRDEVIDEOINNOTIFYDETACH));
222
223 LogFlowFunc(("DETACH[%d,%d]\n", p->deviceHandle.u32ClientId, p->deviceHandle.u32DeviceId));
224
225 /** @todo */
226 if (mpRemote)
227 {
228 if (mpDrv && mpDrv->pIWebcamUp)
229 mpDrv->pIWebcamUp->pfnDetached(mpDrv->pIWebcamUp, mpRemote->u64DeviceId);
230 /* mpRemote is deallocated in EmWebcamDestruct */
231 }
232
233 break;
234 }
235
236 default:
237 AssertFailed();
238 break;
239 }
240
241 return;
242}
243
244void EmWebcam::EmWebcamCbDeviceDesc(int rcRequest, void *pDeviceCtx, void *pvUser,
245 const VRDEVIDEOINDEVICEDESC *pDeviceDesc, uint32_t cbDeviceDesc)
246{
247 RT_NOREF(pvUser);
248 EMWEBCAMREMOTE *pRemote = (EMWEBCAMREMOTE *)pDeviceCtx;
249 Assert(pRemote == mpRemote);
250
251 LogFlowFunc(("mpDrv %p, rcRequest %Rrc %p %p %p %d\n",
252 mpDrv, rcRequest, pDeviceCtx, pvUser, pDeviceDesc, cbDeviceDesc));
253
254 if (RT_SUCCESS(rcRequest))
255 {
256 /* Save device description. */
257 Assert(pRemote->pDeviceDesc == NULL);
258 pRemote->pDeviceDesc = (VRDEVIDEOINDEVICEDESC *)RTMemDup(pDeviceDesc, cbDeviceDesc);
259 pRemote->cbDeviceDesc = cbDeviceDesc;
260
261 /* Try to attach the device. */
262 EmulatedUSB *pEUSB = mParent->getConsole()->i_getEmulatedUSB();
263 pEUSB->i_webcamAttachInternal("", "", "EmWebcam", pRemote);
264 }
265 else
266 {
267 mParent->VideoInDeviceDetach(&mpRemote->deviceHandle);
268 RTMemFree(mpRemote);
269 mpRemote = NULL;
270 }
271}
272
273void EmWebcam::EmWebcamCbControl(int rcRequest, void *pDeviceCtx, void *pvUser,
274 const VRDEVIDEOINCTRLHDR *pControl, uint32_t cbControl)
275{
276 RT_NOREF(rcRequest);
277 EMWEBCAMREMOTE *pRemote = (EMWEBCAMREMOTE *)pDeviceCtx; NOREF(pRemote);
278 Assert(pRemote == mpRemote);
279
280 LogFlowFunc(("rcRequest %Rrc %p %p %p %d\n",
281 rcRequest, pDeviceCtx, pvUser, pControl, cbControl));
282
283 bool fResponse = (pvUser != NULL);
284
285 if (mpDrv && mpDrv->pIWebcamUp)
286 {
287 mpDrv->pIWebcamUp->pfnControl(mpDrv->pIWebcamUp,
288 fResponse,
289 pvUser,
290 mpRemote->u64DeviceId,
291 pControl,
292 cbControl);
293 }
294
295 RTMemFree(pvUser);
296}
297
298void EmWebcam::EmWebcamCbFrame(int rcRequest, void *pDeviceCtx,
299 const VRDEVIDEOINPAYLOADHDR *pFrame, uint32_t cbFrame)
300{
301 RT_NOREF(rcRequest, pDeviceCtx);
302 LogFlowFunc(("rcRequest %Rrc %p %p %d\n",
303 rcRequest, pDeviceCtx, pFrame, cbFrame));
304
305 if (mpDrv && mpDrv->pIWebcamUp)
306 {
307 if ( cbFrame >= sizeof(VRDEVIDEOINPAYLOADHDR)
308 && cbFrame >= pFrame->u8HeaderLength)
309 {
310 uint32_t cbImage = cbFrame - pFrame->u8HeaderLength;
311 const uint8_t *pu8Image = cbImage > 0? (const uint8_t *)pFrame + pFrame->u8HeaderLength: NULL;
312
313 mpDrv->pIWebcamUp->pfnFrame(mpDrv->pIWebcamUp,
314 mpRemote->u64DeviceId,
315 pFrame,
316 pFrame->u8HeaderLength,
317 pu8Image,
318 cbImage);
319 }
320 }
321}
322
323int EmWebcam::SendControl(EMWEBCAMDRV *pDrv, void *pvUser, uint64_t u64DeviceId,
324 const VRDEVIDEOINCTRLHDR *pControl, uint32_t cbControl)
325{
326 AssertReturn(pDrv == mpDrv, VERR_NOT_SUPPORTED);
327
328 int vrc = VINF_SUCCESS;
329
330 EMWEBCAMREQCTX *pCtx = NULL;
331
332 /* Verify that there is a remote device. */
333 if ( !mpRemote
334 || mpRemote->u64DeviceId != u64DeviceId)
335 {
336 vrc = VERR_NOT_SUPPORTED;
337 }
338
339 if (RT_SUCCESS(vrc))
340 {
341 pCtx = (EMWEBCAMREQCTX *)RTMemAlloc(sizeof(EMWEBCAMREQCTX));
342 if (!pCtx)
343 {
344 vrc = VERR_NO_MEMORY;
345 }
346 }
347
348 if (RT_SUCCESS(vrc))
349 {
350 pCtx->pRemote = mpRemote;
351 pCtx->pvUser = pvUser;
352
353 vrc = mParent->VideoInControl(pCtx, &mpRemote->deviceHandle, pControl, cbControl);
354
355 if (RT_FAILURE(vrc))
356 {
357 RTMemFree(pCtx);
358 }
359 }
360
361 return vrc;
362}
363
364/* static */ DECLCALLBACK(void *) EmWebcam::drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
365{
366 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
367 PEMWEBCAMDRV pThis = PDMINS_2_DATA(pDrvIns, PEMWEBCAMDRV);
368
369 LogFlowFunc(("pszIID:%s\n", pszIID));
370
371 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
372 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIWEBCAMDRV, &pThis->IWebcamDrv);
373 return NULL;
374}
375
376/* static */ DECLCALLBACK(void) EmWebcam::drvDestruct(PPDMDRVINS pDrvIns)
377{
378 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
379 PEMWEBCAMDRV pThis = PDMINS_2_DATA(pDrvIns, PEMWEBCAMDRV);
380 EMWEBCAMREMOTE *pRemote = pThis->pRemote;
381
382 LogFlowFunc(("iInstance %d, pRemote %p, pIWebcamUp %p\n",
383 pDrvIns->iInstance, pRemote, pThis->pIWebcamUp));
384
385 if (pRemote && pRemote->pEmWebcam)
386 {
387 pRemote->pEmWebcam->EmWebcamDestruct(pThis);
388 }
389}
390
391/* static */ DECLCALLBACK(int) EmWebcam::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
392{
393 RT_NOREF(fFlags);
394 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
395 LogFlowFunc(("iInstance:%d, pCfg:%p, fFlags:%x\n", pDrvIns->iInstance, pCfg, fFlags));
396
397 PEMWEBCAMDRV pThis = PDMINS_2_DATA(pDrvIns, PEMWEBCAMDRV);
398
399 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
400 ("Configuration error: Not possible to attach anything to this driver!\n"),
401 VERR_PDM_DRVINS_NO_ATTACH);
402
403 /* Check early that there is a device. No need to init anything if there is no device. */
404 pThis->pIWebcamUp = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIWEBCAMDEV);
405 if (pThis->pIWebcamUp == NULL)
406 {
407 LogRel(("USBWEBCAM: Emulated webcam device does not exist.\n"));
408 return VERR_PDM_MISSING_INTERFACE;
409 }
410
411 char *pszId = NULL;
412 int vrc = pDrvIns->pHlpR3->pfnCFGMQueryStringAlloc(pCfg, "Id", &pszId);
413 if (RT_SUCCESS(vrc))
414 {
415 RTUUID UuidEmulatedUsbIf;
416 vrc = RTUuidFromStr(&UuidEmulatedUsbIf, EMULATEDUSBIF_OID); AssertRC(vrc);
417
418 PEMULATEDUSBIF pEmulatedUsbIf = (PEMULATEDUSBIF)PDMDrvHlpQueryGenericUserObject(pDrvIns, &UuidEmulatedUsbIf);
419 AssertPtrReturn(pEmulatedUsbIf, VERR_INVALID_PARAMETER);
420
421 vrc = pEmulatedUsbIf->pfnQueryEmulatedUsbDataById(pEmulatedUsbIf->pvUser, pszId,
422 NULL /*ppvEmUsbCb*/, NULL /*ppvEmUsbCbData*/, (void **)&pThis->pRemote);
423 pDrvIns->pHlpR3->pfnMMHeapFree(pDrvIns, pszId);
424 AssertRCReturn(vrc, vrc);
425 }
426 else
427 return vrc;
428
429 /* Everything ok. Initialize. */
430 pThis->pRemote->pEmWebcam->EmWebcamConstruct(pThis);
431
432 pDrvIns->IBase.pfnQueryInterface = drvQueryInterface;
433
434 pThis->IWebcamDrv.pfnReady = drvEmWebcamReady;
435 pThis->IWebcamDrv.pfnControl = drvEmWebcamControl;
436
437 return VINF_SUCCESS;
438}
439
440/* static */ const PDMDRVREG EmWebcam::DrvReg =
441{
442 /* u32Version */
443 PDM_DRVREG_VERSION,
444 /* szName[32] */
445 "EmWebcam",
446 /* szRCMod[32] */
447 "",
448 /* szR0Mod[32] */
449 "",
450 /* pszDescription */
451 "Main Driver communicating with VRDE",
452 /* fFlags */
453 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
454 /* fClass */
455 PDM_DRVREG_CLASS_USB,
456 /* cMaxInstances */
457 1,
458 /* cbInstance */
459 sizeof(EMWEBCAMDRV),
460 /* pfnConstruct */
461 EmWebcam::drvConstruct,
462 /* pfnDestruct */
463 EmWebcam::drvDestruct,
464 /* pfnRelocate */
465 NULL,
466 /* pfnIOCtl */
467 NULL,
468 /* pfnPowerOn */
469 NULL,
470 /* pfnReset */
471 NULL,
472 /* pfnSuspend */
473 NULL,
474 /* pfnResume */
475 NULL,
476 /* pfnAttach */
477 NULL,
478 /* pfnDetach */
479 NULL,
480 /* pfnPowerOff */
481 NULL,
482 /* pfnSoftReset */
483 NULL,
484 /* u32VersionEnd */
485 PDM_DRVREG_VERSION
486};
487/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette