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 public class MediaVisionSource : IBufferOwner, IDisposable
28 private IntPtr _handle = IntPtr.Zero;
29 private bool _disposed = false;
31 internal MediaVisionSource()
33 InteropSource.Create(out _handle).Validate("Failed to create media vision source");
36 private MediaVisionSource(Action<IntPtr> fillAction)
45 InteropSource.Destroy(_handle);
51 private static void FillMediaPacket(IntPtr handle, MediaPacket mediaPacket)
53 Debug.Assert(handle != IntPtr.Zero);
55 if (mediaPacket == null)
57 throw new ArgumentNullException(nameof(mediaPacket));
60 InteropSource.FillMediaPacket(handle, mediaPacket.GetHandle()).
61 Validate("Failed to fill media packet");
65 /// Initializes a new instance of the <see cref="MediaVisionSource"/> class based on the <see cref="MediaPacket"/>.
67 /// <param name="mediaPacket">The <see cref="MediaPacket"/> from which the source will be filled.</param>
68 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
69 /// <exception cref="ArgumentNullException"><paramref name="mediaPacket"/> is null.</exception>
70 /// <exception cref="ObjectDisposedException"><paramref name="mediaPacket"/> has already been disposed of.</exception>
71 public MediaVisionSource(MediaPacket mediaPacket)
72 : this(handle => FillMediaPacket(handle, mediaPacket))
76 private static void FillBuffer(IntPtr handle, byte[] buffer, uint width, uint height, Colorspace colorspace)
78 Debug.Assert(handle != IntPtr.Zero);
82 throw new ArgumentNullException(nameof(buffer));
85 if (buffer.Length == 0)
87 throw new ArgumentException("Buffer.Length is zero.", nameof(buffer));
90 if (colorspace == Colorspace.Invalid)
92 throw new ArgumentException($"color space must not be {Colorspace.Invalid}.", nameof(colorspace));
95 ValidationUtil.ValidateEnum(typeof(Colorspace), colorspace, nameof(colorspace));
97 InteropSource.FillBuffer(handle, buffer, buffer.Length, width, height, colorspace).
98 Validate("Failed to fill buffer");
102 /// Initializes a new instance of the <see cref="MediaVisionSource"/> class based on the buffer and <see cref="Colorspace"/>.
104 /// <param name="buffer">The buffer of image data.</param>
105 /// <param name="width">The width of image.</param>
106 /// <param name="height">The height of image.</param>
107 /// <param name="colorspace">The image <see cref="Colorspace"/>.</param>
108 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
109 /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
110 /// <exception cref="ArgumentException">
111 /// <paramref name="buffer"/> has no element.(The length is zero.)\n
113 /// <paramref name="colorspace"/> is invalid.
115 public MediaVisionSource(byte[] buffer, uint width, uint height, Colorspace colorspace)
116 : this(handle => FillBuffer(handle, buffer, width, height, colorspace))
125 private IMediaBuffer _buffer;
128 /// Gets the buffer of the media source.
130 /// <exception cref="ObjectDisposedException">The <see cref="MediaVisionSource"/> has already been disposed of.</exception>
131 public IMediaBuffer Buffer
137 IntPtr bufferHandle = IntPtr.Zero;
140 InteropSource.GetBuffer(Handle, out bufferHandle, out bufferSize).
141 Validate("Failed to get buffer");
143 _buffer = new DependentMediaBuffer(this, bufferHandle, bufferSize);
150 /// Gets height of the media source.
152 /// <exception cref="ObjectDisposedException">The <see cref="MediaVisionSource"/> has already been disposed of.</exception>
158 var ret = InteropSource.GetHeight(Handle, out height);
159 MultimediaDebug.AssertNoError(ret);
165 /// Gets width of the media source.
167 /// <exception cref="ObjectDisposedException">The <see cref="MediaVisionSource"/> has already been disposed of.</exception>
173 var ret = InteropSource.GetWidth(Handle, out width);
174 MultimediaDebug.AssertNoError(ret);
180 /// Gets <see cref="Colorspace"/> of the media source.
182 /// <exception cref="ObjectDisposedException">The <see cref="MediaVisionSource"/> has already been disposed of.</exception>
183 public Colorspace Colorspace
187 Colorspace colorspace = Colorspace.Invalid;
188 var ret = InteropSource.GetColorspace(Handle, out colorspace);
189 MultimediaDebug.AssertNoError(ret);
194 public void Dispose()
197 GC.SuppressFinalize(this);
200 protected virtual void Dispose(bool disposing)
206 InteropSource.Destroy(_handle);
210 internal IntPtr Handle
216 throw new ObjectDisposedException(nameof(MediaVisionSource));
222 bool IBufferOwner.IsBufferAccessible(object buffer, MediaBufferAccessMode accessMode)
227 bool IBufferOwner.IsDisposed
229 get { return _disposed; }