[MediaPlayer] add API to export video frames (#983)
authornam <36914158+aferin@users.noreply.github.com>
Tue, 17 Sep 2019 04:36:35 +0000 (13:36 +0900)
committerGitHub <noreply@github.com>
Tue, 17 Sep 2019 04:36:35 +0000 (13:36 +0900)
* [MediaPlayer] add API to export video frames

src/Tizen.Multimedia.MediaPlayer/Player/Player.Events.cs
src/Tizen.Multimedia.MediaPlayer/Player/Player.cs

index e7ba765..158fa46 100644 (file)
@@ -171,10 +171,6 @@ namespace Tizen.Multimedia
         }
 
         #region VideoFrameDecoded event
-        private EventHandler<VideoFrameDecodedEventArgs> _videoFrameDecoded;
-
-        private NativePlayer.VideoFrameDecodedCallback _videoFrameDecodedCallback;
-
         /// <summary>
         /// Occurs when a video frame is decoded.
         /// </summary>
@@ -186,42 +182,8 @@ namespace Tizen.Multimedia
         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
         /// <seealso cref="VideoFrameDecodedEventArgs.Packet"/>
         /// <since_tizen> 3 </since_tizen>
-        public event EventHandler<VideoFrameDecodedEventArgs> VideoFrameDecoded
-        {
-            add
-            {
-                ValidationUtil.ValidateFeatureSupported(PlayerFeatures.RawVideo);
-
-                _videoFrameDecoded += value;
-            }
-            remove
-            {
-                ValidationUtil.ValidateFeatureSupported(PlayerFeatures.RawVideo);
-
-                _videoFrameDecoded -= value;
-            }
-        }
-
-        private void RegisterVideoFrameDecodedCallback()
-        {
-            _videoFrameDecodedCallback = (packetHandle, _) =>
-            {
-                var handler = _videoFrameDecoded;
-                if (handler != null)
-                {
-                    Log.Debug(PlayerLog.Tag, "packet : " + packetHandle);
-                    handler.Invoke(this,
-                        new VideoFrameDecodedEventArgs(MediaPacket.From(packetHandle)));
-                }
-                else
-                {
-                    MediaPacket.From(packetHandle).Dispose();
-                }
-            };
-
-            NativePlayer.SetVideoFrameDecodedCb(Handle, _videoFrameDecodedCallback).
-                ThrowIfFailed(this, "Failed to register the VideoFrameDecoded");
-        }
+        public event EventHandler<VideoFrameDecodedEventArgs> VideoFrameDecoded;
+        private NativePlayer.VideoFrameDecodedCallback _videoFrameDecodedCallback;
         #endregion
 
         #region AudioFrameDecoded event
index fd7557e..e1693a1 100644 (file)
@@ -94,11 +94,6 @@ namespace Tizen.Multimedia
                 _audioEffect = new AudioEffect(this);
             }
 
-            if (Features.IsSupported(PlayerFeatures.RawVideo))
-            {
-                RegisterVideoFrameDecodedCallback();
-            }
-
             RegisterEvents();
 
             _displaySettings = PlayerDisplaySettings.Create(this);
@@ -1027,5 +1022,78 @@ namespace Tizen.Multimedia
 
             _audioFrameDecodedCallback = null;
         }
+
+        /// <summary>
+        /// Enables to decode a video data for every frame.
+        /// </summary>
+        /// <remarks><para>The player must be in the <see cref="PlayerState.Idle"/> state.
+        /// And, <see cref="Multimedia.Display"/> must not be set.</para>
+        /// <para>A <see cref="VideoFrameDecoded"/> event is called in a separate thread(not in the main loop).</para>
+        /// <para>The video frame can be retrieved using a <see cref="VideoFrameDecoded"/> event as a media packet.
+        /// So if you change the media packet in the <see cref="VideoFrameDecoded"/> event, it will be displayed on the device.
+        /// The callback function holds the same buffer that will be drawn on the display device.
+        /// and the <see cref="MediaPacket"/> is available until it's destroyed by <see cref="MediaPacket.Dispose()"/>.
+        /// The packet has to be destroyed as quickly as possible after rendering the data
+        /// and all the packets have to be destroyed before <see cref="Unprepare"/> is called.</para></remarks>
+        /// <feature>http://tizen.org/feature/multimedia.raw_video</feature>
+        /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
+        /// <exception cref="ObjectDisposedException">The player has already been disposed of.</exception>
+        /// <exception cref="InvalidOperationException">
+        ///     Operation failed; internal error.
+        ///     -or-<br/>
+        ///     The player is not in the valid state.
+        ///     </exception>
+        /// <seealso cref="DisableExportingVideoFrame"/>
+        /// <since_tizen> 6 </since_tizen>
+        public void EnableExportingVideoFrame()
+        {
+            ValidationUtil.ValidateFeatureSupported(PlayerFeatures.RawVideo);
+            ValidatePlayerState(PlayerState.Idle);
+
+            if (Display != null)
+            {
+                throw new InvalidOperationException("Display must be none.");
+            }
+
+            _videoFrameDecodedCallback = (packetHandle, _) =>
+            {
+                var handler = VideoFrameDecoded;
+                if (handler != null)
+                {
+                    Log.Debug(PlayerLog.Tag, "packet : " + packetHandle);
+                    handler.Invoke(this,
+                        new VideoFrameDecodedEventArgs(MediaPacket.From(packetHandle)));
+                }
+                else
+                {
+                    MediaPacket.From(packetHandle).Dispose();
+                }
+            };
+
+            NativePlayer.SetVideoFrameDecodedCb(Handle, _videoFrameDecodedCallback).
+                ThrowIfFailed(this, "Failed to register the VideoFrameDecoded");
+        }
+
+        /// <summary>
+        /// Disables to decode a video data.
+        /// </summary>
+        /// <remarks>The player must be in the <see cref="PlayerState.Idle"/> or <see cref="PlayerState.Ready"/>
+        /// state.</remarks>
+        /// <feature>http://tizen.org/feature/multimedia.raw_video</feature>
+        /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
+        /// <exception cref="ObjectDisposedException">The player has already been disposed of.</exception>
+        /// <exception cref="InvalidOperationException">The player is not in the valid state.</exception>
+        /// <seealso cref="EnableExportingVideoFrame"/>
+        /// <since_tizen> 6 </since_tizen>
+        public void DisableExportingVideoFrame()
+        {
+            ValidationUtil.ValidateFeatureSupported(PlayerFeatures.RawVideo);
+            ValidatePlayerState(PlayerState.Idle, PlayerState.Ready);
+
+            NativePlayer.UnsetVideoFrameDecodedCb(Handle).
+                ThrowIfFailed(this, "Failed to unset the VideoFrameDecoded");
+
+            _videoFrameDecodedCallback = null;
+        }
     }
 }