AndroidVideoClient.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #if UNITY_ANDROID
  2. #pragma warning disable 0642 // Possible mistaken empty statement
  3. namespace GooglePlayGames.Android
  4. {
  5. using System;
  6. using System.Collections.Generic;
  7. using GooglePlayGames.BasicApi;
  8. using GooglePlayGames.BasicApi.Video;
  9. using GooglePlayGames.OurUtils;
  10. using UnityEngine;
  11. internal class AndroidVideoClient : IVideoClient
  12. {
  13. private volatile AndroidJavaObject mVideosClient;
  14. private bool mIsCaptureSupported;
  15. private OnCaptureOverlayStateListenerProxy mOnCaptureOverlayStateListenerProxy = null;
  16. public AndroidVideoClient(bool isCaptureSupported, AndroidJavaObject account)
  17. {
  18. mIsCaptureSupported = isCaptureSupported;
  19. using (var gamesClass = new AndroidJavaClass("com.google.android.gms.games.Games"))
  20. {
  21. mVideosClient = gamesClass.CallStatic<AndroidJavaObject>("getVideosClient",
  22. AndroidHelperFragment.GetActivity(), account);
  23. }
  24. }
  25. public void GetCaptureCapabilities(Action<ResponseStatus, VideoCapabilities> callback)
  26. {
  27. callback = ToOnGameThread(callback);
  28. using (var task = mVideosClient.Call<AndroidJavaObject>("getCaptureCapabilities"))
  29. {
  30. AndroidTaskUtils.AddOnSuccessListener<AndroidJavaObject>(
  31. task,
  32. videoCapabilities => callback(ResponseStatus.Success, CreateVideoCapabilities(videoCapabilities)));
  33. AndroidTaskUtils.AddOnFailureListener(
  34. task,
  35. exception => callback(ResponseStatus.InternalError, null));
  36. }
  37. }
  38. public void ShowCaptureOverlay()
  39. {
  40. AndroidHelperFragment.ShowCaptureOverlayUI();
  41. }
  42. public void GetCaptureState(Action<ResponseStatus, VideoCaptureState> callback)
  43. {
  44. callback = ToOnGameThread(callback);
  45. using (var task = mVideosClient.Call<AndroidJavaObject>("getCaptureState"))
  46. {
  47. AndroidTaskUtils.AddOnSuccessListener<AndroidJavaObject>(
  48. task,
  49. captureState =>
  50. callback(ResponseStatus.Success, CreateVideoCaptureState(captureState)));
  51. AndroidTaskUtils.AddOnFailureListener(
  52. task,
  53. exception => callback(ResponseStatus.InternalError, null));
  54. }
  55. }
  56. public void IsCaptureAvailable(VideoCaptureMode captureMode, Action<ResponseStatus, bool> callback)
  57. {
  58. callback = ToOnGameThread(callback);
  59. using (var task =
  60. mVideosClient.Call<AndroidJavaObject>("isCaptureAvailable", ToVideoCaptureMode(captureMode)))
  61. {
  62. AndroidTaskUtils.AddOnSuccessListener<bool>(
  63. task,
  64. isCaptureAvailable => callback(ResponseStatus.Success, isCaptureAvailable));
  65. AndroidTaskUtils.AddOnFailureListener(
  66. task,
  67. exception => callback(ResponseStatus.InternalError, false));
  68. }
  69. }
  70. public bool IsCaptureSupported()
  71. {
  72. return mIsCaptureSupported;
  73. }
  74. public void RegisterCaptureOverlayStateChangedListener(CaptureOverlayStateListener listener)
  75. {
  76. if (mOnCaptureOverlayStateListenerProxy != null)
  77. {
  78. UnregisterCaptureOverlayStateChangedListener();
  79. }
  80. mOnCaptureOverlayStateListenerProxy = new OnCaptureOverlayStateListenerProxy(listener);
  81. using (mVideosClient.Call<AndroidJavaObject>("registerOnCaptureOverlayStateChangedListener",
  82. mOnCaptureOverlayStateListenerProxy)) ;
  83. }
  84. public void UnregisterCaptureOverlayStateChangedListener()
  85. {
  86. if (mOnCaptureOverlayStateListenerProxy != null)
  87. {
  88. using (mVideosClient.Call<AndroidJavaObject>("unregisterOnCaptureOverlayStateChangedListener",
  89. mOnCaptureOverlayStateListenerProxy)) ;
  90. mOnCaptureOverlayStateListenerProxy = null;
  91. }
  92. }
  93. private class OnCaptureOverlayStateListenerProxy : AndroidJavaProxy
  94. {
  95. private CaptureOverlayStateListener mListener;
  96. public OnCaptureOverlayStateListenerProxy(CaptureOverlayStateListener listener)
  97. : base("com/google/android/gms/games/VideosClient$OnCaptureOverlayStateListener")
  98. {
  99. mListener = listener;
  100. }
  101. public void onCaptureOverlayStateChanged(int overlayState)
  102. {
  103. PlayGamesHelperObject.RunOnGameThread(() =>
  104. mListener.OnCaptureOverlayStateChanged(FromVideoCaptureOverlayState(overlayState))
  105. );
  106. }
  107. private static VideoCaptureOverlayState FromVideoCaptureOverlayState(int overlayState)
  108. {
  109. switch (overlayState)
  110. {
  111. case 1: // CAPTURE_OVERLAY_STATE_SHOWN
  112. return VideoCaptureOverlayState.Shown;
  113. case 2: // CAPTURE_OVERLAY_STATE_CAPTURE_STARTED
  114. return VideoCaptureOverlayState.Started;
  115. case 3: // CAPTURE_OVERLAY_STATE_CAPTURE_STOPPED
  116. return VideoCaptureOverlayState.Stopped;
  117. case 4: // CAPTURE_OVERLAY_STATE_DISMISSED
  118. return VideoCaptureOverlayState.Dismissed;
  119. default:
  120. return VideoCaptureOverlayState.Unknown;
  121. }
  122. }
  123. }
  124. private static Action<T1, T2> ToOnGameThread<T1, T2>(Action<T1, T2> toConvert)
  125. {
  126. return (val1, val2) => PlayGamesHelperObject.RunOnGameThread(() => toConvert(val1, val2));
  127. }
  128. private static VideoQualityLevel FromVideoQualityLevel(int captureQualityJava)
  129. {
  130. switch (captureQualityJava)
  131. {
  132. case 0: // QUALITY_LEVEL_SD
  133. return VideoQualityLevel.SD;
  134. case 1: // QUALITY_LEVEL_HD
  135. return VideoQualityLevel.HD;
  136. case 2: // QUALITY_LEVEL_XHD
  137. return VideoQualityLevel.XHD;
  138. case 3: // QUALITY_LEVEL_FULLHD
  139. return VideoQualityLevel.FullHD;
  140. default:
  141. return VideoQualityLevel.Unknown;
  142. }
  143. }
  144. private static VideoCaptureMode FromVideoCaptureMode(int captureMode)
  145. {
  146. switch (captureMode)
  147. {
  148. case 0: // CAPTURE_MODE_FILE
  149. return VideoCaptureMode.File;
  150. case 1: // CAPTURE_MODE_STREAM
  151. return VideoCaptureMode.Stream;
  152. default:
  153. return VideoCaptureMode.Unknown;
  154. }
  155. }
  156. private static int ToVideoCaptureMode(VideoCaptureMode captureMode)
  157. {
  158. switch (captureMode)
  159. {
  160. case VideoCaptureMode.File:
  161. return 0; // CAPTURE_MODE_FILE
  162. case VideoCaptureMode.Stream:
  163. return 1; // CAPTURE_MODE_STREAM
  164. default:
  165. return -1; // CAPTURE_MODE_UNKNOWN
  166. }
  167. }
  168. private static VideoCaptureState CreateVideoCaptureState(AndroidJavaObject videoCaptureState)
  169. {
  170. bool isCapturing = videoCaptureState.Call<bool>("isCapturing");
  171. VideoCaptureMode captureMode = FromVideoCaptureMode(videoCaptureState.Call<int>("getCaptureMode"));
  172. VideoQualityLevel qualityLevel = FromVideoQualityLevel(videoCaptureState.Call<int>("getCaptureQuality"));
  173. bool isOverlayVisible = videoCaptureState.Call<bool>("isOverlayVisible");
  174. bool isPaused = videoCaptureState.Call<bool>("isPaused");
  175. return new VideoCaptureState(isCapturing, captureMode,
  176. qualityLevel, isOverlayVisible, isPaused);
  177. }
  178. private static VideoCapabilities CreateVideoCapabilities(AndroidJavaObject videoCapabilities)
  179. {
  180. bool isCameraSupported = videoCapabilities.Call<bool>("isCameraSupported");
  181. bool isMicSupported = videoCapabilities.Call<bool>("isMicSupported");
  182. bool isWriteStorageSupported = videoCapabilities.Call<bool>("isWriteStorageSupported");
  183. bool[] captureModesSupported = videoCapabilities.Call<bool[]>("getSupportedCaptureModes");
  184. bool[] qualityLevelsSupported = videoCapabilities.Call<bool[]>("getSupportedQualityLevels");
  185. return new VideoCapabilities(isCameraSupported, isMicSupported, isWriteStorageSupported,
  186. captureModesSupported, qualityLevelsSupported);
  187. }
  188. }
  189. }
  190. #endif