[WebRTC] Add initial code (#2977)
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Remoting / WebRTC / WebRTCDataChannel.Events.cs
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using static Interop;
19
20 namespace Tizen.Multimedia.Remoting
21 {
22     /// <summary>
23     /// Provides the ability to control WebRTC data channel.
24     /// </summary>
25     /// <since_tizen> 9 </since_tizen>
26     public partial class WebRTCDataChannel
27     {
28         private NativeDataChannel.OpenedCallback _webRtcDataChannelOpenedCallback;
29         private NativeDataChannel.ClosedCallback _webRtcDataChannelClosedCallback;
30         private NativeDataChannel.MessageReceivedCallback _webRtcDataChannelMsgRecvCallback;
31         private NativeDataChannel.ErrorOccurredCallback _webRtcDataChannelErrorOccurredCallback;
32
33         /// <summary>
34         /// Occurs when the data channel's underlying data transport is established.
35         /// </summary>
36         /// <since_tizen> 9 </since_tizen>
37         public event EventHandler<EventArgs> Opened;
38
39         /// <summary>
40         /// Occurs when the data channel has closed down.
41         /// </summary>
42         /// <since_tizen> 9 </since_tizen>
43         public event EventHandler<EventArgs> Closed;
44
45         /// <summary>
46         /// Occurs when a message is received from the remote peer.
47         /// </summary>
48         /// <since_tizen> 9 </since_tizen>
49         public event EventHandler<WebRTCDataChannelMessageReceivedEventArgs> MessageReceived;
50
51         /// <summary>
52         /// Occurs when an error occurs on the data channel.
53         /// </summary>
54         /// <since_tizen> 9 </since_tizen>
55         public event EventHandler<WebRTCDataChannelErrorOccurredEventArgs> ErrorOccurred;
56
57         private void RegisterEvents()
58         {
59             RegisterDataChannelOpenedCallback();
60             RegisterDataChannelClosedCallback();
61             RegisterDataChannelMsgRecvCallback();
62             RegisterDataChannelErrorOccurredCallback();
63         }
64
65         private void RegisterDataChannelOpenedCallback()
66         {
67             _webRtcDataChannelOpenedCallback = (dataChannelHandle, _) =>
68             {
69                 Opened?.Invoke(this, new EventArgs());
70             };
71
72             NativeDataChannel.SetOpenedCb(_handle, _webRtcDataChannelOpenedCallback).
73                 ThrowIfFailed("Failed to set data channel opened callback.");
74         }
75
76         private void RegisterDataChannelMsgRecvCallback()
77         {
78             _webRtcDataChannelMsgRecvCallback = (dataChannelHandle, type, message, _) =>
79             {
80                 MessageReceived?.Invoke(this, new WebRTCDataChannelMessageReceivedEventArgs(type, message));
81             };
82
83             NativeDataChannel.SetMessageReceivedCb(_handle, _webRtcDataChannelMsgRecvCallback).
84                 ThrowIfFailed("Failed to set data channel message received callback.");
85         }
86
87         private void RegisterDataChannelErrorOccurredCallback()
88         {
89             _webRtcDataChannelErrorOccurredCallback = (dataChannelHandle, error, _) =>
90             {
91                 ErrorOccurred?.Invoke(this, new WebRTCDataChannelErrorOccurredEventArgs((WebRTCError)error));
92             };
93
94             NativeDataChannel.SetErrorOccurredCb(_handle, _webRtcDataChannelErrorOccurredCallback).
95                 ThrowIfFailed("Failed to set data channel error callback.");
96         }
97
98         private void RegisterDataChannelClosedCallback()
99         {
100             _webRtcDataChannelClosedCallback = (dataChannelHandle, _) =>
101             {
102                 Closed?.Invoke(this, new EventArgs());
103             };
104
105             NativeDataChannel.SetClosedCb(_handle, _webRtcDataChannelClosedCallback).
106                 ThrowIfFailed("Failed to set data channel closed callback.");
107         }
108     }
109 }