| 1 | namespace Oculus.Platform.Samples.VrVoiceChat |
| 2 | { |
| 3 | using UnityEngine; |
| 4 | using System; |
| 5 | using Oculus.Platform; |
| 6 | using Oculus.Platform.Models; |
| 7 | |
| 8 | // Helper class to manage a Peer-to-Peer connection to the other user. |
| 9 | // The connection is used to send and received the Transforms for the |
| 10 | // Avatars. The Transforms are sent via unreliable UDP at a fixed |
| 11 | // frequency. |
| 12 | public class P2PManager |
| 13 | { |
| 14 | // number of seconds to delay between transform updates |
| 15 | private static readonly float UPDATE_DELAY = 0.1f; |
| 16 | |
| 17 | // the ID of the remote player we expect to be connected to |
| 18 | private ulong m_remoteID; |
| 19 | |
| 20 | // the result of the last connection state update message |
| 21 | private PeerConnectionState m_state = PeerConnectionState.Unknown; |
| 22 | |
| 23 | // the next time to send an updated transform to the remote User |
| 24 | private float m_timeForNextUpdate; |
| 25 | |
| 26 | // the size of the packet we are sending and receiving |
| 27 | private static readonly byte PACKET_SIZE = 29; |
| 28 | |
| 29 | // packet format type just in case we want to add new future packet types |
| 30 | private static readonly byte PACKET_FORMAT = 0; |
| 31 | |
| 32 | // reusable buffer to serialize the Transform into |
| 33 | private readonly byte[] sendTransformBuffer = new byte[PACKET_SIZE]; |
| 34 | |
| 35 | // reusable buffer to deserialize the Transform into |
| 36 | private readonly byte[] receiveTransformBuffer = new byte[PACKET_SIZE]; |
| 37 | |
| 38 | // the last received position update |
| 39 | private Vector3 receivedPosition; |
| 40 | |
| 41 | // the previous received position to interpolate from |
| 42 | private Vector3 receivedPositionPrior; |
| 43 | |
| 44 | // the last received rotation update |
| 45 | private Quaternion receivedRotation; |
| 46 | |
| 47 | // the previous received rotation to interpolate from |
| 48 | private Quaternion receivedRotationPrior; |
| 49 | |
| 50 | // when the last transform was received |
| 51 | private float receivedTime; |
| 52 | |
| 53 | public P2PManager(Transform initialHeadTransform) |
| 54 | { |
| 55 | receivedPositionPrior = receivedPosition = initialHeadTransform.localPosition; |
| 56 | receivedRotationPrior = receivedRotation = initialHeadTransform.localRotation; |
| 57 | |
| 58 | Net.SetPeerConnectRequestCallback(PeerConnectRequestCallback); |
| 59 | Net.SetConnectionStateChangedCallback(ConnectionStateChangedCallback); |
| 60 | } |
| 61 | |
| 62 | #region Connection Management |
| 63 | |
| 64 | public void ConnectTo(ulong userID) |
| 65 | { |
| 66 | m_remoteID = userID; |
| 67 | |
| 68 | // ID comparison is used to decide who calls Connect and who calls Accept |
| 69 | if (PlatformManager.MyID < userID) |
| 70 | { |
| 71 | Net.Connect(userID); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | public void Disconnect() |
| 76 | { |
| 77 | if (m_remoteID != 0) |
| 78 | { |
| 79 | Net.Close(m_remoteID); |
| 80 | m_remoteID = 0; |
| 81 | m_state = PeerConnectionState.Unknown; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | public bool Connected |
| 86 | { |
| 87 | get |
| 88 | { |
| 89 | return m_state == PeerConnectionState.Connected; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void PeerConnectRequestCallback(Message<NetworkingPeer> msg) |
| 94 | { |
| 95 | Debug.LogFormat("Connection request from {0}, authorized is {1}", msg.Data.ID, m_remoteID); |
| 96 | |
| 97 | if (msg.Data.ID == m_remoteID) |
| 98 | { |
| 99 | Net.Accept(msg.Data.ID); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void ConnectionStateChangedCallback(Message<NetworkingPeer> msg) |
| 104 | { |
| 105 | Debug.LogFormat("Connection state to {0} changed to {1}", msg.Data.ID, msg.Data.State); |
| 106 | |
| 107 | if (msg.Data.ID == m_remoteID) |
| 108 | { |
| 109 | m_state = msg.Data.State; |
| 110 | |
| 111 | if (m_state == PeerConnectionState.Timeout && |
| 112 | // ID comparison is used to decide who calls Connect and who calls Accept |
| 113 | PlatformManager.MyID < m_remoteID) |
| 114 | { |
| 115 | // keep trying until hangup! |
| 116 | Net.Connect(m_remoteID); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | PlatformManager.SetBackgroundColorForState(); |
| 121 | } |
| 122 | |
| 123 | #endregion |
| 124 | |
| 125 | #region Send Update |
| 126 | |
| 127 | public bool ShouldSendHeadUpdate |
| 128 | { |
| 129 | get |
| 130 | { |
| 131 | return Time.time >= m_timeForNextUpdate && m_state == PeerConnectionState.Connected; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | public void SendHeadTransform(Transform headTransform) |
| 136 | { |
| 137 | m_timeForNextUpdate = Time.time + UPDATE_DELAY; |
| 138 | |
| 139 | sendTransformBuffer[0] = PACKET_FORMAT; |
| 140 | int offset = 1; |
| 141 | |
| 142 | PackFloat(headTransform.localPosition.x, sendTransformBuffer, ref offset); |
| 143 | PackFloat(headTransform.localPosition.y, sendTransformBuffer, ref offset); |
| 144 | PackFloat(headTransform.localPosition.z, sendTransformBuffer, ref offset); |
| 145 | PackFloat(headTransform.localRotation.x, sendTransformBuffer, ref offset); |
| 146 | PackFloat(headTransform.localRotation.y, sendTransformBuffer, ref offset); |
| 147 | PackFloat(headTransform.localRotation.z, sendTransformBuffer, ref offset); |
| 148 | PackFloat(headTransform.localRotation.w, sendTransformBuffer, ref offset); |
| 149 | |
| 150 | Net.SendPacket(m_remoteID, sendTransformBuffer, SendPolicy.Unreliable); |
| 151 | } |
| 152 | |
| 153 | void PackFloat(float f, byte[] buf, ref int offset) |
| 154 | { |
| 155 | Buffer.BlockCopy(BitConverter.GetBytes(f), 0, buf, offset, 4); |
| 156 | offset = offset + 4; |
| 157 | } |
| 158 | |
| 159 | #endregion |
| 160 | |
| 161 | #region Receive Update |
| 162 | |
| 163 | public void GetRemoteHeadTransform(Transform headTransform) |
| 164 | { |
| 165 | bool hasNewTransform = false; |
| 166 | |
| 167 | Packet packet; |
| 168 | while ((packet = Net.ReadPacket()) != null) |
| 169 | { |
| 170 | if (packet.Size != PACKET_SIZE) |
| 171 | { |
| 172 | Debug.Log("Invalid packet size: " + packet.Size); |
| 173 | continue; |
| 174 | } |
| 175 | |
| 176 | packet.ReadBytes(receiveTransformBuffer); |
| 177 | |
| 178 | if (receiveTransformBuffer[0] != PACKET_FORMAT) |
| 179 | { |
| 180 | Debug.Log("Invalid packet type: " + packet.Size); |
| 181 | continue; |
| 182 | } |
| 183 | hasNewTransform = true; |
| 184 | } |
| 185 | |
| 186 | if (hasNewTransform) |
| 187 | { |
| 188 | receivedPositionPrior = receivedPosition; |
| 189 | receivedPosition.x = BitConverter.ToSingle(receiveTransformBuffer, 1); |
| 190 | receivedPosition.y = BitConverter.ToSingle(receiveTransformBuffer, 5); |
| 191 | receivedPosition.z = BitConverter.ToSingle(receiveTransformBuffer, 9); |
| 192 | |
| 193 | receivedRotationPrior = receivedRotation; |
| 194 | receivedRotation.x = BitConverter.ToSingle(receiveTransformBuffer, 13); |
| 195 | receivedRotation.y = BitConverter.ToSingle(receiveTransformBuffer, 17) * -1.0f; |
| 196 | receivedRotation.z = BitConverter.ToSingle(receiveTransformBuffer, 21); |
| 197 | receivedRotation.w = BitConverter.ToSingle(receiveTransformBuffer, 25) * -1.0f; |
| 198 | |
| 199 | receivedTime = Time.time; |
| 200 | } |
| 201 | |
| 202 | // since we're receiving updates at a slower rate than we render, |
| 203 | // interpolate to make the motion look smoother |
| 204 | float completed = Math.Min(Time.time - receivedTime, UPDATE_DELAY) / UPDATE_DELAY; |
| 205 | headTransform.localPosition = |
| 206 | Vector3.Lerp(receivedPositionPrior, receivedPosition, completed); |
| 207 | headTransform.localRotation = |
| 208 | Quaternion.Slerp(receivedRotationPrior, receivedRotation, completed); |
| 209 | } |
| 210 | |
| 211 | #endregion |
| 212 | } |
| 213 | } |