Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[camera] Convert iOS Obj-C->Dart calls to Pigeon #6568

Merged
merged 8 commits into from
Apr 19, 2024

Conversation

stuartmorgan
Copy link
Contributor

Converts all of the Obj-C -> Dart calls to Pigeon, using the new suffix-based Pigeon API instantiation feature.

This required decentralizing some threading code slightly: since method channel calls only involve one method (due to the lack of strong types), a wrapper that automatically did thread bouncing was feasible, but with Pigeon it's not since a wrapper would have to duplicate the entire API surface, and that's more work than just doing the dispatches at the call site.

Part of flutter/flutter#117905

Pre-launch Checklist

@@ -30,49 +32,24 @@ extern AVCaptureFlashMode FLTGetAVCaptureFlashModeForFLTFlashMode(FLTFlashMode m

#pragma mark - exposure mode

/// Represents camera's exposure mode. Mirrors ExposureMode in camera.dart.
typedef NS_ENUM(NSInteger, FLTExposureMode) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These enums are no longer needed because we can just use the Pigeon-generated enums instead.

extern FLTExposureMode FLTGetFLTExposureModeForString(NSString *mode);
/// Gets FCPPlatformExposureMode from its string representation.
/// @param mode a string representation of the exposure mode.
extern FCPPlatformExposureMode FCPGetExposureModeForString(NSString *mode);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are temporary methods; there are Dart->host calls that send exposure and focus modes, so until I convert those in a follow-up we need to be able to convert method channel strings to the Pigeon enum that replaced the ones removed here.

_camera.dartAPI = [[FCPCameraEventApi alloc]
initWithBinaryMessenger:_messenger
messageChannelSuffix:[NSString stringWithFormat:@"%ld", cameraId]];
[_camera reportInitializationState];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the actual channel invocation into FLTCam because it seemed strange to have it here when a) the camera instance already has the channel, and b) all of the values here come from the camera instance.

[UIDevice.currentDevice endGeneratingDeviceOrientationNotifications];
}

- (void)startOrientationListener {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I inlined this as a drive-by cleanup because it was only being called from one place, and that was init, which shouldn't be calling methods.

[NSString stringWithFormat:@"Unknown exposure mode %@", modeStr]
}]));
return;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm now preventing this error from being possible on the Dart side (same below).

@@ -552,6 +536,38 @@ class AVFoundationCamera extends CameraPlatform {
return Texture(textureId: cameraId);
}

String _serializeFocusMode(FocusMode mode) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added these instead of continuing to use the ones from the platform interface package (the versions without the _) because having serialization in the platform interface package and deserialization in the implementation package is an anti-pattern since it means changes in one package can break internal communication in another. We created a lot of it by accident during the initial process of federating plugins; Pigeon migration has been eliminating it, but doing it now meant I could remove all the native code that required an "invalid" state for these enums, since I can guarantee within this package that we won't get a state at the native layer that we don't handle.

(These methods will become enum->enum conversion methods instead of enum->string conversion when I finish the Pigeon migration for this package.)

await streamQueue.cancel();
});

test('Should receive resolution changes', () async {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the next test are testing functionality that was dead code. The Dart method call handler handled several messages that are never sent by the native code. It doesn't look like Android uses them either, so I'm guessing it was code mistakenly left in during refactoring in code review of a feature.

Copy link
Contributor

@hellohuanlin hellohuanlin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went through a thorough review. Looks pretty clean.

arguments:@{@"orientation" : FLTGetStringForUIDeviceOrientation(orientation)}];
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.globalEventAPI
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I vaguely remember there was an effort to make method channel thread safe. what's the progress on that and do we still need to manually dispatch to main in plugin code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current state is that replies are thread-safe, but calls that start on the host side still have to be done from the main thread. I don't think there's active work on addressing that part (although I believe it's still something we do eventually want to do).

@interface CameraOrientationTests : XCTestCase
@end

@implementation CameraOrientationTests

// Ensure that the given queue and then the main queue have both cycled, to wait for any pending
// async events that may have been bounced between them.
- (void)waitForRoundTripWithQueue:(dispatch_queue_t)queue {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

}

FLTFocusMode FLTGetFLTFocusModeForString(NSString *mode) {
FCPPlatformFocusMode FCPGetFocusModeForString(NSString *mode) {
if ([mode isEqualToString:@"auto"]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will these hard coded strings be gone eventually after pigeon work is done?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is just for the remaining non-Pigeon Dart->host calls.

@stuartmorgan stuartmorgan added the autosubmit Merge PR when tree becomes green via auto submit App label Apr 19, 2024
@auto-submit auto-submit bot merged commit 88a3a56 into flutter:main Apr 19, 2024
78 checks passed
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 19, 2024
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Apr 19, 2024
flutter/packages@0e3809d...88a3a56

2024-04-19 stuartmorgan@google.com [camera] Convert iOS Obj-C->Dart calls to Pigeon (flutter/packages#6568)
2024-04-19 highdro0479@gmail.com [flutter_markdown] Ensure customize nested bullet list style. (flutter/packages#6384)
2024-04-18 stuartmorgan@google.com [ci] Add more dev dependency checks, and fix errors (flutter/packages#6563)
2024-04-18 engine-flutter-autoroll@skia.org Roll Flutter from 3882afb to fb110b9 (56 revisions) (flutter/packages#6565)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC flutter-ecosystem@google.com,rmistry@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
gilnobrega pushed a commit to gilnobrega/flutter that referenced this pull request Apr 22, 2024
…r#147059)

flutter/packages@0e3809d...88a3a56

2024-04-19 stuartmorgan@google.com [camera] Convert iOS Obj-C->Dart calls to Pigeon (flutter/packages#6568)
2024-04-19 highdro0479@gmail.com [flutter_markdown] Ensure customize nested bullet list style. (flutter/packages#6384)
2024-04-18 stuartmorgan@google.com [ci] Add more dev dependency checks, and fix errors (flutter/packages#6563)
2024-04-18 engine-flutter-autoroll@skia.org Roll Flutter from 3882afb to fb110b9 (56 revisions) (flutter/packages#6565)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC flutter-ecosystem@google.com,rmistry@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
TecHaxter pushed a commit to TecHaxter/flutter_packages that referenced this pull request May 22, 2024
Converts all of the Obj-C -> Dart calls to Pigeon, using the new suffix-based Pigeon API instantiation feature.

This required decentralizing some threading code slightly: since method channel calls only involve one method (due to the lack of strong types), a wrapper that automatically did thread bouncing was feasible, but with Pigeon it's not since a wrapper would have to duplicate the entire API surface, and that's more work than just doing the dispatches at the call site.

Part of flutter/flutter#117905
arc-yong pushed a commit to Arctuition/packages-arc that referenced this pull request Jun 14, 2024
Converts all of the Obj-C -> Dart calls to Pigeon, using the new suffix-based Pigeon API instantiation feature.

This required decentralizing some threading code slightly: since method channel calls only involve one method (due to the lack of strong types), a wrapper that automatically did thread bouncing was feasible, but with Pigeon it's not since a wrapper would have to duplicate the entire API surface, and that's more work than just doing the dispatches at the call site.

Part of flutter/flutter#117905
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App p: camera platform-ios platform-macos
Projects
None yet
2 participants