4890dd15e317e46a72a87edc4bb4f92449653fb1
[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">mimeType is invalid(i.e. undefined value).</exception>
37         /// <exception cref="ArgumentOutOfRangeException">width, or 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">mimeType is invalid(i.e. undefined value).</exception>
49         /// <exception cref="ArgumentOutOfRangeException">width, or height 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">mimeType is invalid(i.e. undefined value).</exception>
64         /// <exception cref="ArgumentOutOfRangeException">width, height or frameRate is less than zero.</exception>
65         public VideoMediaFormat(MediaFormatVideoMimeType mimeType, int width, int height,
66             int frameRate)
67             : this(mimeType, width, height, frameRate, DefaultBitRate)
68         {
69         }
70
71         /// <summary>
72         /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
73         /// width, height and frame rate.
74         /// </summary>
75         /// <param name="mimeType">The mime type of the format.</param>
76         /// <param name="size">The video size of the format.</param>
77         /// <param name="frameRate">The frame rate of the format.</param>
78         /// <exception cref="ArgumentException">mimeType is invalid(i.e. undefined value).</exception>
79         /// <exception cref="ArgumentOutOfRangeException">width, height or frameRate is less than zero.</exception>
80         public VideoMediaFormat(MediaFormatVideoMimeType mimeType, Size size,
81             int frameRate)
82             : this(mimeType, size, frameRate, DefaultBitRate)
83         {
84         }
85
86         /// <summary>
87         /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
88         /// width, height, frame rate and bit rate.
89         /// </summary>
90         /// <param name="mimeType">The mime type of the format.</param>
91         /// <param name="width">The width value of the format.</param>
92         /// <param name="height">The height value of the format</param>
93         /// <param name="frameRate">The frame rate of the format.</param>
94         /// <param name="bitRate">The bit rate of the format.</param>
95         /// <exception cref="ArgumentException">mimeType is invalid(i.e. undefined value).</exception>
96         /// <exception cref="ArgumentOutOfRangeException">width, height, frameRate or bitRate is less than zero.</exception>
97         public VideoMediaFormat(MediaFormatVideoMimeType mimeType, int width, int height,
98             int frameRate, int bitRate)
99             : this(mimeType, new Size(width, height), frameRate, bitRate)
100         {
101         }
102
103         /// <summary>
104         /// Initializes a new instance of the VideoMediaFormat class with the specified mime type,
105         /// size, frame rate and bit rate.
106         /// </summary>
107         /// <param name="mimeType">The mime type of the format.</param>
108         /// <param name="size">The size of the format.</param>
109         /// <param name="frameRate">The frame rate of the format.</param>
110         /// <param name="bitRate">The bit rate of the format.</param>
111         /// <exception cref="ArgumentException">mimeType is invalid(i.e. undefined value).</exception>
112         /// <exception cref="ArgumentOutOfRangeException">width, height, frameRate or bitRate is less than zero.</exception>
113         public VideoMediaFormat(MediaFormatVideoMimeType mimeType, Size size,
114             int frameRate, int bitRate)
115             : base(MediaFormatType.Video)
116         {
117             if (!Enum.IsDefined(typeof(MediaFormatVideoMimeType), mimeType))
118             {
119                 throw new ArgumentException($"Invalid mime type value : { (int)mimeType }");
120             }
121             if (size.Width < 0)
122             {
123                 throw new ArgumentOutOfRangeException(nameof(size), size.Width, "Size.Width value can't be less than zero.");
124             }
125             if (size.Height < 0)
126             {
127                 throw new ArgumentOutOfRangeException(nameof(size), size.Height, "Size.Height value can't be less than zero.");
128             }
129             if (frameRate < 0)
130             {
131                 throw new ArgumentOutOfRangeException(nameof(frameRate), frameRate, "Frame rate can't be less than zero.");
132             }
133             if (bitRate < 0)
134             {
135                 throw new ArgumentOutOfRangeException(nameof(bitRate), bitRate, "Bit rate value can't be less than zero.");
136             }
137
138             MimeType = mimeType;
139             Size = size;
140             FrameRate = frameRate;
141             BitRate = bitRate;
142         }
143
144         /// <summary>
145         /// Initializes a new instance of the VideoMediaForma class from a native handle.
146         /// </summary>
147         /// <param name="handle">A native handle.</param>
148         internal VideoMediaFormat(IntPtr handle)
149             : base(MediaFormatType.Video)
150         {
151             Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
152
153             int width = 0;
154             int height = 0;
155             int bitRate = 0;
156             int frameRate = 0;
157             MediaFormatVideoMimeType mimeType;
158             GetInfo(handle, out width, out height, out bitRate, out mimeType);
159
160             GetFrameRate(handle, out frameRate);
161
162             MimeType = mimeType;
163             Size = new Size(width, height);
164             FrameRate = frameRate;
165             BitRate = bitRate;
166         }
167
168         /// <summary>
169         /// Retrieves video properties of media format from a native handle.
170         /// </summary>
171         /// <param name="handle">A native handle that properties are retrieved from.</param>
172         /// <param name="width">An out parameter for width.</param>
173         /// <param name="height">An out parameter for height.</param>
174         /// <param name="bitRate">An out parameter for bit rate.</param>
175         /// <param name="mimeType">An out parameter for mime type.</param>
176         private static void GetInfo(IntPtr handle, out int width, out int height, out int bitRate,
177             out MediaFormatVideoMimeType mimeType)
178         {
179             Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
180
181             int mimeTypeValue = 0;
182             int maxBps = 0;
183
184             int ret = Interop.MediaFormat.GetVideoInfo(handle,
185                 out mimeTypeValue, out width, out height, out bitRate, out maxBps);
186
187             MultimediaDebug.AssertNoError(ret);
188
189             mimeType = (MediaFormatVideoMimeType)mimeTypeValue;
190
191             Debug.Assert(Enum.IsDefined(typeof(MediaFormatVideoMimeType), mimeType),
192                 "Invalid video mime type!");
193         }
194
195         /// <summary>
196         /// Retrieves frame rate from a native handle.
197         /// </summary>
198         /// <param name="handle">A native handle that properties are retrieved from.</param>
199         /// <param name="frameRate">An out parameter for frame rate.</param>
200         private static void GetFrameRate(IntPtr handle, out int frameRate)
201         {
202             Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
203
204             int ret = Interop.MediaFormat.GetVideoFrameRate(handle, out frameRate);
205
206             MultimediaDebug.AssertNoError(ret);
207         }
208
209         internal override void AsNativeHandle(IntPtr handle)
210         {
211             Debug.Assert(Type == MediaFormatType.Video);
212
213             int ret = Interop.MediaFormat.SetVideoMimeType(handle, (int)MimeType);
214             MultimediaDebug.AssertNoError(ret);
215
216             ret = Interop.MediaFormat.SetVideoWidth(handle, Size.Width);
217             MultimediaDebug.AssertNoError(ret);
218
219             ret = Interop.MediaFormat.SetVideoHeight(handle, Size.Height);
220             MultimediaDebug.AssertNoError(ret);
221
222             ret = Interop.MediaFormat.SetVideoAverageBps(handle, BitRate);
223             MultimediaDebug.AssertNoError(ret);
224
225             ret = Interop.MediaFormat.SetVideoFrameRate(handle, FrameRate);
226             MultimediaDebug.AssertNoError(ret);
227         }
228
229         /// <summary>
230         /// Gets the mime type of the current format.
231         /// </summary>
232         public MediaFormatVideoMimeType MimeType { get; }
233
234         /// <summary>
235         /// Gets the size of the current format.
236         /// </summary>
237         public Size Size { get; }
238
239         /// <summary>
240         /// Gets the frame rate value of the current format.
241         /// </summary>
242         public int FrameRate { get; }
243
244         /// <summary>
245         /// Gets the bit rate value of the current format.
246         /// </summary>
247         public int BitRate { get; }
248
249         public override string ToString()
250         {
251             return $@"MimeType={ MimeType.ToString() }, Size=({ Size.ToString() }), FrameRate=
252                 { FrameRate.ToString() }, BitRate={ BitRate.ToString() }";
253         }
254
255         public override bool Equals(object obj)
256         {
257             var rhs = obj as VideoMediaFormat;
258             if (rhs == null)
259             {
260                 return false;
261             }
262
263             return MimeType == rhs.MimeType && Size == rhs.Size &&
264                 FrameRate == rhs.FrameRate && BitRate == rhs.BitRate;
265         }
266
267         public override int GetHashCode()
268         {
269             return new { MimeType, Size, FrameRate, BitRate }.GetHashCode();
270         }
271     }
272 }