Skip to content

Commit 18f9c64

Browse files
authored
[camera_windows] Fall back to a physical stream for cameras that reject preferred-stream selectors (#12748)
Fixes [#140014](flutter/flutter#140014) ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 8e058b4 commit 18f9c64

11 files changed

Lines changed: 410 additions & 45 deletions

‎packages/camera/camera_windows/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.3.0+1
2+
3+
* Fixes `CameraException(camera_error, Failed to initialize video preview)` on
4+
multi-stream USB cameras.
5+
16
## 0.3.0
27

38
* Adds support for custom video output path in video recording.

‎packages/camera/camera_windows/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: camera_windows
22
description: A Flutter plugin for getting information about and controlling the camera on Windows.
33
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera_windows
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
5-
version: 0.3.0
5+
version: 0.3.0+1
66

77
environment:
88
sdk: ^3.11.0

‎packages/camera/camera_windows/windows/capture_controller.cpp‎

Lines changed: 67 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
473523
HRESULT 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.

‎packages/camera/camera_windows/windows/capture_controller.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ class CaptureControllerImpl : public CaptureController,
185185
// for a given source.
186186
HRESULT FindBaseMediaTypesForSource(IMFCaptureSource* source);
187187

188+
// Resolves the source stream indices for preview, record and photo, falling
189+
// back to a physical stream for devices that reject the
190+
// MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_* selectors.
191+
void ResolveSourceStreamIndices(IMFCaptureSource* source);
192+
188193
// Stops preview. Called internally on camera reset and dispose.
189194
HRESULT StopPreview();
190195

@@ -231,6 +236,14 @@ class CaptureControllerImpl : public CaptureController,
231236
ComPtr<ID3D11Device> dx11_device_;
232237
ComPtr<IMFMediaType> base_capture_media_type_;
233238
ComPtr<IMFMediaType> base_preview_media_type_;
239+
240+
// Resolved by ResolveSourceStreamIndices().
241+
DWORD preview_source_stream_index_ = static_cast<DWORD>(
242+
MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_VIDEO_PREVIEW);
243+
DWORD record_source_stream_index_ = static_cast<DWORD>(
244+
MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_VIDEO_RECORD);
245+
DWORD photo_source_stream_index_ =
246+
static_cast<DWORD>(MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_PHOTO);
234247
ComPtr<IMFMediaSource> video_source_;
235248
ComPtr<IMFMediaSource> audio_source_;
236249

‎packages/camera/camera_windows/windows/photo_handler.cpp‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ HRESULT BuildMediaTypeForPhotoCapture(IMFMediaType* src_media_type,
5050
}
5151

5252
HRESULT PhotoHandler::InitPhotoSink(IMFCaptureEngine* capture_engine,
53-
IMFMediaType* base_media_type) {
53+
IMFMediaType* base_media_type,
54+
DWORD source_stream_index) {
5455
assert(capture_engine);
5556
assert(base_media_type);
5657

@@ -99,9 +100,8 @@ HRESULT PhotoHandler::InitPhotoSink(IMFCaptureEngine* capture_engine,
99100
}
100101

101102
DWORD photo_sink_stream_index;
102-
hr = photo_sink_->AddStream(
103-
(DWORD)MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_PHOTO,
104-
photo_media_type.Get(), nullptr, &photo_sink_stream_index);
103+
hr = photo_sink_->AddStream(source_stream_index, photo_media_type.Get(),
104+
nullptr, &photo_sink_stream_index);
105105
if (FAILED(hr)) {
106106
photo_sink_ = nullptr;
107107
return hr;
@@ -118,14 +118,16 @@ HRESULT PhotoHandler::InitPhotoSink(IMFCaptureEngine* capture_engine,
118118

119119
HRESULT PhotoHandler::TakePhoto(const std::string& file_path,
120120
IMFCaptureEngine* capture_engine,
121-
IMFMediaType* base_media_type) {
121+
IMFMediaType* base_media_type,
122+
DWORD source_stream_index) {
122123
assert(!file_path.empty());
123124
assert(capture_engine);
124125
assert(base_media_type);
125126

126127
file_path_ = file_path;
127128

128-
HRESULT hr = InitPhotoSink(capture_engine, base_media_type);
129+
HRESULT hr =
130+
InitPhotoSink(capture_engine, base_media_type, source_stream_index);
129131
if (FAILED(hr)) {
130132
return hr;
131133
}

‎packages/camera/camera_windows/windows/photo_handler.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ class PhotoHandler {
4747
// base_media_type: A pointer to base media type used as a base
4848
// for the actual photo capture media type.
4949
// file_path: A string that hold file path for photo capture.
50+
// source_stream_index: The source stream to connect to the photo sink.
5051
HRESULT TakePhoto(const std::string& file_path,
5152
IMFCaptureEngine* capture_engine,
52-
IMFMediaType* base_media_type);
53+
IMFMediaType* base_media_type, DWORD source_stream_index);
5354

5455
// Set the photo handler recording state to: kIdle.
5556
void OnPhotoTaken();
@@ -68,7 +69,8 @@ class PhotoHandler {
6869
private:
6970
// Initializes record sink for video file capture.
7071
HRESULT InitPhotoSink(IMFCaptureEngine* capture_engine,
71-
IMFMediaType* base_media_type);
72+
IMFMediaType* base_media_type,
73+
DWORD source_stream_index);
7274

7375
std::string file_path_;
7476
PhotoState photo_state_ = PhotoState::kNotStarted;

‎packages/camera/camera_windows/windows/preview_handler.cpp‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ HRESULT BuildMediaTypeForVideoPreview(IMFMediaType* src_media_type,
4949
return hr;
5050
}
5151

52-
HRESULT PreviewHandler::InitPreviewSink(
53-
IMFCaptureEngine* capture_engine, IMFMediaType* base_media_type,
54-
CaptureEngineListener* sample_callback) {
52+
HRESULT PreviewHandler::InitPreviewSink(IMFCaptureEngine* capture_engine,
53+
IMFMediaType* base_media_type,
54+
CaptureEngineListener* sample_callback,
55+
DWORD source_stream_index) {
5556
assert(capture_engine);
5657
assert(base_media_type);
5758
assert(sample_callback);
@@ -94,9 +95,8 @@ HRESULT PreviewHandler::InitPreviewSink(
9495
}
9596

9697
DWORD preview_sink_stream_index;
97-
hr = preview_sink_->AddStream(
98-
(DWORD)MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_VIDEO_PREVIEW,
99-
preview_media_type.Get(), nullptr, &preview_sink_stream_index);
98+
hr = preview_sink_->AddStream(source_stream_index, preview_media_type.Get(),
99+
nullptr, &preview_sink_stream_index);
100100

101101
if (FAILED(hr)) {
102102
return hr;
@@ -115,12 +115,13 @@ HRESULT PreviewHandler::InitPreviewSink(
115115

116116
HRESULT PreviewHandler::StartPreview(IMFCaptureEngine* capture_engine,
117117
IMFMediaType* base_media_type,
118-
CaptureEngineListener* sample_callback) {
118+
CaptureEngineListener* sample_callback,
119+
DWORD source_stream_index) {
119120
assert(capture_engine);
120121
assert(base_media_type);
121122

122-
HRESULT hr =
123-
InitPreviewSink(capture_engine, base_media_type, sample_callback);
123+
HRESULT hr = InitPreviewSink(capture_engine, base_media_type, sample_callback,
124+
source_stream_index);
124125

125126
if (FAILED(hr)) {
126127
return hr;

‎packages/camera/camera_windows/windows/preview_handler.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ class PreviewHandler {
5252
// for the actual video capture media type.
5353
// sample_callback: A pointer to capture engine listener.
5454
// This is set as sample callback for preview sink.
55+
// source_stream_index: The source stream to connect to the preview sink.
5556
HRESULT StartPreview(IMFCaptureEngine* capture_engine,
5657
IMFMediaType* base_media_type,
57-
CaptureEngineListener* sample_callback);
58+
CaptureEngineListener* sample_callback,
59+
DWORD source_stream_index);
5860

5961
// Stops existing recording.
6062
//
@@ -90,7 +92,8 @@ class PreviewHandler {
9092
// Initializes record sink for video file capture.
9193
HRESULT InitPreviewSink(IMFCaptureEngine* capture_engine,
9294
IMFMediaType* base_media_type,
93-
CaptureEngineListener* sample_callback);
95+
CaptureEngineListener* sample_callback,
96+
DWORD source_stream_index);
9497

9598
PreviewState preview_state_ = PreviewState::kNotStarted;
9699
ComPtr<IMFCapturePreviewSink> preview_sink_;

‎packages/camera/camera_windows/windows/record_handler.cpp‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ inline HRESULT SetAudioBitrate(IMFMediaType* pType, UINT32 bitrate) {
131131
}
132132

133133
HRESULT RecordHandler::InitRecordSink(IMFCaptureEngine* capture_engine,
134-
IMFMediaType* base_media_type) {
134+
IMFMediaType* base_media_type,
135+
DWORD source_stream_index) {
135136
assert(!file_path_.empty());
136137
assert(capture_engine);
137138
assert(base_media_type);
@@ -189,9 +190,9 @@ HRESULT RecordHandler::InitRecordSink(IMFCaptureEngine* capture_engine,
189190
}
190191

191192
DWORD video_record_sink_stream_index;
192-
hr = record_sink_->AddStream(
193-
(DWORD)MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_VIDEO_RECORD,
194-
video_record_media_type.Get(), nullptr, &video_record_sink_stream_index);
193+
hr = record_sink_->AddStream(source_stream_index,
194+
video_record_media_type.Get(), nullptr,
195+
&video_record_sink_stream_index);
195196
if (FAILED(hr)) {
196197
return hr;
197198
}
@@ -228,7 +229,8 @@ HRESULT RecordHandler::InitRecordSink(IMFCaptureEngine* capture_engine,
228229

229230
HRESULT RecordHandler::StartRecord(const std::string& file_path,
230231
IMFCaptureEngine* capture_engine,
231-
IMFMediaType* base_media_type) {
232+
IMFMediaType* base_media_type,
233+
DWORD source_stream_index) {
232234
assert(!file_path.empty());
233235
assert(capture_engine);
234236
assert(base_media_type);
@@ -237,7 +239,8 @@ HRESULT RecordHandler::StartRecord(const std::string& file_path,
237239
recording_start_timestamp_us_ = -1;
238240
recording_duration_us_ = 0;
239241

240-
HRESULT hr = InitRecordSink(capture_engine, base_media_type);
242+
HRESULT hr =
243+
InitRecordSink(capture_engine, base_media_type, source_stream_index);
241244
if (FAILED(hr)) {
242245
return hr;
243246
}

‎packages/camera/camera_windows/windows/record_handler.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ class RecordHandler {
4848
// the actual recording.
4949
// base_media_type: A pointer to base media type used as a base
5050
// for the actual video capture media type.
51+
// source_stream_index: The source stream to connect to the record sink.
5152
HRESULT StartRecord(const std::string& file_path,
5253
IMFCaptureEngine* capture_engine,
53-
IMFMediaType* base_media_type);
54+
IMFMediaType* base_media_type, DWORD source_stream_index);
5455

5556
// Stops existing recording.
5657
//
@@ -83,7 +84,8 @@ class RecordHandler {
8384
private:
8485
// Initializes record sink for video file capture.
8586
HRESULT InitRecordSink(IMFCaptureEngine* capture_engine,
86-
IMFMediaType* base_media_type);
87+
IMFMediaType* base_media_type,
88+
DWORD source_stream_index);
8789

8890
const PlatformMediaSettings media_settings_;
8991
int64_t recording_start_timestamp_us_ = -1;

0 commit comments

Comments
 (0)