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