Release 4.0.0-preview1-00201
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia / MediaTool / AudioMediaFormat.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 an audio media format. This class cannot be inherited.
24     /// </summary>
25     public sealed class AudioMediaFormat : MediaFormat
26     {
27
28         /// <summary>
29         /// Initializes a new instance of the AudioMediaFormat class with the specified mime type,
30         /// channel, sample rate, bit, and bit rate.
31         /// </summary>
32         /// <param name="mimeType">The mime type of the format.</param>
33         /// <param name="channel">The channel value of the format.</param>
34         /// <param name="sampleRate">The sample rate value of the format.</param>
35         /// <param name="bit">The bit value of the format.</param>
36         /// <param name="bitRate">The bit rate value of the format.</param>
37         /// <exception cref="ArgumentException"><paramref name="mimeType"/> is invalid(i.e. undefined value).</exception>
38         /// <exception cref="ArgumentOutOfRangeException">
39         ///     <paramref name="channel"/>, <paramref name="sampleRate"/>, <paramref name="bit"/>, or <paramref name="bitRate"/> is less than zero.
40         /// </exception>
41         public AudioMediaFormat(MediaFormatAudioMimeType mimeType,
42             int channel, int sampleRate, int bit, int bitRate)
43         : this(mimeType, channel, sampleRate, bit, bitRate, MediaFormatAacType.None)
44         {
45         }
46
47         /// <summary>
48         /// Initializes a new instance of the AudioMediaFormat class with the specified mime type,
49         ///     channel, sample rate, bit, bit rate, and AAC type.
50         /// </summary>
51         /// <param name="mimeType">The mime type of the format.</param>
52         /// <param name="channel">The channel value of the format.</param>
53         /// <param name="sampleRate">The sample rate value of the format.</param>
54         /// <param name="bit">The bit value of the format.</param>
55         /// <param name="bitRate">The bit rate value of the format.</param>
56         /// <param name="aacType">The AAC bitstream format(ADIF or ADTS).</param>
57         /// <exception cref="ArgumentException">
58         ///     <paramref name="mimeType"/> or <paramref name="aacType"/> is invalid (i.e. undefined value).\n
59         ///     -or-\n
60         ///     <paramref name="aacType"/> is not <see cref="MediaFormatAacType.None"/>, but <paramref name="mimeType"/> is one of the AAC types.
61         ///     </exception>
62         /// <exception cref="ArgumentOutOfRangeException">
63         ///     <paramref name="channel"/>, <paramref name="sampleRate"/>, <paramref name="bit"/>, or <paramref name="bitRate"/> is less than zero.
64         /// </exception>
65         public AudioMediaFormat(MediaFormatAudioMimeType mimeType,
66             int channel, int sampleRate, int bit, int bitRate, MediaFormatAacType aacType)
67             : base(MediaFormatType.Audio)
68         {
69             if (!Enum.IsDefined(typeof(MediaFormatAudioMimeType), mimeType))
70             {
71                 throw new ArgumentException($"Invalid mime type value : { (int)mimeType }");
72             }
73             if (channel < 0)
74             {
75                 throw new ArgumentOutOfRangeException("Channel value can't be negative.");
76             }
77             if (sampleRate < 0)
78             {
79                 throw new ArgumentOutOfRangeException("Sample rate value can't be negative.");
80             }
81             if (bit < 0)
82             {
83                 throw new ArgumentOutOfRangeException("Bit value can't be negative.");
84             }
85             if (bitRate < 0)
86             {
87                 throw new ArgumentOutOfRangeException("Bit rate value can't be negative.");
88             }
89             if (!Enum.IsDefined(typeof(MediaFormatAacType), aacType))
90             {
91                 throw new ArgumentException($"Invalid aac type value : { (int)aacType }");
92             }
93             if (!IsAacSupportedMimeType(mimeType) && aacType != MediaFormatAacType.None)
94             {
95                 throw new ArgumentException("Aac is supported only with aac mime types.");
96             }
97
98             MimeType = mimeType;
99             Channel = channel;
100             SampleRate = sampleRate;
101             Bit = bit;
102             BitRate = bitRate;
103             AacType = aacType;
104         }
105
106         /// <summary>
107         /// Initializes a new instance of the AudioMediaFormat class from a native handle.
108         /// </summary>
109         /// <param name="handle">A native handle.</param>
110         internal AudioMediaFormat(IntPtr handle)
111             : base(MediaFormatType.Audio)
112         {
113             Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
114
115             MediaFormatAudioMimeType mimeType;
116             int channel = 0;
117             int sampleRate = 0;
118             int bit = 0;
119             int bitRate = 0;
120             MediaFormatAacType aacType;
121             GetInfo(handle, out mimeType, out channel, out sampleRate, out bit, out bitRate);
122
123             if (IsAacSupportedMimeType(mimeType))
124             {
125                 GetAacType(handle, out aacType);
126             }
127             else
128             {
129                 aacType = MediaFormatAacType.None;
130             }
131
132             MimeType = mimeType;
133             Channel = channel;
134             SampleRate = sampleRate;
135             Bit = bit;
136             BitRate = bitRate;
137             AacType = aacType;
138         }
139
140         /// <summary>
141         /// Returns an indication whether a specified mime type is an AAC type.
142         /// </summary>
143         /// <param name="mimeType">A mime type.</param>
144         private static bool IsAacSupportedMimeType(MediaFormatAudioMimeType mimeType)
145         {
146             return mimeType == MediaFormatAudioMimeType.AacLC ||
147                 mimeType == MediaFormatAudioMimeType.AacHE ||
148                 mimeType == MediaFormatAudioMimeType.AacHEPS;
149         }
150
151         /// <summary>
152         /// Retrieves audio properties of the media format from a native handle.
153         /// </summary>
154         /// <param name="handle">A native handle that the properties are retrieved from.</param>
155         /// <param name="mimeType">An out parameter for the mime type.</param>
156         /// <param name="channel">An out parameter for the channel.</param>
157         /// <param name="sampleRate">An out parameter for the sample rate.</param>
158         /// <param name="bit">An out parameter for the bit.</param>
159         /// <param name="bitRate">An out parameter for the bit rate.</param>
160         private static void GetInfo(IntPtr handle, out MediaFormatAudioMimeType mimeType,
161             out int channel, out int sampleRate, out int bit, out int bitRate)
162         {
163             Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
164
165             int mimeTypeValue = 0;
166
167             int ret = Interop.MediaFormat.GetAudioInfo(handle,
168                 out mimeTypeValue, out channel, out sampleRate, out bit, out bitRate);
169
170             mimeType = (MediaFormatAudioMimeType)mimeTypeValue;
171
172             MultimediaDebug.AssertNoError(ret);
173
174             Debug.Assert(Enum.IsDefined(typeof(MediaFormatAudioMimeType), mimeType),
175                 "Invalid audio mime type!");
176         }
177
178         /// <summary>
179         /// Retrieves the AAC type value from a native handle.
180         /// </summary>
181         /// <param name="handle">A native handle that the properties are retrieved from.</param>
182         /// <param name="aacType">An out parameter for tha AAC type.</param>
183         private static void GetAacType(IntPtr handle, out MediaFormatAacType aacType)
184         {
185             Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
186
187             int aacTypeValue = 0;
188
189             int ret = Interop.MediaFormat.GetAudioAacType(handle, out aacTypeValue);
190
191             MultimediaDebug.AssertNoError(ret);
192
193             aacType = (MediaFormatAacType)aacTypeValue;
194
195             Debug.Assert(Enum.IsDefined(typeof(MediaFormatAacType), aacType), "Invalid aac type!");
196         }
197
198         internal override void AsNativeHandle(IntPtr handle)
199         {
200             Debug.Assert(Type == MediaFormatType.Audio);
201
202             int ret = Interop.MediaFormat.SetAudioMimeType(handle, (int)MimeType);
203             MultimediaDebug.AssertNoError(ret);
204
205             ret = Interop.MediaFormat.SetAudioChannel(handle, Channel);
206             MultimediaDebug.AssertNoError(ret);
207
208             ret = Interop.MediaFormat.SetAudioSampleRate(handle, SampleRate);
209             MultimediaDebug.AssertNoError(ret);
210
211             ret = Interop.MediaFormat.SetAudioBit(handle, Bit);
212             MultimediaDebug.AssertNoError(ret);
213
214             ret = Interop.MediaFormat.SetAudioAverageBps(handle, BitRate);
215             MultimediaDebug.AssertNoError(ret);
216
217             ret = Interop.MediaFormat.SetAudioAacType(handle, (int)AacType);
218             MultimediaDebug.AssertNoError(ret);
219         }
220
221         /// <summary>
222         /// Gets the mime type of the current format.
223         /// </summary>
224         public MediaFormatAudioMimeType MimeType { get; }
225
226         /// <summary>
227         /// Gets the channel value of the current format.
228         /// </summary>
229         public int Channel { get; }
230
231         /// <summary>
232         /// Gets the sample rate value of the current format.
233         /// </summary>
234         public int SampleRate { get; }
235
236         /// <summary>
237         /// Gets the bit value of the current format.
238         /// </summary>
239         public int Bit { get; }
240
241         /// <summary>
242         /// Gets the bit rate value of the current format.
243         /// </summary>
244         public int BitRate { get; }
245
246         /// <summary>
247         /// Gets the AAC type of the current format.
248         /// </summary>
249         public MediaFormatAacType AacType { get; }
250
251         public override string ToString()
252         {
253             return $@"MimeTyp={ MimeType.ToString() }, Channel={ Channel.ToString() }, SampleRate=
254                 { SampleRate }, Bit={ Bit.ToString() }, BitRate={ BitRate.ToString() }, AacType={ AacType.ToString() }";
255         }
256
257         public override bool Equals(object obj)
258         {
259             var rhs = obj as AudioMediaFormat;
260             if (rhs == null)
261             {
262                 return false;
263             }
264
265             return MimeType == rhs.MimeType && Channel == rhs.Channel && SampleRate == rhs.SampleRate &&
266                 Bit == rhs.Bit && BitRate == rhs.BitRate;
267         }
268
269         public override int GetHashCode()
270         {
271             return new { MimeType, Channel, SampleRate, Bit, BitRate }.GetHashCode();
272         }
273     }
274 }