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