Setting since_tizen 3/4 on Tizen.NET API
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia / MediaTool / VideoMediaFormat.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 using System;
17 using System.Diagnostics;
18 using Tizen.Internals.Errors;
19
20 namespace Tizen.Multimedia
21 {
22     /// <summary>
23     /// Represents a video media format. This class cannot be inherited.
24     /// </summary>
25     /// <since_tizen> 3 </since_tizen>
26     public sealed class VideoMediaFormat : MediaFormat
27     {
28         private const int DefaultFrameRate = 0;
29         private const int DefaultBitRate = 0;
30
31         /// <summary>
32         /// Initializes a new instance of the VideoMediaFormat class with the specified mime type, width, and height.
33         /// </summary>
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)
42         {
43         }
44
45         /// <summary>
46         /// Initializes a new instance of the VideoMediaFormat class with the specified mime type and size.
47         /// </summary>
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)
55         {
56         }
57
58         /// <summary>
59         /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
60         /// width, height, and frame rate.
61         /// </summary>
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.
69         /// </exception>
70         /// <since_tizen> 3 </since_tizen>
71         public VideoMediaFormat(MediaFormatVideoMimeType mimeType, int width, int height, int frameRate)
72             : this(mimeType, width, height, frameRate, DefaultBitRate)
73         {
74         }
75
76         /// <summary>
77         /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
78         /// width, height, and frame rate.
79         /// </summary>
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/>
86         ///     -or-<br/>
87         ///     <paramref name="frameRate"/> is less than zero.
88         /// </exception>
89         /// <since_tizen> 3 </since_tizen>
90         public VideoMediaFormat(MediaFormatVideoMimeType mimeType, Size size,
91             int frameRate)
92             : this(mimeType, size, frameRate, DefaultBitRate)
93         {
94         }
95
96         /// <summary>
97         /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
98         /// width, height, frame rate, and bit rate.
99         /// </summary>
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.
108         /// </exception>
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)
113         {
114         }
115
116         /// <summary>
117         /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
118         /// size, frame rate, and bit rate.
119         /// </summary>
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/>
127         ///     -or-<br/>
128         ///     <paramref name="frameRate"/> is less than zero.<br/>
129         ///     -or-<br/>
130         ///     <paramref name="bitRate"/> is less than zero.
131         /// </exception>
132         /// <since_tizen> 3 </since_tizen>
133         public VideoMediaFormat(MediaFormatVideoMimeType mimeType, Size size,
134             int frameRate, int bitRate)
135             : base(MediaFormatType.Video)
136         {
137             if (!Enum.IsDefined(typeof(MediaFormatVideoMimeType), mimeType))
138             {
139                 throw new ArgumentException($"Invalid mime type value : { (int)mimeType }");
140             }
141             if (size.Width < 0)
142             {
143                 throw new ArgumentOutOfRangeException(nameof(size), size.Width, "Size.Width value can't be less than zero.");
144             }
145             if (size.Height < 0)
146             {
147                 throw new ArgumentOutOfRangeException(nameof(size), size.Height, "Size.Height value can't be less than zero.");
148             }
149             if (frameRate < 0)
150             {
151                 throw new ArgumentOutOfRangeException(nameof(frameRate), frameRate, "Frame rate can't be less than zero.");
152             }
153             if (bitRate < 0)
154             {
155                 throw new ArgumentOutOfRangeException(nameof(bitRate), bitRate, "Bit rate value can't be less than zero.");
156             }
157
158             MimeType = mimeType;
159             Size = size;
160             FrameRate = frameRate;
161             BitRate = bitRate;
162         }
163
164         /// <summary>
165         /// Initializes a new instance of the VideoMediaForma class from a native handle.
166         /// </summary>
167         /// <param name="handle">A native handle.</param>
168         internal VideoMediaFormat(IntPtr handle)
169             : base(MediaFormatType.Video)
170         {
171             Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
172
173             int width = 0;
174             int height = 0;
175             int bitRate = 0;
176             int frameRate = 0;
177             MediaFormatVideoMimeType mimeType;
178             GetInfo(handle, out width, out height, out bitRate, out mimeType);
179
180             GetFrameRate(handle, out frameRate);
181
182             MimeType = mimeType;
183             Size = new Size(width, height);
184             FrameRate = frameRate;
185             BitRate = bitRate;
186         }
187
188         /// <summary>
189         /// Retrieves video properties of the media format from a native handle.
190         /// </summary>
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)
198         {
199             Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
200
201             int mimeTypeValue = 0;
202             int maxBps = 0;
203
204             int ret = Interop.MediaFormat.GetVideoInfo(handle,
205                 out mimeTypeValue, out width, out height, out bitRate, out maxBps);
206
207             MultimediaDebug.AssertNoError(ret);
208
209             mimeType = (MediaFormatVideoMimeType)mimeTypeValue;
210
211             Debug.Assert(Enum.IsDefined(typeof(MediaFormatVideoMimeType), mimeType),
212                 "Invalid video mime type!");
213         }
214
215         /// <summary>
216         /// Retrieves frame rate from a native handle.
217         /// </summary>
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)
221         {
222             Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
223
224             int ret = Interop.MediaFormat.GetVideoFrameRate(handle, out frameRate);
225
226             MultimediaDebug.AssertNoError(ret);
227         }
228
229         internal override void AsNativeHandle(IntPtr handle)
230         {
231             Debug.Assert(Type == MediaFormatType.Video);
232
233             int ret = Interop.MediaFormat.SetVideoMimeType(handle, (int)MimeType);
234             MultimediaDebug.AssertNoError(ret);
235
236             ret = Interop.MediaFormat.SetVideoWidth(handle, Size.Width);
237             MultimediaDebug.AssertNoError(ret);
238
239             ret = Interop.MediaFormat.SetVideoHeight(handle, Size.Height);
240             MultimediaDebug.AssertNoError(ret);
241
242             ret = Interop.MediaFormat.SetVideoAverageBps(handle, BitRate);
243             MultimediaDebug.AssertNoError(ret);
244
245             ret = Interop.MediaFormat.SetVideoFrameRate(handle, FrameRate);
246             MultimediaDebug.AssertNoError(ret);
247         }
248
249         /// <summary>
250         /// Gets the mime type of the current format.
251         /// </summary>
252         /// <since_tizen> 3 </since_tizen>
253         public MediaFormatVideoMimeType MimeType { get; }
254
255         /// <summary>
256         /// Gets the size of the current format.
257         /// </summary>
258         /// <since_tizen> 3 </since_tizen>
259         public Size Size { get; }
260
261         /// <summary>
262         /// Gets the frame rate value of the current format.
263         /// </summary>
264         /// <since_tizen> 3 </since_tizen>
265         public int FrameRate { get; }
266
267         /// <summary>
268         /// Gets the bit rate value of the current format.
269         /// </summary>
270         /// <since_tizen> 3 </since_tizen>
271         public int BitRate { get; }
272
273         /// <summary>
274         /// Returns a string that represents the current object.
275         /// </summary>
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() }";
281
282         /// <summary>
283         /// Compares an object to an instance of <see cref="VideoMediaFormat"/> for equality.
284         /// </summary>
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)
289         {
290             var rhs = obj as VideoMediaFormat;
291             if (rhs == null)
292             {
293                 return false;
294             }
295
296             return MimeType == rhs.MimeType && Size == rhs.Size &&
297                 FrameRate == rhs.FrameRate && BitRate == rhs.BitRate;
298         }
299
300         /// <summary>
301         /// Gets the hash code for this instance of <see cref="VideoMediaFormat"/>.
302         /// </summary>
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();
307     }
308 }