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_CameraEvent.cpp
20 * @brief This file contains the event processing for Camera
24 #include <FBaseSysLog.h>
25 #include <FMediaICameraEventListener.h>
26 #include "FMedia_CameraImpl.h"
27 #include "FMedia_CameraEvent.h"
28 #include "FMedia_CameraCoordinator.h"
29 #include "FMedia_CameraUtil.h"
30 #include "FMedia_ImageUtil.h"
31 #include "FMedia_ColorConverter.h"
33 using namespace Tizen::Base;
34 using namespace Tizen::Graphics;
36 namespace Tizen { namespace Media
39 _CameraEvent::_CameraEvent(void)
45 _CameraEvent::~_CameraEvent(void)
51 _CameraEvent::Construct(_CameraImpl& cameraImpl)
55 __pCameraImpl = &cameraImpl;
56 r = _Event::Initialize();
61 _CameraEvent::SendEvent(_CameraEventType event, CameraErrorReason err, result res, ByteBuffer* pByteBuffer)
64 std::unique_ptr <_CameraEventArg> pCameraEventArg (new (std::nothrow) _CameraEventArg());
65 SysTryReturn(NID_MEDIA, pCameraEventArg.get() != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
67 pCameraEventArg->SetEventType(event);
68 pCameraEventArg->SetResult(res);
69 pCameraEventArg->SetError(err);
70 pCameraEventArg->SetData(pByteBuffer);
71 r = FireAsync(*(pCameraEventArg.get()));
72 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
74 // In case of success, event argument should be released for Base::Event to use the argument continuously.
75 pCameraEventArg.release();
80 _CameraEvent::FireImpl(Tizen::Base::Runtime::IEventListener& listener, const Tizen::Base::Runtime::IEventArg& arg)
82 ICameraEventListener* pCameraEventListener = dynamic_cast <ICameraEventListener*>(&listener);
83 SysTryReturnVoidResult(NID_MEDIA, pCameraEventListener != null, E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Listener is null.");
85 Tizen::Base::Runtime::IEventArg* pTempArg = const_cast <Tizen::Base::Runtime::IEventArg*>(&arg);
86 _CameraEventArg* pArg = dynamic_cast <_CameraEventArg*>(pTempArg);
87 SysTryReturnVoidResult(NID_MEDIA, pArg != null, E_SYSTEM, "[E_SYSTEM] A system error has been occurred. EventArg is null.");
90 switch (pArg->GetEventType())
92 case _CAMERA_EVENT_ERROR:
93 __pCameraImpl->SetState(CAMERA_STATE_ERROR);
94 pCameraEventListener->OnCameraErrorOccurred(pArg->GetError());
97 case _CAMERA_EVENT_AUTO_FOCUSED:
98 if (__pCameraImpl->GetState() == CAMERA_STATE_AUTO_FOCUSING)
100 __pCameraImpl->SetState(CAMERA_STATE_PREVIEW);
102 pCameraEventListener->OnCameraAutoFocused(pArg->GetResult() == E_SUCCESS ? true : false);
105 case _CAMERA_EVENT_CAPTURED:
107 if (__pCameraImpl->GetState() == CAMERA_STATE_CAPTURING)
109 if (__pCameraImpl->IsZeroShutterLag())
111 r = __pCameraImpl->GetCoordinator()->StartMmPreview();
114 SysLogException(NID_MEDIA, r, "[%s] Start Preview is failed.", GetErrorMessage(r));
117 __pCameraImpl->SetState(CAMERA_STATE_PREVIEW);
121 __pCameraImpl->SetState(CAMERA_STATE_CAPTURED);
124 if (pArg->GetData() != null)
126 pCameraEventListener->OnCameraCaptured(*(pArg->GetData()), pArg->GetResult());
130 ByteBuffer emptyBuffer;
131 pCameraEventListener->OnCameraCaptured(emptyBuffer, pArg->GetResult());
136 __pCameraImpl->SetState(CAMERA_STATE_ERROR);
137 pCameraEventListener->OnCameraErrorOccurred(CAMERA_ERROR_DEVICE_FAILED);
140 case _CAMERA_EVENT_PREVIEWED_DATA:
142 std::unique_ptr <_CameraBuffer> pBuffer (__pCameraImpl->DequeueDataN(_CAMERA_BUFFER_PREVIEW));
143 SysTryReturnVoidResult(NID_MEDIA, pBuffer.get() != null, E_INVALID_DATA, "[E_INVALID_DATA] Invalid preview data.");
145 MediaPixelFormat mediaSrcFormat = pBuffer->GetPixelFormat();
147 // Get the preview format which is from the application setting.
148 MediaPixelFormat mediaUserFormat = MEDIA_PIXEL_FORMAT_NONE;
150 _CameraUtil::GetOspMediaPixelFormat(__pCameraImpl->GetPreviewFormat(), mediaUserFormat, ratio);
152 // If the conversion is needed
153 if (mediaSrcFormat != mediaUserFormat)
155 result r = E_SUCCESS;
157 const byte * pSrcBuf = pBuffer->GetPointer();
158 _CameraBuffer dstCameraBuffer;
159 int convertedBufSize = 0;
160 byte * pConvertedBuf = null;
161 int width = pBuffer->GetWidth();
162 int height = pBuffer->GetHeight();
164 SysLog(NID_MEDIA, "Preview callback. MM format should be converted from %d to %d", mediaSrcFormat, mediaUserFormat);
165 r = cvt.Construct(mediaSrcFormat, width, height, mediaUserFormat, width, height);
166 SysTryReturnVoidResult(NID_MEDIA, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
168 convertedBufSize = _ImageUtil::GetBufferSize(mediaUserFormat, width, height);
169 SysTryReturnVoidResult(NID_MEDIA, convertedBufSize > 0, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
171 r = dstCameraBuffer.Construct(convertedBufSize, _CAMERA_BUFFER_PREVIEW, mediaUserFormat, width, height);
172 SysTryReturnVoidResult(NID_MEDIA, r == E_SUCCESS, r, "[%s] Propagating. size:%d", GetErrorMessage(r), convertedBufSize);
174 pConvertedBuf = const_cast<byte*>(dstCameraBuffer.GetPointer());
175 SysTryReturnVoidResult(NID_MEDIA, pConvertedBuf != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] byte instance is not available. dstCameraBuffer.GetPointer() is null.");
177 r = cvt.Convert(pSrcBuf, pBuffer->GetCapacity(), pConvertedBuf, convertedBufSize);
178 SysTryReturnVoidResult(NID_MEDIA, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
180 pCameraEventListener->OnCameraPreviewed(dstCameraBuffer, pArg->GetResult());
182 else //If the conversion is not needed.
184 SysLog(NID_MEDIA, "Preview callback. Format is %d", mediaSrcFormat);
185 pCameraEventListener->OnCameraPreviewed(*pBuffer.get(), pArg->GetResult());