2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Runtime.InteropServices;
19 using Tizen.Applications;
20 using Native = Interop.MediaControllerServer;
22 namespace Tizen.Multimedia.MediaController
26 /// The MediaControllerServer class provides APIs required for media-controller-server.
29 /// http://tizen.org/privilege/mediacontroller.server
32 /// The MediaControllerServer APIs provides functions to update media information.
34 public class MediaControllerServer : IDisposable
36 internal IntPtr _handle = IntPtr.Zero;
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;
44 private bool IsValidHandle
46 get { return (_handle != IntPtr.Zero); }
49 private IntPtr SafeHandle
55 throw new ObjectDisposedException(nameof(MediaControllerServer), "Fail to operate MediaControllerServer");
63 /// The constructor of MediaControllerServer class.
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()
70 MediaControllerValidator.ThrowIfError(
71 Native.Create(out _handle), "Create server failed");
74 ~MediaControllerServer()
82 GC.SuppressFinalize(this);
85 protected virtual void Dispose(bool disposing)
91 // To be used if there are any other disposable objects
96 Native.Destroy(_handle);
97 _handle = IntPtr.Zero;
105 /// PlaybackStateCommandRecieved event is raised when client send command for playback
107 /// <since_tizen> 3 </since_tizen>
108 public event EventHandler<PlaybackStateCommandEventArgs> PlaybackStateCommand
112 if (_playbackCommand == null)
114 RegisterPlaybackCmdRecvEvent();
117 _playbackCommand += value;
123 _playbackCommand -= value;
124 if (_playbackCommand == null)
126 UnregisterPlaybackCmdRecvEvent();
132 /// CustomCommandRecieved event is raised when client send customized command
134 /// <since_tizen> 3 </since_tizen>
135 public event EventHandler<CustomCommandEventArgs> CustomCommand
139 if (_customCommand == null)
141 RegisterCustomCommandEvent();
144 _customCommand += value;
150 _customCommand -= value;
151 if (_customCommand == null)
153 UnregisterCustomCommandEvent();
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)
166 if (playback == null)
168 throw new ArgumentNullException("playback is null");
171 MediaControllerValidator.ThrowIfError(
172 Native.SetPlaybackState(SafeHandle, playback.State), "Set Playback state failed");
174 MediaControllerValidator.ThrowIfError(
175 Native.SetPlaybackPosition(SafeHandle, playback.Position), "Set Playback position failed");
177 MediaControllerValidator.ThrowIfError(
178 Native.UpdatePlayback(SafeHandle), "Update Playback failed");
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)
189 if (metadata == null)
191 throw new ArgumentNullException("metadata is null");
194 MediaControllerValidator.ThrowIfError(
195 Native.SetMetadata(SafeHandle, MediaControllerAttributes.Title, metadata.Title), "Set Title failed");
197 MediaControllerValidator.ThrowIfError(
198 Native.SetMetadata(SafeHandle, MediaControllerAttributes.Artist, metadata.Artist), "Set Artist failed");
200 MediaControllerValidator.ThrowIfError(
201 Native.SetMetadata(SafeHandle, MediaControllerAttributes.Album, metadata.Album), "Set Album failed");
203 MediaControllerValidator.ThrowIfError(
204 Native.SetMetadata(SafeHandle, MediaControllerAttributes.Author, metadata.Author), "Set Author failed");
206 MediaControllerValidator.ThrowIfError(
207 Native.SetMetadata(SafeHandle, MediaControllerAttributes.Genre, metadata.Genre), "Set Genre failed");
209 MediaControllerValidator.ThrowIfError(
210 Native.SetMetadata(SafeHandle, MediaControllerAttributes.Duration, metadata.Duration), "Set Duration failed");
212 MediaControllerValidator.ThrowIfError(
213 Native.SetMetadata(SafeHandle, MediaControllerAttributes.Date, metadata.Date), "Set Date failed");
215 MediaControllerValidator.ThrowIfError(
216 Native.SetMetadata(SafeHandle, MediaControllerAttributes.Copyright, metadata.Copyright), "Set Copyright failed");
218 MediaControllerValidator.ThrowIfError(
219 Native.SetMetadata(SafeHandle, MediaControllerAttributes.Description, metadata.Description), "Set Description failed");
221 MediaControllerValidator.ThrowIfError(
222 Native.SetMetadata(SafeHandle, MediaControllerAttributes.TrackNumber, metadata.TrackNumber), "Set TrackNumber failed");
224 MediaControllerValidator.ThrowIfError(
225 Native.SetMetadata(SafeHandle, MediaControllerAttributes.Picture, metadata.Picture), "Set Picture failed");
227 MediaControllerValidator.ThrowIfError(
228 Native.UpdateMetadata(SafeHandle), "UpdateMetadata Metadata failed");
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)
239 MediaControllerValidator.ThrowIfError(
240 Native.UpdateShuffleMode(SafeHandle, mode), "Update Shuffle Mode failed");
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)
251 MediaControllerValidator.ThrowIfError(
252 Native.UpdateRepeatMode(SafeHandle, mode), "Update Repeat Mode failed");
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)
265 MediaControllerValidator.ThrowIfError(
266 Native.SendCommandReply(SafeHandle, clientName, result, bundle.SafeBundleHandle), "Send reply for command failed");
269 private void RegisterPlaybackCmdRecvEvent()
271 _playbackCommandCallback = (IntPtr clientName, MediaControllerPlaybackState state, IntPtr userData) =>
273 PlaybackStateCommandEventArgs eventArgs = new PlaybackStateCommandEventArgs(Marshal.PtrToStringAnsi(clientName), state);
274 _playbackCommand?.Invoke(this, eventArgs);
276 Native.SetPlaybackStateCmdRecvCb(SafeHandle, _playbackCommandCallback, IntPtr.Zero);
279 private void UnregisterPlaybackCmdRecvEvent()
281 Native.UnsetPlaybackStateCmdRecvCb(SafeHandle);
284 private void RegisterCustomCommandEvent()
286 _customCommandCallback = (IntPtr clientName, IntPtr command, IntPtr bundle, IntPtr userData) =>
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);
293 Native.SetCustomCmdRecvCb(SafeHandle, _customCommandCallback, IntPtr.Zero);
296 private void UnregisterCustomCommandEvent()
298 Native.UnsetCustomCmdRecvCb(SafeHandle);