appium-webdriveragent 16.18.4 → 16.18.5

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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,13 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [16.18.5](https://github.com/appium/appium-ios/compare/appium-webdriveragent@16.18.4...appium-webdriveragent@16.18.5) (2026-11-10)
7
+
8
+ ### Bug Fixes
9
+
10
+ * **webdriveragent:** keep a late MJPEG frame instead of backing off ([#179](https://github.com/appium/appium-ios/issues/179)) ([e57ef10](https://github.com/appium/appium-ios/commit/e57ef1069c155fbf37a3c223bcc96af7788025b3))
11
+
12
+
6
13
  ## [16.18.4](https://github.com/appium/appium-ios/compare/appium-webdriveragent@16.18.3...appium-webdriveragent@16.18.4) (2026-11-09)
7
14
 
8
15
  ### Performance Improvements
@@ -1176,6 +1176,7 @@
1176
1176
  EE3A18661CDE734B00DE4205 /* FBKeyboard.h in Headers */ = {isa = PBXBuildFile; fileRef = EE3A18641CDE734B00DE4205 /* FBKeyboard.h */; settings = {ATTRIBUTES = (Public, ); }; };
1177
1177
  EE3A18671CDE734B00DE4205 /* FBKeyboard.m in Sources */ = {isa = PBXBuildFile; fileRef = EE3A18651CDE734B00DE4205 /* FBKeyboard.m */; };
1178
1178
  EE3F8CFE1D08AA17006F02CE /* FBRunLoopSpinnerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = EE3F8CFD1D08AA17006F02CE /* FBRunLoopSpinnerTests.m */; };
1179
+ AA11BB22CC33DD44EE55FF11 /* FBMjpegServerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AA11BB22CC33DD44EE55FF12 /* FBMjpegServerTests.m */; };
1179
1180
  EE3F8D001D08B05F006F02CE /* FBElementTypeTransformerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = EE3F8CFF1D08B05F006F02CE /* FBElementTypeTransformerTests.m */; };
1180
1181
  EE5095E51EBCC9090028E2FE /* FBTypingTest.m in Sources */ = {isa = PBXBuildFile; fileRef = AD76723F1D6B826F00610457 /* FBTypingTest.m */; };
1181
1182
  EE5095EB1EBCC9090028E2FE /* XCElementSnapshotHitPointTests.m in Sources */ = {isa = PBXBuildFile; fileRef = EE006EB21EBA1C7B006900A4 /* XCElementSnapshotHitPointTests.m */; };
@@ -1888,6 +1889,7 @@
1888
1889
  EE3A18641CDE734B00DE4205 /* FBKeyboard.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FBKeyboard.h; path = WebDriverAgentLib/Utilities/FBKeyboard.h; sourceTree = SOURCE_ROOT; };
1889
1890
  EE3A18651CDE734B00DE4205 /* FBKeyboard.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FBKeyboard.m; path = WebDriverAgentLib/Utilities/FBKeyboard.m; sourceTree = SOURCE_ROOT; };
1890
1891
  EE3F8CFD1D08AA17006F02CE /* FBRunLoopSpinnerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FBRunLoopSpinnerTests.m; sourceTree = "<group>"; };
1892
+ AA11BB22CC33DD44EE55FF12 /* FBMjpegServerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FBMjpegServerTests.m; sourceTree = "<group>"; };
1891
1893
  EE3F8CFF1D08B05F006F02CE /* FBElementTypeTransformerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FBElementTypeTransformerTests.m; sourceTree = "<group>"; };
1892
1894
  EE5095FE1EBCC9090028E2FE /* IntegrationTests_2.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = IntegrationTests_2.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
1893
1895
  EE55B3221D1D5388003AAAEC /* FBTableDataSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FBTableDataSource.h; sourceTree = "<group>"; };
@@ -2762,6 +2764,7 @@
2762
2764
  76B7399DDF52C85A21433C1D /* FBHTTPServerTests.m */,
2763
2765
  EE9B76571CF7987300275851 /* FBRouteTests.m */,
2764
2766
  EE3F8CFD1D08AA17006F02CE /* FBRunLoopSpinnerTests.m */,
2767
+ AA11BB22CC33DD44EE55FF12 /* FBMjpegServerTests.m */,
2765
2768
  ADEF63AE1D09DEBE0070A7E3 /* FBRuntimeUtilsTests.m */,
2766
2769
  714801D01FA9D9FA00DC5997 /* FBSDKVersionTests.m */,
2767
2770
  EE6A89251D0B19E60083E92B /* FBSessionTests.m */,
@@ -4922,6 +4925,7 @@
4922
4925
  6C07E44E139D41D6BA092F39 /* FBTouchableViewTests.m in Sources */,
4923
4926
  713352FD26CEF31D00523CBC /* FBLRUCacheTests.m in Sources */,
4924
4927
  EE3F8CFE1D08AA17006F02CE /* FBRunLoopSpinnerTests.m in Sources */,
4928
+ AA11BB22CC33DD44EE55FF11 /* FBMjpegServerTests.m in Sources */,
4925
4929
  A09D847635CA4C155583B967 /* FBXCAXClientProxyTests.m in Sources */,
4926
4930
  714801D11FA9D9FA00DC5997 /* FBSDKVersionTests.m in Sources */,
4927
4931
  EE3F8D001D08B05F006F02CE /* FBElementTypeTransformerTests.m in Sources */,
