2014 snapchat source code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
6.6 KiB

  1. //
  2. // SCCaptureBaseState.m
  3. // Snapchat
  4. //
  5. // Created by Lin Jia on 10/19/17.
  6. //
  7. //
  8. #import "SCCaptureBaseState.h"
  9. #import "SCCaptureStateMachineBookKeeper.h"
  10. #import "SCCapturerToken.h"
  11. #import "SCManagedCapturerV1_Private.h"
  12. #import <SCFoundation/SCAppEnvironment.h>
  13. #import <SCFoundation/SCAssertWrapper.h>
  14. #import <SCFoundation/SCQueuePerformer.h>
  15. @implementation SCCaptureBaseState {
  16. SCCaptureStateMachineBookKeeper *_bookKeeper;
  17. SCQueuePerformer *_performer;
  18. __weak id<SCCaptureStateDelegate> _delegate;
  19. }
  20. - (instancetype)initWithPerformer:(SCQueuePerformer *)performer
  21. bookKeeper:(SCCaptureStateMachineBookKeeper *)bookKeeper
  22. delegate:(id<SCCaptureStateDelegate>)delegate
  23. {
  24. self = [super init];
  25. if (self) {
  26. SCAssert(performer, @"");
  27. SCAssert(bookKeeper, @"");
  28. _bookKeeper = bookKeeper;
  29. _performer = performer;
  30. _delegate = delegate;
  31. }
  32. return self;
  33. }
  34. - (SCCaptureStateMachineStateId)stateId
  35. {
  36. return SCCaptureBaseStateId;
  37. }
  38. - (void)didBecomeCurrentState:(SCStateTransitionPayload *)payload
  39. resource:(SCCaptureResource *)resource
  40. context:(NSString *)context
  41. {
  42. [self _handleBaseStateBehavior:@"didBecomeCurrentState" context:context];
  43. }
  44. - (void)initializeCaptureWithDevicePosition:(SCManagedCaptureDevicePosition)devicePosition
  45. resource:(SCCaptureResource *)resource
  46. completionHandler:(dispatch_block_t)completionHandler
  47. context:(NSString *)context
  48. {
  49. [self _handleBaseStateBehavior:@"initializeCaptureWithDevicePosition" context:context];
  50. }
  51. - (void)startRunningWithCapturerToken:(SCCapturerToken *)token
  52. resource:(SCCaptureResource *)resource
  53. completionHandler:(dispatch_block_t)completionHandler
  54. context:(NSString *)context
  55. {
  56. [self _handleBaseStateBehavior:@"startRunningWithCapturerToken" context:context];
  57. }
  58. - (void)stopRunningWithCapturerToken:(SCCapturerToken *)token
  59. resource:(SCCaptureResource *)resource
  60. completionHandler:(sc_managed_capturer_stop_running_completion_handler_t)completionHandler
  61. context:(NSString *)context
  62. {
  63. SCAssertPerformer(_performer);
  64. BOOL actuallyStopped = [[SCManagedCapturerV1 sharedInstance] stopRunningWithCaptureToken:token
  65. completionHandler:completionHandler
  66. context:context];
  67. // TODO: Fix CCAM-14450
  68. // This is a temporary solution for https://jira.sc-corp.net/browse/CCAM-14450
  69. // It is caused by switching from scanning state to stop running state when the view is disappearing in the scanning
  70. // state, which can be reproduced by triggering scanning and then switch to maps page.
  71. // We remove SCAssert to ingore the crashes in master branch and will find a solution for the illegal call for the
  72. // state machine later
  73. if (self.stateId != SCCaptureScanningStateId) {
  74. SCAssert(!actuallyStopped, @"actuallyStopped in state: %@ with context: %@", SCCaptureStateName([self stateId]),
  75. context);
  76. } else {
  77. SCLogCaptureStateMachineInfo(@"actuallyStopped:%d in state: %@ with context: %@", actuallyStopped,
  78. SCCaptureStateName([self stateId]), context);
  79. }
  80. if (actuallyStopped) {
  81. [_delegate currentState:self
  82. requestToTransferToNewState:SCCaptureInitializedStateId
  83. payload:nil
  84. context:context];
  85. }
  86. }
  87. - (void)prepareForRecordingWithResource:(SCCaptureResource *)resource
  88. audioConfiguration:(SCAudioConfiguration *)configuration
  89. context:(NSString *)context
  90. {
  91. [self _handleBaseStateBehavior:@"prepareForRecordingWithResource" context:context];
  92. }
  93. - (void)startRecordingWithResource:(SCCaptureResource *)resource
  94. audioConfiguration:(SCAudioConfiguration *)configuration
  95. outputSettings:(SCManagedVideoCapturerOutputSettings *)outputSettings
  96. maxDuration:(NSTimeInterval)maxDuration
  97. fileURL:(NSURL *)fileURL
  98. captureSessionID:(NSString *)captureSessionID
  99. completionHandler:(sc_managed_capturer_start_recording_completion_handler_t)completionHandler
  100. context:(NSString *)context
  101. {
  102. [self _handleBaseStateBehavior:@"startRecordingWithResource" context:context];
  103. }
  104. - (void)stopRecordingWithResource:(SCCaptureResource *)resource context:(NSString *)context
  105. {
  106. [self _handleBaseStateBehavior:@"stopRecordingWithResource" context:context];
  107. }
  108. - (void)cancelRecordingWithResource:(SCCaptureResource *)resource context:(NSString *)context
  109. {
  110. [self _handleBaseStateBehavior:@"cancelRecordingWithResource" context:context];
  111. }
  112. - (void)captureStillImageWithResource:(SCCaptureResource *)resource
  113. aspectRatio:(CGFloat)aspectRatio
  114. captureSessionID:(NSString *)captureSessionID
  115. completionHandler:(sc_managed_capturer_capture_still_image_completion_handler_t)completionHandler
  116. context:(NSString *)context
  117. {
  118. [self _handleBaseStateBehavior:@"captureStillImageWithResource" context:context];
  119. }
  120. - (void)startScanWithScanConfiguration:(SCScanConfiguration *)configuration
  121. resource:(SCCaptureResource *)resource
  122. context:(NSString *)context
  123. {
  124. [self _handleBaseStateBehavior:@"startScanWithScanConfiguration" context:context];
  125. }
  126. - (void)stopScanWithCompletionHandler:(dispatch_block_t)completionHandler
  127. resource:(SCCaptureResource *)resource
  128. context:(NSString *)context
  129. {
  130. // Temporary solution until IDT-12520 is resolved.
  131. [SCCaptureWorker stopScanWithCompletionHandler:completionHandler resource:resource];
  132. //[self _handleBaseStateBehavior:@"stopScanWithCompletionHandler"];
  133. }
  134. - (void)_handleBaseStateBehavior:(NSString *)illegalAPIName context:(NSString *)context
  135. {
  136. [_bookKeeper state:[self stateId]
  137. illegalAPIcalled:illegalAPIName
  138. callStack:[NSThread callStackSymbols]
  139. context:context];
  140. if (SCIsDebugBuild()) {
  141. SCAssertFail(@"illegal API invoked on capture state machine");
  142. }
  143. }
  144. - (SCCaptureStateMachineBookKeeper *)bookKeeper
  145. {
  146. return _bookKeeper;
  147. }
  148. @end