merge commits of 2.2.1 to public
[platform/framework/native/media.git] / src / FMediaAudioIn.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <FMediaAudioIn.h>
19 #include <FMediaAudioTypes.h>
20 #include <FBaseSysLog.h>
21 #include <FSec_AccessController.h>
22 #include "FMedia_AudioInImpl.h"
23
24 using namespace Tizen::Security;
25
26 namespace Tizen { namespace Media
27 {
28
29
30 AudioIn::AudioIn(void)
31         : __pAudioInImpl(null)
32 {
33 }
34
35 AudioIn::~AudioIn(void)
36 {
37         if (__pAudioInImpl)
38         {
39                 delete __pAudioInImpl;
40         }
41 }
42
43 result
44 AudioIn::Construct(IAudioInEventListener& listener)
45 {
46         ClearLastResult();
47         result r = E_SUCCESS;
48
49         SysAssertf(__pAudioInImpl == null,      "Failed to construct an instance of AudioIn. Calling Construct() twice or more on a same instance is not allowed for this class");
50
51         _AudioInImpl* pAudioInImpl = new (std::nothrow) _AudioInImpl;
52
53         SysTryCatch(NID_MEDIA, pAudioInImpl, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY , "[E_OUT_OF_MEMORY] Memory allocation failed.");
54
55         r = pAudioInImpl->Construct(listener);
56         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
57
58         __pAudioInImpl = pAudioInImpl;
59
60         return r;
61
62 CATCH:
63         if (pAudioInImpl)
64         {
65                 delete pAudioInImpl;
66                 pAudioInImpl = null;
67         }
68         return r;
69 }
70
71 result
72 AudioIn::Prepare(AudioInputDevice audioInputDevice, AudioSampleType audioSampleType, AudioChannelType audioChannelType,
73                                  int audioSampleRate)
74 {
75         ClearLastResult();
76         result r = E_SUCCESS;
77
78         SysAssertf(__pAudioInImpl !=  null, "Not yet constructed! Construct() should be called before use");
79
80         r = _AccessController::CheckUserPrivilege(_PRV_AUDIORECORDER);
81         SysTryReturn(NID_MEDIA,  r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED]. This application does not have the privilege to call this method.");
82
83         r = __pAudioInImpl->Prepare(audioSampleType, audioChannelType, audioSampleRate);
84         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
85         return r;
86 }
87
88 result
89 AudioIn::Prepare(AudioSampleType audioSampleType, AudioChannelType audioChannelType, int audioSampleRate)
90 {
91         ClearLastResult();
92         result r = E_SUCCESS;
93
94         SysAssertf(__pAudioInImpl !=  null, "Not yet constructed! Construct() should be called before use");
95
96         r = _AccessController::CheckUserPrivilege(_PRV_AUDIORECORDER);
97         SysTryReturn(NID_MEDIA,  r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED]. This application does not have the privilege to call this method.");
98
99         r = __pAudioInImpl->Prepare(audioSampleType, audioChannelType, audioSampleRate);
100         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
101         return r;
102 }
103
104 result
105 AudioIn::Unprepare(void)
106 {
107         ClearLastResult();
108         result r = E_SUCCESS;
109
110         SysAssertf(__pAudioInImpl !=  null, "Not yet constructed! Construct() should be called before use");
111
112         r = _AccessController::CheckUserPrivilege(_PRV_AUDIORECORDER);
113         SysTryReturn(NID_MEDIA,  r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED]. This application does not have the privilege to call this method.");
114
115         r = __pAudioInImpl->Unprepare();
116         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
117         return r;
118 }
119
120 result
121 AudioIn::AddBuffer(const Tizen::Base::ByteBuffer* pByteBuffer)
122 {
123         ClearLastResult();
124         result r = E_SUCCESS;
125
126         SysAssertf(__pAudioInImpl !=  null, "Not yet constructed! Construct() should be called before use");
127         SysTryReturn(NID_MEDIA, pByteBuffer, E_INVALID_ARG, E_INVALID_ARG, "The value of pByteBuffer is null.");
128
129         r = __pAudioInImpl->AddBuffer(pByteBuffer);
130         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
131         return r;
132 }
133
134 result
135 AudioIn::Start(void)
136 {
137         ClearLastResult();
138         result r = E_SUCCESS;
139
140         SysAssertf(__pAudioInImpl !=  null, "Not yet constructed! Construct() should be called before use");
141
142         r = _AccessController::CheckUserPrivilege(_PRV_AUDIORECORDER);
143         SysTryReturn(NID_MEDIA,  r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED]. This application does not have the privilege to call this method.");
144
145         r = __pAudioInImpl->Start();
146         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
147
148         return r;
149 }
150
151 result
152 AudioIn::Stop(void)
153 {
154         ClearLastResult();
155         result r = E_SUCCESS;
156
157         SysAssertf(__pAudioInImpl !=  null, "Not yet constructed! Construct() should be called before use");
158
159         r = _AccessController::CheckUserPrivilege(_PRV_AUDIORECORDER);
160         SysTryReturn(NID_MEDIA,  r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED]. This application does not have the privilege to call this method.");
161
162         r = __pAudioInImpl->Stop();
163         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
164         return r;
165 }
166
167 result
168 AudioIn::Reset(void)
169 {
170         ClearLastResult();
171         result r = E_SUCCESS;
172
173         SysAssertf(__pAudioInImpl !=  null, "Not yet constructed! Construct() should be called before use");
174
175         r = _AccessController::CheckUserPrivilege(_PRV_AUDIORECORDER);
176         SysTryReturn(NID_MEDIA,  r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED]. This application does not have the privilege to call this method.");
177
178         r = __pAudioInImpl->Reset();
179         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
180         return r;
181 }
182
183 AudioInState
184 AudioIn::GetState(void) const
185 {
186         ClearLastResult();
187         AudioInState audioState = AUDIOIN_STATE_ERROR;
188
189         SysAssertf(__pAudioInImpl !=  null, "Not yet constructed! Construct() should be called before use");
190
191         audioState = __pAudioInImpl->GetState();
192         SysTryReturn(NID_MEDIA, audioState != AUDIOIN_STATE_ERROR , audioState, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
193         return audioState;
194
195 }
196
197 int
198 AudioIn::GetMaxBufferSize(void) const
199 {
200         ClearLastResult();
201         int bufferSize = -1;
202
203         SysAssertf(__pAudioInImpl !=  null, "Not yet constructed! Construct() should be called before use");
204
205         bufferSize = __pAudioInImpl->GetMaxBufferSize();
206         SysTryReturn(NID_MEDIA, bufferSize != -1, bufferSize, GetLastResult(), "[%s] Progogated", GetErrorMessage(GetLastResult()));
207         return bufferSize;
208 }
209
210 int
211 AudioIn::GetMinBufferSize(void) const
212 {
213         ClearLastResult();
214         int bufferSize = -1;
215
216         SysAssertf(__pAudioInImpl !=  null, "Not yet constructed! Construct() should be called before use");
217
218         bufferSize = __pAudioInImpl->GetMinBufferSize();
219         SysTryReturn(NID_MEDIA,  bufferSize != -1, bufferSize, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
220         return bufferSize;
221 }
222
223 AudioSampleType
224 AudioIn::GetOptimizedSampleType(void) const
225 {
226         ClearLastResult();
227         AudioSampleType audioSampleType = AUDIO_TYPE_NONE;
228
229         SysAssertf(__pAudioInImpl !=  null, "Not yet constructed! Construct() should be called before use");
230
231         audioSampleType = __pAudioInImpl->GetOptimizedSampleType();
232         SysTryReturn(NID_MEDIA, audioSampleType != AUDIO_TYPE_NONE , audioSampleType, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
233         return audioSampleType;
234 }
235
236 AudioChannelType
237 AudioIn::GetOptimizedChannelType(void) const
238 {
239         ClearLastResult();
240         AudioChannelType audioChannelType = AUDIO_CHANNEL_TYPE_NONE;
241
242         SysAssertf(__pAudioInImpl !=  null, "Not yet constructed! Construct() should be called before use");
243
244         audioChannelType = __pAudioInImpl->GetOptimizedChannelType();
245         SysTryReturn(NID_MEDIA, audioChannelType != AUDIO_CHANNEL_TYPE_NONE , audioChannelType, GetLastResult(), " [%s] Progpogated", GetErrorMessage(GetLastResult()));
246         return audioChannelType;
247 }
248
249 int
250 AudioIn::GetOptimizedSampleRate(void) const
251 {
252         ClearLastResult();
253         int sampleRate = 0;
254
255         SysAssertf(__pAudioInImpl !=  null, "Not yet constructed! Construct() should be called before use");
256
257         sampleRate = __pAudioInImpl->GetOptimizedSampleRate();
258         SysTryReturn(NID_MEDIA, sampleRate != 0, sampleRate, GetLastResult(), "[%s] Propogeated", GetErrorMessage(GetLastResult()));
259         return sampleRate;
260 }
261
262
263 }} // Tizen::Media