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