Release 8.0.0.15812
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Remoting / MediaController / MediaControlServer.Events.cs
1 /*
2  * Copyright (c) 2018 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 Tizen.Applications;
18 using System;
19 using System.Collections.Generic;
20 using Native = Interop.MediaControllerServer;
21
22 namespace Tizen.Multimedia.Remoting
23 {
24     public static partial class MediaControlServer
25     {
26         private static Native.PlaybackActionCommandReceivedCallback _playbackActionCommandCallback;
27         private static Native.PlaybackPositionCommandReceivedCallback _playbackPositionCommandCallback;
28         private static Native.PlaylistCommandReceivedCallback _playlistCommandCallback;
29         private static Native.ShuffleModeCommandReceivedCallback _shuffleModeCommandCallback;
30         private static Native.RepeatModeCommandReceivedCallback _repeatModeCommandCallback;
31         private static Native.SimpleCommandReceivedCallback _subtitleModeCommandCallback;
32         private static Native.SimpleCommandReceivedCallback _mode360CommandCallback;
33         private static Native.DisplayModeCommandReceivedCallback _displayModeCommandCallback;
34         private static Native.DisplayRotationCommandReceivedCallback _displayRotationCommandCallback;
35
36         private static Native.CustomCommandReceivedCallback _customCommandCallback;
37         private static Native.SearchCommandReceivedCallback _searchCommandCallback;
38         private static Native.CommandCompletedCallback _commandCompletedCallback;
39
40
41         /// <summary>
42         /// Occurs when a client sends playback command.
43         /// </summary>
44         /// <since_tizen> 4 </since_tizen>
45         [Obsolete("Please do not use! This will be deprecated. Please use PlaybackActionCommandReceived instead.")]
46         public static event EventHandler<PlaybackCommandReceivedEventArgs> PlaybackCommandReceived;
47
48         /// <summary>
49         /// Occurs when a client sends playback command.
50         /// </summary>
51         /// <since_tizen> 5 </since_tizen>
52         public static event EventHandler<PlaybackActionCommandReceivedEventArgs> PlaybackActionCommandReceived;
53
54         /// <summary>
55         /// Occurs when a client sends playback position command.
56         /// </summary>
57         /// <since_tizen> 5 </since_tizen>
58         public static event EventHandler<PlaybackPositionCommandReceivedEventArgs> PlaybackPositionCommandReceived;
59
60         /// <summary>
61         /// Occurs when a client sends playlist command.
62         /// </summary>
63         /// <since_tizen> 5 </since_tizen>
64         public static event EventHandler<PlaylistCommandReceivedEventArgs> PlaylistCommandReceived;
65
66         /// <summary>
67         /// Occurs when a client sends shuffle mode command.
68         /// </summary>
69         /// <since_tizen> 5 </since_tizen>
70         public static event EventHandler<ShuffleModeCommandReceivedEventArgs> ShuffleModeCommandReceived;
71
72         /// <summary>
73         /// Occurs when a client sends repeat mode command.
74         /// </summary>
75         /// <since_tizen> 5 </since_tizen>
76         public static event EventHandler<RepeatModeCommandReceivedEventArgs> RepeatModeCommandReceived;
77
78         /// <summary>
79         /// Occurs when a client sends subtitle mode command.
80         /// </summary>
81         /// <since_tizen> 6 </since_tizen>
82         public static event EventHandler<SubtitleModeCommandReceivedEventArgs> SubtitleModeCommandReceived;
83
84         /// <summary>
85         /// Occurs when a client sends mode 360 command.
86         /// </summary>
87         /// <since_tizen> 6 </since_tizen>
88         public static event EventHandler<Mode360CommandReceivedEventArgs> Mode360CommandReceived;
89
90         /// <summary>
91         /// Occurs when a client sends display mode command.
92         /// </summary>
93         /// <since_tizen> 6 </since_tizen>
94         public static event EventHandler<DisplayModeCommandReceivedEventArgs> DisplayModeCommandReceived;
95
96         /// <summary>
97         /// Occurs when a client sends display rotation command.
98         /// </summary>
99         /// <since_tizen> 6 </since_tizen>
100         public static event EventHandler<DisplayRotationCommandReceivedEventArgs> DisplayRotationCommandReceived;
101
102         /// <summary>
103         /// Occurs when a client sends custom command.
104         /// </summary>
105         /// <since_tizen> 5 </since_tizen>
106         public static event EventHandler<CustomCommandReceivedEventArgs> CustomCommandReceived;
107
108         /// <summary>
109         /// Occurs when a client sends search command.
110         /// </summary>
111         /// <since_tizen> 5 </since_tizen>
112         public static event EventHandler<SearchCommandReceivedEventArgs> SearchCommandReceived;
113
114         /// <summary>
115         /// Occurs when a client sends custom command.
116         /// </summary>
117         /// <since_tizen> 5 </since_tizen>
118         internal static event EventHandler<CommandCompletedEventArgs> CommandCompleted;
119
120         private static void RegisterPlaybackActionCommandReceivedEvent()
121         {
122             _playbackActionCommandCallback = (clientName, requestId, playbackCommand, _) =>
123             {
124                 // SendPlaybackCommand doesn't use requestId. It'll be removed in Level 7.
125                 if (requestId == null)
126                 {
127 #pragma warning disable CS0618 // Type or member is obsolete
128                     PlaybackCommandReceived?.Invoke(null, new PlaybackCommandReceivedEventArgs(clientName, playbackCommand.ToPublic()));
129 #pragma warning restore CS0618 // Type or member is obsolete
130                 }
131                 else
132                 {
133                     var command = new PlaybackCommand(playbackCommand.ToPublic());
134                     command.SetResponseInformation(clientName, requestId);
135
136                     PlaybackActionCommandReceived?.Invoke(null, new PlaybackActionCommandReceivedEventArgs(command));
137                 }
138             };
139             Native.SetPlaybackActionCommandReceivedCb(Handle, _playbackActionCommandCallback).
140                 ThrowIfError("Failed to init PlaybackActionCommandReceived event.");
141         }
142
143         private static void RegisterPlaybackPositionCommandReceivedEvent()
144         {
145             _playbackPositionCommandCallback = (clientName, requestId, playbackPosition, _) =>
146             {
147                 var command = new PlaybackPositionCommand(playbackPosition);
148                 command.SetResponseInformation(clientName, requestId);
149
150                 PlaybackPositionCommandReceived?.Invoke(null, new PlaybackPositionCommandReceivedEventArgs(command));
151             };
152             Native.SetPlaybackPosotionCommandReceivedCb(Handle, _playbackPositionCommandCallback).
153                 ThrowIfError("Failed to init PlaybackPositionCommandReceived event.");
154         }
155
156         private static void RegisterPlaylistCommandReceivedEvent()
157         {
158             _playlistCommandCallback = (clientName, requestId, playlistName, index, playbackCommand, playbackPosition, _) =>
159             {
160                 var command = new PlaylistCommand(playbackCommand.ToPublic(), playlistName, index, playbackPosition);
161                 command.SetResponseInformation(clientName, requestId);
162
163                 PlaylistCommandReceived?.Invoke(null, new PlaylistCommandReceivedEventArgs(command));
164             };
165             Native.SetPlaylistCommandReceivedCb(Handle, _playlistCommandCallback).
166                 ThrowIfError("Failed to init PlaylistCommandReceived event.");
167         }
168
169         private static void RegisterShuffleModeCommandReceivedEvent()
170         {
171             _shuffleModeCommandCallback = (clientName, requestId, mode, _) =>
172             {
173                 var command = new ShuffleModeCommand(mode == MediaControllerNativeShuffleMode.On ? true : false);
174                 command.SetResponseInformation(clientName, requestId);
175
176                 ShuffleModeCommandReceived?.Invoke(null, new ShuffleModeCommandReceivedEventArgs(command));
177             };
178             Native.SetShuffleModeCommandReceivedCb(Handle, _shuffleModeCommandCallback).
179                 ThrowIfError("Failed to init ShuffleModeCommandReceived event.");
180         }
181
182         private static void RegisterRepeatModeCommandReceivedEvent()
183         {
184             _repeatModeCommandCallback = (clientName, requestId, mode, _) =>
185             {
186                 var command = new RepeatModeCommand(mode.ToPublic());
187                 command.SetResponseInformation(clientName, requestId);
188
189                 RepeatModeCommandReceived?.Invoke(null, new RepeatModeCommandReceivedEventArgs(command));
190             };
191             Native.SetRepeatModeCommandReceivedCb(Handle, _repeatModeCommandCallback).
192                 ThrowIfError("Failed to init RepeatModeCommandReceived event.");
193         }
194
195         private static void RegisterSubtitleModeCommandReceivedEvent()
196         {
197             _subtitleModeCommandCallback = (clientName, requestId, isEnabled, _) =>
198             {
199                 var command = new SubtitleModeCommand(isEnabled);
200                 command.SetResponseInformation(clientName, requestId);
201
202                 SubtitleModeCommandReceived?.Invoke(null, new SubtitleModeCommandReceivedEventArgs(command));
203             };
204             Native.SetSubtitleModeCommandReceivedCb(Handle, _subtitleModeCommandCallback).
205                 ThrowIfError("Failed to init SubtitleModeCommandReceived event.");
206         }
207
208         private static void RegisterMode360CommandReceivedEvent()
209         {
210             _mode360CommandCallback = (clientName, requestId, isEnabled, _) =>
211             {
212                 var command = new Mode360Command(isEnabled);
213                 command.SetResponseInformation(clientName, requestId);
214
215                 Mode360CommandReceived?.Invoke(null, new Mode360CommandReceivedEventArgs(command));
216             };
217             Native.SetMode360CommandReceivedCb(Handle, _mode360CommandCallback).
218                 ThrowIfError("Failed to init Mode360CommandReceived event.");
219         }
220
221         private static void RegisterDisplayModeCommandReceivedEvent()
222         {
223             _displayModeCommandCallback = (clientName, requestId, mode, _) =>
224             {
225                 var command = new DisplayModeCommand(mode.ToPublic());
226                 command.SetResponseInformation(clientName, requestId);
227
228                 DisplayModeCommandReceived?.Invoke(null, new DisplayModeCommandReceivedEventArgs(command));
229             };
230             Native.SetDisplayModeCommandReceivedCb(Handle, _displayModeCommandCallback).
231                 ThrowIfError("Failed to init DisplayModeCommandReceived event.");
232         }
233
234         private static void RegisterDisplayRotationCommandReceivedEvent()
235         {
236             _displayRotationCommandCallback = (clientName, requestId, rotation, _) =>
237             {
238                 var command = new DisplayRotationCommand(rotation.ToPublic());
239                 command.SetResponseInformation(clientName, requestId);
240
241                 DisplayRotationCommandReceived?.Invoke(null, new DisplayRotationCommandReceivedEventArgs(command));
242             };
243             Native.SetDisplayRotationCommandReceivedCb(Handle, _displayRotationCommandCallback).
244                 ThrowIfError("Failed to init DisplayRotationCommandReceived event.");
245         }
246
247         private static void RegisterCustomCommandReceivedEvent()
248         {
249             _customCommandCallback = (clientName, requestId, customCommand, bundleHandle, _) =>
250             {
251                 CustomCommand command = null;
252                 if (bundleHandle != IntPtr.Zero)
253                 {
254                     command = new CustomCommand(customCommand, new Bundle(new SafeBundleHandle(bundleHandle, true)));
255                 }
256                 else
257                 {
258                     command = new CustomCommand(customCommand);
259                 }
260
261                 command.SetResponseInformation(clientName, requestId);
262
263                 CustomCommandReceived?.Invoke(null, new CustomCommandReceivedEventArgs(command));
264             };
265             Native.SetCustomCommandReceivedCb(Handle, _customCommandCallback).
266                 ThrowIfError("Failed to init CustomCommandReceived event.");
267         }
268
269         private static SearchCommand CreateSearchCommandReceivedEventArgs(IntPtr searchHandle)
270         {
271             var searchConditions = new List<MediaControlSearchCondition>();
272
273             Native.SearchItemCallback searchItemCallback = (type, category, keyword, bundleHandle, _) =>
274             {
275                 Bundle bundle = null;
276
277                 if (bundleHandle != IntPtr.Zero)
278                 {
279                     bundle = new Bundle(new SafeBundleHandle(bundleHandle, true));
280                 }
281
282                 searchConditions.Add(new MediaControlSearchCondition(type, category, keyword, bundle));
283
284                 return true;
285             };
286
287             Native.ForeachSearchCondition(searchHandle, searchItemCallback).
288                 ThrowIfError("Failed to get search items.");
289
290             return new SearchCommand(searchConditions, searchHandle);
291         }
292
293         private static void RegisterSearchCommandReceivedEvent()
294         {
295             _searchCommandCallback = (clientName, requestId, searchHandle, _) =>
296             {
297                 var command = CreateSearchCommandReceivedEventArgs(searchHandle);
298
299                 command.SetResponseInformation(clientName, requestId);
300
301                 SearchCommandReceived?.Invoke(null, new SearchCommandReceivedEventArgs(command));
302             };
303             Native.SetSearchCommandReceivedCb(Handle, _searchCommandCallback).
304                 ThrowIfError("Failed to init SearchCommandReceived event.");
305         }
306
307         private static void RegisterCommandCompletedEvent()
308         {
309             _commandCompletedCallback = (clientName, requestId, result, bundleHandle, _) =>
310             {
311                 if (bundleHandle != IntPtr.Zero)
312                 {
313                     CommandCompleted?.Invoke(null, new CommandCompletedEventArgs(requestId, result, new Bundle(new SafeBundleHandle(bundleHandle, true))));
314                 }
315                 else
316                 {
317                     CommandCompleted?.Invoke(null, new CommandCompletedEventArgs(requestId, result));
318                 }
319             };
320             Native.SetEventReceivedCb(Handle, _commandCompletedCallback).
321                 ThrowIfError("Failed to init RegisterEventCompletedEvent.");
322         }
323     }
324 }