Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move SetCurrentDeviceMediaType to StartPreview
  • Loading branch information
dasyad00 committed Nov 8, 2024
commit 1f17a42b447b4fb0ab1cf84bccfcdfbf6dc8bc14
32 changes: 22 additions & 10 deletions packages/camera/camera_windows/windows/capture_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,26 +467,23 @@ HRESULT CaptureControllerImpl::FindBaseMediaTypes() {
return hr;
}

return FindBaseMediaTypes(source.Get());
}

HRESULT CaptureControllerImpl::FindBaseMediaTypes(IMFCaptureSource* source) {
// Find base media type for previewing.
if (!FindBestMediaType(
(DWORD)MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_VIDEO_PREVIEW,
source.Get(), base_preview_media_type_.GetAddressOf(),
source, base_preview_media_type_.GetAddressOf(),
GetMaxPreviewHeight(), &preview_frame_width_,
&preview_frame_height_)) {
return E_FAIL;
}

hr = source->SetCurrentDeviceMediaType(
(DWORD)MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_VIDEO_PREVIEW,
base_preview_media_type_.Get());
if (FAILED(hr)) {
return hr;
}

// Find base media type for record and photo capture.
if (!FindBestMediaType(
(DWORD)MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_VIDEO_RECORD,
source.Get(), base_capture_media_type_.GetAddressOf(), 0xffffffff,
source, base_capture_media_type_.GetAddressOf(), 0xffffffff,
nullptr, nullptr)) {
return E_FAIL;
}
Expand Down Expand Up @@ -573,15 +570,30 @@ void CaptureControllerImpl::StartPreview() {

HRESULT hr = S_OK;

ComPtr<IMFCaptureSource> source;
hr = capture_engine_->GetSource(&source);
if (FAILED(hr)) {
return OnPreviewStarted(GetCameraResult(hr),
"Failed to initialize video preview");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use the same error message for three different failures; we want to be able to distinguish what failed. Each case in the method should describe what specifically failed.

Copy link
Contributor Author

@dasyad00 dasyad00 Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have clarified the error messages in 79610aa2c52f6971f4e26ab1ada6dd8be04fb6f0 923fc4d

}

if (!base_preview_media_type_) {
// Enumerates mediatypes and finds media type for video capture.
hr = FindBaseMediaTypes();
hr = FindBaseMediaTypes(source.Get());
if (FAILED(hr)) {
return OnPreviewStarted(GetCameraResult(hr),
"Failed to initialize video preview");
}
}

hr = source->SetCurrentDeviceMediaType(
(DWORD)MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_VIDEO_PREVIEW,
base_preview_media_type_.Get());
if (FAILED(hr)) {
return OnPreviewStarted(GetCameraResult(hr),
"Failed to initialize video preview");
}

texture_handler_->UpdateTextureSize(preview_frame_width_,
preview_frame_height_);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ class CaptureControllerImpl : public CaptureController,
// Enumerates video_sources media types and finds out best resolution
// for preview and video capture.
HRESULT FindBaseMediaTypes();
HRESULT FindBaseMediaTypes(IMFCaptureSource* source);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please give these different names, and their own declaration comments explaining what they do. Function overloading should only be used when the methods do exactly the same thing, just with different input types.

The new method could be called FindBaseMediaTypesForSource.

Copy link
Contributor Author

@dasyad00 dasyad00 Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have renamed the method in ff678d6e23138246d2d7553d3093b926186d2405 2445b24


// Stops preview. Called internally on camera reset and dispose.
HRESULT StopPreview();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,6 @@ void MockAvailableMediaTypes(MockCaptureEngine* engine,
return S_OK;
});

EXPECT_CALL(
*capture_source,
SetCurrentDeviceMediaType(
Eq((DWORD)
MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_VIDEO_PREVIEW),
_))
.Times(1)
.WillOnce(Return(S_OK));

EXPECT_CALL(
*capture_source,
GetAvailableDeviceMediaType(
Expand Down Expand Up @@ -162,6 +153,15 @@ void MockStartPreview(CaptureControllerImpl* capture_controller,
MockAvailableMediaTypes(engine, capture_source.Get(), mock_preview_width,
mock_preview_height);

EXPECT_CALL(
*capture_source.Get(),
SetCurrentDeviceMediaType(
Eq((DWORD)
MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_VIDEO_PREVIEW),
_))
.Times(1)
.WillOnce(Return(S_OK));

EXPECT_CALL(*engine, StartPreview()).Times(1).WillOnce(Return(S_OK));

// Called by destructor
Expand Down