@@ -15,11 +15,11 @@
15
15
  <key>CFBundlePackageType</key>
16
16
  <string>FMWK</string>
17
17
  <key>CFBundleShortVersionString</key>
18
- <string>16.18.4</string>
18
+ <string>16.18.5</string>
19
19
  <key>CFBundleSignature</key>
20
20
  <string>????</string>
21
21
  <key>CFBundleVersion</key>
22
- <string>16.18.4</string>
22
+ <string>16.18.5</string>
23
23
  <key>NSPrincipalClass</key>
24
24
  <string/>
25
25
  </dict>
@@ -14,6 +14,7 @@
14
14
  // Textual import, not `@import Network;` - see the comment in FBTCPSocket.h.
15
15
  #import <Network/Network.h>
16
16
  #import "FBConfiguration.h"
17
+ #import "FBErrorBuilder.h"
17
18
  #import "FBLogger.h"
18
19
  #import "FBScreenshot.h"
19
20
  #import "FBScreen.h"
@@ -22,11 +23,13 @@
22
23
  #import "XCUIScreen.h"
23
24
 
24
25
  static const NSUInteger MAX_FPS = 60;
25
- static const NSTimeInterval FRAME_TIMEOUT = 1.;
26
+ static const NSTimeInterval DEFAULT_FRAME_TIMEOUT = 1.;
27
+ // A frame that misses the frame timeout is still awaited this long (testmanagerd answers it after its snapshot).
28
+ static const NSTimeInterval DEFAULT_LATE_FRAME_LIMIT = 10.;
26
29
  // nw_connection_send buffers without backpressure, so a client that stops reading would retain
27
30
  // every generated frame. Frames past this cap are dropped instead of queued.
28
31
  static const NSUInteger MAX_PENDING_FRAMES_PER_CLIENT = 4;
29
- static const NSTimeInterval FAILURE_BACKOFF_MIN = 1.0;
32
+ static const NSTimeInterval DEFAULT_FAILURE_BACKOFF_MIN = 1.0;
30
33
  static const NSTimeInterval FAILURE_BACKOFF_MAX = 10.0;
31
34
  // The screen lookup is a round trip to testmanagerd, so it is not repeated for every frame.
32
35
  static const uint64_t SCREEN_REFRESH_INTERVAL = 3 * NSEC_PER_SEC;
@@ -39,6 +42,56 @@ static NSUInteger FBNormalizedMjpegFramerate(NSUInteger framerate)
39
42
  return (0 == framerate || framerate > MAX_FPS) ? MAX_FPS : framerate;
40
43
  }
41
44
 
45
+ // A screenshot request whose answer may arrive after the stream stopped waiting for it.
46
+ @interface FBMjpegPendingFrame : NSObject
47
+ @property (nonatomic, readonly, nullable) NSData *data;
48
+ @property (nonatomic, readonly, nullable) NSError *error;
49
+ // What the request was made for: a frame is not reused for other listeners or another display.
50
+ @property (nonatomic, readonly) NSUInteger epoch;
51
+ @property (nonatomic, readonly, nullable) NSNumber *displayId;
52
+ - (instancetype)initWithEpoch:(NSUInteger)epoch displayId:(nullable NSNumber *)displayId;
53
+ - (void)fulfillWithData:(nullable NSData *)data error:(nullable NSError *)error;
54
+ - (BOOL)isOlderThan:(NSTimeInterval)seconds;
55
+ // Returns NO if there is no answer yet.
56
+ - (BOOL)waitWithTimeout:(NSTimeInterval)timeout;
57
+ @end
58
+
59
+ @implementation FBMjpegPendingFrame {
60
+ dispatch_semaphore_t _answered;
61
+ uint64_t _timeRequested;
62
+ }
63
+
64
+ - (instancetype)initWithEpoch:(NSUInteger)epoch displayId:(NSNumber *)displayId
65
+ {
66
+ if ((self = [super init])) {
67
+ _epoch = epoch;
68
+ _displayId = displayId;
69
+ _answered = dispatch_semaphore_create(0);
70
+ _timeRequested = clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW);
71
+ }
72
+ return self;
73
+ }
74
+
75
+ - (BOOL)isOlderThan:(NSTimeInterval)seconds
76
+ {
77
+ return clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW) - _timeRequested >= (uint64_t)(seconds * NSEC_PER_SEC);
78
+ }
79
+
80
+ // The result is written before the signal and read only after the wait succeeds.
81
+ - (void)fulfillWithData:(NSData *)data error:(NSError *)error
82
+ {
83
+ _data = data;
84
+ _error = error;
85
+ dispatch_semaphore_signal(_answered);
86
+ }
87
+
88
+ - (BOOL)waitWithTimeout:(NSTimeInterval)timeout
89
+ {
90
+ return 0 == dispatch_semaphore_wait(_answered, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC)));
91
+ }
92
+
93
+ @end
94
+
42
95
 
43
96
  @interface FBMjpegServer()
44
97
 
@@ -54,6 +107,15 @@ static NSUInteger FBNormalizedMjpegFramerate(NSUInteger framerate)
54
107
  @property (nonatomic, strong, nullable) XCUIScreen *cachedScreen;
55
108
  @property (nonatomic, strong, nullable) NSNumber *cachedScreenDisplayId;
56
109
  @property (nonatomic, assign) uint64_t cachedScreenTimestamp;
