@blotch/angular-lynx-native 22.3.1 → 22.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/README.md +47 -15
  2. package/dist/android/project.d.ts +114 -1
  3. package/dist/android/project.d.ts.map +1 -1
  4. package/dist/android/project.js +440 -5
  5. package/dist/eject.d.ts +15 -1
  6. package/dist/eject.d.ts.map +1 -1
  7. package/dist/eject.js +14 -2
  8. package/dist/features.d.ts +70 -0
  9. package/dist/features.d.ts.map +1 -0
  10. package/dist/features.js +115 -0
  11. package/dist/files.d.ts +24 -0
  12. package/dist/files.d.ts.map +1 -0
  13. package/dist/files.js +41 -0
  14. package/dist/generate.d.ts +34 -3
  15. package/dist/generate.d.ts.map +1 -1
  16. package/dist/generate.js +78 -19
  17. package/dist/index.d.ts +1 -0
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +16 -2
  20. package/dist/ios/build.d.ts.map +1 -1
  21. package/dist/ios/build.js +8 -2
  22. package/dist/ios/frameworks.d.ts +6 -5
  23. package/dist/ios/frameworks.d.ts.map +1 -1
  24. package/dist/ios/frameworks.js +46 -15
  25. package/dist/ios/project.d.ts +192 -11
  26. package/dist/ios/project.d.ts.map +1 -1
  27. package/dist/ios/project.js +823 -33
  28. package/dist/options.d.ts +208 -1
  29. package/dist/options.d.ts.map +1 -1
  30. package/dist/options.js +154 -0
  31. package/dist/permissions.d.ts +18 -1
  32. package/dist/permissions.d.ts.map +1 -1
  33. package/dist/permissions.js +79 -4
  34. package/dist/plugins.d.ts +245 -0
  35. package/dist/plugins.d.ts.map +1 -0
  36. package/dist/plugins.js +1244 -0
  37. package/package.json +1 -1
  38. package/templates/android/README.md +46 -5
  39. package/templates/android/angular-lynx.properties +2 -0
  40. package/templates/android/app/build.gradle.kts +78 -11
  41. package/templates/android/app/src/main/AndroidManifest.xml +19 -3
  42. package/templates/android/app/src/main/java/dev/angularlynx/shell/AngularLynxPlugins.kt +13 -0
  43. package/templates/android/app/src/main/java/dev/angularlynx/shell/AudioRecorderModule.kt +520 -0
  44. package/templates/android/app/src/main/java/dev/angularlynx/shell/BarcodeScannerModule.kt +144 -0
  45. package/templates/android/app/src/main/java/dev/angularlynx/shell/BiometricsModule.kt +433 -0
  46. package/templates/android/app/src/main/java/dev/angularlynx/shell/BitmapPresenter.kt +98 -0
  47. package/templates/android/app/src/main/java/dev/angularlynx/shell/BitmapRendererContext.kt +175 -0
  48. package/templates/android/app/src/main/java/dev/angularlynx/shell/BluetoothModule.kt +1288 -0
  49. package/templates/android/app/src/main/java/dev/angularlynx/shell/CalendarModule.kt +127 -0
  50. package/templates/android/app/src/main/java/dev/angularlynx/shell/Canvas2DContext.kt +1557 -0
  51. package/templates/android/app/src/main/java/dev/angularlynx/shell/Canvas2DOps.kt +63 -0
  52. package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasFilter.kt +357 -0
  53. package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasUI.kt +4 -3
  54. package/templates/android/app/src/main/java/dev/angularlynx/shell/EdgeToEdgeOverlay.kt +24 -0
  55. package/templates/android/app/src/main/java/dev/angularlynx/shell/EngineThreadHint.kt +156 -0
  56. package/templates/android/app/src/main/java/dev/angularlynx/shell/FilePickerModule.kt +263 -0
  57. package/templates/android/app/src/main/java/dev/angularlynx/shell/GeolocationModule.kt +557 -0
  58. package/templates/android/app/src/main/java/dev/angularlynx/shell/HapticsModule.kt +82 -0
  59. package/templates/android/app/src/main/java/dev/angularlynx/shell/HostElementUI.kt +331 -0
  60. package/templates/android/app/src/main/java/dev/angularlynx/shell/ImagePickerModule.kt +143 -0
  61. package/templates/android/app/src/main/java/dev/angularlynx/shell/ImagePipeline.kt +83 -0
  62. package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +523 -9
  63. package/templates/android/app/src/main/java/dev/angularlynx/shell/ModuleBytes.kt +58 -0
  64. package/templates/android/app/src/main/java/dev/angularlynx/shell/MotionModule.kt +332 -0
  65. package/templates/android/app/src/main/java/dev/angularlynx/shell/NetworkStatus.kt +121 -0
  66. package/templates/android/app/src/main/java/dev/angularlynx/shell/NfcModule.kt +274 -0
  67. package/templates/android/app/src/main/java/dev/angularlynx/shell/PermissionRequests.kt +35 -0
  68. package/templates/android/app/src/main/java/dev/angularlynx/shell/PhotoLibraryModule.kt +355 -0
  69. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShareTargetModule.kt +234 -0
  70. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellApplication.kt +7 -8
  71. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellModule.kt +58 -4
  72. package/templates/android/app/src/main/java/dev/angularlynx/shell/StoreReviewModule.kt +66 -0
  73. package/templates/android/app/src/main/java/dev/angularlynx/shell/SystemFontFamilies.kt +68 -0
  74. package/templates/android/app/src/main/java/dev/angularlynx/shell/TrackingModule.kt +74 -0
  75. package/templates/android/app/src/main/java/dev/angularlynx/shell/VideoFrameTarget.kt +429 -0
  76. package/templates/android/app/src/main/java/dev/angularlynx/shell/VideoUI.kt +676 -0
  77. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLContext.kt +128 -13
  78. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLModule.kt +493 -65
  79. package/templates/android/app/src/main/java/dev/angularlynx/shell/WifiModule.kt +429 -0
  80. package/templates/android/app/src/main/res/values/biometrics.xml +9 -0
  81. package/templates/android/app/src/main/res/values/share_target.xml +11 -0
  82. package/templates/android/app/src/main/res/xml/backup_rules.xml +5 -3
  83. package/templates/android/app/src/main/res/xml/data_extraction_rules.xml +6 -3
  84. package/templates/android/app/src/main/res/xml/share_paths.xml +7 -2
  85. package/templates/android/build.gradle.kts +1 -1
  86. package/templates/android/gradle/wrapper/gradle-wrapper.properties +2 -2
  87. package/templates/ios/App/AngularLynxBitmapRendererContext.m +188 -0
  88. package/templates/ios/App/AngularLynxCanvas2DContext.m +2467 -0
  89. package/templates/ios/App/AngularLynxCanvas2DOps.h +61 -0
  90. package/templates/ios/App/AngularLynxPlugins.swift +22 -0
  91. package/templates/ios/App/AngularLynxVideo.h +37 -0
  92. package/templates/ios/App/AngularLynxVideo.m +435 -0
  93. package/templates/ios/App/AngularLynxWebGL.h +69 -7
  94. package/templates/ios/App/AngularLynxWebGL.m +538 -29
  95. package/templates/ios/App/AngularLynxWebGLContext.m +148 -46
  96. package/templates/ios/App/AnimationFrameRate.swift +66 -0
  97. package/templates/ios/App/AppDelegate.swift +37 -3
  98. package/templates/ios/App/AppleSignInButton.swift +106 -0
  99. package/templates/ios/App/AppleSignInModule.swift +222 -0
  100. package/templates/ios/App/AudioRecorderModule.swift +433 -0
  101. package/templates/ios/App/BarcodeScannerModule.swift +284 -0
  102. package/templates/ios/App/BiometricsModule.swift +256 -0
  103. package/templates/ios/App/BluetoothModule.swift +1086 -0
  104. package/templates/ios/App/CalendarModule.swift +158 -0
  105. package/templates/ios/App/FilePickerModule.swift +240 -0
  106. package/templates/ios/App/GeolocationModule.swift +455 -0
  107. package/templates/ios/App/HapticsModule.swift +90 -0
  108. package/templates/ios/App/HostElementUI.swift +270 -0
  109. package/templates/ios/App/ImagePickerModule.swift +160 -1
  110. package/templates/ios/App/Info.plist +2 -0
  111. package/templates/ios/App/LynxViewController.swift +274 -9
  112. package/templates/ios/App/MotionModule.swift +430 -0
  113. package/templates/ios/App/NetworkStatus.swift +41 -0
  114. package/templates/ios/App/NfcModule.swift +346 -0
  115. package/templates/ios/App/PhotoLibraryModule.swift +169 -0
  116. package/templates/ios/App/ShareTargetModule.swift +150 -0
  117. package/templates/ios/App/ShellModule.swift +17 -0
  118. package/templates/ios/App/StoreReviewModule.swift +61 -0
  119. package/templates/ios/App/TrackingModule.swift +121 -0
  120. package/templates/ios/App/WifiModule.swift +237 -0
  121. package/templates/ios/App/XElementRegistry.swift +13 -3
  122. package/templates/ios/ShareExtension/Info.plist +42 -0
  123. package/templates/ios/ShareExtension/ShareViewController.swift +387 -0
