FBVideoCommands.m 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 "FBVideoCommands.h"
  10. #import "FBRouteRequest.h"
  11. #import "FBScreenRecordingContainer.h"
  12. #import "FBScreenRecordingPromise.h"
  13. #import "FBScreenRecordingRequest.h"
  14. #import "FBSession.h"
  15. #import "FBXCTestDaemonsProxy.h"
  16. const NSUInteger DEFAULT_FPS = 24;
  17. const NSUInteger DEFAULT_CODEC = 0;
  18. @implementation FBVideoCommands
  19. + (NSArray *)routes
  20. {
  21. return
  22. @[
  23. [[FBRoute POST:@"/wda/video/start"] respondWithTarget:self action:@selector(handleStartVideoRecording:)],
  24. [[FBRoute POST:@"/wda/video/stop"] respondWithTarget:self action:@selector(handleStopVideoRecording:)],
  25. [[FBRoute GET:@"/wda/video"] respondWithTarget:self action:@selector(handleGetVideoRecording:)],
  26. [[FBRoute POST:@"/wda/video/start"].withoutSession respondWithTarget:self action:@selector(handleStartVideoRecording:)],
  27. [[FBRoute POST:@"/wda/video/stop"].withoutSession respondWithTarget:self action:@selector(handleStopVideoRecording:)],
  28. [[FBRoute GET:@"/wda/video"].withoutSession respondWithTarget:self action:@selector(handleGetVideoRecording:)],
  29. ];
  30. }
  31. + (id<FBResponsePayload>)handleStartVideoRecording:(FBRouteRequest *)request
  32. {
  33. FBScreenRecordingPromise *activeScreenRecording = FBScreenRecordingContainer.sharedInstance.screenRecordingPromise;
  34. if (nil != activeScreenRecording) {
  35. return FBResponseWithObject([FBScreenRecordingContainer.sharedInstance toDictionary] ?: [NSNull null]);
  36. }
  37. NSNumber *fps = (NSNumber *)request.arguments[@"fps"] ?: @(DEFAULT_FPS);
  38. NSNumber *codec = (NSNumber *)request.arguments[@"codec"] ?: @(DEFAULT_CODEC);
  39. FBScreenRecordingRequest *recordingRequest = [[FBScreenRecordingRequest alloc] initWithFps:fps.integerValue
  40. codec:codec.longLongValue];
  41. NSError *error;
  42. FBScreenRecordingPromise* promise = [FBXCTestDaemonsProxy startScreenRecordingWithRequest:recordingRequest
  43. error:&error];
  44. if (nil == promise) {
  45. [FBScreenRecordingContainer.sharedInstance reset];
  46. return FBResponseWithUnknownError(error);
  47. }
  48. [FBScreenRecordingContainer.sharedInstance storeScreenRecordingPromise:promise
  49. fps:fps.integerValue
  50. codec:codec.longLongValue];
  51. return FBResponseWithObject([FBScreenRecordingContainer.sharedInstance toDictionary]);
  52. }
  53. + (id<FBResponsePayload>)handleStopVideoRecording:(FBRouteRequest *)request
  54. {
  55. FBScreenRecordingPromise *activeScreenRecording = FBScreenRecordingContainer.sharedInstance.screenRecordingPromise;
  56. if (nil == activeScreenRecording) {
  57. return FBResponseWithOK();
  58. }
  59. NSUUID *recordingId = activeScreenRecording.identifier;
  60. NSDictionary *response = [FBScreenRecordingContainer.sharedInstance toDictionary];
  61. NSError *error;
  62. if (![FBXCTestDaemonsProxy stopScreenRecordingWithUUID:recordingId error:&error]) {
  63. [FBScreenRecordingContainer.sharedInstance reset];
  64. return FBResponseWithUnknownError(error);
  65. }
  66. [FBScreenRecordingContainer.sharedInstance reset];
  67. return FBResponseWithObject(response);
  68. }
  69. + (id<FBResponsePayload>)handleGetVideoRecording:(FBRouteRequest *)request
  70. {
  71. return FBResponseWithObject([FBScreenRecordingContainer.sharedInstance toDictionary] ?: [NSNull null]);
  72. }
  73. @end