FBScreenRecordingRequest.m 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. #import "FBScreenRecordingRequest.h"
  10. #import "FBErrorBuilder.h"
  11. #import "XCUIScreen.h"
  12. @implementation FBScreenRecordingRequest
  13. - (instancetype)initWithFps:(NSUInteger)fps codec:(long long)codec
  14. {
  15. if ((self = [super init])) {
  16. _fps = fps;
  17. _codec = codec;
  18. }
  19. return self;
  20. }
  21. - (nullable id)createVideoEncodingWithError:(NSError **)error
  22. {
  23. Class videoEncodingClass = NSClassFromString(@"XCTVideoEncoding");
  24. if (nil == videoEncodingClass) {
  25. [[[FBErrorBuilder builder]
  26. withDescription:@"Cannot find XCTVideoEncoding class"]
  27. buildError:error];
  28. return nil;
  29. }
  30. id videoEncodingAllocated = [videoEncodingClass alloc];
  31. SEL videoEncodingConstructorSelector = NSSelectorFromString(@"initWithCodec:frameRate:");
  32. if (![videoEncodingAllocated respondsToSelector:videoEncodingConstructorSelector]) {
  33. [[[FBErrorBuilder builder]
  34. withDescription:@"'initWithCodec:frameRate:' contructor is not found on XCTVideoEncoding class"]
  35. buildError:error];
  36. return nil;
  37. }
  38. NSMethodSignature *videoEncodingContructorSignature = [videoEncodingAllocated methodSignatureForSelector:videoEncodingConstructorSelector];
  39. NSInvocation *videoEncodingInitInvocation = [NSInvocation invocationWithMethodSignature:videoEncodingContructorSignature];
  40. [videoEncodingInitInvocation setSelector:videoEncodingConstructorSelector];
  41. long long codec = self.codec;
  42. [videoEncodingInitInvocation setArgument:&codec atIndex:2];
  43. double frameRate = self.fps;
  44. [videoEncodingInitInvocation setArgument:&frameRate atIndex:3];
  45. [videoEncodingInitInvocation invokeWithTarget:videoEncodingAllocated];
  46. id __unsafe_unretained result;
  47. [videoEncodingInitInvocation getReturnValue:&result];
  48. return result;
  49. }
  50. - (id)toNativeRequestWithError:(NSError **)error
  51. {
  52. Class screenRecordingRequestClass = NSClassFromString(@"XCTScreenRecordingRequest");
  53. if (nil == screenRecordingRequestClass) {
  54. [[[FBErrorBuilder builder]
  55. withDescription:@"Cannot find XCTScreenRecordingRequest class"]
  56. buildError:error];
  57. return nil;
  58. }
  59. id screenRecordingRequestAllocated = [screenRecordingRequestClass alloc];
  60. SEL screenRecordingRequestConstructorSelector = NSSelectorFromString(@"initWithScreenID:rect:preferredEncoding:");
  61. if (![screenRecordingRequestAllocated respondsToSelector:screenRecordingRequestConstructorSelector]) {
  62. [[[FBErrorBuilder builder]
  63. withDescription:@"'initWithScreenID:rect:preferredEncoding:' contructor is not found on XCTScreenRecordingRequest class"]
  64. buildError:error];
  65. return nil;
  66. }
  67. id videoEncoding = [self createVideoEncodingWithError:error];
  68. if (nil == videoEncoding) {
  69. return nil;
  70. }
  71. NSMethodSignature *screenRecordingRequestContructorSignature = [screenRecordingRequestAllocated methodSignatureForSelector:screenRecordingRequestConstructorSelector];
  72. NSInvocation *screenRecordingRequestInitInvocation = [NSInvocation invocationWithMethodSignature:screenRecordingRequestContructorSignature];
  73. [screenRecordingRequestInitInvocation setSelector:screenRecordingRequestConstructorSelector];
  74. long long mainScreenId = XCUIScreen.mainScreen.displayID;
  75. [screenRecordingRequestInitInvocation setArgument:&mainScreenId atIndex:2];
  76. CGRect fullScreenRect = CGRectNull;
  77. [screenRecordingRequestInitInvocation setArgument:&fullScreenRect atIndex:3];
  78. [screenRecordingRequestInitInvocation setArgument:&videoEncoding atIndex:4];
  79. [screenRecordingRequestInitInvocation invokeWithTarget:screenRecordingRequestAllocated];
  80. id __unsafe_unretained result;
  81. [screenRecordingRequestInitInvocation getReturnValue:&result];
  82. return result;
  83. }
  84. @end