diff --git a/packages/camera/camera_windows/lib/src/messages.g.dart b/packages/camera/camera_windows/lib/src/messages.g.dart index 9930549183bb..c91d28ceac03 100644 --- a/packages/camera/camera_windows/lib/src/messages.g.dart +++ b/packages/camera/camera_windows/lib/src/messages.g.dart @@ -1,7 +1,7 @@ // Copyright 2013 The Flutter Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// Autogenerated from Pigeon (v22.6.0), do not edit directly. +// Autogenerated from Pigeon (v22.6.2), do not edit directly. // See also: https://pub.dev/packages/pigeon // ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import, no_leading_underscores_for_local_identifiers diff --git a/packages/camera/camera_windows/pigeons/messages.dart b/packages/camera/camera_windows/pigeons/messages.dart index d0970af423ac..94d8af9230c9 100644 --- a/packages/camera/camera_windows/pigeons/messages.dart +++ b/packages/camera/camera_windows/pigeons/messages.dart @@ -71,11 +71,9 @@ abstract class CameraApi { String stopVideoRecording(int cameraId); /// Starts the image stream for the given camera. - @async void startImageStream(int cameraId); /// Stops the image stream for the given camera. - @async void stopImageStream(int cameraId); /// Starts the preview stream for the given camera. diff --git a/packages/camera/camera_windows/windows/camera.h b/packages/camera/camera_windows/windows/camera.h index 2a44ad2ccf62..44e9fcde5883 100644 --- a/packages/camera/camera_windows/windows/camera.h +++ b/packages/camera/camera_windows/windows/camera.h @@ -22,8 +22,6 @@ enum class PendingResultType { kTakePicture, kStartRecord, kStopRecord, - kStartStream, - kStopStream, kPausePreview, kResumePreview, }; diff --git a/packages/camera/camera_windows/windows/camera_plugin.cpp b/packages/camera/camera_windows/windows/camera_plugin.cpp index 4b2abe39d6ac..fbfe13acc4c6 100644 --- a/packages/camera/camera_windows/windows/camera_plugin.cpp +++ b/packages/camera/camera_windows/windows/camera_plugin.cpp @@ -369,51 +369,35 @@ void CameraPlugin::StopVideoRecording( } } -void CameraPlugin::StartImageStream( - int64_t camera_id, - std::function reply)> result) { - // check if request already exists +std::optional CameraPlugin::StartImageStream(int64_t camera_id) { Camera* camera = GetCameraByCameraId(camera_id); if (!camera) { - return result(FlutterError("camera_error", "Camera not created")); - } - if (camera->HasPendingResultByType(PendingResultType::kStartStream)) { - return result( - FlutterError("camera_error", "Pending start stream request exists")); + return FlutterError("camera_error", "Camera not created"); } if (!event_sink) { - return result(FlutterError("camera_error", - "Unable to make event channel from windows")); + return FlutterError("camera_error", + "Unable to make event channel from windows"); } - if (camera->AddPendingVoidResult(PendingResultType::kStartStream, - std::move(result))) { - CaptureController* cc = camera->GetCaptureController(); - assert(cc); - cc->StartImageStream(std::move(event_sink)); - } + CaptureController* cc = camera->GetCaptureController(); + assert(cc); + cc->StartImageStream(std::move(event_sink)); + + return std::nullopt; } -void CameraPlugin::StopImageStream( - int64_t camera_id, - std::function reply)> result) { - // check if request already exists +std::optional CameraPlugin::StopImageStream(int64_t camera_id) { Camera* camera = GetCameraByCameraId(camera_id); if (!camera) { - return result(FlutterError("camera_error", "Camera not created")); - } - if (camera->HasPendingResultByType(PendingResultType::kStopStream)) { - return result( - FlutterError("camera_error", "Pending stop stream request exists")); + return FlutterError("camera_error", "Camera not created"); } - if (camera->AddPendingVoidResult(PendingResultType::kStopStream, - std::move(result))) { - CaptureController* cc = camera->GetCaptureController(); - assert(cc); - cc->StopImageStream(); - } + CaptureController* cc = camera->GetCaptureController(); + assert(cc); + cc->StopImageStream(); + + return std::nullopt; } void CameraPlugin::TakePicture( diff --git a/packages/camera/camera_windows/windows/camera_plugin.h b/packages/camera/camera_windows/windows/camera_plugin.h index f77711402bb0..d22049613b3d 100644 --- a/packages/camera/camera_windows/windows/camera_plugin.h +++ b/packages/camera/camera_windows/windows/camera_plugin.h @@ -70,12 +70,8 @@ class CameraPlugin : public flutter::Plugin, void StopVideoRecording( int64_t camera_id, std::function reply)> result) override; - void StartImageStream( - int64_t camera_id, - std::function reply)> result) override; - void StopImageStream( - int64_t camera_id, - std::function reply)> result) override; + std::optional StartImageStream(int64_t camera_id) override; + std::optional StopImageStream(int64_t camera_id) override; void TakePicture( int64_t camera_id, std::function reply)> result) override; diff --git a/packages/camera/camera_windows/windows/messages.g.cpp b/packages/camera/camera_windows/windows/messages.g.cpp index 724d42037480..d503a449ceff 100644 --- a/packages/camera/camera_windows/windows/messages.g.cpp +++ b/packages/camera/camera_windows/windows/messages.g.cpp @@ -1,7 +1,7 @@ // Copyright 2013 The Flutter Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// Autogenerated from Pigeon (v22.6.0), do not edit directly. +// Autogenerated from Pigeon (v22.6.2), do not edit directly. // See also: https://pub.dev/packages/pigeon #undef _HAS_EXCEPTIONS @@ -516,16 +516,15 @@ void CameraApi::SetUp(flutter::BinaryMessenger* binary_messenger, return; } const int64_t camera_id_arg = encodable_camera_id_arg.LongValue(); - api->StartImageStream( - camera_id_arg, [reply](std::optional&& output) { - if (output.has_value()) { - reply(WrapError(output.value())); - return; - } - EncodableList wrapped; - wrapped.push_back(EncodableValue()); - reply(EncodableValue(std::move(wrapped))); - }); + std::optional output = + api->StartImageStream(camera_id_arg); + if (output.has_value()) { + reply(WrapError(output.value())); + return; + } + EncodableList wrapped; + wrapped.push_back(EncodableValue()); + reply(EncodableValue(std::move(wrapped))); } catch (const std::exception& exception) { reply(WrapError(exception.what())); } @@ -552,16 +551,15 @@ void CameraApi::SetUp(flutter::BinaryMessenger* binary_messenger, return; } const int64_t camera_id_arg = encodable_camera_id_arg.LongValue(); - api->StopImageStream( - camera_id_arg, [reply](std::optional&& output) { - if (output.has_value()) { - reply(WrapError(output.value())); - return; - } - EncodableList wrapped; - wrapped.push_back(EncodableValue()); - reply(EncodableValue(std::move(wrapped))); - }); + std::optional output = + api->StopImageStream(camera_id_arg); + if (output.has_value()) { + reply(WrapError(output.value())); + return; + } + EncodableList wrapped; + wrapped.push_back(EncodableValue()); + reply(EncodableValue(std::move(wrapped))); } catch (const std::exception& exception) { reply(WrapError(exception.what())); } diff --git a/packages/camera/camera_windows/windows/messages.g.h b/packages/camera/camera_windows/windows/messages.g.h index 9dcb6fab7d14..621466fe031a 100644 --- a/packages/camera/camera_windows/windows/messages.g.h +++ b/packages/camera/camera_windows/windows/messages.g.h @@ -1,7 +1,7 @@ // Copyright 2013 The Flutter Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// Autogenerated from Pigeon (v22.6.0), do not edit directly. +// Autogenerated from Pigeon (v22.6.2), do not edit directly. // See also: https://pub.dev/packages/pigeon #ifndef PIGEON_MESSAGES_G_H_ @@ -190,13 +190,9 @@ class CameraApi { int64_t camera_id, std::function reply)> result) = 0; // Starts the image stream for the given camera. - virtual void StartImageStream( - int64_t camera_id, - std::function reply)> result) = 0; + virtual std::optional StartImageStream(int64_t camera_id) = 0; // Stops the image stream for the given camera. - virtual void StopImageStream( - int64_t camera_id, - std::function reply)> result) = 0; + virtual std::optional StopImageStream(int64_t camera_id) = 0; // Starts the preview stream for the given camera. virtual void PausePreview( int64_t camera_id, diff --git a/packages/camera/camera_windows/windows/test/camera_plugin_test.cpp b/packages/camera/camera_windows/windows/test/camera_plugin_test.cpp index 6bd59dbde8a4..e2ecd42d3135 100644 --- a/packages/camera/camera_windows/windows/test/camera_plugin_test.cpp +++ b/packages/camera/camera_windows/windows/test/camera_plugin_test.cpp @@ -29,6 +29,8 @@ using ::testing::EndsWith; using ::testing::Eq; using ::testing::Pointee; using ::testing::Return; +using ::testing::Optional; +using ::testing::Property; void MockInitCamera(MockCamera* camera, bool success) { EXPECT_CALL(*camera, @@ -366,34 +368,14 @@ TEST(CameraPlugin, StartImageStreamHandlerCallsStartImageStream) { return cam->camera_id_ == camera_id; }); - EXPECT_CALL(*camera, - HasPendingResultByType(Eq(PendingResultType::kStartStream))) - .Times(1) - .WillOnce(Return(false)); - - EXPECT_CALL(*camera, - AddPendingVoidResult(Eq(PendingResultType::kStartStream), _)) - .Times(1) - .WillOnce([cam = camera.get()]( - PendingResultType type, - std::function)> result) { - cam->pending_void_result_ = std::move(result); - return true; - }); - EXPECT_CALL(*camera, GetCaptureController) .Times(1) .WillOnce([cam = camera.get()]() { - assert(cam->pending_void_result_); return cam->capture_controller_.get(); }); EXPECT_CALL(*capture_controller, StartImageStream) - .Times(1) - .WillOnce([cam = camera.get()]() { - assert(cam->pending_void_result_); - return cam->pending_void_result_(std::nullopt); - }); + .Times(1); camera->camera_id_ = mock_camera_id; camera->capture_controller_ = std::move(capture_controller); @@ -402,24 +384,14 @@ TEST(CameraPlugin, StartImageStreamHandlerCallsStartImageStream) { std::make_unique().get(), std::make_unique()); - // Add mocked camera to plugins camera list. - plugin.AddCamera(std::move(camera)); - // Set the event sink to a mocked event sink. auto mock_event_sink = std::make_unique(); plugin.SetEventSink(std::move(mock_event_sink)); - bool result_called = false; - std::function)> start_image_stream_result = - [&result_called](std::optional reply) { - EXPECT_FALSE(result_called); // Ensure only one reply call. - result_called = true; - EXPECT_FALSE(reply); - }; - - plugin.StartImageStream(mock_camera_id, std::move(start_image_stream_result)); + // Add mocked camera to plugins camera list. + plugin.AddCamera(std::move(camera)); - EXPECT_TRUE(result_called); + EXPECT_EQ(plugin.StartImageStream(mock_camera_id), std::nullopt); } TEST(CameraPlugin, StartImageStreamHandlerErrorOnInvalidCameraId) { @@ -452,18 +424,7 @@ TEST(CameraPlugin, StartImageStreamHandlerErrorOnInvalidCameraId) { // Add mocked camera to plugins camera list. plugin.AddCamera(std::move(camera)); - bool result_called = false; - std::function)> start_image_stream_result = - [&result_called](std::optional reply) { - EXPECT_FALSE(result_called); // Ensure only one reply call. - result_called = true; - EXPECT_TRUE(reply); - }; - - plugin.StartImageStream(missing_camera_id, - std::move(start_image_stream_result)); - - EXPECT_TRUE(result_called); + EXPECT_THAT(plugin.StartImageStream(missing_camera_id), Optional(Property("code", &FlutterError::code, "camera_error"))); } TEST(CameraPlugin, StopImageStreamHandlerCallsStopImageStream) { @@ -481,34 +442,14 @@ TEST(CameraPlugin, StopImageStreamHandlerCallsStopImageStream) { return cam->camera_id_ == camera_id; }); - EXPECT_CALL(*camera, - HasPendingResultByType(Eq(PendingResultType::kStopStream))) - .Times(1) - .WillOnce(Return(false)); - - EXPECT_CALL(*camera, - AddPendingVoidResult(Eq(PendingResultType::kStopStream), _)) - .Times(1) - .WillOnce([cam = camera.get()]( - PendingResultType type, - std::function)> result) { - cam->pending_void_result_ = std::move(result); - return true; - }); - EXPECT_CALL(*camera, GetCaptureController) .Times(1) .WillOnce([cam = camera.get()]() { - assert(cam->pending_void_result_); return cam->capture_controller_.get(); }); EXPECT_CALL(*capture_controller, StopImageStream) - .Times(1) - .WillOnce([cam = camera.get()]() { - assert(cam->pending_void_result_); - return cam->pending_void_result_(std::nullopt); - }); + .Times(1); camera->camera_id_ = mock_camera_id; camera->capture_controller_ = std::move(capture_controller); @@ -520,17 +461,7 @@ TEST(CameraPlugin, StopImageStreamHandlerCallsStopImageStream) { // Add mocked camera to plugins camera list. plugin.AddCamera(std::move(camera)); - bool result_called = false; - std::function)> stop_image_stream_result = - [&result_called](std::optional reply) { - EXPECT_FALSE(result_called); // Ensure only one reply call. - result_called = true; - EXPECT_FALSE(reply); - }; - - plugin.StopImageStream(mock_camera_id, std::move(stop_image_stream_result)); - - EXPECT_TRUE(result_called); + EXPECT_EQ(plugin.StopImageStream(mock_camera_id), std::nullopt); } TEST(CameraPlugin, StopImageStreamHandlerErrorOnInvalidCameraId) { @@ -563,18 +494,7 @@ TEST(CameraPlugin, StopImageStreamHandlerErrorOnInvalidCameraId) { // Add mocked camera to plugins camera list. plugin.AddCamera(std::move(camera)); - bool result_called = false; - std::function)> stop_image_stream_result = - [&result_called](std::optional reply) { - EXPECT_FALSE(result_called); // Ensure only one reply call. - result_called = true; - EXPECT_TRUE(reply); - }; - - plugin.StopImageStream(missing_camera_id, - std::move(stop_image_stream_result)); - - EXPECT_TRUE(result_called); + EXPECT_THAT(plugin.StopImageStream(missing_camera_id), Optional(Property("code", &FlutterError::code, "camera_error"))); } TEST(CameraPlugin, InitializeHandlerErrorOnInvalidCameraId) {