UnityReplayKit_Scripting.mm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. BOOL value = yes ? YES : NO;
  33. [UnityReplayKit sharedInstance].microphoneEnabled = value;
  34. return [UnityReplayKit sharedInstance].microphoneEnabled == value;
  35. }
  36. const char* UnityReplayKitLastError()
  37. {
  38. NSString* err = [UnityReplayKit sharedInstance].lastError;
  39. if (err == nil)
  40. {
  41. return NULL;
  42. }
  43. const char* error = [err cStringUsingEncoding: NSUTF8StringEncoding];
  44. if (error != NULL)
  45. {
  46. error = strdup(error);
  47. }
  48. return error;
  49. }
  50. int UnityReplayKitStartRecording(int enableMicrophone)
  51. {
  52. bool enableMic = enableMicrophone != 0;
  53. return [[UnityReplayKit sharedInstance] startRecording: enableMic] ? 1 : 0;
  54. }
  55. int UnityReplayKitIsRecording()
  56. {
  57. return [UnityReplayKit sharedInstance].isRecording ? 1 : 0;
  58. }
  59. int UnityReplayKitShowCameraPreviewAt(float x, float y)
  60. {
  61. float q = 1.0f / UnityScreenScaleFactor([UIScreen mainScreen]);
  62. float h = [[UIScreen mainScreen] bounds].size.height;
  63. return [[UnityReplayKit sharedInstance] showCameraPreviewAt: CGPointMake(x * q, h - y * q)] ? 1 : 0;
  64. }
  65. void UnityReplayKitHideCameraPreview()
  66. {
  67. [[UnityReplayKit sharedInstance] hideCameraPreview];
  68. }
  69. int UnityReplayKitStopRecording()
  70. {
  71. #if !PLATFORM_TVOS
  72. UnityReplayKitHideCameraPreview();
  73. UnityReplayKitSetCameraEnabled(false);
  74. #endif
  75. return [[UnityReplayKit sharedInstance] stopRecording] ? 1 : 0;
  76. }
  77. int UnityReplayKitDiscard()
  78. {
  79. return [[UnityReplayKit sharedInstance] discardPreview] ? 1 : 0;
  80. }
  81. int UnityReplayKitPreview()
  82. {
  83. return [[UnityReplayKit sharedInstance] showPreview] ? 1 : 0;
  84. }
  85. int UnityReplayKitBroadcastingAPIAvailable()
  86. {
  87. return [[UnityReplayKit sharedInstance] broadcastingApiAvailable] ? 1 : 0;
  88. }
  89. void UnityReplayKitStartBroadcasting(void* callback)
  90. {
  91. [[UnityReplayKit sharedInstance] startBroadcastingWithCallback: callback];
  92. }
  93. void UnityReplayKitStopBroadcasting()
  94. {
  95. #if !PLATFORM_TVOS
  96. UnityReplayKitHideCameraPreview();
  97. #endif
  98. [[UnityReplayKit sharedInstance] stopBroadcasting];
  99. }
  100. int UnityReplayKitIsBroadcasting()
  101. {
  102. return [[UnityReplayKit sharedInstance] isBroadcasting] ? 1 : 0;
  103. }
  104. const char* UnityReplayKitGetBroadcastURL()
  105. {
  106. NSURL *url = [[UnityReplayKit sharedInstance] broadcastURL];
  107. if (url != nil)
  108. {
  109. return [[url absoluteString] UTF8String];
  110. }
  111. return nullptr;
  112. }
  113. extern "C" float UnityScreenScaleFactor(UIScreen* screen);
  114. #else
  115. // Impl when ReplayKit is not available.
  116. int UnityReplayKitAPIAvailable() { return 0; }
  117. int UnityReplayKitRecordingAvailable() { return 0; }
  118. const char* UnityReplayKitLastError() { return NULL; }
  119. int UnityReplayKitStartRecording(int enableMicrophone) { return 0; }
  120. int UnityReplayKitIsRecording() { return 0; }
  121. int UnityReplayKitStopRecording() { return 0; }
  122. int UnityReplayKitDiscard() { return 0; }
  123. int UnityReplayKitPreview() { return 0; }
  124. int UnityReplayKitIsCameraEnabled() { return 0; }
  125. int UnityReplayKitSetCameraEnabled(bool) { return 0; }
  126. int UnityReplayKitIsMicrophoneEnabled() { return 0; }
  127. int UnityReplayKitSetMicrophoneEnabled(bool) { return 0; }
  128. int UnityReplayKitShowCameraPreviewAt(float x, float y) { return 0; }
  129. void UnityReplayKitHideCameraPreview() {}
  130. void UnityReplayKitTriggerBroadcastStatusCallback(void*, bool, const char*);
  131. int UnityReplayKitBroadcastingAPIAvailable() { return 0; }
  132. void UnityReplayKitStartBroadcasting(void* callback) { UnityReplayKitTriggerBroadcastStatusCallback(callback, false, "ReplayKit not implemented."); }
  133. void UnityReplayKitStopBroadcasting() {}
  134. int UnityReplayKitIsBroadcasting() { return 0; }
  135. const char* UnityReplayKitGetBroadcastURL() { return nullptr; }
  136. #endif // UNITY_REPLAY_KIT_AVAILABLE
  137. } // extern "C"