[NUI][API10] Create Context/CookieManager when WebView is created.
[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         private const int DefaultMaxBps = 0;
31
32         /// <summary>
33         /// Initializes a new instance of the VideoMediaFormat class with the specified mime type, width, and height.
34         /// </summary>
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)
43         {
44         }
45
46         /// <summary>
47         /// Initializes a new instance of the VideoMediaFormat class with the specified mime type and size.
48         /// </summary>
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)
56         {
57         }
58
59         /// <summary>
60         /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
61         /// width, height, and frame rate.
62         /// </summary>
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.
70         /// </exception>
71         /// <since_tizen> 3 </since_tizen>
72         public VideoMediaFormat(MediaFormatVideoMimeType mimeType, int width, int height, int frameRate)
73             : this(mimeType, width, height, frameRate, DefaultBitRate)
74         {
75         }
76
77         /// <summary>
78         /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
79         /// width, height, and frame rate.
80         /// </summary>
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/>
87         ///     -or-<br/>
88         ///     <paramref name="frameRate"/> is less than zero.
89         /// </exception>
90         /// <since_tizen> 3 </since_tizen>
91         public VideoMediaFormat(MediaFormatVideoMimeType mimeType, Size size,
92             int frameRate)
93             : this(mimeType, size, frameRate, DefaultBitRate)
94         {
95         }
96
97         /// <summary>
98         /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
99         /// width, height, frame rate, and bit rate.
100         /// </summary>
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.
109         /// </exception>
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)
114         {
115         }
116
117         /// <summary>
118         /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
119         /// size, frame rate, and bit rate.
120         /// </summary>
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/>
128         ///     -or-<br/>
129         ///     <paramref name="frameRate"/> is less than zero.<br/>
130         ///     -or-<br/>
131         ///     <paramref name="bitRate"/> is less than zero.
132         /// </exception>
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)
137         {
138         }
139
140         /// <summary>
141         /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
142         /// width, height, frame rate, bit rate and max bps.
143         /// </summary>
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/>
153         ///     -or-<br/>
154         ///     <paramref name="frameRate"/>, or <paramref name="bitRate"/>, or <paramref name="maxBps"/> is less than zero.
155         /// </exception>
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)
160         {
161         }
162
163         /// <summary>
164         /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
165         /// size, frame rate, bit rate and max bps.
166         /// </summary>
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/>
175         ///     -or-<br/>
176         ///     <paramref name="frameRate"/> is less than zero.<br/>
177         ///     -or-<br/>
178         ///     <paramref name="bitRate"/> is less than zero.
179         ///     -or-<br/>
180         ///     <paramref name="maxBps"/> is less than zero.
181         /// </exception>
182         /// <since_tizen> 6 </since_tizen>
183         public VideoMediaFormat(MediaFormatVideoMimeType mimeType, Size size, int frameRate, int bitRate, int maxBps)
184             : base(MediaFormatType.Video)
185         {
186             ValidationUtil.ValidateEnum(typeof(MediaFormatVideoMimeType), mimeType, nameof(mimeType));
187
188             if (size.Width < 0)
189             {
190                 throw new ArgumentOutOfRangeException(nameof(size), size.Width, "Size.Width value can't be less than zero.");
191             }
192             if (size.Height < 0)
193             {
194                 throw new ArgumentOutOfRangeException(nameof(size), size.Height, "Size.Height value can't be less than zero.");
195             }
196             if (frameRate < 0)
197             {
198                 throw new ArgumentOutOfRangeException(nameof(frameRate), frameRate, "Frame rate can't be less than zero.");
199             }
200             if (bitRate < 0)
201             {
202                 throw new ArgumentOutOfRangeException(nameof(bitRate), bitRate, "Bit rate value can't be less than zero.");
203             }
204             if (maxBps < 0)
205             {
206                 throw new ArgumentOutOfRangeException(nameof(maxBps), maxBps, "Max bps value can't be less than zero.");
207             }
208
209             MimeType = mimeType;
210             Size = size;
211             FrameRate = frameRate;
212             BitRate = bitRate;
213             MaxBps = maxBps;
214         }
215
216         /// <summary>
217         /// Initializes a new instance of the VideoMediaForma class from a native handle.
218         /// </summary>
219         /// <param name="handle">A native handle.</param>
220         internal VideoMediaFormat(IntPtr handle)
221             : base(MediaFormatType.Video)
222         {
223             Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
224
225             int ret = Interop.MediaFormat.GetVideoInfo(handle,
226                 out var mimeType, out var width, out var height, out var bitRate, out var maxBps);
227
228             MultimediaDebug.AssertNoError(ret);
229
230             Debug.Assert(Enum.IsDefined(typeof(MediaFormatVideoMimeType), mimeType),
231                 "Invalid video mime type!");
232
233             ret = Interop.MediaFormat.GetVideoFrameRate(handle, out var frameRate);
234
235             MultimediaDebug.AssertNoError(ret);
236
237             MimeType = mimeType;
238             Size = new Size(width, height);
239             FrameRate = frameRate;
240             BitRate = bitRate;
241             MaxBps = maxBps;
242         }
243
244         internal override void AsNativeHandle(IntPtr handle)
245         {
246             Debug.Assert(Type == MediaFormatType.Video);
247
248             int ret = Interop.MediaFormat.SetVideoMimeType(handle, MimeType);
249             MultimediaDebug.AssertNoError(ret);
250
251             ret = Interop.MediaFormat.SetVideoWidth(handle, Size.Width);
252             MultimediaDebug.AssertNoError(ret);
253
254             ret = Interop.MediaFormat.SetVideoHeight(handle, Size.Height);
255             MultimediaDebug.AssertNoError(ret);
256
257             ret = Interop.MediaFormat.SetVideoAverageBps(handle, BitRate);
258             MultimediaDebug.AssertNoError(ret);
259
260             ret = Interop.MediaFormat.SetVideoFrameRate(handle, FrameRate);
261             MultimediaDebug.AssertNoError(ret);
262
263             ret = Interop.MediaFormat.SetVideoMaxBps(handle, MaxBps);
264             MultimediaDebug.AssertNoError(ret);
265         }
266
267         /// <summary>
268         /// Gets the mime type of the current format.
269         /// </summary>
270         /// <since_tizen> 3 </since_tizen>
271         public MediaFormatVideoMimeType MimeType { get; }
272
273         /// <summary>
274         /// Gets the size of the current format.
275         /// </summary>
276         /// <since_tizen> 3 </since_tizen>
277         public Size Size { get; }
278
279         /// <summary>
280         /// Gets the frame rate value of the current format.
281         /// </summary>
282         /// <since_tizen> 3 </since_tizen>
283         public int FrameRate { get; }
284
285         /// <summary>
286         /// Gets the bit rate value of the current format.
287         /// </summary>
288         /// <since_tizen> 3 </since_tizen>
289         public int BitRate { get; }
290
291         /// <summary>
292         /// Gets the max bps value of the current format.
293         /// </summary>
294         /// <since_tizen> 6 </since_tizen>
295         public int MaxBps { get; }
296
297         /// <summary>
298         /// Returns a string that represents the current object.
299         /// </summary>
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() }";
305
306         /// <summary>
307         /// Compares an object to an instance of <see cref="VideoMediaFormat"/> for equality.
308         /// </summary>
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)
313         {
314             var rhs = obj as VideoMediaFormat;
315             if (rhs == null)
316             {
317                 return false;
318             }
319
320             return MimeType == rhs.MimeType && Size == rhs.Size &&
321                 FrameRate == rhs.FrameRate && BitRate == rhs.BitRate && MaxBps == rhs.MaxBps;
322         }
323
324         /// <summary>
325         /// Gets the hash code for this instance of <see cref="VideoMediaFormat"/>.
326         /// </summary>
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();
331     }
332 }