BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / Platform / Samples / VrVoiceChat / Scripts / VoipManager.cs

VoipManager.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
VoipManager.cs 2.2 KiB Download Raw
1namespace Oculus.Platform.Samples.VrVoiceChat
2{
3 using UnityEngine;
4 using System.Collections;
5
6 using Oculus.Platform;
7 using Oculus.Platform.Models;
8
9 // Helper class to manage the Voice-over-IP connection to the
10 // remote user
11 public class VoipManager
12 {
13 // the ID of the remote user I expect to talk to
14 private ulong m_remoteID;
15
16 // the last reported state of the VOIP connection
17 private PeerConnectionState m_state = PeerConnectionState.Unknown;
18
19 // the GameObject where the remote VOIP will project from
20 private readonly GameObject m_remoteHead;
21
22 public VoipManager(GameObject remoteHead)
23 {
24 m_remoteHead = remoteHead;
25
26 Voip.SetVoipConnectRequestCallback(VoipConnectRequestCallback);
27 Voip.SetVoipStateChangeCallback(VoipStateChangedCallback);
28 }
29
30 public void ConnectTo(ulong userID)
31 {
32 m_remoteID = userID;
33 var audioSource = m_remoteHead.AddComponent<VoipAudioSourceHiLevel>();
34 audioSource.senderID = userID;
35
36 // ID comparison is used to decide who initiates and who gets the Callback
37 if (PlatformManager.MyID < m_remoteID)
38 {
39 Voip.Start(userID);
40 }
41 }
42
43
44 public void Disconnect()
45 {
46 if (m_remoteID != 0)
47 {
48 Voip.Stop(m_remoteID);
49 Object.Destroy(m_remoteHead.GetComponent<VoipAudioSourceHiLevel>(), 0);
50 m_remoteID = 0;
51 m_state = PeerConnectionState.Unknown;
52 }
53 }
54
55 public bool Connected
56 {
57 get
58 {
59 return m_state == PeerConnectionState.Connected;
60 }
61 }
62
63 void VoipConnectRequestCallback(Message<NetworkingPeer> msg)
64 {
65 Debug.LogFormat("Voip request from {0}, authorized is {1}", msg.Data.ID, m_remoteID);
66
67 if (msg.Data.ID == m_remoteID)
68 {
69 Voip.Accept(msg.Data.ID);
70 }
71 }
72
73 void VoipStateChangedCallback(Message<NetworkingPeer> msg)
74 {
75 Debug.LogFormat("Voip state to {0} changed to {1}", msg.Data.ID, msg.Data.State);
76
77 if (msg.Data.ID == m_remoteID)
78 {
79 m_state = msg.Data.State;
80
81 if (m_state == PeerConnectionState.Timeout &&
82 // ID comparison is used to decide who initiates and who gets the Callback
83 PlatformManager.MyID < m_remoteID)
84 {
85 // keep trying until hangup!
86 Voip.Start(m_remoteID);
87 }
88 }
89
90 PlatformManager.SetBackgroundColorForState();
91 }
92 }
93}