Release 8.0.0.15812
[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 capabilities are 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 capabilities are 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 display mode capabilities are updated.
263         /// </summary>
264         /// <since_tizen> 6 </since_tizen>
265         public event EventHandler<DisplayModeCapabilityUpdatedEventArgs> DisplayModeCapabilityUpdated;
266
267         internal void RaiseDisplayModeCapabilityUpdatedEvent(MediaControlNativeDisplayMode modes)
268         {
269             DisplayModeCapabilityUpdated?.Invoke(this, new DisplayModeCapabilityUpdatedEventArgs(modes.ToPublicList()));
270         }
271
272         /// <summary>
273         /// Occurs when the display rotation capabilities are updated.
274         /// </summary>
275         /// <since_tizen> 6 </since_tizen>
276         public event EventHandler<DisplayRotationCapabilityUpdatedEventArgs> DisplayRotationCapabilityUpdated;
277
278         internal void RaiseDisplayRotationCapabilityUpdatedEvent(MediaControlNativeDisplayRotation rotations)
279         {
280             DisplayRotationCapabilityUpdated?.Invoke(this, new DisplayRotationCapabilityUpdatedEventArgs(rotations.ToPublicList()));
281         }
282         #endregion
283
284
285         #region Command
286         /// <summary>
287         /// Occurs when the command is completed.
288         /// </summary>
289         /// <remarks>
290         /// User can match the command and this event using <see cref="CommandCompletedEventArgs.RequestId"/> field.
291         /// </remarks>
292         /// <since_tizen> 5 </since_tizen>
293         internal event EventHandler<CommandCompletedEventArgs> CommandCompleted;
294
295         internal void RaiseCommandCompletedEvent(string requestId, int result, IntPtr bundleHandle)
296         {
297             if (bundleHandle != IntPtr.Zero)
298             {
299                 CommandCompleted?.Invoke(this, new CommandCompletedEventArgs(requestId, result, new Bundle(new SafeBundleHandle(bundleHandle, true))));
300             }
301             else
302             {
303                 CommandCompleted?.Invoke(this, new CommandCompletedEventArgs(requestId, result));
304             }
305         }
306
307         /// <summary>
308         /// Occurs when a server sends custom event.
309         /// </summary>
310         /// <since_tizen> 5 </since_tizen>
311         public event EventHandler<CustomCommandReceivedEventArgs> CustomCommandReceived;
312
313         internal void RaiseCustomCommandReceivedEvent(CustomCommand command)
314         {
315             CustomCommandReceived?.Invoke(this, new CustomCommandReceivedEventArgs(command));
316         }
317         #endregion
318     }
319 }