@@ -0,0 +1,61 @@
1
+ // Generated from packages/runtime/src/lib/canvas/canvas-2d-ops.ts by
2
+ // packages/native/scripts/webgl-ops.ts: run `npm run generate:webgl-ops -w
3
+ // packages/native` rather than editing it.
4
+
5
+ #import <Foundation/Foundation.h>
6
+
7
+ /// The commands of the 2D context's bridge, as `<canvas>`'s 2D context in
8
+ /// the runtime records them: below 200 they return nothing, from 200 each
9
+ /// answers one query.
10
+ typedef NS_ENUM(NSInteger, ALCanvas2DOp) {
11
+ ALCanvas2DOpSave = 1,
12
+ ALCanvas2DOpRestore = 2,
13
+ ALCanvas2DOpReset = 3,
14
+ ALCanvas2DOpResize = 4,
15
+ ALCanvas2DOpPresent = 5,
16
+ ALCanvas2DOpSetTransform = 10,
17
+ ALCanvas2DOpSetGlobalAlpha = 11,
18
+ ALCanvas2DOpSetCompositeOperation = 12,
19
+ ALCanvas2DOpSetFillColor = 13,
20
+ ALCanvas2DOpSetFillGradient = 14,
21
+ ALCanvas2DOpSetFillPattern = 15,
22
+ ALCanvas2DOpSetStrokeColor = 16,
23
+ ALCanvas2DOpSetStrokeGradient = 17,
24
+ ALCanvas2DOpSetStrokePattern = 18,
25
+ ALCanvas2DOpSetLineWidth = 19,
26
+ ALCanvas2DOpSetLineCap = 20,
27
+ ALCanvas2DOpSetLineJoin = 21,
28
+ ALCanvas2DOpSetMiterLimit = 22,
29
+ ALCanvas2DOpSetLineDash = 23,
30
+ ALCanvas2DOpSetShadow = 24,
31
+ ALCanvas2DOpSetFont = 25,
32
+ ALCanvas2DOpSetTextAlign = 26,
33
+ ALCanvas2DOpSetTextBaseline = 27,
34
+ ALCanvas2DOpSetDirection = 28,
35
+ ALCanvas2DOpSetLetterSpacing = 29,
36
+ ALCanvas2DOpSetImageSmoothing = 30,
37
+ ALCanvas2DOpSetFilter = 31,
38
+ ALCanvas2DOpCreatePattern = 44,
39
+ ALCanvas2DOpReleaseObject = 46,
40
+ ALCanvas2DOpClearPath = 50,
41
+ ALCanvas2DOpFill = 51,
42
+ ALCanvas2DOpStroke = 52,
43
+ ALCanvas2DOpClip = 53,
44
+ ALCanvas2DOpFillText = 54,
45
+ ALCanvas2DOpStrokeText = 55,
46
+ ALCanvas2DOpDrawImage = 56,
47
+ ALCanvas2DOpPutImageData = 57,
48
+ ALCanvas2DOpMeasureText = 200,
49
+ ALCanvas2DOpGetImageData = 201,
50
+ ALCanvas2DOpIsPointInPath = 202,
51
+ ALCanvas2DOpIsPointInStroke = 203,
52
+ };
53
+
54
+ /// The segments of a path, each followed by its points.
55
+ typedef NS_ENUM(NSInteger, ALCanvas2DPathOp) {
56
+ ALCanvas2DPathOpMoveTo = 0,
57
+ ALCanvas2DPathOpLineTo = 1,
58
+ ALCanvas2DPathOpQuadTo = 2,
59
+ ALCanvas2DPathOpCubicTo = 3,
60
+ ALCanvas2DPathOpClose = 4,
61
+ };
@@ -0,0 +1,22 @@
1
+ // Written by @blotch/angular-lynx-native from the plugins angular.json's
2
+ // `nativePlugins` lists: a generated project gets a new copy on every
3
+ // build, so change the plugins rather than this file. In an ejected
4
+ // project it is yours, and `nativePlugins` no longer applies.
5
+ import Lynx
6
+ import UIKit
7
+
8
+ /// The app's native plugins' modules and elements.
9
+ enum AngularLynxPlugins {
10
+ static func register(in config: LynxConfig, host: UIViewController) {}
11
+ }
12
+
13
+ /// A plugin module's param: the view controller showing the app, for a
14
+ /// module that presents a screen of its own. Weak, as Lynx keeps the
15
+ /// param as long as the config, which the controller keeps.
16
+ final class AngularLynxPluginHost: NSObject {
17
+ weak var viewController: UIViewController?
18
+
19
+ init(_ viewController: UIViewController) {
20
+ self.viewController = viewController
21
+ }
22
+ }
@@ -0,0 +1,37 @@
1
+ // `<video>` as an image for the canvas contexts: XElement's video element,
2
+ // whose frames a 2D canvas can draw (`drawImage()`, `createPattern()`) and a
3
+ // WebGL canvas can upload (`texImage2D()`, three.js's VideoTexture), as a
4
+ // browser's HTMLVideoElement's can. See
5
+ // packages/runtime/src/lib/canvas/video-element.ts.
6
+ //
7
+ // XElement's player shows its frames in an AVPlayerLayer, which can't be
8
+ // read. So the element adds an AVPlayerItemVideoOutput to the player's item,
9
+ // which hands over the frame showing now as a pixel buffer, and registers
10
+ // what reads it (AngularLynxVideoFrames) in the canvas registry under the
11
+ // video's `video-id`, which the runtime sets. The contexts then read a video
12
+ // as they read another canvas, by its id (`{ canvas: id }`), so the pixels
13
+ // never travel through JavaScript.
14
+
15
+ #import <Foundation/Foundation.h>
16
+ #import <XElement/LynxUIVideo.h>
17
+
18
+ #import "AngularLynxWebGL.h"
19
+
20
+ NS_ASSUME_NONNULL_BEGIN
21
+
22
+ /// A video's frames, as the canvas registry holds a canvas's context: its
23
+ /// snapshot is the frame the player shows now. Its view's video element
24
+ /// keeps it; the registry holds it weakly. Its frames are read on Lynx's
25
+ /// engine thread, and its output set on the UI thread.
26
+ @interface AngularLynxVideoFrames : NSObject <AngularLynxCanvasContext>
27
+ /// The size of the latest frame, in pixels, or zero before the first: the
28
+ /// runtime's `videoWidth` and `videoHeight` (the module's `videoSize`).
29
+ - (CGSize)frameSize;
30
+ @end
31
+
32
+ /// The `video` element: XElement's, and its `video-id` attribute, which
33
+ /// registers its frames under the id.
34
+ @interface AngularLynxVideo : LynxUIVideo
35
+ @end
36
+
37
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,435 @@
1
+ #import "AngularLynxVideo.h"
2
+
3
+ #import <AVFoundation/AVFoundation.h>
4
+ #import <Lynx/LynxPropsProcessor.h>
5
+ #import <QuartzCore/QuartzCore.h>
6
+ #import <VideoToolbox/VideoToolbox.h>
7
+
8
+ static void *const kAngularLynxVideoItemContext = (void *)&kAngularLynxVideoItemContext;
9
+
10
+ #pragma mark - Frames
11
+
12
+ @interface AngularLynxVideoFrames ()
13
+ /// Whether the frames have an output whose transform is still loading, so
14
+ /// they have no frame to give yet however many the output has.
15
+ - (BOOL)waitsForTransform;
16
+ @end
17
+
18
+ /// Where a frame of `size` lands through a track's `transform`. A file's
19
+ /// transform turns the frame about its top-left corner, so a turned frame
20
+ /// can land left of or above zero: only the rect's size and how far it's
21
+ /// off matter.
22
+ static CGRect displayRect(CGSize size, CGAffineTransform transform) {
23
+ return CGRectStandardize(
24
+ CGRectApplyAffineTransform(CGRectMake(0, 0, size.width, size.height), transform));
25
+ }
26
+
27
+ /// A frame's size as it's shown, in whole pixels.
28
+ static CGSize displaySize(CGSize size, CGAffineTransform transform) {
29
+ CGRect rect = displayRect(size, transform);
30
+ return CGSizeMake(round(rect.size.width), round(rect.size.height));
31
+ }
32
+
33
+ @implementation AngularLynxVideoFrames {
34
+ NSString *_canvasId;
35
+ /// The output on the player's item now, set on the UI thread. Guarded by
36
+ /// self, as is the frame below.
37
+ AVPlayerItemVideoOutput *_output;
38
+ /// The last frame the output handed over: a paused video's output has no
39
+ /// new one to give, and still shows this one.
40
+ CVPixelBufferRef _frame;
41
+ /// How the output's frames are shown: its item's video track's
42
+ /// preferredTransform. The output hands a frame over as the file stores
43
+ /// it, which for a portrait phone recording is landscape, turned by this
44
+ /// transform when it's played. Loaded after the output is set; until then
45
+ /// there's no frame to read, rather than one the wrong way round.
46
+ CGAffineTransform _transform;
47
+ BOOL _hasTransform;
48
+ }
49
+
50
+ - (instancetype)initWithVideoId:(NSString *)videoId {
51
+ if ((self = [super init])) {
52
+ _canvasId = [videoId copy];
53
+ }
54
+ return self;
55
+ }
56
+
57
+ - (void)dealloc {
58
+ CVPixelBufferRelease(_frame);
59
+ }
60
+
61
+ - (NSString *)canvasId {
62
+ return _canvasId;
63
+ }
64
+
65
+ // A video shows itself in its own view: there's nothing to attach.
66
+ - (void)attachTarget:(AngularLynxCanvasTarget *)target {
67
+ }
68
+
69
+ - (void)targetDidLayOut:(AngularLynxCanvasTarget *)target {
70
+ }
71
+
72
+ // Only an OffscreenCanvas is transferred: a video has nothing to clear.
73
+ - (void)clearForTransfer {
74
+ }
75
+
76
+ - (void)setOutput:(AVPlayerItemVideoOutput *)output {
77
+ @synchronized(self) {
78
+ if (output == _output) return;
79
+ _output = output;
80
+ // The last item's frame isn't this one's: a new source has nothing to
81
+ // draw until its first frame, as in a browser.
82
+ CVPixelBufferRelease(_frame);
83
+ _frame = NULL;
84
+ _hasTransform = NO;
85
+ }
86
+ }
87
+
88
+ - (BOOL)waitsForTransform {
89
+ @synchronized(self) {
90
+ return _output && !_hasTransform;
91
+ }
92
+ }
93
+
94
+ - (void)setTransform:(CGAffineTransform)transform forOutput:(AVPlayerItemVideoOutput *)output {
95
+ @synchronized(self) {
96
+ // Loaded for an output that's been replaced since: it's another item's.
97
+ if (output != _output) return;
98
+ _transform = transform;
99
+ _hasTransform = YES;
100
+ }
101
+ }
102
+
103
+ /// Takes over another's output and the frame it last read, for a video
104
+ /// whose id changed: a paused video's output gives its frame only once.
105
+ - (void)takeOutputAndFrameFrom:(AngularLynxVideoFrames *)other {
106
+ AVPlayerItemVideoOutput *output;
107
+ CVPixelBufferRef frame;
108
+ CGAffineTransform transform;
109
+ BOOL hasTransform;
110
+ @synchronized(other) {
111
+ output = other->_output;
112
+ frame = other->_frame ? CVPixelBufferRetain(other->_frame) : NULL;
113
+ transform = other->_transform;
114
+ hasTransform = other->_hasTransform;
115
+ }
116
+ @synchronized(self) {
117
+ _output = output;
118
+ CVPixelBufferRelease(_frame);
119
+ _frame = frame;
120
+ _transform = transform;
121
+ _hasTransform = hasTransform;
122
+ }
123
+ }
124
+
125
+ /// The frame showing now, retained, and how it's shown, or NULL before the
126
+ /// first, or before its transform is loaded.
127
+ - (CVPixelBufferRef)copyFrameWithTransform:(CGAffineTransform *)transform CF_RETURNS_RETAINED {
128
+ @synchronized(self) {
129
+ if (!_hasTransform) return NULL;
130
+ *transform = _transform;
131
+ AVPlayerItemVideoOutput *output = _output;
132
+ if (output) {
133
+ // The item's time now, as the layer shows it: the output gives the
134
+ // frame for it, or nothing when it gave that one already.
135
+ CMTime time = [output itemTimeForHostTime:CACurrentMediaTime()];
136
+ if ([output hasNewPixelBufferForItemTime:time]) {
137
+ CVPixelBufferRef frame = [output copyPixelBufferForItemTime:time itemTimeForDisplay:NULL];
138
+ if (frame) {
139
+ CVPixelBufferRelease(_frame);
140
+ _frame = frame;
141
+ }
142
+ }
143
+ }
144
+ return _frame ? CVPixelBufferRetain(_frame) : NULL;
145
+ }
146
+ }
147
+
148
+ - (CGSize)frameSize {
149
+ CGAffineTransform transform;
150
+ CVPixelBufferRef frame = [self copyFrameWithTransform:&transform];
151
+ if (!frame) return CGSizeZero;
152
+ CGSize size = CGSizeMake(CVPixelBufferGetWidth(frame), CVPixelBufferGetHeight(frame));
153
+ CVPixelBufferRelease(frame);
154
+ // Turned, as a browser's videoWidth and videoHeight are: a portrait
155
+ // recording stored landscape is taller than it's wide.
156
+ return displaySize(size, transform);
157
+ }
158
+
159
+ /// The frame as RGBA, rows top to bottom, as a decoded image's, turned as
160
+ /// the player shows it. The output hands over the decoder's own format (no
161
+ /// pixel format is asked of it, so AVFoundation converts none of the frames
162
+ /// nothing reads), which VideoToolbox converts here, for the frames that
163
+ /// are read.
164
+ - (AngularLynxDecodedImage *)snapshot {
165
+ CGAffineTransform transform;
166
+ CVPixelBufferRef frame = [self copyFrameWithTransform:&transform];
167
+ if (!frame) return nil;
168
+ CGImageRef image = NULL;
169
+ OSStatus status = VTCreateCGImageFromCVPixelBuffer(frame, NULL, &image);
170
+ CVPixelBufferRelease(frame);
171
+ if (status != noErr || !image) return nil;
172
+ CGSize stored = CGSizeMake(CGImageGetWidth(image), CGImageGetHeight(image));
173
+ CGRect shown = displayRect(stored, transform);
174
+ CGSize shownSize = displaySize(stored, transform);
175
+ size_t width = (size_t)shownSize.width;
176
+ size_t height = (size_t)shownSize.height;
177
+ if (width == 0 || height == 0) {
178
+ CGImageRelease(image);
179
+ return nil;
180
+ }
181
+ NSMutableData *pixels = [NSMutableData dataWithLength:width * height * 4];
182
+ CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
183
+ CGContextRef context = CGBitmapContextCreate(
184
+ pixels.mutableBytes, width, height, 8, width * 4, space,
185
+ kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
186
+ CGColorSpaceRelease(space);
187
+ if (!context) {
188
+ CGImageRelease(image);
189
+ return nil;
190
+ }
191
+ // Drawn upright, as decodeImage draws a file: the bitmap's first row is
192
+ // the frame's top. The track's transform is in the file's coordinates,
193
+ // whose y runs down, so the context is flipped to match: y down from the
194
+ // bitmap's first row. Then the transform, moved so the turned frame
195
+ // starts at zero, then the frame drawn the right way up in those
196
+ // coordinates (CGContextDrawImage puts an image's top at the larger y).
197
+ CGContextSetBlendMode(context, kCGBlendModeCopy);
198
+ CGContextTranslateCTM(context, 0, height);
199
+ CGContextScaleCTM(context, 1, -1);
200
+ CGContextConcatCTM(context, CGAffineTransformConcat(
201
+ transform, CGAffineTransformMakeTranslation(
202
+ -shown.origin.x, -shown.origin.y)));
203
+ CGContextTranslateCTM(context, 0, stored.height);
204
+ CGContextScaleCTM(context, 1, -1);
205
+ CGContextDrawImage(context, CGRectMake(0, 0, stored.width, stored.height), image);
206
+ CGContextRelease(context);
207
+ CGImageRelease(image);
208
+ // A video frame is opaque, so premultiplied or not, its colors are the
209
+ // same.
210
+ return [[AngularLynxDecodedImage alloc] initWithPixels:pixels
211
+ width:(NSInteger)width
212
+ height:(NSInteger)height
213
+ premultiplied:YES];
214
+ }
215
+
216
+ @end
217
+
218
+ #pragma mark - Element
219
+
220
+ @implementation AngularLynxVideo {
221
+ /// The LynxView's context, kept from when the id was set: the element may
222
+ /// have let go of its own by the time it's freed.
223
+ __weak LynxContext *_lynxContext;
224
+ NSString *_videoId;
225
+ AngularLynxVideoFrames *_frames;
226
+ /// The layer whose player's item gets the output, observed while there's
227
+ /// an id.
228
+ CALayer *_observedLayer;
229
+ /// The item the output is on, and the output.
230
+ AVPlayerItem *_item;
231
+ AVPlayerItemVideoOutput *_output;
232
+ }
233
+
234
+ /// Runs `block` on Lynx's engine thread, where the canvas registry lives.
235
+ - (void)onEngineThread:(dispatch_block_t)block {
236
+ [_lynxContext runOnTasmThread:block];
237
+ }
238
+
239
+ LYNX_PROP_SETTER("video-id", setVideoId, NSString *) {
240
+ _lynxContext = self.context.lynxContext;
241
+ NSString *videoId = requestReset || ![value isKindOfClass:NSString.class] ? nil : value;
242
+ if (videoId == _videoId || [videoId isEqualToString:_videoId]) return;
243
+ NSString *old = _videoId;
244
+ AngularLynxVideoFrames *oldFrames = _frames;
245
+ _videoId = [videoId copy];
246
+ _frames = videoId ? [[AngularLynxVideoFrames alloc] initWithVideoId:videoId] : nil;
247
+ AngularLynxVideoFrames *frames = _frames;
248
+ // A new id on the same item: its frames read the output already there,
249
+ // from the frame the old ones last read.
250
+ if (oldFrames) {
251
+ [frames takeOutputAndFrameFrom:oldFrames];
252
+ } else {
253
+ [frames setOutput:_output];
254
+ }
255
+ [self onEngineThread:^{
256
+ if (old && [AngularLynxCanvasRegistry contextForCanvasId:old] == oldFrames) {
257
+ [AngularLynxCanvasRegistry setContext:nil forCanvasId:old];
258
+ }
259
+ if (frames) [AngularLynxCanvasRegistry setContext:frames forCanvasId:frames.canvasId];
260
+ }];
261
+ [self observePlayer:videoId != nil];
262
+ [self attachOutputTo:[self currentItem]];
263
+ }
264
+
265
+ /// How many times, a frame apart, `bindfirstframe` waits for the output's
266
+ /// first frame: half a second at most.
267
+ static const NSInteger kFirstFrameTries = 30;
268
+
269
+ /// A frame at 60 Hz, in nanoseconds: how long each try waits.
270
+ static const int64_t kFrameNanoseconds = 16666667;
271
+
272
+ /// XElement's view says it has a first frame when its layer can show one,
273
+ /// which is before the output added to the same item hands that frame over:
274
+ /// a canvas drawing the video from `bindfirstframe` read nothing, which the
275
+ /// E2E canvas flow caught. So the event waits for the output's frame, a
276
+ /// frame at a time and for half a second at most, then goes as XElement's
277
+ /// would. It also waits for the track's transform, past the half second if
278
+ /// it has to: the frames give none without it, and its load always ends
279
+ /// (loadTransformOf:forOutput:), so a handler that draws once gets the
280
+ /// frame turned. Android's frame is there first already
281
+ /// (VideoUI.onFirstFrame).
282
+ - (void)playableDidLoadFirstFrame:(id<LynxVideoPlayable>)playable
283
+ duration:(NSTimeInterval)duration {
284
+ [self sendFirstFrame:playable item:_item duration:duration triesLeft:kFirstFrameTries];
285
+ }
286
+
287
+ /// `item` is the one the first frame is of: a new `src` while the event
288
+ /// waits makes it stale, and the new item's player sends one of its own.
289
+ - (void)sendFirstFrame:(id<LynxVideoPlayable>)playable
290
+ item:(AVPlayerItem *)item
291
+ duration:(NSTimeInterval)duration
292
+ triesLeft:(NSInteger)triesLeft {
293
+ if (item != _item) return;
294
+ if (!_frames || [_frames frameSize].width > 0 ||
295
+ (triesLeft <= 0 && ![_frames waitsForTransform])) {
296
+ [super playableDidLoadFirstFrame:playable duration:duration];
297
+ return;
298
+ }
299
+ __weak typeof(self) weakSelf = self;
300
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, kFrameNanoseconds),
301
+ dispatch_get_main_queue(), ^{
302
+ [weakSelf sendFirstFrame:playable
303
+ item:item
304
+ duration:duration
305
+ triesLeft:triesLeft - 1];
306
+ });
307
+ }
308
+
309
+ /// Watches the player's item while the video has an id, so each item, the
310
+ /// first and each new `src`'s, gets an output. XElement's view makes its
311
+ /// player with the first source, and replaces the item on the UI thread.
312
+ - (void)observePlayer:(BOOL)observe {
313
+ CALayer *layer = self.videoView.layer;
314
+ if (observe && !_observedLayer && [layer isKindOfClass:AVPlayerLayer.class]) {
315
+ [layer addObserver:self
316
+ forKeyPath:@"player.currentItem"
317
+ options:NSKeyValueObservingOptionNew
318
+ context:kAngularLynxVideoItemContext];
319
+ _observedLayer = layer;
320
+ } else if (!observe && _observedLayer) {
321
+ [_observedLayer removeObserver:self
322
+ forKeyPath:@"player.currentItem"
323
+ context:kAngularLynxVideoItemContext];
324
+ _observedLayer = nil;
325
+ }
326
+ }
327
+
328
+ - (AVPlayerItem *)currentItem {
329
+ CALayer *layer = self.videoView.layer;
330
+ if (![layer isKindOfClass:AVPlayerLayer.class]) return nil;
331
+ return ((AVPlayerLayer *)layer).player.currentItem;
332
+ }
333
+
334
+ - (void)observeValueForKeyPath:(NSString *)keyPath
335
+ ofObject:(id)object
336
+ change:(NSDictionary *)change
337
+ context:(void *)context {
338
+ if (context != kAngularLynxVideoItemContext) {
339
+ [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
340
+ return;
341
+ }
342
+ // AVPlayer tells of a new item on the thread that replaced it, which for
343
+ // XElement's view is the UI thread; the outputs are changed there.
344
+ dispatch_block_t attach = ^{
345
+ [self attachOutputTo:[self currentItem]];
346
+ };
347
+ if (NSThread.isMainThread) {
348
+ attach();
349
+ } else {
350
+ dispatch_async(dispatch_get_main_queue(), attach);
351
+ }
352
+ }
353
+
354
+ /// Moves the output to `item`: a new one for each item, since an output
355
+ /// belongs to one item at a time, and none without an id.
356
+ - (void)attachOutputTo:(AVPlayerItem *)item {
357
+ if (!_videoId) item = nil;
358
+ if (item == _item) return;
359
+ if (_item && _output) [_item removeOutput:_output];
360
+ _item = item;
361
+ _output = item ? [[AVPlayerItemVideoOutput alloc] initWithPixelBufferAttributes:nil] : nil;
362
+ if (_output) [item addOutput:_output];
363
+ [_frames setOutput:_output];
364
+ if (_output) [self loadTransformOf:item forOutput:_output];
365
+ }
366
+
367
+ /// Loads how `item`'s video track is shown, its preferredTransform, for the
368
+ /// frames reading `output`: a phone records portrait video landscape, with a
369
+ /// transform that turns it upright, which the output's frames don't have.
370
+ /// Loaded without blocking, as AVFoundation asks; a file whose track or
371
+ /// transform won't load is shown as stored, as the frames were before. The
372
+ /// track is the first enabled one, the one the player shows: a file can hold
373
+ /// a disabled video track ahead of it. They're the asset's tracks rather
374
+ /// than the item's (`item.tracks`), which are empty until the item is ready
375
+ /// to play, and the frames have none to give until this loads. Every path
376
+ /// ends in `loaded`, which the first-frame wait relies on.
377
+ - (void)loadTransformOf:(AVPlayerItem *)item forOutput:(AVPlayerItemVideoOutput *)output {
378
+ __weak typeof(self) weakSelf = self;
379
+ void (^loaded)(CGAffineTransform) = ^(CGAffineTransform transform) {
380
+ // Back on the UI thread, where the element's frames are replaced: a new
381
+ // video-id while this loaded has new frames on the same output.
382
+ dispatch_async(dispatch_get_main_queue(), ^{
383
+ typeof(self) strongSelf = weakSelf;
384
+ if (strongSelf) [strongSelf->_frames setTransform:transform forOutput:output];
385
+ });
386
+ };
387
+ [item.asset
388
+ loadTracksWithMediaType:AVMediaTypeVideo
389
+ completionHandler:^(NSArray<AVAssetTrack *> *tracks, NSError *error) {
390
+ if (tracks.count == 0) {
391
+ loaded(CGAffineTransformIdentity);
392
+ return;
393
+ }
394
+ NSArray<NSString *> *keys = @[ @"enabled", @"preferredTransform" ];
395
+ dispatch_group_t group = dispatch_group_create();
396
+ for (AVAssetTrack *track in tracks) {
397
+ dispatch_group_enter(group);
398
+ [track loadValuesAsynchronouslyForKeys:keys
399
+ completionHandler:^{
400
+ dispatch_group_leave(group);
401
+ }];
402
+ }
403
+ dispatch_group_notify(group, dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
404
+ AVAssetTrack *shown = tracks.firstObject;
405
+ for (AVAssetTrack *track in tracks) {
406
+ if ([track statusOfValueForKey:@"enabled" error:NULL] ==
407
+ AVKeyValueStatusLoaded &&
408
+ track.enabled) {
409
+ shown = track;
410
+ break;
411
+ }
412
+ }
413
+ loaded([shown statusOfValueForKey:@"preferredTransform" error:NULL] ==
414
+ AVKeyValueStatusLoaded
415
+ ? shown.preferredTransform
416
+ : CGAffineTransformIdentity);
417
+ });
418
+ }];
419
+ }
420
+
421
+ - (void)dealloc {
422
+ [self observePlayer:NO];
423
+ if (_item && _output) [_item removeOutput:_output];
424
+ NSString *videoId = _videoId;
425
+ AngularLynxVideoFrames *frames = _frames;
426
+ if (videoId) {
427
+ [self onEngineThread:^{
428
+ if ([AngularLynxCanvasRegistry contextForCanvasId:videoId] == frames) {
429
+ [AngularLynxCanvasRegistry setContext:nil forCanvasId:videoId];
430
+ }
431
+ }];
432
+ }
433
+ }
434
+
435
+ @end
@@ -57,6 +57,12 @@ NS_ASSUME_NONNULL_BEGIN
57
57
  /// (runOnTasmThread:). The canvas element sets it before the canvas id.
