1 /*
2 ** Copyright (c) 2011-2012 The Linux Foundation. All rights reserved.
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16
17 /*#error uncomment this for compiler test!*/
18
19 #define ALOG_NDEBUG 0
20 #define ALOG_NIDEBUG 0
21
22 #define LOG_TAG __FILE__
23 #include <utils/Log.h>
24 #include <utils/threads.h>
25
26
27 #include "QCameraStream.h"
28
29 /* QCameraStream class implementation goes here*/
30 /* following code implement the control logic of this class*/
31
32 namespace android {
33
StreamQueue()34 StreamQueue::StreamQueue(){
35 mInitialized = false;
36 }
37
~StreamQueue()38 StreamQueue::~StreamQueue(){
39 flush();
40 }
41
init()42 void StreamQueue::init(){
43 Mutex::Autolock l(&mQueueLock);
44 mInitialized = true;
45 mQueueWait.signal();
46 }
47
deinit()48 void StreamQueue::deinit(){
49 Mutex::Autolock l(&mQueueLock);
50 mInitialized = false;
51 mQueueWait.signal();
52 }
53
isInitialized()54 bool StreamQueue::isInitialized(){
55 Mutex::Autolock l(&mQueueLock);
56 return mInitialized;
57 }
58
enqueue(void * element)59 bool StreamQueue::enqueue(
60 void * element){
61 Mutex::Autolock l(&mQueueLock);
62 if(mInitialized == false)
63 return false;
64
65 mContainer.add(element);
66 mQueueWait.signal();
67 return true;
68 }
69
isEmpty()70 bool StreamQueue::isEmpty(){
71 return (mInitialized && mContainer.isEmpty());
72 }
dequeue()73 void* StreamQueue::dequeue(){
74
75 void *frame;
76 mQueueLock.lock();
77 while(mInitialized && mContainer.isEmpty()){
78 mQueueWait.wait(mQueueLock);
79 }
80
81 if(!mInitialized){
82 mQueueLock.unlock();
83 return NULL;
84 }
85
86 frame = mContainer.itemAt(0);
87 mContainer.removeAt(0);
88 mQueueLock.unlock();
89 return frame;
90 }
91
flush()92 void StreamQueue::flush(){
93 Mutex::Autolock l(&mQueueLock);
94 mContainer.clear();
95 }
96
97
98 // ---------------------------------------------------------------------------
99 // QCameraStream
100 // ---------------------------------------------------------------------------
101
102 /* initialize a streaming channel*/
initChannel(int cameraId,uint32_t ch_type_mask)103 status_t QCameraStream::initChannel(int cameraId,
104 uint32_t ch_type_mask)
105 {
106 #if 0
107 int rc = MM_CAMERA_OK;
108 int i;
109 status_t ret = NO_ERROR;
110 int width = 0; /* width of channel */
111 int height = 0; /* height of channel */
112 cam_ctrl_dimension_t dim;
113 mm_camera_ch_image_fmt_parm_t fmt;
114
115 memset(&dim, 0, sizeof(cam_ctrl_dimension_t));
116 rc = cam_config_get_parm(cameraId, MM_CAMERA_PARM_DIMENSION, &dim);
117 if (MM_CAMERA_OK != rc) {
118 ALOGE("%s: error - can't get camera dimension!", __func__);
119 ALOGE("%s: X", __func__);
120 return BAD_VALUE;
121 }
122
123 if(MM_CAMERA_CH_PREVIEW_MASK & ch_type_mask) {
124 rc = cam_ops_ch_acquire(cameraId, MM_CAMERA_CH_PREVIEW);
125 ALOGV("%s:ch_acquire MM_CAMERA_CH_PREVIEW, rc=%d\n",__func__, rc);
126
127 if(MM_CAMERA_OK != rc) {
128 ALOGE("%s: preview channel acquir error =%d\n", __func__, rc);
129 ALOGE("%s: X", __func__);
130 return BAD_VALUE;
131 }
132 else{
133 memset(&fmt, 0, sizeof(mm_camera_ch_image_fmt_parm_t));
134 fmt.ch_type = MM_CAMERA_CH_PREVIEW;
135 fmt.def.fmt = CAMERA_YUV_420_NV12; //dim.prev_format;
136 fmt.def.dim.width = dim.display_width;
137 fmt.def.dim.height = dim.display_height;
138 ALOGV("%s: preview channel fmt = %d", __func__,
139 dim.prev_format);
140 ALOGV("%s: preview channel resolution = %d X %d", __func__,
141 dim.display_width, dim.display_height);
142
143 rc = cam_config_set_parm(cameraId, MM_CAMERA_PARM_CH_IMAGE_FMT, &fmt);
144 ALOGV("%s: preview MM_CAMERA_PARM_CH_IMAGE_FMT rc = %d\n", __func__, rc);
145 if(MM_CAMERA_OK != rc) {
146 ALOGE("%s:set preview channel format err=%d\n", __func__, ret);
147 ALOGE("%s: X", __func__);
148 ret = BAD_VALUE;
149 }
150 }
151 }
152
153
154 if(MM_CAMERA_CH_VIDEO_MASK & ch_type_mask)
155 {
156 rc = cam_ops_ch_acquire(cameraId, MM_CAMERA_CH_VIDEO);
157 ALOGV("%s:ch_acquire MM_CAMERA_CH_VIDEO, rc=%d\n",__func__, rc);
158
159 if(MM_CAMERA_OK != rc) {
160 ALOGE("%s: video channel acquir error =%d\n", __func__, rc);
161 ALOGE("%s: X", __func__);
162 ret = BAD_VALUE;
163 }
164 else {
165 memset(&fmt, 0, sizeof(mm_camera_ch_image_fmt_parm_t));
166 fmt.ch_type = MM_CAMERA_CH_VIDEO;
167 fmt.video.video.fmt = CAMERA_YUV_420_NV12; //dim.enc_format;
168 fmt.video.video.dim.width = dim.video_width;
169 fmt.video.video.dim.height = dim.video_height;
170 ALOGV("%s: video channel fmt = %d", __func__,
171 dim.enc_format);
172 ALOGV("%s: video channel resolution = %d X %d", __func__,
173 dim.video_width, dim.video_height);
174
175 rc = cam_config_set_parm(cameraId, MM_CAMERA_PARM_CH_IMAGE_FMT, &fmt);
176
177 ALOGV("%s: video MM_CAMERA_PARM_CH_IMAGE_FMT rc = %d\n", __func__, rc);
178 if(MM_CAMERA_OK != rc) {
179 ALOGE("%s:set video channel format err=%d\n", __func__, rc);
180 ALOGE("%s: X", __func__);
181 ret= BAD_VALUE;
182 }
183 }
184
185 } /*MM_CAMERA_CH_VIDEO*/
186 #endif
187
188 int rc = MM_CAMERA_OK;
189 status_t ret = NO_ERROR;
190 mm_camera_op_mode_type_t op_mode=MM_CAMERA_OP_MODE_VIDEO;
191 int i;
192
193 ALOGV("QCameraStream::initChannel : E");
194 if(MM_CAMERA_CH_PREVIEW_MASK & ch_type_mask){
195 rc = cam_ops_ch_acquire(cameraId, MM_CAMERA_CH_PREVIEW);
196 ALOGV("%s:ch_acquire MM_CAMERA_CH_PREVIEW, rc=%d\n",__func__, rc);
197 if(MM_CAMERA_OK != rc) {
198 ALOGE("%s: preview channel acquir error =%d\n", __func__, rc);
199 ALOGV("%s: X", __func__);
200 return BAD_VALUE;
201 }
202 /*Callback register*/
203 /* register a notify into the mmmm_camera_t object*/
204 /* ret = cam_evt_register_buf_notify(mCameraId, MM_CAMERA_CH_PREVIEW,
205 preview_notify_cb,
206 this);
207 ALOGV("Buf notify MM_CAMERA_CH_PREVIEW, rc=%d\n",rc);*/
208 }else if(MM_CAMERA_CH_VIDEO_MASK & ch_type_mask){
209 rc = cam_ops_ch_acquire(cameraId, MM_CAMERA_CH_VIDEO);
210 ALOGV("%s:ch_acquire MM_CAMERA_CH_VIDEO, rc=%d\n",__func__, rc);
211 if(MM_CAMERA_OK != rc) {
212 ALOGE("%s: preview channel acquir error =%d\n", __func__, rc);
213 ALOGV("%s: X", __func__);
214 return BAD_VALUE;
215 }
216 /*Callback register*/
217 /* register a notify into the mmmm_camera_t object*/
218 /*ret = cam_evt_register_buf_notify(mCameraId, MM_CAMERA_CH_VIDEO,
219 record_notify_cb,
220 this);
221 ALOGV("Buf notify MM_CAMERA_CH_VIDEO, rc=%d\n",rc);*/
222 }
223
224 ret = (MM_CAMERA_OK==rc)? NO_ERROR : BAD_VALUE;
225 ALOGV("%s: X, ret = %d", __func__, ret);
226 return ret;
227 }
228
deinitChannel(int cameraId,mm_camera_channel_type_t ch_type)229 status_t QCameraStream::deinitChannel(int cameraId,
230 mm_camera_channel_type_t ch_type)
231 {
232
233 int rc = MM_CAMERA_OK;
234
235 ALOGV("%s: E, channel = %d\n", __func__, ch_type);
236
237 if (MM_CAMERA_CH_MAX <= ch_type) {
238 ALOGE("%s: X: BAD_VALUE", __func__);
239 return BAD_VALUE;
240 }
241
242 cam_ops_ch_release(cameraId, ch_type);
243
244 ALOGV("%s: X, channel = %d\n", __func__, ch_type);
245 return NO_ERROR;
246 }
247
setMode(int enable)248 status_t QCameraStream::setMode(int enable) {
249 ALOGV("%s :myMode %x ", __func__, myMode);
250 if (enable) {
251 myMode = (camera_mode_t)(myMode | CAMERA_ZSL_MODE);
252 } else {
253 myMode = (camera_mode_t)(myMode & ~CAMERA_ZSL_MODE);
254 }
255 return NO_ERROR;
256 }
257
setFormat(uint8_t ch_type_mask,cam_format_t previewFmt)258 status_t QCameraStream::setFormat(uint8_t ch_type_mask, cam_format_t previewFmt)
259 {
260 int rc = MM_CAMERA_OK;
261 status_t ret = NO_ERROR;
262 int width = 0; /* width of channel */
263 int height = 0; /* height of channel */
264 cam_ctrl_dimension_t dim;
265 mm_camera_ch_image_fmt_parm_t fmt;
266 ALOGV("%s: E",__func__);
267
268 memset(&dim, 0, sizeof(cam_ctrl_dimension_t));
269 rc = cam_config_get_parm(mCameraId, MM_CAMERA_PARM_DIMENSION, &dim);
270 if (MM_CAMERA_OK != rc) {
271 ALOGE("%s: error - can't get camera dimension!", __func__);
272 ALOGV("%s: X", __func__);
273 return BAD_VALUE;
274 }
275 char mDeviceName[PROPERTY_VALUE_MAX];
276 property_get("ro.product.device",mDeviceName," ");
277 memset(&fmt, 0, sizeof(mm_camera_ch_image_fmt_parm_t));
278 if(MM_CAMERA_CH_PREVIEW_MASK & ch_type_mask){
279 fmt.ch_type = MM_CAMERA_CH_PREVIEW;
280 fmt.def.fmt = (cam_format_t)previewFmt;
281 fmt.def.dim.width = dim.display_width;
282 fmt.def.dim.height = dim.display_height;
283 }else if(MM_CAMERA_CH_VIDEO_MASK & ch_type_mask){
284 fmt.ch_type = MM_CAMERA_CH_VIDEO;
285 fmt.video.video.fmt = CAMERA_YUV_420_NV21; //dim.enc_format;
286 fmt.video.video.dim.width = dim.video_width;
287 fmt.video.video.dim.height = dim.video_height;
288 }/*else if(MM_CAMERA_CH_SNAPSHOT_MASK & ch_type_mask){
289 if(mHalCamCtrl->isRawSnapshot()) {
290 fmt.ch_type = MM_CAMERA_CH_RAW;
291 fmt.def.fmt = CAMERA_BAYER_SBGGR10;
292 fmt.def.dim.width = dim.raw_picture_width;
293 fmt.def.dim.height = dim.raw_picture_height;
294 }else{
295 //Jpeg???
296 fmt.ch_type = MM_CAMERA_CH_SNAPSHOT;
297 fmt.snapshot.main.fmt = dim.main_img_format;
298 fmt.snapshot.main.dim.width = dim.picture_width;
299 fmt.snapshot.main.dim.height = dim.picture_height;
300
301 fmt.snapshot.thumbnail.fmt = dim.thumb_format;
302 fmt.snapshot.thumbnail.dim.width = dim.ui_thumbnail_width;
303 fmt.snapshot.thumbnail.dim.height = dim.ui_thumbnail_height;
304 }
305 }*/
306
307 rc = cam_config_set_parm(mCameraId, MM_CAMERA_PARM_CH_IMAGE_FMT, &fmt);
308 ALOGV("%s: Stream MM_CAMERA_PARM_CH_IMAGE_FMT rc = %d\n", __func__, rc);
309 if(MM_CAMERA_OK != rc) {
310 ALOGE("%s:set stream channel format err=%d\n", __func__, ret);
311 ALOGV("%s: X", __func__);
312 ret = BAD_VALUE;
313 }
314 ALOGV("%s: X",__func__);
315 return ret;
316 }
317
QCameraStream()318 QCameraStream::QCameraStream (){
319 mInit = false;
320 mActive = false;
321 /* memset*/
322 memset(&mCrop, 0, sizeof(mm_camera_ch_crop_t));
323 }
324
QCameraStream(int cameraId,camera_mode_t mode)325 QCameraStream::QCameraStream (int cameraId, camera_mode_t mode)
326 :mCameraId(cameraId),
327 myMode(mode)
328 {
329 mInit = false;
330 mActive = false;
331
332 /* memset*/
333 memset(&mCrop, 0, sizeof(mm_camera_ch_crop_t));
334 }
335
~QCameraStream()336 QCameraStream::~QCameraStream () {;}
337
338
init()339 status_t QCameraStream::init() {
340 return NO_ERROR;
341 }
342
start()343 status_t QCameraStream::start() {
344 return NO_ERROR;
345 }
346
stop()347 void QCameraStream::stop() {
348 return;
349 }
350
release()351 void QCameraStream::release() {
352 return;
353 }
354
setHALCameraControl(QCameraHardwareInterface * ctrl)355 void QCameraStream::setHALCameraControl(QCameraHardwareInterface* ctrl) {
356
357 /* provide a frame data user,
358 for the queue monitor thread to call the busy queue is not empty*/
359 mHalCamCtrl = ctrl;
360 }
361
362 }; // namespace android
363