Tizen 2.0 Release
[framework/osp/media.git] / src / FMediaAudioRecorder.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 /**
19  * @file                FMediaAudioRecorder.cpp
20  * @brief               This file contains the implementation of the %AudioRecorder class.
21  */
22
23 #include <FMediaAudioRecorder.h>
24 #include <FBaseSysLog.h>
25 #include <FSec_AccessController.h>
26 #include "FMedia_RecorderTypes.h"
27 #include "FMedia_AudioRecorderImpl.h"
28
29 using namespace Tizen::Base;
30 using namespace Tizen::Base::Collection;
31 using namespace Tizen::Security;
32
33 namespace Tizen { namespace Media
34 {
35
36 AudioRecorder::AudioRecorder(void)
37         : __pImpl(null)
38 {
39
40 }
41
42 AudioRecorder::~AudioRecorder(void)
43 {
44         if (__pImpl != null)
45         {
46                 delete __pImpl;
47         }
48 }
49
50 result
51 AudioRecorder::Construct(IAudioRecorderEventListener& listener)
52 {
53         SysAssertf(__pImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
54         result r = E_SUCCESS;
55
56         //Create impl
57         _AudioRecorderImpl* pImpl = new (std::nothrow) _AudioRecorderImpl();
58         SysTryCatch(NID_MEDIA, pImpl != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.  The object is not created.");
59
60         r = pImpl->Construct(listener);
61         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
62
63         __pImpl = pImpl;
64         return r;
65
66 CATCH:
67         if (pImpl != null)
68         {
69                 delete pImpl;
70                 pImpl = null;
71         }
72         return r;
73 }
74
75 result
76 AudioRecorder::CreateAudioFile(const Tizen::Base::String& mediaLocalPath, bool overwrite)
77 {
78         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
79         result r = E_SUCCESS;
80
81         r = _AccessController::CheckUserPrivilege(_PRV_AUDIORECORDER);
82         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.");
83
84         r = __pImpl->CreateAudioFile(mediaLocalPath, overwrite);
85         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
86
87         return r;
88 }
89
90 result
91 AudioRecorder::Close(void)
92 {
93         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
94         result r = E_SUCCESS;
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 = __pImpl->Close();
100         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
101
102         return r;
103 }
104
105 result
106 AudioRecorder::Record(void)
107 {
108         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
109         result r = E_SUCCESS;
110
111         r = _AccessController::CheckUserPrivilege(_PRV_AUDIORECORDER);
112         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.");
113
114         r = __pImpl->Record();
115         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
116
117         return r;
118 }
119
120 result
121 AudioRecorder::Stop(void)
122 {
123         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
124         result r = E_SUCCESS;
125
126         r = _AccessController::CheckUserPrivilege(_PRV_AUDIORECORDER);
127         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.");
128
129         r = __pImpl->Stop();
130         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
131
132         return r;
133 }
134
135 result
136 AudioRecorder::Pause(void)
137 {
138         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
139         result r = E_SUCCESS;
140
141         r = _AccessController::CheckUserPrivilege(_PRV_AUDIORECORDER);
142         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.");
143
144         r = __pImpl->Pause();
145         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
146
147         return r;
148 }
149
150 result
151 AudioRecorder::Cancel(void)
152 {
153         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
154         result r = E_SUCCESS;
155
156         r = _AccessController::CheckUserPrivilege(_PRV_AUDIORECORDER);
157         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.");
158
159         r = __pImpl->Cancel();
160         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
161
162         return r;
163 }
164
165 RecorderState
166 AudioRecorder::GetState(void) const
167 {
168         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
169         result r = E_SUCCESS;
170         RecorderState state = RECORDER_STATE_ERROR;
171         ClearLastResult();
172
173         state = __pImpl->GetState();
174         r = GetLastResult();
175         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
176
177         return state;
178
179 CATCH:
180         return static_cast<RecorderState>( MEDIA_INVALID_VALUE);
181
182 }
183
184 long
185 AudioRecorder::GetRecordingTime(void) const
186 {
187         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
188         result r = E_SUCCESS;
189         long time = MEDIA_INVALID_VALUE;
190         ClearLastResult();
191
192         time = __pImpl->GetRecordingTime();
193         r = GetLastResult();
194         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
195
196         return time;
197
198 CATCH:
199         return MEDIA_INVALID_VALUE;
200 }
201
202 long
203 AudioRecorder::GetRecordingSize(void) const
204 {
205         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
206         result r = E_SUCCESS;
207         long size = MEDIA_INVALID_VALUE;
208         ClearLastResult();
209
210         size = __pImpl->GetRecordingSize();
211         r = GetLastResult();
212         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
213
214         return size;
215
216 CATCH:
217         return MEDIA_INVALID_VALUE;
218 }
219
220 result
221 AudioRecorder::SetMaxRecordingTime(long msTime)
222 {
223         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
224         result r = E_SUCCESS;
225
226         r = _AccessController::CheckUserPrivilege(_PRV_AUDIORECORDER);
227         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.");
228
229         r = __pImpl->SetMaxRecordingTime(msTime);
230         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
231
232         return r;
233 }
234
235 long
236 AudioRecorder::GetMaxRecordingTime(void) const
237 {
238         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
239         result r = E_SUCCESS;
240         long time = MEDIA_INVALID_VALUE;
241         ClearLastResult();
242
243         time = __pImpl->GetMaxRecordingTime();
244         r = GetLastResult();
245         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
246
247         return time;
248
249 CATCH:
250         return MEDIA_INVALID_VALUE;
251
252 }
253
254
255 result
256 AudioRecorder::SetFormat(AudioRecordingFormat format)
257 {
258         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
259         result r = E_SUCCESS;
260
261         r = _AccessController::CheckUserPrivilege(_PRV_AUDIORECORDER);
262         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.");
263
264         r = __pImpl->SetFormat(format);
265         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
266
267         return r;
268 }
269
270 AudioRecordingFormat
271 AudioRecorder::GetFormat(void) const
272 {
273         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
274         result r = E_SUCCESS;
275         AudioRecordingFormat format = AUDIORECORDING_FORMAT_AMR;
276         ClearLastResult();
277
278         format = __pImpl->GetFormat();
279         r = GetLastResult();
280         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
281
282         return format;
283
284 CATCH:
285         return static_cast<AudioRecordingFormat>( MEDIA_INVALID_VALUE);
286
287 }
288
289 result
290 AudioRecorder::SetFormat(CodecType audioCodec, MediaContainerType container)
291 {
292         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
293         result r = E_SUCCESS;
294
295         r = _AccessController::CheckUserPrivilege(_PRV_AUDIORECORDER);
296         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.");
297
298         r = __pImpl->SetFormat(audioCodec, container);
299         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
300
301         return r;
302 }
303
304 result
305 AudioRecorder::GetFormat(CodecType& audioCodec, MediaContainerType& container) const
306 {
307         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
308         result r = E_SUCCESS;
309
310         __pImpl->GetFormat(audioCodec, container);
311         return r;
312 }
313
314 Tizen::Base::Collection::IListT <CodecType>*
315 AudioRecorder::GetSupportedCodecListN(void) const
316 {
317         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
318         result r = E_SUCCESS;
319         ArrayListT <CodecType>* pDestListT = null;
320         ClearLastResult();
321
322         pDestListT = __pImpl->GetSupportedCodecListN();
323         r = GetLastResult();
324         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
325         return pDestListT;
326
327 CATCH:
328         if (pDestListT != null)
329         {
330                 pDestListT->RemoveAll();
331                 delete pDestListT;
332         }
333
334         return null;
335 }
336
337 Tizen::Base::Collection::IListT <MediaContainerType>*
338 AudioRecorder::GetSupportedContainerListN(void) const
339 {
340         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
341         result r = E_SUCCESS;
342         ArrayListT <MediaContainerType>* pDestListT = null;
343         ClearLastResult();
344
345         pDestListT = __pImpl->GetSupportedContainerListN();
346         r = GetLastResult();
347         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
348
349         return pDestListT;
350
351 CATCH:
352         if (pDestListT != null)
353         {
354                 pDestListT->RemoveAll();
355                 delete pDestListT;
356         }
357
358         return null;
359 }
360
361 result
362 AudioRecorder::SetQuality(RecordingQuality quality)
363 {
364         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
365         result r = E_SUCCESS;
366
367         r = _AccessController::CheckUserPrivilege(_PRV_AUDIORECORDER);
368         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.");
369
370         r = __pImpl->SetQuality(quality);
371         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
372
373         return r;
374 }
375
376 RecordingQuality
377 AudioRecorder::GetQuality(void) const
378 {
379         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
380         result r = E_SUCCESS;
381         RecordingQuality quality = RECORDING_QUALITY_LOW;
382         ClearLastResult();
383
384         quality = __pImpl->GetQuality();
385         r = GetLastResult();
386         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
387
388         return quality;
389
390 CATCH:
391         return static_cast<RecordingQuality>( MEDIA_INVALID_VALUE);
392
393 }
394
395 result
396 AudioRecorder::SetMute(bool mute)
397 {
398         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
399         result r = E_SUCCESS;
400
401         r = _AccessController::CheckUserPrivilege(_PRV_AUDIORECORDER);
402         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.");
403
404         r = __pImpl->SetMute(mute);
405         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
406
407         return r;
408 }
409
410 bool
411 AudioRecorder::IsMuted(void) const
412 {
413         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
414         result r = E_SUCCESS;
415         bool mute = false;
416         ClearLastResult();
417
418         mute = __pImpl->IsMuted();
419         r = GetLastResult();
420         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
421
422         return mute;
423
424 CATCH:
425         return false;
426 }
427
428 }}// Tizen::Media