[MediaController] Add new capability APIs (#5472)
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Remoting / MediaController / MediaController.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 System;
18 using System.Linq;
19 using System.Collections.Generic;
20 using Tizen.Applications;
21 using Native = Interop.MediaControllerClient;
22
23 namespace Tizen.Multimedia.Remoting
24 {
25     /// <summary>
26     /// Provides a means to to send commands to and handle events from media control server.
27     /// </summary>
28     /// <since_tizen> 4 </since_tizen>
29     public partial class MediaController
30     {
31         /// <summary>
32         /// Occurs when the server is stopped.
33         /// </summary>
34         /// <since_tizen> 4 </since_tizen>
35         public event EventHandler ServerStopped;
36
37         internal void RaiseStoppedEvent()
38         {
39             IsStopped = true;
40             ServerStopped?.Invoke(this, EventArgs.Empty);
41         }
42
43         #region Updated event
44         /// <summary>
45         /// Occurs when the playback state is updated.
46         /// </summary>
47         /// <since_tizen> 4 </since_tizen>
48         public event EventHandler<PlaybackStateUpdatedEventArgs> PlaybackStateUpdated;
49
50         private PlaybackStateUpdatedEventArgs CreatePlaybackUpdatedEventArgs(IntPtr playbackHandle)
51         {
52             try
53             {
54                 Native.GetPlaybackState(playbackHandle, out var playbackCode).ThrowIfError("Failed to get state.");
55
56                 Native.GetPlaybackPosition(playbackHandle, out var position).ThrowIfError("Failed to get position.");
57
58                 return new PlaybackStateUpdatedEventArgs(playbackCode.ToPublic(), (long)position);
59             }
60             catch (Exception e)
61             {
62                 Log.Error(GetType().FullName, e.ToString());
63             }
64             return null;
65         }
66
67         internal void RaisePlaybackUpdatedEvent(IntPtr playbackHandle)
68         {
69             var eventHandler = PlaybackStateUpdated;
70
71             if (eventHandler == null)
72             {
73                 return;
74             }
75
76             var args = CreatePlaybackUpdatedEventArgs(playbackHandle);
77
78             if (args != null)
79             {
80                 eventHandler.Invoke(this, args);
81             }
82         }
83
84         /// <summary>
85         /// Occurs when the playlist is updated.
86         /// </summary>
87         /// <since_tizen> 5 </since_tizen>
88         public event EventHandler<PlaylistUpdatedEventArgs> PlaylistUpdated;
89
90         internal void RaisePlaylistUpdatedEvent(MediaControlPlaylistMode mode, string name, IntPtr playlistHandle)
91         {
92             PlaylistUpdated?.Invoke(this, new PlaylistUpdatedEventArgs(mode, name, new MediaControlPlaylist(playlistHandle)));
93         }
94
95         /// <summary>
96         /// Occurs when the metadata is updated.
97         /// </summary>
98         /// <since_tizen> 4 </since_tizen>
99         public event EventHandler<MetadataUpdatedEventArgs> MetadataUpdated;
100
101         private MetadataUpdatedEventArgs CreateMetadataUpdatedEventArgs(IntPtr metadataHandle)
102         {
103             try
104             {
105                 return new MetadataUpdatedEventArgs(new MediaControlMetadata(metadataHandle));
106             }
107             catch (Exception e)
108             {
109                 Log.Error(GetType().FullName, e.ToString());
110             }
111             return null;
112         }
113
114         internal void RaiseMetadataUpdatedEvent(IntPtr metadataHandle)
115         {
116             var eventHandler = MetadataUpdated;
117
118             if (eventHandler == null)
119             {
120                 return;
121             }
122
123             var args = CreateMetadataUpdatedEventArgs(metadataHandle);
124
125             if (args != null)
126             {
127                 eventHandler.Invoke(this, args);
128             }
129         }
130
131         /// <summary>
132         /// Occurs when the shuffle mode is updated.
133         /// </summary>
134         /// <since_tizen> 4 </since_tizen>
135         public event EventHandler<ShuffleModeUpdatedEventArgs> ShuffleModeUpdated;
136
137         internal void RaiseShuffleModeUpdatedEvent(MediaControllerNativeShuffleMode mode)
138         {
139             ShuffleModeUpdated?.Invoke(this, new ShuffleModeUpdatedEventArgs(mode == MediaControllerNativeShuffleMode.On));
140         }
141
142         /// <summary>
143         /// Occurs when the repeat mode is updated.
144         /// </summary>
145         /// <since_tizen> 4 </since_tizen>
146         public event EventHandler<RepeatModeUpdatedEventArgs> RepeatModeUpdated;
147
148         internal void RaiseRepeatModeUpdatedEvent(MediaControlRepeatMode mode)
149         {
150             RepeatModeUpdated?.Invoke(this, new RepeatModeUpdatedEventArgs(mode));
151         }
152
153         /// <summary>
154         /// Occurs when the subtitle mode is updated.
155         /// </summary>
156         /// <since_tizen> 6 </since_tizen>
157         public event EventHandler<SubtitleModeUpdatedEventArgs> SubtitleModeUpdated;
158         internal void RaiseSubtitleModeUpdatedEvent(bool isEnabled)
159         {
160             SubtitleModeUpdated?.Invoke(this, new SubtitleModeUpdatedEventArgs(isEnabled));
161         }
162
163         /// <summary>
164         /// Occurs when the 360 mode is updated.
165         /// </summary>
166         /// <since_tizen> 6 </since_tizen>
167         public event EventHandler<Mode360UpdatedEventArgs> Mode360Updated;
168         internal void RaiseMode360UpdatedEvent(bool isEnabled)
169         {
170             Mode360Updated?.Invoke(this, new Mode360UpdatedEventArgs(isEnabled));
171         }
172
173         /// <summary>
174         /// Occurs when the display mode is updated.
175         /// </summary>
176         /// <since_tizen> 6 </since_tizen>
177         public event EventHandler<DisplayModeUpdatedEventArgs> DisplayModeUpdated;
178         internal void RaiseDisplayModeUpdatedEvent(MediaControlNativeDisplayMode mode)
179         {
180             DisplayModeUpdated?.Invoke(this, new DisplayModeUpdatedEventArgs(mode.ToPublic()));
181         }
182
183         /// <summary>
184         /// Occurs when the display rotation is updated.
185         /// </summary>
186         /// <since_tizen> 6 </since_tizen>
187         public event EventHandler<DisplayRotationUpdatedEventArgs> DisplayRotationUpdated;
188         internal void RaiseDisplayRotationUpdatedEvent(MediaControlNativeDisplayRotation rotation)
189         {
190             DisplayRotationUpdated?.Invoke(this, new DisplayRotationUpdatedEventArgs(rotation.ToPublic()));
191         }
192         #endregion
193
194
195         #region Capability updated event
196         /// <summary>
197         /// Occurs when the playback capabilities are updated.
198         /// </summary>
199         /// <since_tizen> 5 </since_tizen>
200         public event EventHandler<PlaybackCapabilityUpdatedEventArgs> PlaybackCapabilityUpdated;
201
202         private PlaybackCapabilityUpdatedEventArgs CreatePlaybackCapabilityUpdatedEventArgs(IntPtr playbackCapaHandle)
203         {
204             var capabilities = new Dictionary<MediaControlPlaybackCommand, MediaControlCapabilitySupport>();
205             try
206             {
207                 foreach (MediaControllerNativePlaybackAction action in Enum.GetValues(typeof(MediaControllerNativePlaybackAction)))
208                 {
209                     Native.GetPlaybackCapability(playbackCapaHandle, action, out MediaControlCapabilitySupport support);
210                     capabilities.Add(action.ToPublic(), support);
211                 }
212
213                 return new PlaybackCapabilityUpdatedEventArgs(capabilities);
214             }
215             catch (Exception e)
216             {
217                 Log.Error(GetType().FullName, e.ToString());
218             }
219             return null;
220         }
221
222         internal void RaisePlaybackCapabilityUpdatedEvent(IntPtr playbackCapaHandle)
223         {
224             var eventHandler = PlaybackCapabilityUpdated;
225
226             if (eventHandler == null)
227             {
228                 return;
229             }
230
231             var args = CreatePlaybackCapabilityUpdatedEventArgs(playbackCapaHandle);
232
233             if (args != null)
234             {
235                 eventHandler.Invoke(this, args);
236             }
237         }
238
239         /// <summary>
240         /// Occurs when the repeat mode capability is updated.
241         /// </summary>
242         /// <since_tizen> 5 </since_tizen>
243         public event EventHandler<RepeatModeCapabilityUpdatedEventArgs> RepeatModeCapabilityUpdated;
244
245         internal void RaiseRepeatModeCapabilityUpdatedEvent(MediaControlCapabilitySupport support)
246         {
247             RepeatModeCapabilityUpdated?.Invoke(this, new RepeatModeCapabilityUpdatedEventArgs(support));
248         }
249
250         /// <summary>
251         /// Occurs when the shuffle mode capability is updated.
252         /// </summary>
253         /// <since_tizen> 5 </since_tizen>
254         public event EventHandler<ShuffleModeCapabilityUpdatedEventArgs> ShuffleModeCapabilityUpdated;
255
256         internal void RaiseShuffleModeCapabilityUpdatedEvent(MediaControlCapabilitySupport support)
257         {
258             ShuffleModeCapabilityUpdated?.Invoke(this, new ShuffleModeCapabilityUpdatedEventArgs(support));
259         }
260
261         /// <summary>
262         /// Occurs when the playback position capability is updated.
263         /// </summary>
264         /// <since_tizen> 11 </since_tizen>
265         public event EventHandler<PlaybackPositionCapabilityUpdatedEventArgs> PlaybackPositionCapabilityUpdated;
266
267         internal void RaisePlaybackPositionCapabilityUpdatedEvent(MediaControlCapabilitySupport support)
268         {
269             PlaybackPositionCapabilityUpdated?.Invoke(this, new PlaybackPositionCapabilityUpdatedEventArgs(support));
270         }
271
272         /// <summary>
273         /// Occurs when the playlist capability is updated.
274         /// </summary>
275         /// <since_tizen> 11 </since_tizen>
276         public event EventHandler<PlaylistCapabilityUpdatedEventArgs> PlaylistCapabilityUpdated;
277
278         internal void RaisePlaylistCapabilityUpdatedEvent(MediaControlCapabilitySupport support)
279         {
280             PlaylistCapabilityUpdated?.Invoke(this, new PlaylistCapabilityUpdatedEventArgs(support));
281         }
282
283         /// <summary>
284         /// Occurs when the custom command capability is updated.
285         /// </summary>
286         /// <since_tizen> 11 </since_tizen>
287         public event EventHandler<CustomCommandCapabilityUpdatedEventArgs> CustomCommandCapabilityUpdated;
288
289         internal void RaiseCustomCommandCapabilityUpdatedEvent(MediaControlCapabilitySupport support)
290         {
291             CustomCommandCapabilityUpdated?.Invoke(this, new CustomCommandCapabilityUpdatedEventArgs(support));
292         }
293
294         /// <summary>
295         /// Occurs when the search capability is updated.
296         /// </summary>
297         /// <since_tizen> 11 </since_tizen>
298         public event EventHandler<SearchCapabilityUpdatedEventArgs> SearchCapabilityUpdated;
299
300         internal void RaiseSearchCapabilityUpdatedEvent(MediaControlCapabilitySupport support)
301         {
302             SearchCapabilityUpdated?.Invoke(this, new SearchCapabilityUpdatedEventArgs(support));
303         }
304
305         /// <summary>
306         /// Occurs when the subtitle capability is updated.
307         /// </summary>
308         /// <since_tizen> 11 </since_tizen>
309         public event EventHandler<SubtitleCapabilityUpdatedEventArgs> SubtitleCapabilityUpdated;
310
311         internal void RaiseSubtitleCapabilityUpdatedEvent(MediaControlCapabilitySupport support)
312         {
313             SubtitleCapabilityUpdated?.Invoke(this, new SubtitleCapabilityUpdatedEventArgs(support));
314         }
315
316         /// <summary>
317         /// Occurs when the mode360 capability is updated.
318         /// </summary>
319         /// <since_tizen> 11 </since_tizen>
320         public event EventHandler<Mode360CapabilityUpdatedEventArgs> Mode360CapabilityUpdated;
321
322         internal void RaiseMode360CapabilityUpdatedEvent(MediaControlCapabilitySupport support)
323         {
324             Mode360CapabilityUpdated?.Invoke(this, new Mode360CapabilityUpdatedEventArgs(support));
325         }
326
327         /// <summary>
328         /// Occurs when the display mode capability is updated.
329         /// </summary>
330         /// <since_tizen> 6 </since_tizen>
331         public event EventHandler<DisplayModeCapabilityUpdatedEventArgs> DisplayModeCapabilityUpdated;
332
333         internal void RaiseDisplayModeCapabilityUpdatedEvent(MediaControlNativeDisplayMode modes)
334         {
335             DisplayModeCapabilityUpdated?.Invoke(this, new DisplayModeCapabilityUpdatedEventArgs(modes.ToPublicList()));
336         }
337
338         /// <summary>
339         /// Occurs when the display rotation capability is updated.
340         /// </summary>
341         /// <since_tizen> 6 </since_tizen>
342         public event EventHandler<DisplayRotationCapabilityUpdatedEventArgs> DisplayRotationCapabilityUpdated;
343
344         internal void RaiseDisplayRotationCapabilityUpdatedEvent(MediaControlNativeDisplayRotation rotations)
345         {
346             DisplayRotationCapabilityUpdated?.Invoke(this, new DisplayRotationCapabilityUpdatedEventArgs(rotations.ToPublicList()));
347         }
348         #endregion
349
350
351         #region Command
352         /// <summary>
353         /// Occurs when the command is completed.
354         /// </summary>
355         /// <remarks>
356         /// User can match the command and this event using <see cref="CommandCompletedEventArgs.RequestId"/> field.
357         /// </remarks>
358         /// <since_tizen> 5 </since_tizen>
359         internal event EventHandler<CommandCompletedEventArgs> CommandCompleted;
360
361         internal void RaiseCommandCompletedEvent(string requestId, int result, IntPtr bundleHandle)
362         {
363             if (bundleHandle != IntPtr.Zero)
364             {
365                 CommandCompleted?.Invoke(this, new CommandCompletedEventArgs(requestId, result, new Bundle(new SafeBundleHandle(bundleHandle, true))));
366             }
367             else
368             {
369                 CommandCompleted?.Invoke(this, new CommandCompletedEventArgs(requestId, result));
370             }
371         }
372
373         /// <summary>
374         /// Occurs when a server sends custom event.
375         /// </summary>
376         /// <since_tizen> 5 </since_tizen>
377         public event EventHandler<CustomCommandReceivedEventArgs> CustomCommandReceived;
378
379         internal void RaiseCustomCommandReceivedEvent(CustomCommand command)
380         {
381             CustomCommandReceived?.Invoke(this, new CustomCommandReceivedEventArgs(command));
382         }
383         #endregion
384     }
385 }