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.");
}
}
{
ThrowIfBufferIsNotReadable();
+ if (source == null)
+ {
+ throw new ArgumentNullException(nameof(source));
+ }
+
if (startIndex < 0)
{
throw new ArgumentOutOfRangeException(nameof(startIndex), startIndex,
{
ThrowIfBufferIsNotWritable();
+ if (dest == null)
+ {
+ throw new ArgumentNullException(nameof(dest));
+ }
+
if (startIndex < 0)
{
throw new ArgumentOutOfRangeException(nameof(startIndex), startIndex,
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);
/// <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;
Initialize(format);
_format = format;
+ _buffer = new Lazy<IMediaBuffer>(GetBuffer);
}
/// <summary>
}
}
- 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
{
return null;
}
- if (_buffer == null)
- {
- _buffer = GetBuffer();
- }
-
- return _buffer;
+ return _buffer.Value;
}
}
/// </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
{
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
/// 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;
/// <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;
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>
{
return new SimpleMediaPacket(handle);
}
+
+ bool IBufferOwner.IsDisposed => IsDisposed;
+
+ bool IBufferOwner.IsBufferAccessible(object buffer, MediaBufferAccessMode accessMode) => true;
}
internal class SimpleMediaPacket : MediaPacket
+++ /dev/null
-/*
- * 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;
- }
- }
- }
-}
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)
{
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
{