CAF state bug fix
[platform/framework/native/media.git] / src / FMedia_CameraEvent.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_CameraEvent.cpp
20  * @brief                       This file contains the event processing for Camera
21  *
22  */
23
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"
32
33 using namespace Tizen::Base;
34 using namespace Tizen::Graphics;
35
36 namespace Tizen { namespace Media
37 {
38
39 _CameraEvent::_CameraEvent(void)
40         : __pCameraImpl(null)
41 {
42
43 }
44
45 _CameraEvent::~_CameraEvent(void)
46 {
47
48 }
49
50 result
51 _CameraEvent::Construct(_CameraImpl& cameraImpl)
52 {
53         result r = E_SUCCESS;
54
55         __pCameraImpl = &cameraImpl;
56         r = _Event::Initialize();
57         return r;
58 }
59
60 result
61 _CameraEvent::SendEvent(_CameraEventType event, CameraErrorReason err, result res, ByteBuffer* pByteBuffer)
62 {
63         result r = E_SUCCESS;
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.");
66
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));
73
74         // In case of success, event argument should be released for Base::Event to use the argument continuously.
75         pCameraEventArg.release();
76         return r;
77 }
78
79 void
80 _CameraEvent::FireImpl(Tizen::Base::Runtime::IEventListener& listener, const Tizen::Base::Runtime::IEventArg& arg)
81 {
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.");
84
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.");
88
89         result r = E_SUCCESS;
90         switch (pArg->GetEventType())
91         {
92         case _CAMERA_EVENT_ERROR:
93                 __pCameraImpl->SetState(CAMERA_STATE_ERROR);
94                 pCameraEventListener->OnCameraErrorOccurred(pArg->GetError());
95                 break;
96
97         case _CAMERA_EVENT_AUTO_FOCUSED:
98                 if (__pCameraImpl->GetState() == CAMERA_STATE_AUTO_FOCUSING)
99                 {
100                         __pCameraImpl->SetState(CAMERA_STATE_PREVIEW);
101                 }
102                 pCameraEventListener->OnCameraAutoFocused(pArg->GetResult() == E_SUCCESS ? true : false);
103                 break;
104
105         case _CAMERA_EVENT_CAPTURED:
106         {
107                 if (__pCameraImpl->GetState() == CAMERA_STATE_CAPTURING)
108                 {
109                         if (__pCameraImpl->IsZeroShutterLag())
110                         {
111                                 r = __pCameraImpl->GetCoordinator()->StartMmPreview();
112                                 if (r != E_SUCCESS)
113                                 {
114                                         SysLogException(NID_MEDIA, r, "[%s] Start Preview is failed.", GetErrorMessage(r));
115                                         goto CATCH_CAPTURE;
116                                 }
117                                 __pCameraImpl->SetState(CAMERA_STATE_PREVIEW);
118                         }
119                         else
120                         {
121                                 __pCameraImpl->SetState(CAMERA_STATE_CAPTURED);
122                         }
123                 }
124                 if (pArg->GetData() != null)
125                 {
126                         pCameraEventListener->OnCameraCaptured(*(pArg->GetData()), pArg->GetResult());
127                 }
128                 else
129                 {
130                         ByteBuffer emptyBuffer;
131                         pCameraEventListener->OnCameraCaptured(emptyBuffer, pArg->GetResult());
132                 }
133         }
134                 break;
135         CATCH_CAPTURE:
136                 __pCameraImpl->SetState(CAMERA_STATE_ERROR);
137                 pCameraEventListener->OnCameraErrorOccurred(CAMERA_ERROR_DEVICE_FAILED);
138                 break;
139
140         case _CAMERA_EVENT_PREVIEWED_DATA:
141         {
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.");
144
145                 MediaPixelFormat mediaSrcFormat = pBuffer->GetPixelFormat();
146
147                 // Get the preview format which is from the application setting.
148                 MediaPixelFormat mediaUserFormat = MEDIA_PIXEL_FORMAT_NONE;
149                 double ratio = 0.0;
150                 _CameraUtil::GetOspMediaPixelFormat(__pCameraImpl->GetPreviewFormat(), mediaUserFormat, ratio);
151
152                 // If the conversion is needed
153                 if (mediaSrcFormat != mediaUserFormat)
154                 {
155                         result r = E_SUCCESS;
156                         _ColorConverter cvt;
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();
163
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));
167
168                         convertedBufSize = _ImageUtil::GetBufferSize(mediaUserFormat, width, height);
169                         SysTryReturnVoidResult(NID_MEDIA, convertedBufSize > 0, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
170
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);
173
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.");
176
177                         r = cvt.Convert(pSrcBuf, pBuffer->GetCapacity(), pConvertedBuf, convertedBufSize);
178                         SysTryReturnVoidResult(NID_MEDIA, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
179
180                         pCameraEventListener->OnCameraPreviewed(dstCameraBuffer, pArg->GetResult());
181                 }
182                 else            //If the conversion is not needed.
183                 {
184                         SysLog(NID_MEDIA, "Preview callback. Format is %d", mediaSrcFormat);
185                         pCameraEventListener->OnCameraPreviewed(*pBuffer.get(), pArg->GetResult());
186                 }
187         }
188         break;
189
190         default:
191                 break;
192
193         }
194 }
195
196 }}
197