Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.2 KiB

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