[MediaController] Code refactoring
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia / MediaController / MediaControllerServer.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
20 namespace Tizen.Multimedia.MediaController
21 {
22
23     /// <summary>
24     /// The MediaControllerServer class provides APIs required for media-controller-server.
25     /// </summary>
26     /// <privilege>
27     /// http://tizen.org/privilege/mediacontroller.server
28     /// </privilege>
29     /// <remarks>
30     /// The MediaControllerServer APIs provides functions to update media information.
31     /// </remarks>
32     public class MediaControllerServer : IDisposable
33     {
34         internal IntPtr _handle = IntPtr.Zero;
35
36         private bool _disposed = false;
37         private EventHandler<PlaybackStateCommandEventArgs> _playbackCommand;
38         private Interop.MediaControllerServer.PlaybackStateCommandRecievedCallback _playbackCommandCallback;
39         private EventHandler<CustomCommandEventArgs> _customCommand;
40         private Interop.MediaControllerServer.CustomCommandRecievedCallback _customCommandCallback;
41
42         /// <summary>
43         /// The constructor of MediaControllerServer class.
44         /// </summary>
45         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
46         /// <exception cref="UnauthorizedAccessException">Thrown when the access is denied for media controller client</exception>
47         public MediaControllerServer ()
48         {
49             MediaControllerValidator.ThrowIfError(
50                 Interop.MediaControllerServer.Create(out _handle), "Create  server failed");
51         }
52
53         ~MediaControllerServer ()
54         {
55             Dispose(false);
56         }
57
58         public void Dispose()
59         {
60             Dispose(true);
61             GC.SuppressFinalize(this);
62         }
63
64         protected virtual void Dispose(bool disposing)
65         {
66             if(!_disposed)
67             {
68                 if(disposing)
69                 {
70                     // To be used if there are any other disposable objects
71                 }
72                 if(_handle != IntPtr.Zero)
73                 {
74                     Interop.MediaControllerServer.Destroy(_handle);
75                     _handle = IntPtr.Zero;
76                 }
77                 _disposed = true;
78             }
79         }
80
81         /// <summary>
82         /// PlaybackStateCommandRecieved event is raised when client send command for playback
83         /// </summary>
84         public event EventHandler<PlaybackStateCommandEventArgs> PlaybackStateCommand
85         {
86             add
87             {
88                 if(_playbackCommand == null)
89                 {
90                     RegisterPlaybackCmdRecvEvent();
91                 }
92                 _playbackCommand += value;
93
94             }
95             remove
96             {
97                 _playbackCommand -= value;
98                 if(_playbackCommand == null)
99                 {
100                     UnregisterPlaybackCmdRecvEvent();
101                 }
102             }
103         }
104
105         /// <summary>
106         /// CustomCommandRecieved event is raised when client send customized command
107         /// </summary>
108         public event EventHandler<CustomCommandEventArgs> CustomCommand
109         {
110             add
111             {
112                 if(_customCommand == null)
113                 {
114                     RegisterCustomCommandEvent();
115                 }
116                 _customCommand += value;
117
118             }
119             remove
120             {
121                 _customCommand -= value;
122                 if(_customCommand == null)
123                 {
124                     UnregisterCustomCommandEvent();
125                 }
126             }
127         }
128
129         /// <summary>
130         /// Update playback state and playback position</summary>
131         /// <param name="playback"> playback state and playback position  </param>
132         /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
133         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
134         public void UpdatePlayback(MediaControllerPlayback playback)
135         {
136             if (playback == null)
137             {
138                 throw new ArgumentNullException("playback is null");
139             }
140
141             MediaControllerValidator.ThrowIfError(
142                 Interop.MediaControllerServer.SetPlaybackState(_handle, playback.State), "Set Playback state failed");
143
144             MediaControllerValidator.ThrowIfError(
145                 Interop.MediaControllerServer.SetPlaybackPosition(_handle, playback.Position), "Set Playback position failed");
146
147             MediaControllerValidator.ThrowIfError(
148                 Interop.MediaControllerServer.UpdatePlayback(_handle), "Update Playback failed");
149         }
150
151         /// <summary>
152         /// Update metadata information </summary>
153         /// <param name="metadata"> metadata information  </param>
154         /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
155         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
156         public void UpdateMetadata(MediaControllerMetadata metadata)
157         {
158             if (metadata == null)
159             {
160                 throw new ArgumentNullException("metadata is null");
161             }
162
163             MediaControllerValidator.ThrowIfError(
164                 Interop.MediaControllerServer.SetMetadata(_handle, MediaControllerAttributes.Title, metadata.Title), "Set Title failed");
165
166             MediaControllerValidator.ThrowIfError(
167                 Interop.MediaControllerServer.SetMetadata(_handle, MediaControllerAttributes.Artist, metadata.Artist), "Set Artist failed");
168
169             MediaControllerValidator.ThrowIfError(
170                 Interop.MediaControllerServer.SetMetadata(_handle, MediaControllerAttributes.Album, metadata.Album), "Set Album failed");
171
172             MediaControllerValidator.ThrowIfError(
173                 Interop.MediaControllerServer.SetMetadata(_handle, MediaControllerAttributes.Author, metadata.Author), "Set Author failed");
174
175             MediaControllerValidator.ThrowIfError(
176                 Interop.MediaControllerServer.SetMetadata(_handle, MediaControllerAttributes.Genre, metadata.Genre), "Set Genre failed");
177
178             MediaControllerValidator.ThrowIfError(
179                 Interop.MediaControllerServer.SetMetadata(_handle, MediaControllerAttributes.Duration, metadata.Duration), "Set Duration failed");
180
181             MediaControllerValidator.ThrowIfError(
182                 Interop.MediaControllerServer.SetMetadata(_handle, MediaControllerAttributes.Date, metadata.Date), "Set Date failed");
183
184             MediaControllerValidator.ThrowIfError(
185                 Interop.MediaControllerServer.SetMetadata(_handle, MediaControllerAttributes.Copyright, metadata.Copyright), "Set Copyright failed");
186
187             MediaControllerValidator.ThrowIfError(
188                 Interop.MediaControllerServer.SetMetadata(_handle, MediaControllerAttributes.Description, metadata.Description), "Set Description failed");
189
190             MediaControllerValidator.ThrowIfError(
191                 Interop.MediaControllerServer.SetMetadata(_handle, MediaControllerAttributes.TrackNumber, metadata.TrackNumber), "Set TrackNumber failed");
192
193             MediaControllerValidator.ThrowIfError(
194                 Interop.MediaControllerServer.SetMetadata(_handle, MediaControllerAttributes.Picture, metadata.Picture), "Set Picture failed");
195
196             MediaControllerValidator.ThrowIfError(
197                 Interop.MediaControllerServer.UpdateMetadata(_handle), "UpdateMetadata Metadata failed");
198         }
199
200         /// <summary>
201         /// Update shuffle mode </summary>
202         /// <param name="mode"> shuffle mode  </param>
203         /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
204         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
205         public void UpdateShuffleMode(MediaControllerShuffleMode mode)
206         {
207             MediaControllerValidator.ThrowIfError(
208                 Interop.MediaControllerServer.UpdateShuffleMode(_handle, mode), "Update Shuffle Mode failed");
209         }
210
211         /// <summary>
212         /// Update repeat mode </summary>
213         /// <param name="mode"> repeat mode  </param>
214         /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
215         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
216         public void UpdateRepeatMode(MediaControllerRepeatMode mode)
217         {
218             MediaControllerValidator.ThrowIfError(
219                 Interop.MediaControllerServer.UpdateRepeatMode(_handle, mode), "Update Repeat Mode failed");
220         }
221
222         /// <summary>
223         /// Send reply for command from server to client </summary>
224         /// <param name="clientName"> client name to recieve reply  </param>
225         /// <param name="result"> result to run command  </param>
226         /// <param name="bundleData"> Bundle data  </param>
227         /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
228         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
229         public void SendCustomCommandReply(string clientName, int result, Bundle bundle)
230         {
231             MediaControllerValidator.ThrowIfError(
232                 Interop.MediaControllerServer.SendCommandReply(_handle, clientName, result, bundle.SafeBundleHandle), "Send reply for command failed");
233         }
234
235         private void RegisterPlaybackCmdRecvEvent()
236         {
237             _playbackCommandCallback = (string clientName, MediaControllerPlaybackState state, IntPtr userData) =>
238             {
239                 PlaybackStateCommandEventArgs eventArgs = new PlaybackStateCommandEventArgs(clientName, state);
240                 _playbackCommand?.Invoke(this, eventArgs);
241             };
242             Interop.MediaControllerServer.SetPlaybackStateCmdRecvCb(_handle, _playbackCommandCallback, IntPtr.Zero);
243         }
244
245         private void UnregisterPlaybackCmdRecvEvent()
246         {
247             Interop.MediaControllerServer.UnsetPlaybackStateCmdRecvCb(_handle);
248         }
249
250         private void RegisterCustomCommandEvent()
251         {
252             _customCommandCallback = (string clientName, string command, IntPtr bundle, IntPtr userData) =>
253             {
254                 SafeBundleHandle bundleHandle = new SafeBundleHandle(bundle, true);
255                 Applications.Bundle _bundle = new Bundle(bundleHandle);
256                 CustomCommandEventArgs eventArgs = new CustomCommandEventArgs(clientName, command, _bundle);
257                 _customCommand?.Invoke(this, eventArgs);
258             };
259             Interop.MediaControllerServer.SetCustomCmdRecvCb(_handle, _customCommandCallback, IntPtr.Zero);
260         }
261
262         private void UnregisterCustomCommandEvent()
263         {
264             Interop.MediaControllerServer.UnsetCustomCmdRecvCb(_handle);
265         }
266     }
267 }
268