Skip to main content
Version: 2.x

Class OdinRoom

Wrapper class of OdinNative.Wrapper.Room.Room for Unity.

This convenient class provides dispatching of events to Unity with passthrough UnityEngine.Events.UnityEvent as well as predefined helper functions to cover default use cases where the voice chat is visually and logically represented by Unity gameobjects that are manageable with the Unity editor like Context menu, Inspector and/or Hierarchy window.

Default Unity GameObject altering event callback functions:

  • OdinNative.Unity.OdinRoom.PeerJoinedCreateComponent(System.Object,OdinNative.Wrapper.Room.PeerJoinedEventArgs) Creates GameObject with OdinNative.Unity.OdinPeer component
  • OdinNative.Unity.OdinRoom.PeerLeftRemoveComponent(System.Object,OdinNative.Wrapper.Room.PeerLeftEventArgs) Destroy GameObject with OdinNative.Unity.OdinPeer component
  • OdinNative.Unity.OdinRoom.RoomStatusState(System.Object,OdinNative.Wrapper.Room.RoomStateChangedEventArgs) Destroy this components GameObject if the connection is closed i.e cleanup
Declaration
[HelpURL("https://docs.4players.io/voice/unity/next/api/OdinNative.Unity/OdinRoom/")]
[AddComponentMenu("Odin/Instance/OdinRoom")]
[DefaultExecutionOrder(-99)]
public class OdinRoom : MonoBehaviour, IRoom

Inheritance: System.Object -> UnityEngine.Object -> UnityEngine.Component -> UnityEngine.Behaviour -> UnityEngine.MonoBehaviour

Implements:
OdinNative.Wrapper.IRoom

Properties

Samplerate

Unity samplerate

Declaration
public uint Samplerate { get; set; }

OutputSampleRate

Playback samplerate reported by Unity, or OdinNative.Unity.OdinRoom.DefaultSampleRate when the Unity audio engine is disabled.

Declaration
public static uint OutputSampleRate { get; }

IsStereo

Unity channel flag

Declaration
public bool IsStereo { get; set; }

Id

Odin room id

Declaration
public ulong Id { get; }

CryptoCipher

Encryption handle container

Declaration
public Crypto CryptoCipher { get; }

Parent

Default value gameObject parent or Unity root

Declaration
public object Parent { get; }

IsJoined

Indicates whether the room is currently joined

Declaration
public bool IsJoined { get; }

Self

The local peer for this client in this room. Null until the room is joined.

Declaration
public IPeer Self { get; }

Fields

Gateway

Odin endpoint server

Declaration
public string Gateway

Token

Odin room token

Declaration
public string Token

InitialUserData

User data of the local peer that is sent together with the join request

Declaration
[Tooltip("User data sent with the join request, e.g. a json string. Change it after joining with Room.UpdateUserData.")]
public string InitialUserData

AudioMixerGroup

Unity mixer

Declaration
public AudioMixerGroup AudioMixerGroup

DefaultSampleRate

Fallback samplerate for setups that run without the Unity audio engine.

Declaration
public const uint DefaultSampleRate = 48000

PeerPrefab

Prefab instantiated per joining peer. Falls back to a blank GameObject when null.

Declaration
public GameObject PeerPrefab

CryptoComponent

Odin Crypto cipher component

Declaration
[Tooltip("Odin room encryption. Provide each OdinRoom its own OdinCrypto instance!")]
public OdinCrypto CryptoComponent

AutoCreateMedia

Automatically create default medias: an input encoder when audio is pushed via OdinNative.Unity.OdinRoom.ProxyAudio(System.Single[],System.Int32,System.Boolean) and a decoder when audio arrives from a peer that has no decoder with a matching listen channel mask.

Declaration
[Tooltip("Automatically create a default input encoder on pushed audio and a default decoder when audio arrives from a peer without a matching decoder. Disable to manage medias manually, e.g. one decoder per channel mask.")]
public bool AutoCreateMedia

AutoDecoderMediaId

