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"><paramref name="mimeType"/> is invalid (i.e. undefined value).</exception>
37 /// <exception cref="ArgumentOutOfRangeException"><paramref name="width"/> or <paramref name="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"><paramref name="mimeType"/> is invalid (i.e. undefined value).</exception>
49 /// <exception cref="ArgumentOutOfRangeException">The width or the height of <paramref name="size"/> 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"><paramref name="mimeType"/> is invalid (i.e. undefined value).</exception>
64 /// <exception cref="ArgumentOutOfRangeException">
65 /// <paramref name="width"/>, <paramref name="height"/>, or <paramref name="frameRate"/> is less than zero.
67 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, int width, int height, int frameRate)
68 : this(mimeType, width, height, frameRate, DefaultBitRate)
73 /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
74 /// width, height, and frame rate.
76 /// <param name="mimeType">The mime type of the format.</param>
77 /// <param name="size">The video size of the format.</param>
78 /// <param name="frameRate">The frame rate of the format.</param>
79 /// <exception cref="ArgumentException"><paramref name="mimeType"/> is invalid (i.e. undefined value).</exception>
80 /// <exception cref="ArgumentOutOfRangeException">
81 /// The width or the height of <paramref name="size"/> is less than zero.<br/>
83 /// <paramref name="frameRate"/> is less than zero.
85 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, Size size,
87 : this(mimeType, size, frameRate, DefaultBitRate)
92 /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
93 /// width, height, frame rate, and bit rate.
95 /// <param name="mimeType">The mime type of the format.</param>
96 /// <param name="width">The width value of the format.</param>
97 /// <param name="height">The height value of the format</param>
98 /// <param name="frameRate">The frame rate of the format.</param>
99 /// <param name="bitRate">The bit rate of the format.</param>
100 /// <exception cref="ArgumentException"><paramref name="mimeType"/> is invalid (i.e. undefined value).</exception>
101 /// <exception cref="ArgumentOutOfRangeException">
102 /// <paramref name="width"/>, <paramref name="height"/>, <paramref name="frameRate"/>, or <paramref name="bitRate"/> is less than zero.
104 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, int width, int height,
105 int frameRate, int bitRate)
106 : this(mimeType, new Size(width, height), frameRate, bitRate)
111 /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
112 /// size, frame rate, and bit rate.
114 /// <param name="mimeType">The mime type of the format.</param>
115 /// <param name="size">The size of the format.</param>
116 /// <param name="frameRate">The frame rate of the format.</param>
117 /// <param name="bitRate">The bit rate of the format.</param>
118 /// <exception cref="ArgumentException"><paramref name="mimeType"/> is invalid (i.e. undefined value).</exception>
119 /// <exception cref="ArgumentOutOfRangeException">
120 /// The width or the height of <paramref name="size"/> is less than zero.<br/>
122 /// <paramref name="frameRate"/> is less than zero.<br/>
124 /// <paramref name="bitRate"/> is less than zero.
126 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, Size size,
127 int frameRate, int bitRate)
128 : base(MediaFormatType.Video)
130 if (!Enum.IsDefined(typeof(MediaFormatVideoMimeType), mimeType))
132 throw new ArgumentException($"Invalid mime type value : { (int)mimeType }");
136 throw new ArgumentOutOfRangeException(nameof(size), size.Width, "Size.Width value can't be less than zero.");
140 throw new ArgumentOutOfRangeException(nameof(size), size.Height, "Size.Height value can't be less than zero.");
144 throw new ArgumentOutOfRangeException(nameof(frameRate), frameRate, "Frame rate can't be less than zero.");
148 throw new ArgumentOutOfRangeException(nameof(bitRate), bitRate, "Bit rate value can't be less than zero.");
153 FrameRate = frameRate;
158 /// Initializes a new instance of the VideoMediaForma class from a native handle.
160 /// <param name="handle">A native handle.</param>
161 internal VideoMediaFormat(IntPtr handle)
162 : base(MediaFormatType.Video)
164 Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
170 MediaFormatVideoMimeType mimeType;
171 GetInfo(handle, out width, out height, out bitRate, out mimeType);
173 GetFrameRate(handle, out frameRate);
176 Size = new Size(width, height);
177 FrameRate = frameRate;
182 /// Retrieves video properties of the media format from a native handle.
184 /// <param name="handle">A native handle that the properties are retrieved from.</param>
185 /// <param name="width">An out parameter for the width.</param>
186 /// <param name="height">An out parameter for the height.</param>
187 /// <param name="bitRate">An out parameter for the bit rate.</param>
188 /// <param name="mimeType">An out parameter for the mime type.</param>
189 private static void GetInfo(IntPtr handle, out int width, out int height, out int bitRate,
190 out MediaFormatVideoMimeType mimeType)
192 Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
194 int mimeTypeValue = 0;
197 int ret = Interop.MediaFormat.GetVideoInfo(handle,
198 out mimeTypeValue, out width, out height, out bitRate, out maxBps);
200 MultimediaDebug.AssertNoError(ret);
202 mimeType = (MediaFormatVideoMimeType)mimeTypeValue;
204 Debug.Assert(Enum.IsDefined(typeof(MediaFormatVideoMimeType), mimeType),
205 "Invalid video mime type!");
209 /// Retrieves frame rate from a native handle.
211 /// <param name="handle">A native handle that the properties are retrieved from.</param>
212 /// <param name="frameRate">An out parameter for the frame rate.</param>
213 private static void GetFrameRate(IntPtr handle, out int frameRate)
215 Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
217 int ret = Interop.MediaFormat.GetVideoFrameRate(handle, out frameRate);
219 MultimediaDebug.AssertNoError(ret);
222 internal override void AsNativeHandle(IntPtr handle)
224 Debug.Assert(Type == MediaFormatType.Video);
226 int ret = Interop.MediaFormat.SetVideoMimeType(handle, (int)MimeType);
227 MultimediaDebug.AssertNoError(ret);
229 ret = Interop.MediaFormat.SetVideoWidth(handle, Size.Width);
230 MultimediaDebug.AssertNoError(ret);
232 ret = Interop.MediaFormat.SetVideoHeight(handle, Size.Height);
233 MultimediaDebug.AssertNoError(ret);
235 ret = Interop.MediaFormat.SetVideoAverageBps(handle, BitRate);
236 MultimediaDebug.AssertNoError(ret);
238 ret = Interop.MediaFormat.SetVideoFrameRate(handle, FrameRate);
239 MultimediaDebug.AssertNoError(ret);
243 /// Gets the mime type of the current format.
245 public MediaFormatVideoMimeType MimeType { get; }
248 /// Gets the size of the current format.
250 public Size Size { get; }
253 /// Gets the frame rate value of the current format.
255 public int FrameRate { get; }
258 /// Gets the bit rate value of the current format.
260 public int BitRate { get; }
263 /// Returns a string that represents the current object.
265 /// <returns>A string that represents the current object.</returns>
266 public override string ToString()
267 => $@"MimeType={ MimeType.ToString() }, Size=({ Size.ToString() }), FrameRate=
268 { FrameRate.ToString() }, BitRate={ BitRate.ToString() }";
271 /// Compares an object to an instance of <see cref="VideoMediaFormat"/> for equality.
273 /// <param name="obj">A <see cref="Object"/> to compare.</param>
274 /// <returns>true if the formats are equal; otherwise, false.</returns>
275 public override bool Equals(object obj)
277 var rhs = obj as VideoMediaFormat;
283 return MimeType == rhs.MimeType && Size == rhs.Size &&
284 FrameRate == rhs.FrameRate && BitRate == rhs.BitRate;
288 /// Gets the hash code for this instance of <see cref="VideoMediaFormat"/>.
290 /// <returns>The hash code for this instance of <see cref="VideoMediaFormat"/>.</returns>
291 public override int GetHashCode()
292 => new { MimeType, Size, FrameRate, BitRate }.GetHashCode();