2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 using System.Diagnostics;
18 using Tizen.Internals.Errors;
20 namespace Tizen.Multimedia
23 /// Represents an audio media format. This class cannot be inherited.
25 public sealed class AudioMediaFormat : MediaFormat
29 /// Initializes a new instance of the AudioMediaFormat class with the specified mime type,
30 /// channel, sample rate, bit, and bit rate.
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.
41 public AudioMediaFormat(MediaFormatAudioMimeType mimeType,
42 int channel, int sampleRate, int bit, int bitRate)
43 : this(mimeType, channel, sampleRate, bit, bitRate, MediaFormatAacType.None)
48 /// Initializes a new instance of the AudioMediaFormat class with the specified mime type,
49 /// channel, sample rate, bit, bit rate, and AAC type.
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
60 /// <paramref name="aacType"/> is not <see cref="MediaFormatAacType.None"/>, but <paramref name="mimeType"/> is one of the AAC types.
62 /// <exception cref="ArgumentOutOfRangeException">
63 /// <paramref name="channel"/>, <paramref name="sampleRate"/>, <paramref name="bit"/>, or <paramref name="bitRate"/> is less than zero.
65 public AudioMediaFormat(MediaFormatAudioMimeType mimeType,
66 int channel, int sampleRate, int bit, int bitRate, MediaFormatAacType aacType)
67 : base(MediaFormatType.Audio)
69 if (!Enum.IsDefined(typeof(MediaFormatAudioMimeType), mimeType))
71 throw new ArgumentException($"Invalid mime type value : { (int)mimeType }");
75 throw new ArgumentOutOfRangeException("Channel value can't be negative.");
79 throw new ArgumentOutOfRangeException("Sample rate value can't be negative.");
83 throw new ArgumentOutOfRangeException("Bit value can't be negative.");
87 throw new ArgumentOutOfRangeException("Bit rate value can't be negative.");
89 if (!Enum.IsDefined(typeof(MediaFormatAacType), aacType))
91 throw new ArgumentException($"Invalid aac type value : { (int)aacType }");
93 if (!IsAacSupportedMimeType(mimeType) && aacType != MediaFormatAacType.None)
95 throw new ArgumentException("Aac is supported only with aac mime types.");
100 SampleRate = sampleRate;
107 /// Initializes a new instance of the AudioMediaFormat class from a native handle.
109 /// <param name="handle">A native handle.</param>
110 internal AudioMediaFormat(IntPtr handle)
111 : base(MediaFormatType.Audio)
113 Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
115 MediaFormatAudioMimeType mimeType;
120 MediaFormatAacType aacType;
121 GetInfo(handle, out mimeType, out channel, out sampleRate, out bit, out bitRate);
123 if (IsAacSupportedMimeType(mimeType))
125 GetAacType(handle, out aacType);
129 aacType = MediaFormatAacType.None;
134 SampleRate = sampleRate;
141 /// Returns an indication whether a specified mime type is an AAC type.
143 /// <param name="mimeType">A mime type.</param>
144 private static bool IsAacSupportedMimeType(MediaFormatAudioMimeType mimeType)
146 return mimeType == MediaFormatAudioMimeType.AacLC ||
147 mimeType == MediaFormatAudioMimeType.AacHE ||
148 mimeType == MediaFormatAudioMimeType.AacHEPS;
152 /// Retrieves audio properties of the media format from a native handle.
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)
163 Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
165 int mimeTypeValue = 0;
167 int ret = Interop.MediaFormat.GetAudioInfo(handle,
168 out mimeTypeValue, out channel, out sampleRate, out bit, out bitRate);
170 mimeType = (MediaFormatAudioMimeType)mimeTypeValue;
172 MultimediaDebug.AssertNoError(ret);
174 Debug.Assert(Enum.IsDefined(typeof(MediaFormatAudioMimeType), mimeType),
175 "Invalid audio mime type!");
179 /// Retrieves the AAC type value from a native handle.
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)
185 Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
187 int aacTypeValue = 0;
189 int ret = Interop.MediaFormat.GetAudioAacType(handle, out aacTypeValue);
191 MultimediaDebug.AssertNoError(ret);
193 aacType = (MediaFormatAacType)aacTypeValue;
195 Debug.Assert(Enum.IsDefined(typeof(MediaFormatAacType), aacType), "Invalid aac type!");
198 internal override void AsNativeHandle(IntPtr handle)
200 Debug.Assert(Type == MediaFormatType.Audio);
202 int ret = Interop.MediaFormat.SetAudioMimeType(handle, (int)MimeType);
203 MultimediaDebug.AssertNoError(ret);
205 ret = Interop.MediaFormat.SetAudioChannel(handle, Channel);
206 MultimediaDebug.AssertNoError(ret);
208 ret = Interop.MediaFormat.SetAudioSampleRate(handle, SampleRate);
209 MultimediaDebug.AssertNoError(ret);
211 ret = Interop.MediaFormat.SetAudioBit(handle, Bit);
212 MultimediaDebug.AssertNoError(ret);
214 ret = Interop.MediaFormat.SetAudioAverageBps(handle, BitRate);
215 MultimediaDebug.AssertNoError(ret);
217 ret = Interop.MediaFormat.SetAudioAacType(handle, (int)AacType);
218 MultimediaDebug.AssertNoError(ret);
222 /// Gets the mime type of the current format.
224 public MediaFormatAudioMimeType MimeType { get; }
227 /// Gets the channel value of the current format.
229 public int Channel { get; }
232 /// Gets the sample rate value of the current format.
234 public int SampleRate { get; }
237 /// Gets the bit value of the current format.
239 public int Bit { get; }
242 /// Gets the bit rate value of the current format.
244 public int BitRate { get; }
247 /// Gets the AAC type of the current format.
249 public MediaFormatAacType AacType { get; }
251 public override string ToString()
253 return $@"MimeTyp={ MimeType.ToString() }, Channel={ Channel.ToString() }, SampleRate=
254 { SampleRate }, Bit={ Bit.ToString() }, BitRate={ BitRate.ToString() }, AacType={ AacType.ToString() }";
257 public override bool Equals(object obj)
259 var rhs = obj as AudioMediaFormat;
265 return MimeType == rhs.MimeType && Channel == rhs.Channel && SampleRate == rhs.SampleRate &&
266 Bit == rhs.Bit && BitRate == rhs.BitRate;
269 public override int GetHashCode()
271 return new { MimeType, Channel, SampleRate, Bit, BitRate }.GetHashCode();