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 /// <since_tizen> 3 </since_tizen>
26 public sealed class VideoMediaFormat : MediaFormat
28 private const int DefaultFrameRate = 0;
29 private const int DefaultBitRate = 0;
32 /// Initializes a new instance of the VideoMediaFormat class with the specified mime type, width, and height.
34 /// <param name="mimeType">The mime type of the format.</param>
35 /// <param name="width">The width value of the format.</param>
36 /// <param name="height">The height value of the format</param>
37 /// <exception cref="ArgumentException"><paramref name="mimeType"/> is invalid (i.e. undefined value).</exception>
38 /// <exception cref="ArgumentOutOfRangeException"><paramref name="width"/> or <paramref name="height"/> is less than zero.</exception>
39 /// <since_tizen> 3 </since_tizen>
40 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, int width, int height)
41 : this(mimeType, width, height, DefaultFrameRate)
46 /// Initializes a new instance of the VideoMediaFormat class with the specified mime type and size.
48 /// <param name="mimeType">The mime type of the format.</param>
49 /// <param name="size">The size of the format.</param>
50 /// <exception cref="ArgumentException"><paramref name="mimeType"/> is invalid (i.e. undefined value).</exception>
51 /// <exception cref="ArgumentOutOfRangeException">The width or the height of <paramref name="size"/> is less than zero.</exception>
52 /// <since_tizen> 3 </since_tizen>
53 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, Size size)
54 : this(mimeType, size, DefaultFrameRate)
59 /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
60 /// width, height, and frame rate.
62 /// <param name="mimeType">The mime type of the format.</param>
63 /// <param name="width">The width value of the format.</param>
64 /// <param name="height">The height value of the format</param>
65 /// <param name="frameRate">The frame rate of the format.</param>
66 /// <exception cref="ArgumentException"><paramref name="mimeType"/> is invalid (i.e. undefined value).</exception>
67 /// <exception cref="ArgumentOutOfRangeException">
68 /// <paramref name="width"/>, <paramref name="height"/>, or <paramref name="frameRate"/> is less than zero.
70 /// <since_tizen> 3 </since_tizen>
71 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, int width, int height, int frameRate)
72 : this(mimeType, width, height, frameRate, DefaultBitRate)
77 /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
78 /// width, height, and frame rate.
80 /// <param name="mimeType">The mime type of the format.</param>
81 /// <param name="size">The video size of the format.</param>
82 /// <param name="frameRate">The frame rate of the format.</param>
83 /// <exception cref="ArgumentException"><paramref name="mimeType"/> is invalid (i.e. undefined value).</exception>
84 /// <exception cref="ArgumentOutOfRangeException">
85 /// The width or the height of <paramref name="size"/> is less than zero.<br/>
87 /// <paramref name="frameRate"/> is less than zero.
89 /// <since_tizen> 3 </since_tizen>
90 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, Size size,
92 : this(mimeType, size, frameRate, DefaultBitRate)
97 /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
98 /// width, height, frame rate, and bit rate.
100 /// <param name="mimeType">The mime type of the format.</param>
101 /// <param name="width">The width value of the format.</param>
102 /// <param name="height">The height value of the format</param>
103 /// <param name="frameRate">The frame rate of the format.</param>
104 /// <param name="bitRate">The bit rate of the format.</param>
105 /// <exception cref="ArgumentException"><paramref name="mimeType"/> is invalid (i.e. undefined value).</exception>
106 /// <exception cref="ArgumentOutOfRangeException">
107 /// <paramref name="width"/>, <paramref name="height"/>, <paramref name="frameRate"/>, or <paramref name="bitRate"/> is less than zero.
109 /// <since_tizen> 3 </since_tizen>
110 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, int width, int height,
111 int frameRate, int bitRate)
112 : this(mimeType, new Size(width, height), frameRate, bitRate)
117 /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
118 /// size, frame rate, and bit rate.
120 /// <param name="mimeType">The mime type of the format.</param>
121 /// <param name="size">The size of the format.</param>
122 /// <param name="frameRate">The frame rate of the format.</param>
123 /// <param name="bitRate">The bit rate of the format.</param>
124 /// <exception cref="ArgumentException"><paramref name="mimeType"/> is invalid (i.e. undefined value).</exception>
125 /// <exception cref="ArgumentOutOfRangeException">
126 /// The width or the height of <paramref name="size"/> is less than zero.<br/>
128 /// <paramref name="frameRate"/> is less than zero.<br/>
130 /// <paramref name="bitRate"/> is less than zero.
132 /// <since_tizen> 3 </since_tizen>
133 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, Size size,
134 int frameRate, int bitRate)
135 : base(MediaFormatType.Video)
137 if (!Enum.IsDefined(typeof(MediaFormatVideoMimeType), mimeType))
139 throw new ArgumentException($"Invalid mime type value : { (int)mimeType }");
143 throw new ArgumentOutOfRangeException(nameof(size), size.Width, "Size.Width value can't be less than zero.");
147 throw new ArgumentOutOfRangeException(nameof(size), size.Height, "Size.Height value can't be less than zero.");
151 throw new ArgumentOutOfRangeException(nameof(frameRate), frameRate, "Frame rate can't be less than zero.");
155 throw new ArgumentOutOfRangeException(nameof(bitRate), bitRate, "Bit rate value can't be less than zero.");
160 FrameRate = frameRate;
165 /// Initializes a new instance of the VideoMediaForma class from a native handle.
167 /// <param name="handle">A native handle.</param>
168 internal VideoMediaFormat(IntPtr handle)
169 : base(MediaFormatType.Video)
171 Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
177 MediaFormatVideoMimeType mimeType;
178 GetInfo(handle, out width, out height, out bitRate, out mimeType);
180 GetFrameRate(handle, out frameRate);
183 Size = new Size(width, height);
184 FrameRate = frameRate;
189 /// Retrieves video properties of the media format from a native handle.
191 /// <param name="handle">A native handle that the properties are retrieved from.</param>
192 /// <param name="width">An out parameter for the width.</param>
193 /// <param name="height">An out parameter for the height.</param>
194 /// <param name="bitRate">An out parameter for the bit rate.</param>
195 /// <param name="mimeType">An out parameter for the mime type.</param>
196 private static void GetInfo(IntPtr handle, out int width, out int height, out int bitRate,
197 out MediaFormatVideoMimeType mimeType)
199 Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
201 int mimeTypeValue = 0;
204 int ret = Interop.MediaFormat.GetVideoInfo(handle,
205 out mimeTypeValue, out width, out height, out bitRate, out maxBps);
207 MultimediaDebug.AssertNoError(ret);
209 mimeType = (MediaFormatVideoMimeType)mimeTypeValue;
211 Debug.Assert(Enum.IsDefined(typeof(MediaFormatVideoMimeType), mimeType),
212 "Invalid video mime type!");
216 /// Retrieves frame rate from a native handle.
218 /// <param name="handle">A native handle that the properties are retrieved from.</param>
219 /// <param name="frameRate">An out parameter for the frame rate.</param>
220 private static void GetFrameRate(IntPtr handle, out int frameRate)
222 Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
224 int ret = Interop.MediaFormat.GetVideoFrameRate(handle, out frameRate);
226 MultimediaDebug.AssertNoError(ret);
229 internal override void AsNativeHandle(IntPtr handle)
231 Debug.Assert(Type == MediaFormatType.Video);
233 int ret = Interop.MediaFormat.SetVideoMimeType(handle, (int)MimeType);
234 MultimediaDebug.AssertNoError(ret);
236 ret = Interop.MediaFormat.SetVideoWidth(handle, Size.Width);
237 MultimediaDebug.AssertNoError(ret);
239 ret = Interop.MediaFormat.SetVideoHeight(handle, Size.Height);
240 MultimediaDebug.AssertNoError(ret);
242 ret = Interop.MediaFormat.SetVideoAverageBps(handle, BitRate);
243 MultimediaDebug.AssertNoError(ret);
245 ret = Interop.MediaFormat.SetVideoFrameRate(handle, FrameRate);
246 MultimediaDebug.AssertNoError(ret);
250 /// Gets the mime type of the current format.
252 /// <since_tizen> 3 </since_tizen>
253 public MediaFormatVideoMimeType MimeType { get; }
256 /// Gets the size of the current format.
258 /// <since_tizen> 3 </since_tizen>
259 public Size Size { get; }
262 /// Gets the frame rate value of the current format.
264 /// <since_tizen> 3 </since_tizen>
265 public int FrameRate { get; }
268 /// Gets the bit rate value of the current format.
270 /// <since_tizen> 3 </since_tizen>
271 public int BitRate { get; }
274 /// Returns a string that represents the current object.
276 /// <returns>A string that represents the current object.</returns>
277 /// <since_tizen> 3 </since_tizen>
278 public override string ToString()
279 => $@"MimeType={ MimeType.ToString() }, Size=({ Size.ToString() }), FrameRate=
280 { FrameRate.ToString() }, BitRate={ BitRate.ToString() }";
283 /// Compares an object to an instance of <see cref="VideoMediaFormat"/> for equality.
285 /// <param name="obj">A <see cref="Object"/> to compare.</param>
286 /// <returns>true if the formats are equal; otherwise, false.</returns>
287 /// <since_tizen> 3 </since_tizen>
288 public override bool Equals(object obj)
290 var rhs = obj as VideoMediaFormat;
296 return MimeType == rhs.MimeType && Size == rhs.Size &&
297 FrameRate == rhs.FrameRate && BitRate == rhs.BitRate;
301 /// Gets the hash code for this instance of <see cref="VideoMediaFormat"/>.
303 /// <returns>The hash code for this instance of <see cref="VideoMediaFormat"/>.</returns>
304 /// <since_tizen> 3 </since_tizen>
305 public override int GetHashCode()
306 => new { MimeType, Size, FrameRate, BitRate }.GetHashCode();