// // /*===============================================================================
// // Copyright (C) 2024 PhantomsXR Ltd. All Rights Reserved.
// //
// // This file is part of the Assembly-CSharp.
// //
// // The Nin1 cannot be copied, distributed, or made available to
// // third-parties for commercial purposes without written permission of PhantomsXR Ltd.
// //
// // Contact nswell@phantomsxr.com for licensing requests.
// // ===============================================================================*/
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine.Assertions;
namespace Phantom.XRMOD.GameServices.Runtime
{
///
/// Types of voice channels that can be joined.
///
public enum JoinChannelType
{
///
/// Standard group voice channel.
///
Group,
///
/// Echo channel for testing the user's voice.
///
Echo,
///
/// Positional audio channel for 3D spatialized voice.
///
Positional
}
///
/// Manages the Voice communication system.
/// Supports various providers like Vivox, Agora, and Photon Voice.
///
public class VoiceSystemManager : IVoiceProvider, ITextMessage
{
private IVoiceProvider voiceProvider;
private static VoiceSystemManager _INSTANCE;
///
/// Singleton instance of VoiceSystemManager.
///
public static VoiceSystemManager GetInstance => _INSTANCE ??= new VoiceSystemManager();
///
/// The name of the current voice channel.
///
public string ChannelName;
///
/// Whether the voice system is initialized.
///
public bool Initialized { get; private set; }
private VoiceSystemManager()
{
#if USE_VIVOX
voiceProvider = new VivoxProvider();
#elif USE_AGORA_RTC
voiceProvider = new AgoraOfficialSDKProvider();
#elif USE_PHOTON_VOICE
voiceProvider = new PhotonVoice.PhotonVoiceProvider();
#endif
Assert.IsNotNull(voiceProvider, "Voice provider is empty.");
}
///
/// Initializes the voice provider with a JSON configuration and 3D audio properties.
///
/// JSON configuration string for the voice provider.
/// Properties for 3D audio positioning.
public void Initialize(string _configuration, Voice3DProperties _voice3DProperties)
{
if (Initialized) return;
voiceProvider.Initialize(_configuration, _voice3DProperties);
Initialized = true;
}
///
/// De-initializes the voice provider.
///
public void DeInitialize()
{
voiceProvider.DeInitialize();
Initialized = false;
}
///
/// Join voice channel by channel name
///
/// will join channel's name
/// The channel type is indicated by the method used to initiate the channel join.
/// JoinEchoChannelAsync joins an echo channel, JoinGroupChannelAsyc joins a group channel,
/// and JoinPositionalChannelAsync joins a positional channel.
///
public void JoinVoiceChannel(string _channelName, JoinChannelType _joinChannelType)
{
if (voiceProvider == null || !Initialized) return;
voiceProvider.JoinVoiceChannel(_channelName, _joinChannelType);
ChannelName = _channelName;
}
///
/// Leave current voice channel
///
public void LeaveVoiceChannel()
{
if (voiceProvider == null || !Initialized) return;
voiceProvider.LeaveVoiceChannel();
ChannelName = string.Empty;
}
///
/// Create a new voice channel by channel name
///
/// will create channel's name
/// The channel type is indicated by the method used to initiate the channel join.
/// JoinEchoChannelAsync joins an echo channel, JoinGroupChannelAsyc joins a group channel,
/// and JoinPositionalChannelAsync joins a positional channel.
///
public void CreateVoiceChannel(string _channelName, JoinChannelType _joinChannelType)
{
if (voiceProvider == null || !Initialized) return;
voiceProvider.CreateVoiceChannel(_channelName, _joinChannelType);
}
///
/// Mute self microphone
///
/// Muted state, True is muted otherwise is unmuted
public void MuteSelfMicrophone(bool _muted)
{
if (voiceProvider == null || !Initialized) return;
voiceProvider.MuteSelfMicrophone(_muted);
}
///
/// Mute other user microphone
///
/// Muted state, True is muted otherwise is unmuted
/// Channel name
/// Mute target
public void MuteOtherMicrophone(bool _muted, string _channelId, string _userId)
{
if (voiceProvider == null || !Initialized) return;
voiceProvider.MuteOtherMicrophone(_muted, _channelId, _userId);
}
///
/// "Block" a user, bidirectionally muting audio/text between that player and the local user.
///
/// Block state, True is blocked otherwise is unblock
/// Channel name
/// The userid to bidirectionally mute or unmute
public void BlockUser(bool _block, string _channelId, string _userId)
{
if (voiceProvider == null || !Initialized) return;
voiceProvider.BlockUser(_block, _channelId, _userId);
}
///
/// All participant
///
///
public List GetParticipants()
{
if (voiceProvider == null || !Initialized) return new List();
return voiceProvider.GetParticipants();
}
///
/// Get single participant by user id/name
///
/// The user id or user name
///
public VoiceParticipant GetParticipant(string _userId)
{
if (voiceProvider == null || !Initialized) return null;
return voiceProvider.GetParticipant(_userId);
}
///
/// You can send a message to a player with playerId being the playerId of
/// the player that the message should be sent to, and message being the message that should be sent.
///
/// The user id or user name
/// The direct message
///
public Task SendDirectTextMessage(string _userId, string _message)
{
if (voiceProvider == null || !Initialized) return null;
return ((ITextMessage) voiceProvider).SendDirectTextMessage(_userId, _message);
}
///
/// Access the history of a direct message conversation between two players
///
/// The user id or user name
///
public Task> FetchDirectTextMessageHistory(string _userId)
{
if (voiceProvider == null || !Initialized) return null;
return ((ITextMessage) voiceProvider).FetchDirectTextMessageHistory(_userId);
}
///
/// Edit the text of messages they have sent.
/// with the messageId being the ID of the message to change,
/// and newMessage being the updated text of the message.
///
/// The old message
/// NewMessage being the updated text of the message.
///
public Task EditDirectedTextMessage(TextMessage _message, string _editedMessageText)
{
if (voiceProvider == null || !Initialized) return null;
return ((ITextMessage) voiceProvider).EditDirectedTextMessage(_message, _editedMessageText);
}
///
/// Delete the message by message id
///
/// The message id
///
public Task DeletingDirectTextMessage(string _messageId)
{
if (voiceProvider == null || !Initialized) return null;
return ((ITextMessage) voiceProvider).DeletingDirectTextMessage(_messageId);
}
}
}