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;
20 using System.Collections.Generic;
21 using InteropSource = Interop.MediaVision.MediaSource;
23 namespace Tizen.Multimedia.Vision
26 /// Represents the media vision source to keep information on image or video frame data as raw buffer.
28 /// <since_tizen> 3</since_tizen>
29 public class MediaVisionSource : IBufferOwner, IDisposable
31 private IntPtr _handle = IntPtr.Zero;
32 private bool _disposed = false;
34 internal MediaVisionSource()
36 InteropSource.Create(out _handle).Validate("Failed to create media vision source");
39 private MediaVisionSource(Action<IntPtr> fillAction)
48 InteropSource.Destroy(_handle);
54 private static void FillMediaPacket(IntPtr handle, MediaPacket mediaPacket)
56 Debug.Assert(handle != IntPtr.Zero);
58 if (mediaPacket == null)
60 throw new ArgumentNullException(nameof(mediaPacket));
63 InteropSource.FillMediaPacket(handle, mediaPacket.GetHandle()).
64 Validate("Failed to fill media packet");
68 /// Initializes a new instance of the <see cref="MediaVisionSource"/> class based on the <see cref="MediaPacket"/>.
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))
80 private static void FillBuffer(IntPtr handle, byte[] buffer, uint width, uint height, ColorSpace colorSpace)
82 Debug.Assert(handle != IntPtr.Zero);
86 throw new ArgumentNullException(nameof(buffer));
89 if (buffer.Length == 0)
91 throw new ArgumentException("Buffer.Length is zero.", nameof(buffer));
94 ValidationUtil.ValidateEnum(typeof(ColorSpace), colorSpace, nameof(colorSpace));
96 InteropSource.FillBuffer(handle, buffer, buffer.Length, width, height, colorSpace.ToVisionColorSpace()).
97 Validate("Failed to fill buffer");
101 /// Initializes a new instance of the <see cref="MediaVisionSource"/> class based on the buffer and <see cref="ColorSpace"/>.
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
110 /// <paramref name="colorSpace"/> is not supported.
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
116 /// <paramref name="colorSpace"/> is invalid.
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))
129 private IMediaBuffer _buffer;
132 /// Gets the buffer of the media source.
134 /// <exception cref="ObjectDisposedException">The <see cref="MediaVisionSource"/> has already been disposed of.</exception>
135 /// <since_tizen> 3</since_tizen>
136 public IMediaBuffer Buffer
142 IntPtr bufferHandle = IntPtr.Zero;
145 InteropSource.GetBuffer(Handle, out bufferHandle, out bufferSize).
146 Validate("Failed to get buffer");
148 _buffer = new DependentMediaBuffer(this, bufferHandle, bufferSize);
155 /// Gets MediaVision's supported ColorSpace state.
156 /// true if supported, otherwise false.
158 public static bool IsSupportedColorSpace(ColorSpace colorSpace)
160 return SupportedColorSpaces.Contains(colorSpace);
164 /// Gets height of the media source.
166 /// <exception cref="ObjectDisposedException">The <see cref="MediaVisionSource"/> has already been disposed of.</exception>
167 /// <since_tizen> 3</since_tizen>
173 var ret = InteropSource.GetHeight(Handle, out height);
174 MultimediaDebug.AssertNoError(ret);
180 /// Gets width of the media source.
182 /// <exception cref="ObjectDisposedException">The <see cref="MediaVisionSource"/> has already been disposed of.</exception>
183 /// <since_tizen> 3</since_tizen>
189 var ret = InteropSource.GetWidth(Handle, out width);
190 MultimediaDebug.AssertNoError(ret);
196 /// Gets <see cref="ColorSpace"/> of the media source.
198 /// <exception cref="ObjectDisposedException">The <see cref="MediaVisionSource"/> has already been disposed of.</exception>
199 /// <since_tizen> 3</since_tizen>
200 public ColorSpace Colorspace
204 VisionColorSpace visionColorSpace;
206 var ret = InteropSource.GetColorspace(Handle, out visionColorSpace);
207 MultimediaDebug.AssertNoError(ret);
208 return visionColorSpace.ToCommonColorSpace();
213 /// Gets the supported colorspaces for <see cref="MediaVisionSource"/>.
215 public static IEnumerable<ColorSpace> SupportedColorSpaces
219 foreach (VisionColorSpace value in Enum.GetValues(typeof(VisionColorSpace)))
221 yield return value.ToCommonColorSpace();
226 public void Dispose()
229 GC.SuppressFinalize(this);
233 /// Releases the resources used by the <see cref="MediaVisionSource"/> object.
235 /// <param name="disposing">
236 /// true to release both managed and unmanaged resources; false to release only unmanaged resources.
238 protected virtual void Dispose(bool disposing)
244 InteropSource.Destroy(_handle);
248 internal IntPtr Handle
254 throw new ObjectDisposedException(nameof(MediaVisionSource));
260 bool IBufferOwner.IsBufferAccessible(object buffer, MediaBufferAccessMode accessMode)
265 bool IBufferOwner.IsDisposed
267 get { return _disposed; }