Remove compiler warnings.
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia / Common / IMediaBuffer.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Diagnostics;
19 using System.Runtime.InteropServices;
20
21 namespace Tizen.Multimedia
22 {
23     public interface IReadOnlyBuffer
24     {
25         /// <summary>
26         /// Gets or sets a value at the specified index.
27         /// </summary>
28         /// <param name="index">The index of the value to get or set.</param>
29         /// <exception cref="ArgumentOutOfRangeException">
30         ///     index is less than zero.
31         ///     <para>-or-</para>
32         ///     index is equal to or greater than <see cref="Length"/>.
33         /// </exception>
34         /// <exception cref="ObjectDisposedException">The object that owns the current buffer already has been disposed of.</exception>
35         /// <exception cref="InvalidOperationException">The buffer is not available. i.e. not writable state.</exception>
36         byte this[int index]
37         {
38             get;
39             set;
40         }
41         /// <summary>
42         /// Gets the size of the buffer, in bytes.
43         /// </summary>
44         int Length
45         {
46             get;
47         }
48
49         /// <summary>
50         /// Copies data from a byte array to the buffer.
51         /// </summary>
52         /// <param name="source">The array to copy from.</param>
53         /// <param name="startIndex">The zero-based index in the source array where copying should start.</param>
54         /// <param name="length">The number of array elements to copy.</param>
55         /// <exception cref="ArgumentOutOfRangeException">startIndex or length is not valid.</exception>
56         /// <exception cref="ObjectDisposedException">The object that owns the current buffer already has been disposed of.</exception>
57         void CopyTo(byte[] dest, int startIndex, int length);
58
59         /// <summary>
60         /// Copies data from a byte array to the buffer.
61         /// </summary>
62         /// <param name="source">The array to copy from.</param>
63         /// <param name="startIndex">The zero-based index in the source array where copying should start.</param>
64         /// <param name="length">The number of array elements to copy.</param>
65         /// <param name="offset">The zero-based index in the buffer where copying should start.</param>
66         /// <exception cref="ArgumentOutOfRangeException">startIndex, offset or length is not valid.</exception>
67         /// <exception cref="ObjectDisposedException">The object that owns the current buffer already has been disposed of.</exception>
68         void CopyTo(byte[] dest, int startIndex, int length, int offset);
69     }
70
71     public interface IMediaBuffer : IReadOnlyBuffer
72     {
73         /// <summary>
74         /// Copies data from the buffer to a byte array.
75         /// </summary>
76         /// <param name="dest">The array to copy to.</param>
77         /// <param name="startIndex">The zero-based index in the dest array where copying should start.</param>
78         /// <param name="length">The number of elements to copy.</param>
79         /// <exception cref="ArgumentOutOfRangeException">startIndex or length is not valid.</exception>
80         /// <exception cref="ObjectDisposedException">The object that owns the current buffer already has been disposed of.</exception>
81         /// <exception cref="InvalidOperationException">The buffer is not available. i.e. not writable state.</exception>
82
83         void CopyFrom(byte[] source, int startIndex, int length);
84
85         /// <summary>
86         /// Copies data from the buffer to a byte array.
87         /// </summary>
88         /// <param name="dest">The array to copy to.</param>
89         /// <param name="startIndex">The zero-based index in the dest array where copying should start.</param>
90         /// <param name="length">The number of elements to copy.</param>
91         /// <param name="offset">The zero-based index in the buffer where copying should start.</param>
92         /// <exception cref="ArgumentOutOfRangeException">startIndex, offset or length is not valid.</exception>
93         /// <exception cref="ObjectDisposedException">The object that owns the current buffer already has been disposed of.</exception>
94         /// <exception cref="InvalidOperationException">The buffer is not available. i.e. not writable state.</exception>
95         void CopyFrom(byte[] source, int startIndex, int length, int offset);
96     }
97
98     /// <summary>
99     /// Represents a buffer for a <see cref="MediaPacket"/>.
100     /// </summary>
101     internal class DependentMediaBuffer : IMediaBuffer
102     {
103         private readonly IBufferOwner _owner;
104         private readonly IntPtr _dataHandle;
105
106         internal DependentMediaBuffer(IBufferOwner owner, IntPtr dataHandle, int size)
107         {
108             Debug.Assert(owner != null, "Owner is null!");
109             Debug.Assert(!owner.IsDisposed, "Owner has been already disposed!");
110             Debug.Assert(dataHandle != IntPtr.Zero, "dataHandle is null!");
111             Debug.Assert(size >= 0, "size must not be negative!");
112
113             _owner = owner;
114             _dataHandle = dataHandle;
115             Length = size;
116         }
117
118         public byte this[int index]
119         {
120             get
121             {
122                 _owner.ValidateBufferReadable(this);
123
124                 if (index < 0 || index >= Length)
125                 {
126                     throw new ArgumentOutOfRangeException($"Valid index range is [0, { nameof(Length) }).");
127                 }
128
129                 return Marshal.ReadByte(_dataHandle, index);
130             }
131             set
132             {
133                 _owner.ValidateBufferWritable(this);
134
135                 Marshal.WriteByte(_dataHandle, index, value);
136             }
137         }
138
139         /// <summary>
140         /// Validates the range
141         /// </summary>
142         /// <param name="offset"></param>
143         /// <param name="length"></param>
144         /// <exception cref="ArgumentOutOfRangeException">
145         ///     offset + length is greater than <see cref="Length"/>.
146         ///     <para>-or-</para>
147         ///     offset or length is less than zero.
148         /// </exception>
149         private void ValidateRange(int offset, int length)
150         {
151             if (offset + length > Length)
152             {
153                 throw new ArgumentOutOfRangeException("offset + length can't be greater than length of the buffer.");
154             }
155             if (length < 0)
156             {
157                 throw new ArgumentOutOfRangeException($"Length can't be less than zero : { length }.");
158             }
159             if (offset < 0)
160             {
161                 throw new ArgumentOutOfRangeException($"Offset can't be less than zero : { offset }.");
162             }
163         }
164
165         public void CopyFrom(byte[] source, int startIndex, int length, int offset)
166         {
167             _owner.ValidateBufferReadable(this);
168
169             if (startIndex < 0)
170             {
171                 throw new ArgumentOutOfRangeException("startIndex can't be less than zero.");
172             }
173             if (startIndex + length > source.Length)
174             {
175                 throw new ArgumentOutOfRangeException("startIndex + length can't be greater than source.Length.");
176             }
177
178             ValidateRange(offset, length);
179
180             Marshal.Copy(source, startIndex, IntPtr.Add(_dataHandle, offset), length);
181         }
182
183         public void CopyFrom(byte[] source, int startIndex, int length)
184         {
185             CopyFrom(source, startIndex, length, 0);
186         }
187
188         public void CopyTo(byte[] dest, int startIndex, int length, int offset)
189         {
190             _owner.ValidateBufferWritable(this);
191
192             if (startIndex < 0)
193             {
194                 throw new ArgumentOutOfRangeException("Start index can't be less than zero.");
195             }
196             if (startIndex + length > dest.Length)
197             {
198                 throw new ArgumentOutOfRangeException("startIndex + length can't be greater than dest.Length.");
199             }
200
201             ValidateRange(offset, length);
202
203             Marshal.Copy(IntPtr.Add(_dataHandle, offset), dest, startIndex, length);
204         }
205
206         public void CopyTo(byte[] dest, int startIndex, int length)
207         {
208             CopyTo(dest, startIndex, length, 0);
209         }
210
211         public int Length { get; }
212     }
213 }