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;
30 private const int DefaultMaxBps = 0;
33 /// Initializes a new instance of the VideoMediaFormat class with the specified mime type, width, and height.
35 /// <param name="mimeType">The mime type of the format.</param>
36 /// <param name="width">The width value of the format.</param>
37 /// <param name="height">The height value of the format</param>
38 /// <exception cref="ArgumentException"><paramref name="mimeType"/> is invalid (i.e. undefined value).</exception>
39 /// <exception cref="ArgumentOutOfRangeException"><paramref name="width"/> or <paramref name="height"/> is less than zero.</exception>
40 /// <since_tizen> 3 </since_tizen>
41 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, int width, int height)
42 : this(mimeType, width, height, DefaultFrameRate)
47 /// Initializes a new instance of the VideoMediaFormat class with the specified mime type and size.
49 /// <param name="mimeType">The mime type of the format.</param>
50 /// <param name="size">The size of the format.</param>
51 /// <exception cref="ArgumentException"><paramref name="mimeType"/> is invalid (i.e. undefined value).</exception>
52 /// <exception cref="ArgumentOutOfRangeException">The width or the height of <paramref name="size"/> is less than zero.</exception>
53 /// <since_tizen> 3 </since_tizen>
54 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, Size size)
55 : this(mimeType, size, DefaultFrameRate)
60 /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
61 /// width, height, and frame rate.
63 /// <param name="mimeType">The mime type of the format.</param>
64 /// <param name="width">The width value of the format.</param>
65 /// <param name="height">The height value of the format</param>
66 /// <param name="frameRate">The frame rate of the format.</param>
67 /// <exception cref="ArgumentException"><paramref name="mimeType"/> is invalid (i.e. undefined value).</exception>
68 /// <exception cref="ArgumentOutOfRangeException">
69 /// <paramref name="width"/>, <paramref name="height"/>, or <paramref name="frameRate"/> is less than zero.
71 /// <since_tizen> 3 </since_tizen>
72 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, int width, int height, int frameRate)
73 : this(mimeType, width, height, frameRate, DefaultBitRate)
78 /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
79 /// width, height, and frame rate.
81 /// <param name="mimeType">The mime type of the format.</param>
82 /// <param name="size">The video size of the format.</param>
83 /// <param name="frameRate">The frame rate of the format.</param>
84 /// <exception cref="ArgumentException"><paramref name="mimeType"/> is invalid (i.e. undefined value).</exception>
85 /// <exception cref="ArgumentOutOfRangeException">
86 /// The width or the height of <paramref name="size"/> is less than zero.<br/>
88 /// <paramref name="frameRate"/> is less than zero.
90 /// <since_tizen> 3 </since_tizen>
91 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, Size size,
93 : this(mimeType, size, frameRate, DefaultBitRate)
98 /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
99 /// width, height, frame rate, and bit rate.
101 /// <param name="mimeType">The mime type of the format.</param>
102 /// <param name="width">The width value of the format.</param>
103 /// <param name="height">The height value of the format</param>
104 /// <param name="frameRate">The frame rate of the format.</param>
105 /// <param name="bitRate">The bit rate of the format.</param>
106 /// <exception cref="ArgumentException"><paramref name="mimeType"/> is invalid (i.e. undefined value).</exception>
107 /// <exception cref="ArgumentOutOfRangeException">
108 /// <paramref name="width"/>, <paramref name="height"/>, <paramref name="frameRate"/>, or <paramref name="bitRate"/> is less than zero.
110 /// <since_tizen> 3 </since_tizen>
111 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, int width, int height,
112 int frameRate, int bitRate)
113 : this(mimeType, width, height, frameRate, bitRate, DefaultMaxBps)
118 /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
119 /// size, frame rate, and bit rate.
121 /// <param name="mimeType">The mime type of the format.</param>
122 /// <param name="size">The size of the format.</param>
123 /// <param name="frameRate">The frame rate of the format.</param>
124 /// <param name="bitRate">The bit rate of the format.</param>
125 /// <exception cref="ArgumentException"><paramref name="mimeType"/> is invalid (i.e. undefined value).</exception>
126 /// <exception cref="ArgumentOutOfRangeException">
127 /// The width or the height of <paramref name="size"/> is less than zero.<br/>
129 /// <paramref name="frameRate"/> is less than zero.<br/>
131 /// <paramref name="bitRate"/> is less than zero.
133 /// <since_tizen> 3 </since_tizen>
134 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, Size size,
135 int frameRate, int bitRate)
136 : this (mimeType, size, frameRate, bitRate, DefaultMaxBps)
141 /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
142 /// width, height, frame rate, bit rate and max bps.
144 /// <param name="mimeType">The mime type of the format.</param>
145 /// <param name="width">The width value of the format.</param>
146 /// <param name="height">The height value of the format</param>
147 /// <param name="frameRate">The frame rate of the format.</param>
148 /// <param name="bitRate">The bit rate of the format.</param>
149 /// <param name="maxBps">The max bps of the format.</param>
150 /// <exception cref="ArgumentException"><paramref name="mimeType"/> is invalid (i.e. undefined value).</exception>
151 /// <exception cref="ArgumentOutOfRangeException">
152 /// <paramref name="width"/>, <paramref name="height"/>, <br/>
154 /// <paramref name="frameRate"/>, or <paramref name="bitRate"/>, or <paramref name="maxBps"/> is less than zero.
156 /// <since_tizen> 6 </since_tizen>
157 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, int width, int height,
158 int frameRate, int bitRate, int maxBps)
159 : this(mimeType, new Size(width, height), frameRate, bitRate, maxBps)
164 /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
165 /// size, frame rate, bit rate and max bps.
167 /// <param name="mimeType">The mime type of the format.</param>
168 /// <param name="size">The size of the format.</param>
169 /// <param name="frameRate">The frame rate of the format.</param>
170 /// <param name="bitRate">The bit rate of the format.</param>
171 /// <param name="maxBps">The max bps of the format.</param>
172 /// <exception cref="ArgumentException"><paramref name="mimeType"/> is invalid (i.e. undefined value).</exception>
173 /// <exception cref="ArgumentOutOfRangeException">
174 /// The width or the height of <paramref name="size"/> is less than zero.<br/>
176 /// <paramref name="frameRate"/> is less than zero.<br/>
178 /// <paramref name="bitRate"/> is less than zero.
180 /// <paramref name="maxBps"/> is less than zero.
182 /// <since_tizen> 6 </since_tizen>
183 public VideoMediaFormat(MediaFormatVideoMimeType mimeType, Size size, int frameRate, int bitRate, int maxBps)
184 : base(MediaFormatType.Video)
186 ValidationUtil.ValidateEnum(typeof(MediaFormatVideoMimeType), mimeType, nameof(mimeType));
190 throw new ArgumentOutOfRangeException(nameof(size), size.Width, "Size.Width value can't be less than zero.");
194 throw new ArgumentOutOfRangeException(nameof(size), size.Height, "Size.Height value can't be less than zero.");
198 throw new ArgumentOutOfRangeException(nameof(frameRate), frameRate, "Frame rate can't be less than zero.");
202 throw new ArgumentOutOfRangeException(nameof(bitRate), bitRate, "Bit rate value can't be less than zero.");
206 throw new ArgumentOutOfRangeException(nameof(maxBps), maxBps, "Max bps value can't be less than zero.");
211 FrameRate = frameRate;
217 /// Initializes a new instance of the VideoMediaForma class from a native handle.
219 /// <param name="handle">A native handle.</param>
220 internal VideoMediaFormat(IntPtr handle)
221 : base(MediaFormatType.Video)
223 Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
225 int ret = Interop.MediaFormat.GetVideoInfo(handle,
226 out var mimeType, out var width, out var height, out var bitRate, out var maxBps);
228 MultimediaDebug.AssertNoError(ret);
230 Debug.Assert(Enum.IsDefined(typeof(MediaFormatVideoMimeType), mimeType),
231 "Invalid video mime type!");
233 ret = Interop.MediaFormat.GetVideoFrameRate(handle, out var frameRate);
235 MultimediaDebug.AssertNoError(ret);
238 Size = new Size(width, height);
239 FrameRate = frameRate;
244 internal override void AsNativeHandle(IntPtr handle)
246 Debug.Assert(Type == MediaFormatType.Video);
248 int ret = Interop.MediaFormat.SetVideoMimeType(handle, MimeType);
249 MultimediaDebug.AssertNoError(ret);
251 ret = Interop.MediaFormat.SetVideoWidth(handle, Size.Width);
252 MultimediaDebug.AssertNoError(ret);
254 ret = Interop.MediaFormat.SetVideoHeight(handle, Size.Height);
255 MultimediaDebug.AssertNoError(ret);
257 ret = Interop.MediaFormat.SetVideoAverageBps(handle, BitRate);
258 MultimediaDebug.AssertNoError(ret);
260 ret = Interop.MediaFormat.SetVideoFrameRate(handle, FrameRate);
261 MultimediaDebug.AssertNoError(ret);
263 ret = Interop.MediaFormat.SetVideoMaxBps(handle, MaxBps);
264 MultimediaDebug.AssertNoError(ret);
268 /// Gets the mime type of the current format.
270 /// <since_tizen> 3 </since_tizen>
271 public MediaFormatVideoMimeType MimeType { get; }
274 /// Gets the size of the current format.
276 /// <since_tizen> 3 </since_tizen>
277 public Size Size { get; }
280 /// Gets the frame rate value of the current format.
282 /// <since_tizen> 3 </since_tizen>
283 public int FrameRate { get; }
286 /// Gets the bit rate value of the current format.
288 /// <since_tizen> 3 </since_tizen>
289 public int BitRate { get; }
292 /// Gets the max bps value of the current format.
294 /// <since_tizen> 6 </since_tizen>
295 public int MaxBps { get; }
298 /// Returns a string that represents the current object.
300 /// <returns>A string that represents the current object.</returns>
301 /// <since_tizen> 3 </since_tizen>
302 public override string ToString()
303 => $@"MimeType={ MimeType.ToString() }, Size=({ Size.ToString() }), FrameRate=
304 { FrameRate.ToString() }, BitRate={ BitRate.ToString() }, MaxBps = { MaxBps.ToString() }";
307 /// Compares an object to an instance of <see cref="VideoMediaFormat"/> for equality.
309 /// <param name="obj">A <see cref="Object"/> to compare.</param>
310 /// <returns>true if the formats are equal; otherwise, false.</returns>
311 /// <since_tizen> 3 </since_tizen>
312 public override bool Equals(object obj)
314 var rhs = obj as VideoMediaFormat;
320 return MimeType == rhs.MimeType && Size == rhs.Size &&
321 FrameRate == rhs.FrameRate && BitRate == rhs.BitRate && MaxBps == rhs.MaxBps;
325 /// Gets the hash code for this instance of <see cref="VideoMediaFormat"/>.
327 /// <returns>The hash code for this instance of <see cref="VideoMediaFormat"/>.</returns>
328 /// <since_tizen> 3 </since_tizen>
329 public override int GetHashCode()
330 => new { MimeType, Size, FrameRate, BitRate, MaxBps }.GetHashCode();