Release 4.0.0-preview1-00224
[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                 if (value == RecorderAudioCodec.None)
87                 {
88                     throw new ArgumentException("Audio codec can't be None.");
89                 }
90
91                 _codec = value;
92             }
93         }
94
95         private int _sampleRate;
96
97         /// <summary>
98         /// Gets or sets the sampling rate of the audio stream in hertz.
99         /// </summary>
100         /// <remarks>If the value is zero, the sample rate will be decided based on input buffers.</remarks>
101         /// <value>The sample rate value for stream recorder. The default is zero.</value>
102         /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is less than zero.</exception>
103         public int SampleRate
104         {
105             get => _sampleRate;
106             set
107             {
108                 if (value < 0)
109                 {
110                     throw new ArgumentOutOfRangeException(nameof(value), value,
111                         "Sample rate can't be less than or equal to zero.");
112                 }
113
114                 _sampleRate = value;
115             }
116         }
117
118         private int _bitRate;
119
120         /// <summary>
121         /// Gets or sets the bit rate of the audio encoder in bits per second.
122         /// </summary>
123         /// <value>The bit rate value for audio stream recording. The default is 128000.</value>
124         /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is less than or equal to zero.</exception>
125         public int BitRate
126         {
127             get => _bitRate;
128             set
129             {
130                 if (value <= 0)
131                 {
132                     throw new ArgumentOutOfRangeException(nameof(value), value,
133                         "Bit rate can't be less than or equal to zero.");
134                 }
135
136                 _bitRate = value;
137             }
138         }
139
140         private int _channels;
141
142         /// <summary>
143         /// Gets or sets the number of audio channels.
144         /// </summary>
145         /// <value>The number of audio channels for audio stream recording. The default is 2.</value>
146         /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is less than or equal to zero.</exception>
147         public int Channels
148         {
149             get => _channels;
150             set
151             {
152                 if (value <= 0)
153                 {
154                     throw new ArgumentOutOfRangeException(nameof(value), value,
155                         "Channels can't be less than or equal to zero.");
156                 }
157
158                 _channels = value;
159             }
160         }
161
162         internal void Apply(StreamRecorder recorder)
163         {
164             recorder.ValidateAudioCodec(Codec);
165
166             Native.SetAudioEncoder(recorder.Handle, Codec.ToStreamRecorderEnum()).
167                 ThrowIfError("Failed to set audio codec.");
168
169             Native.SetAudioSampleRate(recorder.Handle, SampleRate).
170                 ThrowIfError("Failed to set audio sample rate.");
171
172             Native.SetAudioEncoderBitrate(recorder.Handle, BitRate).
173                 ThrowIfError("Failed to set audio bit rate.");
174
175             Native.SetAudioChannel(recorder.Handle, Channels).
176                 ThrowIfError("Failed to set audio channels.");
177         }
178     }
179
180 }