58
58
  @property(nonatomic, weak, nullable) LynxContext *lynxContext;
59
59
  @property(nonatomic, readonly) AngularLynxCanvasTarget *target;
60
+ /// Shows a 2D context's bitmap, scaled to the view (nil shows nothing). UI
61
+ /// thread only.
62
+ /// Shows nothing for a frame of another canvas than the view's now.
63
+ - (void)showImage:(nullable CGImageRef)image
64
+ opaque:(BOOL)opaque
65
+ canvasId:(NSString *)canvasId;
60
66
  @end
61
67
 
62
68
  /// The `canvas` element. Its `canvas-id` attribute, which the runtime sets,
@@ -72,13 +78,36 @@ NS_ASSUME_NONNULL_BEGIN
72
78
  @property(nonatomic, readonly) NSInteger height;
73
79
  /// Whether the pixels are premultiplied by alpha.
74
80
  @property(nonatomic, readonly) BOOL premultiplied;
81
+ - (instancetype)initWithPixels:(NSData *)pixels
82
+ width:(NSInteger)width
83
+ height:(NSInteger)height
84
+ premultiplied:(BOOL)premultiplied;
75
85
  + (nullable AngularLynxDecodedImage *)imageWithId:(NSInteger)imageId;
76
86
  @end
77
87
 
