From b79eea57bb42ddf7bace2f923c1bb2bc021ec126 Mon Sep 17 00:00:00 2001 From: Danang Rahmatullah Date: Mon, 19 Aug 2024 15:33:28 +0200 Subject: [PATCH 1/8] Set device media type for video preview explicitly --- .../camera/camera_windows/windows/capture_controller.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/camera/camera_windows/windows/capture_controller.cpp b/packages/camera/camera_windows/windows/capture_controller.cpp index 44df8437437..fab71b1dd24 100644 --- a/packages/camera/camera_windows/windows/capture_controller.cpp +++ b/packages/camera/camera_windows/windows/capture_controller.cpp @@ -476,6 +476,13 @@ HRESULT CaptureControllerImpl::FindBaseMediaTypes() { 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, From d887b8f558fe831f26f6b8e67b5eba7e4d727417 Mon Sep 17 00:00:00 2001 From: Danang Rahmatullah Date: Mon, 19 Aug 2024 20:17:55 +0200 Subject: [PATCH 2/8] Bump camera_windows version and document changes --- packages/camera/camera_windows/CHANGELOG.md | 4 ++++ packages/camera/camera_windows/pubspec.yaml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/camera/camera_windows/CHANGELOG.md b/packages/camera/camera_windows/CHANGELOG.md index 3479db4ae26..8fe3c0dff2d 100644 --- a/packages/camera/camera_windows/CHANGELOG.md +++ b/packages/camera/camera_windows/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.2.6+1 + +* Fixes black bars on camera preview [#122966](https://github.com/flutter/flutter/issues/122966). + ## 0.2.6 * Reverts streaming frame support, as the implementation was incorrect and never diff --git a/packages/camera/camera_windows/pubspec.yaml b/packages/camera/camera_windows/pubspec.yaml index ae1336971a5..a2a9efb321f 100644 --- a/packages/camera/camera_windows/pubspec.yaml +++ b/packages/camera/camera_windows/pubspec.yaml @@ -2,7 +2,7 @@ name: camera_windows description: A Flutter plugin for getting information about and controlling the camera on Windows. repository: https://github.com/flutter/packages/tree/main/packages/camera/camera_windows issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22 -version: 0.2.6 +version: 0.2.6+1 environment: sdk: ^3.3.0 From b01c17cfbb4da951c268f375a9bffddd431d5646 Mon Sep 17 00:00:00 2001 From: Danang Rahmatullah Date: Wed, 21 Aug 2024 20:32:18 +0200 Subject: [PATCH 3/8] Verify usage of SetCurrentDeviceMediaType --- .../windows/test/capture_controller_test.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/camera/camera_windows/windows/test/capture_controller_test.cpp b/packages/camera/camera_windows/windows/test/capture_controller_test.cpp index aa0b5469b85..76f8bb936c5 100644 --- a/packages/camera/camera_windows/windows/test/capture_controller_test.cpp +++ b/packages/camera/camera_windows/windows/test/capture_controller_test.cpp @@ -103,6 +103,15 @@ 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( From 1f17a42b447b4fb0ab1cf84bccfcdfbf6dc8bc14 Mon Sep 17 00:00:00 2001 From: Danang Rahmatullah Date: Thu, 31 Oct 2024 08:08:07 +0100 Subject: [PATCH 4/8] Move SetCurrentDeviceMediaType to StartPreview --- .../windows/capture_controller.cpp | 32 +++++++++++++------ .../windows/capture_controller.h | 1 + .../windows/test/capture_controller_test.cpp | 18 +++++------ 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/packages/camera/camera_windows/windows/capture_controller.cpp b/packages/camera/camera_windows/windows/capture_controller.cpp index fab71b1dd24..4672bc469b8 100644 --- a/packages/camera/camera_windows/windows/capture_controller.cpp +++ b/packages/camera/camera_windows/windows/capture_controller.cpp @@ -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; } @@ -573,15 +570,30 @@ void CaptureControllerImpl::StartPreview() { HRESULT hr = S_OK; + ComPtr source; + hr = capture_engine_->GetSource(&source); + if (FAILED(hr)) { + return OnPreviewStarted(GetCameraResult(hr), + "Failed to initialize video preview"); + } + 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_); diff --git a/packages/camera/camera_windows/windows/capture_controller.h b/packages/camera/camera_windows/windows/capture_controller.h index 8dd541421f7..9fe59b9191d 100644 --- a/packages/camera/camera_windows/windows/capture_controller.h +++ b/packages/camera/camera_windows/windows/capture_controller.h @@ -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); // Stops preview. Called internally on camera reset and dispose. HRESULT StopPreview(); diff --git a/packages/camera/camera_windows/windows/test/capture_controller_test.cpp b/packages/camera/camera_windows/windows/test/capture_controller_test.cpp index 76f8bb936c5..98e775845a6 100644 --- a/packages/camera/camera_windows/windows/test/capture_controller_test.cpp +++ b/packages/camera/camera_windows/windows/test/capture_controller_test.cpp @@ -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( @@ -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 From cdf4f5c6ddb1a3bf91a08bbdff01d4d6738300f4 Mon Sep 17 00:00:00 2001 From: Danang Rahmatullah Date: Thu, 31 Oct 2024 09:12:47 +0100 Subject: [PATCH 5/8] Fix file formatting --- packages/camera/camera_windows/windows/capture_controller.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/camera/camera_windows/windows/capture_controller.cpp b/packages/camera/camera_windows/windows/capture_controller.cpp index 4672bc469b8..7923cdfccf8 100644 --- a/packages/camera/camera_windows/windows/capture_controller.cpp +++ b/packages/camera/camera_windows/windows/capture_controller.cpp @@ -483,8 +483,8 @@ HRESULT CaptureControllerImpl::FindBaseMediaTypes(IMFCaptureSource* source) { // Find base media type for record and photo capture. if (!FindBestMediaType( (DWORD)MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_VIDEO_RECORD, - source, base_capture_media_type_.GetAddressOf(), 0xffffffff, - nullptr, nullptr)) { + source, base_capture_media_type_.GetAddressOf(), 0xffffffff, nullptr, + nullptr)) { return E_FAIL; } From 2445b243c2dd9de136333ce8f38b3f1fc7233ba2 Mon Sep 17 00:00:00 2001 From: Danang Rahmatullah Date: Wed, 13 Nov 2024 12:45:18 +0100 Subject: [PATCH 6/8] Rename overloaded function with suffix --- .../camera/camera_windows/windows/capture_controller.cpp | 7 ++++--- .../camera/camera_windows/windows/capture_controller.h | 5 ++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/camera/camera_windows/windows/capture_controller.cpp b/packages/camera/camera_windows/windows/capture_controller.cpp index 7923cdfccf8..469e417ba3f 100644 --- a/packages/camera/camera_windows/windows/capture_controller.cpp +++ b/packages/camera/camera_windows/windows/capture_controller.cpp @@ -467,10 +467,11 @@ HRESULT CaptureControllerImpl::FindBaseMediaTypes() { return hr; } - return FindBaseMediaTypes(source.Get()); + return FindBaseMediaTypesForSource(source.Get()); } -HRESULT CaptureControllerImpl::FindBaseMediaTypes(IMFCaptureSource* source) { +HRESULT CaptureControllerImpl::FindBaseMediaTypesForSource( + IMFCaptureSource* source) { // Find base media type for previewing. if (!FindBestMediaType( (DWORD)MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_VIDEO_PREVIEW, @@ -579,7 +580,7 @@ void CaptureControllerImpl::StartPreview() { if (!base_preview_media_type_) { // Enumerates mediatypes and finds media type for video capture. - hr = FindBaseMediaTypes(source.Get()); + hr = FindBaseMediaTypesForSource(source.Get()); if (FAILED(hr)) { return OnPreviewStarted(GetCameraResult(hr), "Failed to initialize video preview"); diff --git a/packages/camera/camera_windows/windows/capture_controller.h b/packages/camera/camera_windows/windows/capture_controller.h index 9fe59b9191d..27135dd46bf 100644 --- a/packages/camera/camera_windows/windows/capture_controller.h +++ b/packages/camera/camera_windows/windows/capture_controller.h @@ -180,7 +180,10 @@ 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); + + // Enumerates video_sources media types and finds out best resolution + // for a given source source. + HRESULT FindBaseMediaTypesForSource(IMFCaptureSource* source); // Stops preview. Called internally on camera reset and dispose. HRESULT StopPreview(); From 923fc4d7ffb0c55c698bc2a125bfc016482d4e59 Mon Sep 17 00:00:00 2001 From: Danang Rahmatullah Date: Wed, 13 Nov 2024 14:21:32 +0100 Subject: [PATCH 7/8] Clarify error messages for StartPreview --- packages/camera/camera_windows/windows/capture_controller.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/camera/camera_windows/windows/capture_controller.cpp b/packages/camera/camera_windows/windows/capture_controller.cpp index 469e417ba3f..44675daebec 100644 --- a/packages/camera/camera_windows/windows/capture_controller.cpp +++ b/packages/camera/camera_windows/windows/capture_controller.cpp @@ -575,7 +575,7 @@ void CaptureControllerImpl::StartPreview() { hr = capture_engine_->GetSource(&source); if (FAILED(hr)) { return OnPreviewStarted(GetCameraResult(hr), - "Failed to initialize video preview"); + "Failed to get capture engine source"); } if (!base_preview_media_type_) { @@ -592,7 +592,7 @@ void CaptureControllerImpl::StartPreview() { base_preview_media_type_.Get()); if (FAILED(hr)) { return OnPreviewStarted(GetCameraResult(hr), - "Failed to initialize video preview"); + "Failed to set video preview output format"); } texture_handler_->UpdateTextureSize(preview_frame_width_, From 74cbd438d84ef1ed1f23db7dabe9379c010d1c59 Mon Sep 17 00:00:00 2001 From: stuartmorgan Date: Wed, 13 Nov 2024 08:39:52 -0500 Subject: [PATCH 8/8] Typo fix --- packages/camera/camera_windows/windows/capture_controller.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/camera/camera_windows/windows/capture_controller.h b/packages/camera/camera_windows/windows/capture_controller.h index 27135dd46bf..2280cb3b93f 100644 --- a/packages/camera/camera_windows/windows/capture_controller.h +++ b/packages/camera/camera_windows/windows/capture_controller.h @@ -182,7 +182,7 @@ class CaptureControllerImpl : public CaptureController, HRESULT FindBaseMediaTypes(); // Enumerates video_sources media types and finds out best resolution - // for a given source source. + // for a given source. HRESULT FindBaseMediaTypesForSource(IMFCaptureSource* source); // Stops preview. Called internally on camera reset and dispose.