[Multimedia] Modify MediaPlayer for product reuse (#219)
authorhsgwon <haesu.gwon@samsung.com>
Tue, 17 Apr 2018 05:42:09 +0000 (14:42 +0900)
committerGitHub <noreply@github.com>
Tue, 17 Apr 2018 05:42:09 +0000 (14:42 +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 6f6ddc9..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,28 +33,7 @@ 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);
-                }
-            }
-        }
+        protected static Exception GetException(int errorCode, string message) =>
+            ((PlayerErrorCode)errorCode).GetException(message);
     }
 }
\ No newline at end of file
index 580b2ab..5d2f5f1 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
 {
@@ -88,14 +86,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();
@@ -104,8 +96,6 @@ namespace Tizen.Multimedia
             RegisterMediaStreamBufferStatusCallback();
             RegisterMediaStreamSeekCallback();
             RegisterPlaybackCompletedCallback();
-
-            _callbackRegistered = true;
         }
 
         private void RegisterSubtitleUpdatedCallback()
@@ -166,6 +156,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 39f2e81..81f0490 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 ff61ee1..ea24ff6 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,12 +315,10 @@ namespace Tizen.Multimedia
 
             ValidatePlayerState(PlayerState.Idle);
 
-            SetDisplay(_display).ThrowIfFailed(this, "Failed to configure display of the player");
+            OnPreparing();
 
             SetPreparing();
 
-            OnPreparing();
-
             return Task.Factory.StartNew(() =>
             {
                 try
@@ -409,6 +453,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>
@@ -648,12 +699,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 ff48365..70146da 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($"{message} : Unknown error({err.ToString()}).");
             }