110
+ // The request that missed the frame timeout and is still awaited. Touched only on backgroundQueue.
111
+ @property (nonatomic, strong, nullable) FBMjpegPendingFrame *pendingFrame;
112
+ // Bumped when the last client leaves. Guarded by @synchronized (self.listeningClients).
113
+ @property (nonatomic, assign) NSUInteger listenersEpoch;
114
+ // Overridable in tests. The default sends the request to testmanagerd.
115
+ @property (atomic, copy) BOOL (^frameRequest)(void (^completion)(NSData * _Nullable, NSError * _Nullable), NSError **error);
116
+ @property (atomic, assign) NSTimeInterval frameTimeout;
117
+ @property (atomic, assign) NSTimeInterval lateFrameLimit;
118
+ @property (atomic, assign) NSTimeInterval failureBackoffMin;
57
119
  // Frames submitted but not sent yet, per client. Guarded by @synchronized (self.listeningClients).
58
120
  @property (nonatomic, readonly) NSMapTable<id, NSNumber *> *pendingFrameCounts;
59
121
 
@@ -67,6 +129,9 @@ static NSUInteger FBNormalizedMjpegFramerate(NSUInteger framerate)
67
129
  if ((self = [super init])) {
68
130
  _consecutiveScreenshotFailures = 0;
69
131
  _isStreaming = YES;
132
+ _frameTimeout = DEFAULT_FRAME_TIMEOUT;
133
+ _lateFrameLimit = DEFAULT_LATE_FRAME_LIMIT;
134
+ _failureBackoffMin = DEFAULT_FAILURE_BACKOFF_MIN;
70
135
  _sentFramesCount = 0;
71
136
  _sentBytesCount = 0;
72
137
  _listeningClients = [NSMutableArray array];
@@ -76,6 +141,9 @@ static NSUInteger FBNormalizedMjpegFramerate(NSUInteger framerate)
76
141
  dispatch_queue_attr_t queueAttributes = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_UTILITY, 0);
77
142
  _backgroundQueue = dispatch_queue_create(QUEUE_NAME, queueAttributes);
78
143
  __weak typeof(self) weakSelf = self;
144
+ _frameRequest = ^BOOL(void (^completion)(NSData *, NSError *), NSError **error) {
145
+ return [weakSelf requestScreenshotWithCompletion:completion error:error];
146
+ };
79
147
  dispatch_async(_backgroundQueue, ^{
80
148
  [weakSelf streamScreenshot];
81
149
  });
@@ -122,6 +190,67 @@ static NSUInteger FBNormalizedMjpegFramerate(NSUInteger framerate)
122
190
  return self.cachedScreen;
123
191
  }
124
192
 
193
+ - (BOOL)requestScreenshotWithCompletion:(void (^)(NSData * _Nullable, NSError * _Nullable))completion
194
+ error:(NSError **)error
195
+ {
196
+ XCUIScreen *screen = [self currentScreenWithError:error];
197
+ if (nil == screen) {
198
+ return NO;
199
+ }
200
+ CGFloat compressionQuality = MAX(FBMinCompressionQuality,
201
+ MIN(FBMaxCompressionQuality, (double)FBConfiguration.sharedInstance.mjpegServerScreenshotQuality / 100.0));
202
+ return [FBScreenshot requestInOriginalResolutionWithScreenID:screen.displayID
203
+ compressionQuality:compressionQuality
204
+ uti:UTTypeJPEG
205
+ completion:completion
206
+ error:error];
207
+ }
208
+
209
+ - (nullable FBMjpegPendingFrame *)requestFrameWithEpoch:(NSUInteger)epoch error:(NSError **)error
210
+ {
211
+ FBMjpegPendingFrame *frame = [[FBMjpegPendingFrame alloc] initWithEpoch:epoch
212
+ displayId:FBConfiguration.sharedInstance.currentDisplayId];
213
+ BOOL isRequested = self.frameRequest(^(NSData *data, NSError *err) {
214
+ if (nil == data && nil == err) {
215
+ NSError *emptyError;
216
+ [[[FBErrorBuilder builder]
217
+ withDescription:@"The screenshot request has been answered without an image"]
218
+ buildError:&emptyError];
219
+ err = emptyError;
220
+ }
221
+ [frame fulfillWithData:data error:err];
222
+ }, error);
223
+ return isRequested ? frame : nil;
224
+ }
225
+
226
+ // NO if the frame was requested for other listeners or another display.
227
+ - (BOOL)isFrameCurrent:(FBMjpegPendingFrame *)frame
228
+ {
229
+ NSNumber *displayId = FBConfiguration.sharedInstance.currentDisplayId;
230
+ @synchronized (self.listeningClients) {
231
+ return frame.epoch == self.listenersEpoch
232
+ && (displayId == frame.displayId || [displayId isEqual:frame.displayId]);
233
+ }
234
+ }
235
+
236
+ - (nullable FBMjpegPendingFrame *)takePendingFrame
237
+ {
238
+ FBMjpegPendingFrame *frame = self.pendingFrame;
239
+ self.pendingFrame = nil;
240
+ return nil != frame && [self isFrameCurrent:frame] ? frame : nil;
241
+ }
242
+
243
+ - (void)handleScreenshotFailure:(nullable NSError *)error timeStarted:(uint64_t)timeStarted
244
+ {
245
+ self.cachedScreen = nil;
246
+ [FBLogger logFmt:@"%@", error.description];
247
+ self.consecutiveScreenshotFailures++;
248
+ NSTimeInterval backoffSeconds = MIN(FAILURE_BACKOFF_MAX,
249
+ self.failureBackoffMin * (1 << MIN(self.consecutiveScreenshotFailures, 4)));
250
+ uint64_t backoffInterval = (uint64_t)(backoffSeconds * NSEC_PER_SEC);
251
+ [self scheduleNextScreenshotWithInterval:backoffInterval timeStarted:timeStarted];
252
+ }
253
+
125
254
  - (void)streamScreenshot
