/*
* 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
{
///
/// Represents the media vision source to keep information on image or video frame data as raw buffer.
///
/// 3
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 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");
}
///
/// Initializes a new instance of the class based on the .
///
/// The from which the source will be filled.
/// The feature is not supported.
/// is null.
/// has already been disposed of.
/// 3
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");
}
///
/// Initializes a new instance of the class based on the buffer and .
///
/// The buffer of image data.
/// The width of image.
/// The height of image.
/// The image .
///
/// The feature is not supported.\n
/// -or-\n
/// is not supported.
///
/// is null.
///
/// has no element.(The length is zero.)\n
/// -or-\n
/// is invalid.
///
/// 3
public MediaVisionSource(byte[] buffer, uint width, uint height, ColorSpace colorSpace)
: this(handle => FillBuffer(handle, buffer, width, height, colorSpace))
{
}
~MediaVisionSource()
{
Dispose(false);
}
private IMediaBuffer _buffer;
///
/// Gets the buffer of the media source.
///
/// The has already been disposed of.
/// 3
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;
}
}
///
/// Gets MediaVision's supported ColorSpace state.
/// true if supported, otherwise false.
///
public static bool IsSupportedColorSpace(ColorSpace colorSpace)
{
return SupportedColorSpaces.Contains(colorSpace);
}
///
/// Gets height of the media source.
///
/// The has already been disposed of.
/// 3
public uint Height
{
get
{
uint height = 0;
var ret = InteropSource.GetHeight(Handle, out height);
MultimediaDebug.AssertNoError(ret);
return height;
}
}
///
/// Gets width of the media source.
///
/// The has already been disposed of.
/// 3
public uint Width
{
get
{
uint width = 0;
var ret = InteropSource.GetWidth(Handle, out width);
MultimediaDebug.AssertNoError(ret);
return width;
}
}
///
/// Gets of the media source.
///
/// The has already been disposed of.
/// 3
public ColorSpace Colorspace
{
get
{
VisionColorSpace visionColorSpace;
var ret = InteropSource.GetColorspace(Handle, out visionColorSpace);
MultimediaDebug.AssertNoError(ret);
return visionColorSpace.ToCommonColorSpace();
}
}
///
/// Gets the supported colorspaces for .
///
public static IEnumerable SupportedColorSpaces
{
get
{
foreach (VisionColorSpace value in Enum.GetValues(typeof(VisionColorSpace)))
{
yield return value.ToCommonColorSpace();
}
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Releases the resources used by the object.
///
///
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
///
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; }
}
}
}