[DllImport(Libraries.MediaController, EntryPoint = "mc_server_unset_playback_state_command_received_cb")]
internal static extern MediaControllerError UnsetPlaybackStateCmdRecvCb(IntPtr handle);
+
+ [DllImport(Libraries.MediaController, EntryPoint = "mc_db_connect")]
+ internal static extern MediaControllerError ConnectDb(out IntPtr dbHandle);
+
+ [DllImport(Libraries.MediaController, EntryPoint = "mc_db_disconnect")]
+ internal static extern MediaControllerError DisconnectDb(IntPtr dbHandle);
+
+ [DllImport(Libraries.MediaController, EntryPoint = "mc_db_check_server_table_exist")]
+ internal static extern MediaControllerError CheckServerExist(IntPtr dbHandle, string appId, out bool value);
}
}
public static class MediaControlServer
{
private static IntPtr _handle = IntPtr.Zero;
+ private static bool? _isRunning;
/// <summary>
/// Gets a value indicating whether the server is running.
/// <seealso cref="Stop"/>
public static bool IsRunning
{
- get => _handle != IntPtr.Zero;
+ get
+ {
+ if (_isRunning.HasValue == false)
+ {
+ _isRunning = GetRunningState();
+ }
+
+ return _isRunning.Value;
+ }
+ }
+
+ private static bool GetRunningState()
+ {
+ IntPtr handle = IntPtr.Zero;
+ try
+ {
+ Native.ConnectDb(out handle).ThrowIfError("Failed to retrieve the running state.");
+
+ Native.CheckServerExist(handle, Applications.Application.Current.ApplicationInfo.ApplicationId,
+ out var value).ThrowIfError("Failed to retrieve the running state.");
+
+ return value;
+ }
+ finally
+ {
+ if (handle != IntPtr.Zero)
+ {
+ Native.DisconnectDb(handle);
+ }
+ }
}
- private static void ThrowIfNotRunning()
+ private static void EnsureInitializedIfRunning()
{
+ if (_handle != IntPtr.Zero)
+ {
+ return;
+ }
+
if (IsRunning == false)
{
throw new InvalidOperationException("The server is not running.");
}
+
+ Initialize();
}
private static IntPtr Handle
{
get
{
- ThrowIfNotRunning();
+ EnsureInitializedIfRunning();
return _handle;
}
}
- /// <summary>
- /// Starts the media control server.
- /// </summary>
- /// <remarks>
- /// When the server starts, <see cref="MediaControllerManager.ServerStarted"/> will be raised.
- /// </remarks>
- /// <privilege>http://tizen.org/privilege/mediacontroller.server</privilege>
- /// <exception cref="InvalidOperationException">
- /// The server has already started.\n
- /// -or-\n
- /// An internal error occurs.
- /// </exception>
- /// <exception cref="UnauthorizedAccessException">Caller does not have required privilege.</exception>
- /// <seealso cref="MediaControllerManager.ServerStarted"/>
- public static void Start()
+ private static void Initialize()
{
- if (IsRunning)
- {
- throw new InvalidOperationException("The server is already running.");
- }
-
Native.Create(out _handle).ThrowIfError("Failed to create media controller server.");
try
{
RegisterPlaybackCommandReceivedEvent();
+ _isRunning = true;
}
catch
{
}
/// <summary>
+ /// Starts the media control server.
+ /// </summary>
+ /// <remarks>
+ /// When the server starts, <see cref="MediaControllerManager.ServerStarted"/> will be raised.
+ /// </remarks>
+ /// <privilege>http://tizen.org/privilege/mediacontroller.server</privilege>
+ /// <exception cref="InvalidOperationException">An internal error occurs.</exception>
+ /// <exception cref="UnauthorizedAccessException">Caller does not have required privilege.</exception>
+ /// <seealso cref="MediaControllerManager.ServerStarted"/>
+ public static void Start()
+ {
+ Initialize();
+ }
+
+ /// <summary>
/// Stops the media control server.
/// </summary>
/// <remarks>
/// <seealso cref="MediaControllerManager.ServerStopped"/>
public static void Stop()
{
- ThrowIfNotRunning();
+ EnsureInitializedIfRunning();
Native.Destroy(_handle).ThrowIfError("Failed to stop the server.");
_handle = IntPtr.Zero;
_playbackCommandCallback = null;
+ _isRunning = false;
}
/// <summary>