[MediaController] Add new APIs for event, capabilities and search. (#468)
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Remoting / MediaController / MediaControlServer.Events.cs
1 /*
2  * Copyright (c) 2016 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 RegisterPlaybackCommandReceivedEvent()
92         {
93             _playbackCommandCallback = (clientName, playbackCode, _) =>
94             {
95                 PlaybackCommandReceived?.Invoke(null, new PlaybackCommandReceivedEventArgs(clientName, playbackCode.ToPublic()));
96             };
97             Native.SetPlaybackStateCommandReceivedCb(Handle, _playbackCommandCallback).
98                 ThrowIfError("Failed to init PlaybackStateCommandReceived event.");
99         }
100
101         private static void RegisterPlaybackActionCommandReceivedEvent()
102         {
103             _playbackActionCommandCallback = (clientName, requestId, playbackCommand, _) =>
104             {
105                 var command = new PlaybackCommand(playbackCommand.ToPublic());
106                 command.SetResponseInformation(clientName, requestId);
107
108                 PlaybackActionCommandReceived?.Invoke(null, new PlaybackActionCommandReceivedEventArgs(command));
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                 if (bundleHandle != IntPtr.Zero)
196                 {
197                     bundle = new Bundle(new SafeBundleHandle(bundleHandle, true));
198                 }
199
200                 searchConditions.Add(new MediaControlSearchCondition(type, category, keyword, bundle));
201
202                 return true;
203             };
204             Native.ForeachSearchCondition(searchHandle, searchItemCallback).
205                 ThrowIfError("Failed to get search items.");
206
207             return new SearchCommand(searchConditions, searchHandle);
208         }
209
210         private static void RegisterSearchCommandReceivedEvent()
211         {
212             _searchCommandCallback = (clientName, requestId, searchHandle, _) =>
213             {
214                 var command = CreateSearchCommandReceivedEventArgs(searchHandle);
215
216                 command.SetResponseInformation(clientName, requestId);
217
218                 SearchCommandReceived?.Invoke(null, new SearchCommandReceivedEventArgs(command));
219             };
220             Native.SetSearchCommandReceivedCb(Handle, _searchCommandCallback).
221                 ThrowIfError("Failed to init SearchCommandReceived event.");
222         }
223
224         private static void RegisterCommandCompletedEvent()
225         {
226             _commandCompletedCallback = (clientName, requestId, result, bundleHandle, _) =>
227             {
228                 if (bundleHandle != IntPtr.Zero)
229                 {
230                     CommandCompleted?.Invoke(null, new CommandCompletedEventArgs(requestId, result, new Bundle(new SafeBundleHandle(bundleHandle, true))));
231                 }
232                 else
233                 {
234                     CommandCompleted?.Invoke(null, new CommandCompletedEventArgs(requestId, result));
235                 }
236             };
237             Native.SetEventReceivedCb(Handle, _commandCompletedCallback).
238                 ThrowIfError("Failed to init RegisterEventCompletedEvent.");
239         }
240     }
241 }