Index: webrtc/examples/unityplugin/README |
diff --git a/webrtc/examples/unityplugin/README b/webrtc/examples/unityplugin/README |
index eade9ef5fac15090e98ce532ac361126ad0c22e0..82a466ac0e2613db087720c005ad1471bfb02d1c 100644 |
--- a/webrtc/examples/unityplugin/README |
+++ b/webrtc/examples/unityplugin/README |
@@ -5,200 +5,302 @@ This plugin dll can also be used by Windows C# applications other than Unity. |
An example of wrapping native plugin into a C# managed class in Unity is given as following: |
using System; |
+using System.Collections.Generic; |
using System.Runtime.InteropServices; |
namespace SimplePeerConnectionM { |
- // This is a managed wrap up class for the native c style peer connection APIs. |
+ // A class for ice candidate. |
+ public class IceCandidate { |
+ public IceCandidate(string candidate, int sdpMlineIndex, string sdpMid) { |
+ mCandidate = candidate; |
+ mSdpMlineIndex = sdpMlineIndex; |
+ mSdpMid = sdpMid; |
+ } |
+ string mCandidate; |
+ int mSdpMlineIndex; |
+ string mSdpMid; |
+ |
+ public string Candidate { |
+ get { return mCandidate; } |
+ set { mCandidate = value; } |
+ } |
+ |
+ public int SdpMlineIndex { |
+ get { return mSdpMlineIndex; } |
+ set { mSdpMlineIndex = value; } |
+ } |
+ |
+ public string SdpMid { |
+ get { return mSdpMid; } |
+ set { mSdpMid = value; } |
+ } |
+ } |
+ |
+ // A managed wrapper up class for the native c style peer connection APIs. |
public class PeerConnectionM { |
- //private const string dll_path = "SimplePeerConnection"; |
- private const string dll_path = "webrtc_unity_plugin"; |
+ private const string dllPath = "webrtc_unity_plugin"; |
- [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
- private static extern int CreatePeerConnection(); |
+ //create a peerconnection with turn servers |
+ [DllImport(dllPath, CallingConvention = CallingConvention.Cdecl)] |
+ private static extern int CreatePeerConnection(string[] turnUrls, int noOfUrls, |
+ string username, string credential); |
- [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
- private static extern bool ClosePeerConnection(int peer_connection_id); |
+ [DllImport(dllPath, CallingConvention = CallingConvention.Cdecl)] |
+ private static extern bool ClosePeerConnection(int peerConnectionId); |
- [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
- private static extern bool AddStream(int peer_connection_id, bool audio_only); |
+ [DllImport(dllPath, CallingConvention = CallingConvention.Cdecl)] |
+ private static extern bool AddStream(int peerConnectionId, bool audioOnly); |
- [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
- private static extern bool AddDataChannel(int peer_connection_id); |
+ [DllImport(dllPath, CallingConvention = CallingConvention.Cdecl)] |
+ private static extern bool AddDataChannel(int peerConnectionId); |
- [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
- private static extern bool CreateOffer(int peer_connection_id); |
+ [DllImport(dllPath, CallingConvention = CallingConvention.Cdecl)] |
+ private static extern bool CreateOffer(int peerConnectionId); |
- [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
- private static extern bool CreateAnswer(int peer_connection_id); |
+ [DllImport(dllPath, CallingConvention = CallingConvention.Cdecl)] |
+ private static extern bool CreateAnswer(int peerConnectionId); |
- [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
- private static extern bool SendDataViaDataChannel(int peer_connection_id, string data); |
+ [DllImport(dllPath, CallingConvention = CallingConvention.Cdecl)] |
+ private static extern bool SendDataViaDataChannel(int peerConnectionId, string data); |
- [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
- private static extern bool SetAudioControl(int peer_connection_id, bool is_mute, bool is_record); |
+ [DllImport(dllPath, CallingConvention = CallingConvention.Cdecl)] |
+ private static extern bool SetAudioControl(int peerConnectionId, bool isMute, bool isRecord); |
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
private delegate void LocalDataChannelReadyInternalDelegate(); |
public delegate void LocalDataChannelReadyDelegate(int id); |
- [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
- private static extern bool RegisterOnLocalDataChannelReady(int peer_connection_id, LocalDataChannelReadyInternalDelegate callback); |
+ [DllImport(dllPath, CallingConvention = CallingConvention.Cdecl)] |
+ private static extern bool RegisterOnLocalDataChannelReady( |
+ int peerConnectionId, LocalDataChannelReadyInternalDelegate callback); |
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
private delegate void DataFromDataChannelReadyInternalDelegate(string s); |
public delegate void DataFromDataChannelReadyDelegate(int id, string s); |
- [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
- private static extern bool RegisterOnDataFromDataChannelReady(int peer_connection_id, DataFromDataChannelReadyInternalDelegate callback); |
+ [DllImport(dllPath, CallingConvention = CallingConvention.Cdecl)] |
+ private static extern bool RegisterOnDataFromDataChannelReady( |
+ int peerConnectionId, DataFromDataChannelReadyInternalDelegate callback); |
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
private delegate void FailureMessageInternalDelegate(string msg); |
public delegate void FailureMessageDelegate(int id, string msg); |
- [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
- private static extern bool RegisterOnFailure(int peer_connection_id, FailureMessageInternalDelegate callback); |
+ [DllImport(dllPath, CallingConvention = CallingConvention.Cdecl)] |
+ private static extern bool RegisterOnFailure(int peerConnectionId, |
+ FailureMessageInternalDelegate callback); |
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
- private delegate void AudioBusReadyInternalDelegate(IntPtr data, int bits_per_sample, |
- int sample_rate, int number_of_channels, int number_of_frames); |
- public delegate void AudioBusReadyDelegate(int id, IntPtr data, int bits_per_sample, |
- int sample_rate, int number_of_channels, int number_of_frames); |
- [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
- private static extern bool RegisterOnAudioBusReady(int peer_connection_id, AudioBusReadyInternalDelegate callback); |
- |
+ private delegate void AudioBusReadyInternalDelegate(IntPtr data, int bitsPerSample, |
+ int sampleRate, int numberOfChannels, int numberOfFrames); |
+ public delegate void AudioBusReadyDelegate(int id, IntPtr data, int bitsPerSample, |
+ int sampleRate, int numberOfChannels, int numberOfFrames); |
+ [DllImport(dllPath, CallingConvention = CallingConvention.Cdecl)] |
+ private static extern bool RegisterOnAudioBusReady(int peerConnectionId, |
+ AudioBusReadyInternalDelegate callback); |
+ |
+ // Video callbacks. |
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
- private delegate void LocalSdpReadytoSendInternalDelegate(string s); |
- public delegate void LocalSdpReadytoSendDelegate(int id, string s); |
- [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
- private static extern bool RegisterOnLocalSdpReadytoSend(int peer_connection_id, LocalSdpReadytoSendInternalDelegate callback); |
+ private delegate void I420FrameReadyInternalDelegate( |
+ IntPtr dataY, IntPtr dataU, IntPtr dataV, |
+ int strideY, int strideU, int strideV, |
+ uint width, uint height); |
+ public delegate void I420FrameReadyDelegate(int id, |
+ IntPtr dataY, IntPtr dataU, IntPtr dataV, |
+ int strideY, int strideU, int strideV, |
+ uint width, uint height); |
+ [DllImport(dllPath, CallingConvention = CallingConvention.Cdecl)] |
+ private static extern bool RegisterOnLocalI420FrameReady(int peerConnectionId, |
+ I420FrameReadyInternalDelegate callback); |
+ [DllImport(dllPath, CallingConvention = CallingConvention.Cdecl)] |
+ private static extern bool RegisterOnRemoteI420FrameReady(int peerConnectionId, |
+ I420FrameReadyInternalDelegate callback); |
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
- private delegate void IceCandiateReadytoSendInternalDelegate(string s); |
- public delegate void IceCandiateReadytoSendDelegate(int id, string s); |
- [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
- private static extern bool RegisterOnIceCandiateReadytoSend(int peer_connection_id, IceCandiateReadytoSendInternalDelegate callback); |
- |
- [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
- private static extern int ReceivedSdp(int peer_connection_id, string sdp); |
+ private delegate void LocalSdpReadytoSendInternalDelegate(string type, string sdp); |
+ public delegate void LocalSdpReadytoSendDelegate(int id, string type, string sdp); |
+ [DllImport(dllPath, CallingConvention = CallingConvention.Cdecl)] |
+ private static extern bool RegisterOnLocalSdpReadytoSend(int peerConnectionId, |
+ LocalSdpReadytoSendInternalDelegate callback); |
- [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
- private static extern bool ReceivedIceCandidate(int peer_connection_id, string ice_candidate); |
- |
- public void CreatePeerConnectionM() { |
- peer_connection_id_ = CreatePeerConnection(); |
+ [UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
+ private delegate void IceCandiateReadytoSendInternalDelegate( |
+ string candidate, int sdpMlineIndex, string sdpMid); |
+ public delegate void IceCandiateReadytoSendDelegate( |
+ int id, string candidate, int sdpMlineIndex, string sdpMid); |
+ [DllImport(dllPath, CallingConvention = CallingConvention.Cdecl)] |
+ private static extern bool RegisterOnIceCandiateReadytoSend( |
+ int peerConnectionId, IceCandiateReadytoSendInternalDelegate callback); |
+ |
+ [DllImport(dllPath, CallingConvention = CallingConvention.Cdecl)] |
+ private static extern bool SetRemoteDescription(int peerConnectionId, string type, string sdp); |
+ |
+ [DllImport(dllPath, CallingConvention = CallingConvention.Cdecl)] |
+ private static extern bool AddIceCandidate(int peerConnectionId, string sdp, |
+ int sdpMlineindex, string sdpMid); |
+ |
+ public PeerConnectionM(List<string> turnUrls, string username, string credential) { |
+ string[] urls = turnUrls != null ? turnUrls.ToArray() : null; |
+ int length = turnUrls != null ? turnUrls.Count : 0; |
+ mPeerConnectionId = CreatePeerConnection(urls, length, username, credential); |
RegisterCallbacks(); |
} |
- private void RegisterCallbacks() { |
- localDataChannelReadyDelegate_ = new LocalDataChannelReadyInternalDelegate(RaiseLocalDataChannelReady); |
- RegisterOnLocalDataChannelReady(peer_connection_id_, localDataChannelReadyDelegate_); |
- |
- dataFromDataChannelReadyDelegate_ = new DataFromDataChannelReadyInternalDelegate(RaiseDataFromDataChannelReady); |
- RegisterOnDataFromDataChannelReady(peer_connection_id_, dataFromDataChannelReadyDelegate_); |
- |
- failureMessageDelegate_ = new FailureMessageInternalDelegate(RaiseFailureMessage); |
- RegisterOnFailure(peer_connection_id_, failureMessageDelegate_); |
- |
- audioBusReadyDelegate_ = new AudioBusReadyInternalDelegate(RaiseAudioBusReady); |
- RegisterOnAudioBusReady(peer_connection_id_, audioBusReadyDelegate_); |
- |
- localSdpReadytoSendDelegate_ = new LocalSdpReadytoSendInternalDelegate(RaiseLocalSdpReadytoSend); |
- RegisterOnLocalSdpReadytoSend(peer_connection_id_, localSdpReadytoSendDelegate_); |
- |
- iceCandiateReadytoSendDelegate_ = new IceCandiateReadytoSendInternalDelegate(RaiseIceCandiateReadytoSend); |
- RegisterOnIceCandiateReadytoSend(peer_connection_id_, iceCandiateReadytoSendDelegate_); |
- } |
- |
- public void ClosePeerConnectionM() { |
- ClosePeerConnection(peer_connection_id_); |
- peer_connection_id_ = -1; |
+ public void ClosePeerConnection() { |
+ ClosePeerConnection(mPeerConnectionId); |
+ mPeerConnectionId = -1; |
} |
// Return -1 if Peerconnection is not available. |
public int GetUniqueId() { |
- return peer_connection_id_; |
+ return mPeerConnectionId; |
} |
- public void AddStreamM(bool audio_only) { |
- AddStream(peer_connection_id_, audio_only); |
+ public void AddStream(bool audioOnly) { |
+ AddStream(mPeerConnectionId, audioOnly); |
} |
- public void AddDataChannelM() { |
- AddDataChannel(peer_connection_id_); |
+ public void AddDataChannel() { |
+ AddDataChannel(mPeerConnectionId); |
} |
- public void CreateOfferM() { |
- CreateOffer(peer_connection_id_); |
+ public void CreateOffer() { |
+ CreateOffer(mPeerConnectionId); |
} |
- public void CreateAnswerM() { |
- CreateAnswer(peer_connection_id_); |
+ public void CreateAnswer() { |
+ CreateAnswer(mPeerConnectionId); |
} |
- public void SendDataViaDataChannelM(string data) { |
- SendDataViaDataChannel(peer_connection_id_, data); |
+ public void SendDataViaDataChannel(string data) { |
+ SendDataViaDataChannel(mPeerConnectionId, data); |
} |
- public void SetAudioControl(bool is_mute, bool is_record) { |
- SetAudioControl(peer_connection_id_, is_mute, is_record); |
+ public void SetAudioControl(bool isMute, bool isRecord) { |
+ SetAudioControl(mPeerConnectionId, isMute, isRecord); |
} |
- public void ReceivedSdpM(string sdp) { |
- peer_connection_id_ = ReceivedSdp(peer_connection_id_, sdp); |
- RegisterCallbacks(); |
+ public void SetRemoteDescription(string type, string sdp) { |
+ SetRemoteDescription(mPeerConnectionId, type, sdp); |
+ } |
+ |
+ public void AddIceCandidate(string candidate, int sdpMlineindex, string sdpMid) { |
+ AddIceCandidate(mPeerConnectionId, candidate, sdpMlineindex, sdpMid); |
} |
- public void ReceivedIceCandidateM(string ice_candidate) { |
- ReceivedIceCandidate(peer_connection_id_, ice_candidate); |
+ private void RegisterCallbacks() { |
+ localDataChannelReadyDelegate = new LocalDataChannelReadyInternalDelegate( |
+ RaiseLocalDataChannelReady); |
+ RegisterOnLocalDataChannelReady(mPeerConnectionId, localDataChannelReadyDelegate); |
+ |
+ dataFromDataChannelReadyDelegate = new DataFromDataChannelReadyInternalDelegate( |
+ RaiseDataFromDataChannelReady); |
+ RegisterOnDataFromDataChannelReady(mPeerConnectionId, dataFromDataChannelReadyDelegate); |
+ |
+ failureMessageDelegate = new FailureMessageInternalDelegate(RaiseFailureMessage); |
+ RegisterOnFailure(mPeerConnectionId, failureMessageDelegate); |
+ |
+ audioBusReadyDelegate = new AudioBusReadyInternalDelegate(RaiseAudioBusReady); |
+ RegisterOnAudioBusReady(mPeerConnectionId, audioBusReadyDelegate); |
+ |
+ localI420FrameReadyDelegate = new I420FrameReadyInternalDelegate( |
+ RaiseLocalVideoFrameReady); |
+ RegisterOnLocalI420FrameReady(mPeerConnectionId, localI420FrameReadyDelegate); |
+ |
+ remoteI420FrameReadyDelegate = new I420FrameReadyInternalDelegate( |
+ RaiseRemoteVideoFrameReady); |
+ RegisterOnRemoteI420FrameReady(mPeerConnectionId, remoteI420FrameReadyDelegate); |
+ |
+ localSdpReadytoSendDelegate = new LocalSdpReadytoSendInternalDelegate( |
+ RaiseLocalSdpReadytoSend); |
+ RegisterOnLocalSdpReadytoSend(mPeerConnectionId, localSdpReadytoSendDelegate); |
+ |
+ iceCandiateReadytoSendDelegate = |
+ new IceCandiateReadytoSendInternalDelegate(RaiseIceCandiateReadytoSend); |
+ RegisterOnIceCandiateReadytoSend( |
+ mPeerConnectionId, iceCandiateReadytoSendDelegate); |
} |
private void RaiseLocalDataChannelReady() { |
if (OnLocalDataChannelReady != null) |
- OnLocalDataChannelReady(peer_connection_id_); |
+ OnLocalDataChannelReady(mPeerConnectionId); |
} |
private void RaiseDataFromDataChannelReady(string data) { |
if (OnDataFromDataChannelReady != null) |
- OnDataFromDataChannelReady(peer_connection_id_, data); |
+ OnDataFromDataChannelReady(mPeerConnectionId, data); |
} |
private void RaiseFailureMessage(string msg) { |
if (OnFailureMessage != null) |
- OnFailureMessage(peer_connection_id_, msg); |
+ OnFailureMessage(mPeerConnectionId, msg); |
} |
- private void RaiseAudioBusReady(IntPtr data, int bits_per_sample, |
- int sample_rate, int number_of_channels, int number_of_frames) { |
+ private void RaiseAudioBusReady(IntPtr data, int bitsPerSample, |
+ int sampleRate, int numberOfChannels, int numberOfFrames) { |
if (OnAudioBusReady != null) |
- OnAudioBusReady(peer_connection_id_, data, bits_per_sample, sample_rate, |
- number_of_channels, number_of_frames); |
+ OnAudioBusReady(mPeerConnectionId, data, bitsPerSample, sampleRate, |
+ numberOfChannels, numberOfFrames); |
+ } |
+ |
+ private void RaiseLocalVideoFrameReady( |
+ IntPtr dataY, IntPtr dataU, IntPtr dataV, |
+ int strideY, int strideU, int strideV, |
+ uint width, uint height) { |
+ if (OnLocalVideoFrameReady != null) |
+ OnLocalVideoFrameReady(mPeerConnectionId, dataY, dataU, dataV, strideY, strideU, strideV, |
+ width, height); |
+ } |
+ |
+ private void RaiseRemoteVideoFrameReady( |
+ IntPtr dataY, IntPtr dataU, IntPtr dataV, |
+ int strideY, int strideU, int strideV, |
+ uint width, uint height) { |
+ if (OnRemoteVideoFrameReady != null) |
+ OnRemoteVideoFrameReady(mPeerConnectionId, dataY, dataU, dataV, strideY, strideU, strideV, |
+ width, height); |
} |
- private void RaiseLocalSdpReadytoSend(string msg) { |
+ |
+ private void RaiseLocalSdpReadytoSend(string type, string sdp) { |
if (OnLocalSdpReadytoSend != null) |
- OnLocalSdpReadytoSend(peer_connection_id_, msg); |
+ OnLocalSdpReadytoSend(mPeerConnectionId, type, sdp); |
} |
- private void RaiseIceCandiateReadytoSend(string msg) { |
+ private void RaiseIceCandiateReadytoSend(string candidate, int sdpMlineIndex, string sdpMid) { |
if (OnIceCandiateReadytoSend != null) |
- OnIceCandiateReadytoSend(peer_connection_id_, msg); |
+ OnIceCandiateReadytoSend(mPeerConnectionId, candidate, sdpMlineIndex, sdpMid); |
+ } |
+ |
+ public void AddQueuedIceCandidate(List<IceCandidate> iceCandidateQueue) { |
+ if (iceCandidateQueue != null) { |
+ foreach (IceCandidate ic in iceCandidateQueue) { |
+ AddIceCandidate(mPeerConnectionId, ic.Candidate, ic.SdpMlineIndex, ic.SdpMid); |
+ } |
+ } |
} |
- private LocalDataChannelReadyInternalDelegate localDataChannelReadyDelegate_ = null; |
+ private LocalDataChannelReadyInternalDelegate localDataChannelReadyDelegate = null; |
public event LocalDataChannelReadyDelegate OnLocalDataChannelReady; |
- private DataFromDataChannelReadyInternalDelegate dataFromDataChannelReadyDelegate_ = null; |
+ private DataFromDataChannelReadyInternalDelegate dataFromDataChannelReadyDelegate = null; |
public event DataFromDataChannelReadyDelegate OnDataFromDataChannelReady; |
- private FailureMessageInternalDelegate failureMessageDelegate_ = null; |
+ private FailureMessageInternalDelegate failureMessageDelegate = null; |
public event FailureMessageDelegate OnFailureMessage; |
- private AudioBusReadyInternalDelegate audioBusReadyDelegate_ = null; |
+ private AudioBusReadyInternalDelegate audioBusReadyDelegate = null; |
public event AudioBusReadyDelegate OnAudioBusReady; |
- private LocalSdpReadytoSendInternalDelegate localSdpReadytoSendDelegate_ = null; |
+ private I420FrameReadyInternalDelegate localI420FrameReadyDelegate = null; |
+ public event I420FrameReadyDelegate OnLocalVideoFrameReady; |
+ |
+ private I420FrameReadyInternalDelegate remoteI420FrameReadyDelegate = null; |
+ public event I420FrameReadyDelegate OnRemoteVideoFrameReady; |
+ |
+ private LocalSdpReadytoSendInternalDelegate localSdpReadytoSendDelegate = null; |
public event LocalSdpReadytoSendDelegate OnLocalSdpReadytoSend; |
- private IceCandiateReadytoSendInternalDelegate iceCandiateReadytoSendDelegate_ = null; |
+ private IceCandiateReadytoSendInternalDelegate iceCandiateReadytoSendDelegate = null; |
public event IceCandiateReadytoSendDelegate OnIceCandiateReadytoSend; |
- private int peer_connection_id_ = -1; |
+ private int mPeerConnectionId = -1; |
} |
} |