暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

UnityReplayKit.mm 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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 || PLATFORM_VISIONOS
  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. NSAssert(_replayKit != nil, @"InitUnityReplayKit should be called before using ReplayKit api.");
  107. return _replayKit;
  108. }
  109. - (BOOL)apiAvailable
  110. {
  111. return ([RPScreenRecorder class] != nil) && [RPScreenRecorder sharedRecorder].isAvailable;
  112. }
  113. - (BOOL)recordingPreviewAvailable
  114. {
  115. return _previewController != nil;
  116. }
  117. - (BOOL)startRecording
  118. {
  119. RPScreenRecorder* recorder = [RPScreenRecorder sharedRecorder];
  120. if (recorder == nil)
  121. {
  122. _lastError = [NSString stringWithUTF8String: "Failed to get Screen Recorder"];
  123. return NO;
  124. }
  125. recorder.delegate = self;
  126. __block BOOL success = YES;
  127. [recorder startRecordingWithHandler:^(NSError* error) {
  128. if (error != nil)
  129. {
  130. _lastError = [error description];
  131. success = NO;
  132. }
  133. else
  134. {
  135. [self shouldCreateOverlayWindow];
  136. }
  137. }];
  138. return success;
  139. }
  140. - (BOOL)isRecording
  141. {
  142. RPScreenRecorder* recorder = [RPScreenRecorder sharedRecorder];
  143. if (recorder == nil)
  144. {
  145. _lastError = [NSString stringWithUTF8String: "Failed to get Screen Recorder"];
  146. return NO;
  147. }
  148. return recorder.isRecording;
  149. }
  150. - (BOOL)stopRecording
  151. {
  152. RPScreenRecorder* recorder = [RPScreenRecorder sharedRecorder];
  153. if (recorder == nil)
  154. {
  155. _lastError = [NSString stringWithUTF8String: "Failed to get Screen Recorder"];
  156. return NO;
  157. }
  158. __block BOOL success = YES;
  159. [recorder stopRecordingWithHandler:^(RPPreviewViewController* previewViewController, NSError* error) {
  160. self->overlayWindow = nil;
  161. if (error != nil)
  162. {
  163. _lastError = [error description];
  164. success = NO;
  165. return;
  166. }
  167. if (previewViewController != nil)
  168. {
  169. [previewViewController setPreviewControllerDelegate: self];
  170. _previewController = previewViewController;
  171. }
  172. }];
  173. return success;
  174. }
  175. - (void)screenRecorder:(RPScreenRecorder*)screenRecorder didStopRecordingWithError:(NSError*)error previewViewController:(RPPreviewViewController*)previewViewController
  176. {
  177. if (error != nil)
  178. {
  179. _lastError = [error description];
  180. }
  181. self->overlayWindow = nil;
  182. _previewController = previewViewController;
  183. }
  184. - (BOOL)showPreview
  185. {
  186. if (_previewController == nil)
  187. {
  188. _lastError = [NSString stringWithUTF8String: "No recording available"];
  189. return NO;
  190. }
  191. [_previewController setModalPresentationStyle: UIModalPresentationFullScreen];
  192. [GetAppController().rootViewController presentViewController: _previewController animated: YES completion:^()
  193. {
  194. _previewController = nil;
  195. }];
  196. currentPreviewControllerActive = YES;
  197. return YES;
  198. }
  199. - (BOOL)discardPreview
  200. {
  201. if (_previewController == nil)
  202. {
  203. return YES;
  204. }
  205. RPScreenRecorder* recorder = [RPScreenRecorder sharedRecorder];
  206. if (recorder == nil)
  207. {
  208. _lastError = [NSString stringWithUTF8String: "Failed to get Screen Recorder"];
  209. return NO;
  210. }
  211. [recorder discardRecordingWithHandler:^()
  212. {
  213. _previewController = nil;
  214. }];
  215. // TODO - the above callback doesn't seem to be working at the moment.
  216. _previewController = nil;
  217. currentPreviewControllerActive = NO;
  218. return YES;
  219. }
  220. - (void)previewControllerDidFinish:(RPPreviewViewController*)previewController
  221. {
  222. if (previewController != nil)
  223. {
  224. [previewController dismissViewControllerAnimated: YES completion: nil];
  225. }
  226. currentPreviewControllerActive = NO;
  227. }
  228. - (BOOL)isPreviewControllerActive
  229. {
  230. return currentPreviewControllerActive;
  231. }
  232. /****************************************
  233. * ReplayKit Broadcasting API *
  234. ****************************************/
  235. - (BOOL)broadcastingApiAvailable
  236. {
  237. return nil != NSClassFromString(@"RPBroadcastController")
  238. && nil != NSClassFromString(@"RPBroadcastActivityViewController");
  239. }
  240. - (NSURL*)broadcastURL
  241. {
  242. if (broadcastController == nil)
  243. {
  244. return nil;
  245. }
  246. return [broadcastController broadcastURL];
  247. }
  248. - (BOOL)isBroadcasting
  249. {
  250. if (broadcastController == nil)
  251. {
  252. return NO;
  253. }
  254. return [broadcastController isBroadcasting];
  255. }
  256. - (BOOL)isBroadcastingPaused
  257. {
  258. if (broadcastController == nil)
  259. {
  260. return NO;
  261. }
  262. return [broadcastController isBroadcastingPaused];
  263. }
  264. - (void)broadcastActivityViewController:(UnityReplayKit_RPBroadcastActivityViewController *)sBroadcastActivityViewController
  265. didFinishWithBroadcastController:(id<UnityReplayKit_RPBroadcastController>)inRPBroadcastController
  266. error:(NSError *)error
  267. {
  268. dispatch_sync(dispatch_get_main_queue(), ^{
  269. UnityPause(0);
  270. broadcastController = inRPBroadcastController;
  271. if (broadcastController == nil) // broadcast was canceled
  272. {
  273. _lastError = [error description];
  274. UnityReplayKitTriggerBroadcastStatusCallback(broadcastStartStatusCallback, false, [_lastError UTF8String]);
  275. broadcastStartStatusCallback = nil;
  276. [UnityGetGLViewController() dismissViewControllerAnimated: YES completion: nil];
  277. }
  278. else // start broadcast
  279. {
  280. [UnityGetGLViewController() dismissViewControllerAnimated: YES completion:^
  281. {
  282. [broadcastController startBroadcastWithHandler:^(NSError* error)
  283. {
  284. if (error != nil)
  285. {
  286. _lastError = [error description];
  287. UnityReplayKitTriggerBroadcastStatusCallback(broadcastStartStatusCallback, false, [_lastError UTF8String]);
  288. broadcastStartStatusCallback = nil; broadcastController = nil;
  289. }
  290. else
  291. {
  292. UnityReplayKitTriggerBroadcastStatusCallback(broadcastStartStatusCallback, true, "");
  293. broadcastStartStatusCallback = nil; _lastError = nil;
  294. }
  295. }];
  296. }];
  297. }
  298. });
  299. }
  300. - (void)startBroadcastingWithCallback:(void *)callback
  301. {
  302. Class class_BroadcastActivityViewController = NSClassFromString(@"RPBroadcastActivityViewController");
  303. if (class_BroadcastActivityViewController == nil)
  304. {
  305. return;
  306. }
  307. if (broadcastController != nil && broadcastController.broadcasting)
  308. {
  309. _lastError = @"Broadcast already in progress";
  310. UnityReplayKitTriggerBroadcastStatusCallback(callback, false, [_lastError UTF8String]);
  311. return;
  312. }
  313. if (broadcastStartStatusCallback != nullptr)
  314. {
  315. _lastError = @"The last attempt to start a broadcast didn\'t finish yet.";
  316. UnityReplayKitTriggerBroadcastStatusCallback(callback, false, [_lastError UTF8String]);
  317. return;
  318. }
  319. [class_BroadcastActivityViewController performSelector: @selector(loadBroadcastActivityViewControllerWithHandler:) withObject:^(UnityReplayKit_RPBroadcastActivityViewController* vc, NSError* error)
  320. {
  321. if (vc == nil || error != nil)
  322. {
  323. _lastError = [error description];
  324. UnityReplayKitTriggerBroadcastStatusCallback(callback, false, [_lastError UTF8String]);
  325. return;
  326. }
  327. [self shouldCreateOverlayWindow];
  328. UnityPause(1);
  329. vc.delegate = self;
  330. broadcastStartStatusCallback = callback;
  331. // oh apple, how much do you like confusing docs and contradicting behaviours
  332. // on ios13 UIModalPresentationPopover meaning was changed; what's more: it is now broken if portrait is disabled
  333. // pre ios13 it was intended to be UIModalPresentationPopover for ipads, UIModalPresentationFullScreen for iphones
  334. // yet having fully blackscreen (UIModalPresentationFullScreen) was not looking good for lots of people
  335. // so we use popover for both ipad/iphone but ONLY before ios13
  336. // note that tvos is fullscreen always (this is the only option here)
  337. #if PLATFORM_TVOS
  338. vc.modalPresentationStyle = UIModalPresentationFullScreen;
  339. #else
  340. if (UnityiOS130orNewer())
  341. {
  342. vc.modalPresentationStyle = UIModalPresentationFormSheet;
  343. }
  344. else
  345. {
  346. vc.modalPresentationStyle = UIModalPresentationPopover;
  347. vc.popoverPresentationController.sourceRect = CGRectMake(GetAppController().rootView.bounds.size.width / 2, 0, 0, 0);
  348. vc.popoverPresentationController.sourceView = GetAppController().rootView;
  349. }
  350. #endif
  351. [UnityGetGLViewController() presentViewController: vc animated: YES completion: nil];
  352. }];
  353. return;
  354. }
  355. - (void)stopBroadcasting
  356. {
  357. self->overlayWindow = nil;
  358. if (broadcastController == nil || !broadcastController.broadcasting)
  359. {
  360. broadcastController = nil;
  361. return;
  362. }
  363. [broadcastController finishBroadcastWithHandler:^(NSError* error)
  364. {
  365. broadcastController = nil;
  366. if (error == nil)
  367. return;
  368. _lastError = [error description];
  369. }];
  370. }
  371. - (void)pauseBroadcasting
  372. {
  373. if (broadcastController == nil || !broadcastController.broadcasting)
  374. {
  375. return;
  376. }
  377. [broadcastController pauseBroadcast];
  378. }
  379. - (void)resumeBroadcasting
  380. {
  381. if (broadcastController == nil || !broadcastController.broadcasting)
  382. {
  383. return;
  384. }
  385. [broadcastController resumeBroadcast];
  386. }
  387. - (BOOL)isCameraEnabled
  388. {
  389. if (![self apiAvailable])
  390. {
  391. return NO;
  392. }
  393. id<UnityReplayKit_RPScreenRecorder> screenRecorder = (id)[RPScreenRecorder sharedRecorder];
  394. if (![screenRecorder respondsToSelector: @selector(isCameraEnabled)])
  395. {
  396. return NO;
  397. }
  398. return screenRecorder.cameraEnabled;
  399. }
  400. - (void)setCameraEnabled:(BOOL)cameraEnabled
  401. {
  402. if (![self apiAvailable])
  403. {
  404. return;
  405. }
  406. id<UnityReplayKit_RPScreenRecorder> screenRecorder = (id)[RPScreenRecorder sharedRecorder];
  407. if (![screenRecorder respondsToSelector: @selector(setCameraEnabled:)])
  408. {
  409. return;
  410. }
  411. screenRecorder.cameraEnabled = cameraEnabled;
  412. }
  413. - (BOOL)isMicrophoneEnabled
  414. {
  415. if (![self apiAvailable])
  416. {
  417. return NO;
  418. }
  419. id<UnityReplayKit_RPScreenRecorder> screenRecorder = (id)[RPScreenRecorder sharedRecorder];
  420. if (![screenRecorder respondsToSelector: @selector(isMicrophoneEnabled)])
  421. {
  422. return NO;
  423. }
  424. return screenRecorder.microphoneEnabled;
  425. }
  426. - (void)setMicrophoneEnabled:(BOOL)microphoneEnabled
  427. {
  428. if (![self apiAvailable])
  429. {
  430. return;
  431. }
  432. id<UnityReplayKit_RPScreenRecorder> screenRecorder = (id)[RPScreenRecorder sharedRecorder];
  433. if (![screenRecorder respondsToSelector: @selector(setMicrophoneEnabled:)])
  434. {
  435. return;
  436. }
  437. screenRecorder.microphoneEnabled = microphoneEnabled;
  438. }
  439. - (BOOL)showCameraPreviewAt:(CGPoint)position width:(float)width height:(float)height
  440. {
  441. if (currentCameraPreviewView == nil)
  442. {
  443. if (![self apiAvailable])
  444. {
  445. return NO;
  446. }
  447. id<UnityReplayKit_RPScreenRecorder> screenRecorder = (id)[RPScreenRecorder sharedRecorder];
  448. UIView* cameraPreviewView = screenRecorder.cameraPreviewView;
  449. if (cameraPreviewView == nil)
  450. {
  451. return NO;
  452. }
  453. [[UnityGetGLViewController() view] addSubview: cameraPreviewView];
  454. currentCameraPreviewView = cameraPreviewView;
  455. [cameraPreviewView setUserInteractionEnabled: NO];
  456. }
  457. if (width < 0.0f)
  458. width = currentCameraPreviewView.frame.size.width;
  459. if (height < 0.0f)
  460. height = currentCameraPreviewView.frame.size.height;
  461. [currentCameraPreviewView setFrame: CGRectMake(position.x, position.y, width, height)];
  462. return YES;
  463. }
  464. - (void)hideCameraPreview
  465. {
  466. if (currentCameraPreviewView != nil)
  467. {
  468. [currentCameraPreviewView removeFromSuperview];
  469. currentCameraPreviewView = nil;
  470. }
  471. }
  472. @end
  473. static dispatch_once_t onceToken_InitUnityReplayKit;
  474. void InitUnityReplayKit()
  475. {
  476. dispatch_once(&onceToken_InitUnityReplayKit, ^{
  477. _replayKit = [[UnityReplayKit alloc] init];
  478. // that seems to be enough to trigger RPScreenRecorder to become "ready"
  479. [RPScreenRecorder sharedRecorder].delegate = _replayKit;
  480. });
  481. }
  482. #endif // UNITY_REPLAY_KIT_AVAILABLE