75 lines
1.8 KiB
C#
75 lines
1.8 KiB
C#
using Oculus.Platform.Models;
|
|
using UnityEngine;
|
|
|
|
namespace Oculus.Platform.Samples.VrVoiceChat
|
|
{
|
|
public class VoipManager
|
|
{
|
|
private ulong m_remoteID;
|
|
|
|
private PeerConnectionState m_state;
|
|
|
|
private readonly GameObject m_remoteHead;
|
|
|
|
public bool Connected
|
|
{
|
|
get
|
|
{
|
|
return m_state == PeerConnectionState.Connected;
|
|
}
|
|
}
|
|
|
|
public VoipManager(GameObject remoteHead)
|
|
{
|
|
m_remoteHead = remoteHead;
|
|
Voip.SetVoipConnectRequestCallback(VoipConnectRequestCallback);
|
|
Voip.SetVoipStateChangeCallback(VoipStateChangedCallback);
|
|
}
|
|
|
|
public void ConnectTo(ulong userID)
|
|
{
|
|
m_remoteID = userID;
|
|
VoipAudioSourceHiLevel voipAudioSourceHiLevel = m_remoteHead.AddComponent<VoipAudioSourceHiLevel>();
|
|
voipAudioSourceHiLevel.senderID = userID;
|
|
if (PlatformManager.MyID < m_remoteID)
|
|
{
|
|
Voip.Start(userID);
|
|
}
|
|
}
|
|
|
|
public void Disconnect()
|
|
{
|
|
if (m_remoteID != 0)
|
|
{
|
|
Voip.Stop(m_remoteID);
|
|
Object.Destroy(m_remoteHead.GetComponent<VoipAudioSourceHiLevel>(), 0f);
|
|
m_remoteID = 0uL;
|
|
m_state = PeerConnectionState.Unknown;
|
|
}
|
|
}
|
|
|
|
private void VoipConnectRequestCallback(Message<Oculus.Platform.Models.NetworkingPeer> msg)
|
|
{
|
|
Debug.LogFormat("Voip request from {0}, authorized is {1}", msg.Data.ID, m_remoteID);
|
|
if (msg.Data.ID == m_remoteID)
|
|
{
|
|
Voip.Accept(msg.Data.ID);
|
|
}
|
|
}
|
|
|
|
private void VoipStateChangedCallback(Message<Oculus.Platform.Models.NetworkingPeer> msg)
|
|
{
|
|
Debug.LogFormat("Voip state to {0} changed to {1}", msg.Data.ID, msg.Data.State);
|
|
if (msg.Data.ID == m_remoteID)
|
|
{
|
|
m_state = msg.Data.State;
|
|
if (m_state == PeerConnectionState.Timeout && PlatformManager.MyID < m_remoteID)
|
|
{
|
|
Voip.Start(m_remoteID);
|
|
}
|
|
}
|
|
PlatformManager.SetBackgroundColorForState();
|
|
}
|
|
}
|
|
}
|