| 1 | namespace Oculus.Platform.Samples.VrVoiceChat |
| 2 | { |
| 3 | using UnityEngine; |
| 4 | using System; |
| 5 | using System.Collections.Generic; |
| 6 | using Oculus.Platform; |
| 7 | using Oculus.Platform.Models; |
| 8 | |
| 9 | // Helper class to manage Room creation, membership and invites. |
| 10 | // Rooms are a mechanism to help Oculus users create a shared experience. |
| 11 | // Users can only be in one Room at a time. If the Owner of a room |
| 12 | // leaves, then ownership is transferred to some other member. |
| 13 | // Here we use rooms to create the notion of a 'call' to help us |
| 14 | // invite a Friend and establish a VOIP and P2P connection. |
| 15 | public class RoomManager |
| 16 | { |
| 17 | // the ID of the Room that I'm in |
| 18 | private ulong m_roomID; |
| 19 | |
| 20 | // the other User in the Room |
| 21 | private User m_remoteUser; |
| 22 | |
| 23 | // how often I should poll for invites |
| 24 | private static readonly float INVITE_POLL_FREQ_SECONDS = 5.0f; |
| 25 | |
| 26 | // the next time I should poll Oculus Platform for valid Room Invite requests |
| 27 | private float m_nextPollTime; |
| 28 | |
| 29 | public struct Invite |
| 30 | { |
| 31 | public readonly ulong RoomID; |
| 32 | public readonly string OwnerID; |
| 33 | |
| 34 | public Invite(ulong roomID, string owner) |
| 35 | { |
| 36 | this.RoomID = roomID; |
| 37 | this.OwnerID = owner; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // cached list of rooms that I've been invited to and I'm waiting |
| 42 | // for more information about |
| 43 | private HashSet<ulong> m_pendingRoomRequests; |
| 44 | |
| 45 | // accumulation list of room invites and the room owner |
| 46 | private List<Invite> m_invites; |
| 47 | |
| 48 | public RoomManager() |
| 49 | { |
| 50 | Rooms.SetRoomInviteAcceptedNotificationCallback(LaunchedFromAcceptingInviteCallback); |
| 51 | Rooms.SetUpdateNotificationCallback(RoomUpdateCallback); |
| 52 | } |
| 53 | |
| 54 | public ulong RemoteUserID |
| 55 | { |
| 56 | get |
| 57 | { |
| 58 | return m_remoteUser != null ? m_remoteUser.ID : 0; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | public String RemoteOculusID |
| 63 | { |
| 64 | get |
| 65 | { |
| 66 | return m_remoteUser != null ? m_remoteUser.OculusID : String.Empty; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | #region Launched Application from Accepting Invite |
| 71 | |
| 72 | // Callback to check whether the User accepted the invite as |
| 73 | // a notification which caused the Application to launch. If so, then |
| 74 | // we know we need to try to join that room. |
| 75 | void LaunchedFromAcceptingInviteCallback(Message<string> msg) |
| 76 | { |
| 77 | if (msg.IsError) |
| 78 | { |
| 79 | PlatformManager.TerminateWithError(msg); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | Debug.Log("Launched Invite to join Room: " + msg.Data); |
| 84 | |
| 85 | m_roomID = Convert.ToUInt64(msg.GetString()); |
| 86 | } |
| 87 | |
| 88 | // Check to see if the App was launched by accepting the Notication from the main Oculus app. |
| 89 | // If so, we can directly join that room. (If it's still available.) |
| 90 | public bool CheckForLaunchInvite() |
| 91 | { |
| 92 | if (m_roomID != 0) |
| 93 | { |
| 94 | JoinExistingRoom(m_roomID); |
| 95 | return true; |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | return false; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | #endregion |
| 104 | |
| 105 | #region Create a Room and Invite Friend(s) from the Oculus Universal Menu |
| 106 | |
| 107 | public void CreateRoomAndLaunchInviteMenu() |
| 108 | { |
| 109 | Rooms.CreateAndJoinPrivate(RoomJoinPolicy.InvitedUsers, 2, true) |
| 110 | .OnComplete(CreateAndJoinPrivateRoomCallback); |
| 111 | } |
| 112 | |
| 113 | void CreateAndJoinPrivateRoomCallback(Message<Room> msg) |
| 114 | { |
| 115 | if (msg.IsError) |
| 116 | { |
| 117 | PlatformManager.TerminateWithError(msg); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | m_roomID = msg.Data.ID; |
| 122 | m_remoteUser = null; |
| 123 | PlatformManager.TransitionToState(PlatformManager.State.WAITING_FOR_ANSWER); |
| 124 | |
| 125 | // launch the Room Invite workflow in the Oculus Univeral Menu |
| 126 | Rooms.LaunchInvitableUserFlow(m_roomID).OnComplete(OnLaunchInviteWorkflowComplete); |
| 127 | } |
| 128 | |
| 129 | void OnLaunchInviteWorkflowComplete(Message msg) |
| 130 | { |
| 131 | if (msg.IsError) |
| 132 | { |
| 133 | PlatformManager.TerminateWithError(msg); |
| 134 | return; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | #endregion |
| 139 | |
| 140 | #region Polling for Invites |
| 141 | |
| 142 | public bool ShouldPollInviteList |
| 143 | { |
| 144 | get |
| 145 | { |
| 146 | return m_pendingRoomRequests == null && Time.time >= m_nextPollTime; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | public void UpdateActiveInvitesList() |
| 151 | { |
| 152 | m_nextPollTime = Time.time + INVITE_POLL_FREQ_SECONDS; |
| 153 | m_pendingRoomRequests = new HashSet<ulong>(); |
| 154 | m_invites = new List<Invite>(); |
| 155 | Notifications.GetRoomInviteNotifications().OnComplete(GetRoomInviteNotificationsCallback); |
| 156 | } |
| 157 | |
| 158 | // task 13572454: add the type to callback definition |
| 159 | void GetRoomInviteNotificationsCallback(Message msg_untyped) |
| 160 | { |
| 161 | Message<RoomInviteNotificationList> msg = (Message<RoomInviteNotificationList>)msg_untyped; |
| 162 | |
| 163 | if (msg.IsError) |
| 164 | { |
| 165 | PlatformManager.TerminateWithError(msg); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | // loop over all the rooms we're invited to and request more info |
| 170 | foreach (RoomInviteNotification invite in msg.Data) |
| 171 | { |
| 172 | m_pendingRoomRequests.Add(invite.RoomID); |
| 173 | Rooms.Get(invite.RoomID).OnComplete(GetRoomInfoCallback); |
| 174 | } |
| 175 | |
| 176 | if (msg.Data.Count == 0) |
| 177 | { |
| 178 | m_pendingRoomRequests = null; |
| 179 | PlatformManager.SetActiveInvites(m_invites); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | void GetRoomInfoCallback(Message<Room> msg) |
| 184 | { |
| 185 | if (msg.IsError) |
| 186 | { |
| 187 | PlatformManager.TerminateWithError(msg); |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | if (msg.Data.OwnerOptional != null) |
| 192 | { |
| 193 | Invite invite = new Invite(msg.Data.ID, msg.Data.OwnerOptional.OculusID); |
| 194 | m_pendingRoomRequests.Remove(invite.RoomID); |
| 195 | |
| 196 | // make sure the room still looks usable |
| 197 | // (e.g. they aren't currently talking to someone) |
| 198 | if (msg.Data.UsersOptional != null && msg.Data.UsersOptional.Count == 1) |
| 199 | { |
| 200 | m_invites.Add(invite); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // once we're received all the room info, let the platform update |
| 205 | // its display |
| 206 | if (m_pendingRoomRequests.Count == 0) |
| 207 | { |
| 208 | m_pendingRoomRequests = null; |
| 209 | PlatformManager.SetActiveInvites(m_invites); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | #endregion |
| 214 | |
| 215 | #region Accept Invite |
| 216 | |
| 217 | public void JoinExistingRoom(ulong roomID) |
| 218 | { |
| 219 | Rooms.Join(roomID, true).OnComplete(JoinRoomCallback); |
| 220 | } |
| 221 | |
| 222 | void JoinRoomCallback(Message<Room> msg) |
| 223 | { |
| 224 | if (msg.IsError) |
| 225 | { |
| 226 | // is reasonable if caller called more than 1 person, and I didn't answer first |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | string oculusOwnerID = msg.Data.OwnerOptional != null ? msg.Data.OwnerOptional.OculusID : ""; |
| 231 | int numUsers = msg.Data.UsersOptional != null ? msg.Data.UsersOptional.Count : 0; |
| 232 | |
| 233 | Debug.LogFormat("Joined room: {0} owner: {1} count: ", |
| 234 | msg.Data.ID, oculusOwnerID, numUsers); |
| 235 | |
| 236 | m_roomID = msg.Data.ID; |
| 237 | |
| 238 | // if the caller left while I was in the process of joining, just hangup |
| 239 | if (msg.Data.UsersOptional == null || msg.Data.UsersOptional.Count != 2) |
| 240 | { |
| 241 | PlatformManager.TransitionToState(PlatformManager.State.HANGUP); |
| 242 | } |
| 243 | else |
| 244 | { |
| 245 | foreach (User user in msg.Data.UsersOptional) |
| 246 | { |
| 247 | if (user.ID != PlatformManager.MyID) |
| 248 | { |
| 249 | m_remoteUser = user; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | PlatformManager.TransitionToState(PlatformManager.State.CONNECTED_IN_A_ROOM); |
| 254 | } |
| 255 | |
| 256 | // update the invite list sooner |
| 257 | m_nextPollTime = Time.time; |
| 258 | } |
| 259 | |
| 260 | #endregion |
| 261 | |
| 262 | #region Room Updates |
| 263 | |
| 264 | void RoomUpdateCallback(Message<Room> msg) |
| 265 | { |
| 266 | if (msg.IsError) |
| 267 | { |
| 268 | PlatformManager.TerminateWithError(msg); |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | string oculusOwnerID = msg.Data.OwnerOptional != null ? msg.Data.OwnerOptional.OculusID : ""; |
| 273 | int numUsers = msg.Data.UsersOptional != null ? msg.Data.UsersOptional.Count : 0; |
| 274 | |
| 275 | Debug.LogFormat("Room {0} Update: {1} owner: {2} count: ", |
| 276 | msg.Data.ID, oculusOwnerID, numUsers); |
| 277 | |
| 278 | // if the Room count is not 2 then the other party has left. |
| 279 | // We'll just hangup the connection here. |
| 280 | // If the other User created then room, ownership would switch to me. |
| 281 | if (msg.Data.UsersOptional == null || msg.Data.UsersOptional.Count != 2) |
| 282 | { |
| 283 | PlatformManager.TransitionToState(PlatformManager.State.HANGUP); |
| 284 | } |
| 285 | else |
| 286 | { |
| 287 | foreach (User user in msg.Data.UsersOptional) |
| 288 | { |
| 289 | if (user.ID != PlatformManager.MyID) |
| 290 | { |
| 291 | m_remoteUser = user; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | PlatformManager.TransitionToState(PlatformManager.State.CONNECTED_IN_A_ROOM); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | #endregion |
| 300 | |
| 301 | #region Room Exit |
| 302 | |
| 303 | public void LeaveCurrentRoom() |
| 304 | { |
| 305 | if (m_roomID != 0) |
| 306 | { |
| 307 | Rooms.Leave(m_roomID); |
| 308 | m_roomID = 0; |
| 309 | m_remoteUser = null; |
| 310 | } |
| 311 | PlatformManager.TransitionToState(PlatformManager.State.WAITING_TO_CALL_OR_ANSWER); |
| 312 | } |
| 313 | |
| 314 | #endregion |
| 315 | } |
| 316 | } |