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.
17 using System.Diagnostics;
18 using Tizen.Internals.Errors;
20 namespace Tizen.Multimedia
23 /// Represents a video media format. This class cannot be inherited.
25 public sealed class VideoMediaFormat : MediaFormat
27 private const int DefaultFrameRate = 0;
28 private const int DefaultBitRate = 0;
31 /// Initializes a new instance of the VideoMediaFormat class with the specified mime type, width and height.
33 /// <param name="mimeType">The mime type of the format.</param>
34 /// <param name="width">The width value of the format.</param>
35 /// <param name="height">The height value of the format</param>
36 /// <exception cref="ArgumentException">mimeType is invalid(i.e. undefined value).</exception>
37 /// <exception cref="ArgumentOutOfRangeException">width, or height is less than zero.</exception>
38 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, int width, int height)
39 : this(mimeType, width, height, DefaultFrameRate)
44 /// Initializes a new instance of the VideoMediaFormat class with the specified mime type and size.
46 /// <param name="mimeType">The mime type of the format.</param>
47 /// <param name="size">The size of the format.</param>
48 /// <exception cref="ArgumentException">mimeType is invalid(i.e. undefined value).</exception>
49 /// <exception cref="ArgumentOutOfRangeException">width, or height is less than zero.</exception>
50 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, Size size)
51 : this(mimeType, size, DefaultFrameRate)
56 /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
57 /// width, height and frame rate.
59 /// <param name="mimeType">The mime type of the format.</param>
60 /// <param name="width">The width value of the format.</param>
61 /// <param name="height">The height value of the format</param>
62 /// <param name="frameRate">The frame rate of the format.</param>
63 /// <exception cref="ArgumentException">mimeType is invalid(i.e. undefined value).</exception>
64 /// <exception cref="ArgumentOutOfRangeException">width, height or frameRate is less than zero.</exception>
65 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, int width, int height,
67 : this(mimeType, width, height, frameRate, DefaultBitRate)
72 /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
73 /// width, height and frame rate.
75 /// <param name="mimeType">The mime type of the format.</param>
76 /// <param name="size">The video size of the format.</param>
77 /// <param name="frameRate">The frame rate of the format.</param>
78 /// <exception cref="ArgumentException">mimeType is invalid(i.e. undefined value).</exception>
79 /// <exception cref="ArgumentOutOfRangeException">width, height or frameRate is less than zero.</exception>
80 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, Size size,
82 : this(mimeType, size, frameRate, DefaultBitRate)
87 /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
88 /// width, height, frame rate and bit rate.
90 /// <param name="mimeType">The mime type of the format.</param>
91 /// <param name="width">The width value of the format.</param>
92 /// <param name="height">The height value of the format</param>
93 /// <param name="frameRate">The frame rate of the format.</param>
94 /// <param name="bitRate">The bit rate of the format.</param>
95 /// <exception cref="ArgumentException">mimeType is invalid(i.e. undefined value).</exception>
96 /// <exception cref="ArgumentOutOfRangeException">width, height, frameRate or bitRate is less than zero.</exception>
97 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, int width, int height,
98 int frameRate, int bitRate)
99 : this(mimeType, new Size(width, height), frameRate, bitRate)
104 /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
105 /// size, frame rate and bit rate.
107 /// <param name="mimeType">The mime type of the format.</param>
108 /// <param name="size">The size of the format.</param>
109 /// <param name="frameRate">The frame rate of the format.</param>
110 /// <param name="bitRate">The bit rate of the format.</param>
111 /// <exception cref="ArgumentException">mimeType is invalid(i.e. undefined value).</exception>
112 /// <exception cref="ArgumentOutOfRangeException">width, height, frameRate or bitRate is less than zero.</exception>
113 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, Size size,
114 int frameRate, int bitRate)
115 : base(MediaFormatType.Video)
117 if (!Enum.IsDefined(typeof(MediaFormatVideoMimeType), mimeType))
119 throw new ArgumentException($"Invalid mime type value : { (int)mimeType }");
123 throw new ArgumentOutOfRangeException(nameof(size), size.Width, "Size.Width value can't be less than zero.");
127 throw new ArgumentOutOfRangeException(nameof(size), size.Height, "Size.Height value can't be less than zero.");
131 throw new ArgumentOutOfRangeException(nameof(frameRate), frameRate, "Frame rate can't be less than zero.");
135 throw new ArgumentOutOfRangeException(nameof(bitRate), bitRate, "Bit rate value can't be less than zero.");
140 FrameRate = frameRate;
145 /// Initializes a new instance of the VideoMediaForma class from a native handle.
147 /// <param name="handle">A native handle.</param>
148 internal VideoMediaFormat(IntPtr handle)
149 : base(MediaFormatType.Video)
151 Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
157 MediaFormatVideoMimeType mimeType;
158 GetInfo(handle, out width, out height, out bitRate, out mimeType);
160 GetFrameRate(handle, out frameRate);
163 Size = new Size(width, height);
164 FrameRate = frameRate;
169 /// Retrieves video properties of media format from a native handle.
171 /// <param name="handle">A native handle that properties are retrieved from.</param>
172 /// <param name="width">An out parameter for width.</param>
173 /// <param name="height">An out parameter for height.</param>
174 /// <param name="bitRate">An out parameter for bit rate.</param>
175 /// <param name="mimeType">An out parameter for mime type.</param>
176 private static void GetInfo(IntPtr handle, out int width, out int height, out int bitRate,
177 out MediaFormatVideoMimeType mimeType)
179 Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
181 int mimeTypeValue = 0;
184 int ret = Interop.MediaFormat.GetVideoInfo(handle,
185 out mimeTypeValue, out width, out height, out bitRate, out maxBps);
187 MultimediaDebug.AssertNoError(ret);
189 mimeType = (MediaFormatVideoMimeType)mimeTypeValue;
191 Debug.Assert(Enum.IsDefined(typeof(MediaFormatVideoMimeType), mimeType),
192 "Invalid video mime type!");
196 /// Retrieves frame rate from a native handle.
198 /// <param name="handle">A native handle that properties are retrieved from.</param>
199 /// <param name="frameRate">An out parameter for frame rate.</param>
200 private static void GetFrameRate(IntPtr handle, out int frameRate)
202 Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
204 int ret = Interop.MediaFormat.GetVideoFrameRate(handle, out frameRate);
206 MultimediaDebug.AssertNoError(ret);
209 internal override void AsNativeHandle(IntPtr handle)
211 Debug.Assert(Type == MediaFormatType.Video);
213 int ret = Interop.MediaFormat.SetVideoMimeType(handle, (int)MimeType);
214 MultimediaDebug.AssertNoError(ret);
216 ret = Interop.MediaFormat.SetVideoWidth(handle, Size.Width);
217 MultimediaDebug.AssertNoError(ret);
219 ret = Interop.MediaFormat.SetVideoHeight(handle, Size.Height);
220 MultimediaDebug.AssertNoError(ret);
222 ret = Interop.MediaFormat.SetVideoAverageBps(handle, BitRate);
223 MultimediaDebug.AssertNoError(ret);
225 ret = Interop.MediaFormat.SetVideoFrameRate(handle, FrameRate);
226 MultimediaDebug.AssertNoError(ret);
230 /// Gets the mime type of the current format.
232 public MediaFormatVideoMimeType MimeType { get; }
235 /// Gets the size of the current format.
237 public Size Size { get; }
240 /// Gets the frame rate value of the current format.
242 public int FrameRate { get; }
245 /// Gets the bit rate value of the current format.
247 public int BitRate { get; }
249 public override string ToString()
251 return $@"MimeType={ MimeType.ToString() }, Size=({ Size.ToString() }), FrameRate=
252 { FrameRate.ToString() }, BitRate={ BitRate.ToString() }";
255 public override bool Equals(object obj)
257 var rhs = obj as VideoMediaFormat;
263 return MimeType == rhs.MimeType && Size == rhs.Size &&
264 FrameRate == rhs.FrameRate && BitRate == rhs.BitRate;
267 public override int GetHashCode()
269 return new { MimeType, Size, FrameRate, BitRate }.GetHashCode();