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.Diagnostics;
19 using Native = Interop.MediaControllerClient;
21 namespace Tizen.Multimedia.Remoting
24 /// Provides a means to to send commands to and handle events from media control server.
26 /// <since_tizen> 4 </since_tizen>
27 public class MediaController
29 internal MediaController(MediaControllerManager manager, string serverAppId)
31 Debug.Assert(manager != null);
32 Debug.Assert(serverAppId != null);
35 ServerAppId = serverAppId;
38 private MediaControllerManager Manager { get; }
41 /// Gets the application id of the server.
43 /// <value>The server application id.</value>
44 /// <since_tizen> 4 </since_tizen>
45 public string ServerAppId { get; }
48 /// Gets a value indicating whether the sever has been stopped.
50 /// <value>true if the server has been stopped; otherwise, false.</value>
51 /// <since_tizen> 4 </since_tizen>
58 private void ThrowIfStopped()
62 throw new InvalidOperationException("The server has already been stopped.");
67 /// Occurs when the server is stopped.
69 /// <since_tizen> 4 </since_tizen>
70 public event EventHandler ServerStopped;
72 internal void RaiseStoppedEvent()
75 ServerStopped?.Invoke(this, EventArgs.Empty);
79 /// Occurs when the playback state is updated.
81 /// <since_tizen> 4 </since_tizen>
82 public event EventHandler<PlaybackStateUpdatedEventArgs> PlaybackStateUpdated;
84 private PlaybackStateUpdatedEventArgs CreatePlaybackUpdatedEventArgs(IntPtr playbackHandle)
88 Native.GetPlaybackState(playbackHandle, out var playbackCode).ThrowIfError("Failed to get state.");
90 Native.GetPlaybackPosition(playbackHandle, out var position).ThrowIfError("Failed to get position.");
92 return new PlaybackStateUpdatedEventArgs(playbackCode.ToState(), (long)position);
96 Log.Error(GetType().FullName, e.ToString());
101 internal void RaisePlaybackUpdatedEvent(IntPtr playbackHandle)
103 var eventHandler = PlaybackStateUpdated;
105 if (eventHandler == null)
110 var args = CreatePlaybackUpdatedEventArgs(playbackHandle);
114 eventHandler.Invoke(this, args);
119 /// Occurs when the metadata is updated.
121 /// <since_tizen> 4 </since_tizen>
122 public event EventHandler<MetadataUpdatedEventArgs> MetadataUpdated;
124 private MetadataUpdatedEventArgs CreateMetadataUpdatedEventArgs(IntPtr metadataHandle)
128 return new MetadataUpdatedEventArgs(new MediaControlMetadata(metadataHandle));
132 Log.Error(GetType().FullName, e.ToString());
137 internal void RaiseMetadataUpdatedEvent(IntPtr metadataHandle)
139 var eventHandler = MetadataUpdated;
141 if (eventHandler == null)
146 var args = CreateMetadataUpdatedEventArgs(metadataHandle);
150 eventHandler.Invoke(this, args);
155 /// Occurs when the shuffle mode is updated.
157 /// <since_tizen> 4 </since_tizen>
158 public event EventHandler<ShuffleModeUpdatedEventArgs> ShuffleModeUpdated;
160 internal void RaiseShuffleModeUpdatedEvent(MediaControllerShuffleMode mode)
162 ShuffleModeUpdated?.Invoke(this, new ShuffleModeUpdatedEventArgs(mode == MediaControllerShuffleMode.On));
166 /// Occurs when the repeat mode is updated.
168 /// <since_tizen> 4 </since_tizen>
169 public event EventHandler<RepeatModeUpdatedEventArgs> RepeatModeUpdated;
171 internal void RaiseRepeatModeUpdatedEvent(MediaControlRepeatMode mode)
173 RepeatModeUpdated?.Invoke(this, new RepeatModeUpdatedEventArgs(mode));
177 /// Returns the playback state set by the server.
179 /// <returns>The playback state.</returns>
180 /// <exception cref="InvalidOperationException">
181 /// The server has already been stopped.<br/>
183 /// An internal error occurs.
185 /// <exception cref="ObjectDisposedException">The <see cref="MediaControllerManager"/> has already been disposed of.</exception>
186 /// <seealso cref="MediaControlServer.SetPlaybackState(MediaControlPlaybackState, long)"/>
187 /// <since_tizen> 4 </since_tizen>
188 public MediaControlPlaybackState GetPlaybackState()
192 IntPtr playbackHandle = IntPtr.Zero;
196 Native.GetServerPlayback(Manager.Handle, ServerAppId, out playbackHandle).ThrowIfError("Failed to get playback.");
198 Native.GetPlaybackState(playbackHandle, out var playbackCode).ThrowIfError("Failed to get state.");
200 return playbackCode.ToState();
204 if (playbackHandle != IntPtr.Zero)
206 Native.DestroyPlayback(playbackHandle);
212 /// Returns the playback position set by the server.
214 /// <returns>The playback position in milliseconds.</returns>
215 /// <exception cref="InvalidOperationException">
216 /// The server has already been stopped.<br/>
218 /// An internal error occurs.
220 /// <exception cref="ObjectDisposedException">The <see cref="MediaControllerManager"/> has already been disposed of.</exception>
221 /// <seealso cref="MediaControlServer.SetPlaybackState(MediaControlPlaybackState, long)"/>
222 /// <since_tizen> 4 </since_tizen>
223 public long GetPlaybackPosition()
227 IntPtr playbackHandle = IntPtr.Zero;
231 Native.GetServerPlayback(Manager.Handle, ServerAppId, out playbackHandle).ThrowIfError("Failed to get playback.");
233 Native.GetPlaybackPosition(playbackHandle, out var position).ThrowIfError("Failed to get position.");
235 return (long)position;
239 if (playbackHandle != IntPtr.Zero)
241 Native.DestroyPlayback(playbackHandle);
247 /// Returns the metadata set by the server.
249 /// <returns>The metadata.</returns>
250 /// <exception cref="InvalidOperationException">
251 /// The server has already been stopped.<br/>
253 /// An internal error occurs.
255 /// <exception cref="ObjectDisposedException">The <see cref="MediaControllerManager"/> has already been disposed of.</exception>
256 /// <seealso cref="MediaControlServer.SetMetadata(MediaControlMetadata)"/>
257 /// <since_tizen> 4 </since_tizen>
258 public MediaControlMetadata GetMetadata()
262 IntPtr metadataHandle = IntPtr.Zero;
266 Native.GetServerMetadata(Manager.Handle, ServerAppId, out metadataHandle).
267 ThrowIfError("Failed to get metadata.");
269 return new MediaControlMetadata(metadataHandle);
273 if (metadataHandle != IntPtr.Zero)
275 Native.DestroyMetadata(metadataHandle);
281 /// Returns whether the shuffle mode is enabled.
283 /// <returns>A value indicating whether the shuffle mode is enabled.</returns>
284 /// <exception cref="InvalidOperationException">
285 /// The server has already been stopped.<br/>
287 /// An internal error occurs.
289 /// <exception cref="ObjectDisposedException">The <see cref="MediaControllerManager"/> has already been disposed of.</exception>
290 /// <seealso cref="MediaControlServer.SetShuffleModeEnabled(bool)"/>
291 /// <since_tizen> 4 </since_tizen>
292 public bool IsShuffleModeEnabled()
296 Native.GetServerShuffleMode(Manager.Handle, ServerAppId, out var shuffleMode).
297 ThrowIfError("Failed to get shuffle mode state.");
299 return shuffleMode == MediaControllerShuffleMode.On;
303 /// Returns the repeat mode.
305 /// <returns>A <see cref="MediaControlRepeatMode"/> set by the server.</returns>
306 /// <exception cref="InvalidOperationException">
307 /// The server has already been stopped.<br/>
309 /// An internal error occurs.
311 /// <exception cref="ObjectDisposedException">The <see cref="MediaControllerManager"/> has already been disposed of.</exception>
312 /// <seealso cref="MediaControlServer.SetRepeatMode(MediaControlRepeatMode)"/>
313 /// <since_tizen> 4 </since_tizen>
314 public MediaControlRepeatMode GetRepeatMode()
318 Native.GetServerRepeatMode(Manager.Handle, ServerAppId, out var repeatMode).
319 ThrowIfError("Failed to get repeat mode state.");
321 return repeatMode.ToPublic();
325 /// Sends playback command to the server.</summary>
326 /// <param name="command">A playback command.</param>
327 /// <exception cref="InvalidOperationException">
328 /// The server has already been stopped.<br/>
330 /// An internal error occurs.
332 /// <exception cref="ArgumentException"><paramref name="command"/> is not valid.</exception>
333 /// <exception cref="ObjectDisposedException">The <see cref="MediaControllerManager"/> has already been disposed of.</exception>
334 /// <seealso cref="MediaControlServer.PlaybackCommandReceived"/>
335 /// <since_tizen> 4 </since_tizen>
336 public void SendPlaybackCommand(MediaControlPlaybackCommand command)
340 ValidationUtil.ValidateEnum(typeof(MediaControlPlaybackCommand), command, nameof(command));
342 Native.SendPlaybackStateCommand(Manager.Handle, ServerAppId, command.ToCode()).
343 ThrowIfError("Failed to send command.");