From: coderhyme Date: Wed, 6 Sep 2017 08:01:35 +0000 (+0900) Subject: [Multimedia] Modified MediaControlServer to check the running state by querying. X-Git-Tag: preview1-00187^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F94%2F147994%2F4;p=platform%2Fcore%2Fcsapi%2Ftizenfx.git [Multimedia] Modified MediaControlServer to check the running state by querying. The reason is when the application crashes, the server is alive in the database, which means the server can work continously after the application is restarted. Change-Id: I8819e0e4a7caca828aa8e493043461203364aa98 Signed-off-by: coderhyme --- diff --git a/src/Tizen.Multimedia.Remoting/Interop/Interop.MediaControllerServer.cs b/src/Tizen.Multimedia.Remoting/Interop/Interop.MediaControllerServer.cs index 6fe514d4a..abd6e5844 100644 --- a/src/Tizen.Multimedia.Remoting/Interop/Interop.MediaControllerServer.cs +++ b/src/Tizen.Multimedia.Remoting/Interop/Interop.MediaControllerServer.cs @@ -62,5 +62,14 @@ internal static partial class Interop [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); } } diff --git a/src/Tizen.Multimedia.Remoting/MediaController/MediaControlServer.cs b/src/Tizen.Multimedia.Remoting/MediaController/MediaControlServer.cs index f4fcdf245..c4909bbe4 100644 --- a/src/Tizen.Multimedia.Remoting/MediaController/MediaControlServer.cs +++ b/src/Tizen.Multimedia.Remoting/MediaController/MediaControlServer.cs @@ -27,6 +27,7 @@ namespace Tizen.Multimedia.Remoting public static class MediaControlServer { private static IntPtr _handle = IntPtr.Zero; + private static bool? _isRunning; /// /// Gets a value indicating whether the server is running. @@ -36,53 +37,71 @@ namespace Tizen.Multimedia.Remoting /// 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; } } - /// - /// Starts the media control server. - /// - /// - /// When the server starts, will be raised. - /// - /// http://tizen.org/privilege/mediacontroller.server - /// - /// The server has already started.\n - /// -or-\n - /// An internal error occurs. - /// - /// Caller does not have required privilege. - /// - 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 { @@ -93,6 +112,21 @@ namespace Tizen.Multimedia.Remoting } } + /// + /// Starts the media control server. + /// + /// + /// When the server starts, will be raised. + /// + /// http://tizen.org/privilege/mediacontroller.server + /// An internal error occurs. + /// Caller does not have required privilege. + /// + public static void Start() + { + Initialize(); + } + /// /// Stops the media control server. /// @@ -107,12 +141,13 @@ namespace Tizen.Multimedia.Remoting /// public static void Stop() { - ThrowIfNotRunning(); + EnsureInitializedIfRunning(); Native.Destroy(_handle).ThrowIfError("Failed to stop the server."); _handle = IntPtr.Zero; _playbackCommandCallback = null; + _isRunning = false; } ///