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