Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

598 lines
15 KiB

  1. /*****************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORPORATION, 1999-2000
  4. *
  5. * TITLE: CWiaVideo.cpp
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: OrenR
  10. *
  11. * DATE: 2000/10/25
  12. *
  13. * DESCRIPTION: COM wrapper for CPreviewGraph class
  14. *
  15. *****************************************************************************/
  16. #include <precomp.h>
  17. #pragma hdrstop
  18. ///////////////////////////////
  19. // CWiaVideo Constructor
  20. //
  21. CWiaVideo::CWiaVideo() :
  22. m_bInited(FALSE)
  23. {
  24. DBG_FN("CWiaVideo::CWiaVideo");
  25. HRESULT hr = S_OK;
  26. hr = CAccessLock::Init(&m_csLock);
  27. if (hr == S_OK)
  28. {
  29. m_bInited = TRUE;
  30. }
  31. hr = m_PreviewGraph.Init(this);
  32. CHECK_S_OK2(hr, ("CWiaVideo::CWiaVideo, error trying to initialize "
  33. "preview Graph, this should never happen"));
  34. ASSERT(hr == S_OK);
  35. }
  36. ///////////////////////////////
  37. // CWiaVideo Destructor
  38. //
  39. CWiaVideo::~CWiaVideo()
  40. {
  41. DBG_FN("CWiaVideo::~CWiaVideo");
  42. m_PreviewGraph.Term();
  43. if (m_bInited)
  44. {
  45. CAccessLock::Term(&m_csLock);
  46. }
  47. }
  48. ///////////////////////////////
  49. // get_PreviewVisible
  50. //
  51. STDMETHODIMP CWiaVideo::get_PreviewVisible(BOOL *pbPreviewVisible)
  52. {
  53. DBG_FN("CWiaVideo::get_PreviewVisible");
  54. ASSERT(pbPreviewVisible != NULL);
  55. HRESULT hr = S_OK;
  56. if (pbPreviewVisible == NULL)
  57. {
  58. hr = E_POINTER;
  59. CHECK_S_OK2(hr, ("CWiaVideo::get_PreviewVisible received NULL "
  60. "parameter"));
  61. }
  62. if (hr == S_OK)
  63. {
  64. *pbPreviewVisible = m_PreviewGraph.IsPreviewVisible();
  65. }
  66. return hr;
  67. }
  68. ///////////////////////////////
  69. // put_PreviewVisible
  70. //
  71. STDMETHODIMP CWiaVideo::put_PreviewVisible(BOOL bPreviewVisible)
  72. {
  73. DBG_FN("CWiaVideo::put_PreviewVisible");
  74. HRESULT hr = S_OK;
  75. CAccessLock Lock(&m_csLock);
  76. if (hr == S_OK)
  77. {
  78. hr = m_PreviewGraph.ShowVideo(bPreviewVisible);
  79. }
  80. return hr;
  81. }
  82. ///////////////////////////////
  83. // get_ImagesDirectory
  84. //
  85. STDMETHODIMP CWiaVideo::get_ImagesDirectory(BSTR *pbstrImageDirectory)
  86. {
  87. DBG_FN("CWiaVideo::get_ImagesDirectory");
  88. ASSERT(pbstrImageDirectory != NULL);
  89. HRESULT hr = S_OK;
  90. CSimpleString strImagesDir;
  91. if (pbstrImageDirectory == NULL)
  92. {
  93. hr = E_POINTER;
  94. CHECK_S_OK2(hr, ("CWiaVideo::get_ImagesDirectory, received a NULL "
  95. "param"));
  96. }
  97. if (hr == S_OK)
  98. {
  99. hr = m_PreviewGraph.GetImagesDirectory(&strImagesDir);
  100. CHECK_S_OK2(hr, ("CWiaVideo::get_ImagesDirectory, failed to get "
  101. "images directory"));
  102. }
  103. if (hr == S_OK)
  104. {
  105. *pbstrImageDirectory =
  106. SysAllocString(CSimpleStringConvert::WideString(strImagesDir));
  107. }
  108. return hr;
  109. }
  110. ///////////////////////////////
  111. // put_ImagesDirectory
  112. //
  113. STDMETHODIMP CWiaVideo::put_ImagesDirectory(BSTR bstrImageDirectory)
  114. {
  115. DBG_FN("CWiaVideo::put_ImagesDirectory");
  116. ASSERT(bstrImageDirectory != NULL);
  117. HRESULT hr = S_OK;
  118. CSimpleStringWide strImagesDir;
  119. if (bstrImageDirectory == NULL)
  120. {
  121. hr = E_POINTER;
  122. CHECK_S_OK2(hr, ("CWiaVideo::put_ImagesDirectory received a "
  123. "NULL param"));
  124. }
  125. CAccessLock Lock(&m_csLock);
  126. if (hr == S_OK)
  127. {
  128. strImagesDir = bstrImageDirectory;
  129. hr = m_PreviewGraph.SetImagesDirectory(
  130. &(CSimpleStringConvert::NaturalString(strImagesDir)));
  131. CHECK_S_OK2(hr, ("CWiaVideo::put_ImagesDirectory, failed to set "
  132. "images directory"));
  133. }
  134. return hr;
  135. }
  136. ///////////////////////////////
  137. // CreateVideoByWiaDevID
  138. //
  139. STDMETHODIMP CWiaVideo::CreateVideoByWiaDevID(BSTR bstrWiaID,
  140. HWND hwndParent,
  141. BOOL bStretchToFitParent,
  142. BOOL bAutoBeginPlayback)
  143. {
  144. DBG_FN("CWiaVideo::CreateVideoByWiaDevID");
  145. ASSERT(bstrWiaID != NULL);
  146. HRESULT hr = S_OK;
  147. CComPtr<IMoniker> pCaptureDeviceMoniker;
  148. CSimpleString strWiaID;
  149. CAccessLock Lock(&m_csLock);
  150. if (bstrWiaID == NULL)
  151. {
  152. hr = E_POINTER;
  153. CHECK_S_OK2(hr, ("CWiaVideo::CreateVideoByWiaDevID received NULL "
  154. "parameter"));
  155. return hr;
  156. }
  157. else if (m_PreviewGraph.GetState() != WIAVIDEO_NO_VIDEO)
  158. {
  159. hr = E_FAIL;
  160. CHECK_S_OK2(hr, ("CWiaVideo::CreateVideoByWiaDevID attempting "
  161. "to create video when previous video hasn't "
  162. "been destroyed yet"));
  163. return hr;
  164. }
  165. //
  166. // Initialize our WiaLink. This enables use to respond to TAKE_PICTURE
  167. // commands sent to the WiaDriver.
  168. //
  169. if (hr == S_OK)
  170. {
  171. strWiaID = CSimpleStringConvert::NaturalString(
  172. CSimpleStringWide(bstrWiaID));
  173. hr = m_WiaLink.Init(&strWiaID, this);
  174. CHECK_S_OK2(hr, ("CWiaVideo::CreateVideoByWiaDevID failed to link to "
  175. "WIA to respond to the video driver TAKE_PICTURE "
  176. "command "));
  177. }
  178. //
  179. // Get the Directshow Capture Filter Moniker associated with this
  180. // WIA Imaging device.
  181. //
  182. if (hr == S_OK)
  183. {
  184. hr = CDShowUtil::FindDeviceByWiaID(&m_WiaLink,
  185. &strWiaID,
  186. NULL,
  187. NULL,
  188. NULL,
  189. &pCaptureDeviceMoniker);
  190. CHECK_S_OK2(hr, ("CWiaVideo::CreateVideoByWiaDevID, failed to find "
  191. "the DShow device specified by Wia ID '%ls'",
  192. strWiaID.String()));
  193. }
  194. //
  195. // Create the Video Preview
  196. //
  197. if (hr == S_OK)
  198. {
  199. hr = m_PreviewGraph.CreateVideo(strWiaID,
  200. pCaptureDeviceMoniker,
  201. hwndParent,
  202. bStretchToFitParent,
  203. bAutoBeginPlayback);
  204. CHECK_S_OK2(hr, ("CWiaVideo::CreateVideoByWiaDevID, failed to "
  205. "CreateVideo"));
  206. }
  207. if (hr == S_OK)
  208. {
  209. hr = m_WiaLink.StartMonitoring();
  210. CHECK_S_OK2(hr, ("CWiaVideo::CreateVideoByWiaID, failed to "
  211. "start monitoring WIA TAKE_PICTURE requests"));
  212. }
  213. if (hr != S_OK)
  214. {
  215. DestroyVideo();
  216. }
  217. return hr;
  218. }
  219. ///////////////////////////////
  220. // CreateVideoByDevNum
  221. //
  222. STDMETHODIMP CWiaVideo::CreateVideoByDevNum(UINT uiDeviceNumber,
  223. HWND hwndParent,
  224. BOOL bStretchToFitParent,
  225. BOOL bAutoBeginPlayback)
  226. {
  227. DBG_FN("CWiaVideo::CreateVideoByDevNum");
  228. HRESULT hr = S_OK;
  229. CComPtr<IMoniker> pCaptureDeviceMoniker;
  230. CSimpleString strDShowDeviceID;
  231. //
  232. // Since we are creating video via the DShow enumeration position,
  233. // we will NOT establish a WIA link.
  234. //
  235. //
  236. // Find the Directshow Capture Filter moniker associated with this
  237. // enumeration position.
  238. //
  239. CAccessLock Lock(&m_csLock);
  240. if (m_PreviewGraph.GetState() != WIAVIDEO_NO_VIDEO)
  241. {
  242. hr = E_FAIL;
  243. CHECK_S_OK2(hr, ("CWiaVideo::CreateVideoByWiaDevNum attempting "
  244. "to create video when previous video hasn't "
  245. "been destroyed yet"));
  246. return hr;
  247. }
  248. if (hr == S_OK)
  249. {
  250. hr = CDShowUtil::FindDeviceByEnumPos(uiDeviceNumber,
  251. &strDShowDeviceID,
  252. NULL,
  253. &pCaptureDeviceMoniker);
  254. CHECK_S_OK2(hr, ("CWiaVideo::CreateVideoByDevNum, failed to find "
  255. "DShow device # '%d'", uiDeviceNumber));
  256. }
  257. //
  258. // Create the Video
  259. //
  260. if (hr == S_OK)
  261. {
  262. hr = m_PreviewGraph.CreateVideo(NULL,
  263. pCaptureDeviceMoniker,
  264. hwndParent,
  265. bStretchToFitParent,
  266. bAutoBeginPlayback);
  267. CHECK_S_OK2(hr, ("CWiaVideo::CreateVideoByDevNum, failed to Create "
  268. "Video for DShow device # '%d'", uiDeviceNumber));
  269. }
  270. if (hr != S_OK)
  271. {
  272. DestroyVideo();
  273. }
  274. return hr;
  275. }
  276. ///////////////////////////////
  277. // CreateVideoByName
  278. //
  279. STDMETHODIMP CWiaVideo::CreateVideoByName(BSTR bstrFriendlyName,
  280. HWND hwndParent,
  281. BOOL bStretchToFitParent,
  282. BOOL bAutoBeginPlayback)
  283. {
  284. DBG_FN("CWiaVideo::CreateVideoByName");
  285. ASSERT(bstrFriendlyName != NULL);
  286. HRESULT hr = S_OK;
  287. CComPtr<IMoniker> pCaptureDeviceMoniker;
  288. CSimpleString strFriendlyName;
  289. CSimpleString strDShowDeviceID;
  290. CAccessLock Lock(&m_csLock);
  291. if (bstrFriendlyName == NULL)
  292. {
  293. hr = E_POINTER;
  294. CHECK_S_OK2(hr, ("CWiaVideo::CreateVideoByName received NULL "
  295. "parameter"));
  296. }
  297. else if (m_PreviewGraph.GetState() != WIAVIDEO_NO_VIDEO)
  298. {
  299. hr = E_FAIL;
  300. CHECK_S_OK2(hr, ("CWiaVideo::CreateVideoByName attempting "
  301. "to create video when previous video hasn't "
  302. "been destroyed yet"));
  303. return hr;
  304. }
  305. if (hr == S_OK)
  306. {
  307. strFriendlyName = CSimpleStringConvert::NaturalString(
  308. CSimpleStringWide(bstrFriendlyName));
  309. hr = CDShowUtil::FindDeviceByFriendlyName(&strFriendlyName,
  310. NULL,
  311. &strDShowDeviceID,
  312. &pCaptureDeviceMoniker);
  313. CHECK_S_OK2(hr, ("CWiaVideo::CreateVideoByName failed to find DShow "
  314. "device identified by friendly name '%ls'",
  315. strFriendlyName.String()));
  316. }
  317. if (hr == S_OK)
  318. {
  319. hr = m_PreviewGraph.CreateVideo(NULL,
  320. pCaptureDeviceMoniker,
  321. hwndParent,
  322. bStretchToFitParent,
  323. bAutoBeginPlayback);
  324. CHECK_S_OK2(hr, ("CWiaVideo::CreateVideoByName failed to create "
  325. "video for DShow device identified by friendly "
  326. "name '%ls'", strFriendlyName.String()));
  327. }
  328. if (hr != S_OK)
  329. {
  330. DestroyVideo();
  331. }
  332. return hr;
  333. }
  334. ///////////////////////////////
  335. // DestroyVideo
  336. //
  337. STDMETHODIMP CWiaVideo::DestroyVideo()
  338. {
  339. DBG_FN("CWiaVideo::DestroyVideo");
  340. HRESULT hr = S_OK;
  341. CAccessLock Lock(&m_csLock);
  342. if (hr == S_OK)
  343. {
  344. if (m_WiaLink.IsEnabled())
  345. {
  346. m_WiaLink.StopMonitoring();
  347. m_WiaLink.Term();
  348. }
  349. }
  350. if (hr == S_OK)
  351. {
  352. hr = m_PreviewGraph.DestroyVideo();
  353. CHECK_S_OK2(hr, ("CWiaVideo::DestroyVideo failed to destroy video"));
  354. }
  355. return hr;
  356. }
  357. ///////////////////////////////
  358. // Play
  359. //
  360. STDMETHODIMP CWiaVideo::Play()
  361. {
  362. DBG_FN("CWiaVideo::Play");
  363. HRESULT hr = S_OK;
  364. CAccessLock Lock(&m_csLock);
  365. if (hr == S_OK)
  366. {
  367. hr = m_PreviewGraph.Play();
  368. CHECK_S_OK2(hr, ("CWiaVideo::Play failed"));
  369. }
  370. return hr;
  371. }
  372. ///////////////////////////////
  373. // Pause
  374. //
  375. STDMETHODIMP CWiaVideo::Pause()
  376. {
  377. DBG_FN("CWiaVideo::Pause");
  378. HRESULT hr = S_OK;
  379. CAccessLock Lock(&m_csLock);
  380. if (hr == S_OK)
  381. {
  382. hr = m_PreviewGraph.Pause();
  383. CHECK_S_OK2(hr, ("CWiaVideo::Pause failed"));
  384. }
  385. return hr;
  386. }
  387. ///////////////////////////////
  388. // GetCurrentState
  389. //
  390. STDMETHODIMP CWiaVideo::GetCurrentState(WIAVIDEO_STATE *pCurrentState)
  391. {
  392. DBG_FN("CWiaVideo::GetCurrentState");
  393. ASSERT(pCurrentState != NULL);
  394. HRESULT hr = S_OK;
  395. CAccessLock Lock(&m_csLock);
  396. if (pCurrentState == NULL)
  397. {
  398. hr = E_POINTER;
  399. CHECK_S_OK2(hr, ("CWiaVideo::GetCurrentState received NULL param"));
  400. }
  401. if (hr == S_OK)
  402. {
  403. *pCurrentState = m_PreviewGraph.GetState();
  404. }
  405. return hr;
  406. }
  407. ///////////////////////////////
  408. // TakePicture
  409. //
  410. STDMETHODIMP CWiaVideo::TakePicture(BSTR *pbstrNewImageFileName)
  411. {
  412. DBG_FN("CWiaVideo::TakePicture");
  413. HRESULT hr = S_OK;
  414. CSimpleString strNewImageFileName;
  415. CAccessLock Lock(&m_csLock);
  416. if (hr == S_OK)
  417. {
  418. hr = m_PreviewGraph.TakePicture(&strNewImageFileName);
  419. CHECK_S_OK2(hr, ("CWiaVideo::TakePicture failed"));
  420. }
  421. if (hr == S_OK)
  422. {
  423. *pbstrNewImageFileName =::SysAllocString(
  424. (CSimpleStringConvert::WideString(
  425. strNewImageFileName)).String());
  426. if (*pbstrNewImageFileName)
  427. {
  428. DBG_TRC(("CWiaVideo::TakePicture, new image file name is '%ls'",
  429. *pbstrNewImageFileName));
  430. }
  431. else
  432. {
  433. hr = E_OUTOFMEMORY;
  434. CHECK_S_OK2(hr, ("CWiaVideo::TakePicture, SysAllocString "
  435. "returned NULL BSTR"));
  436. }
  437. }
  438. return hr;
  439. }
  440. ///////////////////////////////
  441. // ResizeVideo
  442. //
  443. STDMETHODIMP CWiaVideo::ResizeVideo(BOOL bStretchToFitParent)
  444. {
  445. DBG_FN("CWiaVideo::ResizeVideo");
  446. HRESULT hr = S_OK;
  447. CAccessLock Lock(&m_csLock);
  448. if (hr == S_OK)
  449. {
  450. hr = m_PreviewGraph.ResizeVideo(bStretchToFitParent);
  451. CHECK_S_OK2(hr, ("CWiaVideo::ResizeVideo failed"));
  452. }
  453. return hr;
  454. }
  455. ///////////////////////////////
  456. // ProcessAsyncImage
  457. //
  458. // Called by CPreviewGraph
  459. // when user presses hardware
  460. // button and it is delivered to
  461. // Still Pin.
  462. //
  463. HRESULT CWiaVideo::ProcessAsyncImage(const CSimpleString *pNewImage)
  464. {
  465. DBG_FN("CWiaVideo::ProcessAsyncImage");
  466. HRESULT hr = S_OK;
  467. hr = m_WiaLink.SignalNewImage(pNewImage);
  468. return hr;
  469. }