VirtualBox

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

Last change on this file since 75392 was 73097, checked in by vboxsync, 6 years ago

*: Made RT_UOFFSETOF, RT_OFFSETOF, RT_UOFFSETOF_ADD and RT_OFFSETOF_ADD work like builtin_offsetof() and require compile time resolvable requests, adding RT_UOFFSETOF_DYN for the dynamic questions that can only be answered at runtime.

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

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