Media id used for default medias created by OdinNative.Unity.OdinRoom.AutoCreateMedia

Declaration
public const ulong AutoDecoderMediaId = 18446744073709551615

OnRoomJoined

Event OdinNative.Wrapper.Room.Room.OnRoomJoined redirected as Unity event

Declaration
[Header("Events")]
public RoomJoinedProxy OnRoomJoined

OnDecoderAdded

Event raised when a decoder is added for a peer, redirected as Unity event

Declaration
[FormerlySerializedAs("OnMediaAdded")]
public DecoderAddedProxy OnDecoderAdded

OnDecoderRemoved

Event raised when a decoder is removed for a peer, redirected as Unity event

Declaration
[FormerlySerializedAs("OnMediaRemoved")]
public DecoderRemovedProxy OnDecoderRemoved

OnPeerJoined

Event OdinNative.Wrapper.Room.Room.OnPeerJoined redirected as Unity event

Declaration
public PeerJoinedProxy OnPeerJoined

OnPeerLeft

Event OdinNative.Wrapper.Room.Room.OnPeerLeft redirected as Unity event

Declaration
public PeerLeftProxy OnPeerLeft

OnMessageReceived

Event OdinNative.Wrapper.Room.Room.OnMessageReceived redirected as Unity event

Declaration
public MessageReceivedProxy OnMessageReceived

OnRoomStateChanged

Event OdinNative.Wrapper.Room.Room.OnRoomStatusChanged redirected as Unity event

Declaration
[FormerlySerializedAs("OnConnectionStateChanged")]
public RoomStateChangedProxy OnRoomStateChanged

Methods

GetBaseRoom<T>()

Odin base room

Declaration
public T GetBaseRoom<T>() where T : IRoom
Returns

<T>: wrapper room object

Type Parameters
  • T

SendAudio(float[], MediaEncoder)

Push audio samples to the room via a specific encoder.

Declaration
public virtual bool SendAudio(float[] samples, MediaEncoder encoder)
Returns

System.Boolean

Parameters
TypeName
System.Single[]samples
OdinNative.Wrapper.MediaEncoderencoder

SendAudio(float[], MediaEncoder, bool)

Push audio samples to the room via a specific encoder with a silence flag.

Declaration
public virtual bool SendAudio(float[] samples, MediaEncoder encoder, bool isSilent)
Returns

System.Boolean

Parameters
TypeName
System.Single[]samples
OdinNative.Wrapper.MediaEncoderencoder
System.BooleanisSilent

Room_OnAutoCreateDecoder(object, DatagramEventArgs)

Creates a default decoder for a peer on incoming audio if the peer has no decoder with a matching listen channel mask.

Declaration
protected virtual void Room_OnAutoCreateDecoder(object sender, DatagramEventArgs args)
Parameters
TypeNameDescription
System.ObjectsenderRoom object
OdinNative.Wrapper.Room.DatagramEventArgsargsdatagram data

Room_OnDatagram(object, DatagramEventArgs)

Declaration
[Obsolete("Not subscribed by the component; re-raises the wrapper event and would recurse if wired to the event it re-raises")]
protected virtual void Room_OnDatagram(object sender, DatagramEventArgs args)
Parameters
TypeName
System.Objectsender
OdinNative.Wrapper.Room.DatagramEventArgsargs

Room_OnRpc(object, RpcEventArgs)

Declaration
[Obsolete("Not subscribed by the component; re-raises the wrapper event and would recurse if wired to the event it re-raises")]
protected virtual void Room_OnRpc(object sender, RpcEventArgs args)
Parameters
TypeName
System.Objectsender
OdinNative.Wrapper.Room.RpcEventArgsargs

Room_OnSocket(object, SocketMessageEventArgs)

Declaration
[Obsolete("Not subscribed by the component; re-raises the wrapper event and would recurse if wired to the event it re-raises")]
protected virtual void Room_OnSocket(object sender, SocketMessageEventArgs args)
Parameters
TypeName
System.Objectsender
OdinNative.Wrapper.Socket.SocketMessageEventArgsargs

