UnityReplayKit_Scripting.mm 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //==============================================================================
  2. //
  3. // ReplayKit Unity Interface
  4. #import "UnityReplayKit.h"
  5. extern "C"
  6. {
  7. #if UNITY_REPLAY_KIT_AVAILABLE
  8. int UnityReplayKitAPIAvailable()
  9. {
  10. return [UnityReplayKit sharedInstance].apiAvailable ? 1 : 0;
  11. }
  12. int UnityReplayKitRecordingAvailable()
  13. {
  14. return [UnityReplayKit sharedInstance].recordingPreviewAvailable ? 1 : 0;
  15. }
  16. int UnityReplayKitIsCameraEnabled()
  17. {
  18. return [UnityReplayKit sharedInstance].cameraEnabled != NO ? 1 : 0;
  19. }
  20. int UnityReplayKitSetCameraEnabled(bool yes)
  21. {
  22. BOOL value = yes ? YES : NO;
  23. [UnityReplayKit sharedInstance].cameraEnabled = value;
  24. return [UnityReplayKit sharedInstance].cameraEnabled == value;
  25. }
  26. int UnityReplayKitIsMicrophoneEnabled()
  27. {
  28. return [UnityReplayKit sharedInstance].microphoneEnabled != NO ? 1 : 0;
  29. }
  30. int UnityReplayKitSetMicrophoneEnabled(bool yes)
  31. {
  32. if ([UnityReplayKit sharedInstance].isRecording)
  33. {
  34. printf_console("It is not possible to change microphoneEnabled during recording.\n");
  35. return 0;
  36. }
  37. BOOL value = yes ? YES : NO;
  38. [UnityReplayKit sharedInstance].microphoneEnabled = value;
  39. return [UnityReplayKit sharedInstance].microphoneEnabled == value;
  40. }
  41. const char* UnityReplayKitLastError()
  42. {
  43. NSString* err = [UnityReplayKit sharedInstance].lastError;
  44. if (err == nil)
  45. {
  46. return NULL;
  47. }
  48. const char* error = [err cStringUsingEncoding: NSUTF8StringEncoding];
  49. if (error != NULL)
  50. {
  51. error = strdup(error);
  52. }
  53. return error;
  54. }
  55. int UnityReplayKitStartRecording()
  56. {
  57. return [[UnityReplayKit sharedInstance] startRecording] ? 1 : 0;
  58. }
  59. int UnityReplayKitIsRecording()
  60. {
  61. return [UnityReplayKit sharedInstance].isRecording ? 1 : 0;
  62. }
  63. int UnityReplayKitShowCameraPreviewAt(float x, float y, float width, float height)
  64. {
  65. float q = 1.0f / UnityScreenScaleFactor([UIScreen mainScreen]);
  66. float h = [[UIScreen mainScreen] bounds].size.height;
  67. return [[UnityReplayKit sharedInstance] showCameraPreviewAt: CGPointMake(x * q, h - y * q) width: width height: height] ? 1 : 0;
  68. }
  69. void UnityReplayKitHideCameraPreview()
  70. {
  71. [[UnityReplayKit sharedInstance] hideCameraPreview];
  72. }
  73. int UnityReplayKitStopRecording()
  74. {
  75. #if !PLATFORM_TVOS
  76. UnityReplayKitHideCameraPreview();
  77. UnityReplayKitSetCameraEnabled(false);
  78. #endif
  79. return [[UnityReplayKit sharedInstance] stopRecording] ? 1 : 0;
  80. }
  81. int UnityReplayKitDiscard()
  82. {
  83. return [[UnityReplayKit sharedInstance] discardPreview] ? 1 : 0;
  84. }
  85. int UnityReplayKitPreview()
  86. {
  87. return [[UnityReplayKit sharedInstance] showPreview] ? 1 : 0;
  88. }
  89. int UnityReplayKitBroadcastingAPIAvailable()
  90. {
  91. return [[UnityReplayKit sharedInstance] broadcastingApiAvailable] ? 1 : 0;
  92. }
  93. void UnityReplayKitStartBroadcasting(void* callback)
  94. {
  95. [[UnityReplayKit sharedInstance] startBroadcastingWithCallback: callback];
  96. }
  97. void UnityReplayKitStopBroadcasting()
  98. {
  99. #if !PLATFORM_TVOS
  100. UnityReplayKitHideCameraPreview();
  101. #endif
  102. [[UnityReplayKit sharedInstance] stopBroadcasting];
  103. }
  104. void UnityReplayKitPauseBroadcasting()
  105. {
  106. [[UnityReplayKit sharedInstance] pauseBroadcasting];
  107. }
  108. void UnityReplayKitResumeBroadcasting()
  109. {
  110. [[UnityReplayKit sharedInstance] resumeBroadcasting];
  111. }
  112. int UnityReplayKitIsBroadcasting()
  113. {
  114. return [[UnityReplayKit sharedInstance] isBroadcasting] ? 1 : 0;
  115. }
  116. int UnityReplayKitIsBroadcastingPaused()
  117. {
  118. return [[UnityReplayKit sharedInstance] isBroadcastingPaused] ? 1 : 0;
  119. }
  120. int UnityReplayKitIsPreviewControllerActive()
  121. {
  122. return [[UnityReplayKit sharedInstance] isPreviewControllerActive] ? 1 : 0;
  123. }
  124. const char* UnityReplayKitGetBroadcastURL()
  125. {
  126. NSURL *url = [[UnityReplayKit sharedInstance] broadcastURL];
  127. if (url != nil)
  128. {
  129. return [[url absoluteString] UTF8String];
  130. }
  131. return nullptr;
  132. }
  133. void UnityReplayKitCreateOverlayWindow()
  134. {
  135. [[UnityReplayKit sharedInstance] createOverlayWindow];
  136. }
  137. extern "C" float UnityScreenScaleFactor(UIScreen* screen);
  138. #else
  139. // Impl when ReplayKit is not available.
  140. int UnityReplayKitAPIAvailable() { return 0; }
  141. int UnityReplayKitRecordingAvailable() { return 0; }
  142. const char* UnityReplayKitLastError() { return NULL; }
  143. int UnityReplayKitStartRecording(int enableMicrophone, int enableCamera) { return 0; }
  144. int UnityReplayKitIsRecording() { return 0; }
  145. int UnityReplayKitStopRecording() { return 0; }
  146. int UnityReplayKitDiscard() { return 0; }
  147. int UnityReplayKitPreview() { return 0; }
  148. int UnityReplayKitIsCameraEnabled() { return 0; }
  149. int UnityReplayKitSetCameraEnabled(bool) { return 0; }
  150. int UnityReplayKitIsMicrophoneEnabled() { return 0; }
  151. int UnityReplayKitSetMicrophoneEnabled(bool) { return 0; }
  152. int UnityReplayKitShowCameraPreviewAt(float x, float y, float width, float height) { return 0; }
  153. void UnityReplayKitHideCameraPreview() {}
  154. void UnityReplayKitCreateOverlayWindow() {}
  155. void UnityReplayKitTriggerBroadcastStatusCallback(void*, bool, const char*);
  156. int UnityReplayKitBroadcastingAPIAvailable() { return 0; }
  157. void UnityReplayKitStartBroadcasting(void* callback) { UnityReplayKitTriggerBroadcastStatusCallback(callback, false, "ReplayKit not implemented."); }
  158. void UnityReplayKitStopBroadcasting() {}
  159. void UnityReplayKitPauseBroadcasting() {}
  160. void UnityReplayKitResumeBroadcasting() {}
  161. int UnityReplayKitIsBroadcasting() { return 0; }
  162. int UnityReplayKitIsBroadcastingPaused() { return 0; }
  163. int UnityReplayKitIsPreviewControllerActive() { return 0; }
  164. const char* UnityReplayKitGetBroadcastURL() { return nullptr; }
  165. #endif // UNITY_REPLAY_KIT_AVAILABLE
  166. } // extern "C"