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 Native = Interop.MediaControllerServer;
20 namespace Tizen.Multimedia.Remoting
23 /// Provides a means to set playback information and metadata and receive commands from clients.
25 /// <seealso cref="MediaControllerManager"/>
26 /// <seealso cref="MediaController"/>
27 public static class MediaControlServer
29 private static IntPtr _handle = IntPtr.Zero;
32 /// Gets a value indicating whether the server is running.
34 /// <value>true if the server has started; otherwise, false.</value>
35 /// <seealso cref="Start"/>
36 /// <seealso cref="Stop"/>
37 public static bool IsRunning
39 get => _handle != IntPtr.Zero;
42 private static void ThrowIfNotRunning()
44 if (IsRunning == false)
46 throw new InvalidOperationException("The server is not running.");
50 private static IntPtr Handle
61 /// Starts the media control server.
64 /// When the server starts, <see cref="MediaControllerManager.ServerStarted"/> will be raised.
66 /// <privilege>http://tizen.org/privilege/mediacontroller.server</privilege>
67 /// <exception cref="InvalidOperationException">
68 /// The server has already started.\n
70 /// An internal error occurs.
72 /// <exception cref="UnauthorizedAccessException">Caller does not have required privilege.</exception>
73 /// <seealso cref="MediaControllerManager.ServerStarted"/>
74 public static void Start()
78 throw new InvalidOperationException("The server is already running.");
81 Native.Create(out _handle).ThrowIfError("Failed to create media controller server.");
85 RegisterPlaybackCommandReceivedEvent();
89 Native.Destroy(_handle);
90 _playbackCommandCallback = null;
91 _handle = IntPtr.Zero;
97 /// Stops the media control server.
100 /// When the server stops, <see cref="MediaControllerManager.ServerStopped"/> will be raised.
102 /// <exception cref="InvalidOperationException">
103 /// The server is not running .\n
105 /// An internal error occurs.
107 /// <seealso cref="MediaControllerManager.ServerStopped"/>
108 public static void Stop()
112 Native.Destroy(_handle).ThrowIfError("Failed to stop the server.");
114 _handle = IntPtr.Zero;
115 _playbackCommandCallback = null;
119 /// Updates playback state and playback position.</summary>
120 /// <param name="state">The playback state.</param>
121 /// <param name="position">The playback position in milliseconds.</param>
122 /// <exception cref="ArgumentException"><paramref name="state"/> is not valid.</exception>
123 /// <exception cref="ArgumentOutOfRangeException"><paramref name="position"/> is less than zero.</exception>
124 /// <exception cref="InvalidOperationException">
125 /// The server is not running .\n
127 /// An internal error occurs.
129 public static void SetPlaybackState(MediaControlPlaybackState state, long position)
131 ValidationUtil.ValidateEnum(typeof(MediaControlPlaybackState), state, nameof(state));
135 throw new ArgumentOutOfRangeException(nameof(position), position, "position can't be less than zero.");
138 Native.SetPlaybackState(Handle, state.ToCode()).ThrowIfError("Failed to set playback state.");
140 Native.SetPlaybackPosition(Handle, (ulong)position).ThrowIfError("Failed to set playback position.");
142 Native.UpdatePlayback(Handle).ThrowIfError("Failed to set playback.");
145 private static void SetMetadata(MediaControllerAttribute attribute, string value)
147 Native.SetMetadata(Handle, attribute, value).ThrowIfError($"Failed to set metadata({attribute}).");
151 /// Updates metadata information.
153 /// <param name="metadata">The metadata to update.</param>
154 /// <exception cref="ArgumentNullException"><paramref name="metadata"/> is null.</exception>
155 /// <exception cref="InvalidOperationException">
156 /// The server is not running .\n
158 /// An internal error occurs.
160 public static void SetMetadata(MediaControlMetadata metadata)
162 if (metadata == null)
164 throw new ArgumentNullException(nameof(metadata));
167 SetMetadata(MediaControllerAttribute.Title, metadata.Title);
168 SetMetadata(MediaControllerAttribute.Artist, metadata.Artist);
169 SetMetadata(MediaControllerAttribute.Album, metadata.Album);
170 SetMetadata(MediaControllerAttribute.Author, metadata.Author);
171 SetMetadata(MediaControllerAttribute.Genre, metadata.Genre);
172 SetMetadata(MediaControllerAttribute.Duration, metadata.Duration);
173 SetMetadata(MediaControllerAttribute.Date, metadata.Date);
174 SetMetadata(MediaControllerAttribute.Copyright, metadata.Copyright);
175 SetMetadata(MediaControllerAttribute.Description, metadata.Description);
176 SetMetadata(MediaControllerAttribute.TrackNumber, metadata.TrackNumber);
177 SetMetadata(MediaControllerAttribute.Picture, metadata.AlbumArtPath);
179 Native.UpdateMetadata(Handle).ThrowIfError("Failed to set metadata.");
183 /// Updates the shuffle mode.
185 /// <param name="enabled">A value indicating whether the shuffle mode is enabled.</param>
186 /// <exception cref="InvalidOperationException">
187 /// The server is not running .\n
189 /// An internal error occurs.
191 public static void SetShuffleModeEnabled(bool enabled)
193 Native.UpdateShuffleMode(Handle, enabled ? MediaControllerShuffleMode.On : MediaControllerShuffleMode.Off).
194 ThrowIfError("Failed to set shuffle mode.");
198 /// Updates the repeat mode.
200 /// <param name="mode">A value indicating the repeat mode.</param>
201 /// <exception cref="InvalidOperationException">
202 /// The server is not running .\n
204 /// An internal error occurs.
206 /// <exception cref="ArgumentException"/><paramref name="mode"/> is invalid.</exception>
207 public static void SetRepeatMode(MediaControlRepeatMode mode)
209 ValidationUtil.ValidateEnum(typeof(MediaControlRepeatMode), mode, nameof(mode));
211 Native.UpdateRepeatMode(Handle, mode.ToNative()).ThrowIfError("Failed to set repeat mode.");
215 /// Occurs when a client sends playback command.
217 public static event EventHandler<PlaybackCommandReceivedEventArgs> PlaybackCommandReceived;
219 private static Native.PlaybackStateCommandReceivedCallback _playbackCommandCallback;
221 private static void RegisterPlaybackCommandReceivedEvent()
223 _playbackCommandCallback = (clientName, playbackCode, _) =>
225 PlaybackCommandReceived?.Invoke(null, new PlaybackCommandReceivedEventArgs(clientName, playbackCode.ToCommand()));
227 Native.SetPlaybackStateCmdRecvCb(Handle, _playbackCommandCallback).
228 ThrowIfError("Failed to init PlaybackStateCommandReceived event."); ;