feat: Reintroduce Macros for Frame Processor Plugin registration (#2027)

in VisionCamera v1 & v2 there were two ObjC macros that were helping
in creation/registration of Frame Processors, but these were removed with
v3

This PR reintroduces such macros, which will not only make FP development
easier, but also it will also fix issues people had with registration of
Swift Frame Processors (+load vs +initialize issues)

Docs were also updated to reflect that the macros should be used to
correctly initialize and register ObjC/Swift Frame Processors
This commit is contained in:
Mateusz Mędrek
2023-10-19 10:35:14 +02:00
committed by GitHub
parent 2666ac53a6
commit a291642c53
15 changed files with 92 additions and 60 deletions

View File

@@ -21,8 +21,39 @@
/// VisionCamera Runtime.
@interface FrameProcessorPlugin : NSObject
/// The initializer for a Frame Processor Plugin class that takes optional object that consists
/// options passed from JS layer
- (instancetype _Nonnull)initWithOptions:(NSDictionary* _Nullable)options;
/// The actual callback when calling this plugin. Any Frame Processing should be handled there.
/// Make sure your code is optimized, as this is a hot path.
- (id _Nullable)callback:(Frame* _Nonnull)frame withArguments:(NSDictionary* _Nullable)arguments;
@end
#define VISION_CONCAT2(A, B) A##B
#define VISION_CONCAT(A, B) VISION_CONCAT2(A, B)
#define VISION_EXPORT_FRAME_PROCESSOR(frame_processor_class, frame_processor_plugin_name) \
+(void)load { \
[FrameProcessorPluginRegistry addFrameProcessorPlugin:@ #frame_processor_plugin_name \
withInitializer:^FrameProcessorPlugin*(NSDictionary* _Nullable options) { \
return [[frame_processor_class alloc] initWithOptions:options]; \
}]; \
}
#define VISION_EXPORT_SWIFT_FRAME_PROCESSOR(frame_processor_class, frame_processor_plugin_name) \
\
@interface frame_processor_class (FrameProcessorPluginLoader) \
@end \
\
@implementation frame_processor_class (FrameProcessorPluginLoader) \
\
__attribute__((constructor)) static void VISION_CONCAT(initialize_, frame_processor_plugin_name)(void) { \
[FrameProcessorPluginRegistry addFrameProcessorPlugin:@ #frame_processor_plugin_name \
withInitializer:^FrameProcessorPlugin* _Nonnull(NSDictionary* _Nullable options) { \
return [[frame_processor_class alloc] initWithOptions:options]; \
}]; \
} \
\
@end

View File

@@ -11,6 +11,11 @@
// Base implementation (empty)
@implementation FrameProcessorPlugin
- (instancetype)initWithOptions:(NSDictionary* _Nullable)options {
self = [super init];
return self;
}
- (id _Nullable)callback:(Frame* _Nonnull)frame withArguments:(NSDictionary* _Nullable)arguments {
[NSException raise:NSInternalInconsistencyException
format:@"Frame Processor Plugin does not override the `callback(frame:withArguments:)` method!"];