/* * 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 { /// /// Represents a buffer for a . /// 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; } /// /// Gets or sets a value at the specified index. /// /// The index of the value to get or set. /// /// is less than zero.\n /// -or-\n /// is equal to or greater than . /// /// The MediaPacket that owns the current buffer has already been disposed of. /// The MediaPacket that owns the current buffer is being used by another module. 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); } } /// /// Validates the range. /// /// /// /// /// + is greater than .\n /// -or-\n /// or is less than zero. /// 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 }."); } } /// /// Copies the data from a byte array to the buffer. /// /// The array to copy from. /// The zero-based index in the source array where copying should start. /// The number of array elements to copy. /// The zero-based index in the buffer where copying should start. /// /// , , or is not valid. /// /// The MediaPacket that owns the current buffer has already been disposed of. 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); } /// /// Copies the data from the buffer to a byte array. /// /// The array to copy to. /// The zero-based index in the destination array where copying should start. /// The number of elements to copy. /// The zero-based index in the buffer where copying should start. /// /// , , , or is not valid. /// /// The MediaPacket that owns the current buffer has already been disposed of. /// The MediaPacket that owns the current buffer is being used by another module. 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; /// /// Gets the size of the buffer, in bytes. /// /// The MediaPacket that owns the current buffer has already been disposed of. public int Length { get { _packet.EnsureReadableState(); return _length; } } } }