Tizen 2.0 Release
[framework/osp/media.git] / src / FMedia_RecorderCapability.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                FMedia_RecorderCapability.cpp
20  * @brief               This file contains the implementation of recorder capability header file FMedia_RecorderCapability.h
21  */
22 #include <libxml/tree.h>
23 #include <unique_ptr.h>
24 #include <FBaseSysLog.h>
25 #include <FBaseInteger.h>
26 #include <FBaseColArrayList.h>
27 #include <FGrpDimension.h>
28 #include <FMediaTypes.h>
29 #include "FMedia_RecorderTypes.h"
30 #include "FMedia_RecorderCapability.h"
31 #include "FMedia_RecorderUtil.h"
32 #include "FMedia_RecorderSession.h"
33 #include "FMedia_CamPtrUtil.h"
34
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Collection;
37 using namespace Tizen::Graphics;
38 using namespace Tizen::System;
39
40 namespace Tizen { namespace Media
41 {
42
43 const _RecorderCapability::keyServiceMap _RecorderCapability::__SERVICE_MAP[] =
44 {
45         {
46                 _ROP_NONE, _RECORDER_SOURCE_VIDEO, null
47         },
48         {
49                 _VROP_FILE_FORMAT, _RECORDER_SOURCE_VIDEO, GetSupportedVideoFileFormatN
50         },
51         {
52                 _VROP_AUDIO_ENCODER, _RECORDER_SOURCE_VIDEO, GetSupportedAudioEncoderN
53         },
54         {
55                 _VROP_VIDEO_ENCODER, _RECORDER_SOURCE_VIDEO, GetSupportedVideoEncoderN
56         },
57         {
58                 _AROP_FILE_FORMAT, _RECORDER_SOURCE_AUDIO, GetSupportedAudioFileFormatN
59         },
60         {
61                 _AROP_AUDIO_ENCODER, _RECORDER_SOURCE_AUDIO, GetSupportedAudioEncoderN
62         },
63         {
64                 _ROP_MAX, _RECORDER_SOURCE_VIDEO, null
65         },
66 };
67
68 _RecorderCapability::_RecorderCapability(void)
69 {
70 }
71
72 _RecorderCapability::~_RecorderCapability(void)
73 {
74 }
75
76 Tizen::Base::Collection::ArrayList*
77 _RecorderCapability::GetListN(_RecorderOperationType key, _ResultType& itemType)
78 {
79         //check key boundary condition
80         SysTryReturn(NID_MEDIA, (key > _ROP_NONE && key < _ROP_MAX), null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument(KEY) is used. KEY :%d", key);
81
82         _RecorderSourceType sourceType = __SERVICE_MAP[key].sourceType;
83         _RecorderQueryFunc serviceRoutine = __SERVICE_MAP[key].serviceRoutine;
84
85         //check service handler for a given key
86         SysTryReturn(NID_MEDIA, serviceRoutine != null, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The query function is not found.");
87
88         std::unique_ptr<ArrayList, _ListPtrUtil::Remover> pList (null, _ListPtrUtil::remover);
89         Object* pObj = serviceRoutine(sourceType, _QUERY_LIST, itemType);
90         switch (itemType)
91         {
92         case _RESULT_INTEGER_LIST:
93                 //fall through
94         case _RESULT_PAIR_INTEGER_LIST:
95                 //fall through
96         case _RESULT_DIMENSION_LIST:
97                 //fall through
98         case _RESULT_DOUBLE_LIST:
99                 //fall through
100         case _RESULT_STRING_LIST:
101                 pList.reset(dynamic_cast<ArrayList*>(pObj));
102                 SysTryCatch(NID_MEDIA, pList.get() != null, , E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] There is no data list in key = [%d].", key);
103                 SysTryCatch(NID_MEDIA, pList->GetCount() > 0, , E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Empty data returned for key = [%d]", key);
104                 SysLog(NID_MEDIA, "Valid data returned for key = %d count = %d", key, pList->GetCount());
105                 break;
106
107         default:
108                 SysLogException(NID_MEDIA, E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Supported List is not retrieved. key = [%d]", key);
109                 goto CATCH;
110                 break;
111         }
112         return pList.release();
113
114 CATCH:
115         return null;
116 }
117
118 bool
119 _RecorderCapability::IsSupported(_RecorderOperationType key)
120 {
121         //check key boundary condition
122         SysTryReturn(NID_MEDIA, (key > _ROP_NONE && key < _ROP_MAX), false, E_INVALID_ARG, "[E_INVALID_ARG] The key:%d is invalid.", key);
123
124         _RecorderSourceType sourceType = __SERVICE_MAP[key].sourceType;
125         _RecorderQueryFunc serviceRoutine = __SERVICE_MAP[key].serviceRoutine;
126         _ResultType itemType = _RESULT_NONE;
127
128         //check service handler for a given key
129         SysTryReturn(NID_MEDIA, serviceRoutine != null, false, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The user list is not found.");
130
131         std::unique_ptr<ArrayList, _ListPtrUtil::Remover> pList (null, _ListPtrUtil::remover);
132         Object* pObj = serviceRoutine(sourceType, _QUERY_LIST, itemType);
133         switch (itemType)
134         {
135         case _RESULT_INTEGER_LIST:
136                 //fall through
137         case _RESULT_DIMENSION_LIST:
138                 //fall through
139         case _RESULT_DOUBLE_LIST:
140                 //fall through
141         case _RESULT_STRING_LIST:
142                 pList.reset(dynamic_cast<ArrayList*>(pObj));
143                 SysTryCatch(NID_MEDIA, pList.get() != null, , E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] There is no data list in key = [%d].", key);
144                 SysTryCatch(NID_MEDIA, pList->GetCount()>0, , E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] There is no valid data in key = [%d].", key);
145                 break;
146
147         case _RESULT_PAIR_INTEGER_LIST:
148                 pList.reset(dynamic_cast<ArrayList*>(pObj));
149                 SysTryCatch(NID_MEDIA, pList.get() != null, , E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] There is no data list in key = [%d].", key);
150                 {
151                         Integer* pInteger = null;
152                         pInteger = dynamic_cast<Integer*>(pList->GetAt(1));              // Get max value;
153                         SysTryCatch(NID_MEDIA, pInteger != null, , E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Max value is null for key = [%d].", key);
154                         SysTryCatch(NID_MEDIA, pInteger->ToInt() > 0, , E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Max value is %d for key = [%d].", pInteger->ToInt(), key);
155                 }
156                 break;
157
158         default:
159                 SysLogException(NID_MEDIA, E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] There is no data list in key = [%d].", key);
160                 goto CATCH;
161                 break;
162         }
163         SysLog(NID_MEDIA, "Valid data returned for key = %d count = %d", key, pList->GetCount());
164
165         return true;
166
167 CATCH:
168         return false;
169 }
170
171 Tizen::Base::Object*
172 _RecorderCapability::GetValueN(_RecorderOperationType key, _QueryType queryType, _ResultType& itemType)
173 {
174         Tizen::Base::Object* pValue = null;
175
176         //check key boundary condition
177         SysTryReturn(NID_MEDIA, (key > _ROP_NONE && key < _ROP_MAX), null, E_INVALID_ARG, "[E_INVALID_ARG] The key:%d is invalid.", key);
178         SysTryReturn(NID_MEDIA, (queryType >= _QUERY_DEFAULT_VALUE && queryType < _QUERY_MAX), null, E_INVALID_ARG, "[E_INVALID_ARG] The query:%d is invalid.", queryType);
179
180         _RecorderSourceType sourceType = __SERVICE_MAP[key].sourceType;
181         _RecorderQueryFunc serviceRoutine = __SERVICE_MAP[key].serviceRoutine;
182
183         //check service handler for a given key
184         SysTryReturn(NID_MEDIA, serviceRoutine != null, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The query function is not found.");
185
186         Object* pObj = serviceRoutine(sourceType, queryType, itemType);
187
188         switch (itemType)
189         {
190         case _RESULT_INTEGER:
191                 pValue = dynamic_cast<Integer*>(pObj);
192                 SysTryReturn(NID_MEDIA, null != pValue, null, E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] There is no data list in key = [%d].", key);
193                 break;
194
195         case _RESULT_DIMENSION:
196                 pValue = dynamic_cast<Dimension*>(pObj);
197                 SysTryReturn(NID_MEDIA, null != pValue, null, E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] There is no data list in key = [%d].", key);
198                 break;
199
200         case _RESULT_INTEGER_LIST:
201                 //fall through
202         case _RESULT_PAIR_INTEGER_LIST:
203                 //fall through
204         case _RESULT_DIMENSION_LIST:
205                 //fall through
206         case _RESULT_DOUBLE_LIST:
207                 //fall through
208         case _RESULT_STRING_LIST:
209         default:
210                 SysLogException(NID_MEDIA, E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] Default value cannot be retrived. key = [%d], itemType:%d", key, itemType);
211                 goto CATCH;
212                 break;
213         }
214         SysLog(NID_MEDIA, "Valid data returned for key = %d", key);
215         return pValue;
216
217 CATCH:
218         return null;
219 }
220
221
222 Tizen::Base::Object*
223 _RecorderCapability::GetSupportedVideoFileFormatN(_RecorderSourceType sourceType, _QueryType queryType, _ResultType& itemType)
224 {
225         Object* pfileFormat = null;
226         result r = E_SUCCESS;
227         int err = MM_SUCCESS;
228
229         _RecorderSession recorderSession;
230         r = recorderSession.Construct(sourceType);
231         SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
232
233         _RecorderHandle recorderHandle = recorderSession.GetHandle();
234         SysTryReturn(NID_MEDIA, recorderHandle != MM_INVALID_HANDLE, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Valid handle is not found.");
235
236         if (queryType == _QUERY_LIST)
237         {
238                 std::unique_ptr<_MediaSafeArrayList, _ListPtrUtil::Remover> pfileFormatList (new (std::nothrow) _MediaSafeArrayList(), _ListPtrUtil::remover);
239                 SysTryReturn(NID_MEDIA, pfileFormatList.get() != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
240
241                 err = recorder_foreach_supported_file_format(recorderHandle, SupportedRecorderVideoFileFormatCb, pfileFormatList.get());
242                 SysTryReturn(NID_MEDIA, err == MM_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] Getting the capability failed.");
243
244                 itemType = _RESULT_INTEGER_LIST;
245                 pfileFormat = pfileFormatList.release();
246         }
247         else if (queryType == _QUERY_DEFAULT_VALUE)
248         {
249                 itemType = _RESULT_INTEGER;
250         }
251
252         return pfileFormat;
253 }
254
255 Tizen::Base::Object*
256 _RecorderCapability::GetSupportedAudioFileFormatN(_RecorderSourceType sourceType, _QueryType queryType, _ResultType& itemType)
257 {
258         Object* pfileFormat = null;
259         result r = E_SUCCESS;
260         int err = MM_SUCCESS;
261
262         _RecorderSession recorderSession;
263         r = recorderSession.Construct(sourceType);
264         SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
265
266         _RecorderHandle recorderHandle = recorderSession.GetHandle();
267         SysTryReturn(NID_MEDIA, recorderHandle != MM_INVALID_HANDLE, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Valid handle is not found.");
268
269         if (queryType == _QUERY_LIST)
270         {
271                 std::unique_ptr<_MediaSafeArrayList, _ListPtrUtil::Remover> pfileFormatList (new (std::nothrow) _MediaSafeArrayList(), _ListPtrUtil::remover);
272                 SysTryReturn(NID_MEDIA, pfileFormatList.get() != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
273
274                 err = recorder_foreach_supported_file_format(recorderHandle, SupportedRecorderAudioFileFormatCb, pfileFormatList.get());
275                 SysTryReturn(NID_MEDIA, err == MM_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] Getting the capability failed.");
276
277                 itemType = _RESULT_INTEGER_LIST;
278                 pfileFormat = pfileFormatList.release();
279         }
280         else if (queryType == _QUERY_DEFAULT_VALUE)
281         {
282                 itemType = _RESULT_INTEGER;
283         }
284
285         return pfileFormat;
286 }
287
288
289 Tizen::Base::Object*
290 _RecorderCapability::GetSupportedAudioEncoderN(_RecorderSourceType sourceType, _QueryType queryType, _ResultType& itemType)
291 {
292         Object* pAudioEncoder = null;
293         result r = E_SUCCESS;
294         int err = MM_SUCCESS;
295
296         _RecorderSession recorderSession;
297         r = recorderSession.Construct(sourceType);
298         SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
299
300         _RecorderHandle recorderHandle = recorderSession.GetHandle();
301         SysTryReturn(NID_MEDIA, recorderHandle != MM_INVALID_HANDLE, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Valid handle is not found.");
302
303         if (queryType == _QUERY_LIST)
304         {
305                 std::unique_ptr<_MediaSafeArrayList, _ListPtrUtil::Remover> pAudioEncoderList (new (std::nothrow) _MediaSafeArrayList(), _ListPtrUtil::remover);
306                 SysTryReturn(NID_MEDIA, pAudioEncoderList.get() != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
307
308                 err = recorder_foreach_supported_audio_encoder(recorderHandle, SupportedRecorderAudioEncoderCb, pAudioEncoderList.get());
309                 SysTryReturn(NID_MEDIA, err == MM_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] Getting the capability failed.");
310
311                 itemType = _RESULT_INTEGER_LIST;
312                 pAudioEncoder = pAudioEncoderList.release();
313         }
314         else if (queryType == _QUERY_DEFAULT_VALUE)
315         {
316                 itemType = _RESULT_INTEGER;
317         }
318
319         return pAudioEncoder;
320 }
321
322 Tizen::Base::Object*
323 _RecorderCapability::GetSupportedVideoEncoderN(_RecorderSourceType sourceType, _QueryType queryType, _ResultType& itemType)
324 {
325         Object* pVideoEncoder = null;
326         result r = E_SUCCESS;
327         int err = MM_SUCCESS;
328
329         _RecorderSession recorderSession;
330         r = recorderSession.Construct(sourceType);
331         SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
332
333         _RecorderHandle recorderHandle = recorderSession.GetHandle();
334         SysTryReturn(NID_MEDIA, recorderHandle != MM_INVALID_HANDLE, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Valid handle is not found.");
335
336         if (queryType == _QUERY_LIST)
337         {
338                 std::unique_ptr<_MediaSafeArrayList, _ListPtrUtil::Remover> pVideoEncoderList (new (std::nothrow) _MediaSafeArrayList(), _ListPtrUtil::remover);
339                 SysTryReturn(NID_MEDIA, pVideoEncoderList.get() != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
340
341                 err = recorder_foreach_supported_video_encoder(recorderHandle, SupportedRecorderVideoEncoderCb, pVideoEncoderList.get());
342                 SysTryReturn(NID_MEDIA, err == MM_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] Getting the capability failed.");
343
344                 itemType = _RESULT_INTEGER_LIST;
345                 pVideoEncoder = pVideoEncoderList.release();
346         }
347         else if (queryType == _QUERY_DEFAULT_VALUE)
348         {
349                 itemType = _RESULT_INTEGER;
350         }
351
352         return pVideoEncoder;
353 }
354
355 bool
356 _RecorderCapability::SupportedRecorderVideoFileFormatCb(recorder_file_format_e format, void *pUserData)
357 {
358         result r = E_SUCCESS;
359         MediaContainerType formatValue = (MediaContainerType)MEDIA_INVALID_VALUE;
360
361         _MediaSafeArrayList* pFormatList = static_cast<_MediaSafeArrayList*>(pUserData);
362         SysTryReturn(NID_MEDIA, pFormatList != null, false, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The user list is not found.");
363
364         r = _RecorderUtil::GetOspVideoContainer((recorder_file_format_e)format, formatValue);
365         if (E_SUCCESS == r)
366         {
367                 std::unique_ptr<Object> pInteger (new (std::nothrow) Integer(formatValue));
368                 SysTryReturn(NID_MEDIA, pInteger.get() != null, false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
369
370                 r = pFormatList->AddPtr(std::move(pInteger));
371                 SysTryReturn(NID_MEDIA, r == E_SUCCESS, false, r, "[%s] Propagating.", GetErrorMessage(r));
372         }
373
374         return true;
375 }
376
377 bool
378 _RecorderCapability::SupportedRecorderAudioFileFormatCb(recorder_file_format_e format, void *pUserData)
379 {
380         result r = E_SUCCESS;
381         MediaContainerType formatValue = (MediaContainerType)MEDIA_INVALID_VALUE;
382
383         _MediaSafeArrayList* pFormatList = static_cast<_MediaSafeArrayList*>(pUserData);
384         SysTryReturn(NID_MEDIA, pFormatList != null, false, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The user list is not found.");
385
386         r = _RecorderUtil::GetOspAudioContainer((recorder_file_format_e)format, formatValue);
387         if (E_SUCCESS == r)
388         {
389                 std::unique_ptr<Object> pInteger (new (std::nothrow) Integer(formatValue));
390                 SysTryReturn(NID_MEDIA, pInteger.get() != null, false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
391
392                 r = pFormatList->AddPtr(std::move(pInteger));
393                 SysTryReturn(NID_MEDIA, r == E_SUCCESS, false, r, "[%s] Propagating.", GetErrorMessage(r));
394         }
395
396         return true;
397 }
398
399 bool
400 _RecorderCapability::SupportedRecorderAudioEncoderCb(recorder_audio_codec_e codec, void *pUserData)
401 {
402         result r = E_SUCCESS;
403         CodecType audioCodecType = (CodecType)MEDIA_INVALID_VALUE;
404
405         _MediaSafeArrayList* pAudioEncoderList = static_cast<_MediaSafeArrayList*>(pUserData);
406         SysTryReturn(NID_MEDIA, pAudioEncoderList != null, false, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The user list is not found.");
407
408         r = _RecorderUtil::GetOspAudioCodec((recorder_audio_codec_e)codec, audioCodecType);
409         if (E_SUCCESS == r)
410         {
411                 std::unique_ptr<Object> pInteger (new (std::nothrow) Integer(audioCodecType));
412                 SysTryReturn(NID_MEDIA, pInteger.get() != null, false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
413
414                 r = pAudioEncoderList->AddPtr(std::move(pInteger));
415                 SysTryReturn(NID_MEDIA, r == E_SUCCESS, false, r, "[%s] Propagating.", GetErrorMessage(r));
416         }
417
418         return true;
419 }
420
421 bool
422 _RecorderCapability::SupportedRecorderVideoEncoderCb(recorder_video_codec_e codec, void *pUserData)
423 {
424         result r = E_SUCCESS;
425         CodecType videoCodecType = (CodecType)MEDIA_INVALID_VALUE;
426
427         _MediaSafeArrayList* pVideoEncoderList = static_cast<_MediaSafeArrayList*>(pUserData);
428         SysTryReturn(NID_MEDIA, pVideoEncoderList != null, false, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The user list is not found.");
429
430         r = _RecorderUtil::GetOspVideoCodec((recorder_video_codec_e)codec, videoCodecType);
431         if (E_SUCCESS == r)
432         {
433                 std::unique_ptr<Object> pInteger (new (std::nothrow) Integer(videoCodecType));
434                 SysTryReturn(NID_MEDIA, pInteger.get() != null, false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
435
436                 r = pVideoEncoderList->AddPtr(std::move(pInteger));
437                 SysTryReturn(NID_MEDIA, r == E_SUCCESS, false, r, "[%s] Propagating.", GetErrorMessage(r));
438         }
439
440         return true;
441 }
442
443 }}