Room_OnMessageReceived(object, uint, byte[])

Declaration
protected virtual void Room_OnMessageReceived(object sender, uint peerId, byte[] message)
Parameters
TypeName
System.Objectsender
System.UInt32peerId
System.Byte[]message

ExtraOnNativeDecoderCreated(object, uint, ulong)

Called by OdinNative.Wrapper.Room.Room when the native SDK creates a decoder for a peer

Declaration
public virtual void ExtraOnNativeDecoderCreated(object sender, uint peerId, ulong mediaId)
Parameters
TypeNameDescription
System.ObjectsenderRoom object
System.UInt32peerIdpeer that started streaming
System.UInt64mediaIdmedia id of the created decoder

DecoderAddedPeerCreateComponent(object, DecoderAddedEventArgs)

Dispatches OdinNative.Unity.OdinRoom.OnDecoderAdded to the matching OdinNative.Unity.OdinPeer

Declaration
public virtual void DecoderAddedPeerCreateComponent(object sender, DecoderAddedEventArgs args)
Parameters
TypeNameDescription
System.ObjectsenderOdinRoom object
OdinNative.Unity.Events.DecoderAddedEventArgsargsdecoder data

ExtraOnNativeDecoderRemoved(object, uint, ulong)

Called by OdinNative.Wrapper.Room.Room when the native SDK removes a decoder for a peer

Declaration
public virtual void ExtraOnNativeDecoderRemoved(object sender, uint peerId, ulong mediaId)
Parameters
TypeNameDescription
System.ObjectsenderRoom object
System.UInt32peerIdpeer that stopped streaming
System.UInt64mediaIdmedia id of the removed decoder

DecoderRemovedPeerRemoveComponent(object, DecoderRemovedEventArgs)

Dispatches OdinNative.Unity.OdinRoom.OnDecoderRemoved to the matching OdinNative.Unity.OdinPeer

Declaration
public virtual void DecoderRemovedPeerRemoveComponent(object sender, DecoderRemovedEventArgs args)
Parameters
TypeNameDescription
System.ObjectsenderOdinRoom object
OdinNative.Unity.Events.DecoderRemovedEventArgsargsdecoder data

Room_OnRoomJoined(object, JoinedObjectContainer)

Declaration
protected virtual void Room_OnRoomJoined(object sender, JoinedObjectContainer args)
Parameters
TypeName
System.Objectsender
OdinNative.Wrapper.Room.Rpc.JoinedObjectContainerargs

Room_OnPeerLeft(object, PeerLeftObjectContainer)

Declaration
protected virtual void Room_OnPeerLeft(object sender, PeerLeftObjectContainer args)
Parameters
TypeName
System.Objectsender
OdinNative.Wrapper.Peer.Rpc.PeerLeftObjectContainerargs

PeerLeftRemoveComponent(object, PeerLeftEventArgs)

Removes all child components with the same peer id

Declaration
public void PeerLeftRemoveComponent(object sender, PeerLeftEventArgs args)
Parameters
TypeNameDescription
System.ObjectsenderOdinRoom object
OdinNative.Wrapper.Room.PeerLeftEventArgsargsleft peer data

RemovePeerComponent(GameObject)

Remove a OdinNative.Unity.OdinPeer from a gameobject

Declaration
public void RemovePeerComponent(GameObject containerObject)
Parameters
TypeName
UnityEngine.GameObjectcontainerObject

Room_OnPeerJoined(object, PeerJoinedObjectContainer)

Declaration
protected virtual void Room_OnPeerJoined(object sender, PeerJoinedObjectContainer args)
Parameters
TypeName
System.Objectsender
OdinNative.Wrapper.Peer.Rpc.PeerJoinedObjectContainerargs

PeerJoinedCreateComponent(object, PeerJoinedEventArgs)

Add a new GameObject with a new OdinNative.Unity.OdinPeer component. Uses OdinNative.Unity.OdinRoom.PeerPrefab if set.

