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