911cf345296a528f7b3373b0bd4f57816b3df4ae
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.StreamRecorder / StreamRecorder / StreamRecorderAudioOptions.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
17 using System;
18 using Native = Interop.StreamRecorder;
19
20 namespace Tizen.Multimedia
21 {
22     /// <summary>
23     /// Specifies the options associated with audio recording.
24     /// </summary>
25     /// <seealso cref="StreamRecorder"/>
26     /// <seealso cref="StreamRecorderOptions"/>
27     /// <seealso cref="StreamRecorderVideoOptions"/>
28     public class StreamRecorderAudioOptions
29     {
30         private const int DefaultSampleRate = 0;
31         private const int DefaultBitRate = 128000;
32         private const int DefaultChannels = 2;
33
34         /// <summary>
35         /// Initialize a new instance of the <see cref="StreamRecorderAudioOptions"/> class with the specified codec.
36         /// </summary>
37         /// <param name="codec">The <see cref="RecorderAudioCodec"/> for encoding audio stream.</param>
38         /// <remarks>
39         /// <see cref="SampleRate"/>, <see cref="BitRate"/> and <see cref="Channels"/> will be set as default.
40         /// </remarks>
41         /// <exception cref="ArgumentException"><paramref name="codec"/> is not valid.</exception>
42         public StreamRecorderAudioOptions(RecorderAudioCodec codec) :
43             this(codec, DefaultSampleRate, DefaultBitRate, DefaultChannels)
44         {
45         }
46
47         /// <summary>
48         /// Initialize a new instance of the <see cref="StreamRecorderAudioOptions"/> class with the specified
49         /// codec, sample rate, bit rate and channel value.
50         /// </summary>
51         /// <param name="codec">The <see cref="RecorderAudioCodec"/> for encoding audio stream.</param>
52         /// <param name="sampleRate">The sample rate for encoding audio stream.</param>
53         /// <param name="bitRate">The bit rate for encoding audio stream.</param>
54         /// <param name="channels">The number of channels for encoding audio stream.</param>
55         /// <exception cref="ArgumentException"><paramref name="codec"/> is not valid.</exception>
56         /// <exception cref="ArgumentOutOfRangeException">
57         ///     <paramref name="sampleRate"/> is less than zero.\n
58         ///     -or-\n
59         ///     <paramref name="bitRate"/> is less than or equal to zero.\n
60         ///     -or-\n
61         ///     <paramref name="channels"/> is less than or equal to zero.
62         /// </exception>
63         public StreamRecorderAudioOptions(RecorderAudioCodec codec, int sampleRate, int bitRate, int channels)
64         {
65             Codec = codec;
66             SampleRate = sampleRate;
67             BitRate = bitRate;
68             Channels = channels;
69         }
70
71         private RecorderAudioCodec _codec;
72
73         /// <summary>
74         /// Gets or sets the audio codec for encoding an audio stream.
75         /// </summary>
76         /// <value>The codec for audio stream recording.</value>
77         /// <exception cref="ArgumentException"><paramref name="value"/> is not valid.</exception>
78         /// <seealso cref="StreamRecorder.GetSupportedAudioCodecs"/>
79         public RecorderAudioCodec Codec
80         {
81             get => _codec;
82             set
83             {
84                 ValidationUtil.ValidateEnum(typeof(RecorderAudioCodec), value, nameof(value));
85
86                 _codec = value;
87             }
88         }
89
90         private int _sampleRate;
91
92         /// <summary>
93         /// Gets or sets the sampling rate of the audio stream in hertz.
94         /// </summary>
95         /// <remarks>If the value is zero, the sample rate will be decided based on input buffers.</remarks>
96         /// <value>The sample rate value for stream recorder. The default is zero.</value>
97         /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is less than zero.</exception>
98         public int SampleRate
99         {
100             get => _sampleRate;
101             set
102             {
103                 if (value < 0)
104                 {
105                     throw new ArgumentOutOfRangeException(nameof(value), value,
106                         "Sample rate can't be less than or equal to zero.");
107                 }
108
109                 _sampleRate = value;
110             }
111         }
112
113         private int _bitRate;
114
115         /// <summary>
116         /// Gets or sets the bit rate of the audio encoder in bits per second.
117         /// </summary>
118         /// <value>The bit rate value for audio stream recording. The default is 128000.</value>
119         /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is less than or equal to zero.</exception>
120         public int BitRate
121         {
122             get => _bitRate;
123             set
124             {
125                 if (value <= 0)
126                 {
127                     throw new ArgumentOutOfRangeException(nameof(value), value,
128                         "Bit rate can't be less than or equal to zero.");
129                 }
130
131                 _bitRate = value;
132             }
133         }
134
135         private int _channels;
136
137         /// <summary>
138         /// Gets or sets the number of audio channels.
139         /// </summary>
140         /// <value>The number of audio channels for audio stream recording. The default is 2.</value>
141         /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is less than or equal to zero.</exception>
142         public int Channels
143         {
144             get => _channels;
145             set
146             {
147                 if (value <= 0)
148                 {
149                     throw new ArgumentOutOfRangeException(nameof(value), value,
150                         "Channels can't be less than or equal to zero.");
151                 }
152
153                 _channels = value;
154             }
155         }
156
157         internal void Apply(StreamRecorder recorder)
158         {
159             recorder.ValidateAudioCodec(Codec);
160
161             Native.SetAudioEncoder(recorder.Handle, Codec.ToStreamRecorderEnum()).
162                 ThrowIfError("Failed to set audio codec.");
163
164             Native.SetAudioSampleRate(recorder.Handle, SampleRate).
165                 ThrowIfError("Failed to set audio sample rate.");
166
167             Native.SetAudioEncoderBitrate(recorder.Handle, BitRate).
168                 ThrowIfError("Failed to set audio bit rate.");
169
170             Native.SetAudioChannel(recorder.Handle, Channels).
171                 ThrowIfError("Failed to set audio channels.");
172         }
173     }
174
175 }