@@ -284,6 +284,12 @@ void CaptureControllerImpl::ResetCaptureController() {
284284 video_source_ = nullptr ;
285285 base_preview_media_type_ = nullptr ;
286286 base_capture_media_type_ = nullptr ;
287+ preview_source_stream_index_ = static_cast <DWORD >(
288+ MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_VIDEO_PREVIEW );
289+ record_source_stream_index_ = static_cast <DWORD >(
290+ MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_VIDEO_RECORD );
291+ photo_source_stream_index_ =
292+ static_cast <DWORD >(MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_PHOTO );
287293
288294 if (dxgi_device_manager_) {
289295 dxgi_device_manager_->ResetDevice (dx11_device_.Get (),
@@ -371,7 +377,8 @@ void CaptureControllerImpl::TakePicture(const std::string& file_path) {
371377 // Check MF_CAPTURE_ENGINE_PHOTO_TAKEN event handling
372378 // for response process.
373379 hr = photo_handler_->TakePhoto (file_path, capture_engine_.Get (),
374- base_capture_media_type_.Get ());
380+ base_capture_media_type_.Get (),
381+ photo_source_stream_index_);
375382 if (FAILED (hr)) {
376383 // Destroy photo handler on error cases to make sure state is resetted.
377384 photo_handler_ = nullptr ;
@@ -470,22 +477,65 @@ HRESULT CaptureControllerImpl::FindBaseMediaTypes() {
470477 return FindBaseMediaTypesForSource (source.Get ());
471478}
472479
480+ void CaptureControllerImpl::ResolveSourceStreamIndices (
481+ IMFCaptureSource* source) {
482+ // Devices that don't support the preferred-stream selectors fail this probe,
483+ // typically with MF_E_INVALIDSTREAMNUMBER.
484+ ComPtr<IMFMediaType> probe;
485+ HRESULT hr = source->GetAvailableDeviceMediaType (preview_source_stream_index_,
486+ 0 , probe.GetAddressOf ());
487+ if (SUCCEEDED (hr)) {
488+ return ;
489+ }
490+
491+ DWORD stream_count = 0 ;
492+ if (FAILED (source->GetDeviceStreamCount (&stream_count))) {
493+ return ;
494+ }
495+
496+ // Prefer a video-preview stream, then video-capture.
497+ DWORD video_stream_index = MAXDWORD ;
498+ for (DWORD i = 0 ; i < stream_count; i++) {
499+ MF_CAPTURE_ENGINE_STREAM_CATEGORY category;
500+ if (FAILED (source->GetDeviceStreamCategory (i, &category))) {
501+ continue ;
502+ }
503+ if (category == MF_CAPTURE_ENGINE_STREAM_CATEGORY_VIDEO_PREVIEW ) {
504+ video_stream_index = i;
505+ break ;
506+ }
507+ if (category == MF_CAPTURE_ENGINE_STREAM_CATEGORY_VIDEO_CAPTURE &&
508+ video_stream_index == MAXDWORD ) {
509+ video_stream_index = i;
510+ }
511+ }
512+ if (video_stream_index == MAXDWORD ) {
513+ // No matching stream found.
514+ return ;
515+ }
516+
517+ // One stream for every role keeps the media types consistent with the sinks.
518+ preview_source_stream_index_ = video_stream_index;
519+ record_source_stream_index_ = video_stream_index;
520+ photo_source_stream_index_ = video_stream_index;
521+ }
522+
473523HRESULT CaptureControllerImpl::FindBaseMediaTypesForSource (
474524 IMFCaptureSource* source) {
525+ ResolveSourceStreamIndices (source);
526+
475527 // Find base media type for previewing.
476- if (!FindBestMediaType (
477- (DWORD )MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_VIDEO_PREVIEW ,
478- source, base_preview_media_type_.GetAddressOf (),
479- GetMaxPreviewHeight (), &preview_frame_width_,
480- &preview_frame_height_)) {
528+ if (!FindBestMediaType (preview_source_stream_index_, source,
529+ base_preview_media_type_.GetAddressOf (),
530+ GetMaxPreviewHeight (), &preview_frame_width_,
531+ &preview_frame_height_)) {
481532 return E_FAIL ;
482533 }
483534
484535 // Find base media type for record and photo capture.
485- if (!FindBestMediaType (
486- (DWORD )MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_VIDEO_RECORD ,
487- source, base_capture_media_type_.GetAddressOf (), 0xffffffff , nullptr ,
488- nullptr )) {
536+ if (!FindBestMediaType (record_source_stream_index_, source,
537+ base_capture_media_type_.GetAddressOf (), 0xffffffff ,
538+ nullptr , nullptr )) {
489539 return E_FAIL ;
490540 }
491541
@@ -561,7 +611,8 @@ void CaptureControllerImpl::StartRecord(const std::string& file_path) {
561611 // Check MF_CAPTURE_ENGINE_RECORD_STARTED event handling for response
562612 // process.
563613 hr = record_handler_->StartRecord (file_path, capture_engine_.Get (),
564- base_capture_media_type_.Get ());
614+ base_capture_media_type_.Get (),
615+ record_source_stream_index_);
565616 if (FAILED (hr)) {
566617 // Destroy record handler on error cases to make sure state is resetted.
567618 record_handler_ = nullptr ;
@@ -624,9 +675,8 @@ void CaptureControllerImpl::StartPreview() {
624675 }
625676 }
626677
627- hr = source->SetCurrentDeviceMediaType (
628- (DWORD )MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_VIDEO_PREVIEW ,
629- base_preview_media_type_.Get ());
678+ hr = source->SetCurrentDeviceMediaType (preview_source_stream_index_,
679+ base_preview_media_type_.Get ());
630680 if (FAILED (hr)) {
631681 return OnPreviewStarted (GetCameraResult (hr),
632682 " Failed to set video preview output format" );
@@ -647,9 +697,9 @@ void CaptureControllerImpl::StartPreview() {
647697
648698 // Check MF_CAPTURE_ENGINE_PREVIEW_STARTED event handling for response
649699 // process.
650- hr = preview_handler_->StartPreview (capture_engine_. Get (),
651- base_preview_media_type_.Get (),
652- capture_engine_callback_handler_.Get ());
700+ hr = preview_handler_->StartPreview (
701+ capture_engine_. Get (), base_preview_media_type_.Get (),
702+ capture_engine_callback_handler_.Get (), preview_source_stream_index_ );
653703
654704 if (FAILED (hr)) {
655705 // Destroy preview handler on error cases to make sure state is resetted.
0 commit comments