Release 4.0.0-preview1-00201
[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     public sealed class VideoMediaFormat : MediaFormat
26     {
27         private const int DefaultFrameRate = 0;
28         private const int DefaultBitRate = 0;
29
30         /// <summary>
31         /// Initializes a new instance of the VideoMediaFormat class with the specified mime type, width, and height.
32         /// </summary>
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)
40         {
41         }
42
43         /// <summary>
44         /// Initializes a new instance of the VideoMediaFormat class with the specified mime type and size.
45         /// </summary>
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)
52         {
53         }
54
55         /// <summary>
56         /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
57         /// width, height, and frame rate.
58         /// </summary>
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.
66         /// </exception>
67         public VideoMediaFormat(MediaFormatVideoMimeType mimeType, int width, int height, int frameRate)
68             : this(mimeType, width, height, frameRate, DefaultBitRate)
69         {
70         }
71
72         /// <summary>
73         /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
74         /// width, height, and frame rate.
75         /// </summary>
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.\n
82         ///     -or-\n
83         ///     <paramref name="frameRate"/> is less than zero.
84         /// </exception>
85         public VideoMediaFormat(MediaFormatVideoMimeType mimeType, Size size,
86             int frameRate)
87             : this(mimeType, size, frameRate, DefaultBitRate)
88         {
89         }
90
91         /// <summary>
92         /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
93         /// width, height, frame rate, and bit rate.
94         /// </summary>
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.
103         /// </exception>
104         public VideoMediaFormat(MediaFormatVideoMimeType mimeType, int width, int height,
105             int frameRate, int bitRate)
106             : this(mimeType, new Size(width, height), frameRate, bitRate)
107         {
108         }
109
110         /// <summary>
111         /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
112         /// size, frame rate, and bit rate.
113         /// </summary>
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.\n
121         ///     -or-\n
122         ///     <paramref name="frameRate"/> is less than zero.\n
123         ///     -or-\n
124         ///     <paramref name="bitRate"/> is less than zero.
125         /// </exception>
126         public VideoMediaFormat(MediaFormatVideoMimeType mimeType, Size size,
127             int frameRate, int bitRate)
128             : base(MediaFormatType.Video)
129         {
130             if (!Enum.IsDefined(typeof(MediaFormatVideoMimeType), mimeType))
131             {
132                 throw new ArgumentException($"Invalid mime type value : { (int)mimeType }");
133             }
134             if (size.Width < 0)
135             {
136                 throw new ArgumentOutOfRangeException(nameof(size), size.Width, "Size.Width value can't be less than zero.");
137             }
138             if (size.Height < 0)
139             {
140                 throw new ArgumentOutOfRangeException(nameof(size), size.Height, "Size.Height value can't be less than zero.");
141             }
142             if (frameRate < 0)
143             {
144                 throw new ArgumentOutOfRangeException(nameof(frameRate), frameRate, "Frame rate can't be less than zero.");
145             }
146             if (bitRate < 0)
147             {
148                 throw new ArgumentOutOfRangeException(nameof(bitRate), bitRate, "Bit rate value can't be less than zero.");
149             }
150
151             MimeType = mimeType;
152             Size = size;
153             FrameRate = frameRate;
154             BitRate = bitRate;
155         }
156
157         /// <summary>
158         /// Initializes a new instance of the VideoMediaForma class from a native handle.
159         /// </summary>
160         /// <param name="handle">A native handle.</param>
161         internal VideoMediaFormat(IntPtr handle)
162             : base(MediaFormatType.Video)
163         {
164             Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
165
166             int width = 0;
167             int height = 0;
168             int bitRate = 0;
169             int frameRate = 0;
170             MediaFormatVideoMimeType mimeType;
171             GetInfo(handle, out width, out height, out bitRate, out mimeType);
172
173             GetFrameRate(handle, out frameRate);
174
175             MimeType = mimeType;
176             Size = new Size(width, height);
177             FrameRate = frameRate;
178             BitRate = bitRate;
179         }
180
181         /// <summary>
182         /// Retrieves video properties of the media format from a native handle.
183         /// </summary>
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)
191         {
192             Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
193
194             int mimeTypeValue = 0;
195             int maxBps = 0;
196
197             int ret = Interop.MediaFormat.GetVideoInfo(handle,
198                 out mimeTypeValue, out width, out height, out bitRate, out maxBps);
199
200             MultimediaDebug.AssertNoError(ret);
201
202             mimeType = (MediaFormatVideoMimeType)mimeTypeValue;
203
204             Debug.Assert(Enum.IsDefined(typeof(MediaFormatVideoMimeType), mimeType),
205                 "Invalid video mime type!");
206         }
207
208         /// <summary>
209         /// Retrieves frame rate from a native handle.
210         /// </summary>
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)
214         {
215             Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
216
217             int ret = Interop.MediaFormat.GetVideoFrameRate(handle, out frameRate);
218
219             MultimediaDebug.AssertNoError(ret);
220         }
221
222         internal override void AsNativeHandle(IntPtr handle)
223         {
224             Debug.Assert(Type == MediaFormatType.Video);
225
226             int ret = Interop.MediaFormat.SetVideoMimeType(handle, (int)MimeType);
227             MultimediaDebug.AssertNoError(ret);
228
229             ret = Interop.MediaFormat.SetVideoWidth(handle, Size.Width);
230             MultimediaDebug.AssertNoError(ret);
231
232             ret = Interop.MediaFormat.SetVideoHeight(handle, Size.Height);
233             MultimediaDebug.AssertNoError(ret);
234
235             ret = Interop.MediaFormat.SetVideoAverageBps(handle, BitRate);
236             MultimediaDebug.AssertNoError(ret);
237
238             ret = Interop.MediaFormat.SetVideoFrameRate(handle, FrameRate);
239             MultimediaDebug.AssertNoError(ret);
240         }
241
242         /// <summary>
243         /// Gets the mime type of the current format.
244         /// </summary>
245         public MediaFormatVideoMimeType MimeType { get; }
246
247         /// <summary>
248         /// Gets the size of the current format.
249         /// </summary>
250         public Size Size { get; }
251
252         /// <summary>
253         /// Gets the frame rate value of the current format.
254         /// </summary>
255         public int FrameRate { get; }
256
257         /// <summary>
258         /// Gets the bit rate value of the current format.
259         /// </summary>
260         public int BitRate { get; }
261
262         public override string ToString()
263         {
264             return $@"MimeType={ MimeType.ToString() }, Size=({ Size.ToString() }), FrameRate=
265                 { FrameRate.ToString() }, BitRate={ BitRate.ToString() }";
266         }
267
268         public override bool Equals(object obj)
269         {
270             var rhs = obj as VideoMediaFormat;
271             if (rhs == null)
272             {
273                 return false;
274             }
275
276             return MimeType == rhs.MimeType && Size == rhs.Size &&
277                 FrameRate == rhs.FrameRate && BitRate == rhs.BitRate;
278         }
279
280         public override int GetHashCode()
281         {
282             return new { MimeType, Size, FrameRate, BitRate }.GetHashCode();
283         }
284     }
285 }