[Multimedia] Modify MediaPlayer for product reuse (#124)
authorhsgwon <haesu.gwon@samsung.com>
Tue, 13 Feb 2018 03:20:53 +0000 (12:20 +0900)
committerGitHub <noreply@github.com>
Tue, 13 Feb 2018 03:20:53 +0000 (12:20 +0900)
src/Tizen.Multimedia.MediaPlayer/Interop/Interop.Player.cs
src/Tizen.Multimedia.MediaPlayer/Player/Player.ErrorHandler.cs
src/Tizen.Multimedia.MediaPlayer/Player/Player.Events.cs
src/Tizen.Multimedia.MediaPlayer/Player/Player.Properties.cs
src/Tizen.Multimedia.MediaPlayer/Player/Player.cs
src/Tizen.Multimedia.MediaPlayer/Player/PlayerError.cs

index b9378c8..373ee5d 100644 (file)
@@ -269,6 +269,11 @@ internal static partial class Interop
         {
         }
 
+        internal PlayerHandle(IntPtr rawHandle) : this()
+        {
+            handle = rawHandle;
+        }
+
         public override bool IsInvalid => handle == IntPtr.Zero;
 
         protected override bool ReleaseHandle()
index 07ce450..30154ee 100644 (file)
  */
 
 using System;
-using System.Collections.Generic;
 using System.ComponentModel;
 
 namespace Tizen.Multimedia
 {
     public partial class Player
     {
-        private static List<Action<Player, int, string>> _errorHandlers;
+        private Action<int, string> _errorHandler;
 
-        private static object _errorHandlerLock = new object();
-
-        /// <summary>
-        /// This method supports the product infrastructure and is not intended to be used directly from application code.
-        /// </summary>
-        /// <since_tizen> 4 </since_tizen>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        protected static void AddErrorHandler(Action<Player, int, string> errorHandler)
+        internal void NotifyError(int errorCode, string message)
         {
-            if (errorHandler == null)
-            {
-                throw new ArgumentNullException(nameof(errorHandler));
-            }
-
-            lock (_errorHandlerLock)
-            {
-                if (_errorHandlers == null)
-                {
-                    _errorHandlers = new List<Action<Player, int, string>>();
-                }
-
-                _errorHandlers.Add(errorHandler);
-            }
+            _errorHandler?.Invoke(errorCode, message);
         }
 
         /// <summary>
@@ -54,34 +33,6 @@ namespace Tizen.Multimedia
         /// </summary>
         /// <since_tizen> 4 </since_tizen>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        protected static void RemoveErrorHandler(Action<Player, int, string> errorHandler)
-        {
-            lock (_errorHandlerLock)
-            {
-                _errorHandlers?.Remove(errorHandler);
-            }
-        }
-
-        internal static void NotifyError(Player player, int errorCode, string message)
-        {
-            if (_errorHandlers == null)
-            {
-                return;
-            }
-
-            lock (_errorHandlerLock)
-            {
-                foreach (var handler in _errorHandlers)
-                {
-                    handler(player, errorCode, message);
-                }
-            }
-        }
-
-        /// <summary>
-        /// This method supports the product infrastructure and is not intended to be used directly from application code.
-        /// </summary>
-        [EditorBrowsable(EditorBrowsableState.Never)]
         protected static Exception GetException(int errorCode, string message) =>
             ((PlayerErrorCode)errorCode).GetException(message);
     }
index 8d4a824..574a00f 100644 (file)
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
+using static Interop;
 using System;
-using System.Threading.Tasks;
-using System.Runtime.InteropServices;
+using System.ComponentModel;
 using System.Diagnostics;
-using System.IO;
-using System.Threading;
-using static Interop;
 
 namespace Tizen.Multimedia
 {
@@ -82,14 +80,8 @@ namespace Tizen.Multimedia
         internal event EventHandler<MediaStreamSeekingOccurredEventArgs> MediaStreamVideoSeekingOccurred;
         private NativePlayer.MediaStreamSeekCallback _mediaStreamVideoSeekCallback;
 
-        private bool _callbackRegistered;
-
         private void RegisterEvents()
         {
-            if (_callbackRegistered)
-            {
-                return;
-            }
             RegisterSubtitleUpdatedCallback();
             RegisterErrorOccurredCallback();
             RegisterPlaybackInterruptedCallback();
@@ -98,8 +90,6 @@ namespace Tizen.Multimedia
             RegisterMediaStreamBufferStatusCallback();
             RegisterMediaStreamSeekCallback();
             RegisterPlaybackCompletedCallback();
-
-            _callbackRegistered = true;
         }
 
         private void RegisterSubtitleUpdatedCallback()
@@ -160,6 +150,18 @@ namespace Tizen.Multimedia
                 ThrowIfFailed(this, "Failed to set PlaybackError");
         }
 
+        /// <summary>
+        /// Raises the <see cref="ErrorOccurred"/> event.
+        /// </summary>
+        /// <param name="e">
+        /// An <see cref="PlayerErrorOccurredEventArgs"/> that contains the event data.
+        /// </param>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        protected void OnErrorOccurred(PlayerErrorOccurredEventArgs e)
+        {
+            ErrorOccurred?.Invoke(this, e);
+        }
+
         #region VideoFrameDecoded event
 
         private EventHandler<VideoFrameDecodedEventArgs> _videoFrameDecoded;
index e2a4742..396c66a 100644 (file)
@@ -191,12 +191,14 @@ namespace Tizen.Multimedia
 
         #region Display methods
 
+        private PlayerDisplaySettings _displaySettings;
+
         /// <summary>
         /// Gets the display settings.
         /// </summary>
         /// <value>A <see cref="PlayerDisplaySettings"/> that specifies the display settings.</value>
         /// <since_tizen> 3 </since_tizen>
-        public PlayerDisplaySettings DisplaySettings { get; }
+        public PlayerDisplaySettings DisplaySettings => _displaySettings;
 
         private Display _display;
 
@@ -256,6 +258,8 @@ namespace Tizen.Multimedia
                     throw new ArgumentException("The display has already been assigned to another.");
                 }
 
+                SetDisplay(value).ThrowIfFailed(this, "Failed to configure display of the player");
+
                 ReplaceDisplay(value);
             }
         }
