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 Tizen.Applications;
23 namespace Tizen.Multimedia.Remoting
26 /// Provides the ability to control WebRTC data channel.
28 /// <since_tizen> 9 </since_tizen>
29 public partial class WebRTCDataChannel : IDisposable
31 private readonly IntPtr _handle;
34 /// Initializes a new instance of the <see cref="WebRTCDataChannel"/> class.
36 /// <param name="webRtc">The owner of this WebRTCDataChannel.</param>
37 /// <param name="label">The name of this data channel.</param>
38 /// <exception cref="ArgumentNullException">The webRtc or label is null.</exception>
39 /// <since_tizen> 9 </since_tizen>
40 public WebRTCDataChannel(WebRTC webRtc, string label)
41 : this(webRtc, label, null)
46 /// Initializes a new instance of the <see cref="WebRTCDataChannel"/> class.
49 /// The bundle is similar format as the RTCDataChannelInit members outlined https://www.w3.org/TR/webrtc/#dom-rtcdatachannelinit.<br/>
50 /// The following attributes can be set to options by using <see cref="Bundle"/> API:<br/>
51 /// 'ordered' of type bool : Whether the channel will send data with guaranteed ordering. The default value is true.<br/>
52 /// 'max-packet-lifetime' of type int : The time in milliseconds to attempt transmitting unacknowledged data. -1 for unset. The default value is -1.<br/>
53 /// 'max-retransmits' of type int : The number of times data will be attempted to be transmitted without acknowledgement before dropping. The default value is -1.<br/>
54 /// 'protocol' of type string : The subprotocol used by this channel. The default value is NULL.<br/>
55 /// 'id' of type int : Override the default identifier selection of this channel. The default value is -1.<br/>
56 /// 'priority' of type int : The priority to use for this channel(1:very low, 2:low, 3:medium, 4:high). The default value is 2.<br/>
58 /// <param name="webRtc">The owner of this WebRTCDataChannel.</param>
59 /// <param name="label">The name of this data channel.</param>
60 /// <param name="bundle">The data channel option.</param>
61 /// <exception cref="ArgumentNullException">The webRtc or label is null.</exception>
62 /// <since_tizen> 9 </since_tizen>
63 public WebRTCDataChannel(WebRTC webRtc, string label, Bundle bundle)
67 throw new ArgumentNullException(nameof(webRtc), "WebRTC is not created successfully.");
70 if (string.IsNullOrEmpty(label))
72 throw new ArgumentNullException(nameof(label), "label is null.");
75 var bundle_ = bundle?.SafeBundleHandle ?? new SafeBundleHandle();
76 NativeDataChannel.Create(webRtc.Handle, label, bundle_, out _handle).
77 ThrowIfFailed("Failed to create webrtc data channel");
79 Debug.Assert(_handle != IntPtr.Zero);
84 internal WebRTCDataChannel(IntPtr dataChannelHandle)
86 if (dataChannelHandle == IntPtr.Zero)
88 throw new ArgumentNullException(nameof(dataChannelHandle),
89 "WebRTC is not created successfully in native");
92 _handle = dataChannelHandle;
94 NativeDataChannel.GetLabel(_handle, out string label).
95 ThrowIfFailed("Failed to get label");
100 private IntPtr Handle
104 ValidateNotDisposed();
110 /// Gets the label of this data channel.
112 /// <value>The label.</value>
113 /// <since_tizen> 9 </since_tizen>
114 public string Label { get; }
117 /// Gets the amount of buffered data.
119 /// <value>The buffered amount in bytes.</value>
120 /// <exception cref="ObjectDisposedException">The WebRTCDataChannel has already been disposed.</exception>
121 /// <since_tizen> 10 </since_tizen>
122 public uint BufferedAmount
126 ValidateNotDisposed();
128 NativeDataChannel.GetBufferedAmount(Handle, out uint amount).
129 ThrowIfFailed("Failed to get buffered amount");
135 private uint? _bufferThreshold = 0;
137 /// Gets or sets the threshold of data channel buffered amount.<br/>
138 /// If the amount of buffered data is lower than threshold value, <see cref="BufferedAmountLow"/> will be occurred.<br/>
139 /// The default value is 0, which means <see cref="BufferedAmountLow"/> is disabled and will not be raised.
141 /// <exception cref="ObjectDisposedException">The WebRTCDataChannel has already been disposed.</exception>
142 /// <since_tizen> 10 </since_tizen>
143 public uint BufferedAmountLowThreshold
147 ValidateNotDisposed();
149 if (!_bufferThreshold.HasValue)
151 NativeDataChannel.GetBufferedAmountLowThreshold(Handle, out uint threshold).
152 ThrowIfFailed("Failed to get buffer threshold value");
154 _bufferThreshold = threshold;
157 return _bufferThreshold.Value;
161 ValidateNotDisposed();
163 _bufferThreshold = value;
165 RegisterDataChannelBufferedAmountLowThresholdCallback();
170 /// Sends a string data across the data channel to the remote peer.
172 /// <param name="data">The string data to send</param>
173 /// <exception cref="ObjectDisposedException">The WebRTCDataChannel has already been disposed.</exception>
174 /// <since_tizen> 9 </since_tizen>
175 public void Send(string data)
177 ValidateNotDisposed();
179 NativeDataChannel.SendString(Handle, data).
180 ThrowIfFailed("Failed to send string data");
184 /// Sends byte data across the data channel to the remote peer.
186 /// <param name="data">The byte data to send</param>
187 /// <exception cref="ObjectDisposedException">The WebRTCDataChannel has already been disposed.</exception>
188 /// <since_tizen> 9 </since_tizen>
189 public void Send(byte[] data)
191 ValidateNotDisposed();
195 throw new ArgumentNullException(nameof(data), "data is null");
198 NativeDataChannel.SendBytes(Handle, data, (uint)data.Length).
199 ThrowIfFailed("Failed to send bytes data");
202 #region Dispose support
203 private bool _disposed;
206 /// Releases all resources used by the current instance.
208 /// <since_tizen> 9 </since_tizen>
209 public void Dispose()
212 GC.SuppressFinalize(this);
216 /// Releases the unmanaged resources used by the <see cref="WebRTCDataChannel"/>.
218 /// <param name="disposing">
219 /// true to release both managed and unmanaged resources;
220 /// false to release only unmanaged resources.
222 [EditorBrowsable(EditorBrowsableState.Never)]
223 protected virtual void Dispose(bool disposing)
225 if (_disposed || !disposing)
230 if (_handle != IntPtr.Zero)
232 NativeDataChannel.Destroy(_handle);
237 private void ValidateNotDisposed()
241 Log.Warn(WebRTCLog.Tag, "WebRTCDataChannel was disposed");
242 throw new ObjectDisposedException(nameof(WebRTCDataChannel));
245 #endregion Dispose support