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