3320b7dae9d43aec40276ce99334bef09542ac15
[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.PlaybackStateCommandReceivedCallback _playbackCommandCallback;
27         private static Native.PlaybackActionCommandReceivedCallback _playbackActionCommandCallback;
28         private static Native.PlaybackPositionCommandReceivedCallback _playbackPositionCommandCallback;
29         private static Native.PlaylistCommandReceivedCallback _playlistCommandCallback;
30         private static Native.ShuffleModeCommandReceivedCallback _shuffleModeCommandCallback;
31         private static Native.RepeatModeCommandReceivedCallback _repeatModeCommandCallback;
32         private static Native.CustomCommandReceivedCallback _customCommandCallback;
33         private static Native.SearchCommandReceivedCallback _searchCommandCallback;
34         private static Native.CommandCompletedCallback _commandCompletedCallback;
35
36         /// <summary>
37         /// Occurs when a client sends playback command.
38         /// </summary>
39         /// <since_tizen> 4 </since_tizen>
40         [Obsolete("Please do not use! This will be deprecated. Please use PlaybackActionCommandReceived instead.")]
41         public static event EventHandler<PlaybackCommandReceivedEventArgs> PlaybackCommandReceived;
42
43         /// <summary>
44         /// Occurs when a client sends playback command.
45         /// </summary>
46         /// <since_tizen> 5 </since_tizen>
47         public static event EventHandler<PlaybackActionCommandReceivedEventArgs> PlaybackActionCommandReceived;
48
49         /// <summary>
50         /// Occurs when a client sends playback position command.
51         /// </summary>
52         /// <since_tizen> 5 </since_tizen>
53         public static event EventHandler<PlaybackPositionCommandReceivedEventArgs> PlaybackPositionCommandReceived;
54
55         /// <summary>
56         /// Occurs when a client sends playlist command.
57         /// </summary>
58         /// <since_tizen> 5 </since_tizen>
59         public static event EventHandler<PlaylistCommandReceivedEventArgs> PlaylistCommandReceived;
60
61         /// <summary>
62         /// Occurs when a client sends shuffle mode command.
63         /// </summary>
64         /// <since_tizen> 5 </since_tizen>
65         public static event EventHandler<ShuffleModeCommandReceivedEventArgs> ShuffleModeCommandReceived;
66
67         /// <summary>
68         /// Occurs when a client sends repeat mode command.
69         /// </summary>
70         /// <since_tizen> 5 </since_tizen>
71         public static event EventHandler<RepeatModeCommandReceivedEventArgs> RepeatModeCommandReceived;
72
73         /// <summary>
74         /// Occurs when a client sends custom command.
75         /// </summary>
76         /// <since_tizen> 5 </since_tizen>
77         public static event EventHandler<CustomCommandReceivedEventArgs> CustomCommandReceived;
78
79         /// <summary>
80         /// Occurs when a client sends search command.
81         /// </summary>
82         /// <since_tizen> 5 </since_tizen>
83         public static event EventHandler<SearchCommandReceivedEventArgs> SearchCommandReceived;
84
85         /// <summary>
86         /// Occurs when a client sends custom command.
87         /// </summary>
88         /// <since_tizen> 5 </since_tizen>
89         internal static event EventHandler<CommandCompletedEventArgs> CommandCompleted;
90
91         private static void RegisterPlaybackActionCommandReceivedEvent()
92         {
93             _playbackActionCommandCallback = (clientName, requestId, playbackCommand, _) =>
94             {
95                 // SendPlaybackCommand doesn't use requestId. It'll be removed in Level 7.
96                 if (requestId == null)
97                 {
98 #pragma warning disable CS0618 // Type or member is obsolete
99                     PlaybackCommandReceived?.Invoke(null, new PlaybackCommandReceivedEventArgs(clientName, playbackCommand.ToPublic()));
100 #pragma warning restore CS0618 // Type or member is obsolete
101                 }
102                 else
103                 {
104                     var command = new PlaybackCommand(playbackCommand.ToPublic());
105                     command.SetResponseInformation(clientName, requestId);
106
107                     PlaybackActionCommandReceived?.Invoke(null, new PlaybackActionCommandReceivedEventArgs(command));
108                 }
109             };
110             Native.SetPlaybackActionCommandReceivedCb(Handle, _playbackActionCommandCallback).
111                 ThrowIfError("Failed to init PlaybackActionCommandReceived event.");
112         }
113
114         private static void RegisterPlaybackPositionCommandReceivedEvent()
115         {
116             _playbackPositionCommandCallback = (clientName, requestId, playbackPosition, _) =>
117             {
118                 var command = new PlaybackPositionCommand(playbackPosition);
119                 command.SetResponseInformation(clientName, requestId);
120
121                 PlaybackPositionCommandReceived?.Invoke(null, new PlaybackPositionCommandReceivedEventArgs(command));
122             };
123             Native.SetPlaybackPosotionCommandReceivedCb(Handle, _playbackPositionCommandCallback).
124                 ThrowIfError("Failed to init PlaybackPositionCommandReceived event.");
125         }
126
127         private static void RegisterPlaylistCommandReceivedEvent()
128         {
129             _playlistCommandCallback = (clientName, requestId, playlistName, index, playbackCommand, playbackPosition, _) =>
130             {
131                 var command = new PlaylistCommand(playbackCommand.ToPublic(), playlistName, index, playbackPosition);
132                 command.SetResponseInformation(clientName, requestId);
133
134                 PlaylistCommandReceived?.Invoke(null, new PlaylistCommandReceivedEventArgs(command));
135             };
136             Native.SetPlaylistCommandReceivedCb(Handle, _playlistCommandCallback).
137                 ThrowIfError("Failed to init PlaylistCommandReceived event.");
138         }
139
140         private static void RegisterShuffleModeCommandReceivedEvent()
141         {
142             _shuffleModeCommandCallback = (clientName, requestId, mode, _) =>
143             {
144                 var command = new ShuffleModeCommand(mode == MediaControllerNativeShuffleMode.On ? true : false);
145                 command.SetResponseInformation(clientName, requestId);
146
147                 ShuffleModeCommandReceived?.Invoke(null, new ShuffleModeCommandReceivedEventArgs(command));
148             };
149             Native.SetShuffleModeCommandReceivedCb(Handle, _shuffleModeCommandCallback).
150                 ThrowIfError("Failed to init ShuffleModeCommandReceived event.");
151         }
152
153         private static void RegisterRepeatModeCommandReceivedEvent()
154         {
155             _repeatModeCommandCallback = (clientName, requestId, mode, _) =>
156             {
157                 var command = new RepeatModeCommand(mode.ToPublic());
158                 command.SetResponseInformation(clientName, requestId);
159
160                 RepeatModeCommandReceived?.Invoke(null, new RepeatModeCommandReceivedEventArgs(command));
161             };
162             Native.SetRepeatModeCommandReceivedCb(Handle, _repeatModeCommandCallback).
163                 ThrowIfError("Failed to init RepeatModeCommandReceived event.");
164         }
165
166         private static void RegisterCustomCommandReceivedEvent()
167         {
168             _customCommandCallback = (clientName, requestId, customCommand, bundleHandle, _) =>
169             {
170                 CustomCommand command = null;
171                 if (bundleHandle != IntPtr.Zero)
172                 {
173                     command = new CustomCommand(customCommand, new Bundle(new SafeBundleHandle(bundleHandle, true)));
174                 }
175                 else
176                 {
177                     command = new CustomCommand(customCommand);
178                 }
179
180                 command.SetResponseInformation(clientName, requestId);
181
182                 CustomCommandReceived?.Invoke(null, new CustomCommandReceivedEventArgs(command));
183             };
184             Native.SetCustomCommandReceivedCb(Handle, _customCommandCallback).
185                 ThrowIfError("Failed to init CustomCommandReceived event.");
186         }
187
188         private static SearchCommand CreateSearchCommandReceivedEventArgs(IntPtr searchHandle)
189         {
190             var searchConditions = new List<MediaControlSearchCondition>();
191
192             Native.SearchItemCallback searchItemCallback = (type, category, keyword, bundleHandle, _) =>
193             {
194                 Bundle bundle = null;
195
196                 if (bundleHandle != IntPtr.Zero)
197                 {
198                     bundle = new Bundle(new SafeBundleHandle(bundleHandle, true));
199                 }
200
201                 searchConditions.Add(new MediaControlSearchCondition(type, category, keyword, bundle));
202
203                 return true;
204             };
205
206             Native.ForeachSearchCondition(searchHandle, searchItemCallback).
207                 ThrowIfError("Failed to get search items.");
208
209             return new SearchCommand(searchConditions, searchHandle);
210         }
211
212         private static void RegisterSearchCommandReceivedEvent()
213         {
214             _searchCommandCallback = (clientName, requestId, searchHandle, _) =>
215             {
216                 var command = CreateSearchCommandReceivedEventArgs(searchHandle);
217
218                 command.SetResponseInformation(clientName, requestId);
219
220                 SearchCommandReceived?.Invoke(null, new SearchCommandReceivedEventArgs(command));
221             };
222             Native.SetSearchCommandReceivedCb(Handle, _searchCommandCallback).
223                 ThrowIfError("Failed to init SearchCommandReceived event.");
224         }
225
226         private static void RegisterCommandCompletedEvent()
227         {
228             _commandCompletedCallback = (clientName, requestId, result, bundleHandle, _) =>
229             {
230                 if (bundleHandle != IntPtr.Zero)
231                 {
232                     CommandCompleted?.Invoke(null, new CommandCompletedEventArgs(requestId, result, new Bundle(new SafeBundleHandle(bundleHandle, true))));
233                 }
234                 else
235                 {
236                     CommandCompleted?.Invoke(null, new CommandCompletedEventArgs(requestId, result));
237                 }
238             };
239             Native.SetEventReceivedCb(Handle, _commandCompletedCallback).
240                 ThrowIfError("Failed to init RegisterEventCompletedEvent.");
241         }
242     }
243 }