enable dual camera
[platform/framework/native/media.git] / src / FMedia_CameraCoordinator.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_CameraCoordinator.cpp
20  * @brief                       This file contains the implementation of the %FMedia_CameraCoordinator class.
21  *
22  */
23 #include <FBaseSysLog.h>
24 #include "FMedia_CameraManager.h"
25 #include "FMedia_RecorderManager.h"
26 #include "FMedia_CameraCoordinator.h"
27 #include "FMedia_ICameraCoordinatorListener.h"
28 #include "FMedia_CameraUtil.h"
29 #include "FMedia_RecorderUtil.h"
30
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33
34 namespace Tizen { namespace Media
35 {
36 static const int _ORIENTATION_FOR_RECORDING = 0x1;
37 static const int _ORIENTATION_FOR_CAMERA = 0x1 << 1;
38
39 typedef int (*_CameraFunc)(_CameraHandle mmHandle);
40
41 typedef struct
42 {
43         camera_state_e prevState;
44         camera_state_e postState;
45         _CameraFunc pFunc;
46 }_CameraStateTransitTable;
47
48 static const _CameraStateTransitTable _CAMERA_STATE_TRANSIT[] =
49 {
50         {::CAMERA_STATE_CREATED, ::CAMERA_STATE_PREVIEW, camera_start_preview },
51         {::CAMERA_STATE_CAPTURED, ::CAMERA_STATE_PREVIEW, camera_start_preview },
52         {::CAMERA_STATE_CAPTURED, ::CAMERA_STATE_CREATED, camera_stop_preview },
53         {::CAMERA_STATE_PREVIEW, ::CAMERA_STATE_CREATED, camera_stop_preview },
54 };
55
56 typedef int (*_RecorderFunc)(_RecorderHandle mmHandle);
57
58 typedef struct
59 {
60         recorder_state_e prevState;
61         recorder_state_e postState;
62         _RecorderFunc pFunc;
63 }_RecorderStateTransitTable;
64
65 static const _RecorderStateTransitTable _RECORDER_STATE_TRANSIT[] =
66 {
67         {::RECORDER_STATE_CREATED, ::RECORDER_STATE_READY, recorder_prepare },
68         {::RECORDER_STATE_READY, ::RECORDER_STATE_RECORDING, recorder_start },
69         {::RECORDER_STATE_RECORDING, ::RECORDER_STATE_PAUSED, recorder_pause },
70         {::RECORDER_STATE_RECORDING, ::RECORDER_STATE_READY, recorder_commit },
71 //      {::RECORDER_STATE_RECORDING, ::RECORDER_STATE_READY, recorder_cancel },
72         {::RECORDER_STATE_PAUSED, ::RECORDER_STATE_READY, recorder_commit },
73         {::RECORDER_STATE_PAUSED, ::RECORDER_STATE_READY, recorder_cancel },
74         {::RECORDER_STATE_READY, ::RECORDER_STATE_CREATED, recorder_unprepare },
75 };
76
77 _CameraCoordinatorSafeHashMapT* _CameraCoordinator::__pMap = null;
78
79 _CameraCoordinatorSafeHashMapT::_CameraCoordinatorSafeHashMapT(void)
80 {
81 }
82
83 _CameraCoordinatorSafeHashMapT::~_CameraCoordinatorSafeHashMapT(void)
84 {
85         RemoveItems();
86         _CameraCoordinator::__pMap = null;
87 }
88
89 void
90 _CameraCoordinatorSafeHashMapT::RemoveItems(void)
91 {
92         _CameraCoordinator* pCoordinator = null;
93         result r = E_SUCCESS;
94
95         std::unique_ptr <IListT <_CameraDeviceType>, _ListPtrUtil::Remover> pList (GetKeysN(), _ListPtrUtil::remover);
96         SysTryReturnVoidResult(NID_MEDIA, pList.get() != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] There is no instance.");
97         for (int i = 0; i < GetCount(); i++)
98         {
99                 _CameraDeviceType deviceType = _CAMERA_DEVICE_NONE;
100                 r = pList->GetAt(i, deviceType);
101                 if (IsFailed(r) && deviceType == _CAMERA_DEVICE_NONE)
102                 {
103                         continue;
104                 }
105
106                 r = GetValue(deviceType, pCoordinator);
107                 if (IsFailed(r) && pCoordinator == null)
108                 {
109                         continue;
110                 }
111                 delete pCoordinator;
112         }
113 }
114
115 _CameraCoordinator::_CameraCoordinator()
116         : __cameraDevice(_CAMERA_DEVICE_NONE)
117         , __cameraHandle(MM_INVALID_HANDLE)
118         , __recorderHandle(MM_INVALID_HANDLE)
119         , __mode(_CAMERA_MODE_NONE)
120         , __pListenerList(null, _ListPtrUtil::remover)
121         , __reloadCameraPreviewFormat(::CAMERA_PIXEL_FORMAT_I420)
122 {
123 }
124
125 _CameraCoordinator::~_CameraCoordinator()
126 {
127         _CameraManager::Release(__cameraDevice);
128         SysLog(NID_MEDIA, "Camera manager released");
129 }
130
131 result
132 _CameraCoordinator::Construct(_CameraDeviceType cameraDevice)
133 {
134         result r = E_SUCCESS;
135         _CameraManager* pCameraManager = null;
136         SysLog(NID_MEDIA, "Enter. camera device:%d", cameraDevice);
137
138         pCameraManager = _CameraManager::AddInstance(cameraDevice);
139         r = GetLastResult();
140         SysTryReturn(NID_MEDIA, pCameraManager !=null && r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
141
142         __cameraHandle = pCameraManager->GetHandle();
143
144         __pListenerList.reset(new (std::nothrow) LinkedListT <_ICameraCoordinatorListener*>());
145         SysTryReturn(NID_MEDIA, __pListenerList.get() !=null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.  The object is not created.");
146
147         __cameraDevice = cameraDevice;
148         __mode = _CAMERA_MODE_IMAGE;
149
150         return r;
151 }
152
153 result
154 _CameraCoordinator::AddRecorder(_RecorderDeviceType recorderDevice)
155 {
156         result r = E_SUCCESS;
157         _RecorderManager* pRecorderManager = null;
158         SysLog(NID_MEDIA, "Enter. recorder device:%d", recorderDevice);
159
160         pRecorderManager = _RecorderManager::AddInstance(recorderDevice);
161         r = GetLastResult();
162         SysTryCatch(NID_MEDIA, pRecorderManager !=null && r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
163
164         __recorderHandle = pRecorderManager->GetHandle();               // new recorder handle
165
166         return r;
167
168 CATCH:
169         return r;
170 }
171
172 void
173 _CameraCoordinator::RemoveRecorder(_RecorderDeviceType recorderDevice)
174 {
175         SysLog(NID_MEDIA, "Enter. recorder device:%d", recorderDevice);
176         _RecorderManager::Release(recorderDevice);
177         if (_RecorderManager::HasInstance(recorderDevice) == null)
178         {
179                 __recorderHandle = MM_INVALID_HANDLE;
180         }
181 }
182
183 void
184 _CameraCoordinator::InitSingleton()
185 {
186         result r = E_SUCCESS;
187         std::unique_ptr <_CameraCoordinatorSafeHashMapT> pMap (new (std::nothrow) _CameraCoordinatorSafeHashMapT());
188         SysTryReturnVoidResult(NID_MEDIA, pMap.get() != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.  The object is not created.");
189
190         r = pMap->Construct();
191         SysTryReturnVoidResult(NID_MEDIA, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
192
193         __pMap = pMap.release();
194         std::atexit(DestroySingleton);
195 }
196
197 void
198 _CameraCoordinator::DestroySingleton(void)
199 {
200         delete __pMap;
201 }
202
203 _CameraCoordinator*
204 _CameraCoordinator::AddInstance(_CameraDeviceType cameraDevice)
205 {
206         result r = E_SUCCESS;
207         bool out = false;
208         ClearLastResult();
209
210         _CameraCoordinator* pCoordinator = null;
211
212         SysTryReturn(NID_MEDIA, cameraDevice > _CAMERA_DEVICE_NONE && cameraDevice < _CAMERA_DEVICE_MAX,
213                                 null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument(camderDevice) is used. camderDevice:%d is wrong.", cameraDevice);
214
215         static pthread_once_t once_block = PTHREAD_ONCE_INIT;
216         if (!__pMap)
217         {
218                 pthread_once(&once_block, InitSingleton);
219                 r = GetLastResult();
220                 SysTryReturn(NID_MEDIA, __pMap != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
221         }
222
223         r = __pMap->ContainsKey(cameraDevice, out);
224         SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
225
226         if (out)
227         {
228                 r = __pMap->GetValue(cameraDevice, pCoordinator);
229                 SysTryReturn(NID_MEDIA, r == E_SUCCESS && pCoordinator != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
230         }
231         else
232         {
233                 std::unique_ptr <_CameraCoordinator> pCoord (new (std::nothrow) _CameraCoordinator());
234                 SysTryReturn(NID_MEDIA, pCoord.get() !=null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.  The object is not created.");
235
236                 r = pCoord->Construct(cameraDevice);
237                 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
238
239                 r = __pMap->Add(cameraDevice, pCoord.get());
240                 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
241
242                 pCoordinator = pCoord.release();
243         }
244
245         pCoordinator->AddRefCount();
246         return pCoordinator;
247 }
248
249 _CameraCoordinator*
250 _CameraCoordinator::AddInstance(_RecorderDeviceType recorderDevice)
251 {
252         _CameraCoordinator* pCoordinator = null;
253         _CameraDeviceType cameraDevice = _CAMERA_DEVICE_NONE;
254         ClearLastResult();
255
256         switch (recorderDevice)
257         {
258         case _RECORDER_DEVICE_VIDEO_PRIMARY_CAMERA:
259                 cameraDevice = _CAMERA_DEVICE_PRIMARY;
260                 break;
261
262         case _RECORDER_DEVICE_VIDEO_SECONDARY_CAMERA:
263                 cameraDevice = _CAMERA_DEVICE_SECONDARY;
264                 break;
265
266         default:
267                 break;
268         }
269
270         if (cameraDevice == _CAMERA_DEVICE_NONE)
271         {
272                 SysLogException(NID_MEDIA, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument(recorderDevice) is used. recorderDevice :%d is wrong.", recorderDevice);
273         }
274         else
275         {
276                 pCoordinator = AddInstance(cameraDevice);
277                 if (pCoordinator == null)
278                 {
279                         SysLogException(NID_MEDIA, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The camera device(%d)'s coordinator is not available.", cameraDevice);
280                 }
281         }
282         return pCoordinator;
283 }
284
285 void
286 _CameraCoordinator::Release(_CameraDeviceType cameraDevice)
287 {
288         result r = E_SUCCESS;
289
290         if (__pMap != null)
291         {
292                 _CameraCoordinator* pCoordinator = null;
293                 r = __pMap->GetValue(cameraDevice, pCoordinator);
294                 SysTryReturnVoidResult(NID_MEDIA, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
295
296                 if (pCoordinator->ReleaseRefCount() == 0)
297                 {
298                         delete pCoordinator;
299                         r = __pMap->Remove(cameraDevice);
300                         SysTryReturnVoidResult(NID_MEDIA, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
301                 }
302         }
303 }
304
305 void
306 _CameraCoordinator::Release(_RecorderDeviceType recorderDevice)
307 {
308         if (__pMap != null)
309         {
310                 _CameraDeviceType cameraDevice = _CAMERA_DEVICE_NONE;
311
312                 switch (recorderDevice)
313                 {
314                 case _RECORDER_DEVICE_VIDEO_PRIMARY_CAMERA:
315                         cameraDevice = _CAMERA_DEVICE_PRIMARY;
316                         break;
317                 case _RECORDER_DEVICE_VIDEO_SECONDARY_CAMERA:
318                         cameraDevice = _CAMERA_DEVICE_SECONDARY;
319                         break;
320                 default:
321                         break;
322                 }
323
324                 Release(cameraDevice);
325         }
326 }
327
328 _CameraCoordinator*
329 _CameraCoordinator::HasInstance(_CameraDeviceType cameraDevice)
330 {
331         result r = E_SUCCESS;
332         _CameraCoordinator* pCoordinator = null;
333         bool out = false;
334         ClearLastResult();
335
336         SysTryReturn(NID_MEDIA, cameraDevice > _CAMERA_DEVICE_NONE && cameraDevice < _CAMERA_DEVICE_MAX,
337                                 null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument(camderDevice) is used. camderDevice:%d is wrong.", cameraDevice);
338         SysTryReturn(NID_MEDIA, __pMap != null, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] No coordinator was created.");
339
340         r = __pMap->ContainsKey(cameraDevice, out);
341         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
342         SysTryCatch(NID_MEDIA, out == true, r = E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Camera device(%d) was not created.", cameraDevice);
343
344         r = __pMap->GetValue(cameraDevice, pCoordinator);
345         SysTryCatch(NID_MEDIA, r == E_SUCCESS && pCoordinator != null, , r, "[%s] Propagating.", GetErrorMessage(r));
346
347         return pCoordinator;
348
349 CATCH:
350         return null;
351 }
352
353 _CameraCoordinator*
354 _CameraCoordinator::HasInstance(_RecorderDeviceType recorderDevice)
355 {
356         _CameraCoordinator* pCoordinator = null;
357         _CameraDeviceType cameraDevice = _CAMERA_DEVICE_NONE;
358         ClearLastResult();
359
360         switch (recorderDevice)
361         {
362         case _RECORDER_DEVICE_VIDEO_PRIMARY_CAMERA:
363                 cameraDevice = _CAMERA_DEVICE_PRIMARY;
364                 break;
365
366         case _RECORDER_DEVICE_VIDEO_SECONDARY_CAMERA:
367                 cameraDevice = _CAMERA_DEVICE_SECONDARY;
368                 break;
369
370         default:
371                 break;
372         }
373
374         if (cameraDevice == _CAMERA_DEVICE_NONE)
375         {
376                 SysLogException(NID_MEDIA, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument(recorderDevice) is used. recorderDevice :%d is wrong.", recorderDevice);
377         }
378         else
379         {
380                 pCoordinator = HasInstance(cameraDevice);
381                 if (pCoordinator == null)
382                 {
383                         SysLogException(NID_MEDIA, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The camera device(%d)'s coordinator is not available.", cameraDevice);
384                 }
385         }
386         return pCoordinator;
387 }
388
389 result
390 _CameraCoordinator::AddCameraCoordinatorListener(_ICameraCoordinatorListener& listener)
391 {
392         result r = E_SUCCESS;
393         r = __pListenerList->Add(&listener);
394         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
395         return r;
396 }
397
398 result
399 _CameraCoordinator::RemoveCameraCoordinatorListener(_ICameraCoordinatorListener& listener)
400 {
401         result r = E_SUCCESS;
402         r = __pListenerList->Remove(&listener);
403         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
404
405         r = NotifyOtherRemoved();
406         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
407         return r;
408 }
409
410 _CameraMode
411 _CameraCoordinator::GetMode(void) const
412 {
413         return __mode;
414 }
415
416 _CameraHandle
417 _CameraCoordinator::GetCameraHandle(void) const
418 {
419         return __cameraHandle;
420 }
421
422 _RecorderHandle
423 _CameraCoordinator::GetRecorderHandle(void) const
424 {
425         return __recorderHandle;
426 }
427
428 result
429 _CameraCoordinator::NotifyModeChangePrepared(_CameraMode mode)
430 {
431         result r = E_SUCCESS;
432         _ICameraCoordinatorListener* pListener = null;
433         SysLog(NID_MEDIA, "mode:%d", mode);
434
435         std::unique_ptr <IEnumeratorT <_ICameraCoordinatorListener*> > pEnum (__pListenerList->GetEnumeratorN());
436         SysTryReturn(NID_MEDIA, pEnum.get() != null, GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
437
438         while (pEnum->MoveNext() == E_SUCCESS)
439         {
440                 r = pEnum->GetCurrent(pListener);
441                 SysTryReturn(NID_MEDIA, pListener != null && r == E_SUCCESS, E_SYSTEM, E_SYSTEM,
442                                    "[E_SYSTEM] A system error has been occurred.  The listener is not proper");
443
444                 r = pListener->OnCameraCoordinatorModeChangePrepared(mode);
445                 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
446         }
447         return r;
448 }
449
450 result
451 _CameraCoordinator::NotifyModeChanged(_CameraMode mode)
452 {
453         result r = E_SUCCESS;
454         _ICameraCoordinatorListener* pListener = null;
455         SysLog(NID_MEDIA, "mode:%d", mode);
456
457         std::unique_ptr <IEnumeratorT <_ICameraCoordinatorListener*> > pEnum (__pListenerList->GetEnumeratorN());
458         SysTryReturn(NID_MEDIA, pEnum.get() != null, GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
459
460         while (pEnum->MoveNext() == E_SUCCESS)
461         {
462                 r = pEnum->GetCurrent(pListener);
463                 SysTryReturn(NID_MEDIA, pListener != null && r == E_SUCCESS, E_SYSTEM, E_SYSTEM,
464                                    "[E_SYSTEM] A system error has been occurred.  The listener is not proper");
465
466                 r = pListener->OnCameraCoordinatorModeChanged(mode);
467                 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
468         }
469         return r;
470 }
471
472 result
473 _CameraCoordinator::NotifyOtherRemoved(void)
474 {
475         result r = E_SUCCESS;
476         _ICameraCoordinatorListener* pListener = null;
477         SysLog(NID_MEDIA, "Enter");
478
479         std::unique_ptr <IEnumeratorT <_ICameraCoordinatorListener*> > pEnum (__pListenerList->GetEnumeratorN());
480         SysTryReturn(NID_MEDIA, pEnum.get() != null, GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
481
482         while (pEnum->MoveNext() == E_SUCCESS)
483         {
484                 r = pEnum->GetCurrent(pListener);
485                 SysTryReturn(NID_MEDIA, pListener != null && r == E_SUCCESS, E_SYSTEM, E_SYSTEM,
486                                    "[E_SYSTEM] A system error has been occurred.  The listener is not proper");
487
488                 r = pListener->OnCameraCoordinatorOtherRemoved();
489                 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
490         }
491         return r;
492 }
493
494 result
495 _CameraCoordinator::StartMmPreview(void)
496 {
497         result r = E_SUCCESS;
498         SysLog(NID_MEDIA, "Enter. mode:%d", __mode);
499
500         switch (__mode)
501         {
502         case _CAMERA_MODE_IMAGE:
503                 r = ChangeCameraStateTo(::CAMERA_STATE_PREVIEW);
504                 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
505                 break;
506
507         case _CAMERA_MODE_VIDEO:
508                 r = ChangeRecorderStateTo(::RECORDER_STATE_READY);
509                 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
510                 break;
511
512         default:
513                 SysLogException(NID_MEDIA, E_INVALID_STATE, "[E_INVALID_STATE] Camera is in an invalid state. Camcorder mode :%d is wrong", __mode);
514                 goto CATCH;
515                 break;
516         }
517
518 CATCH:
519         return r;
520 }
521
522 result
523 _CameraCoordinator::StopMmPreview(void)
524 {
525         result r = E_SUCCESS;
526         SysLog(NID_MEDIA, "Enter.");
527
528         if (__mode == _CAMERA_MODE_IMAGE)
529         {
530                 r = ChangeCameraStateTo(::CAMERA_STATE_CREATED);
531                 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
532         }
533         else if (__mode == _CAMERA_MODE_VIDEO)
534         {
535                 recorder_state_e recorderState = ::RECORDER_STATE_NONE;
536                 recorderState = GetRecorderState();
537
538                 switch (recorderState)
539                 {
540                 case ::RECORDER_STATE_RECORDING:
541                         r = ChangeRecorderStateTo(::RECORDER_STATE_READY);
542                         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
543
544                         r = ChangeRecorderStateTo(::RECORDER_STATE_CREATED);
545                         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
546                         break;
547
548                 case ::RECORDER_STATE_READY:
549                         r = ChangeRecorderStateTo(::RECORDER_STATE_CREATED);
550                         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
551                         break;
552
553                 default:
554                         r = E_INVALID_STATE;
555                         SysLogException(NID_MEDIA, E_INVALID_STATE, "[E_INVALID_STATE] recorderState is in an invalid state. recorderState:%d", recorderState);
556                         break;
557                 }
558         }
559         else
560         {
561                 r = E_INVALID_STATE;
562                 SysLogException(NID_MEDIA, E_INVALID_STATE, "[E_INVALID_STATE] CamcorderMode is in an invalid state. mode :%d", __mode);
563         }
564
565 CATCH:
566         return r;
567 }
568
569 result
570 _CameraCoordinator::CreateVideoRecorder(void)
571 {
572         result r = E_SUCCESS;
573         _RecorderDeviceType recorderDevice = _RECORDER_DEVICE_NONE;
574         SysLog(NID_MEDIA, "enter.");
575
576         // Start recorder
577         recorderDevice = __cameraDevice == _CAMERA_DEVICE_PRIMARY ?
578         _RECORDER_DEVICE_VIDEO_PRIMARY_CAMERA : _RECORDER_DEVICE_VIDEO_SECONDARY_CAMERA;
579         r = AddRecorder(recorderDevice);                // recorder_create() will be called.
580         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
581         __mode = _CAMERA_MODE_VIDEO;
582
583         return r;
584 CATCH:
585         return r;
586 }
587
588 result
589 _CameraCoordinator::DestroyVideoRecorder(void)
590 {
591         result r = E_SUCCESS;
592         int err = MM_SUCCESS;
593         recorder_state_e recorderState = ::RECORDER_STATE_NONE;
594         _RecorderDeviceType recorderDevice = _RECORDER_DEVICE_NONE;
595         SysLog(NID_MEDIA, "enter.");
596
597         if ( __recorderHandle != MM_INVALID_HANDLE )
598         {
599                 recorderState = GetRecorderState();
600                 // Stop the recorder
601                 switch (recorderState)
602                 {
603                 case ::RECORDER_STATE_NONE:
604                         break;
605
606                 case ::RECORDER_STATE_CREATED:
607                         //fall through
608                 case ::RECORDER_STATE_READY:
609                         recorderDevice = __cameraDevice == _CAMERA_DEVICE_PRIMARY ?
610                                 _RECORDER_DEVICE_VIDEO_PRIMARY_CAMERA : _RECORDER_DEVICE_VIDEO_SECONDARY_CAMERA;
611                         RemoveRecorder(recorderDevice);
612                         __mode = _CAMERA_MODE_IMAGE;
613                         break;
614
615                 case ::RECORDER_STATE_RECORDING:
616                         //fall through
617                 case ::RECORDER_STATE_PAUSED:
618                         err = recorder_cancel(__recorderHandle);
619                         r = ConvertResult(err);
620                         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
621                         recorderDevice = __cameraDevice == _CAMERA_DEVICE_PRIMARY ?
622                                 _RECORDER_DEVICE_VIDEO_PRIMARY_CAMERA : _RECORDER_DEVICE_VIDEO_SECONDARY_CAMERA;
623                         RemoveRecorder(recorderDevice);
624                         __mode = _CAMERA_MODE_IMAGE;
625                         break;
626
627                 default:
628                         break;
629                 }
630         }
631
632         return r;
633 CATCH:
634         return r;
635 }
636
637
638 result
639 _CameraCoordinator::ChangeFormat(_CameraMode mode, bool callback, camera_pixel_format_e reloadCameraPreviewFormat)
640 {
641         result r = E_SUCCESS;
642         int err = MM_SUCCESS;
643         _RecorderDeviceType recorderDevice = _RECORDER_DEVICE_NONE;
644         camera_state_e cameraState = ::CAMERA_STATE_NONE;
645         recorder_state_e recorderState = ::RECORDER_STATE_NONE;
646         SysLog(NID_MEDIA, "enter. mode:%d, callback:%d,         reloadCameraPreviewFormat:%d", mode, callback, reloadCameraPreviewFormat);
647
648         if (mode == _CAMERA_MODE_VIDEO)
649         {
650                 cameraState = GetCameraState();
651
652                 switch (cameraState)
653                 {
654                 case ::CAMERA_STATE_CREATED:
655                         //fall through
656                 case ::CAMERA_STATE_NONE:
657                         break;
658
659                 case ::CAMERA_STATE_PREVIEW:
660                         //fall through
661                 case ::CAMERA_STATE_CAPTURED:
662                         r = ChangeCameraStateTo(::CAMERA_STATE_CREATED);
663                         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
664                         break;
665
666                 case ::CAMERA_STATE_CAPTURING:
667                         r = E_INVALID_STATE;
668                         SysLogException(NID_MEDIA, r, "[E_INVALID_STATE] cameraState(%d) is in an invalid state. While the camera is capturing, the mode cannot be changed.", cameraState);
669                         goto CATCH;
670                         break;
671
672                 default:
673                         break;
674                 }
675
676                 __mode = _CAMERA_MODE_VIDEO;
677                 __reloadCameraPreviewFormat = reloadCameraPreviewFormat;
678                 if (callback)
679                 {
680                         r = NotifyModeChangePrepared(mode);
681                         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
682                 }
683
684                 switch (cameraState)
685                 {
686                 case ::CAMERA_STATE_NONE:
687                         //fall through
688                 case ::CAMERA_STATE_CREATED:
689                         break;
690
691                 case ::CAMERA_STATE_PREVIEW:
692                         r = ChangeRecorderStateTo(::RECORDER_STATE_READY);
693                         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
694                         break;
695
696                 case ::CAMERA_STATE_CAPTURED:
697                 default:
698                         break;
699                 }
700         }
701         else if ( mode == _CAMERA_MODE_IMAGE )
702         {
703                 recorderState = GetRecorderState();
704
705                 switch (recorderState)
706                 {
707                 case ::RECORDER_STATE_NONE:
708                         break;
709                 case ::RECORDER_STATE_CREATED:
710                         __mode = _CAMERA_MODE_IMAGE;
711                         __reloadCameraPreviewFormat = reloadCameraPreviewFormat;
712                         if (callback)
713                         {
714                                 r = NotifyModeChangePrepared(mode);
715                                 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
716                         }
717                         break;
718
719                 case ::RECORDER_STATE_READY:
720                         r = ChangeRecorderStateTo(::RECORDER_STATE_CREATED);
721                         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
722
723                         __mode = _CAMERA_MODE_IMAGE;
724                         __reloadCameraPreviewFormat = reloadCameraPreviewFormat;
725                         if (callback)
726                         {
727                                 r = NotifyModeChangePrepared(mode);
728                                 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
729                         }
730                         r = ChangeCameraStateTo(::CAMERA_STATE_PREVIEW);
731                         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
732                         break;
733
734                 case ::RECORDER_STATE_RECORDING:
735                         //fall through
736                 case ::RECORDER_STATE_PAUSED:
737                         err = recorder_cancel(__recorderHandle);                // TODO
738                         r = ConvertResult(err);
739                         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
740
741                         r = ChangeRecorderStateTo(::RECORDER_STATE_CREATED);
742                         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
743                         recorderDevice = __cameraDevice == _CAMERA_DEVICE_PRIMARY ?
744                                 _RECORDER_DEVICE_VIDEO_PRIMARY_CAMERA : _RECORDER_DEVICE_VIDEO_SECONDARY_CAMERA;
745
746                         __mode = _CAMERA_MODE_IMAGE;
747                         __reloadCameraPreviewFormat = reloadCameraPreviewFormat;
748                         if (callback)
749                         {
750                                 r = NotifyModeChangePrepared(mode);
751                                 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
752                         }
753                         r = ChangeCameraStateTo(::CAMERA_STATE_PREVIEW);
754                         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
755                         break;
756
757                 default:
758                         break;
759                 }
760         }
761
762         if (callback)
763         {
764                 // Notify that the mode is changed to callback camera and video recorder's configurations.
765                 r = NotifyModeChanged(mode);
766                 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
767         }
768         return r;
769 CATCH:
770         return r;
771 }
772
773 result
774 _CameraCoordinator::ChangeMode(_CameraMode mode, bool callback)
775 {
776         result r = E_SUCCESS;
777         camera_state_e cameraState = ::CAMERA_STATE_NONE;
778         recorder_state_e recorderState = ::RECORDER_STATE_NONE;
779         _RecorderDeviceType recorderDevice = _RECORDER_DEVICE_NONE;
780         int err = MM_SUCCESS;
781
782         SysLog(NID_MEDIA, "Mode should be changed. mode:%d", mode);
783         if (mode == _CAMERA_MODE_VIDEO)
784         {
785                 cameraState = GetCameraState();
786
787                 switch (cameraState)
788                 {
789                 case ::CAMERA_STATE_CREATED:
790                         //fall through
791                 case ::CAMERA_STATE_NONE:
792                         break;
793
794                 case ::CAMERA_STATE_PREVIEW:
795                         //fall through
796                 case ::CAMERA_STATE_CAPTURED:
797                         r = ChangeCameraStateTo(::CAMERA_STATE_CREATED);
798                         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
799                         break;
800
801                 case ::CAMERA_STATE_CAPTURING:
802                         r = E_INVALID_STATE;
803                         SysLogException(NID_MEDIA, r, "[E_INVALID_STATE] cameraState(%d) is in an invalid state. While the camera is capturing, the mode cannot be changed.", cameraState);
804                         goto CATCH;
805                         break;
806
807                 default:
808                         break;
809                 }
810
811                 // Start recorder
812                 recorderDevice = __cameraDevice == _CAMERA_DEVICE_PRIMARY ?
813                         _RECORDER_DEVICE_VIDEO_PRIMARY_CAMERA : _RECORDER_DEVICE_VIDEO_SECONDARY_CAMERA;
814                 r = AddRecorder(recorderDevice);                // recorder_create() will be called.
815                 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
816                 __mode = _CAMERA_MODE_VIDEO;
817
818                 if (callback)
819                 {
820                         r = NotifyModeChangePrepared(mode);
821                         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
822                 }
823
824                 switch (cameraState)
825                 {
826                 case ::CAMERA_STATE_NONE:
827                         //fall through
828                 case ::CAMERA_STATE_CREATED:
829                         break;
830
831                 case ::CAMERA_STATE_PREVIEW:
832                         r = ChangeRecorderStateTo(::RECORDER_STATE_READY);
833                         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
834                         break;
835
836                 case ::CAMERA_STATE_CAPTURED:
837                 default:
838                         break;
839                 }
840         }
841         else if ( mode == _CAMERA_MODE_IMAGE )
842         {
843                 recorderState = GetRecorderState();
844
845                 // Stop the recorder
846                 switch (recorderState)
847                 {
848                 case ::RECORDER_STATE_NONE:
849                         break;
850
851                 case ::RECORDER_STATE_CREATED:
852                         recorderDevice = __cameraDevice == _CAMERA_DEVICE_PRIMARY ?
853                                 _RECORDER_DEVICE_VIDEO_PRIMARY_CAMERA : _RECORDER_DEVICE_VIDEO_SECONDARY_CAMERA;
854                         RemoveRecorder(recorderDevice);         // the mode will be changed to IMAGE.
855                         __mode = _CAMERA_MODE_IMAGE;
856                         break;
857
858                 case ::RECORDER_STATE_READY:
859                         r = ChangeRecorderStateTo(::RECORDER_STATE_CREATED);
860                         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
861
862                         recorderDevice = __cameraDevice == _CAMERA_DEVICE_PRIMARY ?
863                                 _RECORDER_DEVICE_VIDEO_PRIMARY_CAMERA : _RECORDER_DEVICE_VIDEO_SECONDARY_CAMERA;
864                         RemoveRecorder(recorderDevice);
865                         __mode = _CAMERA_MODE_IMAGE;
866                         if (callback)
867                         {
868                                 r = NotifyModeChangePrepared(mode);
869                                 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
870                         }
871                         r = ChangeCameraStateTo(::CAMERA_STATE_PREVIEW);
872                         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
873                         break;
874
875                 case ::RECORDER_STATE_RECORDING:
876                         //fall through
877                 case ::RECORDER_STATE_PAUSED:
878                         err = recorder_cancel(__recorderHandle);                // TODO
879                         r = ConvertResult(err);
880                         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
881
882                         r = ChangeRecorderStateTo(::RECORDER_STATE_CREATED);
883                         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
884                         recorderDevice = __cameraDevice == _CAMERA_DEVICE_PRIMARY ?
885                                 _RECORDER_DEVICE_VIDEO_PRIMARY_CAMERA : _RECORDER_DEVICE_VIDEO_SECONDARY_CAMERA;
886
887                         RemoveRecorder(recorderDevice);
888                         __mode = _CAMERA_MODE_IMAGE;
889                         if (callback)
890                         {
891                                 r = NotifyModeChangePrepared(mode);
892                                 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
893                         }
894                         r = ChangeCameraStateTo(::CAMERA_STATE_PREVIEW);
895                         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
896                         break;
897
898                 default:
899                         break;
900                 }
901         }
902
903         if (callback)
904         {
905                 // Notify that the mode is changed to callback camera and video recorder's configurations.
906                 r = NotifyModeChanged(mode);
907                 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
908         }
909
910         return r;
911
912 CATCH:
913         return r;
914 }
915
916 bool
917 _CameraCoordinator::IsCalmChangeMode(_CameraMode mode)
918 {
919         camera_state_e cameraState = ::CAMERA_STATE_NONE;
920         recorder_state_e recorderState = ::RECORDER_STATE_NONE;
921         bool calmChange = false;
922
923         if (mode == _CAMERA_MODE_VIDEO)
924         {
925                 cameraState = GetCameraState();
926
927                 switch (cameraState)
928                 {
929                 case ::CAMERA_STATE_CREATED:
930                         //fall through
931                 case ::CAMERA_STATE_NONE:
932                         calmChange = true;
933                         break;
934
935                 case ::CAMERA_STATE_PREVIEW:
936                         //fall through
937                 case ::CAMERA_STATE_CAPTURED:
938                         //fall through
939                 case ::CAMERA_STATE_CAPTURING:
940                         calmChange = false;
941                         break;
942
943                 default:
944                         break;
945                 }
946         }
947         else if ( mode == _CAMERA_MODE_IMAGE )
948         {
949                 recorderState = GetRecorderState();
950
951                 // Stop the recorder
952                 switch (recorderState)
953                 {
954                 case ::RECORDER_STATE_NONE:
955                         //fall through
956                 case ::RECORDER_STATE_CREATED:
957                         calmChange = true;
958                         break;
959
960                 case ::RECORDER_STATE_READY:
961                         //fall through
962                 case ::RECORDER_STATE_RECORDING:
963                         //fall through
964                 case ::RECORDER_STATE_PAUSED:
965                         calmChange = false;
966                         break;
967
968                 default:
969                         break;
970                 }
971         }
972
973         SysLog(NID_MEDIA, "mode:%d, calm:%d", mode, calmChange);
974         return calmChange;
975 }
976
977
978 _CameraDeviceType
979 _CameraCoordinator::GetCameraDevice(void) const
980 {
981         return __cameraDevice;
982 }
983
984 camera_state_e
985 _CameraCoordinator::GetCameraState(void) const
986 {
987         result r = E_SUCCESS;
988         int err = MM_SUCCESS;
989         camera_state_e state = ::CAMERA_STATE_NONE;
990
991         err = camera_get_state(__cameraHandle, &state);
992         r = ConvertResult(err);
993         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
994
995         return state;
996
997 CATCH:
998         return ::CAMERA_STATE_NONE;
999 }
1000
1001 recorder_state_e
1002 _CameraCoordinator::GetRecorderState(void) const
1003 {
1004         result r = E_SUCCESS;
1005         int err = MM_SUCCESS;
1006         recorder_state_e state = ::RECORDER_STATE_NONE;
1007
1008         err = recorder_get_state(__recorderHandle, &state);
1009         r = ConvertResult(err);
1010         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
1011
1012         return state;
1013
1014 CATCH:
1015         return ::RECORDER_STATE_NONE;
1016 }
1017
1018 result
1019 _CameraCoordinator::ConvertResult(int err) const
1020 {
1021         result r = E_SYSTEM;
1022         if (err != CAMERA_ERROR_NONE)
1023         {
1024                 SysLog(NID_MEDIA, "MM Err:0x%x", err);
1025         }
1026
1027         // Global error
1028         if (err == CAMERA_ERROR_NONE || err == RECORDER_ERROR_NONE)
1029         {
1030                 r = E_SUCCESS;
1031         }
1032         else if (err == CAMERA_ERROR_INVALID_PARAMETER || err == RECORDER_ERROR_INVALID_PARAMETER)
1033         {
1034                 r = E_INVALID_ARG;
1035         }
1036         else if (err == CAMERA_ERROR_INVALID_STATE || err == RECORDER_ERROR_INVALID_STATE)
1037         {
1038                 r = E_INVALID_STATE;
1039         }
1040         else if (err == CAMERA_ERROR_OUT_OF_MEMORY || err == RECORDER_ERROR_OUT_OF_MEMORY)
1041         {
1042                 r = E_OUT_OF_MEMORY;
1043         }
1044         else if (err == CAMERA_ERROR_DEVICE || err == RECORDER_ERROR_DEVICE)
1045         {
1046                 r = E_DEVICE_FAILED;
1047         }
1048         else if (err == CAMERA_ERROR_INVALID_OPERATION
1049                         || err == CAMERA_ERROR_SECURITY_RESTRICTED
1050                         || err == RECORDER_ERROR_INVALID_OPERATION
1051                         || err == RECORDER_ERROR_SECURITY_RESTRICTED)
1052         {
1053                 r = E_SYSTEM;
1054         }
1055         else if (err == CAMERA_ERROR_DEVICE_BUSY
1056                         || err == CAMERA_ERROR_SOUND_POLICY
1057                         || err == RECORDER_ERROR_SOUND_POLICY)
1058         {
1059                 r = E_DEVICE_BUSY;
1060         }
1061         else if (err == CAMERA_ERROR_DEVICE_NOT_FOUND)
1062         {
1063                 r = E_DEVICE_UNAVAILABLE;
1064         }
1065         else
1066         {
1067                 r = E_UNKNOWN;
1068         }
1069
1070         return r;
1071 }
1072
1073
1074 result
1075 _CameraCoordinator::ChangeCameraStateTo(camera_state_e destState)
1076 {
1077         result r = E_SUCCESS;
1078         int tableTotalCount = 0;
1079         int i = 0;
1080         int err = MM_SUCCESS;
1081         camera_state_e preState = ::CAMERA_STATE_NONE;
1082         camera_state_e postState = ::CAMERA_STATE_NONE;
1083
1084         preState = GetCameraState();
1085         tableTotalCount = sizeof(_CAMERA_STATE_TRANSIT) / sizeof(_CAMERA_STATE_TRANSIT[0]);
1086         SysLog(NID_MEDIA, " PreState:%d, destState:%d, tableTotalCount:%d", preState, destState, tableTotalCount);
1087
1088         for (i = 0; i < tableTotalCount; i++)
1089         {
1090                 if (preState == _CAMERA_STATE_TRANSIT[i].prevState && destState == _CAMERA_STATE_TRANSIT[i].postState)
1091                 {
1092                         if (_CAMERA_STATE_TRANSIT[i].pFunc != null)
1093                         {
1094                                 SysLog(NID_MEDIA,
1095                                                         "Exist the matching state field. loop i:%d, current state:%d, destState:%d", i,
1096                                                         preState, destState);
1097                                 err = _CAMERA_STATE_TRANSIT[i].pFunc(__cameraHandle);
1098                                 r = ConvertResult(err);
1099                                 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating. i:%d", GetErrorMessage(r), i);
1100
1101                                 // Check whether the retuned states are destinationed states.
1102                                 postState = GetCameraState();
1103                                 SysTryCatch(NID_MEDIA, destState == postState, r = E_INVALID_STATE, E_INVALID_STATE,
1104                                                    "[E_INVALID_STATE] camera_state_e of C-API is in an invalid state. result preState:%d, is different from the desired state:%d", postState,
1105                                                    destState);
1106                                 SysLog(NID_MEDIA, "[E_SUCCESS] Found. result postState:%d, param destState:%d", postState,
1107                                                         destState);
1108                         }
1109                         break;
1110                 }
1111
1112         }
1113         SysTryCatch(NID_MEDIA, i != tableTotalCount, r = E_INVALID_STATE, E_INVALID_STATE, "[E_INVALID_STATE] Not found. ");
1114         return r;
1115
1116 CATCH:
1117         SysLog(NID_MEDIA, "[%s] Real preState:%d, param destState:%d", GetErrorMessage(r), preState, destState);
1118         return r;
1119 }
1120
1121 result
1122 _CameraCoordinator::ChangeRecorderStateTo(recorder_state_e destState)
1123 {
1124         result r = E_SUCCESS;
1125         int tableTotalCount = 0;
1126         int i = 0;
1127         int err = MM_SUCCESS;
1128         recorder_state_e preState = ::RECORDER_STATE_NONE;
1129         recorder_state_e postState = ::RECORDER_STATE_NONE;
1130
1131         preState = GetRecorderState();
1132         tableTotalCount = sizeof(_RECORDER_STATE_TRANSIT) / sizeof(_RECORDER_STATE_TRANSIT[0]);
1133         SysLog(NID_MEDIA, " PreState:%d, destState:%d, tableTotalCount:%d", preState, destState, tableTotalCount);
1134
1135         for (i = 0; i < tableTotalCount; i++)
1136         {
1137                 if (preState == _RECORDER_STATE_TRANSIT[i].prevState && destState == _RECORDER_STATE_TRANSIT[i].postState)
1138                 {
1139                         if (_RECORDER_STATE_TRANSIT[i].pFunc != null)
1140                         {
1141                                 SysLog(NID_MEDIA,
1142                                                         "Exist the matching state field. loop i:%d, current state:%d, destState:%d", i,
1143                                                         preState, destState);
1144                                 err = _RECORDER_STATE_TRANSIT[i].pFunc(__recorderHandle);
1145                                 r = ConvertResult(err);
1146                                 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating. i:%d", GetErrorMessage(r), i);
1147
1148                                 // Check whether the retuned states are destinationed states.
1149                                 postState = GetRecorderState();
1150                                 SysTryCatch(NID_MEDIA, destState == postState, r = E_INVALID_STATE, E_INVALID_STATE,
1151                                                    "[E_INVALID_STATE] recorder_state_e of C-API is in an invalid state. result preState:%d, is different from the desired state:%d", postState,
1152                                                    destState);
1153                                 SysLog(NID_MEDIA, "[E_SUCCESS] Found. result preState:%d, param destState:%d", postState,
1154                                                         destState);
1155                         }
1156                         break;
1157                 }
1158
1159         }
1160         SysTryCatch(NID_MEDIA, i != tableTotalCount, r = E_INVALID_STATE, E_INVALID_STATE, "[E_INVALID_STATE] Not found. ");
1161         return r;
1162
1163 CATCH:
1164         SysLog(NID_MEDIA, "[%s] Real preState:%d, param destState:%d", GetErrorMessage(r), preState, destState);
1165         return r;
1166 }
1167
1168 result
1169 _CameraCoordinator::SetCameraSourceFormat(camera_pixel_format_e pixelFormat)
1170 {
1171         result r = E_SUCCESS;
1172         int err = MM_SUCCESS;
1173         SysLog(NID_MEDIA, "Camera_pixel_format_e is :%d", pixelFormat);
1174
1175         err = camera_set_preview_format(__cameraHandle, pixelFormat);
1176         r = ConvertResult(err);
1177         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating, pixelFormat:%d", GetErrorMessage(r), pixelFormat);
1178
1179         return r;
1180
1181 CATCH:
1182         return r;
1183 }
1184
1185 camera_pixel_format_e
1186 _CameraCoordinator::GetCameraSourceFormat(void) const
1187 {
1188         result r = E_SUCCESS;
1189         int err = MM_SUCCESS;
1190         camera_pixel_format_e pixelFormat = CAMERA_PIXEL_FORMAT_INVALID;
1191
1192         err = camera_get_preview_format(__cameraHandle, &pixelFormat);
1193         r = ConvertResult(err);
1194         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating", GetErrorMessage(r));
1195
1196         return pixelFormat;
1197
1198 CATCH:
1199         return pixelFormat;
1200 }
1201
1202 result
1203 _CameraCoordinator::StartCapture(camera_capturing_cb capturingCb , camera_capture_completed_cb completedCb , void *pUserData)
1204 {
1205         result r = E_SUCCESS;
1206         int err = MM_SUCCESS;
1207
1208         __mode = _CAMERA_MODE_IMAGE;
1209         err = camera_start_capture(__cameraHandle, capturingCb, completedCb, pUserData);
1210         r = ConvertResult(err);
1211         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Camera capture failed.", GetErrorMessage(r));
1212
1213         return r;
1214 }
1215
1216 result
1217 _CameraCoordinator::StartRecord(void)
1218 {
1219         result r = E_SUCCESS;
1220         int err = MM_SUCCESS;
1221
1222         __mode = _CAMERA_MODE_VIDEO;
1223         err = recorder_start(__recorderHandle);
1224         r = ConvertResult(err);
1225         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Recorder record failed.", GetErrorMessage(r));
1226
1227         return r;
1228 }
1229
1230 camera_pixel_format_e
1231 _CameraCoordinator::GetReloadPreviewFormat(void) const
1232 {
1233         SysLog(NID_MEDIA, "reloadCameraPreviewFormat is :%d", __reloadCameraPreviewFormat);
1234         return __reloadCameraPreviewFormat;
1235 }
1236 }}