#if UNITY_ANDROID #pragma warning disable 0642 // Possible mistaken empty statement namespace GooglePlayGames.Android { using System; using System.Collections.Generic; using GooglePlayGames.BasicApi; using GooglePlayGames.BasicApi.Video; using GooglePlayGames.OurUtils; using UnityEngine; internal class AndroidVideoClient : IVideoClient { private volatile AndroidJavaObject mVideosClient; private bool mIsCaptureSupported; private OnCaptureOverlayStateListenerProxy mOnCaptureOverlayStateListenerProxy = null; public AndroidVideoClient(bool isCaptureSupported, AndroidJavaObject account) { mIsCaptureSupported = isCaptureSupported; using (var gamesClass = new AndroidJavaClass("com.google.android.gms.games.Games")) { mVideosClient = gamesClass.CallStatic("getVideosClient", AndroidHelperFragment.GetActivity(), account); } } public void GetCaptureCapabilities(Action callback) { callback = ToOnGameThread(callback); using (var task = mVideosClient.Call("getCaptureCapabilities")) { AndroidTaskUtils.AddOnSuccessListener( task, videoCapabilities => callback(ResponseStatus.Success, CreateVideoCapabilities(videoCapabilities))); AndroidTaskUtils.AddOnFailureListener( task, exception => callback(ResponseStatus.InternalError, null)); } } public void ShowCaptureOverlay() { AndroidHelperFragment.ShowCaptureOverlayUI(); } public void GetCaptureState(Action callback) { callback = ToOnGameThread(callback); using (var task = mVideosClient.Call("getCaptureState")) { AndroidTaskUtils.AddOnSuccessListener( task, captureState => callback(ResponseStatus.Success, CreateVideoCaptureState(captureState))); AndroidTaskUtils.AddOnFailureListener( task, exception => callback(ResponseStatus.InternalError, null)); } } public void IsCaptureAvailable(VideoCaptureMode captureMode, Action callback) { callback = ToOnGameThread(callback); using (var task = mVideosClient.Call("isCaptureAvailable", ToVideoCaptureMode(captureMode))) { AndroidTaskUtils.AddOnSuccessListener( task, isCaptureAvailable => callback(ResponseStatus.Success, isCaptureAvailable)); AndroidTaskUtils.AddOnFailureListener( task, exception => callback(ResponseStatus.InternalError, false)); } } public bool IsCaptureSupported() { return mIsCaptureSupported; } public void RegisterCaptureOverlayStateChangedListener(CaptureOverlayStateListener listener) { if (mOnCaptureOverlayStateListenerProxy != null) { UnregisterCaptureOverlayStateChangedListener(); } mOnCaptureOverlayStateListenerProxy = new OnCaptureOverlayStateListenerProxy(listener); using (mVideosClient.Call("registerOnCaptureOverlayStateChangedListener", mOnCaptureOverlayStateListenerProxy)) ; } public void UnregisterCaptureOverlayStateChangedListener() { if (mOnCaptureOverlayStateListenerProxy != null) { using (mVideosClient.Call("unregisterOnCaptureOverlayStateChangedListener", mOnCaptureOverlayStateListenerProxy)) ; mOnCaptureOverlayStateListenerProxy = null; } } private class OnCaptureOverlayStateListenerProxy : AndroidJavaProxy { private CaptureOverlayStateListener mListener; public OnCaptureOverlayStateListenerProxy(CaptureOverlayStateListener listener) : base("com/google/android/gms/games/VideosClient$OnCaptureOverlayStateListener") { mListener = listener; } public void onCaptureOverlayStateChanged(int overlayState) { PlayGamesHelperObject.RunOnGameThread(() => mListener.OnCaptureOverlayStateChanged(FromVideoCaptureOverlayState(overlayState)) ); } private static VideoCaptureOverlayState FromVideoCaptureOverlayState(int overlayState) { switch (overlayState) { case 1: // CAPTURE_OVERLAY_STATE_SHOWN return VideoCaptureOverlayState.Shown; case 2: // CAPTURE_OVERLAY_STATE_CAPTURE_STARTED return VideoCaptureOverlayState.Started; case 3: // CAPTURE_OVERLAY_STATE_CAPTURE_STOPPED return VideoCaptureOverlayState.Stopped; case 4: // CAPTURE_OVERLAY_STATE_DISMISSED return VideoCaptureOverlayState.Dismissed; default: return VideoCaptureOverlayState.Unknown; } } } private static Action ToOnGameThread(Action toConvert) { return (val1, val2) => PlayGamesHelperObject.RunOnGameThread(() => toConvert(val1, val2)); } private static VideoQualityLevel FromVideoQualityLevel(int captureQualityJava) { switch (captureQualityJava) { case 0: // QUALITY_LEVEL_SD return VideoQualityLevel.SD; case 1: // QUALITY_LEVEL_HD return VideoQualityLevel.HD; case 2: // QUALITY_LEVEL_XHD return VideoQualityLevel.XHD; case 3: // QUALITY_LEVEL_FULLHD return VideoQualityLevel.FullHD; default: return VideoQualityLevel.Unknown; } } private static VideoCaptureMode FromVideoCaptureMode(int captureMode) { switch (captureMode) { case 0: // CAPTURE_MODE_FILE return VideoCaptureMode.File; case 1: // CAPTURE_MODE_STREAM return VideoCaptureMode.Stream; default: return VideoCaptureMode.Unknown; } } private static int ToVideoCaptureMode(VideoCaptureMode captureMode) { switch (captureMode) { case VideoCaptureMode.File: return 0; // CAPTURE_MODE_FILE case VideoCaptureMode.Stream: return 1; // CAPTURE_MODE_STREAM default: return -1; // CAPTURE_MODE_UNKNOWN } } private static VideoCaptureState CreateVideoCaptureState(AndroidJavaObject videoCaptureState) { bool isCapturing = videoCaptureState.Call("isCapturing"); VideoCaptureMode captureMode = FromVideoCaptureMode(videoCaptureState.Call("getCaptureMode")); VideoQualityLevel qualityLevel = FromVideoQualityLevel(videoCaptureState.Call("getCaptureQuality")); bool isOverlayVisible = videoCaptureState.Call("isOverlayVisible"); bool isPaused = videoCaptureState.Call("isPaused"); return new VideoCaptureState(isCapturing, captureMode, qualityLevel, isOverlayVisible, isPaused); } private static VideoCapabilities CreateVideoCapabilities(AndroidJavaObject videoCapabilities) { bool isCameraSupported = videoCapabilities.Call("isCameraSupported"); bool isMicSupported = videoCapabilities.Call("isMicSupported"); bool isWriteStorageSupported = videoCapabilities.Call("isWriteStorageSupported"); bool[] captureModesSupported = videoCapabilities.Call("getSupportedCaptureModes"); bool[] qualityLevelsSupported = videoCapabilities.Call("getSupportedQualityLevels"); return new VideoCapabilities(isCameraSupported, isMicSupported, isWriteStorageSupported, captureModesSupported, qualityLevelsSupported); } } } #endif