[Vision] Modify vision colorsapce to multimedia common colorspace
[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 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         ~MediaVisionSource()
125         {
126             Dispose(false);
127         }
128
129         private IMediaBuffer _buffer;
130
131         /// <summary>
132         /// Gets the buffer of the media source.
133         /// </summary>
134         /// <exception cref="ObjectDisposedException">The <see cref="MediaVisionSource"/> has already been disposed of.</exception>
135         /// <since_tizen> 3</since_tizen>
136         public IMediaBuffer Buffer
137         {
138             get
139             {
140                 if (_buffer == null)
141                 {
142                     IntPtr bufferHandle = IntPtr.Zero;
143                     int bufferSize = 0;
144
145                     InteropSource.GetBuffer(Handle, out bufferHandle, out bufferSize).
146                         Validate("Failed to get buffer");
147
148                     _buffer = new DependentMediaBuffer(this, bufferHandle, bufferSize);
149                 }
150                 return _buffer;
151             }
152         }
153
154         /// <summary>
155         /// Gets MediaVision's supported ColorSpace state.
156         /// true if supported, otherwise false.
157         /// </summary>
158         public static bool IsSupportedColorSpace(ColorSpace colorSpace)
159         {
160             return SupportedColorSpaces.Contains(colorSpace);
161         }
162
163         /// <summary>
164         /// Gets height of the media source.
165         /// </summary>
166         /// <exception cref="ObjectDisposedException">The <see cref="MediaVisionSource"/> has already been disposed of.</exception>
167         /// <since_tizen> 3</since_tizen>
168         public uint Height
169         {
170             get
171             {
172                 uint height = 0;
173                 var ret = InteropSource.GetHeight(Handle, out height);
174                 MultimediaDebug.AssertNoError(ret);
175                 return height;
176             }
177         }
178
179         /// <summary>
180         /// Gets width of the media source.
181         /// </summary>
182         /// <exception cref="ObjectDisposedException">The <see cref="MediaVisionSource"/> has already been disposed of.</exception>
183         /// <since_tizen> 3</since_tizen>
184         public uint Width
185         {
186             get
187             {
188                 uint width = 0;
189                 var ret = InteropSource.GetWidth(Handle, out width);
190                 MultimediaDebug.AssertNoError(ret);
191                 return width;
192             }
193         }
194
195         /// <summary>
196         /// Gets <see cref="ColorSpace"/> of the media source.
197         /// </summary>
198         /// <exception cref="ObjectDisposedException">The <see cref="MediaVisionSource"/> has already been disposed of.</exception>
199         /// <since_tizen> 3</since_tizen>
200         public ColorSpace Colorspace
201         {
202             get
203             {
204                 VisionColorSpace visionColorSpace;
205
206                 var ret = InteropSource.GetColorspace(Handle, out visionColorSpace);
207                 MultimediaDebug.AssertNoError(ret);
208                 return visionColorSpace.ToCommonColorSpace();
209             }
210         }
211
212         /// <summary>
213         /// Gets the supported colorspaces for <see cref="MediaVisionSource"/>.
214         /// </summary>
215         public static IEnumerable<ColorSpace> SupportedColorSpaces
216         {
217             get
218             {
219                 foreach (VisionColorSpace value in Enum.GetValues(typeof(VisionColorSpace)))
220                 {
221                     yield return value.ToCommonColorSpace();
222                 }
223             }
224         }
225
226         public void Dispose()
227         {
228             Dispose(true);
229             GC.SuppressFinalize(this);
230         }
231
232         /// <summary>
233         /// Releases the resources used by the <see cref="MediaVisionSource"/> object.
234         /// </summary>
235         /// <param name="disposing">
236         /// true to release both managed and unmanaged resources; false to release only unmanaged resources.
237         /// </param>
238         protected virtual void Dispose(bool disposing)
239         {
240             if (_disposed)
241             {
242                 return;
243             }
244             InteropSource.Destroy(_handle);
245             _disposed = true;
246         }
247
248         internal IntPtr Handle
249         {
250             get
251             {
252                 if (_disposed)
253                 {
254                     throw new ObjectDisposedException(nameof(MediaVisionSource));
255                 }
256                 return _handle;
257             }
258         }
259
260         bool IBufferOwner.IsBufferAccessible(object buffer, MediaBufferAccessMode accessMode)
261         {
262             return true;
263         }
264
265         bool IBufferOwner.IsDisposed
266         {
267             get { return _disposed; }
268         }
269     }
270 }