[Tizen.Multimedia] Fix XML Doc warnings
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Vision / MediaVision / MediaVisionSource.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.Linq;
20 using System.Collections.Generic;
21 using InteropSource = Interop.MediaVision.MediaSource;
22
23 namespace Tizen.Multimedia.Vision
24 {
25     /// <summary>
26     /// Represents the media vision source to keep information on the image or video frame data as raw buffer.
27     /// </summary>
28     /// <since_tizen> 3 </since_tizen>
29     public class MediaVisionSource : IBufferOwner, IDisposable
30     {
31         private IntPtr _handle = IntPtr.Zero;
32         private bool _disposed = false;
33
34         internal MediaVisionSource()
35         {
36             InteropSource.Create(out _handle).Validate("Failed to create media vision source");
37         }
38
39         private MediaVisionSource(Action<IntPtr> fillAction)
40             : this()
41         {
42             try
43             {
44                 fillAction(_handle);
45             }
46             catch (Exception)
47             {
48                 InteropSource.Destroy(_handle);
49                 _disposed = true;
50                 throw;
51             }
52         }
53
54         private static void FillMediaPacket(IntPtr handle, MediaPacket mediaPacket)
55         {
56             Debug.Assert(handle != IntPtr.Zero);
57
58             if (mediaPacket == null)
59             {
60                 throw new ArgumentNullException(nameof(mediaPacket));
61             }
62
63             InteropSource.FillMediaPacket(handle, mediaPacket.GetHandle()).
64                 Validate("Failed to fill media packet");
65         }
66
67         /// <summary>
68         /// Initializes a new instance of the <see cref="MediaVisionSource"/> class based on the <see cref="MediaPacket"/>.
69         /// </summary>
70         /// <param name="mediaPacket">The <see cref="MediaPacket"/> from which the source will be filled.</param>
71         /// <exception cref="NotSupportedException">The feature is not supported.</exception>
72         /// <exception cref="ArgumentNullException"><paramref name="mediaPacket"/> is null.</exception>
73         /// <exception cref="ObjectDisposedException"><paramref name="mediaPacket"/> has already been disposed of.</exception>
74         /// <since_tizen> 3 </since_tizen>
75         public MediaVisionSource(MediaPacket mediaPacket)
76             : this(handle => FillMediaPacket(handle, mediaPacket))
77         {
78         }
79
80         private static void FillBuffer(IntPtr handle, byte[] buffer, uint width, uint height, ColorSpace colorSpace)
81         {
82             Debug.Assert(handle != IntPtr.Zero);
83
84             if (buffer == null)
85             {
86                 throw new ArgumentNullException(nameof(buffer));
87             }
88
89             if (buffer.Length == 0)
90             {
91                 throw new ArgumentException("Buffer.Length is zero.", nameof(buffer));
92             }
93
94             ValidationUtil.ValidateEnum(typeof(ColorSpace), colorSpace, nameof(colorSpace));
95
96             InteropSource.FillBuffer(handle, buffer, buffer.Length, width, height, colorSpace.ToVisionColorSpace()).
97                 Validate("Failed to fill buffer");
98         }
99
100         /// <summary>
101         /// Initializes a new instance of the <see cref="MediaVisionSource"/> class based on the buffer and <see cref="ColorSpace"/>.
102         /// </summary>
103         /// <param name="buffer">The buffer of image data.</param>
104         /// <param name="width">The width of image.</param>
105         /// <param name="height">The height of image.</param>
106         /// <param name="colorSpace">The image <see cref="ColorSpace"/>.</param>
107         /// <exception cref="NotSupportedException">
108         ///     The feature is not supported.\n
109         ///     -or-\n
110         ///     <paramref name="colorSpace"/> is not supported.
111         /// </exception>
112         /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
113         /// <exception cref="ArgumentException">
114         ///     <paramref name="buffer"/> has no element.(The length is zero.)\n
115         ///     -or-\n
116         ///     <paramref name="colorSpace"/> is invalid.
117         /// </exception>
118         /// <since_tizen> 3 </since_tizen>
119         public MediaVisionSource(byte[] buffer, uint width, uint height, ColorSpace colorSpace)
120             : this(handle => FillBuffer(handle, buffer, width, height, colorSpace))
121         {
122         }
123
124         /// <summary>
125         /// Finalizes an instance of the MediaVisionSource class.
126         /// </summary>
127         ~MediaVisionSource()
128         {
129             Dispose(false);
130         }
131
132         private IMediaBuffer _buffer;
133
134         /// <summary>
135         /// Gets the buffer of the media source.
136         /// </summary>
137         /// <exception cref="ObjectDisposedException">The <see cref="MediaVisionSource"/> has already been disposed of.</exception>
138         /// <since_tizen> 3 </since_tizen>
139         public IMediaBuffer Buffer
140         {
141             get
142             {
143                 if (_buffer == null)
144                 {
145                     IntPtr bufferHandle = IntPtr.Zero;
146                     int bufferSize = 0;
147
148                     InteropSource.GetBuffer(Handle, out bufferHandle, out bufferSize).
149                         Validate("Failed to get buffer");
150
151                     _buffer = new DependentMediaBuffer(this, bufferHandle, bufferSize);
152                 }
153                 return _buffer;
154             }
155         }
156
157         /// <summary>
158         /// Gets MediaVision's supported ColorSpace state.
159         /// true if supported, otherwise false.
160         /// </summary>
161         public static bool IsSupportedColorSpace(ColorSpace colorSpace)
162         {
163             return SupportedColorSpaces.Contains(colorSpace);
164         }
165
166         /// <summary>
167         /// Gets the height of the media source.
168         /// </summary>
169         /// <exception cref="ObjectDisposedException">The <see cref="MediaVisionSource"/> has already been disposed of.</exception>
170         /// <since_tizen> 3 </since_tizen>
171         public uint Height
172         {
173             get
174             {
175                 uint height = 0;
176                 var ret = InteropSource.GetHeight(Handle, out height);
177                 MultimediaDebug.AssertNoError(ret);
178                 return height;
179             }
180         }
181
182         /// <summary>
183         /// Gets the width of the media source.
184         /// </summary>
185         /// <exception cref="ObjectDisposedException">The <see cref="MediaVisionSource"/> has already been disposed of.</exception>
186         /// <since_tizen> 3 </since_tizen>
187         public uint Width
188         {
189             get
190             {
191                 uint width = 0;
192                 var ret = InteropSource.GetWidth(Handle, out width);
193                 MultimediaDebug.AssertNoError(ret);
194                 return width;
195             }
196         }
197
198         /// <summary>
199         /// Gets <see cref="ColorSpace"/> of the media source.
200         /// </summary>
201         /// <exception cref="ObjectDisposedException">The <see cref="MediaVisionSource"/> has already been disposed of.</exception>
202         /// <since_tizen> 3 </since_tizen>
203         public ColorSpace Colorspace
204         {
205             get
206             {
207                 VisionColorSpace visionColorSpace;
208
209                 var ret = InteropSource.GetColorspace(Handle, out visionColorSpace);
210                 MultimediaDebug.AssertNoError(ret);
211                 return visionColorSpace.ToCommonColorSpace();
212             }
213         }
214
215         /// <summary>
216         /// Gets the supported colorspaces for <see cref="MediaVisionSource"/>.
217         /// </summary>
218         public static IEnumerable<ColorSpace> SupportedColorSpaces
219         {
220             get
221             {
222                 foreach (VisionColorSpace value in Enum.GetValues(typeof(VisionColorSpace)))
223                 {
224                     yield return value.ToCommonColorSpace();
225                 }
226             }
227         }
228
229         /// <summary>
230         /// Releases all resources used by the current instance.
231         /// </summary>
232         public void Dispose()
233         {
234             Dispose(true);
235             GC.SuppressFinalize(this);
236         }
237
238         /// <summary>
239         /// Releases the resources used by the <see cref="MediaVisionSource"/> object.
240         /// </summary>
241         /// <param name="disposing">
242         /// true to release both managed and unmanaged resources; otherwise false to release only unmanaged resources.
243         /// </param>
244         protected virtual void Dispose(bool disposing)
245         {
246             if (_disposed)
247             {
248                 return;
249             }
250             InteropSource.Destroy(_handle);
251             _disposed = true;
252         }
253
254         internal IntPtr Handle
255         {
256             get
257             {
258                 if (_disposed)
259                 {
260                     throw new ObjectDisposedException(nameof(MediaVisionSource));
261                 }
262                 return _handle;
263             }
264         }
265
266         bool IBufferOwner.IsBufferAccessible(object buffer, MediaBufferAccessMode accessMode)
267         {
268             return true;
269         }
270
271         bool IBufferOwner.IsDisposed
272         {
273             get { return _disposed; }
274         }
275     }
276 }