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 public class MediaController
28 internal MediaController(MediaControllerManager manager, string serverAppId)
30 Debug.Assert(manager != null);
31 Debug.Assert(serverAppId != null);
34 ServerAppId = serverAppId;
37 private MediaControllerManager Manager { get; }
40 /// Gets the application id of the server.
42 /// <value>The server application id.</value>
43 public string ServerAppId { get; }
46 /// Gets a value indicating whether the sever has been stopped.
48 /// <value>true if the server has been stopped; otherwise, false.</value>
55 private void ThrowIfStopped()
59 throw new InvalidOperationException("The server has already been stopped.");
64 /// Occurs when the server is stopped.
66 public event EventHandler ServerStopped;
68 internal void RaiseStoppedEvent()
71 ServerStopped?.Invoke(this, EventArgs.Empty);
75 /// Occurs when the playback state is updated.
77 public event EventHandler<PlaybackStateUpdatedEventArgs> PlaybackStateUpdated;
79 private PlaybackStateUpdatedEventArgs CreatePlaybackUpdatedEventArgs(IntPtr playbackHandle)
83 Native.GetPlaybackState(playbackHandle, out var playbackCode).ThrowIfError("Failed to get state.");
85 Native.GetPlaybackPosition(playbackHandle, out var position).ThrowIfError("Failed to get position.");
87 return new PlaybackStateUpdatedEventArgs(playbackCode.ToState(), (long)position);
91 Log.Error(GetType().FullName, e.ToString());
96 internal void RaisePlaybackUpdatedEvent(IntPtr playbackHandle)
98 var eventHandler = PlaybackStateUpdated;
100 if (eventHandler == null)
105 var args = CreatePlaybackUpdatedEventArgs(playbackHandle);
109 eventHandler.Invoke(this, args);
114 /// Occurs when the metadata is updated.
116 public event EventHandler<MetadataUpdatedEventArgs> MetadataUpdated;
118 private MetadataUpdatedEventArgs CreateMetadataUpdatedEventArgs(IntPtr metadataHandle)
122 return new MetadataUpdatedEventArgs(new MediaControlMetadata(metadataHandle));
126 Log.Error(GetType().FullName, e.ToString());
131 internal void RaiseMetadataUpdatedEvent(IntPtr metadataHandle)
133 var eventHandler = MetadataUpdated;
135 if (eventHandler == null)
140 var args = CreateMetadataUpdatedEventArgs(metadataHandle);
144 eventHandler.Invoke(this, args);
149 /// Occurs when the shuffle mode is updated.
151 public event EventHandler<ShuffleModeUpdatedEventArgs> ShuffleModeUpdated;
153 internal void RaiseShuffleModeUpdatedEvent(MediaControllerShuffleMode mode)
155 ShuffleModeUpdated?.Invoke(this, new ShuffleModeUpdatedEventArgs(mode == MediaControllerShuffleMode.On));
159 /// Occurs when the repeat mode is updated.
161 public event EventHandler<RepeatModeUpdatedEventArgs> RepeatModeUpdated;
163 internal void RaiseRepeatModeUpdatedEvent(MediaControlRepeatMode mode)
165 RepeatModeUpdated?.Invoke(this, new RepeatModeUpdatedEventArgs(mode));
169 /// Returns the playback state set by the server.
171 /// <returns>The playback state.</returns>
172 /// <exception cref="InvalidOperationException">
173 /// The server has already been stopped.<br/>
175 /// An internal error occurs.
177 /// <exception cref="ObjectDisposedException">The <see cref="MediaControllerManager"/> has already been disposed of.</exception>
178 /// <seealso cref="MediaControlServer.SetPlaybackState(MediaControlPlaybackState, long)"/>
179 public MediaControlPlaybackState GetPlaybackState()
183 IntPtr playbackHandle = IntPtr.Zero;
187 Native.GetServerPlayback(Manager.Handle, ServerAppId, out playbackHandle).ThrowIfError("Failed to get playback.");
189 Native.GetPlaybackState(playbackHandle, out var playbackCode).ThrowIfError("Failed to get state.");
191 return playbackCode.ToState();
195 if (playbackHandle != IntPtr.Zero)
197 Native.DestroyPlayback(playbackHandle);
203 /// Returns the playback position set by the server.
205 /// <returns>The playback position in milliseconds.</returns>
206 /// <exception cref="InvalidOperationException">
207 /// The server has already been stopped.<br/>
209 /// An internal error occurs.
211 /// <exception cref="ObjectDisposedException">The <see cref="MediaControllerManager"/> has already been disposed of.</exception>
212 /// <seealso cref="MediaControlServer.SetPlaybackState(MediaControlPlaybackState, long)"/>
213 public long GetPlaybackPosition()
217 IntPtr playbackHandle = IntPtr.Zero;
221 Native.GetServerPlayback(Manager.Handle, ServerAppId, out playbackHandle).ThrowIfError("Failed to get playback.");
223 Native.GetPlaybackPosition(playbackHandle, out var position).ThrowIfError("Failed to get position.");
225 return (long)position;
229 if (playbackHandle != IntPtr.Zero)
231 Native.DestroyPlayback(playbackHandle);
237 /// Returns the metadata set by the server.
239 /// <returns>The metadata.</returns>
240 /// <exception cref="InvalidOperationException">
241 /// The server has already been stopped.<br/>
243 /// An internal error occurs.
245 /// <exception cref="ObjectDisposedException">The <see cref="MediaControllerManager"/> has already been disposed of.</exception>
246 /// <seealso cref="MediaControlServer.SetMetadata(MediaControlMetadata)"/>
247 public MediaControlMetadata GetMetadata()
251 IntPtr metadataHandle = IntPtr.Zero;
255 Native.GetServerMetadata(Manager.Handle, ServerAppId, out metadataHandle).
256 ThrowIfError("Failed to get metadata.");
258 return new MediaControlMetadata(metadataHandle);
262 if (metadataHandle != IntPtr.Zero)
264 Native.DestroyMetadata(metadataHandle);
270 /// Returns whether the shuffle mode is enabled.
272 /// <returns>A value indicating whether the shuffle mode is enabled.</returns>
273 /// <exception cref="InvalidOperationException">
274 /// The server has already been stopped.<br/>
276 /// An internal error occurs.
278 /// <exception cref="ObjectDisposedException">The <see cref="MediaControllerManager"/> has already been disposed of.</exception>
279 /// <seealso cref="MediaControlServer.SetShuffleModeEnabled(bool)"/>
280 public bool IsShuffleModeEnabled()
284 Native.GetServerShuffleMode(Manager.Handle, ServerAppId, out var shuffleMode).
285 ThrowIfError("Failed to get shuffle mode state.");
287 return shuffleMode == MediaControllerShuffleMode.On;
291 /// Returns the repeat mode.
293 /// <returns>A <see cref="MediaControlRepeatMode"/> set by the server.</returns>
294 /// <exception cref="InvalidOperationException">
295 /// The server has already been stopped.<br/>
297 /// An internal error occurs.
299 /// <exception cref="ObjectDisposedException">The <see cref="MediaControllerManager"/> has already been disposed of.</exception>
300 /// <seealso cref="MediaControlServer.SetRepeatMode(MediaControlRepeatMode)"/>
301 public MediaControlRepeatMode GetRepeatMode()
305 Native.GetServerRepeatMode(Manager.Handle, ServerAppId, out var repeatMode).
306 ThrowIfError("Failed to get repeat mode state.");
308 return repeatMode.ToPublic();
312 /// Sends playback command to the server.</summary>
313 /// <param name="command">A playback command.</param>
314 /// <exception cref="InvalidOperationException">
315 /// The server has already been stopped.<br/>
317 /// An internal error occurs.
319 /// <exception cref="ArgumentException"><paramref name="command"/> is not valid.</exception>
320 /// <exception cref="ObjectDisposedException">The <see cref="MediaControllerManager"/> has already been disposed of.</exception>
321 /// <seealso cref="MediaControlServer.PlaybackCommandReceived"/>
322 public void SendPlaybackCommand(MediaControlPlaybackCommand command)
326 ValidationUtil.ValidateEnum(typeof(MediaControlPlaybackCommand), command, nameof(command));
328 Native.SendPlaybackStateCommand(Manager.Handle, ServerAppId, command.ToCode()).
329 ThrowIfError("Failed to send command.");