2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FMedia_CameraCoordinator.cpp
20 * @brief This file contains the implementation of the %FMedia_CameraCoordinator class.
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"
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
34 namespace Tizen { namespace Media
36 static const int _ORIENTATION_FOR_RECORDING = 0x1;
37 static const int _ORIENTATION_FOR_CAMERA = 0x1 << 1;
39 typedef int (*_CameraFunc)(_CameraHandle mmHandle);
43 camera_state_e prevState;
44 camera_state_e postState;
46 }_CameraStateTransitTable;
48 static const _CameraStateTransitTable _CAMERA_STATE_TRANSIT[] =
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 },
56 typedef int (*_RecorderFunc)(_RecorderHandle mmHandle);
60 recorder_state_e prevState;
61 recorder_state_e postState;
63 }_RecorderStateTransitTable;
65 static const _RecorderStateTransitTable _RECORDER_STATE_TRANSIT[] =
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 },
77 _CameraCoordinatorSafeHashMapT* _CameraCoordinator::__pMap = null;
79 _CameraCoordinatorSafeHashMapT::_CameraCoordinatorSafeHashMapT(void)
83 _CameraCoordinatorSafeHashMapT::~_CameraCoordinatorSafeHashMapT(void)
86 _CameraCoordinator::__pMap = null;
90 _CameraCoordinatorSafeHashMapT::RemoveItems(void)
92 _CameraCoordinator* pCoordinator = null;
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++)
99 _CameraDeviceType deviceType = _CAMERA_DEVICE_NONE;
100 r = pList->GetAt(i, deviceType);
101 if (IsFailed(r) && deviceType == _CAMERA_DEVICE_NONE)
106 r = GetValue(deviceType, pCoordinator);
107 if (IsFailed(r) && pCoordinator == null)
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)
125 _CameraCoordinator::~_CameraCoordinator()
127 _CameraManager::Release(__cameraDevice);
128 SysLog(NID_MEDIA, "Camera manager released");
132 _CameraCoordinator::Construct(_CameraDeviceType cameraDevice)
134 result r = E_SUCCESS;
135 _CameraManager* pCameraManager = null;
136 SysLog(NID_MEDIA, "Enter. camera device:%d", cameraDevice);
138 pCameraManager = _CameraManager::AddInstance(cameraDevice);
140 SysTryReturn(NID_MEDIA, pCameraManager !=null && r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
142 __cameraHandle = pCameraManager->GetHandle();
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.");
147 __cameraDevice = cameraDevice;
148 __mode = _CAMERA_MODE_IMAGE;
154 _CameraCoordinator::AddRecorder(_RecorderDeviceType recorderDevice)
156 result r = E_SUCCESS;
157 _RecorderManager* pRecorderManager = null;
158 SysLog(NID_MEDIA, "Enter. recorder device:%d", recorderDevice);
160 pRecorderManager = _RecorderManager::AddInstance(recorderDevice);
162 SysTryCatch(NID_MEDIA, pRecorderManager !=null && r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
164 __recorderHandle = pRecorderManager->GetHandle(); // new recorder handle
173 _CameraCoordinator::RemoveRecorder(_RecorderDeviceType recorderDevice)
175 SysLog(NID_MEDIA, "Enter. recorder device:%d", recorderDevice);
176 _RecorderManager::Release(recorderDevice);
177 if (_RecorderManager::HasInstance(recorderDevice) == null)
179 __recorderHandle = MM_INVALID_HANDLE;
184 _CameraCoordinator::InitSingleton()
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.");
190 r = pMap->Construct();
191 SysTryReturnVoidResult(NID_MEDIA, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
193 __pMap = pMap.release();
194 std::atexit(DestroySingleton);
198 _CameraCoordinator::DestroySingleton(void)
204 _CameraCoordinator::AddInstance(_CameraDeviceType cameraDevice)
206 result r = E_SUCCESS;
210 _CameraCoordinator* pCoordinator = null;
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);
215 static pthread_once_t once_block = PTHREAD_ONCE_INIT;
218 pthread_once(&once_block, InitSingleton);
220 SysTryReturn(NID_MEDIA, __pMap != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
223 r = __pMap->ContainsKey(cameraDevice, out);
224 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
228 r = __pMap->GetValue(cameraDevice, pCoordinator);
229 SysTryReturn(NID_MEDIA, r == E_SUCCESS && pCoordinator != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
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.");
236 r = pCoord->Construct(cameraDevice);
237 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
239 r = __pMap->Add(cameraDevice, pCoord.get());
240 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
242 pCoordinator = pCoord.release();
245 pCoordinator->AddRefCount();
250 _CameraCoordinator::AddInstance(_RecorderDeviceType recorderDevice)
252 _CameraCoordinator* pCoordinator = null;
253 _CameraDeviceType cameraDevice = _CAMERA_DEVICE_NONE;
256 switch (recorderDevice)
258 case _RECORDER_DEVICE_VIDEO_PRIMARY_CAMERA:
259 cameraDevice = _CAMERA_DEVICE_PRIMARY;
262 case _RECORDER_DEVICE_VIDEO_SECONDARY_CAMERA:
263 cameraDevice = _CAMERA_DEVICE_SECONDARY;
270 if (cameraDevice == _CAMERA_DEVICE_NONE)
272 SysLogException(NID_MEDIA, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument(recorderDevice) is used. recorderDevice :%d is wrong.", recorderDevice);
276 pCoordinator = AddInstance(cameraDevice);
277 if (pCoordinator == null)
279 SysLogException(NID_MEDIA, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The camera device(%d)'s coordinator is not available.", cameraDevice);
286 _CameraCoordinator::Release(_CameraDeviceType cameraDevice)
288 result r = E_SUCCESS;
292 _CameraCoordinator* pCoordinator = null;
293 r = __pMap->GetValue(cameraDevice, pCoordinator);
294 SysTryReturnVoidResult(NID_MEDIA, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
296 if (pCoordinator->ReleaseRefCount() == 0)
299 r = __pMap->Remove(cameraDevice);
300 SysTryReturnVoidResult(NID_MEDIA, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
306 _CameraCoordinator::Release(_RecorderDeviceType recorderDevice)
310 _CameraDeviceType cameraDevice = _CAMERA_DEVICE_NONE;
312 switch (recorderDevice)
314 case _RECORDER_DEVICE_VIDEO_PRIMARY_CAMERA:
315 cameraDevice = _CAMERA_DEVICE_PRIMARY;
317 case _RECORDER_DEVICE_VIDEO_SECONDARY_CAMERA:
318 cameraDevice = _CAMERA_DEVICE_SECONDARY;
324 Release(cameraDevice);
329 _CameraCoordinator::HasInstance(_CameraDeviceType cameraDevice)
331 result r = E_SUCCESS;
332 _CameraCoordinator* pCoordinator = null;
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.");
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);
344 r = __pMap->GetValue(cameraDevice, pCoordinator);
345 SysTryCatch(NID_MEDIA, r == E_SUCCESS && pCoordinator != null, , r, "[%s] Propagating.", GetErrorMessage(r));
354 _CameraCoordinator::HasInstance(_RecorderDeviceType recorderDevice)
356 _CameraCoordinator* pCoordinator = null;
357 _CameraDeviceType cameraDevice = _CAMERA_DEVICE_NONE;
360 switch (recorderDevice)
362 case _RECORDER_DEVICE_VIDEO_PRIMARY_CAMERA:
363 cameraDevice = _CAMERA_DEVICE_PRIMARY;
366 case _RECORDER_DEVICE_VIDEO_SECONDARY_CAMERA:
367 cameraDevice = _CAMERA_DEVICE_SECONDARY;
374 if (cameraDevice == _CAMERA_DEVICE_NONE)
376 SysLogException(NID_MEDIA, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument(recorderDevice) is used. recorderDevice :%d is wrong.", recorderDevice);
380 pCoordinator = HasInstance(cameraDevice);
381 if (pCoordinator == null)
383 SysLogException(NID_MEDIA, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The camera device(%d)'s coordinator is not available.", cameraDevice);
390 _CameraCoordinator::AddCameraCoordinatorListener(_ICameraCoordinatorListener& listener)
392 result r = E_SUCCESS;
393 r = __pListenerList->Add(&listener);
394 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
399 _CameraCoordinator::RemoveCameraCoordinatorListener(_ICameraCoordinatorListener& listener)
401 result r = E_SUCCESS;
402 r = __pListenerList->Remove(&listener);
403 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
405 r = NotifyOtherRemoved();
406 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
411 _CameraCoordinator::GetMode(void) const
417 _CameraCoordinator::GetCameraHandle(void) const
419 return __cameraHandle;
423 _CameraCoordinator::GetRecorderHandle(void) const
425 return __recorderHandle;
429 _CameraCoordinator::NotifyModeChangePrepared(_CameraMode mode)
431 result r = E_SUCCESS;
432 _ICameraCoordinatorListener* pListener = null;
433 SysLog(NID_MEDIA, "mode:%d", mode);
435 std::unique_ptr <IEnumeratorT <_ICameraCoordinatorListener*> > pEnum (__pListenerList->GetEnumeratorN());
436 SysTryReturn(NID_MEDIA, pEnum.get() != null, GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
438 while (pEnum->MoveNext() == E_SUCCESS)
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");
444 r = pListener->OnCameraCoordinatorModeChangePrepared(mode);
445 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
451 _CameraCoordinator::NotifyModeChanged(_CameraMode mode)
453 result r = E_SUCCESS;
454 _ICameraCoordinatorListener* pListener = null;
455 SysLog(NID_MEDIA, "mode:%d", mode);
457 std::unique_ptr <IEnumeratorT <_ICameraCoordinatorListener*> > pEnum (__pListenerList->GetEnumeratorN());
458 SysTryReturn(NID_MEDIA, pEnum.get() != null, GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
460 while (pEnum->MoveNext() == E_SUCCESS)
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");
466 r = pListener->OnCameraCoordinatorModeChanged(mode);
467 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
473 _CameraCoordinator::NotifyOtherRemoved(void)
475 result r = E_SUCCESS;
476 _ICameraCoordinatorListener* pListener = null;
477 SysLog(NID_MEDIA, "Enter");
479 std::unique_ptr <IEnumeratorT <_ICameraCoordinatorListener*> > pEnum (__pListenerList->GetEnumeratorN());
480 SysTryReturn(NID_MEDIA, pEnum.get() != null, GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
482 while (pEnum->MoveNext() == E_SUCCESS)
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");
488 r = pListener->OnCameraCoordinatorOtherRemoved();
489 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
495 _CameraCoordinator::StartMmPreview(void)
497 result r = E_SUCCESS;
498 SysLog(NID_MEDIA, "Enter. mode:%d", __mode);
502 case _CAMERA_MODE_IMAGE:
503 r = ChangeCameraStateTo(::CAMERA_STATE_PREVIEW);
504 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
507 case _CAMERA_MODE_VIDEO:
508 r = ChangeRecorderStateTo(::RECORDER_STATE_READY);
509 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
513 SysLogException(NID_MEDIA, E_INVALID_STATE, "[E_INVALID_STATE] Camera is in an invalid state. Camcorder mode :%d is wrong", __mode);
523 _CameraCoordinator::StopMmPreview(void)
525 result r = E_SUCCESS;
526 SysLog(NID_MEDIA, "Enter.");
528 if (__mode == _CAMERA_MODE_IMAGE)
530 r = ChangeCameraStateTo(::CAMERA_STATE_CREATED);
531 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
533 else if (__mode == _CAMERA_MODE_VIDEO)
535 recorder_state_e recorderState = ::RECORDER_STATE_NONE;
536 recorderState = GetRecorderState();
538 switch (recorderState)
540 case ::RECORDER_STATE_RECORDING:
541 r = ChangeRecorderStateTo(::RECORDER_STATE_READY);
542 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
544 r = ChangeRecorderStateTo(::RECORDER_STATE_CREATED);
545 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
548 case ::RECORDER_STATE_READY:
549 r = ChangeRecorderStateTo(::RECORDER_STATE_CREATED);
550 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
555 SysLogException(NID_MEDIA, E_INVALID_STATE, "[E_INVALID_STATE] recorderState is in an invalid state. recorderState:%d", recorderState);
562 SysLogException(NID_MEDIA, E_INVALID_STATE, "[E_INVALID_STATE] CamcorderMode is in an invalid state. mode :%d", __mode);
570 _CameraCoordinator::CreateVideoRecorder(void)
572 result r = E_SUCCESS;
573 _RecorderDeviceType recorderDevice = _RECORDER_DEVICE_NONE;
574 SysLog(NID_MEDIA, "enter.");
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;
589 _CameraCoordinator::DestroyVideoRecorder(void)
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.");
597 if ( __recorderHandle != MM_INVALID_HANDLE )
599 recorderState = GetRecorderState();
601 switch (recorderState)
603 case ::RECORDER_STATE_NONE:
606 case ::RECORDER_STATE_CREATED:
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;
615 case ::RECORDER_STATE_RECORDING:
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;
639 _CameraCoordinator::ChangeFormat(_CameraMode mode, bool callback, camera_pixel_format_e reloadCameraPreviewFormat)
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);
648 if (mode == _CAMERA_MODE_VIDEO)
650 cameraState = GetCameraState();
654 case ::CAMERA_STATE_CREATED:
656 case ::CAMERA_STATE_NONE:
659 case ::CAMERA_STATE_PREVIEW:
661 case ::CAMERA_STATE_CAPTURED:
662 r = ChangeCameraStateTo(::CAMERA_STATE_CREATED);
663 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
666 case ::CAMERA_STATE_CAPTURING:
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);
676 __mode = _CAMERA_MODE_VIDEO;
677 __reloadCameraPreviewFormat = reloadCameraPreviewFormat;
680 r = NotifyModeChangePrepared(mode);
681 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
686 case ::CAMERA_STATE_NONE:
688 case ::CAMERA_STATE_CREATED:
691 case ::CAMERA_STATE_PREVIEW:
692 r = ChangeRecorderStateTo(::RECORDER_STATE_READY);
693 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
696 case ::CAMERA_STATE_CAPTURED:
701 else if ( mode == _CAMERA_MODE_IMAGE )
703 recorderState = GetRecorderState();
705 switch (recorderState)
707 case ::RECORDER_STATE_NONE:
709 case ::RECORDER_STATE_CREATED:
710 __mode = _CAMERA_MODE_IMAGE;
711 __reloadCameraPreviewFormat = reloadCameraPreviewFormat;
714 r = NotifyModeChangePrepared(mode);
715 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
719 case ::RECORDER_STATE_READY:
720 r = ChangeRecorderStateTo(::RECORDER_STATE_CREATED);
721 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
723 __mode = _CAMERA_MODE_IMAGE;
724 __reloadCameraPreviewFormat = reloadCameraPreviewFormat;
727 r = NotifyModeChangePrepared(mode);
728 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
730 r = ChangeCameraStateTo(::CAMERA_STATE_PREVIEW);
731 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
734 case ::RECORDER_STATE_RECORDING:
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));
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;
746 __mode = _CAMERA_MODE_IMAGE;
747 __reloadCameraPreviewFormat = reloadCameraPreviewFormat;
750 r = NotifyModeChangePrepared(mode);
751 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
753 r = ChangeCameraStateTo(::CAMERA_STATE_PREVIEW);
754 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
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));
774 _CameraCoordinator::ChangeMode(_CameraMode mode, bool callback)
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;
782 SysLog(NID_MEDIA, "Mode should be changed. mode:%d", mode);
783 if (mode == _CAMERA_MODE_VIDEO)
785 cameraState = GetCameraState();
789 case ::CAMERA_STATE_CREATED:
791 case ::CAMERA_STATE_NONE:
794 case ::CAMERA_STATE_PREVIEW:
796 case ::CAMERA_STATE_CAPTURED:
797 r = ChangeCameraStateTo(::CAMERA_STATE_CREATED);
798 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
801 case ::CAMERA_STATE_CAPTURING:
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);
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;
820 r = NotifyModeChangePrepared(mode);
821 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
826 case ::CAMERA_STATE_NONE:
828 case ::CAMERA_STATE_CREATED:
831 case ::CAMERA_STATE_PREVIEW:
832 r = ChangeRecorderStateTo(::RECORDER_STATE_READY);
833 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
836 case ::CAMERA_STATE_CAPTURED:
841 else if ( mode == _CAMERA_MODE_IMAGE )
843 recorderState = GetRecorderState();
846 switch (recorderState)
848 case ::RECORDER_STATE_NONE:
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;
858 case ::RECORDER_STATE_READY:
859 r = ChangeRecorderStateTo(::RECORDER_STATE_CREATED);
860 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
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;
868 r = NotifyModeChangePrepared(mode);
869 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
871 r = ChangeCameraStateTo(::CAMERA_STATE_PREVIEW);
872 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
875 case ::RECORDER_STATE_RECORDING:
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));
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;
887 RemoveRecorder(recorderDevice);
888 __mode = _CAMERA_MODE_IMAGE;
891 r = NotifyModeChangePrepared(mode);
892 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
894 r = ChangeCameraStateTo(::CAMERA_STATE_PREVIEW);
895 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
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));
917 _CameraCoordinator::IsCalmChangeMode(_CameraMode mode)
919 camera_state_e cameraState = ::CAMERA_STATE_NONE;
920 recorder_state_e recorderState = ::RECORDER_STATE_NONE;
921 bool calmChange = false;
923 if (mode == _CAMERA_MODE_VIDEO)
925 cameraState = GetCameraState();
929 case ::CAMERA_STATE_CREATED:
931 case ::CAMERA_STATE_NONE:
935 case ::CAMERA_STATE_PREVIEW:
937 case ::CAMERA_STATE_CAPTURED:
939 case ::CAMERA_STATE_CAPTURING:
947 else if ( mode == _CAMERA_MODE_IMAGE )
949 recorderState = GetRecorderState();
952 switch (recorderState)
954 case ::RECORDER_STATE_NONE:
956 case ::RECORDER_STATE_CREATED:
960 case ::RECORDER_STATE_READY:
962 case ::RECORDER_STATE_RECORDING:
964 case ::RECORDER_STATE_PAUSED:
973 SysLog(NID_MEDIA, "mode:%d, calm:%d", mode, calmChange);
979 _CameraCoordinator::GetCameraDevice(void) const
981 return __cameraDevice;
985 _CameraCoordinator::GetCameraState(void) const
987 result r = E_SUCCESS;
988 int err = MM_SUCCESS;
989 camera_state_e state = ::CAMERA_STATE_NONE;
991 err = camera_get_state(__cameraHandle, &state);
992 r = ConvertResult(err);
993 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
998 return ::CAMERA_STATE_NONE;
1002 _CameraCoordinator::GetRecorderState(void) const
1004 result r = E_SUCCESS;
1005 int err = MM_SUCCESS;
1006 recorder_state_e state = ::RECORDER_STATE_NONE;
1008 err = recorder_get_state(__recorderHandle, &state);
1009 r = ConvertResult(err);
1010 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
1015 return ::RECORDER_STATE_NONE;
1019 _CameraCoordinator::ConvertResult(int err) const
1021 result r = E_SYSTEM;
1022 if (err != CAMERA_ERROR_NONE)
1024 SysLog(NID_MEDIA, "MM Err:0x%x", err);
1028 if (err == CAMERA_ERROR_NONE || err == RECORDER_ERROR_NONE)
1032 else if (err == CAMERA_ERROR_INVALID_PARAMETER || err == RECORDER_ERROR_INVALID_PARAMETER)
1036 else if (err == CAMERA_ERROR_INVALID_STATE || err == RECORDER_ERROR_INVALID_STATE)
1038 r = E_INVALID_STATE;
1040 else if (err == CAMERA_ERROR_OUT_OF_MEMORY || err == RECORDER_ERROR_OUT_OF_MEMORY)
1042 r = E_OUT_OF_MEMORY;
1044 else if (err == CAMERA_ERROR_DEVICE || err == RECORDER_ERROR_DEVICE)
1046 r = E_DEVICE_FAILED;
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)
1055 else if (err == CAMERA_ERROR_DEVICE_BUSY
1056 || err == CAMERA_ERROR_SOUND_POLICY
1057 || err == RECORDER_ERROR_SOUND_POLICY)
1061 else if (err == CAMERA_ERROR_DEVICE_NOT_FOUND)
1063 r = E_DEVICE_UNAVAILABLE;
1075 _CameraCoordinator::ChangeCameraStateTo(camera_state_e destState)
1077 result r = E_SUCCESS;
1078 int tableTotalCount = 0;
1080 int err = MM_SUCCESS;
1081 camera_state_e preState = ::CAMERA_STATE_NONE;
1082 camera_state_e postState = ::CAMERA_STATE_NONE;
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);
1088 for (i = 0; i < tableTotalCount; i++)
1090 if (preState == _CAMERA_STATE_TRANSIT[i].prevState && destState == _CAMERA_STATE_TRANSIT[i].postState)
1092 if (_CAMERA_STATE_TRANSIT[i].pFunc != null)
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);
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,
1106 SysLog(NID_MEDIA, "[E_SUCCESS] Found. result postState:%d, param destState:%d", postState,
1113 SysTryCatch(NID_MEDIA, i != tableTotalCount, r = E_INVALID_STATE, E_INVALID_STATE, "[E_INVALID_STATE] Not found. ");
1117 SysLog(NID_MEDIA, "[%s] Real preState:%d, param destState:%d", GetErrorMessage(r), preState, destState);
1122 _CameraCoordinator::ChangeRecorderStateTo(recorder_state_e destState)
1124 result r = E_SUCCESS;
1125 int tableTotalCount = 0;
1127 int err = MM_SUCCESS;
1128 recorder_state_e preState = ::RECORDER_STATE_NONE;
1129 recorder_state_e postState = ::RECORDER_STATE_NONE;
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);
1135 for (i = 0; i < tableTotalCount; i++)
1137 if (preState == _RECORDER_STATE_TRANSIT[i].prevState && destState == _RECORDER_STATE_TRANSIT[i].postState)
1139 if (_RECORDER_STATE_TRANSIT[i].pFunc != null)
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);
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,
1153 SysLog(NID_MEDIA, "[E_SUCCESS] Found. result preState:%d, param destState:%d", postState,
1160 SysTryCatch(NID_MEDIA, i != tableTotalCount, r = E_INVALID_STATE, E_INVALID_STATE, "[E_INVALID_STATE] Not found. ");
1164 SysLog(NID_MEDIA, "[%s] Real preState:%d, param destState:%d", GetErrorMessage(r), preState, destState);
1169 _CameraCoordinator::SetCameraSourceFormat(camera_pixel_format_e pixelFormat)
1171 result r = E_SUCCESS;
1172 int err = MM_SUCCESS;
1173 SysLog(NID_MEDIA, "Camera_pixel_format_e is :%d", pixelFormat);
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);
1185 camera_pixel_format_e
1186 _CameraCoordinator::GetCameraSourceFormat(void) const
1188 result r = E_SUCCESS;
1189 int err = MM_SUCCESS;
1190 camera_pixel_format_e pixelFormat = CAMERA_PIXEL_FORMAT_INVALID;
1192 err = camera_get_preview_format(__cameraHandle, &pixelFormat);
1193 r = ConvertResult(err);
1194 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating", GetErrorMessage(r));
1203 _CameraCoordinator::StartCapture(camera_capturing_cb capturingCb , camera_capture_completed_cb completedCb , void *pUserData)
1205 result r = E_SUCCESS;
1206 int err = MM_SUCCESS;
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));
1217 _CameraCoordinator::StartRecord(void)
1219 result r = E_SUCCESS;
1220 int err = MM_SUCCESS;
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));
1230 camera_pixel_format_e
1231 _CameraCoordinator::GetReloadPreviewFormat(void) const
1233 SysLog(NID_MEDIA, "reloadCameraPreviewFormat is :%d", __reloadCameraPreviewFormat);
1234 return __reloadCameraPreviewFormat;