Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 257eacb

Browse files
authored
[camera_windows] Improve several error handling scenarios (#6149)
1 parent e183aec commit 257eacb

File tree

14 files changed

+575
-130
lines changed

14 files changed

+575
-130
lines changed

packages/camera/camera_windows/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.1.0+5
2+
3+
* Fixes bugs in in error handling.
4+
15
## 0.1.0+4
26

37
* Allows retrying camera initialization after error.

packages/camera/camera_windows/lib/camera_windows.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class CameraWindows extends CameraPlatform {
2222
final MethodChannel pluginChannel =
2323
const MethodChannel('plugins.flutter.io/camera_windows');
2424

25-
/// Camera specific method channels to allow comminicating with specific cameras.
25+
/// Camera specific method channels to allow communicating with specific cameras.
2626
final Map<int, MethodChannel> _cameraChannels = <int, MethodChannel>{};
2727

2828
/// The controller that broadcasts events coming from handleCameraMethodCall

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/plugins/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.1.0+4
5+
version: 0.1.0+5
66

77
environment:
88
sdk: ">=2.12.0 <3.0.0"

packages/camera/camera_windows/windows/camera.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ bool CameraImpl::HasPendingResultByType(PendingResultType type) const {
8585
}
8686

8787
void CameraImpl::SendErrorForPendingResults(const std::string& error_code,
88-
const std::string& descripion) {
88+
const std::string& description) {
8989
for (const auto& pending_result : pending_results_) {
90-
pending_result.second->Error(error_code, descripion);
90+
pending_result.second->Error(error_code, description);
9191
}
9292
pending_results_.clear();
9393
}

packages/camera/camera_windows/windows/camera.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ class CameraImpl : public Camera {
139139
// error ID and description. Pending results are cleared in the process.
140140
//
141141
// error_code: A string error code describing the error.
142-
// error_message: A user-readable error message (optional).
142+
// description: A user-readable error message (optional).
143143
void SendErrorForPendingResults(const std::string& error_code,
144-
const std::string& descripion);
144+
const std::string& description);
145145

146146
// Called when camera is disposed.
147147
// Sends camera closing message to the cameras method channel.

packages/camera/camera_windows/windows/capture_controller.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,16 @@ HRESULT CaptureControllerImpl::CreateCaptureEngine() {
237237
return hr;
238238
}
239239

240+
// Check MF_CAPTURE_ENGINE_INITIALIZED event handling
241+
// for response process.
240242
hr = capture_engine_->Initialize(capture_engine_callback_handler_.Get(),
241243
attributes.Get(), audio_source_.Get(),
242244
video_source_.Get());
243245
return hr;
244246
}
245247

246248
void CaptureControllerImpl::ResetCaptureController() {
247-
if (record_handler_) {
249+
if (record_handler_ && record_handler_->CanStop()) {
248250
if (record_handler_->IsContinuousRecording()) {
249251
StopRecord();
250252
} else if (record_handler_->IsTimedRecording()) {
@@ -391,7 +393,7 @@ uint32_t CaptureControllerImpl::GetMaxPreviewHeight() const {
391393
}
392394
}
393395

394-
// Finds best mediat type for given source stream index and max height;
396+
// Finds best media type for given source stream index and max height;
395397
bool FindBestMediaType(DWORD source_stream_index, IMFCaptureSource* source,
396398
IMFMediaType** target_media_type, uint32_t max_height,
397399
uint32_t* target_frame_width,
@@ -533,8 +535,6 @@ void CaptureControllerImpl::StopRecord() {
533535
// Check MF_CAPTURE_ENGINE_RECORD_STOPPED event handling for response
534536
// process.
535537
if (!record_handler_->StopRecord(capture_engine_.Get())) {
536-
// Destroy record handler on error cases to make sure state is resetted.
537-
record_handler_ = nullptr;
538538
return OnRecordStopped(false, "Failed to stop video recording");
539539
}
540540
}
@@ -578,6 +578,8 @@ void CaptureControllerImpl::StartPreview() {
578578
texture_handler_->UpdateTextureSize(preview_frame_width_,
579579
preview_frame_height_);
580580

581+
// TODO(loic-sharma): This does not handle duplicate calls properly.
582+
// See: https://github.com/flutter/flutter/issues/108404
581583
if (!preview_handler_) {
582584
preview_handler_ = std::make_unique<PreviewHandler>();
583585
} else if (preview_handler_->IsInitialized()) {
@@ -605,7 +607,7 @@ void CaptureControllerImpl::StartPreview() {
605607
void CaptureControllerImpl::StopPreview() {
606608
assert(capture_engine_);
607609

608-
if (!IsInitialized() && !preview_handler_) {
610+
if (!IsInitialized() || !preview_handler_) {
609611
return;
610612
}
611613

@@ -619,7 +621,7 @@ void CaptureControllerImpl::StopPreview() {
619621
void CaptureControllerImpl::PausePreview() {
620622
assert(capture_controller_listener_);
621623

622-
if (!preview_handler_ && !preview_handler_->IsInitialized()) {
624+
if (!preview_handler_ || !preview_handler_->IsInitialized()) {
623625
return capture_controller_listener_->OnPausePreviewFailed(
624626
"Preview not started");
625627
}
@@ -638,7 +640,7 @@ void CaptureControllerImpl::PausePreview() {
638640
void CaptureControllerImpl::ResumePreview() {
639641
assert(capture_controller_listener_);
640642

641-
if (!preview_handler_ && !preview_handler_->IsInitialized()) {
643+
if (!preview_handler_ || !preview_handler_->IsInitialized()) {
642644
return capture_controller_listener_->OnResumePreviewFailed(
643645
"Preview not started");
644646
}
@@ -722,6 +724,13 @@ void CaptureControllerImpl::OnPicture(bool success, const std::string& error) {
722724
void CaptureControllerImpl::OnCaptureEngineInitialized(
723725
bool success, const std::string& error) {
724726
if (capture_controller_listener_) {
727+
if (!success) {
728+
capture_controller_listener_->OnCreateCaptureEngineFailed(
729+
"Failed to initialize capture engine");
730+
ResetCaptureController();
731+
return;
732+
}
733+
725734
// Create texture handler and register new texture.
726735
texture_handler_ = std::make_unique<TextureHandler>(texture_registrar_);
727736

@@ -848,7 +857,7 @@ void CaptureControllerImpl::UpdateCaptureTime(uint64_t capture_time_us) {
848857
}
849858

850859
if (preview_handler_ && preview_handler_->IsStarting()) {
851-
// Informs that first frame is captured succeffully and preview has
860+
// Informs that first frame is captured successfully and preview has
852861
// started.
853862
OnPreviewStarted(true, "");
854863
}

packages/camera/camera_windows/windows/photo_handler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class PhotoHandler {
5151
bool TakePhoto(const std::string& file_path, IMFCaptureEngine* capture_engine,
5252
IMFMediaType* base_media_type);
5353

54-
// Set the photo handler recording state to: kIdel.
54+
// Set the photo handler recording state to: kIdle.
5555
void OnPhotoTaken();
5656

5757
// Returns true if photo state is kIdle.

packages/camera/camera_windows/windows/preview_handler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class PreviewHandler {
7575

7676
// Returns true if preview state is running or paused.
7777
bool IsInitialized() const {
78-
return preview_state_ == PreviewState::kRunning &&
78+
return preview_state_ == PreviewState::kRunning ||
7979
preview_state_ == PreviewState::kPaused;
8080
}
8181

packages/camera/camera_windows/windows/record_handler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ void RecordHandler::OnRecordStopped() {
238238
recording_duration_us_ = 0;
239239
max_video_duration_ms_ = -1;
240240
recording_state_ = RecordState::kNotStarted;
241+
type_ = RecordingType::kNone;
241242
}
242243
}
243244

packages/camera/camera_windows/windows/record_handler.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ namespace camera_windows {
1616
using Microsoft::WRL::ComPtr;
1717

1818
enum class RecordingType {
19+
// Camera is not recording.
20+
kNone,
1921
// Recording continues until it is stopped with a separate stop command.
2022
kContinuous,
2123
// Recording stops automatically after requested record time is passed.
@@ -109,7 +111,7 @@ class RecordHandler {
109111
uint64_t recording_duration_us_ = 0;
110112
std::string file_path_;
111113
RecordState recording_state_ = RecordState::kNotStarted;
112-
RecordingType type_;
114+
RecordingType type_ = RecordingType::kNone;
113115
ComPtr<IMFCaptureRecordSink> record_sink_;
114116
};
115117

0 commit comments

Comments
 (0)