Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using System;
using UnityEngine;
#if UNITY_ANDROID
using UnityEngine.Android;
#endif
#if UNITY_IOS
using System.Collections;
#endif

namespace StreamVideo.ExampleProject.UI
{
public class PermissionsManager
{
public enum PermissionType
{
Camera,
Microphone
}

public PermissionsManager(MonoBehaviour coroutineRunner)
{
_coroutineRunner = coroutineRunner;
}

public bool HasPermission(PermissionType permissionType)
{
#if UNITY_STANDALONE
return true;
#elif UNITY_ANDROID
var androidPermission = PermissionTypeToAndroidPermission(permissionType);
return Permission.HasUserAuthorizedPermission(androidPermission);
#elif UNITY_IOS
var iosPermission = PermissionTypeToIOSPermission(permissionType);
return Application.HasUserAuthorization(iosPermission);
#else
Debug.LogWarning($"Handling permissions not implemented for platform: {Application.platform}. Requested {permissionType}. Assuming permission is granted.");
return true;
#endif
}


public void RequestPermission(PermissionType permissionType, Action onGranted = null, Action onDenied = null)
{
#if UNITY_ANDROID
RequestAndroidPermission(permissionType, onGranted, onDenied);
#elif UNITY_IOS
_coroutineRunner.StartCoroutine(RequestIOSPermissionCoroutine(permissionType, onGranted, onDenied));
#else
Debug.LogError($"Handling permissions not implemented for platform: " + Application.platform);
#endif
}

private readonly MonoBehaviour _coroutineRunner;

#if UNITY_ANDROID
private void RequestAndroidPermission(PermissionType permissionType, Action onGranted = null,
Action onDenied = null)
{
var androidPermission = PermissionTypeToAndroidPermission(permissionType);
var callbacks = new PermissionCallbacks();
Permission.RequestUserPermission(androidPermission, callbacks);

callbacks.PermissionGranted += permissionName =>
{
if (androidPermission == permissionName)
{
onGranted?.Invoke();
}
};
callbacks.PermissionDenied += permissionName =>
{
if (androidPermission == permissionName)
{
onDenied?.Invoke();
}
};
callbacks.PermissionDeniedAndDontAskAgain += permissionName =>
{
if (androidPermission == permissionName)
{
onDenied?.Invoke();
}
};
}

private string PermissionTypeToAndroidPermission(PermissionType type)
{
switch (type)
{
case PermissionType.Camera: return Permission.Camera;
case PermissionType.Microphone: return Permission.Microphone;
default:
throw new ArgumentOutOfRangeException(nameof(type), type, null);
}
}
#endif

#if UNITY_IOS
private IEnumerator RequestIOSPermissionCoroutine(PermissionType permissionType, Action onGranted = null,
Action onDenied = null)
{
var iosPermission = PermissionTypeToIOSPermission(permissionType);
yield return Application.RequestUserAuthorization(iosPermission);

if (Application.HasUserAuthorization(iosPermission))
{
onGranted?.Invoke();
}
else
{
onDenied?.Invoke();
}
}

UserAuthorization PermissionTypeToIOSPermission(PermissionType type)
{
switch (type)
{
case PermissionType.Camera: return UserAuthorization.WebCam;
case PermissionType.Microphone: return UserAuthorization.Microphone;
default:
throw new ArgumentOutOfRangeException(nameof(type), type, null);
}
}
#endif
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
using StreamVideo.Core.StatefulModels;
using StreamVideo.Libs.Utils;
using UnityEngine;
#if UNITY_ANDROID
using UnityEngine.Android;
#endif

namespace StreamVideo.ExampleProject.UI
{
Expand All @@ -19,76 +16,10 @@ public class UIManager : MonoBehaviour
public VideoResolution SenderVideoResolution => new VideoResolution(_senderVideoWidth, _senderVideoHeight);
public int SenderVideoFps => _senderVideoFps;

public void RequestCameraPermissions(Action onGranted = null, Action onDenied = null)
{
#if UNITY_ANDROID
var callbacks = new PermissionCallbacks();
Permission.RequestUserPermission(Permission.Camera, callbacks);

callbacks.PermissionGranted += _ => { onGranted?.Invoke(); };
callbacks.PermissionDenied += permissionName =>
{
onDenied?.Invoke();
Debug.LogError($"{permissionName} permission was not granted. Video capturing will not work.");
};
callbacks.PermissionDeniedAndDontAskAgain += (permissionName) =>
{
onDenied?.Invoke();
Debug.LogError($"{permissionName} permission was not granted. Video capturing will not work.");
};
#elif UNITY_IOS
Debug.LogError($"Handling permissions not implemented for platform: " + Application.platform);
#endif
}

public bool HasUserAuthorizedCameraPermission()
{
#if UNITY_STANDALONE
return true; //StreamTodo: check if this is true for all platforms
#elif UNITY_ANDROID
return Permission.HasUserAuthorizedPermission(Permission.Camera);
#else
Debug.LogError($"Handling permissions not implemented for platform: " + Application.platform);
#endif
}

public void RequestMicrophonePermissions(Action onGranted = null, Action onDenied = null)
{
#if UNITY_ANDROID
var callbacks = new PermissionCallbacks();

callbacks.PermissionGranted += _ => { onGranted?.Invoke(); };
callbacks.PermissionDenied += permissionName =>
{
onDenied?.Invoke();
Debug.LogError($"{permissionName} permission was not granted. Video capturing will not work.");
};
callbacks.PermissionDeniedAndDontAskAgain += (permissionName) =>
{
onDenied?.Invoke();
Debug.LogError($"{permissionName} permission was not granted. Video capturing will not work.");
};

Permission.RequestUserPermission(Permission.Microphone, callbacks);

#else
Debug.LogError($"Handling permissions not implemented for platform: " + Application.platform);
#endif
}

public bool HasUserAuthorizedMicrophonePermission()
{
#if UNITY_STANDALONE
return true; //StreamTodo: check if this is true for all platforms
#elif UNITY_ANDROID
return Permission.HasUserAuthorizedPermission(Permission.Microphone);
#else
Debug.LogError($"Handling permissions not implemented for platform: " + Application.platform);
#endif
}

protected void Awake()
{
_permissionsManager = new PermissionsManager(this);

_videoManager.Init();

_videoManager.CallStarted += OnCallStarted;
Expand All @@ -100,9 +31,10 @@ protected void Awake()
_portraitModeUIScreensSet.Init(_videoManager, uiManager: this);
_landscapeModeUIScreensSet.Init(_videoManager, uiManager: this);

if (!HasUserAuthorizedCameraPermission())
if (!_permissionsManager.HasPermission(PermissionsManager.PermissionType.Camera))
{
RequestCameraPermissions(onGranted: () => { SelectFirstWorkingCameraOrDefaultAsync().LogIfFailed(); },
_permissionsManager.RequestPermission(PermissionsManager.PermissionType.Camera,
onGranted: () => { SelectFirstWorkingCameraOrDefaultAsync().LogIfFailed(); },
onDenied: ()
=> Debug.LogError("Camera permission was not granted. Video capturing will not work."));
}
Expand All @@ -111,9 +43,10 @@ protected void Awake()
SelectFirstWorkingCameraOrDefaultAsync().LogIfFailed();
}

if (!HasUserAuthorizedMicrophonePermission())
if (!_permissionsManager.HasPermission(PermissionsManager.PermissionType.Microphone))
{
RequestMicrophonePermissions(onGranted: SelectFirstMicrophone,
_permissionsManager.RequestPermission(PermissionsManager.PermissionType.Microphone,
onGranted: SelectFirstMicrophone,
onDenied: ()
=> Debug.LogError("Microphone permission was not granted. Audio capturing will not work."));
}
Expand Down Expand Up @@ -151,13 +84,15 @@ protected void OnDestroy()

[SerializeField]
private UIScreensSet _landscapeModeUIScreensSet;

[SerializeField]
private UIScreensSet _portraitModeUIScreensSet;

[SerializeField]
private bool _forceTestPortraitMode;

private PermissionsManager _permissionsManager;

private void OnCallStarted(IStreamCall call) => ShowCallScreen(call);

private void OnCallEnded() => ShowMainScreen();
Expand Down Expand Up @@ -242,10 +177,10 @@ private void SelectFirstMicrophone()
private UIScreensSet GetCurrentScreenSet()
{
var isPortraitMode = IsPotraitMode();

_portraitModeUIScreensSet.gameObject.SetActive(isPortraitMode);
_landscapeModeUIScreensSet.gameObject.SetActive(!isPortraitMode);

return isPortraitMode ? _portraitModeUIScreensSet : _landscapeModeUIScreensSet;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public Task<bool> TestDeviceAsync(TDeviceInfo device, float timeout = 1f)
return OnTestDeviceAsync(device, (int)(timeout * 1000));
}

// StreamTODO: add filter option. E.g. so we can easily consider only front cameras on ios/android
public async Task<TDeviceInfo?> TryFindFirstWorkingDeviceAsync(float testTimeoutPerDevice = 1f)
{
foreach (var device in EnumerateDevices())
Expand Down
Loading
Loading