@@ -336,7 +340,7 @@ namespace Tizen.Multimedia
             }
         }
 
-        private readonly AudioEffect _audioEffect;
+        private AudioEffect _audioEffect;
 
         /// <summary>
         /// Gets the audio effect.
index 82b62f8..247e5d7 100644 (file)
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
+using static Interop;
 using System;
-using System.Threading.Tasks;
-using System.Runtime.InteropServices;
+using System.ComponentModel;
 using System.Diagnostics;
 using System.IO;
+using System.Runtime.InteropServices;
 using System.Threading;
-using static Interop;
-using System.ComponentModel;
+using System.Threading.Tasks;
 
 namespace Tizen.Multimedia
 {
@@ -39,7 +40,7 @@ namespace Tizen.Multimedia
     /// </remarks>
     public partial class Player : IDisposable, IDisplayable<PlayerErrorCode>
     {
-        private PlayerHandle _handle;
+        private readonly PlayerHandle _handle;
 
         /// <summary>
         /// Initializes a new instance of the <see cref="Player"/> class.
@@ -51,6 +52,41 @@ namespace Tizen.Multimedia
 
             Debug.Assert(_handle != null);
 
+            Initialize();
+        }
+
+        /// <summary>
+        /// Initializes a new instance of the <see cref="Player"/> class with a native handle.
+        /// The class takes care of the life cycle of the handle.
+        /// Thus, it should not be closed/destroyed in another location.
+        /// </summary>
+        /// <remarks>
+        /// This supports the product infrastructure and is not intended to be used directly from application code.
+        /// </remarks>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        protected Player(IntPtr handle, Action<int, string> errorHandler)
+        {
+            // This constructor is to support TV product player.
+            // Be careful with 'handle'. It must be wrapped in safe handle, first.
+            _handle = handle != IntPtr.Zero ? new PlayerHandle(handle) :
+                throw new ArgumentException("Handle is invalid.", nameof(handle));
+
+            _errorHandler = errorHandler;
+        }
+
+        private bool _initialized;
+
+        /// <summary>
+        /// This supports the product infrastructure and is not intended to be used directly from application code.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        protected void Initialize()
+        {
+            if (_initialized)
+            {
+                throw new InvalidOperationException("It has already been initialized.");
+            }
+
             if (Features.IsSupported(PlayerFeatures.AudioEffect))
             {
                 _audioEffect = new AudioEffect(this);
@@ -61,7 +97,11 @@ namespace Tizen.Multimedia
                 RegisterVideoFrameDecodedCallback();
             }
 
-            DisplaySettings = PlayerDisplaySettings.Create(this);
+            RegisterEvents();
+
+            _displaySettings = PlayerDisplaySettings.Create(this);
+
+            _initialized = true;
         }
 
         internal void ValidatePlayerState(params PlayerState[] desiredStates)
@@ -92,7 +132,14 @@ namespace Tizen.Multimedia
             Dispose(true);
         }
 