Declaration
public void PeerJoinedCreateComponent(object sender, PeerJoinedEventArgs args)
Parameters
TypeNameDescription
System.ObjectsenderOdinRoom object
OdinNative.Wrapper.Room.PeerJoinedEventArgsargspeer join data

AddPeerComponent(GameObject, uint, bool)

Add OdinNative.Unity.OdinPeer to a gameobject, detecting an existing component on a prefab

Declaration
public OdinPeer AddPeerComponent(GameObject containerObject, uint peerId, bool enable = true)
Returns

OdinNative.Unity.OdinPeer: created component

Parameters
TypeNameDescription
UnityEngine.GameObjectcontainerObjectgameobject where the component will be added
System.UInt32peerIdid of OdinNative.Wrapper.PeerEntity
System.Booleanenableflag if the new OdinNative.Unity.OdinPeer component is enabled

Room_OnConnectionStatusChanged(object, string)

Declaration
protected virtual void Room_OnConnectionStatusChanged(object sender, string connectionStatus)
Parameters
TypeName
System.Objectsender
System.StringconnectionStatus

RoomStatusState(object, RoomStateChangedEventArgs)

Check status if the room should destroy the gameobject

Declaration
public virtual void RoomStatusState(object sender, RoomStateChangedEventArgs status)
Parameters
TypeNameDescription
System.ObjectsenderOdinRoom object
OdinNative.Wrapper.Room.RoomStateChangedEventArgsstatusnew status

MediaAddedPeerCreateComponent(object, DecoderAddedEventArgs)

Forward for serialized UnityEvents of previous versions

Declaration
[Obsolete("Renamed to DecoderAddedPeerCreateComponent; kept so serialized UnityEvents from previous versions keep working")]
public void MediaAddedPeerCreateComponent(object sender, DecoderAddedEventArgs args)
Parameters
TypeName
System.Objectsender
OdinNative.Unity.Events.DecoderAddedEventArgsargs

MediaRemovedPeerRemoveComponent(object, DecoderRemovedEventArgs)

Forward for serialized UnityEvents of previous versions

Declaration
[Obsolete("Renamed to DecoderRemovedPeerRemoveComponent; kept so serialized UnityEvents from previous versions keep working")]
public void MediaRemovedPeerRemoveComponent(object sender, DecoderRemovedEventArgs args)
Parameters
TypeName
System.Objectsender
OdinNative.Unity.Events.DecoderRemovedEventArgsargs

ConnectionStatusState(object, RoomStateChangedEventArgs)

Forward for serialized UnityEvents of previous versions

Declaration
[Obsolete("Renamed to RoomStatusState; kept so serialized UnityEvents from previous versions keep working")]
public void ConnectionStatusState(object sender, RoomStateChangedEventArgs status)
Parameters
TypeName
System.Objectsender
OdinNative.Wrapper.Room.RoomStateChangedEventArgsstatus

Join(string)

Room join

Declaration
public bool Join(string token)
Returns

System.Boolean: result of Join or false

Parameters
TypeName
System.Stringtoken

Join(string, OdinCipherHandle)

Room join with optional encryption

Declaration
public bool Join(string token, OdinCipherHandle cipher)
Returns

System.Boolean: result of Join or false

Parameters
TypeNameDescription
System.Stringtoken
OdinNative.Core.Imports.OdinCipherHandleciphercrypto cipher

ProxyAudio(float[], int, bool)

Redirects audio to all media encoders in the corresponding room.

Declaration
public virtual void ProxyAudio(float[] buffer, int position, bool isSilent = false)
Parameters
TypeName
System.Single[]buffer
System.Int32position
System.BooleanisSilent

LinkInputMedia(uint, bool, out MediaEncoder)

Add a input media encoder to the corresponding room.

Declaration
public virtual bool LinkInputMedia(uint samplerate, bool stereo, out MediaEncoder encoder)
Returns

System.Boolean: true on start or false