126
255
  {
127
256
  if (!self.isStreaming) {
@@ -130,30 +259,47 @@ static NSUInteger FBNormalizedMjpegFramerate(NSUInteger framerate)
130
259
  NSUInteger framerate = FBNormalizedMjpegFramerate(FBConfiguration.sharedInstance.mjpegServerFramerate);
131
260
  uint64_t timerInterval = (uint64_t)(1.0 / (double)framerate * NSEC_PER_SEC);
132
261
  uint64_t timeStarted = clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW);
262
+ NSUInteger epoch;
133
263
  @synchronized (self.listeningClients) {
264
+ epoch = self.listenersEpoch;
134
265
  if (0 == self.listeningClients.count) {
266
+ self.pendingFrame = nil;
135
267
  [self scheduleNextScreenshotWithInterval:timerInterval timeStarted:timeStarted];
136
268
  return;
137
269
  }
138
270
  }
139
271
 
140
272
  NSError *error;
141
- CGFloat compressionQuality = MAX(FBMinCompressionQuality,
142
- MIN(FBMaxCompressionQuality, (double)FBConfiguration.sharedInstance.mjpegServerScreenshotQuality / 100.0));
143
- XCUIScreen *screen = [self currentScreenWithError:&error];
144
- NSData *screenshotData = nil == screen ? nil : [FBScreenshot takeInOriginalResolutionWithScreenID:screen.displayID
145
- compressionQuality:compressionQuality
146
- uti:UTTypeJPEG
147
- timeout:FRAME_TIMEOUT
148
- error:&error];
273
+ FBMjpegPendingFrame *frame = [self takePendingFrame];
274
+ BOOL wasLate = nil != frame;
275
+ frame = frame ?: [self requestFrameWithEpoch:epoch error:&error];
276
+ if (nil != frame) {
277
+ if ([frame waitWithTimeout:self.frameTimeout]) {
278
+ if (![self isFrameCurrent:frame]) {
279
+ // The audience or the display has changed while waiting; this is not a failure.
280
+ [self scheduleNextScreenshotWithInterval:0 timeStarted:timeStarted];
281
+ return;
282
+ }
283
+ error = frame.error;
284
+ } else if (![frame isOlderThan:self.lateFrameLimit]) {
285
+ if (!wasLate) {
286
+ [FBLogger verboseLog:@"The screenshot misses the frame timeout, still waiting for it"];
287
+ }
288
+ // No other request while this one is unanswered; keep waiting for it right away.
289
+ self.pendingFrame = frame;
290
+ [self scheduleNextScreenshotWithInterval:0 timeStarted:timeStarted];
291
+ return;
292
+ } else {
293
+ [[[FBErrorBuilder builder]
294
+ withDescriptionFormat:@"Cannot take a screenshot within %@ s timeout", @(self.lateFrameLimit)]
295
+ buildError:&error];
296
+ frame = nil;
297
+ }
298
+ }
299
+
300
+ NSData *screenshotData = frame.data;
149
301
  if (nil == screenshotData) {
150
- self.cachedScreen = nil;
151
- [FBLogger logFmt:@"%@", error.description];
152
- self.consecutiveScreenshotFailures++;
153
- NSTimeInterval backoffSeconds = MIN(FAILURE_BACKOFF_MAX,
154
- FAILURE_BACKOFF_MIN * (1 << MIN(self.consecutiveScreenshotFailures, 4)));
155
- uint64_t backoffInterval = (uint64_t)(backoffSeconds * NSEC_PER_SEC);
156
- [self scheduleNextScreenshotWithInterval:backoffInterval timeStarted:timeStarted];
302
+ [self handleScreenshotFailure:error timeStarted:timeStarted];
157
303
  return;
158
304
  }
159
305
 
@@ -164,13 +310,13 @@ static NSUInteger FBNormalizedMjpegFramerate(NSUInteger framerate)
164
310
  [self.imageProcessor submitImageData:screenshotData
165
311
  scalingFactor:scalingFactor
166
312
  completionHandler:^(NSData * _Nonnull scaled) {
167
- [weakSelf sendScreenshot:scaled];
313
+ [weakSelf sendScreenshot:scaled forFrame:frame];
168
314
  }];
169
315
 
170
316
  [self scheduleNextScreenshotWithInterval:timerInterval timeStarted:timeStarted];
171
317
  }
172
318
 
