Skip to content

Commit 653155a

Browse files
committed
Make {Start,Stop}ImageStream synchronous
The logic is synchronous, so there is no need to have the `@async` annotation here. Addresses: flutter#7067 (comment)
1 parent 5e8c2db commit 653155a

File tree

8 files changed

+51
-161
lines changed

8 files changed

+51
-161
lines changed

packages/camera/camera_windows/lib/src/messages.g.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2013 The Flutter Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4-
// Autogenerated from Pigeon (v22.6.0), do not edit directly.
4+
// Autogenerated from Pigeon (v22.6.2), do not edit directly.
55
// See also: https://pub.dev/packages/pigeon
66
// 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
77

packages/camera/camera_windows/pigeons/messages.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,9 @@ abstract class CameraApi {
7171
String stopVideoRecording(int cameraId);
7272

7373
/// Starts the image stream for the given camera.
74-
@async
7574
void startImageStream(int cameraId);
7675

7776
/// Stops the image stream for the given camera.
78-
@async
7977
void stopImageStream(int cameraId);
8078

8179
/// Starts the preview stream for the given camera.

packages/camera/camera_windows/windows/camera.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ enum class PendingResultType {
2222
kTakePicture,
2323
kStartRecord,
2424
kStopRecord,
25-
kStartStream,
26-
kStopStream,
2725
kPausePreview,
2826
kResumePreview,
2927
};

packages/camera/camera_windows/windows/camera_plugin.cpp

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -369,51 +369,35 @@ void CameraPlugin::StopVideoRecording(
369369
}
370370
}
371371