Parameters
TypeNameDescription
System.UInt32samplerateencoder samplerate
System.Booleanstereoencoder channel flag
OdinNative.Wrapper.MediaEncoderencoderstarted encoder or null

UnlinkInputMedia(MediaEncoder)

Remove a input media encoder from the corresponding room.

Declaration
public virtual bool UnlinkInputMedia(MediaEncoder encoder)
Returns

System.Boolean: true on stop or false

Parameters
TypeNameDescription
OdinNative.Wrapper.MediaEncoderencoderinput media

RegisterDecoder(OdinDecoder, uint)

Register an OdinNative.Unity.OdinDecoder with the scene-wide decoder registry for the given peer. Called automatically from OdinNative.Unity.OdinDecoder.OnEnable .

Declaration
public void RegisterDecoder(OdinDecoder decoder, uint peerId)
Parameters
TypeNameDescription
OdinNative.Unity.OdinDecoderdecoderdecoder to register
System.UInt32peerIdpeer this decoder belongs to

UnregisterDecoder(OdinDecoder)

Unregister an OdinNative.Unity.OdinDecoder from the scene-wide decoder registry. Called automatically from OdinNative.Unity.OdinDecoder.OnDestroy .

Declaration
public void UnregisterDecoder(OdinDecoder decoder)
Parameters
TypeNameDescription
OdinNative.Unity.OdinDecoderdecoderdecoder to unregister

DestroyDecodersForPeer(uint)

Destroy all OdinNative.Unity.OdinDecoder instances registered for the given peer, regardless of where they live in the scene hierarchy.

Declaration
public void DestroyDecodersForPeer(uint peerId)
Parameters
TypeNameDescription
System.UInt32peerIdpeer whose decoders should be destroyed

CreateMediaDecoder(uint)

Manually create a decoder for a peer and notify OdinNative.Unity.OdinRoom.OnDecoderAdded listeners.

Declaration
public MediaDecoder CreateMediaDecoder(uint peerId)
Returns

OdinNative.Wrapper.MediaDecoder: new OdinNative.Wrapper.MediaDecoder or null

Parameters
TypeNameDescription
System.UInt32peerIdpeer to create the decoder for

RemoveMediaDecoder(uint, ulong)

Manually remove a decoder of a peer and notify OdinNative.Unity.OdinRoom.OnDecoderRemoved listeners.

Declaration
public void RemoveMediaDecoder(uint peerId, ulong mediaId)
Parameters
TypeNameDescription
System.UInt32peerIdpeer the decoder belongs to
System.UInt64mediaIdmedia id of the removed decoder

GenerateTestToken(string, string, double, string)

Generate a test token from a test key

Declaration
public static string GenerateTestToken(string roomId, string userId, double lifetimeMinutes = 5, string testKey = "")
Returns

System.String: Token or empty

Parameters
TypeNameDescription
System.StringroomIdRoom name
System.StringuserIdUser name
System.DoublelifetimeMinutestoken valid timeframe
System.StringtestKeyoptional test accesskey

WebRequestToken(string, string)

Uses UnityWebRequest with POST data as json to get a response from a token-server

Declaration
public IEnumerator WebRequestToken(string url, string jsonPayload)
Returns

System.Collections.IEnumerator: IEnumerator for Coroutine

Parameters
TypeNameDescription
System.StringurlToken-Server endpoint
System.StringjsonPayloadRequest data

WebRequestToken(string, string, UnityAction<DownloadHandler>)

Uses UnityWebRequest with POST data as json to get a response from a token-server

Declaration
public IEnumerator WebRequestToken(string url, string jsonPayload, UnityAction<DownloadHandler> response)
Returns

System.Collections.IEnumerator: IEnumerator for Coroutine

Parameters
TypeNameDescription
System.StringurlToken-Server endpoint
System.StringjsonPayloadRequest data
UnityEngine.Events.UnityAction<UnityEngine.Networking.DownloadHandler>responseResponse callback

Implements