OLD | NEW |
(Empty) | |
| 1 This directory contains an example Unity native plugin for Windows OS. |
| 2 The APIs use Platform Invoke (P/Invoke) technology as required by Unity native p
lugin. |
| 3 This plugin dll can also be used by Windows C# applications other than Unity. |
| 4 |
| 5 An example of wrapping native plugin into a C# managed class in Unity is given a
s following: |
| 6 |
| 7 using System; |
| 8 using System.Runtime.InteropServices; |
| 9 |
| 10 namespace SimplePeerConnectionM { |
| 11 // This is a managed wrap up class for the native c style peer connection APIs
. |
| 12 public class PeerConnectionM { |
| 13 //private const string dll_path = "SimplePeerConnection"; |
| 14 private const string dll_path = "webrtc_unity_plugin"; |
| 15 |
| 16 [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
| 17 private static extern int CreatePeerConnection(); |
| 18 |
| 19 [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
| 20 private static extern bool ClosePeerConnection(int peer_connection_id); |
| 21 |
| 22 [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
| 23 private static extern bool AddStream(int peer_connection_id, bool audio_only
); |
| 24 |
| 25 [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
| 26 private static extern bool AddDataChannel(int peer_connection_id); |
| 27 |
| 28 [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
| 29 private static extern bool CreateOffer(int peer_connection_id); |
| 30 |
| 31 [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
| 32 private static extern bool CreateAnswer(int peer_connection_id); |
| 33 |
| 34 [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
| 35 private static extern bool SendDataViaDataChannel(int peer_connection_id, st
ring data); |
| 36 |
| 37 [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
| 38 private static extern bool SetAudioControl(int peer_connection_id, bool is_m
ute, bool is_record); |
| 39 |
| 40 [UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
| 41 private delegate void LocalDataChannelReadyInternalDelegate(); |
| 42 public delegate void LocalDataChannelReadyDelegate(int id); |
| 43 [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
| 44 private static extern bool RegisterOnLocalDataChannelReady(int peer_connecti
on_id, LocalDataChannelReadyInternalDelegate callback); |
| 45 |
| 46 [UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
| 47 private delegate void DataFromDataChannelReadyInternalDelegate(string s); |
| 48 public delegate void DataFromDataChannelReadyDelegate(int id, string s); |
| 49 [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
| 50 private static extern bool RegisterOnDataFromDataChannelReady(int peer_conne
ction_id, DataFromDataChannelReadyInternalDelegate callback); |
| 51 |
| 52 [UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
| 53 private delegate void FailureMessageInternalDelegate(string msg); |
| 54 public delegate void FailureMessageDelegate(int id, string msg); |
| 55 [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
| 56 private static extern bool RegisterOnFailure(int peer_connection_id, Failure
MessageInternalDelegate callback); |
| 57 |
| 58 [UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
| 59 private delegate void AudioBusReadyInternalDelegate(IntPtr data, int bits_pe
r_sample, |
| 60 int sample_rate, int number_of_channels, int number_of_frames); |
| 61 public delegate void AudioBusReadyDelegate(int id, IntPtr data, int bits_per
_sample, |
| 62 int sample_rate, int number_of_channels, int number_of_frames); |
| 63 [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
| 64 private static extern bool RegisterOnAudioBusReady(int peer_connection_id, A
udioBusReadyInternalDelegate callback); |
| 65 |
| 66 [UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
| 67 private delegate void LocalSdpReadytoSendInternalDelegate(string s); |
| 68 public delegate void LocalSdpReadytoSendDelegate(int id, string s); |
| 69 [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
| 70 private static extern bool RegisterOnLocalSdpReadytoSend(int peer_connection
_id, LocalSdpReadytoSendInternalDelegate callback); |
| 71 |
| 72 [UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
| 73 private delegate void IceCandiateReadytoSendInternalDelegate(string s); |
| 74 public delegate void IceCandiateReadytoSendDelegate(int id, string s); |
| 75 [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
| 76 private static extern bool RegisterOnIceCandiateReadytoSend(int peer_connect
ion_id, IceCandiateReadytoSendInternalDelegate callback); |
| 77 |
| 78 [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
| 79 private static extern int ReceivedSdp(int peer_connection_id, string sdp); |
| 80 |
| 81 [DllImport(dll_path, CallingConvention = CallingConvention.Cdecl)] |
| 82 private static extern bool ReceivedIceCandidate(int peer_connection_id, stri
ng ice_candidate); |
| 83 |
| 84 public void CreatePeerConnectionM() { |
| 85 peer_connection_id_ = CreatePeerConnection(); |
| 86 RegisterCallbacks(); |
| 87 } |
| 88 |
| 89 private void RegisterCallbacks() { |
| 90 localDataChannelReadyDelegate_ = new LocalDataChannelReadyInternalDelegate
(RaiseLocalDataChannelReady); |
| 91 RegisterOnLocalDataChannelReady(peer_connection_id_, localDataChannelReady
Delegate_); |
| 92 |
| 93 dataFromDataChannelReadyDelegate_ = new DataFromDataChannelReadyInternalDe
legate(RaiseDataFromDataChannelReady); |
| 94 RegisterOnDataFromDataChannelReady(peer_connection_id_, dataFromDataChanne
lReadyDelegate_); |
| 95 |
| 96 failureMessageDelegate_ = new FailureMessageInternalDelegate(RaiseFailureM
essage); |
| 97 RegisterOnFailure(peer_connection_id_, failureMessageDelegate_); |
| 98 |
| 99 audioBusReadyDelegate_ = new AudioBusReadyInternalDelegate(RaiseAudioBusRe
ady); |
| 100 RegisterOnAudioBusReady(peer_connection_id_, audioBusReadyDelegate_); |
| 101 |
| 102 localSdpReadytoSendDelegate_ = new LocalSdpReadytoSendInternalDelegate(Rai
seLocalSdpReadytoSend); |
| 103 RegisterOnLocalSdpReadytoSend(peer_connection_id_, localSdpReadytoSendDele
gate_); |
| 104 |
| 105 iceCandiateReadytoSendDelegate_ = new IceCandiateReadytoSendInternalDelega
te(RaiseIceCandiateReadytoSend); |
| 106 RegisterOnIceCandiateReadytoSend(peer_connection_id_, iceCandiateReadytoSe
ndDelegate_); |
| 107 } |
| 108 |
| 109 public void ClosePeerConnectionM() { |
| 110 ClosePeerConnection(peer_connection_id_); |
| 111 peer_connection_id_ = -1; |
| 112 } |
| 113 |
| 114 // Return -1 if Peerconnection is not available. |
| 115 public int GetUniqueId() { |
| 116 return peer_connection_id_; |
| 117 } |
| 118 |
| 119 public void AddStreamM(bool audio_only) { |
| 120 AddStream(peer_connection_id_, audio_only); |
| 121 } |
| 122 |
| 123 public void AddDataChannelM() { |
| 124 AddDataChannel(peer_connection_id_); |
| 125 } |
| 126 |
| 127 public void CreateOfferM() { |
| 128 CreateOffer(peer_connection_id_); |
| 129 } |
| 130 |
| 131 public void CreateAnswerM() { |
| 132 CreateAnswer(peer_connection_id_); |
| 133 } |
| 134 |
| 135 public void SendDataViaDataChannelM(string data) { |
| 136 SendDataViaDataChannel(peer_connection_id_, data); |
| 137 } |
| 138 |
| 139 public void SetAudioControl(bool is_mute, bool is_record) { |
| 140 SetAudioControl(peer_connection_id_, is_mute, is_record); |
| 141 } |
| 142 |
| 143 public void ReceivedSdpM(string sdp) { |
| 144 peer_connection_id_ = ReceivedSdp(peer_connection_id_, sdp); |
| 145 RegisterCallbacks(); |
| 146 } |
| 147 |
| 148 public void ReceivedIceCandidateM(string ice_candidate) { |
| 149 ReceivedIceCandidate(peer_connection_id_, ice_candidate); |
| 150 } |
| 151 |
| 152 private void RaiseLocalDataChannelReady() { |
| 153 if (OnLocalDataChannelReady != null) |
| 154 OnLocalDataChannelReady(peer_connection_id_); |
| 155 } |
| 156 |
| 157 private void RaiseDataFromDataChannelReady(string data) { |
| 158 if (OnDataFromDataChannelReady != null) |
| 159 OnDataFromDataChannelReady(peer_connection_id_, data); |
| 160 } |
| 161 |
| 162 private void RaiseFailureMessage(string msg) { |
| 163 if (OnFailureMessage != null) |
| 164 OnFailureMessage(peer_connection_id_, msg); |
| 165 } |
| 166 |
| 167 private void RaiseAudioBusReady(IntPtr data, int bits_per_sample, |
| 168 int sample_rate, int number_of_channels, int number_of_frames) { |
| 169 if (OnAudioBusReady != null) |
| 170 OnAudioBusReady(peer_connection_id_, data, bits_per_sample, sample_rate, |
| 171 number_of_channels, number_of_frames); |
| 172 } |
| 173 |
| 174 private void RaiseLocalSdpReadytoSend(string msg) { |
| 175 if (OnLocalSdpReadytoSend != null) |
| 176 OnLocalSdpReadytoSend(peer_connection_id_, msg); |
| 177 } |
| 178 |
| 179 private void RaiseIceCandiateReadytoSend(string msg) { |
| 180 if (OnIceCandiateReadytoSend != null) |
| 181 OnIceCandiateReadytoSend(peer_connection_id_, msg); |
| 182 } |
| 183 |
| 184 private LocalDataChannelReadyInternalDelegate localDataChannelReadyDelegate_
= null; |
| 185 public event LocalDataChannelReadyDelegate OnLocalDataChannelReady; |
| 186 |
| 187 private DataFromDataChannelReadyInternalDelegate dataFromDataChannelReadyDel
egate_ = null; |
| 188 public event DataFromDataChannelReadyDelegate OnDataFromDataChannelReady; |
| 189 |
| 190 private FailureMessageInternalDelegate failureMessageDelegate_ = null; |
| 191 public event FailureMessageDelegate OnFailureMessage; |
| 192 |
| 193 private AudioBusReadyInternalDelegate audioBusReadyDelegate_ = null; |
| 194 public event AudioBusReadyDelegate OnAudioBusReady; |
| 195 |
| 196 private LocalSdpReadytoSendInternalDelegate localSdpReadytoSendDelegate_ = n
ull; |
| 197 public event LocalSdpReadytoSendDelegate OnLocalSdpReadytoSend; |
| 198 |
| 199 private IceCandiateReadytoSendInternalDelegate iceCandiateReadytoSendDelegat
e_ = null; |
| 200 public event IceCandiateReadytoSendDelegate OnIceCandiateReadytoSend; |
| 201 |
| 202 private int peer_connection_id_ = -1; |
| 203 } |
| 204 } |
OLD | NEW |