[Multimedia] Replaced the type of the buffer of MediaPacket with the common buffer... 63/148163/2
authorcoderhyme <jhyo.kim@samsung.com>
Thu, 7 Sep 2017 04:24:26 +0000 (13:24 +0900)
committerJungHo Kim <jhyo.kim@samsung.com>
Tue, 19 Sep 2017 06:35:57 +0000 (06:35 +0000)
Change-Id: I427e517b52469f0270f98f4a360a2898d9645d12
Signed-off-by: coderhyme <jhyo.kim@samsung.com>
src/Tizen.Multimedia/Common.Internal/DependentMediaBuffer.cs
src/Tizen.Multimedia/Common.Internal/IBufferOwner.cs
src/Tizen.Multimedia/Common.Internal/MediaBufferBase.cs
src/Tizen.Multimedia/Interop/Interop.MediaTool.cs
src/Tizen.Multimedia/MediaTool/MediaPacket.cs
src/Tizen.Multimedia/MediaTool/MediaPacketBuffer.cs [deleted file]
src/Tizen.Multimedia/MediaTool/MediaPacketVideoPlane.cs

index 855c83d..0f59619 100644 (file)
@@ -35,7 +35,6 @@ namespace Tizen.Multimedia
             _owner = owner;
         }
 
-
         internal override void ValidateBufferReadable()
         {
             _owner.ValidateBufferReadable(this);
index 16acf08..fee68ca 100644 (file)
@@ -31,7 +31,6 @@ namespace Tizen.Multimedia
         bool IsDisposed { get; }
     }
 
-
     internal static class BufferOwnerExtensions
     {
         internal static void ValidateBufferReadable(this IBufferOwner bufferOwner, IMediaBuffer buffer)
index 553a0dc..3c89173 100644 (file)
@@ -65,12 +65,12 @@ namespace Tizen.Multimedia
             if (length < 0)
             {
                 throw new ArgumentOutOfRangeException(nameof(length), length,
-                    $"Length can't be less than zero.");
+                    $"length can't be less than zero.");
             }
             if (offset < 0)
             {
                 throw new ArgumentOutOfRangeException(nameof(offset), offset,
-                    $"Offset can't be less than zero.");
+                    $"offset can't be less than zero.");
             }
         }
 
