/*
* 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 Tizen.Internals.Errors;
namespace Tizen.Multimedia
{
///
/// Represents a video media format. This class cannot be inherited.
///
public sealed class VideoMediaFormat : MediaFormat
{
private const int DefaultFrameRate = 0;
private const int DefaultBitRate = 0;
///
/// Initializes a new instance of the VideoMediaFormat class with the specified mime type, width, and height.
///
/// The mime type of the format.
/// The width value of the format.
/// The height value of the format
/// is invalid (i.e. undefined value).
/// or is less than zero.
public VideoMediaFormat(MediaFormatVideoMimeType mimeType, int width, int height)
: this(mimeType, width, height, DefaultFrameRate)
{
}
///
/// Initializes a new instance of the VideoMediaFormat class with the specified mime type and size.
///
/// The mime type of the format.
/// The size of the format.
/// is invalid (i.e. undefined value).
/// The width or the height of is less than zero.
public VideoMediaFormat(MediaFormatVideoMimeType mimeType, Size size)
: this(mimeType, size, DefaultFrameRate)
{
}
///
/// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
/// width, height, and frame rate.
///
/// The mime type of the format.
/// The width value of the format.
/// The height value of the format
/// The frame rate of the format.
/// is invalid (i.e. undefined value).
///
/// , , or is less than zero.
///
public VideoMediaFormat(MediaFormatVideoMimeType mimeType, int width, int height, int frameRate)
: this(mimeType, width, height, frameRate, DefaultBitRate)
{
}
///
/// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
/// width, height, and frame rate.
///
/// The mime type of the format.
/// The video size of the format.
/// The frame rate of the format.
/// is invalid (i.e. undefined value).
///
/// The width or the height of is less than zero.\n
/// -or-\n
/// is less than zero.
///
public VideoMediaFormat(MediaFormatVideoMimeType mimeType, Size size,
int frameRate)
: this(mimeType, size, frameRate, DefaultBitRate)
{
}
///
/// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
/// width, height, frame rate, and bit rate.
///
/// The mime type of the format.
/// The width value of the format.
/// The height value of the format
/// The frame rate of the format.
/// The bit rate of the format.
/// is invalid (i.e. undefined value).
///
/// , , , or is less than zero.
///
public VideoMediaFormat(MediaFormatVideoMimeType mimeType, int width, int height,
int frameRate, int bitRate)
: this(mimeType, new Size(width, height), frameRate, bitRate)
{
}
///
/// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
/// size, frame rate, and bit rate.
///
/// The mime type of the format.
/// The size of the format.
/// The frame rate of the format.
/// The bit rate of the format.
/// is invalid (i.e. undefined value).
///
/// The width or the height of is less than zero.\n
/// -or-\n
/// is less than zero.\n
/// -or-\n
/// is less than zero.
///
public VideoMediaFormat(MediaFormatVideoMimeType mimeType, Size size,
int frameRate, int bitRate)
: base(MediaFormatType.Video)
{
if (!Enum.IsDefined(typeof(MediaFormatVideoMimeType), mimeType))
{
throw new ArgumentException($"Invalid mime type value : { (int)mimeType }");
}
if (size.Width < 0)
{
throw new ArgumentOutOfRangeException(nameof(size), size.Width, "Size.Width value can't be less than zero.");
}
if (size.Height < 0)
{
throw new ArgumentOutOfRangeException(nameof(size), size.Height, "Size.Height value can't be less than zero.");
}
if (frameRate < 0)
{
throw new ArgumentOutOfRangeException(nameof(frameRate), frameRate, "Frame rate can't be less than zero.");
}
if (bitRate < 0)
{
throw new ArgumentOutOfRangeException(nameof(bitRate), bitRate, "Bit rate value can't be less than zero.");
}
MimeType = mimeType;
Size = size;
FrameRate = frameRate;
BitRate = bitRate;
}
///
/// Initializes a new instance of the VideoMediaForma class from a native handle.
///
/// A native handle.
internal VideoMediaFormat(IntPtr handle)
: base(MediaFormatType.Video)
{
Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
int width = 0;
int height = 0;
int bitRate = 0;
int frameRate = 0;
MediaFormatVideoMimeType mimeType;
GetInfo(handle, out width, out height, out bitRate, out mimeType);
GetFrameRate(handle, out frameRate);
MimeType = mimeType;
Size = new Size(width, height);
FrameRate = frameRate;
BitRate = bitRate;
}
///
/// Retrieves video properties of the media format from a native handle.
///
/// A native handle that the properties are retrieved from.
/// An out parameter for the width.
/// An out parameter for the height.
/// An out parameter for the bit rate.
/// An out parameter for the mime type.
private static void GetInfo(IntPtr handle, out int width, out int height, out int bitRate,
out MediaFormatVideoMimeType mimeType)
{
Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
int mimeTypeValue = 0;
int maxBps = 0;
int ret = Interop.MediaFormat.GetVideoInfo(handle,
out mimeTypeValue, out width, out height, out bitRate, out maxBps);
MultimediaDebug.AssertNoError(ret);
mimeType = (MediaFormatVideoMimeType)mimeTypeValue;
Debug.Assert(Enum.IsDefined(typeof(MediaFormatVideoMimeType), mimeType),
"Invalid video mime type!");
}
///
/// Retrieves frame rate from a native handle.
///
/// A native handle that the properties are retrieved from.
/// An out parameter for the frame rate.
private static void GetFrameRate(IntPtr handle, out int frameRate)
{
Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
int ret = Interop.MediaFormat.GetVideoFrameRate(handle, out frameRate);
MultimediaDebug.AssertNoError(ret);
}
internal override void AsNativeHandle(IntPtr handle)
{
Debug.Assert(Type == MediaFormatType.Video);
int ret = Interop.MediaFormat.SetVideoMimeType(handle, (int)MimeType);
MultimediaDebug.AssertNoError(ret);
ret = Interop.MediaFormat.SetVideoWidth(handle, Size.Width);
MultimediaDebug.AssertNoError(ret);
ret = Interop.MediaFormat.SetVideoHeight(handle, Size.Height);
MultimediaDebug.AssertNoError(ret);
ret = Interop.MediaFormat.SetVideoAverageBps(handle, BitRate);
MultimediaDebug.AssertNoError(ret);
ret = Interop.MediaFormat.SetVideoFrameRate(handle, FrameRate);
MultimediaDebug.AssertNoError(ret);
}
///
/// Gets the mime type of the current format.
///
public MediaFormatVideoMimeType MimeType { get; }
///
/// Gets the size of the current format.
///
public Size Size { get; }
///
/// Gets the frame rate value of the current format.
///
public int FrameRate { get; }
///
/// Gets the bit rate value of the current format.
///
public int BitRate { get; }
///
/// Returns a string that represents the current object.
///
/// A string that represents the current object.
public override string ToString()
=> $@"MimeType={ MimeType.ToString() }, Size=({ Size.ToString() }), FrameRate=
{ FrameRate.ToString() }, BitRate={ BitRate.ToString() }";
///
/// Compares an object to an instance of for equality.
///
/// A to compare.
/// true if the formats are equal; otherwise, false.
public override bool Equals(object obj)
{
var rhs = obj as VideoMediaFormat;
if (rhs == null)
{
return false;
}
return MimeType == rhs.MimeType && Size == rhs.Size &&
FrameRate == rhs.FrameRate && BitRate == rhs.BitRate;
}
///
/// Gets the hash code for this instance of .
///
/// The hash code for this instance of .
public override int GetHashCode()
=> new { MimeType, Size, FrameRate, BitRate }.GetHashCode();
}
}