173
- - (void)sendScreenshot:(NSData *)screenshotData {
319
+ - (void)sendScreenshot:(NSData *)screenshotData forFrame:(FBMjpegPendingFrame *)frame {
174
320
  if (!self.isStreaming) {
175
321
  return;
176
322
  }
@@ -179,7 +325,7 @@ static NSUInteger FBNormalizedMjpegFramerate(NSUInteger framerate)
179
325
  [chunk appendData:screenshotData];
180
326
  [chunk appendData:(id)[@"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
181
327
  @synchronized (self.listeningClients) {
182
- if (!self.isStreaming || 0 == self.listeningClients.count) {
328
+ if (!self.isStreaming || 0 == self.listeningClients.count || ![self isFrameCurrent:frame]) {
183
329
  return;
184
330
  }
185
331
  NSUInteger clientCount = self.listeningClients.count;
@@ -246,6 +392,9 @@ static NSUInteger FBNormalizedMjpegFramerate(NSUInteger framerate)
246
392
  @synchronized (self.listeningClients) {
247
393
  [self.listeningClients removeObject:client];
248
394
  [self.pendingFrameCounts removeObjectForKey:client];
395
+ if (0 == self.listeningClients.count) {
396
+ self.listenersEpoch++;
397
+ }
249
398
  }
250
399
  // Nothing is owed to a client that stopped listening, so its socket can go right away.
251
400
  nw_connection_cancel(client);
@@ -39,6 +39,22 @@ NS_ASSUME_NONNULL_BEGIN
39
39
  timeout:(NSTimeInterval)timeout
40
40
  error:(NSError **)error;
41
41
 
42
+ /**
43
+ Requests a non-scaled screenshot without waiting for the answer
44
+
45
+ @param screenID The screen identifier to take the screenshot from
46
+ @param compressionQuality Normalized screenshot quality value in range 0..1, where 1 is the best quality
47
+ @param uti UTType... constant, which defines the type of the returned screenshot image
48
+ @param completion Called once, on an arbitrary queue and at any time later, with either the image data or the error
49
+ @param error If the request cannot be made, upon return contains an NSError object that describes the problem.
50
+ @return YES if the request has been sent, in which case the completion will be called
51
+ */
52
+ + (BOOL)requestInOriginalResolutionWithScreenID:(long long)screenID
53
+ compressionQuality:(CGFloat)compressionQuality
54
+ uti:(UTType *)uti
55
+ completion:(void (^)(NSData * _Nullable data, NSError * _Nullable error))completion
56
+ error:(NSError **)error;
57
+
42
58
  @end
43
59
 
44
60
  NS_ASSUME_NONNULL_END
@@ -93,36 +93,52 @@ NSString *formatTimeInterval(NSTimeInterval interval) {
93
93
  error:error];
94
94
  }
95
95
 
96
+ + (BOOL)requestInOriginalResolutionWithScreenID:(long long)screenID
97
+ compressionQuality:(CGFloat)compressionQuality
98
+ uti:(UTType *)uti
99
+ completion:(void (^)(NSData * _Nullable, NSError * _Nullable))completion
100
+ error:(NSError **)error
101
+ {
102
+ id screenshotRequest = [self.class screenshotRequestWithScreenID:screenID
103
+ rect:CGRectNull
104
+ uti:uti
105
+ compressionQuality:compressionQuality
106
+ error:error];
107
+ if (nil == screenshotRequest) {
108
+ return NO;
109
+ }
110
+ id<XCTMessagingChannel_RunnerToDaemon> proxy = [FBXCTestDaemonsProxy testRunnerProxy];
111
+ [proxy _XCT_requestScreenshot:screenshotRequest
112
+ withReply:^(id image, NSError *err) {
113
+ completion(nil == err ? [image data] : nil, err);
114
+ }];
115
+ return YES;
116
+ }
117
+
96
118
  + (NSData *)takeInOriginalResolutionWithScreenID:(long long)screenID
97
119
  compressionQuality:(CGFloat)compressionQuality
98
120
  uti:(UTType *)uti
99
121
  timeout:(NSTimeInterval)timeout
100
122
  error:(NSError **)error
101
123
  {
102
- id<XCTMessagingChannel_RunnerToDaemon> proxy = [FBXCTestDaemonsProxy testRunnerProxy];
103
124
  __block NSData *screenshotData = nil;
104
125
  __block NSError *innerError = nil;
105
126
  dispatch_semaphore_t sem = dispatch_semaphore_create(0);
106
- id screnshotRequest = [self.class screenshotRequestWithScreenID:screenID
107
- rect:CGRectNull
108
- uti:uti
109
- compressionQuality:compressionQuality
110
- error:error];
111
- if (nil == screnshotRequest) {
127
+ BOOL isRequested = [self.class requestInOriginalResolutionWithScreenID:screenID
128
+ compressionQuality:compressionQuality
129
+ uti:uti
130
+ completion:^(NSData *data, NSError *err) {
131
+ screenshotData = data;
132
+ innerError = err;
133
+ dispatch_semaphore_signal(sem);
134
+ }
135
+ error:error];
136
+ if (!isRequested) {
112
137
  return nil;
113
138
  }
114
- [proxy _XCT_requestScreenshot:screnshotRequest
115
- withReply:^(id image, NSError *err) {
116
- if (nil != err) {
117
- innerError = err;
118
- } else {
119
- screenshotData = [image data];
120
- }
121
- dispatch_semaphore_signal(sem);
122
- }];
123
139
  int64_t timeoutNs = (int64_t)(timeout * NSEC_PER_SEC);
124
140
  if (0 != dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, timeoutNs))) {
125
- NSString *timeoutMsg = [NSString stringWithFormat:@"Cannot take a screenshot within %@ timeout", formatTimeInterval(SCREENSHOT_TIMEOUT)];
141
+ NSString *timeoutMsg = [NSString stringWithFormat:@"Cannot take a screenshot within %@ timeout", formatTimeInterval(timeout)];
126
142
  if (nil == error) {
127
143
  [FBLogger log:timeoutMsg];
128
144
  } else if (nil == innerError) {
@@ -0,0 +1,357 @@
1
+ /**
2
+ * Copyright (c) 2015-present, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #import <XCTest/XCTest.h>
10
+
11
+ #import "FBConfiguration.h"
12
+ #import "FBMjpegServer.h"
13
+ #import "FBTCPSocket.h"
14
+
15
+ typedef void (^FBFrameCompletion)(NSData * _Nullable, NSError * _Nullable);
16
+
17
+ // Redeclared here, the implementations live in FBMjpegServer.m.
18
+ @interface FBMjpegServer (Testing)
19
+ @property (atomic, copy) BOOL (^frameRequest)(FBFrameCompletion completion, NSError **error);
20
+ @property (atomic, assign) NSTimeInterval frameTimeout;
21
+ @property (atomic, assign) NSTimeInterval lateFrameLimit;
22
+ @property (atomic, assign) NSTimeInterval failureBackoffMin;
23
+ - (void)sendScreenshot:(NSData *)screenshotData forFrame:(id)frame;
24
+ @end
25
+
26
+ @interface NSObject (FBMjpegPendingFrameTesting)
27
+ - (instancetype)initWithEpoch:(NSUInteger)epoch displayId:(nullable NSNumber *)displayId;
28
+ @end
29
+
30
+ // Counts the frames that reach a client instead of writing them anywhere.
31
+ @interface FBMjpegRecordingSocket : FBTCPSocket
32
+ @property (atomic, assign) NSUInteger framesCount;
33
+ @end
34
+
35
+ @implementation FBMjpegRecordingSocket
36
+
37
+ - (void)writeData:(NSData *)data toClient:(nw_connection_t)client
38
+ {
39
+ }
40
+
41
+ - (void)writeData:(NSData *)data toClient:(nw_connection_t)client completion:(void (^)(BOOL))completion
42
+ {
43
+ self.framesCount++;
44
+ if (nil != completion) {
45
+ completion(YES);
46
+ }
47
+ }
48
+
49
+ @end
50
+
51
+
52
+ @interface FBMjpegServerTests : XCTestCase
53
+ @property (nonatomic, strong) FBMjpegServer *server;
54
+ @property (nonatomic, strong) FBMjpegRecordingSocket *socket;
55
+ @property (nonatomic, strong) NSMutableArray<FBFrameCompletion> *requests;
56
+ @property (nonatomic, strong) NSLock *requestsLock;
57
+ @property (nonatomic, strong) NSData *frameData;
58
+ @property (nonatomic, strong, nullable) NSError *requestError;
59
+ @property (nonatomic, strong) NSMutableArray<id> *clients;
60
+ @end
61
+
62
+ @implementation FBMjpegServerTests
63
+
64
+ - (void)setUp
65
+ {
66
+ [super setUp];
67
+ self.requests = [NSMutableArray array];
68
+ self.requestsLock = [[NSLock alloc] init];
69
+ self.clients = [NSMutableArray array];
70
+ self.requestError = nil;
71
+ UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:CGSizeMake(4, 4)];
72
+ self.frameData = [renderer JPEGDataWithCompressionQuality:0.9 actions:^(UIGraphicsImageRendererContext *ctx) {
73
+ [UIColor.redColor setFill];
74
+ [ctx fillRect:CGRectMake(0, 0, 4, 4)];
75
+ }];
76
+
77
+ self.socket = [[FBMjpegRecordingSocket alloc] initWithPort:0];
78
+ self.server = [[FBMjpegServer alloc] init];
79
+ self.server.socket = self.socket;
80
+ self.server.frameTimeout = 0.05;
81
+ self.server.lateFrameLimit = 5;
82
+ // The first failure waits 2 s, which is much more than any check below waits for.
83
+ self.server.failureBackoffMin = 1.0;
84
+ __weak typeof(self) weakSelf = self;
85
+ self.server.frameRequest = ^BOOL(FBFrameCompletion completion, NSError **error) {
86
+ typeof(self) strongSelf = weakSelf;
87
+ if (nil == strongSelf) {
88
+ return NO;
89
+ }
90
+ if (nil != strongSelf.requestError) {
91
+ *error = strongSelf.requestError;
92
+ [strongSelf recordRequest:nil];
93
+ return NO;
94
+ }
95
+ [strongSelf recordRequest:completion];
96
+ return YES;
97
+ };
98
+ }
99
+
100
+ - (void)tearDown
101
+ {
102
+ FBConfiguration.sharedInstance.currentDisplayId = nil;
103
+ [self.server stopStreaming];
104
+ [super tearDown];
105
+ }
106
+
107
+ #pragma mark - Helpers
108
+
109
+ - (void)recordRequest:(nullable FBFrameCompletion)completion
110
+ {
111
+ [self.requestsLock lock];
112
+ [self.requests addObject:completion ?: ^(NSData *data, NSError *error) {}];
113
+ [self.requestsLock unlock];
114
+ }
115
+
116
+ - (NSUInteger)requestsCount
117
+ {
118
+ [self.requestsLock lock];
119
+ NSUInteger count = self.requests.count;
120
+ [self.requestsLock unlock];
121
+ return count;
122
+ }
123
+
124
+ - (FBFrameCompletion)requestAtIndex:(NSUInteger)index
125
+ {
126
+ [self.requestsLock lock];
127
+ FBFrameCompletion request = self.requests[index];
128
+ [self.requestsLock unlock];
129
+ return request;
130
+ }
131
+
132
+ - (BOOL)waitUntil:(BOOL (^)(void))condition timeout:(NSTimeInterval)timeout
133
+ {
134
+ NSDate *deadline = [NSDate dateWithTimeIntervalSinceNow:timeout];
135
+ while (!condition()) {
136
+ if ([deadline timeIntervalSinceNow] <= 0) {
137
+ return NO;
138
+ }
139
+ [NSThread sleepForTimeInterval:0.01];
140
+ }
141
+ return YES;
142
+ }
143
+
144
+ - (void)waitFor:(NSTimeInterval)seconds
145
+ {
146
+ [NSThread sleepForTimeInterval:seconds];
147
+ }
148
+
149
+ - (nw_connection_t)connectClient
150
+ {
151
+ nw_endpoint_t endpoint = nw_endpoint_create_host("127.0.0.1", "9");
152
+ nw_parameters_t parameters = nw_parameters_create_secure_tcp(NW_PARAMETERS_DISABLE_PROTOCOL,
153
+ NW_PARAMETERS_DEFAULT_CONFIGURATION);
154
+ // Never started, so nothing is actually connected.
155
+ nw_connection_t client = nw_connection_create(endpoint, parameters);
156
+ [self.clients addObject:client];
157
+ [self.server client:client didReceiveData:[NSData data]];
158
+ return client;
159
+ }
160
+
161
+ #pragma mark - Tests
162
+
163
+ - (void)testDoesNotRequestFramesWithoutClients
164
+ {
165
+ [self waitFor:0.3];
166
+ XCTAssertEqual(0, self.requestsCount);
167
+ }
168
+
169
+ - (void)testSendsAnsweredFrameAndRequestsNext
170
+ {
171
+ [self connectClient];
172
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
173
+ [self requestAtIndex:0](self.frameData, nil);
174
+
175
+ XCTAssertTrue([self waitUntil:^BOOL { return self.socket.framesCount >= 1; } timeout:5]);
176
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 2; } timeout:2],
177
+ @"The next frame must be requested after an answer within the timeout");
178
+ }
179
+
180
+ - (void)testLateFrameIsSentAfterTimeoutWithoutBackoff
181
+ {
182
+ [self connectClient];
183
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
184
+ // Several frame timeouts pass with no answer.
185
+ [self waitFor:0.4];
186
+ XCTAssertEqual(0, self.socket.framesCount);
187
+
188
+ [self requestAtIndex:0](self.frameData, nil);
189
+ XCTAssertTrue([self waitUntil:^BOOL { return self.socket.framesCount >= 1; } timeout:1],
190
+ @"The late answer must go out as a frame");
191
+ // A backoff would keep the next request away for 2 s.
192
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 2; } timeout:1],
193
+ @"The loop must go on at once after the late frame");
194
+ }
195
+
196
+ - (void)testSendsNoOtherRequestWhileFrameIsUnanswered
197
+ {
198
+ [self connectClient];
199
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
200
+ [self waitFor:0.5];
201
+ XCTAssertEqual(1, self.requestsCount);
202
+ XCTAssertEqual(0, self.socket.framesCount);
203
+ }
204
+
205
+ - (void)testGivesUpOnFrameAfterLimitAndBacksOff
206
+ {
207
+ self.server.lateFrameLimit = 0.2;
208
+ [self connectClient];
209
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
210
+ [self waitFor:1.0];
211
+ XCTAssertEqual(1, self.requestsCount, @"The failed frame must back off before the next request");
212
+
213
+ // The abandoned request is not sent even if it is answered later.
214
+ [self requestAtIndex:0](self.frameData, nil);
215
+ [self waitFor:0.2];
216
+ XCTAssertEqual(0, self.socket.framesCount);
217
+ }
218
+
219
+ - (void)testErrorAnswerBacksOff
220
+ {
221
+ [self connectClient];
222
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
223
+ NSError *error = [NSError errorWithDomain:@"test" code:1 userInfo:nil];
224
+ [self requestAtIndex:0](nil, error);
225
+ [self waitFor:1.0];
226
+ XCTAssertEqual(1, self.requestsCount);
227
+ XCTAssertEqual(0, self.socket.framesCount);
228
+ }
229
+
230
+ - (void)testFailedRequestBacksOff
231
+ {
232
+ self.requestError = [NSError errorWithDomain:@"test" code:2 userInfo:nil];
233
+ [self connectClient];
234
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
235
+ [self waitFor:1.0];
236
+ XCTAssertEqual(1, self.requestsCount);
237
+ XCTAssertEqual(0, self.socket.framesCount);
238
+ }
239
+
240
+ - (void)testDropsPendingFrameWhenLastClientLeaves
241
+ {
242
+ nw_connection_t client = [self connectClient];
243
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
244
+ [self.server didClientDisconnect:client];
245
+ // Let the loop notice that nobody listens.
246
+ [self waitFor:0.4];
247
+
248
+ [self requestAtIndex:0](self.frameData, nil);
249
+ [self waitFor:0.2];
250
+ XCTAssertEqual(0, self.socket.framesCount, @"A frame for a vanished audience must not be kept");
251
+
252
+ [self connectClient];
253
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 2; } timeout:2],
254
+ @"A returning client gets a fresh request");
255
+ [self requestAtIndex:1](self.frameData, nil);
256
+ XCTAssertTrue([self waitUntil:^BOOL { return self.socket.framesCount >= 1; } timeout:5]);
257
+ }
258
+
259
+ - (void)testDoesNotReuseFrameRequestedForPreviousListener
260
+ {
261
+ nw_connection_t first = [self connectClient];
262
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
263
+ // Swap the clients at once, before the loop sees that nobody listens.
264
+ [self.server didClientDisconnect:first];
265
+ [self connectClient];
266
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 2; } timeout:2],
267
+ @"The new client must not wait for a frame requested for the previous one");
268
+
269
+ [self requestAtIndex:0](self.frameData, nil);
270
+ [self waitFor:0.2];
271
+ XCTAssertEqual(0, self.socket.framesCount);
272
+ [self requestAtIndex:1](self.frameData, nil);
273
+ XCTAssertTrue([self waitUntil:^BOOL { return self.socket.framesCount >= 1; } timeout:5]);
274
+ }
275
+
276
+ - (void)testDoesNotReuseFrameRequestedForPreviousDisplay
277
+ {
278
+ [self connectClient];
279
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
280
+ FBConfiguration.sharedInstance.currentDisplayId = @1;
281
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 2; } timeout:2],
282
+ @"A display switch must request the frame again");
283
+
284
+ [self requestAtIndex:0](self.frameData, nil);
285
+ [self waitFor:0.2];
286
+ XCTAssertEqual(0, self.socket.framesCount);
287
+ }
288
+
289
+ - (void)testDropsFrameAnsweredAfterListenerSwitchedWhileWaiting
290
+ {
291
+ nw_connection_t first = [self connectClient];
292
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
293
+ [self waitFor:0.2];
294
+ // The frame is late now, so the next wait is a long one.
295
+ self.server.frameTimeout = 2;
296
+ [self waitFor:0.2];
297
+ [self.server didClientDisconnect:first];
298
+ [self connectClient];
299
+
300
+ [self requestAtIndex:0](self.frameData, nil);
301
+ [self waitFor:0.3];
302
+ XCTAssertEqual(0, self.socket.framesCount, @"The answer was requested for the previous listener");
303
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 2; } timeout:2],
304
+ @"Dropping the stale answer is not a failure, no backoff");
305
+ }
306
+
307
+ - (void)testDropsFrameAnsweredAfterDisplaySwitchedWhileWaiting
308
+ {
309
+ [self connectClient];
310
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
311
+ [self waitFor:0.2];
312
+ self.server.frameTimeout = 2;
313
+ [self waitFor:0.2];
314
+ FBConfiguration.sharedInstance.currentDisplayId = @1;
315
+
316
+ [self requestAtIndex:0](self.frameData, nil);
317
+ [self waitFor:0.3];
318
+ XCTAssertEqual(0, self.socket.framesCount, @"The answer was requested for the previous display");
319
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 2; } timeout:2]);
320
+ }
321
+
322
+ - (void)testDoesNotSendFrameThatWentStaleDuringScaling
323
+ {
324
+ [self connectClient];
325
+ Class frameClass = NSClassFromString(@"FBMjpegPendingFrame");
326
+ NSUInteger epoch = [[self.server valueForKey:@"listenersEpoch"] unsignedIntegerValue];
327
+
328
+ [self.server sendScreenshot:self.frameData forFrame:[[frameClass alloc] initWithEpoch:epoch + 1 displayId:nil]];
329
+ XCTAssertEqual(0, self.socket.framesCount, @"Other listeners");
330
+ [self.server sendScreenshot:self.frameData forFrame:[[frameClass alloc] initWithEpoch:epoch displayId:@7]];
331
+ XCTAssertEqual(0, self.socket.framesCount, @"Other display");
332
+ [self.server sendScreenshot:self.frameData forFrame:[[frameClass alloc] initWithEpoch:epoch displayId:nil]];
333
+ XCTAssertEqual(1, self.socket.framesCount);
334
+ }
335
+
336
+ - (void)testEmptyAnswerBacksOff
337
+ {
338
+ [self connectClient];
339
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
340
+ [self requestAtIndex:0](nil, nil);
341
+ [self waitFor:1.0];
342
+ XCTAssertEqual(1, self.requestsCount);
343
+ XCTAssertEqual(0, self.socket.framesCount);
344
+ }
345
+
346
+ - (void)testStopsRequestingAfterStopStreaming
347
+ {
348
+ [self connectClient];
349
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
350
+ [self.server stopStreaming];
351
+ [self requestAtIndex:0](self.frameData, nil);
352
+ [self waitFor:0.3];
353
+ XCTAssertEqual(1, self.requestsCount);
354
+ XCTAssertEqual(0, self.socket.framesCount);
355
+ }
356
+
357
+ @end
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "appium-webdriveragent",
3
- "version": "16.18.4",
3
+ "version": "16.18.5",
4
4
  "description": "Package bundling WebDriverAgent",
5
5
  "keywords": [
6
6
  "Appium",
@@ -83,5 +83,5 @@
83
83
  "node": "^20.19.0 || ^22.12.0 || >=24.0.0",
84
84
  "npm": ">=10"
85
85
  },
86
- "gitHead": "b79ff5587b7273c2ad1712a168114ba45641e665"
86
+ "gitHead": "83c02537ecaaee4964476ba35243e822cc378d00"
87
87
  }