AdvancedCapture.xaml.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. //*********************************************************
  2. //
  3. // Copyright (c) Microsoft. All rights reserved.
  4. // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
  5. // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
  6. // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
  7. // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
  8. //
  9. //*********************************************************
  10. //
  11. // AdvancedCapture.xaml.cpp
  12. // Implementation of the AdvancedCapture class
  13. //
  14. #include "pch.h"
  15. #include "AdvancedCapture.xaml.h"
  16. using namespace SDKSample::MediaCapture;
  17. using namespace Windows::UI::Xaml;
  18. using namespace Windows::UI::Xaml::Navigation;
  19. using namespace Windows::UI::Xaml::Data;
  20. using namespace Windows::System;
  21. using namespace Windows::Foundation;
  22. using namespace Windows::Foundation::Collections;
  23. using namespace Platform;
  24. using namespace Windows::UI;
  25. using namespace Windows::UI::Core;
  26. using namespace Windows::UI::Xaml;
  27. using namespace Windows::UI::Xaml::Controls;
  28. using namespace Windows::UI::Xaml::Data;
  29. using namespace Windows::UI::Xaml::Media;
  30. using namespace Windows::Storage;
  31. using namespace Windows::Media::MediaProperties;
  32. using namespace Windows::Storage::Streams;
  33. using namespace Windows::System;
  34. using namespace Windows::UI::Xaml::Media::Imaging;
  35. using namespace Windows::Devices::Enumeration;
  36. ref class ReencodeState sealed
  37. {
  38. public:
  39. ReencodeState()
  40. {
  41. }
  42. virtual ~ReencodeState()
  43. {
  44. if (InputStream != nullptr)
  45. {
  46. delete InputStream;
  47. }
  48. if (OutputStream != nullptr)
  49. {
  50. delete OutputStream;
  51. }
  52. }
  53. internal:
  54. Windows::Storage::Streams::IRandomAccessStream ^InputStream;
  55. Windows::Storage::Streams::IRandomAccessStream ^OutputStream;
  56. Windows::Storage::StorageFile ^PhotoStorage;
  57. Windows::Graphics::Imaging::BitmapDecoder ^Decoder;
  58. Windows::Graphics::Imaging::BitmapEncoder ^Encoder;
  59. };
  60. AdvancedCapture::AdvancedCapture()
  61. {
  62. InitializeComponent();
  63. ScenarioInit();
  64. }
  65. /// <summary>
  66. /// Invoked when this page is about to be displayed in a Frame.
  67. /// </summary>
  68. /// <param name="e">Event data that describes how this page was reached. The Parameter
  69. /// property is typically used to configure the page.</param>
  70. void AdvancedCapture::OnNavigatedTo(NavigationEventArgs^ e)
  71. {
  72. // A pointer back to the main page. This is needed if you want to call methods in MainPage such
  73. // as NotifyUser()
  74. rootPage = MainPage::Current;
  75. m_orientationChangedEventToken = Windows::Graphics::Display::DisplayProperties::OrientationChanged += ref new Windows::Graphics::Display::DisplayPropertiesEventHandler(this, &AdvancedCapture::DisplayProperties_OrientationChanged);
  76. }
  77. void AdvancedCapture::OnNavigatedFrom(NavigationEventArgs^ e)
  78. {
  79. Windows::Media::MediaControl::SoundLevelChanged -= m_eventRegistrationToken;
  80. Windows::Graphics::Display::DisplayProperties::OrientationChanged -= m_orientationChangedEventToken;
  81. }
  82. void AdvancedCapture::ScenarioInit()
  83. {
  84. rootPage = MainPage::Current;
  85. btnStartDevice2->IsEnabled = true;
  86. btnStartPreview2->IsEnabled = false;
  87. m_bRecording = false;
  88. m_bPreviewing = false;
  89. m_bEffectAdded = false;
  90. previewElement2->Source = nullptr;
  91. ShowStatusMessage("");
  92. EffectTypeCombo->IsEnabled = false;
  93. previewCanvas2->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
  94. EnumerateWebcamsAsync();
  95. m_bSuspended = false;
  96. }
  97. void AdvancedCapture::ScenarioReset()
  98. {
  99. previewCanvas2->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
  100. ScenarioInit();
  101. }
  102. void AdvancedCapture::Failed(Windows::Media::Capture::MediaCapture ^currentCaptureObject, Windows::Media::Capture::MediaCaptureFailedEventArgs^ currentFailure)
  103. {
  104. String ^message = "Fatal error" + currentFailure->Message;
  105. create_task(Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::High,
  106. ref new Windows::UI::Core::DispatchedHandler([this, message]()
  107. {
  108. ShowStatusMessage(message);
  109. })));
  110. }
  111. void AdvancedCapture::btnStartDevice_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  112. {
  113. try
  114. {
  115. EnableButton(false, "StartDevice");
  116. ShowStatusMessage("Starting device");
  117. auto mediaCapture = ref new Windows::Media::Capture::MediaCapture();
  118. m_mediaCaptureMgr = mediaCapture;
  119. auto settings = ref new Windows::Media::Capture::MediaCaptureInitializationSettings();
  120. auto chosenDevInfo = m_devInfoCollection->GetAt(EnumedDeviceList2->SelectedIndex);
  121. settings->VideoDeviceId = chosenDevInfo->Id;
  122. if (chosenDevInfo->EnclosureLocation != nullptr && chosenDevInfo->EnclosureLocation->Panel == Windows::Devices::Enumeration::Panel::Back)
  123. {
  124. m_bRotateVideoOnOrientationChange = true;
  125. m_bReversePreviewRotation = false;
  126. }
  127. else if (chosenDevInfo->EnclosureLocation != nullptr && chosenDevInfo->EnclosureLocation->Panel == Windows::Devices::Enumeration::Panel::Front)
  128. {
  129. m_bRotateVideoOnOrientationChange = true;
  130. m_bReversePreviewRotation = true;
  131. }
  132. else
  133. {
  134. m_bRotateVideoOnOrientationChange = false;
  135. }
  136. create_task(mediaCapture->InitializeAsync(settings)).then([this](task<void> initTask)
  137. {
  138. try
  139. {
  140. initTask.get();
  141. auto mediaCapture = m_mediaCaptureMgr.Get();
  142. DisplayProperties_OrientationChanged(nullptr);
  143. EnableButton(true, "StartPreview");
  144. EnableButton(true, "StartStopRecord");
  145. EnableButton(true, "TakePhoto");
  146. ShowStatusMessage("Device initialized successful");
  147. EffectTypeCombo->IsEnabled = true;
  148. mediaCapture->Failed += ref new Windows::Media::Capture::MediaCaptureFailedEventHandler(this, &AdvancedCapture::Failed);
  149. }
  150. catch (Exception ^ e)
  151. {
  152. ShowExceptionMessage(e);
  153. }
  154. });
  155. }
  156. catch (Platform::Exception^ e)
  157. {
  158. ShowExceptionMessage(e);
  159. }
  160. }
  161. void AdvancedCapture::btnStartPreview_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  162. {
  163. m_bPreviewing = false;
  164. try
  165. {
  166. ShowStatusMessage("Starting preview");
  167. EnableButton(false, "StartPreview");
  168. auto mediaCapture = m_mediaCaptureMgr.Get();
  169. previewCanvas2->Visibility = Windows::UI::Xaml::Visibility::Visible;
  170. previewElement2->Source = mediaCapture;
  171. create_task(mediaCapture->StartPreviewAsync()).then([this](task<void> previewTask)
  172. {
  173. try
  174. {
  175. previewTask.get();
  176. m_bPreviewing = true;
  177. ShowStatusMessage("Start preview successful");
  178. }
  179. catch (Exception ^e)
  180. {
  181. ShowExceptionMessage(e);
  182. }
  183. });
  184. }
  185. catch (Platform::Exception^ e)
  186. {
  187. m_bPreviewing = false;
  188. previewElement2->Source = nullptr;
  189. EnableButton(true, "StartPreview");
  190. ShowExceptionMessage(e);
  191. }
  192. }
  193. void AdvancedCapture::lstEnumedDevices_SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
  194. {
  195. if ( m_bPreviewing )
  196. {
  197. create_task(m_mediaCaptureMgr->StopPreviewAsync()).then([this](task<void> previewTask)
  198. {
  199. try
  200. {
  201. previewTask.get();
  202. m_bPreviewing = false;
  203. }
  204. catch (Exception ^e)
  205. {
  206. ShowExceptionMessage(e);
  207. }
  208. });
  209. }
  210. btnStartDevice2->IsEnabled = true;
  211. btnStartPreview2->IsEnabled = false;
  212. m_bRecording = false;
  213. previewElement2->Source = nullptr;
  214. EffectTypeCombo->IsEnabled = false;
  215. m_bEffectAdded = false;
  216. m_bEffectAddedToRecord = false;
  217. m_bEffectAddedToPhoto = false;
  218. ShowStatusMessage("");
  219. }
  220. void AdvancedCapture::EnumerateWebcamsAsync()
  221. {
  222. try
  223. {
  224. ShowStatusMessage("Enumerating Webcams...");
  225. m_devInfoCollection = nullptr;
  226. EnumedDeviceList2->Items->Clear();
  227. task<DeviceInformationCollection^>(DeviceInformation::FindAllAsync(DeviceClass::VideoCapture)).then([this](task<DeviceInformationCollection^> findTask)
  228. {
  229. try
  230. {
  231. m_devInfoCollection = findTask.get();
  232. if (m_devInfoCollection == nullptr || m_devInfoCollection->Size == 0)
  233. {
  234. ShowStatusMessage("No WebCams found.");
  235. }
  236. else
  237. {
  238. for(unsigned int i = 0; i < m_devInfoCollection->Size; i++)
  239. {
  240. auto devInfo = m_devInfoCollection->GetAt(i);
  241. EnumedDeviceList2->Items->Append(devInfo->Name);
  242. }
  243. EnumedDeviceList2->SelectedIndex = 0;
  244. ShowStatusMessage("Enumerating Webcams completed successfully.");
  245. btnStartDevice2->IsEnabled = true;
  246. }
  247. }
  248. catch (Exception ^e)
  249. {
  250. ShowExceptionMessage(e);
  251. }
  252. });
  253. }
  254. catch (Platform::Exception^ e)
  255. {
  256. ShowExceptionMessage(e);
  257. }
  258. }
  259. void AdvancedCapture::AddEffectToImageStream()
  260. {
  261. auto mediaCapture = m_mediaCaptureMgr.Get();
  262. Windows::Media::Capture::VideoDeviceCharacteristic charecteristic = mediaCapture->MediaCaptureSettings->VideoDeviceCharacteristic;
  263. if((charecteristic != Windows::Media::Capture::VideoDeviceCharacteristic::AllStreamsIdentical) &&
  264. (charecteristic != Windows::Media::Capture::VideoDeviceCharacteristic::PreviewPhotoStreamsIdentical) &&
  265. (charecteristic != Windows::Media::Capture::VideoDeviceCharacteristic::RecordPhotoStreamsIdentical))
  266. {
  267. Windows::Media::MediaProperties::IMediaEncodingProperties ^props = mediaCapture->VideoDeviceController->GetMediaStreamProperties(Windows::Media::Capture::MediaStreamType::Photo);
  268. if(props->Type->Equals("Image"))
  269. {
  270. //Switch to a video media type instead since we can't add an effect to an image media type
  271. Windows::Foundation::Collections::IVectorView<Windows::Media::MediaProperties::IMediaEncodingProperties^>^ supportedPropsList = mediaCapture->VideoDeviceController->GetAvailableMediaStreamProperties(Windows::Media::Capture::MediaStreamType::Photo);
  272. {
  273. unsigned int i = 0;
  274. while (i < supportedPropsList->Size)
  275. {
  276. Windows::Media::MediaProperties::IMediaEncodingProperties^ props = supportedPropsList->GetAt(i);
  277. String^ s = props->Type;
  278. if(props->Type->Equals("Video"))
  279. {
  280. task<void>(mediaCapture->VideoDeviceController->SetMediaStreamPropertiesAsync(Windows::Media::Capture::MediaStreamType::Photo,props)).then([this](task<void> changeTypeTask)
  281. {
  282. try
  283. {
  284. changeTypeTask.get();
  285. ShowStatusMessage("Change type on photo stream successful");
  286. //Now add the effect on the image pin
  287. task<void>(m_mediaCaptureMgr->AddEffectAsync(Windows::Media::Capture::MediaStreamType::Photo,"OcvTransform.OcvImageManipulations", nullptr)).then([this](task<void> effectTask3)
  288. {
  289. try
  290. {
  291. effectTask3.get();
  292. m_bEffectAddedToPhoto = true;
  293. ShowStatusMessage("Adding effect to photo stream successful");
  294. EffectTypeCombo->IsEnabled = true;
  295. }
  296. catch(Exception ^e)
  297. {
  298. ShowExceptionMessage(e);
  299. EffectTypeCombo->IsEnabled = true;
  300. }
  301. });
  302. }
  303. catch(Exception ^e)
  304. {
  305. ShowExceptionMessage(e);
  306. EffectTypeCombo->IsEnabled = true;
  307. }
  308. });
  309. break;
  310. }
  311. i++;
  312. }
  313. }
  314. }
  315. else
  316. {
  317. //Add the effect to the image pin if the type is already "Video"
  318. task<void>(mediaCapture->AddEffectAsync(Windows::Media::Capture::MediaStreamType::Photo,"OcvTransform.OcvImageManipulations", nullptr)).then([this](task<void> effectTask3)
  319. {
  320. try
  321. {
  322. effectTask3.get();
  323. m_bEffectAddedToPhoto = true;
  324. ShowStatusMessage("Adding effect to photo stream successful");
  325. EffectTypeCombo->IsEnabled = true;
  326. }
  327. catch(Exception ^e)
  328. {
  329. ShowExceptionMessage(e);
  330. EffectTypeCombo->IsEnabled = true;
  331. }
  332. });
  333. }
  334. }
  335. }
  336. void AdvancedCapture::ShowStatusMessage(Platform::String^ text)
  337. {
  338. rootPage->NotifyUser(text, NotifyType::StatusMessage);
  339. }
  340. void AdvancedCapture::ShowExceptionMessage(Platform::Exception^ ex)
  341. {
  342. rootPage->NotifyUser(ex->Message, NotifyType::ErrorMessage);
  343. }
  344. void AdvancedCapture::EnableButton(bool enabled, String^ name)
  345. {
  346. if (name->Equals("StartDevice"))
  347. {
  348. btnStartDevice2->IsEnabled = enabled;
  349. }
  350. else if (name->Equals("StartPreview"))
  351. {
  352. btnStartPreview2->IsEnabled = enabled;
  353. }
  354. }
  355. task<Windows::Storage::StorageFile^> AdvancedCapture::ReencodePhotoAsync(
  356. Windows::Storage::StorageFile ^tempStorageFile,
  357. Windows::Storage::FileProperties::PhotoOrientation photoRotation)
  358. {
  359. ReencodeState ^state = ref new ReencodeState();
  360. return create_task(tempStorageFile->OpenAsync(Windows::Storage::FileAccessMode::Read)).then([state](Windows::Storage::Streams::IRandomAccessStream ^stream)
  361. {
  362. state->InputStream = stream;
  363. return Windows::Graphics::Imaging::BitmapDecoder::CreateAsync(state->InputStream);
  364. }).then([state](Windows::Graphics::Imaging::BitmapDecoder ^decoder)
  365. {
  366. state->Decoder = decoder;
  367. return Windows::Storage::KnownFolders::PicturesLibrary->CreateFileAsync(PHOTO_FILE_NAME, Windows::Storage::CreationCollisionOption::GenerateUniqueName);
  368. }).then([state](Windows::Storage::StorageFile ^storageFile)
  369. {
  370. state->PhotoStorage = storageFile;
  371. return state->PhotoStorage->OpenAsync(Windows::Storage::FileAccessMode::ReadWrite);
  372. }).then([state](Windows::Storage::Streams::IRandomAccessStream ^stream)
  373. {
  374. state->OutputStream = stream;
  375. state->OutputStream->Size = 0;
  376. return Windows::Graphics::Imaging::BitmapEncoder::CreateForTranscodingAsync(state->OutputStream, state->Decoder);
  377. }).then([state, photoRotation](Windows::Graphics::Imaging::BitmapEncoder ^encoder)
  378. {
  379. state->Encoder = encoder;
  380. auto properties = ref new Windows::Graphics::Imaging::BitmapPropertySet();
  381. properties->Insert("System.Photo.Orientation",
  382. ref new Windows::Graphics::Imaging::BitmapTypedValue((unsigned short)photoRotation, Windows::Foundation::PropertyType::UInt16));
  383. return create_task(state->Encoder->BitmapProperties->SetPropertiesAsync(properties));
  384. }).then([state]()
  385. {
  386. return state->Encoder->FlushAsync();
  387. }).then([tempStorageFile, state](task<void> previousTask)
  388. {
  389. auto result = state->PhotoStorage;
  390. delete state;
  391. tempStorageFile->DeleteAsync(Windows::Storage::StorageDeleteOption::PermanentDelete);
  392. previousTask.get();
  393. return result;
  394. });
  395. }
  396. Windows::Storage::FileProperties::PhotoOrientation AdvancedCapture::GetCurrentPhotoRotation()
  397. {
  398. bool counterclockwiseRotation = m_bReversePreviewRotation;
  399. if (m_bRotateVideoOnOrientationChange)
  400. {
  401. return PhotoRotationLookup(Windows::Graphics::Display::DisplayProperties::CurrentOrientation, counterclockwiseRotation);
  402. }
  403. else
  404. {
  405. return Windows::Storage::FileProperties::PhotoOrientation::Normal;
  406. }
  407. }
  408. void AdvancedCapture::PrepareForVideoRecording()
  409. {
  410. Windows::Media::Capture::MediaCapture ^mediaCapture = m_mediaCaptureMgr.Get();
  411. if (mediaCapture == nullptr)
  412. {
  413. return;
  414. }
  415. bool counterclockwiseRotation = m_bReversePreviewRotation;
  416. if (m_bRotateVideoOnOrientationChange)
  417. {
  418. mediaCapture->SetRecordRotation(VideoRotationLookup(Windows::Graphics::Display::DisplayProperties::CurrentOrientation, counterclockwiseRotation));
  419. }
  420. else
  421. {
  422. mediaCapture->SetRecordRotation(Windows::Media::Capture::VideoRotation::None);
  423. }
  424. }
  425. void AdvancedCapture::DisplayProperties_OrientationChanged(Platform::Object^ sender)
  426. {
  427. Windows::Media::Capture::MediaCapture ^mediaCapture = m_mediaCaptureMgr.Get();
  428. if (mediaCapture == nullptr)
  429. {
  430. return;
  431. }
  432. bool previewMirroring = mediaCapture->GetPreviewMirroring();
  433. bool counterclockwiseRotation = (previewMirroring && !m_bReversePreviewRotation) ||
  434. (!previewMirroring && m_bReversePreviewRotation);
  435. if (m_bRotateVideoOnOrientationChange)
  436. {
  437. mediaCapture->SetPreviewRotation(VideoRotationLookup(Windows::Graphics::Display::DisplayProperties::CurrentOrientation, counterclockwiseRotation));
  438. }
  439. else
  440. {
  441. mediaCapture->SetPreviewRotation(Windows::Media::Capture::VideoRotation::None);
  442. }
  443. }
  444. Windows::Storage::FileProperties::PhotoOrientation AdvancedCapture::PhotoRotationLookup(
  445. Windows::Graphics::Display::DisplayOrientations displayOrientation, bool counterclockwise)
  446. {
  447. switch (displayOrientation)
  448. {
  449. case Windows::Graphics::Display::DisplayOrientations::Landscape:
  450. return Windows::Storage::FileProperties::PhotoOrientation::Normal;
  451. case Windows::Graphics::Display::DisplayOrientations::Portrait:
  452. return (counterclockwise) ? Windows::Storage::FileProperties::PhotoOrientation::Rotate270:
  453. Windows::Storage::FileProperties::PhotoOrientation::Rotate90;
  454. case Windows::Graphics::Display::DisplayOrientations::LandscapeFlipped:
  455. return Windows::Storage::FileProperties::PhotoOrientation::Rotate180;
  456. case Windows::Graphics::Display::DisplayOrientations::PortraitFlipped:
  457. return (counterclockwise) ? Windows::Storage::FileProperties::PhotoOrientation::Rotate90 :
  458. Windows::Storage::FileProperties::PhotoOrientation::Rotate270;
  459. default:
  460. return Windows::Storage::FileProperties::PhotoOrientation::Unspecified;
  461. }
  462. }
  463. Windows::Media::Capture::VideoRotation AdvancedCapture::VideoRotationLookup(
  464. Windows::Graphics::Display::DisplayOrientations displayOrientation, bool counterclockwise)
  465. {
  466. switch (displayOrientation)
  467. {
  468. case Windows::Graphics::Display::DisplayOrientations::Landscape:
  469. return Windows::Media::Capture::VideoRotation::None;
  470. case Windows::Graphics::Display::DisplayOrientations::Portrait:
  471. return (counterclockwise) ? Windows::Media::Capture::VideoRotation::Clockwise270Degrees :
  472. Windows::Media::Capture::VideoRotation::Clockwise90Degrees;
  473. case Windows::Graphics::Display::DisplayOrientations::LandscapeFlipped:
  474. return Windows::Media::Capture::VideoRotation::Clockwise180Degrees;
  475. case Windows::Graphics::Display::DisplayOrientations::PortraitFlipped:
  476. return (counterclockwise) ? Windows::Media::Capture::VideoRotation::Clockwise90Degrees:
  477. Windows::Media::Capture::VideoRotation::Clockwise270Degrees ;
  478. default:
  479. return Windows::Media::Capture::VideoRotation::None;
  480. }
  481. }
  482. void SDKSample::MediaCapture::AdvancedCapture::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  483. {
  484. try
  485. {
  486. create_task(m_mediaCaptureMgr->ClearEffectsAsync(Windows::Media::Capture::MediaStreamType::VideoPreview)).then([this](task<void> cleanTask)
  487. {
  488. m_bEffectAdded = true;
  489. int index = EffectTypeCombo->SelectedIndex;
  490. PropertySet^ props = ref new PropertySet();
  491. props->Insert(L"{698649BE-8EAE-4551-A4CB-3EC98FBD3D86}", index);
  492. create_task(m_mediaCaptureMgr->AddEffectAsync(Windows::Media::Capture::MediaStreamType::VideoPreview,"OcvTransform.OcvImageManipulations", props)).then([this](task<void> effectTask)
  493. {
  494. try
  495. {
  496. effectTask.get();
  497. auto mediaCapture = m_mediaCaptureMgr.Get();
  498. Windows::Media::Capture::VideoDeviceCharacteristic charecteristic = mediaCapture->MediaCaptureSettings->VideoDeviceCharacteristic;
  499. ShowStatusMessage("Add effect successful to preview stream successful");
  500. if((charecteristic != Windows::Media::Capture::VideoDeviceCharacteristic::AllStreamsIdentical) &&
  501. (charecteristic != Windows::Media::Capture::VideoDeviceCharacteristic::PreviewRecordStreamsIdentical))
  502. {
  503. Windows::Media::MediaProperties::IMediaEncodingProperties ^props = mediaCapture->VideoDeviceController->GetMediaStreamProperties(Windows::Media::Capture::MediaStreamType::VideoRecord);
  504. Windows::Media::MediaProperties::VideoEncodingProperties ^videoEncodingProperties = static_cast<Windows::Media::MediaProperties::VideoEncodingProperties ^>(props);
  505. if(!videoEncodingProperties->Subtype->Equals("H264")) //Can't add an effect to an H264 stream
  506. {
  507. task<void>(mediaCapture->AddEffectAsync(Windows::Media::Capture::MediaStreamType::VideoRecord,"OcvTransform.OcvImageManipulations", nullptr)).then([this](task<void> effectTask2)
  508. {
  509. try
  510. {
  511. effectTask2.get();
  512. ShowStatusMessage("Add effect successful to record stream successful");
  513. m_bEffectAddedToRecord = true;
  514. AddEffectToImageStream();
  515. EffectTypeCombo->IsEnabled = true;
  516. }
  517. catch(Exception ^e)
  518. {
  519. ShowExceptionMessage(e);
  520. EffectTypeCombo->IsEnabled = true;
  521. }
  522. });
  523. }
  524. else
  525. {
  526. AddEffectToImageStream();
  527. EffectTypeCombo->IsEnabled = true;
  528. }
  529. }
  530. else
  531. {
  532. AddEffectToImageStream();
  533. EffectTypeCombo->IsEnabled = true;
  534. }
  535. }
  536. catch (Exception ^e)
  537. {
  538. ShowExceptionMessage(e);
  539. EffectTypeCombo->IsEnabled = true;
  540. }
  541. });
  542. });
  543. }
  544. catch (Platform::Exception ^e)
  545. {
  546. ShowExceptionMessage(e);
  547. EffectTypeCombo->IsEnabled = true;
  548. }
  549. }