UnityReplayKit.mm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. #if UNITY_REPLAY_KIT_AVAILABLE
  2. #import "UnityReplayKit.h"
  3. #import "UnityAppController.h"
  4. #import "UI/UnityViewControllerBase.h"
  5. #import "UnityInterface.h"
  6. #import <UIKit/UIKit.h>
  7. extern "C" void UnityReplayKitTriggerBroadcastStatusCallback(void* callback, bool hasSucceeded, const char* errorMessage);
  8. static UnityReplayKit* _replayKit = nil;
  9. @protocol UnityReplayKit_RPScreenRecorder<NSObject>
  10. - (void)setMicrophoneEnabled:(BOOL)value;
  11. - (BOOL)isMicrophoneEnabled;
  12. - (void)setCameraEnabled:(BOOL)value;
  13. - (BOOL)isCameraEnabled;
  14. - (BOOL)isPreviewControllerActive;
  15. @property (nonatomic, setter = setMicrophoneEnabled:, getter = isMicrophoneEnabled) BOOL microphoneEnabled;
  16. @property (nonatomic, setter = setCameraEnabled:, getter = isCameraEnabled) BOOL cameraEnabled;
  17. @property (nonatomic, readonly) UIView* cameraPreviewView;
  18. @property (nonatomic, getter = isPreviewControllerActive) BOOL previewControllerActive;
  19. @end
  20. @protocol UnityReplayKit_RPBroadcastController<NSObject>
  21. @property(nonatomic, readonly) NSURL *broadcastURL;
  22. @property(nonatomic, readonly, getter = isBroadcasting) BOOL broadcasting;
  23. @property(nonatomic, readonly) NSString *broadcastExtensionBundleID;
  24. //@property(nonatomic, weak) id<RPBroadcastControllerDelegate> delegate;
  25. @property(nonatomic, readonly, getter = isBroadcastingPaused) BOOL paused;
  26. @property(nonatomic, readonly) NSDictionary<NSString *, NSObject<NSCoding> *> *serviceInfo;
  27. - (BOOL)isBroadcasting;
  28. - (BOOL)isBroadcastingPaused;
  29. - (void)finishBroadcastWithHandler:(void (^)(NSError *error))handler;
  30. - (void)startBroadcastWithHandler:(void (^)(NSError *error))handler;
  31. - (void)pauseBroadcast;
  32. - (void)resumeBroadcast;
  33. @end
  34. @interface UnityReplayKit_RPBroadcastActivityViewController : UIViewController<NSObject>
  35. @property (nonatomic, weak) id delegate;
  36. @end
  37. // why do we care about orientation handling:
  38. // ReplayKit will disable top-window autorotation
  39. // as users keep asking to do autorotation during broadcast/record we create fake empty window with fake view controller
  40. // this window will have autorotation disabled instead of unity one
  41. // but this is not the end of the story: what fake view controller does is also important
  42. // now it is hard to speculate what *actually* happens but with setup like fake view controller takes over control over "supported orientations"
  43. // meaning that if we dont do anything suddenly all orientations become enabled.
  44. // to avoid that we create this monstrosity that pokes unity for orientation.
  45. #if PLATFORM_IOS
  46. @interface UnityReplayKitViewController : UnityViewControllerBase
  47. {
  48. }
  49. - (NSUInteger)supportedInterfaceOrientations;
  50. @end
  51. @implementation UnityReplayKitViewController
  52. - (NSUInteger)supportedInterfaceOrientations
  53. {
  54. NSUInteger ret = 0;
  55. if (UnityShouldAutorotate())
  56. {
  57. if (UnityIsOrientationEnabled(portrait))
  58. ret |= (1 << UIInterfaceOrientationPortrait);
  59. if (UnityIsOrientationEnabled(portraitUpsideDown))
  60. ret |= (1 << UIInterfaceOrientationPortraitUpsideDown);
  61. if (UnityIsOrientationEnabled(landscapeLeft))
  62. ret |= (1 << UIInterfaceOrientationLandscapeRight);
  63. if (UnityIsOrientationEnabled(landscapeRight))
  64. ret |= (1 << UIInterfaceOrientationLandscapeLeft);
  65. }
  66. else
  67. {
  68. switch (UnityRequestedScreenOrientation())
  69. {
  70. case portrait: ret = (1 << UIInterfaceOrientationPortrait); break;
  71. case portraitUpsideDown: ret = (1 << UIInterfaceOrientationPortraitUpsideDown); break;
  72. case landscapeLeft: ret = (1 << UIInterfaceOrientationLandscapeRight); break;
  73. case landscapeRight: ret = (1 << UIInterfaceOrientationLandscapeLeft); break;
  74. }
  75. }
  76. return ret;
  77. }
  78. @end
  79. #else
  80. #define UnityReplayKitViewController UnityViewControllerBase
  81. #endif
  82. @implementation UnityReplayKit
  83. {
  84. id<UnityReplayKit_RPBroadcastController> broadcastController;
  85. void* broadcastStartStatusCallback;
  86. UIView* currentCameraPreviewView;
  87. bool currentPreviewControllerActive;
  88. UIWindow* overlayWindow;
  89. }
  90. - (void)shouldCreateOverlayWindow
  91. {
  92. UnityShouldCreateReplayKitOverlay();
  93. }
  94. - (void)createOverlayWindow
  95. {
  96. if (self->overlayWindow == nil)
  97. {
  98. UIWindow* wnd = self->overlayWindow = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];
  99. wnd.hidden = wnd.userInteractionEnabled = NO;
  100. wnd.backgroundColor = nil;
  101. wnd.rootViewController = [[UnityReplayKitViewController alloc] init];
  102. }
  103. }
  104. + (UnityReplayKit*)sharedInstance
  105. {
  106. static dispatch_once_t onceToken;
  107. dispatch_once(&onceToken, ^{
  108. _replayKit = [[UnityReplayKit alloc] init];
  109. });
  110. return _replayKit;
  111. }
  112. - (BOOL)apiAvailable
  113. {
  114. return ([RPScreenRecorder class] != nil) && [RPScreenRecorder sharedRecorder].isAvailable;
  115. }
  116. - (BOOL)recordingPreviewAvailable
  117. {
  118. return _previewController != nil;
  119. }
  120. - (BOOL)startRecording
  121. {
  122. RPScreenRecorder* recorder = [RPScreenRecorder sharedRecorder];
  123. if (recorder == nil)
  124. {
  125. _lastError = [NSString stringWithUTF8String: "Failed to get Screen Recorder"];
  126. return NO;
  127. }
  128. recorder.delegate = self;
  129. __block BOOL success = YES;
  130. [recorder startRecordingWithHandler:^(NSError* error) {
  131. if (error != nil)
  132. {
  133. _lastError = [error description];
  134. success = NO;
  135. }
  136. else
  137. {
  138. [self shouldCreateOverlayWindow];
  139. }
  140. }];
  141. return success;
  142. }
  143. - (BOOL)isRecording
  144. {
  145. RPScreenRecorder* recorder = [RPScreenRecorder sharedRecorder];
  146. if (recorder == nil)
  147. {
  148. _lastError = [NSString stringWithUTF8String: "Failed to get Screen Recorder"];
  149. return NO;
  150. }
  151. return recorder.isRecording;
  152. }
  153. - (BOOL)stopRecording
  154. {
  155. RPScreenRecorder* recorder = [RPScreenRecorder sharedRecorder];
  156. if (recorder == nil)
  157. {
  158. _lastError = [NSString stringWithUTF8String: "Failed to get Screen Recorder"];
  159. return NO;
  160. }
  161. __block BOOL success = YES;
  162. [recorder stopRecordingWithHandler:^(RPPreviewViewController* previewViewController, NSError* error) {
  163. self->overlayWindow = nil;
  164. if (error != nil)
  165. {
  166. _lastError = [error description];
  167. success = NO;
  168. return;
  169. }
  170. if (previewViewController != nil)
  171. {
  172. [previewViewController setPreviewControllerDelegate: self];
  173. _previewController = previewViewController;
  174. }
  175. }];
  176. return success;
  177. }
  178. - (void)screenRecorder:(RPScreenRecorder*)screenRecorder didStopRecordingWithError:(NSError*)error previewViewController:(RPPreviewViewController*)previewViewController
  179. {
  180. if (error != nil)
  181. {
  182. _lastError = [error description];
  183. }
  184. self->overlayWindow = nil;
  185. _previewController = previewViewController;
  186. }
  187. - (BOOL)showPreview
  188. {
  189. if (_previewController == nil)
  190. {
  191. _lastError = [NSString stringWithUTF8String: "No recording available"];
  192. return NO;
  193. }
  194. [_previewController setModalPresentationStyle: UIModalPresentationFullScreen];
  195. [GetAppController().rootViewController presentViewController: _previewController animated: YES completion:^()
  196. {
  197. _previewController = nil;
  198. }];
  199. currentPreviewControllerActive = YES;
  200. return YES;
  201. }
  202. - (BOOL)discardPreview
  203. {
  204. if (_previewController == nil)
  205. {
  206. return YES;
  207. }
  208. RPScreenRecorder* recorder = [RPScreenRecorder sharedRecorder];
  209. if (recorder == nil)
  210. {
  211. _lastError = [NSString stringWithUTF8String: "Failed to get Screen Recorder"];
  212. return NO;
  213. }
  214. [recorder discardRecordingWithHandler:^()
  215. {
  216. _previewController = nil;
  217. }];
  218. // TODO - the above callback doesn't seem to be working at the moment.
  219. _previewController = nil;
  220. currentPreviewControllerActive = NO;
  221. return YES;
  222. }
  223. - (void)previewControllerDidFinish:(RPPreviewViewController*)previewController
  224. {
  225. if (previewController != nil)
  226. {
  227. [previewController dismissViewControllerAnimated: YES completion: nil];
  228. }
  229. currentPreviewControllerActive = NO;
  230. }
  231. - (BOOL)isPreviewControllerActive
  232. {
  233. return currentPreviewControllerActive;
  234. }
  235. /****************************************
  236. * ReplayKit Broadcasting API *
  237. ****************************************/
  238. - (BOOL)broadcastingApiAvailable
  239. {
  240. return nil != NSClassFromString(@"RPBroadcastController")
  241. && nil != NSClassFromString(@"RPBroadcastActivityViewController");
  242. }
  243. - (NSURL*)broadcastURL
  244. {
  245. if (broadcastController == nil)
  246. {
  247. return nil;
  248. }
  249. return [broadcastController broadcastURL];
  250. }
  251. - (BOOL)isBroadcasting
  252. {
  253. if (broadcastController == nil)
  254. {
  255. return NO;
  256. }
  257. return [broadcastController isBroadcasting];
  258. }
  259. - (BOOL)isBroadcastingPaused
  260. {
  261. if (broadcastController == nil)
  262. {
  263. return NO;
  264. }
  265. return [broadcastController isBroadcastingPaused];
  266. }
  267. - (void)broadcastActivityViewController:(UnityReplayKit_RPBroadcastActivityViewController *)sBroadcastActivityViewController
  268. didFinishWithBroadcastController:(id<UnityReplayKit_RPBroadcastController>)sBroadcastController
  269. error:(NSError *)error
  270. {
  271. dispatch_sync(dispatch_get_main_queue(), ^{
  272. UnityPause(0);
  273. });
  274. if (sBroadcastController == nil)
  275. {
  276. _lastError = [error description];
  277. UnityReplayKitTriggerBroadcastStatusCallback(broadcastStartStatusCallback, false, [_lastError UTF8String]);
  278. broadcastStartStatusCallback = nullptr;
  279. [UnityGetGLViewController() dismissViewControllerAnimated: YES completion: nil];
  280. return;
  281. }
  282. broadcastController = sBroadcastController;
  283. [UnityGetGLViewController() dismissViewControllerAnimated: YES completion:^
  284. {
  285. [broadcastController startBroadcastWithHandler:^(NSError* error)
  286. {
  287. if (error != nil)
  288. {
  289. _lastError = [error description];
  290. UnityReplayKitTriggerBroadcastStatusCallback(broadcastStartStatusCallback, false, [_lastError UTF8String]);
  291. broadcastStartStatusCallback = nullptr;
  292. broadcastController = nil;
  293. return;
  294. }
  295. UnityReplayKitTriggerBroadcastStatusCallback(broadcastStartStatusCallback, true, "");
  296. broadcastStartStatusCallback = nullptr;
  297. _lastError = nil;
  298. }];
  299. }];
  300. }
  301. - (void)startBroadcastingWithCallback:(void *)callback
  302. {
  303. Class class_BroadcastActivityViewController = NSClassFromString(@"RPBroadcastActivityViewController");
  304. if (class_BroadcastActivityViewController == nil)
  305. {
  306. return;
  307. }
  308. if (broadcastController != nil && broadcastController.broadcasting)
  309. {
  310. _lastError = @"Broadcast already in progress";
  311. UnityReplayKitTriggerBroadcastStatusCallback(callback, false, [_lastError UTF8String]);
  312. return;
  313. }
  314. if (broadcastStartStatusCallback != nullptr)
  315. {
  316. _lastError = @"The last attempt to start a broadcast didn\'t finish yet.";
  317. UnityReplayKitTriggerBroadcastStatusCallback(callback, false, [_lastError UTF8String]);
  318. return;
  319. }
  320. [class_BroadcastActivityViewController performSelector: @selector(loadBroadcastActivityViewControllerWithHandler:) withObject:^(UnityReplayKit_RPBroadcastActivityViewController* vc, NSError* error)
  321. {
  322. if (vc == nil || error != nil)
  323. {
  324. _lastError = [error description];
  325. UnityReplayKitTriggerBroadcastStatusCallback(callback, false, [_lastError UTF8String]);
  326. return;
  327. }
  328. [self shouldCreateOverlayWindow];
  329. UnityPause(1);
  330. vc.delegate = self;
  331. broadcastStartStatusCallback = callback;
  332. #if PLATFORM_TVOS
  333. vc.modalPresentationStyle = UIModalPresentationFullScreen;
  334. #else
  335. vc.modalPresentationStyle = UIModalPresentationPopover;
  336. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  337. {
  338. vc.popoverPresentationController.sourceRect = CGRectMake(GetAppController().rootView.bounds.size.width / 2, 0, 0, 0);
  339. vc.popoverPresentationController.sourceView = GetAppController().rootView;
  340. }
  341. #endif
  342. [UnityGetGLViewController() presentViewController: vc animated: YES completion: nil];
  343. }];
  344. return;
  345. }
  346. - (void)stopBroadcasting
  347. {
  348. self->overlayWindow = nil;
  349. if (broadcastController == nil || !broadcastController.broadcasting)
  350. {
  351. broadcastController = nil;
  352. return;
  353. }
  354. [broadcastController finishBroadcastWithHandler:^(NSError* error)
  355. {
  356. broadcastController = nil;
  357. if (error == nil)
  358. return;
  359. _lastError = [error description];
  360. }];
  361. }
  362. - (void)pauseBroadcasting
  363. {
  364. if (broadcastController == nil || !broadcastController.broadcasting)
  365. {
  366. return;
  367. }
  368. [broadcastController pauseBroadcast];
  369. }
  370. - (void)resumeBroadcasting
  371. {
  372. if (broadcastController == nil || !broadcastController.broadcasting)
  373. {
  374. return;
  375. }
  376. [broadcastController resumeBroadcast];
  377. }
  378. - (BOOL)isCameraEnabled
  379. {
  380. if (![self apiAvailable])
  381. {
  382. return NO;
  383. }
  384. id<UnityReplayKit_RPScreenRecorder> screenRecorder = (id)[RPScreenRecorder sharedRecorder];
  385. if (![screenRecorder respondsToSelector: @selector(isCameraEnabled)])
  386. {
  387. return NO;
  388. }
  389. return screenRecorder.cameraEnabled;
  390. }
  391. - (void)setCameraEnabled:(BOOL)cameraEnabled
  392. {
  393. if (![self apiAvailable])
  394. {
  395. return;
  396. }
  397. id<UnityReplayKit_RPScreenRecorder> screenRecorder = (id)[RPScreenRecorder sharedRecorder];
  398. if (![screenRecorder respondsToSelector: @selector(setCameraEnabled:)])
  399. {
  400. return;
  401. }
  402. screenRecorder.cameraEnabled = cameraEnabled;
  403. }
  404. - (BOOL)isMicrophoneEnabled
  405. {
  406. if (![self apiAvailable])
  407. {
  408. return NO;
  409. }
  410. id<UnityReplayKit_RPScreenRecorder> screenRecorder = (id)[RPScreenRecorder sharedRecorder];
  411. if (![screenRecorder respondsToSelector: @selector(isMicrophoneEnabled)])
  412. {
  413. return NO;
  414. }
  415. return screenRecorder.microphoneEnabled;
  416. }
  417. - (void)setMicrophoneEnabled:(BOOL)microphoneEnabled
  418. {
  419. if (![self apiAvailable])
  420. {
  421. return;
  422. }
  423. id<UnityReplayKit_RPScreenRecorder> screenRecorder = (id)[RPScreenRecorder sharedRecorder];
  424. if (![screenRecorder respondsToSelector: @selector(setMicrophoneEnabled:)])
  425. {
  426. return;
  427. }
  428. screenRecorder.microphoneEnabled = microphoneEnabled;
  429. }
  430. - (BOOL)showCameraPreviewAt:(CGPoint)position width:(float)width height:(float)height
  431. {
  432. if (currentCameraPreviewView == nil)
  433. {
  434. if (![self apiAvailable])
  435. {
  436. return NO;
  437. }
  438. id<UnityReplayKit_RPScreenRecorder> screenRecorder = (id)[RPScreenRecorder sharedRecorder];
  439. UIView* cameraPreviewView = screenRecorder.cameraPreviewView;
  440. if (cameraPreviewView == nil)
  441. {
  442. return NO;
  443. }
  444. [[UnityGetGLViewController() view] addSubview: cameraPreviewView];
  445. currentCameraPreviewView = cameraPreviewView;
  446. [cameraPreviewView setUserInteractionEnabled: NO];
  447. }
  448. if (width < 0.0f)
  449. width = currentCameraPreviewView.frame.size.width;
  450. if (height < 0.0f)
  451. height = currentCameraPreviewView.frame.size.height;
  452. [currentCameraPreviewView setFrame: CGRectMake(position.x, position.y, width, height)];
  453. return YES;
  454. }
  455. - (void)hideCameraPreview
  456. {
  457. if (currentCameraPreviewView != nil)
  458. {
  459. [currentCameraPreviewView removeFromSuperview];
  460. currentCameraPreviewView = nil;
  461. }
  462. }
  463. @end
  464. #endif // UNITY_REPLAY_KIT_AVAILABLE