2 * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.ComponentModel;
19 using System.Diagnostics;
20 using System.Threading.Tasks;
23 namespace Tizen.Multimedia.Remoting
26 /// Provides the ability to control WebRTCSignalingServer.
28 [EditorBrowsable(EditorBrowsableState.Never)]
29 public class WebRTCSignalingServer : IDisposable
31 private readonly IntPtr _handle;
32 private bool _disposed;
35 /// Initializes a new instance of the <see cref="WebRTCSignalingServer"/> class.
37 /// <param name="port">The server port.</param>
38 [EditorBrowsable(EditorBrowsableState.Never)]
39 public WebRTCSignalingServer(int port)
41 SignalingServer.Create(port, out _handle).
42 ThrowIfFailed("Failed to create signaling");
48 /// Starts the signaling server.
50 [EditorBrowsable(EditorBrowsableState.Never)]
53 ValidateNotDisposed();
55 SignalingServer.Start(_handle).
56 ThrowIfFailed("Failed to start signaling server");
60 /// Stops the signaling server.
62 [EditorBrowsable(EditorBrowsableState.Never)]
65 ValidateNotDisposed();
67 SignalingServer.Stop(_handle).
68 ThrowIfFailed("Failed to stop signaling server");
71 #region dispose support
72 internal bool IsDisposed => _disposed;
74 /// Releases all resources used by the current instance.
76 /// <exception cref="ObjectDisposedException">The WebRTCSignalingServer has already been disposed.</exception>
80 GC.SuppressFinalize((object)this);
84 /// Releases the unmanaged resources used by the <see cref="WebRTCSignalingServer"/>.
86 /// <param name="disposing">
87 /// true to release both managed and unmanaged resources;
88 /// false to release only unmanaged resources.
90 [EditorBrowsable(EditorBrowsableState.Never)]
91 protected virtual void Dispose(bool disposing)
93 if (_disposed || !disposing)
98 if (_handle != IntPtr.Zero)
100 SignalingServer.Destroy(_handle);
105 private void ValidateNotDisposed()
109 Log.Error(WebRTCLog.Tag, "WebRTCSignalingServer was disposed");
110 throw new ObjectDisposedException(nameof(WebRTCSignalingServer));
113 #endregion dispose support
118 /// Provides the ability to control WebRTCSignalingClient.
120 [EditorBrowsable(EditorBrowsableState.Never)]
121 public class WebRTCSignalingClient : IDisposable
123 private IntPtr _handle;
124 private bool _isConnected;
125 private (string serverIp, int port) _serverInfo;
126 private SignalingClient.SignalingMessageCallback _signalingMessageCallback;
127 private bool _disposed;
130 /// Initializes a new instance of the <see cref="WebRTCSignalingClient"/> class.
132 /// <param name="serverIp">The server IP.</param>
133 /// <param name="port">The server port.</param>
134 /// <seealso cref="Connect"/>
135 [EditorBrowsable(EditorBrowsableState.Never)]
136 public WebRTCSignalingClient(string serverIp, int port)
138 ValidationUtil.ValidateIsNullOrEmpty(serverIp, nameof(serverIp));
142 throw new ArgumentException("port should be greater than zero.");
145 _serverInfo.serverIp = serverIp;
146 _serverInfo.port = port;
150 /// Occurs when a message to be handled is sent from the remote peer or the signaling server.
152 [EditorBrowsable(EditorBrowsableState.Never)]
153 public event EventHandler<WebRTCSignalingEventArgs> SignalingMessage;
156 /// Connect to signaling server and return client id.
158 /// <returns>The signaling client ID.</returns>
159 /// <exception cref="ObjectDisposedException">The WebRTCSignalingClient has already been disposed.</exception>
160 [EditorBrowsable(EditorBrowsableState.Never)]
161 public async Task<int> Connect()
163 ValidateNotDisposed();
165 var tcsConnected = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
167 _signalingMessageCallback = (type, message, _) =>
169 Log.Info(WebRTCLog.Tag, $"type:{type}, message:{message}");
171 if (!_isConnected && type == SignalingMessageType.Connected)
175 SignalingClient.GetID(_handle, out int id).ThrowIfFailed("Failed to get signaling client ID");
176 Log.Info(WebRTCLog.Tag, $"Client ID[{id}]");
178 tcsConnected.TrySetResult(id);
181 SignalingMessage?.Invoke(this, new WebRTCSignalingEventArgs(type, message));
184 SignalingClient.Connect(_serverInfo.serverIp, _serverInfo.port, _signalingMessageCallback, IntPtr.Zero, out _handle).
185 ThrowIfFailed("Failed to connect to server");
187 return await tcsConnected.Task;
191 /// Requests session with peer ID.
193 /// <param name="peerId">The ID of remote peer.</param>
194 /// <exception cref="ObjectDisposedException">The WebRTCSignalingClient has already been disposed.</exception>
195 /// <see cref="SignalingMessageType.SessionEstablished"/>
196 [EditorBrowsable(EditorBrowsableState.Never)]
197 public void RequestSession(int peerId)
199 ValidateNotDisposed();
201 SignalingClient.RequestSession(_handle, peerId).
202 ThrowIfFailed("Failed to request session to peer");
206 /// Sends the signaling message to remote peer.
208 /// <param name="message"></param>
209 /// <exception cref="ObjectDisposedException">The WebRTCSignalingClient has already been disposed.</exception>
210 [EditorBrowsable(EditorBrowsableState.Never)]
211 public void SendMessage(string message)
213 ValidateNotDisposed();
215 SignalingClient.SendMessage(_handle, message).
216 ThrowIfFailed("Failed to send message to peer");
219 #region dispose support
220 internal bool IsDisposed => _disposed;
222 /// Releases all resources used by the current instance.
224 /// <exception cref="ObjectDisposedException">The WebRTCSignalingClient has already been disposed.</exception>
225 public void Dispose()
228 GC.SuppressFinalize((object)this);
232 /// Releases the unmanaged resources used by the <see cref="WebRTCSignalingClient"/>.
234 /// <param name="disposing">
235 /// true to release both managed and unmanaged resources;
236 /// false to release only unmanaged resources.
238 [EditorBrowsable(EditorBrowsableState.Never)]
239 protected virtual void Dispose(bool disposing)
241 if (_disposed || !disposing)
246 if (_handle != IntPtr.Zero)
248 SignalingClient.Disconnect(_handle);
250 _isConnected = false;
255 private void ValidateNotDisposed()
259 Log.Error(WebRTCLog.Tag, "WebRTCSignalingClient was disposed");
260 throw new ObjectDisposedException(nameof(WebRTCSignalingClient));
263 #endregion dispose support