[WebRTC] Add new DataChannel buffered amount APIs (#4263)
authorHaesu Gwon <haesu.gwon@samsung.com>
Wed, 25 May 2022 01:57:27 +0000 (10:57 +0900)
committerGitHub <noreply@github.com>
Wed, 25 May 2022 01:57:27 +0000 (10:57 +0900)
src/Tizen.Multimedia.Remoting/Interop/Interop.WebRTC.DataChannel.cs
src/Tizen.Multimedia.Remoting/WebRTC/WebRTCDataChannel.Events.cs
src/Tizen.Multimedia.Remoting/WebRTC/WebRTCDataChannel.cs

index 906d02d..3ef3f9e 100755 (executable)
@@ -38,6 +38,9 @@ internal static partial class Interop
         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
         internal delegate void ClosedCallback(IntPtr dataChanndelHandle, IntPtr userData);
 
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+        internal delegate void BufferedAmountLowThresholdCallback(IntPtr dataChanndelHandle, IntPtr userData);
+
 
         [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_create_data_channel")]
         internal static extern WebRTCErrorCode Create(IntPtr handle, string label, SafeBundleHandle bundle, out IntPtr dataChanndelHandle);
@@ -57,6 +60,12 @@ internal static partial class Interop
         [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_get_data")]
         internal static extern WebRTCErrorCode GetData(IntPtr byteDataHandle, out IntPtr data, out ulong size);
 
+        [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_data_channel_get_buffered_amount")]
+        internal static extern WebRTCErrorCode GetBufferedAmount(IntPtr handle, out uint amount);
+
+        [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_data_channel_get_buffered_amount_low_threshold")]
+        internal static extern WebRTCErrorCode GetBufferedAmountLowThreshold(IntPtr handle, out uint threshold);
+
         [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_set_data_channel_cb")]
         internal static extern WebRTCErrorCode SetCreatedByPeerCb(IntPtr handle, CreatedCallback callback, IntPtr userData = default);
 
@@ -86,5 +95,12 @@ internal static partial class Interop
 
         [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_data_channel_unset_close_cb")]
         internal static extern WebRTCErrorCode UnsetClosedCb(IntPtr handle);
+
+        [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_data_channel_set_buffered_amount_low_cb")]
+        internal static extern WebRTCErrorCode SetBufferedAmountLowThresholdCb(IntPtr handle, uint threshold, BufferedAmountLowThresholdCallback callback,
+            IntPtr userData = default);
+
+        [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_data_channel_unset_buffered_amount_low_cb")]
+        internal static extern WebRTCErrorCode UnsetBufferedAmountLowThresholdCb(IntPtr handle);
     }
 }
\ No newline at end of file
index 2107fdc..b998669 100755 (executable)
@@ -30,11 +30,13 @@ namespace Tizen.Multimedia.Remoting
         private NativeDataChannel.ClosedCallback _webRtcDataChannelClosedCallback;
         private NativeDataChannel.MessageReceivedCallback _webRtcDataChannelMsgRecvCallback;
         private NativeDataChannel.ErrorOccurredCallback _webRtcDataChannelErrorOccurredCallback;
+        private NativeDataChannel.BufferedAmountLowThresholdCallback _webRtcDataChannelBufferedAmountLowThresholdCallback;
 
         private event EventHandler<EventArgs> _opened;
         private event EventHandler<EventArgs> _closed;
         private event EventHandler<WebRTCDataChannelMessageReceivedEventArgs> _messageReceived;
         private event EventHandler<WebRTCDataChannelErrorOccurredEventArgs> _errorOccurred;
+        private event EventHandler<EventArgs> _bufferedAmountLowThresholdOccurred;
 
         /// <summary>
         /// Occurs when the data channel's underlying data transport is established.
@@ -132,6 +134,31 @@ namespace Tizen.Multimedia.Remoting
             }
         }
 
+        /// <summary>
+        /// Occurs when the buffered data amount is lower than <see cref="BufferedAmountLowThreshold"/>.<br/>
+        /// If <see cref="BufferedAmountLowThreshold"/> is not set, this event will not be raised.
+        /// </summary>
+        /// <since_tizen> 10 </since_tizen>
+        public event EventHandler<EventArgs> BufferedAmountLow
+        {
+            add
+            {
+                if (_bufferedAmountLowThresholdOccurred == null)
+                {
+                    RegisterDataChannelBufferedAmountLowThresholdCallback();
+                }
+                _bufferedAmountLowThresholdOccurred += value;
+            }
+            remove
+            {
+                _bufferedAmountLowThresholdOccurred -= value;
+                if (_bufferedAmountLowThresholdOccurred == null)
+                {
+                    UnregisterDataChannelBufferedAmountLowThresholdCallback();
+                }
+            }
+        }
+
         private void RegisterDataChannelOpenedCallback()
         {
             _webRtcDataChannelOpenedCallback = (dataChannelHandle, _) =>
@@ -199,5 +226,26 @@ namespace Tizen.Multimedia.Remoting
             NativeDataChannel.UnsetErrorOccurredCb(_handle).
                 ThrowIfFailed("Failed to unset data channel error callback.");
         }
+
+        private void RegisterDataChannelBufferedAmountLowThresholdCallback()
+        {
+            if (_webRtcDataChannelBufferedAmountLowThresholdCallback == null)
+            {
+                _webRtcDataChannelBufferedAmountLowThresholdCallback = (dataChannelHanel, _) =>
+                {
+                    _bufferedAmountLowThresholdOccurred?.Invoke(this, new EventArgs());
+                };
+            }
+
+            NativeDataChannel.SetBufferedAmountLowThresholdCb(_handle, _bufferThreshold.Value,
+                _webRtcDataChannelBufferedAmountLowThresholdCallback).
+                ThrowIfFailed("Failed to set buffered amount low threshold callback.");
+        }
+
+        private void UnregisterDataChannelBufferedAmountLowThresholdCallback()
+        {
+            NativeDataChannel.UnsetBufferedAmountLowThresholdCb(_handle).
+                ThrowIfFailed("Failed to unset buffered amount low threshold callback.");
+        }
     }
 }
\ No newline at end of file
index 327a4df..d36b713 100755 (executable)
@@ -114,6 +114,57 @@ namespace Tizen.Multimedia.Remoting
         public string Label { get; }
 
         /// <summary>
+        /// Gets the amount of buffered data.
+        /// </summary>
+        /// <value>The buffered amount in bytes.</value>
+        /// <since_tizen> 10 </since_tizen>
+        public uint BufferedAmount
+        {
+            get
+            {
+                ValidateNotDisposed();
+
+                NativeDataChannel.GetBufferedAmount(Handle, out uint amount).
+                    ThrowIfFailed("Failed to get buffered amount");
+
+                return amount;
+            }
+        }
+
+        private uint? _bufferThreshold;
+        /// <summary>
+        /// Gets or sets the threshold of data channel buffered amount.<br/>
+        /// If the amount of buffered data is lower than threshold value, <see cref="BufferedAmountLow"/> will be occurred.<br/>
+        /// The default value is 0, which means <see cref="BufferedAmountLow"/> is disabled and will not be raised.
+        /// </summary>
+        /// <since_tizen> 10 </since_tizen>
+        public uint BufferedAmountLowThreshold
+        {
+            get
+            {
+                ValidateNotDisposed();
+
+                if (!_bufferThreshold.HasValue)
+                {
+                    NativeDataChannel.GetBufferedAmountLowThreshold(Handle, out uint threshold).
+                        ThrowIfFailed("Failed to get buffer threshold value");
+
+                    _bufferThreshold = threshold;
+                }
+
+                return _bufferThreshold.Value;
+            }
+            set
+            {
+                ValidateNotDisposed();
+
+                _bufferThreshold = value;
+
+                RegisterDataChannelBufferedAmountLowThresholdCallback();
+            }
+        }
+
+        /// <summary>
         /// Sends a string data across the data channel to the remote peer.
         /// </summary>
         /// <param name="data">The string data to send</param>