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;
19 using InteropSource = Interop.MediaVision.MediaSource;
21 namespace Tizen.Multimedia
24 /// Represents the media vision source to keep information on image or video frame data as raw buffer.
26 /// <since_tizen> 3</since_tizen>
27 public class MediaVisionSource : IBufferOwner, IDisposable
29 private IntPtr _handle = IntPtr.Zero;
30 private bool _disposed = false;
32 internal MediaVisionSource()
34 InteropSource.Create(out _handle).Validate("Failed to create media vision source");
37 private MediaVisionSource(Action<IntPtr> fillAction)
46 InteropSource.Destroy(_handle);
52 private static void FillMediaPacket(IntPtr handle, MediaPacket mediaPacket)
54 Debug.Assert(handle != IntPtr.Zero);
56 if (mediaPacket == null)
58 throw new ArgumentNullException(nameof(mediaPacket));
61 InteropSource.FillMediaPacket(handle, mediaPacket.GetHandle()).
62 Validate("Failed to fill media packet");
66 /// Initializes a new instance of the <see cref="MediaVisionSource"/> class based on the <see cref="MediaPacket"/>.
68 /// <param name="mediaPacket">The <see cref="MediaPacket"/> from which the source will be filled.</param>
69 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
70 /// <exception cref="ArgumentNullException"><paramref name="mediaPacket"/> is null.</exception>
71 /// <exception cref="ObjectDisposedException"><paramref name="mediaPacket"/> has already been disposed of.</exception>
72 /// <since_tizen> 3</since_tizen>
73 public MediaVisionSource(MediaPacket mediaPacket)
74 : this(handle => FillMediaPacket(handle, mediaPacket))
78 private static void FillBuffer(IntPtr handle, byte[] buffer, uint width, uint height, Colorspace colorspace)
80 Debug.Assert(handle != IntPtr.Zero);
84 throw new ArgumentNullException(nameof(buffer));
87 if (buffer.Length == 0)
89 throw new ArgumentException("Buffer.Length is zero.", nameof(buffer));
92 if (colorspace == Colorspace.Invalid)
94 throw new ArgumentException($"color space must not be {Colorspace.Invalid}.", nameof(colorspace));
97 ValidationUtil.ValidateEnum(typeof(Colorspace), colorspace, nameof(colorspace));
99 InteropSource.FillBuffer(handle, buffer, buffer.Length, width, height, colorspace).
100 Validate("Failed to fill buffer");
104 /// Initializes a new instance of the <see cref="MediaVisionSource"/> class based on the buffer and <see cref="Colorspace"/>.
106 /// <param name="buffer">The buffer of image data.</param>
107 /// <param name="width">The width of image.</param>
108 /// <param name="height">The height of image.</param>
109 /// <param name="colorspace">The image <see cref="Colorspace"/>.</param>
110 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
111 /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
112 /// <exception cref="ArgumentException">
113 /// <paramref name="buffer"/> has no element.(The length is zero.)\n
115 /// <paramref name="colorspace"/> is invalid.
117 /// <since_tizen> 3</since_tizen>
118 public MediaVisionSource(byte[] buffer, uint width, uint height, Colorspace colorspace)
119 : this(handle => FillBuffer(handle, buffer, width, height, colorspace))
128 private IMediaBuffer _buffer;
131 /// Gets the buffer of the media source.
133 /// <exception cref="ObjectDisposedException">The <see cref="MediaVisionSource"/> has already been disposed of.</exception>
134 /// <since_tizen> 3</since_tizen>
135 public IMediaBuffer Buffer
141 IntPtr bufferHandle = IntPtr.Zero;
144 InteropSource.GetBuffer(Handle, out bufferHandle, out bufferSize).
145 Validate("Failed to get buffer");
147 _buffer = new DependentMediaBuffer(this, bufferHandle, bufferSize);
154 /// Gets height of the media source.
156 /// <exception cref="ObjectDisposedException">The <see cref="MediaVisionSource"/> has already been disposed of.</exception>
157 /// <since_tizen> 3</since_tizen>
163 var ret = InteropSource.GetHeight(Handle, out height);
164 MultimediaDebug.AssertNoError(ret);
170 /// Gets width of the media source.
172 /// <exception cref="ObjectDisposedException">The <see cref="MediaVisionSource"/> has already been disposed of.</exception>
173 /// <since_tizen> 3</since_tizen>
179 var ret = InteropSource.GetWidth(Handle, out width);
180 MultimediaDebug.AssertNoError(ret);
186 /// Gets <see cref="Colorspace"/> of the media source.
188 /// <exception cref="ObjectDisposedException">The <see cref="MediaVisionSource"/> has already been disposed of.</exception>
189 /// <since_tizen> 3</since_tizen>
190 public Colorspace Colorspace
194 Colorspace colorspace = Colorspace.Invalid;
195 var ret = InteropSource.GetColorspace(Handle, out colorspace);
196 MultimediaDebug.AssertNoError(ret);
202 /// Releases all resources used by the <see cref="MediaVisionSource"/> object.
204 public void Dispose()
207 GC.SuppressFinalize(this);
211 /// Releases the resources used by the <see cref="MediaVisionSource"/> object.
213 /// <param name="disposing">
214 /// true to release both managed and unmanaged resources; false to release only unmanaged resources.
216 protected virtual void Dispose(bool disposing)
222 InteropSource.Destroy(_handle);
226 internal IntPtr Handle
232 throw new ObjectDisposedException(nameof(MediaVisionSource));
238 bool IBufferOwner.IsBufferAccessible(object buffer, MediaBufferAccessMode accessMode)
243 bool IBufferOwner.IsDisposed
245 get { return _disposed; }