88
+ /// A canvas's context, of either kind: what the registry pairs with the
89
+ /// canvas's view, and what another context reads the canvas through (as a
90
+ /// texture, or drawn on a 2D canvas). Engine thread only.
91
+ @protocol AngularLynxCanvasContext <NSObject>
92
+ @property(nonatomic, readonly, copy) NSString *canvasId;
93
+ /// Shows frames in this canvas's target, or nowhere while it's nil.
94
+ - (void)attachTarget:(nullable AngularLynxCanvasTarget *)target;
95
+ /// The view was laid out: a frame that waited for it to have a size is shown.
96
+ - (void)targetDidLayOut:(AngularLynxCanvasTarget *)target;
97
+ /// The canvas's pixels now, rows top to bottom, or nil when it has none.
98
+ - (nullable AngularLynxDecodedImage *)snapshot;
99
+ /// After `OffscreenCanvas.transferToImageBitmap()` took the pixels: a 2D
100
+ /// bitmap is cleared to transparent black (opaque black without alpha), and
101
+ /// a WebGL drawing buffer too, even with preserveDrawingBuffer, which the HTML
102
+ /// spec says has no effect on a transfer. The context's state stays.
103
+ - (void)clearForTransfer;
104
+ @end
105
+
106
+ // angular-lynx:webgl
78
107
  /// One WebGL context: an EGL context on ANGLE's Metal display, with an