372-
void CameraPlugin::StartImageStream(
373-
int64_t camera_id,
374-
std::function<void(std::optional<FlutterError> reply)> result) {
375-
// check if request already exists
372+
std::optional<FlutterError> CameraPlugin::StartImageStream(int64_t camera_id) {
376373
Camera* camera = GetCameraByCameraId(camera_id);
377374
if (!camera) {
378-
return result(FlutterError("camera_error", "Camera not created"));
379-
}
380-
if (camera->HasPendingResultByType(PendingResultType::kStartStream)) {
381-
return result(
382-
FlutterError("camera_error", "Pending start stream request exists"));
375+
return FlutterError("camera_error", "Camera not created");
383376
}
384377

385378
if (!event_sink) {
386-
return result(FlutterError("camera_error",
387-
"Unable to make event channel from windows"));
379+
return FlutterError("camera_error",
380+
"Unable to make event channel from windows");
388381
}
389382

390-
if (camera->AddPendingVoidResult(PendingResultType::kStartStream,
391-
std::move(result))) {
392-
CaptureController* cc = camera->GetCaptureController();
393-
assert(cc);
394-
cc->StartImageStream(std::move(event_sink));
395-
}
383+
CaptureController* cc = camera->GetCaptureController();
384+
assert(cc);
385+
cc->StartImageStream(std::move(event_sink));
386+
387+
return std::nullopt;
396388
}
397389

398-
void CameraPlugin::StopImageStream(
399-
int64_t camera_id,
400-
std::function<void(std::optional<FlutterError> reply)> result) {
401-
// check if request already exists
390+
std::optional<FlutterError> CameraPlugin::StopImageStream(int64_t camera_id) {
402391
Camera* camera = GetCameraByCameraId(camera_id);
403392
if (!camera) {
404-
return result(FlutterError("camera_error", "Camera not created"));
405-
}
406-
if (camera->HasPendingResultByType(PendingResultType::kStopStream)) {
407-
return result(
408-
FlutterError("camera_error", "Pending stop stream request exists"));
393+
return FlutterError("camera_error", "Camera not created");
409394
}
410395

411-
if (camera->AddPendingVoidResult(PendingResultType::kStopStream,
412-
std::move(result))) {
413-
CaptureController* cc = camera->GetCaptureController();
414-
assert(cc);
415-
cc->StopImageStream();
416-
}
396+
CaptureController* cc = camera->GetCaptureController();
397+
assert(cc);
398+
cc->StopImageStream();
399+
400+
return std::nullopt;
417401
}
418402

419403
void CameraPlugin::TakePicture(

packages/camera/camera_windows/windows/camera_plugin.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,8 @@ class CameraPlugin : public flutter::Plugin,
7070
void StopVideoRecording(
7171
int64_t camera_id,
7272
std::function<void(ErrorOr<std::string> reply)> result) override;
73-
void StartImageStream(
74-
int64_t camera_id,
75-
std::function<void(std::optional<FlutterError> reply)> result) override;
76-
void StopImageStream(
77-
int64_t camera_id,
78-
std::function<void(std::optional<FlutterError> reply)> result) override;
73+
std::optional<FlutterError> StartImageStream(int64_t camera_id) override;
74+
std::optional<FlutterError> StopImageStream(int64_t camera_id) override;
7975
void TakePicture(
8076
int64_t camera_id,
8177
std::function<void(ErrorOr<std::string> reply)> result) override;

packages/camera/camera_windows/windows/messages.g.cpp

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2013 The Flutter Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4-
// Autogenerated from Pigeon (v22.6.0), do not edit directly.
4+
// Autogenerated from Pigeon (v22.6.2), do not edit directly.
55
// See also: https://pub.dev/packages/pigeon
66

77
#undef _HAS_EXCEPTIONS
@@ -516,16 +516,15 @@ void CameraApi::SetUp(flutter::BinaryMessenger* binary_messenger,
516516
return;
517517
}
518518
const int64_t camera_id_arg = encodable_camera_id_arg.LongValue();
519-
api->StartImageStream(
520-
camera_id_arg, [reply](std::optional<FlutterError>&& output) {
521-
if (output.has_value()) {
522-
reply(WrapError(output.value()));
523-
return;
524-
}
525-
EncodableList wrapped;
526-
wrapped.push_back(EncodableValue());
527-
reply(EncodableValue(std::move(wrapped)));
528-
});
519+
std::optional<FlutterError> output =
520+
api->StartImageStream(camera_id_arg);
521+
if (output.has_value()) {
522+
reply(WrapError(output.value()));
523+
return;
524+
}
525+
EncodableList wrapped;
526+
wrapped.push_back(EncodableValue());
527+
reply(EncodableValue(std::move(wrapped)));
529528
} catch (const std::exception& exception) {
530529
reply(WrapError(exception.what()));
531530
}
@@ -552,16 +551,15 @@ void CameraApi::SetUp(flutter::BinaryMessenger* binary_messenger,
552551
return;
553552
}
554553
const int64_t camera_id_arg = encodable_camera_id_arg.LongValue();
555-
api->StopImageStream(
556-
camera_id_arg, [reply](std::optional<FlutterError>&& output) {
557-
if (output.has_value()) {
558-
reply(WrapError(output.value()));
559-
return;
560-
}
561-
EncodableList wrapped;
562-
wrapped.push_back(EncodableValue());
563-
reply(EncodableValue(std::move(wrapped)));
564-
});
554+
std::optional<FlutterError> output =
555+
api->StopImageStream(camera_id_arg);
556+
if (output.has_value()) {
557+
reply(WrapError(output.value()));
558+
return;
559+
}
560+
EncodableList wrapped;
561+
wrapped.push_back(EncodableValue());
562+
reply(EncodableValue(std::move(wrapped)));
565563
} catch (const std::exception& exception) {
566564
reply(WrapError(exception.what()));
567565
}

packages/camera/camera_windows/windows/messages.g.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2013 The Flutter Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4-
// Autogenerated from Pigeon (v22.6.0), do not edit directly.
4+
// Autogenerated from Pigeon (v22.6.2), do not edit directly.
55
// See also: https://pub.dev/packages/pigeon
66

77
#ifndef PIGEON_MESSAGES_G_H_
@@ -190,13 +190,9 @@ class CameraApi {
190190
int64_t camera_id,
191191
std::function<void(ErrorOr<std::string> reply)> result) = 0;
192192
// Starts the image stream for the given camera.
193-
virtual void StartImageStream(
194-
int64_t camera_id,
195-
std::function<void(std::optional<FlutterError> reply)> result) = 0;
193+
virtual std::optional<FlutterError> StartImageStream(int64_t camera_id) = 0;
196194
// Stops the image stream for the given camera.
197-
virtual void StopImageStream(
198-
int64_t camera_id,
199-
std::function<void(std::optional<FlutterError> reply)> result) = 0;
195+
virtual std::optional<FlutterError> StopImageStream(int64_t camera_id) = 0;
200196
// Starts the preview stream for the given camera.
201197
virtual void PausePreview(
202198
int64_t camera_id,

packages/camera/camera_windows/windows/test/camera_plugin_test.cpp

Lines changed: 10 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ using ::testing::EndsWith;
2929
using ::testing::Eq;
3030
using ::testing::Pointee;
3131
using ::testing::Return;
32+
using ::testing::Optional;
33+
using ::testing::Property;
3234

3335
void MockInitCamera(MockCamera* camera, bool success) {
3436
EXPECT_CALL(*camera,
@@ -366,34 +368,14 @@ TEST(CameraPlugin, StartImageStreamHandlerCallsStartImageStream) {
366368
return cam->camera_id_ == camera_id;
367369
});
368370

369-
EXPECT_CALL(*camera,
370-
HasPendingResultByType(Eq(PendingResultType::kStartStream)))
371-
.Times(1)
372-
.WillOnce(Return(false));
373-
374-
EXPECT_CALL(*camera,
375-
AddPendingVoidResult(Eq(PendingResultType::kStartStream), _))
376-
.Times(1)
377-
.WillOnce([cam = camera.get()](
378-
PendingResultType type,
379-
std::function<void(std::optional<FlutterError>)> result) {
380-
cam->pending_void_result_ = std::move(result);
381-
return true;
382-
});
383-
384371
EXPECT_CALL(*camera, GetCaptureController)
385372
.Times(1)
386373
.WillOnce([cam = camera.get()]() {
387-
assert(cam->pending_void_result_);
388374
return cam->capture_controller_.get();
389375
});
390376

391377
EXPECT_CALL(*capture_controller, StartImageStream)
392-
.Times(1)
393-
.WillOnce([cam = camera.get()]() {
394-
assert(cam->pending_void_result_);
395-
return cam->pending_void_result_(std::nullopt);
396-
});
378+
.Times(1);
397379

398380
camera->camera_id_ = mock_camera_id;
399381
camera->capture_controller_ = std::move(capture_controller);
@@ -402,24 +384,14 @@ TEST(CameraPlugin, StartImageStreamHandlerCallsStartImageStream) {
402384
std::make_unique<MockBinaryMessenger>().get(),
403385
std::make_unique<MockCameraFactory>());
404386

405-
// Add mocked camera to plugins camera list.
406-
plugin.AddCamera(std::move(camera));
407-
408387
// Set the event sink to a mocked event sink.
409388
auto mock_event_sink = std::make_unique<MockEventSink>();
410389
plugin.SetEventSink(std::move(mock_event_sink));
411390

412-
bool result_called = false;
413-
std::function<void(std::optional<FlutterError>)> start_image_stream_result =
414-
[&result_called](std::optional<FlutterError> reply) {
415-
EXPECT_FALSE(result_called); // Ensure only one reply call.
416-
result_called = true;
417-
EXPECT_FALSE(reply);
418-
};
419-
420-
plugin.StartImageStream(mock_camera_id, std::move(start_image_stream_result));
391+
// Add mocked camera to plugins camera list.
392+
plugin.AddCamera(std::move(camera));
421393

422-
EXPECT_TRUE(result_called);
394+
EXPECT_EQ(plugin.StartImageStream(mock_camera_id), std::nullopt);
423395
}
424396

425397
TEST(CameraPlugin, StartImageStreamHandlerErrorOnInvalidCameraId) {
@@ -452,18 +424,7 @@ TEST(CameraPlugin, StartImageStreamHandlerErrorOnInvalidCameraId) {
452424
// Add mocked camera to plugins camera list.
453425
plugin.AddCamera(std::move(camera));
454426

455-
bool result_called = false;
456-
std::function<void(std::optional<FlutterError>)> start_image_stream_result =
457-
[&result_called](std::optional<FlutterError> reply) {
458-
EXPECT_FALSE(result_called); // Ensure only one reply call.
459-
result_called = true;
460-
EXPECT_TRUE(reply);
461-
};
462-
463-
plugin.StartImageStream(missing_camera_id,
464-
std::move(start_image_stream_result));
465-
466-
EXPECT_TRUE(result_called);
427+
EXPECT_THAT(plugin.StartImageStream(missing_camera_id), Optional(Property("code", &FlutterError::code, "camera_error")));
467428
}
468429

469430
TEST(CameraPlugin, StopImageStreamHandlerCallsStopImageStream) {
@@ -481,34 +442,14 @@ TEST(CameraPlugin, StopImageStreamHandlerCallsStopImageStream) {
481442
return cam->camera_id_ == camera_id;
482443
});
483444

484-
EXPECT_CALL(*camera,
485-
HasPendingResultByType(Eq(PendingResultType::kStopStream)))
486-
.Times(1)
487-
.WillOnce(Return(false));
488-
489-
EXPECT_CALL(*camera,
490-
AddPendingVoidResult(Eq(PendingResultType::kStopStream), _))
491-
.Times(1)
492-
.WillOnce([cam = camera.get()](
493-
PendingResultType type,
494-
std::function<void(std::optional<FlutterError>)> result) {
495-
cam->pending_void_result_ = std::move(result);
496-
return true;
497-
});
498-
499445
EXPECT_CALL(*camera, GetCaptureController)
500446
.Times(1)
501447
.WillOnce([cam = camera.get()]() {
502-
assert(cam->pending_void_result_);
503448
return cam->capture_controller_.get();
504449
});
505450

506451
EXPECT_CALL(*capture_controller, StopImageStream)
507-
.Times(1)
508-
.WillOnce([cam = camera.get()]() {
509-
assert(cam->pending_void_result_);
510-
return cam->pending_void_result_(std::nullopt);
511-
});
452+
.Times(1);
512453

513454
camera->camera_id_ = mock_camera_id;
514455
camera->capture_controller_ = std::move(capture_controller);
@@ -520,17 +461,7 @@ TEST(CameraPlugin, StopImageStreamHandlerCallsStopImageStream) {
520461
// Add mocked camera to plugins camera list.
521462
plugin.AddCamera(std::move(camera));
522463

523-
bool result_called = false;
524-
std::function<void(std::optional<FlutterError>)> stop_image_stream_result =
525-
[&result_called](std::optional<FlutterError> reply) {
526-
EXPECT_FALSE(result_called); // Ensure only one reply call.
527-
result_called = true;
528-
EXPECT_FALSE(reply);
529-
};
530-
531-
plugin.StopImageStream(mock_camera_id, std::move(stop_image_stream_result));
532-
533-
EXPECT_TRUE(result_called);
464+
EXPECT_EQ(plugin.StopImageStream(mock_camera_id), std::nullopt);
534465
}
535466

536467
TEST(CameraPlugin, StopImageStreamHandlerErrorOnInvalidCameraId) {
@@ -563,18 +494,7 @@ TEST(CameraPlugin, StopImageStreamHandlerErrorOnInvalidCameraId) {
563494
// Add mocked camera to plugins camera list.
564495
plugin.AddCamera(std::move(camera));
565496

566-
bool result_called = false;
567-
std::function<void(std::optional<FlutterError>)> stop_image_stream_result =
568-
[&result_called](std::optional<FlutterError> reply) {
569-
EXPECT_FALSE(result_called); // Ensure only one reply call.
570-
result_called = true;
571-
EXPECT_TRUE(reply);
572-
};
573-
574-
plugin.StopImageStream(missing_camera_id,
575-
std::move(stop_image_stream_result));
576-
577-
EXPECT_TRUE(result_called);
497+
EXPECT_THAT(plugin.StopImageStream(missing_camera_id), Optional(Property("code", &FlutterError::code, "camera_error")));
578498
}
579499

580500
TEST(CameraPlugin, InitializeHandlerErrorOnInvalidCameraId) {

0 commit comments

Comments
 (0)