2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Diagnostics;
19 using System.Runtime.InteropServices;
21 namespace Tizen.Multimedia
24 /// Represents a buffer for a <see cref="MediaPacket"/>.
26 public class MediaPacketBuffer
28 private readonly MediaPacket _packet;
29 private readonly IntPtr _dataHandle;
31 internal MediaPacketBuffer(MediaPacket packet, IntPtr dataHandle, int size)
33 Debug.Assert(packet != null, "Packet is null!");
34 Debug.Assert(!packet.IsDisposed, "Packet is already disposed!");
35 Debug.Assert(dataHandle != IntPtr.Zero, "dataHandle is null!");
36 Debug.Assert(size >= 0, "size must not be negative!");
39 _dataHandle = dataHandle;
44 /// Gets or sets a value at the specified index.
46 /// <param name="index">The index of the value to get or set.</param>
47 /// <exception cref="ArgumentOutOfRangeException">
48 /// <paramref name="index"/> is less than zero.\n
50 /// <paramref name="index"/> is equal to or greater than <see cref="Length"/>.
52 /// <exception cref="ObjectDisposedException">The MediaPacket that owns the current buffer has already been disposed of.</exception>
53 /// <exception cref="InvalidOperationException">The MediaPacket that owns the current buffer is being used by another module.</exception>
54 public byte this[int index]
58 _packet.EnsureReadableState();
60 if (index < 0 || index >= Length)
62 throw new ArgumentOutOfRangeException($"Valid index range is [0, { nameof(Length) }).");
65 return Marshal.ReadByte(_dataHandle, index);
69 _packet.EnsureWritableState();
71 Marshal.WriteByte(_dataHandle, index, value);
76 /// Validates the range.
78 /// <param name="offset"></param>
79 /// <param name="length"></param>
80 /// <exception cref="ArgumentOutOfRangeException">
81 /// <paramref name="offset"/> + <paramref name="length"/> is greater than <see cref="Length"/>.\n
83 /// <paramref name="offset"/> or <paramref name="length"/> is less than zero.
85 private void ValidateRange(int offset, int length)
87 if (offset + length > _length)
89 throw new ArgumentOutOfRangeException("offset + length can't be greater than length of the buffer.");
93 throw new ArgumentOutOfRangeException($"Length can't be less than zero : { length }.");
97 throw new ArgumentOutOfRangeException($"Offset can't be less than zero : { offset }.");
102 /// Copies the data from a byte array to the buffer.
104 /// <param name="source">The array to copy from.</param>
105 /// <param name="startIndex">The zero-based index in the source array where copying should start.</param>
106 /// <param name="length">The number of array elements to copy.</param>
107 /// <param name="offset">The zero-based index in the buffer where copying should start.</param>
108 /// <exception cref="ArgumentOutOfRangeException">
109 /// <paramref name="startIndex"/>, <paramref name="offset"/>, or <paramref name="length"/> is not valid.
111 /// <exception cref="ObjectDisposedException">The MediaPacket that owns the current buffer has already been disposed of.</exception>
112 public void CopyFrom(byte[] source, int startIndex, int length, int offset = 0)
114 _packet.EnsureReadableState();
118 throw new ArgumentOutOfRangeException("startIndex can't be less than zero.");
120 if (startIndex + length > source.Length)
122 throw new ArgumentOutOfRangeException("startIndex + length can't be greater than source.Length.");
125 ValidateRange(offset, length);
127 Marshal.Copy(source, startIndex, IntPtr.Add(_dataHandle, offset), length);
131 /// Copies the data from the buffer to a byte array.
133 /// <param name="dest">The array to copy to.</param>
134 /// <param name="startIndex">The zero-based index in the destination array where copying should start.</param>
135 /// <param name="length">The number of elements to copy.</param>
136 /// <param name="offset">The zero-based index in the buffer where copying should start.</param>
137 /// <exception cref="ArgumentOutOfRangeException">
138 /// <paramref name="startIndex"/>, <paramref name="offset"/>, <paramref name="length"/>, or <paramref name="offset"/> is not valid.
140 /// <exception cref="ObjectDisposedException">The MediaPacket that owns the current buffer has already been disposed of.</exception>
141 /// <exception cref="InvalidOperationException">The MediaPacket that owns the current buffer is being used by another module.</exception>
142 public void CopyTo(byte[] dest, int startIndex, int length, int offset = 0)
144 _packet.EnsureWritableState();
148 throw new ArgumentOutOfRangeException("Start index can't be less than zero.");
150 if (startIndex + length > dest.Length)
152 throw new ArgumentOutOfRangeException("startIndex + length can't be greater than dest.Length.");
155 ValidateRange(offset, length);
157 Marshal.Copy(IntPtr.Add(_dataHandle, offset), dest, startIndex, length);
160 private readonly int _length;
163 /// Gets the size of the buffer, in bytes.
165 /// <exception cref="ObjectDisposedException">The MediaPacket that owns the current buffer has already been disposed of.</exception>
170 _packet.EnsureReadableState();