08b7b477a4446f69a25523ec10cee34280637fbd
[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 Tizen.Applications;
19 using Native = Interop.MediaControllerClient;
20
21 namespace Tizen.Multimedia.Remoting
22 {
23     /// <summary>
24     /// Provides a means to to send commands to and handle events from media control server.
25     /// </summary>
26     /// <since_tizen> 4 </since_tizen>
27     public partial class MediaController
28     {
29         /// <summary>
30         /// Occurs when the server is stopped.
31         /// </summary>
32         /// <since_tizen> 4 </since_tizen>
33         public event EventHandler ServerStopped;
34
35         internal void RaiseStoppedEvent()
36         {
37             IsStopped = true;
38             ServerStopped?.Invoke(this, EventArgs.Empty);
39         }
40
41         /// <summary>
42         /// Occurs when the playback state is updated.
43         /// </summary>
44         /// <since_tizen> 4 </since_tizen>
45         public event EventHandler<PlaybackStateUpdatedEventArgs> PlaybackStateUpdated;
46
47         private PlaybackStateUpdatedEventArgs CreatePlaybackUpdatedEventArgs(IntPtr playbackHandle)
48         {
49             try
50             {
51                 Native.GetPlaybackState(playbackHandle, out var playbackCode).ThrowIfError("Failed to get state.");
52
53                 Native.GetPlaybackPosition(playbackHandle, out var position).ThrowIfError("Failed to get position.");
54
55                 return new PlaybackStateUpdatedEventArgs(playbackCode.ToPublic(), (long)position);
56             }
57             catch (Exception e)
58             {
59                 Log.Error(GetType().FullName, e.ToString());
60             }
61             return null;
62         }
63
64         internal void RaisePlaybackUpdatedEvent(IntPtr playbackHandle)
65         {
66             var eventHandler = PlaybackStateUpdated;
67
68             if (eventHandler == null)
69             {
70                 return;
71             }
72
73             var args = CreatePlaybackUpdatedEventArgs(playbackHandle);
74
75             if (args != null)
76             {
77                 eventHandler.Invoke(this, args);
78             }
79         }
80
81         /// <summary>
82         /// Occurs when the metadata is updated.
83         /// </summary>
84         /// <since_tizen> 4 </since_tizen>
85         public event EventHandler<MetadataUpdatedEventArgs> MetadataUpdated;
86
87         private MetadataUpdatedEventArgs CreateMetadataUpdatedEventArgs(IntPtr metadataHandle)
88         {
89             try
90             {
91                 return new MetadataUpdatedEventArgs(new MediaControlMetadata(metadataHandle));
92             }
93             catch (Exception e)
94             {
95                 Log.Error(GetType().FullName, e.ToString());
96             }
97             return null;
98         }
99
100         internal void RaiseMetadataUpdatedEvent(IntPtr metadataHandle)
101         {
102             var eventHandler = MetadataUpdated;
103
104             if (eventHandler == null)
105             {
106                 return;
107             }
108
109             var args = CreateMetadataUpdatedEventArgs(metadataHandle);
110
111             if (args != null)
112             {
113                 eventHandler.Invoke(this, args);
114             }
115         }
116
117         /// <summary>
118         /// Occurs when the shuffle mode is updated.
119         /// </summary>
120         /// <since_tizen> 4 </since_tizen>
121         public event EventHandler<ShuffleModeUpdatedEventArgs> ShuffleModeUpdated;
122
123         internal void RaiseShuffleModeUpdatedEvent(MediaControllerNativeShuffleMode mode)
124         {
125             ShuffleModeUpdated?.Invoke(this, new ShuffleModeUpdatedEventArgs(mode == MediaControllerNativeShuffleMode.On));
126         }
127
128         /// <summary>
129         /// Occurs when the repeat mode is updated.
130         /// </summary>
131         /// <since_tizen> 4 </since_tizen>
132         public event EventHandler<RepeatModeUpdatedEventArgs> RepeatModeUpdated;
133
134         internal void RaiseRepeatModeUpdatedEvent(MediaControlRepeatMode mode)
135         {
136             RepeatModeUpdated?.Invoke(this, new RepeatModeUpdatedEventArgs(mode));
137         }
138
139         /// <summary>
140         /// Occurs when the playlist is updated.
141         /// </summary>
142         /// <since_tizen> 5 </since_tizen>
143         public event EventHandler<PlaylistUpdatedEventArgs> PlaylistUpdated;
144
145         internal void RaisePlaylistUpdatedEvent(MediaControlPlaylistMode mode, string name)
146         {
147             PlaylistUpdated?.Invoke(this, new PlaylistUpdatedEventArgs(mode, name));
148         }
149
150         /// <summary>
151         /// Occurs when the command is completed.
152         /// </summary>
153         /// <remarks>
154         /// User can match the command and this event using <see cref="CommandCompletedEventArgs.RequestId"/> field.
155         /// </remarks>
156         /// <since_tizen> 5 </since_tizen>
157         internal event EventHandler<CommandCompletedEventArgs> CommandCompleted;
158
159         internal void RaiseCommandCompletedEvent(string requestId, MediaControllerError result, IntPtr bundleHandle)
160         {
161             if (bundleHandle != IntPtr.Zero)
162             {
163                 CommandCompleted?.Invoke(this, new CommandCompletedEventArgs(requestId, result, new Bundle(new SafeBundleHandle(bundleHandle, true))));
164             }
165             else
166             {
167                 CommandCompleted?.Invoke(this, new CommandCompletedEventArgs(requestId, result));
168             }
169         }
170     }
171 }