@@ -119,6 +119,11 @@ namespace Tizen.Multimedia
         {
             ThrowIfBufferIsNotReadable();
 
+            if (source == null)
+            {
+                throw new ArgumentNullException(nameof(source));
+            }
+
             if (startIndex < 0)
             {
                 throw new ArgumentOutOfRangeException(nameof(startIndex), startIndex,
@@ -144,6 +149,11 @@ namespace Tizen.Multimedia
         {
             ThrowIfBufferIsNotWritable();
 
+            if (dest == null)
+            {
+                throw new ArgumentNullException(nameof(dest));
+            }
+
             if (startIndex < 0)
             {
                 throw new ArgumentOutOfRangeException(nameof(startIndex), startIndex,
@@ -152,7 +162,7 @@ namespace Tizen.Multimedia
             if (startIndex + length > dest.Length)
             {
                 throw new ArgumentOutOfRangeException($"{nameof(startIndex)}, {nameof(length)}",
-                    "startIndex + length can't be greater than source.Length.");
+                    "startIndex + length can't be greater than dest.Length.");
             }
 
             ValidateRange(offset, length);
index e7992f3..f9177e5 100644 (file)
@@ -63,7 +63,7 @@ namespace Tizen.Multimedia
             internal static extern int IsEncoded(IntPtr handle, out bool value);
 
             [DllImport(Libraries.MediaTool, EntryPoint = "media_packet_get_flags")]
-            internal static extern int GetBufferFlags(IntPtr handle, out int value);
+            internal static extern int GetBufferFlags(IntPtr handle, out MediaPacketBufferFlags value);
 
             [DllImport(Libraries.MediaTool, EntryPoint = "media_packet_set_flags")]
             internal static extern int SetBufferFlags(IntPtr handle, int value);
index a72b05c..1b52493 100644 (file)
@@ -25,7 +25,7 @@ namespace Tizen.Multimedia
     /// <summary>
     /// Represents a packet for multimedia.
     /// </summary>
-    public abstract partial class MediaPacket : IDisposable
+    public abstract partial class MediaPacket : IBufferOwner, IDisposable
     {
         private IntPtr _handle = IntPtr.Zero;
 
@@ -52,6 +52,7 @@ namespace Tizen.Multimedia
 
             Initialize(format);
             _format = format;
+            _buffer = new Lazy<IMediaBuffer>(GetBuffer);
         }
 
         /// <summary>
@@ -248,17 +249,19 @@ namespace Tizen.Multimedia
             }
         }
 
-        private MediaPacketBuffer _buffer;
+        private Lazy<IMediaBuffer> _buffer;
 
         /// <summary>
         /// Gets the buffer of the packet.
         /// </summary>
-        /// <value>The <see cref="MediaPacketBuffer"/> allocated to the packet.
-        ///     This property will return null if the packet is in the raw video format.</value>
+        /// <value>
+        /// The <see cref="IMediaBuffer"/> allocated to the packet.
+        /// This property will return null if the packet is in the raw video format.
+        /// </value>
         /// <exception cref="ObjectDisposedException">The MediaPacket has already been disposed of.</exception>
         /// <seealso cref="IsEncoded"/>
         /// <seealso cref="VideoPlanes"/>
-        public MediaPacketBuffer Buffer
+        public IMediaBuffer Buffer
         {
             get
             {
@@ -269,12 +272,7 @@ namespace Tizen.Multimedia
                     return null;
                 }
 
-                if (_buffer == null)
-                {
-                    _buffer = GetBuffer();
-                }
-
-                return _buffer;
+                return _buffer.Value;
             }
         }
 
@@ -283,7 +281,7 @@ namespace Tizen.Multimedia
         /// </summary>
         /// <exception cref="ObjectDisposedException">The MediaPacket has already been disposed of.</exception>
         /// <exception cref="ArgumentOutOfRangeException">
-        ///     The value specified for this property is less than zero or greater than <see cref="MediaPacketBuffer.Length"/>.</exception>
+        ///     The value specified for this property is less than zero or greater than the length of the <see cref="Buffer"/>.</exception>
         /// <exception cref="InvalidOperationException">
         ///     The MediaPacket has <see cref="VideoPlanes"/> instead of <see cref="Buffer"/>.\n
         ///     -or-\n
@@ -369,13 +367,11 @@ namespace Tizen.Multimedia
             {
                 ValidateNotDisposed();
 
-                int value = 0;
-
-                int ret = Interop.MediaPacket.GetBufferFlags(_handle, out value);
+                int ret = Interop.MediaPacket.GetBufferFlags(_handle, out var value);
 
                 MultimediaDebug.AssertNoError(ret);
 
-                return (MediaPacketBufferFlags)value;
+                return value;
             }
 
             set
@@ -397,13 +393,7 @@ namespace Tizen.Multimedia
         /// Gets a value indicating whether the packet has been disposed of.
         /// </summary>
         /// <value>true if the packet has been disposed of; otherwise, false.</value>
-        public bool IsDisposed
-        {
-            get
-            {
-                return _isDisposed;
-            }
-        }
+        public bool IsDisposed => _isDisposed;
 
         private bool _isDisposed = false;
 
@@ -527,9 +517,11 @@ namespace Tizen.Multimedia
         /// <summary>
         /// Retrieves the buffer of the current packet.
         /// </summary>
-        /// <returns>The <see cref="MediaPacketBuffer"/> allocated to the current MediaPacket.</returns>
-        private MediaPacketBuffer GetBuffer()
+        /// <returns>The <see cref="IMediaBuffer"/> allocated to the current MediaPacket.</returns>
+        private IMediaBuffer GetBuffer()
         {
+            Debug.Assert(!IsDisposed, "Packet is already disposed!");
+
             Debug.Assert(_handle != IntPtr.Zero, "The handle is invalid!");
 
             IntPtr dataHandle;
@@ -543,7 +535,9 @@ namespace Tizen.Multimedia
             ret = Interop.MediaPacket.GetAllocatedBufferSize(_handle, out size);
             MultimediaDebug.AssertNoError(ret);
 
-            return new MediaPacketBuffer(this, dataHandle, size);
+            Debug.Assert(size >= 0, "size must not be negative!");
+
+            return new DependentMediaBuffer(this, dataHandle, size);
         }
 
         /// <summary>
@@ -560,6 +554,10 @@ namespace Tizen.Multimedia
         {
             return new SimpleMediaPacket(handle);
         }
+
+        bool IBufferOwner.IsDisposed => IsDisposed;
+
+        bool IBufferOwner.IsBufferAccessible(object buffer, MediaBufferAccessMode accessMode) => true;
     }
 
     internal class SimpleMediaPacket : MediaPacket
diff --git a/src/Tizen.Multimedia/MediaTool/MediaPacketBuffer.cs b/src/Tizen.Multimedia/MediaTool/MediaPacketBuffer.cs
deleted file mode 100644 (file)
index 2aecc07..0000000
+++ /dev/null
@@ -1,176 +0,0 @@
-/*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an AS IS BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-using System;
-using System.Diagnostics;
-using System.Runtime.InteropServices;
-
-namespace Tizen.Multimedia
-{
-    /// <summary>
-    /// Represents a buffer for a <see cref="MediaPacket"/>.
-    /// </summary>
-    public class MediaPacketBuffer
-    {
-        private readonly MediaPacket _packet;
-        private readonly IntPtr _dataHandle;
-
-        internal MediaPacketBuffer(MediaPacket packet, IntPtr dataHandle, int size)
-        {
-            Debug.Assert(packet != null, "Packet is null!");
-            Debug.Assert(!packet.IsDisposed, "Packet is already disposed!");
-            Debug.Assert(dataHandle != IntPtr.Zero, "dataHandle is null!");
-            Debug.Assert(size >= 0, "size must not be negative!");
-
-            _packet = packet;
-            _dataHandle = dataHandle;
-            _length = size;
-        }
-
-        /// <summary>
-        /// Gets or sets a value at the specified index.
-        /// </summary>
-        /// <param name="index">The index of the value to get or set.</param>
-        /// <exception cref="ArgumentOutOfRangeException">
-        ///     <paramref name="index"/> is less than zero.\n
-        ///     -or-\n
-        ///     <paramref name="index"/> is equal to or greater than <see cref="Length"/>.
-        /// </exception>
-        /// <exception cref="ObjectDisposedException">The MediaPacket that owns the current buffer has already been disposed of.</exception>
-        /// <exception cref="InvalidOperationException">The MediaPacket that owns the current buffer is being used by another module.</exception>
-        public byte this[int index]
-        {
-            get
-            {
-                _packet.EnsureReadableState();
-
-                if (index < 0 || index >= Length)
-                {
-                    throw new ArgumentOutOfRangeException($"Valid index range is [0, { nameof(Length) }).");
-                }
-
-                return Marshal.ReadByte(_dataHandle, index);
-            }
-            set
-            {
-                _packet.EnsureWritableState();
-
-                Marshal.WriteByte(_dataHandle, index, value);
-            }
-        }
-
-        /// <summary>
-        /// Validates the range.
-        /// </summary>
-        /// <param name="offset"></param>
-        /// <param name="length"></param>
-        /// <exception cref="ArgumentOutOfRangeException">
-        ///     <paramref name="offset"/> + <paramref name="length"/> is greater than <see cref="Length"/>.\n
-        ///     -or-\n
-        ///     <paramref name="offset"/> or <paramref name="length"/> is less than zero.
-        /// </exception>
-        private void ValidateRange(int offset, int length)
-        {
-            if (offset + length > _length)
-            {
-                throw new ArgumentOutOfRangeException("offset + length can't be greater than length of the buffer.");
-            }
-            if (length < 0)
-            {
-                throw new ArgumentOutOfRangeException($"Length can't be less than zero : { length }.");
-            }
-            if (offset < 0)
-            {
-                throw new ArgumentOutOfRangeException($"Offset can't be less than zero : { offset }.");
-            }
-        }
-
-        /// <summary>
-        /// Copies the data from a byte array to the buffer.
-        /// </summary>
-        /// <param name="source">The array to copy from.</param>
-        /// <param name="startIndex">The zero-based index in the source array where copying should start.</param>
-        /// <param name="length">The number of array elements to copy.</param>
-        /// <param name="offset">The zero-based index in the buffer where copying should start.</param>
-        /// <exception cref="ArgumentOutOfRangeException">
-        ///     <paramref name="startIndex"/>, <paramref name="offset"/>, or <paramref name="length"/> is not valid.
-        /// </exception>
-        /// <exception cref="ObjectDisposedException">The MediaPacket that owns the current buffer has already been disposed of.</exception>
-        public void CopyFrom(byte[] source, int startIndex, int length, int offset = 0)
-        {
-            _packet.EnsureReadableState();
-
-            if (startIndex < 0)
-            {
-                throw new ArgumentOutOfRangeException("startIndex can't be less than zero.");
-            }
-            if (startIndex + length > source.Length)
-            {
-                throw new ArgumentOutOfRangeException("startIndex + length can't be greater than source.Length.");
-            }
-
-            ValidateRange(offset, length);
-
-            Marshal.Copy(source, startIndex, IntPtr.Add(_dataHandle, offset), length);
-        }
-
-        /// <summary>
-        /// Copies the data from the buffer to a byte array.
-        /// </summary>
-        /// <param name="dest">The array to copy to.</param>
-        /// <param name="startIndex">The zero-based index in the destination array where copying should start.</param>
-        /// <param name="length">The number of elements to copy.</param>
-        /// <param name="offset">The zero-based index in the buffer where copying should start.</param>
-        /// <exception cref="ArgumentOutOfRangeException">
-        ///     <paramref name="startIndex"/>, <paramref name="offset"/>, <paramref name="length"/>, or <paramref name="offset"/> is not valid.
-        /// </exception>
-        /// <exception cref="ObjectDisposedException">The MediaPacket that owns the current buffer has already been disposed of.</exception>
-        /// <exception cref="InvalidOperationException">The MediaPacket that owns the current buffer is being used by another module.</exception>
-        public void CopyTo(byte[] dest, int startIndex, int length, int offset = 0)
-        {
-            _packet.EnsureWritableState();
-
-            if (startIndex < 0)
-            {
-                throw new ArgumentOutOfRangeException("Start index can't be less than zero.");
-            }
-            if (startIndex + length > dest.Length)
-            {
-                throw new ArgumentOutOfRangeException("startIndex + length can't be greater than dest.Length.");
-            }
-
-            ValidateRange(offset, length);
-
-            Marshal.Copy(IntPtr.Add(_dataHandle, offset), dest, startIndex, length);
-        }
-
-        private readonly int _length;
-
-        /// <summary>
-        /// Gets the size of the buffer, in bytes.
-        /// </summary>
-        /// <exception cref="ObjectDisposedException">The MediaPacket that owns the current buffer has already been disposed of.</exception>
-        public int Length
-        {
-            get
-            {
-                _packet.EnsureReadableState();
-
-                return _length;
-            }
-        }
-    }
-}
index aaabb39..08025c8 100644 (file)
@@ -28,7 +28,7 @@ namespace Tizen.Multimedia
         private readonly MediaPacket _packet;
         private readonly int _strideWidth;
         private readonly int _strideHeight;
-        private readonly MediaPacketBuffer _buffer;
+        private readonly IMediaBuffer _buffer;
 
         internal MediaPacketVideoPlane(MediaPacket packet, int index)
         {
@@ -44,18 +44,22 @@ namespace Tizen.Multimedia
             ret = Interop.MediaPacket.GetVideoStrideWidth(packet.GetHandle(), index, out _strideHeight);
             MultimediaDebug.AssertNoError(ret);
 
+            Debug.Assert(_strideWidth >= 0 && _strideHeight >= 0, "size must not be negative!");
+
             IntPtr dataHandle;
             ret = Interop.MediaPacket.GetVideoPlaneData(packet.GetHandle(), index, out dataHandle);
             MultimediaDebug.AssertNoError(ret);
 
-            _buffer = new MediaPacketBuffer(packet, dataHandle, _strideWidth * _strideHeight);
+            Debug.Assert(dataHandle != IntPtr.Zero, "Data handle is invalid!");
+
+            _buffer = new DependentMediaBuffer(packet, dataHandle, _strideWidth * _strideHeight);
         }
 
         /// <summary>
         /// Gets the buffer of the current video plane.
         /// </summary>
         /// <exception cref="ObjectDisposedException">The MediaPacket that owns the current buffer has already been disposed of.</exception>
-        public MediaPacketBuffer Buffer
+        public IMediaBuffer Buffer
         {
             get
             {