79
108
  /// offscreen default framebuffer the size of the canvas's `width` and
80
109
  /// `height`.
81
- @interface AngularLynxWebGLContext : NSObject
110
+ @interface AngularLynxWebGLContext : NSObject <AngularLynxCanvasContext>
82
111
  @property(nonatomic, readonly, copy) NSString *canvasId;
83
112
  - (nullable instancetype)initWithCanvasId:(NSString *)canvasId
84
113
  attributes:(NSDictionary *)attributes
@@ -90,12 +119,44 @@ NS_ASSUME_NONNULL_BEGIN
90
119
  - (NSDictionary *)encodeImageOfType:(NSString *)type
91
120
  quality:(double)quality
92
121
  premultiplied:(BOOL)premultiplied;
93
- /// Shows frames in this canvas's target, or nowhere while it's nil.
94
- - (void)attachTarget:(nullable AngularLynxCanvasTarget *)target;
95
- /// The view was laid out: a frame that waited for it to have a size is shown.
96
- - (void)targetDidLayOut:(AngularLynxCanvasTarget *)target;
97
122
  - (void)destroy;
98
123
  @end
124
+ // /angular-lynx:webgl
125
+
126
+ /// One 2D context: a Core Graphics bitmap the size of the canvas's `width`
127
+ /// and `height`, which the runtime's CanvasRenderingContext2D draws on (see
128
+ /// AngularLynxCanvas2DContext.m).
129
+ @interface AngularLynxCanvas2DContext : NSObject <AngularLynxCanvasContext>
130
+ @property(nonatomic, readonly, copy) NSString *canvasId;
131
+ /// `objects` holds the gradients and patterns of every 2D context in the
132
+ /// LynxView, which any of them can paint with.
133
+ - (nullable instancetype)initWithCanvasId:(NSString *)canvasId
134
+ attributes:(NSDictionary *)attributes
135
+ objects:(NSMutableDictionary<NSNumber *, id> *)objects
136
+ error:(NSString *_Nullable *_Nonnull)error;
137
+ /// Runs the commands in order; returns one result per query.
138
+ - (NSArray *)run:(NSArray *)commands;
139
+ /// The bitmap as a PNG or JPEG file: `{ base64, type }`, or `{ error }`.
140
+ - (NSDictionary *)encodeImageOfType:(NSString *)type quality:(double)quality;
141
+ @end
142
+
143
+ /// One bitmaprenderer context: ImageBitmapRenderingContext, which shows an
144
+ /// ImageBitmap's decoded pixels in the canvas's view as they are, scaled to
145
+ /// it, and reads as them (see AngularLynxBitmapRendererContext.m).
146
+ @interface AngularLynxBitmapRendererContext : NSObject <AngularLynxCanvasContext>
147
+ @property(nonatomic, readonly, copy) NSString *canvasId;
148
+ /// `attributes` holds `alpha`, and the canvas's `width` and `height`, which
149
+ /// the blank canvas it shows first is.
150
+ - (instancetype)initWithCanvasId:(NSString *)canvasId attributes:(NSDictionary *)attributes;
151
+ /// Shows `image`, which the context owns from then on, without copying its
152
+ /// pixels (but for a context without alpha, which shows them over black).
153
+ - (void)showImage:(AngularLynxDecodedImage *)image;
154
+ /// Shows a blank canvas of `width` by `height`: transparent black, or
155
+ /// opaque black without alpha.
156
+ - (void)showBlankOfWidth:(NSInteger)width height:(NSInteger)height;
157
+ /// What it shows as a PNG or JPEG file: `{ base64, type }`, or `{ error }`.
158
+ - (NSDictionary *)encodeImageOfType:(NSString *)type quality:(double)quality;
159
+ @end
99
160
 
100
161
  /// Pairs canvases with contexts by canvas id: either can come first, as the
101
162
  /// runtime may make the context before Lynx has made the element's view.
@@ -103,8 +164,9 @@ NS_ASSUME_NONNULL_BEGIN
103
164
  @interface AngularLynxCanvasRegistry : NSObject
104
165
  + (void)setTarget:(nullable AngularLynxCanvasTarget *)target forCanvasId:(NSString *)canvasId;
105
166
  + (nullable AngularLynxCanvasTarget *)targetForCanvasId:(NSString *)canvasId;
106
- + (void)setContext:(nullable AngularLynxWebGLContext *)context forCanvasId:(NSString *)canvasId;
107
- + (nullable AngularLynxWebGLContext *)contextForCanvasId:(NSString *)canvasId;
167
+ + (void)setContext:(nullable id<AngularLynxCanvasContext>)context
168
+ forCanvasId:(NSString *)canvasId;
169
+ + (nullable id<AngularLynxCanvasContext>)contextForCanvasId:(NSString *)canvasId;
108
170
  @end
109
171
 
110
172
  /// The `AngularLynxWebGL` module. Registered on the LynxView's config, and