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 Tizen.Applications;
20 namespace Tizen.Multimedia.MediaController
24 /// The MediaControllerServer class provides APIs required for media-controller-server.
27 /// http://tizen.org/privilege/mediacontroller.server
30 /// The MediaControllerServer APIs provides functions to update media information.
32 public class MediaControllerServer : IDisposable
34 internal IntPtr _handle = IntPtr.Zero;
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;
43 /// The constructor of MediaControllerServer class.
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 ()
49 MediaControllerValidator.ThrowIfError(
50 Interop.MediaControllerServer.Create(out _handle), "Create server failed");
53 ~MediaControllerServer ()
61 GC.SuppressFinalize(this);
64 protected virtual void Dispose(bool disposing)
70 // To be used if there are any other disposable objects
72 if(_handle != IntPtr.Zero)
74 Interop.MediaControllerServer.Destroy(_handle);
75 _handle = IntPtr.Zero;
82 /// PlaybackStateCommandRecieved event is raised when client send command for playback
84 public event EventHandler<PlaybackStateCommandEventArgs> PlaybackStateCommand
88 if(_playbackCommand == null)
90 RegisterPlaybackCmdRecvEvent();
92 _playbackCommand += value;
97 _playbackCommand -= value;
98 if(_playbackCommand == null)
100 UnregisterPlaybackCmdRecvEvent();
106 /// CustomCommandRecieved event is raised when client send customized command
108 public event EventHandler<CustomCommandEventArgs> CustomCommand
112 if(_customCommand == null)
114 RegisterCustomCommandEvent();
116 _customCommand += value;
121 _customCommand -= value;
122 if(_customCommand == null)
124 UnregisterCustomCommandEvent();
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)
136 if (playback == null)
138 throw new ArgumentNullException("playback is null");
141 MediaControllerValidator.ThrowIfError(
142 Interop.MediaControllerServer.SetPlaybackState(_handle, playback.State), "Set Playback state failed");
144 MediaControllerValidator.ThrowIfError(
145 Interop.MediaControllerServer.SetPlaybackPosition(_handle, playback.Position), "Set Playback position failed");
147 MediaControllerValidator.ThrowIfError(
148 Interop.MediaControllerServer.UpdatePlayback(_handle), "Update Playback failed");
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)
158 if (metadata == null)
160 throw new ArgumentNullException("metadata is null");
163 MediaControllerValidator.ThrowIfError(
164 Interop.MediaControllerServer.SetMetadata(_handle, MediaControllerAttributes.Title, metadata.Title), "Set Title failed");
166 MediaControllerValidator.ThrowIfError(
167 Interop.MediaControllerServer.SetMetadata(_handle, MediaControllerAttributes.Artist, metadata.Artist), "Set Artist failed");
169 MediaControllerValidator.ThrowIfError(
170 Interop.MediaControllerServer.SetMetadata(_handle, MediaControllerAttributes.Album, metadata.Album), "Set Album failed");
172 MediaControllerValidator.ThrowIfError(
173 Interop.MediaControllerServer.SetMetadata(_handle, MediaControllerAttributes.Author, metadata.Author), "Set Author failed");
175 MediaControllerValidator.ThrowIfError(
176 Interop.MediaControllerServer.SetMetadata(_handle, MediaControllerAttributes.Genre, metadata.Genre), "Set Genre failed");
178 MediaControllerValidator.ThrowIfError(
179 Interop.MediaControllerServer.SetMetadata(_handle, MediaControllerAttributes.Duration, metadata.Duration), "Set Duration failed");
181 MediaControllerValidator.ThrowIfError(
182 Interop.MediaControllerServer.SetMetadata(_handle, MediaControllerAttributes.Date, metadata.Date), "Set Date failed");
184 MediaControllerValidator.ThrowIfError(
185 Interop.MediaControllerServer.SetMetadata(_handle, MediaControllerAttributes.Copyright, metadata.Copyright), "Set Copyright failed");
187 MediaControllerValidator.ThrowIfError(
188 Interop.MediaControllerServer.SetMetadata(_handle, MediaControllerAttributes.Description, metadata.Description), "Set Description failed");
190 MediaControllerValidator.ThrowIfError(
191 Interop.MediaControllerServer.SetMetadata(_handle, MediaControllerAttributes.TrackNumber, metadata.TrackNumber), "Set TrackNumber failed");
193 MediaControllerValidator.ThrowIfError(
194 Interop.MediaControllerServer.SetMetadata(_handle, MediaControllerAttributes.Picture, metadata.Picture), "Set Picture failed");
196 MediaControllerValidator.ThrowIfError(
197 Interop.MediaControllerServer.UpdateMetadata(_handle), "UpdateMetadata Metadata failed");
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)
207 MediaControllerValidator.ThrowIfError(
208 Interop.MediaControllerServer.UpdateShuffleMode(_handle, mode), "Update Shuffle Mode failed");
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)
218 MediaControllerValidator.ThrowIfError(
219 Interop.MediaControllerServer.UpdateRepeatMode(_handle, mode), "Update Repeat Mode failed");
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)
231 MediaControllerValidator.ThrowIfError(
232 Interop.MediaControllerServer.SendCommandReply(_handle, clientName, result, bundle.SafeBundleHandle), "Send reply for command failed");
235 private void RegisterPlaybackCmdRecvEvent()
237 _playbackCommandCallback = (string clientName, MediaControllerPlaybackState state, IntPtr userData) =>
239 PlaybackStateCommandEventArgs eventArgs = new PlaybackStateCommandEventArgs(clientName, state);
240 _playbackCommand?.Invoke(this, eventArgs);
242 Interop.MediaControllerServer.SetPlaybackStateCmdRecvCb(_handle, _playbackCommandCallback, IntPtr.Zero);
245 private void UnregisterPlaybackCmdRecvEvent()
247 Interop.MediaControllerServer.UnsetPlaybackStateCmdRecvCb(_handle);
250 private void RegisterCustomCommandEvent()
252 _customCommandCallback = (string clientName, string command, IntPtr bundle, IntPtr userData) =>
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);
259 Interop.MediaControllerServer.SetCustomCmdRecvCb(_handle, _customCommandCallback, IntPtr.Zero);
262 private void UnregisterCustomCommandEvent()
264 Interop.MediaControllerServer.UnsetCustomCmdRecvCb(_handle);