-        private void Dispose(bool disposing)
+        /// <summary>
+        /// Releases the unmanaged resources used by the <see cref="Player"/>.
+        /// </summary>
+        /// <param name="disposing">
+        /// true to release both managed and unmanaged resources; false to release only unmanaged resources.
+        /// </param>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        protected virtual void Dispose(bool disposing)
         {
             if (!_disposed)
             {
@@ -247,7 +294,6 @@ namespace Tizen.Multimedia
         /// <since_tizen> 3 </since_tizen>
         protected virtual void OnPreparing()
         {
-            RegisterEvents();
         }
 
         /// <summary>
@@ -269,8 +315,6 @@ namespace Tizen.Multimedia
 
             ValidatePlayerState(PlayerState.Idle);
 
-            SetDisplay(_display).ThrowIfFailed(this, "Failed to configure display of the player");
-
             OnPreparing();
 
             var completionSource = new TaskCompletionSource<bool>();
@@ -414,6 +458,13 @@ namespace Tizen.Multimedia
         private MediaSource _source;
 
         /// <summary>
+        /// Determines whether MediaSource has set.
+        /// This supports the product infrastructure and is not intended to be used directly from application code.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        protected bool HasSource => _source != null;
+
+        /// <summary>
         /// Sets a media source for the player.
         /// </summary>
         /// <param name="source">A <see cref="MediaSource"/> that specifies the source for playback.</param>
@@ -653,12 +704,20 @@ namespace Tizen.Multimedia
             return Interlocked.CompareExchange(ref _isPreparing, 1, 1) == 1;
         }
 
-        private void SetPreparing()
+        /// <summary>
+        /// This supports the product infrastructure and is not intended to be used directly from application code.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        protected void SetPreparing()
         {
             Interlocked.Exchange(ref _isPreparing, 1);
         }
 
-        private void ClearPreparing()
+        /// <summary>
+        /// This supports the product infrastructure and is not intended to be used directly from application code.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        protected void ClearPreparing()
         {
             Interlocked.Exchange(ref _isPreparing, 0);
         }
index dfc989c..316933b 100644 (file)
@@ -65,7 +65,7 @@ namespace Tizen.Multimedia
             if (ex == null)
             {
                 // Notify only when it can't be handled.
-                Player.NotifyError(player, (int)err, message);
+                player?.NotifyError((int)err, message);
 
                 throw new InvalidOperationException($"Unknown error : {err.ToString()}");
             }