1 /* Copyright (c) 2012-2014, The Linux Foundataion. All rights reserved. 2 * 3 * Redistribution and use in source and binary forms, with or without 4 * modification, are permitted provided that the following conditions are 5 * met: 6 * * Redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer. 8 * * Redistributions in binary form must reproduce the above 9 * copyright notice, this list of conditions and the following 10 * disclaimer in the documentation and/or other materials provided 11 * with the distribution. 12 * * Neither the name of The Linux Foundation nor the names of its 13 * contributors may be used to endorse or promote products derived 14 * from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 */ 29 30 #ifndef __QCAMERA_STATEMACHINE_H__ 31 #define __QCAMERA_STATEMACHINE_H__ 32 33 #include <pthread.h> 34 35 #include <cam_semaphore.h> 36 extern "C" { 37 #include <mm_camera_interface.h> 38 } 39 40 #include "QCameraQueue.h" 41 #include "QCameraChannel.h" 42 43 namespace qcamera { 44 45 class QCamera2HardwareInterface; 46 47 typedef enum { 48 /*******BEGIN OF: API EVT*********/ 49 QCAMERA_SM_EVT_SET_PREVIEW_WINDOW = 1, // set preview window 50 QCAMERA_SM_EVT_SET_CALLBACKS, // set callbacks 51 QCAMERA_SM_EVT_ENABLE_MSG_TYPE, // enable msg type 52 QCAMERA_SM_EVT_DISABLE_MSG_TYPE, // disable msg type 53 QCAMERA_SM_EVT_MSG_TYPE_ENABLED, // query certain msg type is enabled 54 55 QCAMERA_SM_EVT_SET_PARAMS, // set parameters 56 QCAMERA_SM_EVT_GET_PARAMS, // get parameters 57 QCAMERA_SM_EVT_PUT_PARAMS, // put parameters, release param buf 58 59 QCAMERA_SM_EVT_START_PREVIEW, // start preview (zsl, camera mode, camcorder mode) 60 QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW, // start no display preview (zsl, camera mode, camcorder mode) 61 QCAMERA_SM_EVT_STOP_PREVIEW, // stop preview (zsl, camera mode, camcorder mode) 62 QCAMERA_SM_EVT_PREVIEW_ENABLED, // query if preview is running 63 64 QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS, // request to store meta data in video buffers 65 QCAMERA_SM_EVT_START_RECORDING, // start recording 66 QCAMERA_SM_EVT_STOP_RECORDING, // stop recording 67 QCAMERA_SM_EVT_RECORDING_ENABLED, // query if recording is running 68 QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME, // release recording frame 69 70 QCAMERA_SM_EVT_PREPARE_SNAPSHOT, // prepare snapshot in case LED needs to be flashed 71 QCAMERA_SM_EVT_TAKE_PICTURE, // take picutre (zsl, regualr capture, live snapshot 72 QCAMERA_SM_EVT_CANCEL_PICTURE, // cancel picture 73 74 QCAMERA_SM_EVT_START_AUTO_FOCUS, // start auto focus 75 QCAMERA_SM_EVT_STOP_AUTO_FOCUS, // stop auto focus 76 QCAMERA_SM_EVT_SEND_COMMAND, // send command 77 78 QCAMERA_SM_EVT_RELEASE, // release camera resource 79 QCAMERA_SM_EVT_DUMP, // dump 80 QCAMERA_SM_EVT_REG_FACE_IMAGE, // register a face image in imaging lib 81 /*******END OF: API EVT*********/ 82 83 QCAMERA_SM_EVT_EVT_INTERNAL, // internal evt notify 84 QCAMERA_SM_EVT_EVT_NOTIFY, // evt notify from server 85 QCAMERA_SM_EVT_JPEG_EVT_NOTIFY, // evt notify from jpeg 86 QCAMERA_SM_EVT_SNAPSHOT_DONE, // internal evt that snapshot is done 87 QCAMERA_SM_EVT_THERMAL_NOTIFY, // evt notify from thermal daemon 88 QCAMERA_SM_EVT_STOP_CAPTURE_CHANNEL, // stop capture channel 89 QCAMERA_SM_EVT_MAX 90 } qcamera_sm_evt_enum_t; 91 92 typedef enum { 93 QCAMERA_API_RESULT_TYPE_DEF, // default type, no additional info 94 QCAMERA_API_RESULT_TYPE_ENABLE_FLAG, // msg_enabled, preview_enabled, recording_enabled 95 QCAMERA_API_RESULT_TYPE_PARAMS, // returned parameters in string 96 QCAMERA_API_RESULT_TYPE_HANDLE, // returned handle in int 97 QCAMERA_API_RESULT_TYPE_MAX 98 } qcamera_api_result_type_t; 99 100 typedef struct { 101 int32_t status; // api call status 102 qcamera_sm_evt_enum_t request_api; // api evt requested 103 qcamera_api_result_type_t result_type; // result type 104 union { 105 int enabled; // result_type == QCAMERA_API_RESULT_TYPE_ENABLE_FLAG 106 char *params; // result_type == QCAMERA_API_RESULT_TYPE_PARAMS 107 int handle; // result_type ==QCAMERA_API_RESULT_TYPE_HANDLE 108 }; 109 } qcamera_api_result_t; 110 111 typedef struct api_result_list { 112 qcamera_api_result_t result; 113 struct api_result_list *next; 114 }api_result_list; 115 116 // definition for payload type of setting callback 117 typedef struct { 118 camera_notify_callback notify_cb; 119 camera_data_callback data_cb; 120 camera_data_timestamp_callback data_cb_timestamp; 121 camera_request_memory get_memory; 122 void *user; 123 } qcamera_sm_evt_setcb_payload_t; 124 125 // definition for payload type of sending command 126 typedef struct { 127 int32_t cmd; 128 int32_t arg1; 129 int32_t arg2; 130 } qcamera_sm_evt_command_payload_t; 131 132 // definition for payload type of sending command 133 typedef struct { 134 void *img_ptr; 135 cam_pp_offline_src_config_t *config; 136 } qcamera_sm_evt_reg_face_payload_t; 137 138 typedef enum { 139 QCAMERA_INTERNAL_EVT_FOCUS_UPDATE, // focus updating result 140 QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE, // prepare snapshot done 141 QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT, // face detection result 142 QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS, // histogram 143 QCAMERA_INTERNAL_EVT_CROP_INFO, // crop info 144 QCAMERA_INTERNAL_EVT_ASD_UPDATE, // asd update result 145 QCAMERA_INTERNAL_EVT_READY_FOR_SNAPSHOT, // Ready for Prepare Snapshot 146 QCAMERA_INTERNAL_EVT_MAX 147 } qcamera_internal_evt_type_t; 148 149 typedef struct { 150 qcamera_internal_evt_type_t evt_type; 151 union { 152 cam_auto_focus_data_t focus_data; 153 cam_prep_snapshot_state_t prep_snapshot_state; 154 cam_face_detection_data_t faces_data; 155 cam_hist_stats_t stats_data; 156 cam_crop_data_t crop_data; 157 cam_auto_scene_t asd_data; 158 }; 159 } qcamera_sm_internal_evt_payload_t; 160 161 class QCameraStateMachine 162 { 163 public: 164 QCameraStateMachine(QCamera2HardwareInterface *ctrl); 165 virtual ~QCameraStateMachine(); 166 int32_t procAPI(qcamera_sm_evt_enum_t evt, void *api_payload); 167 int32_t procEvt(qcamera_sm_evt_enum_t evt, void *evt_payload); 168 169 bool isPreviewRunning(); // check if preview is running 170 bool isPreviewReady(); // check if preview is ready 171 bool isCaptureRunning(); // check if image capture is running 172 bool isNonZSLCaptureRunning(); // check if image capture is running in non ZSL mode 173 String8 dump(); //returns the state information in a string 174 bool isPrepSnapStateRunning(); 175 bool isRecording(); 176 177 private: 178 typedef enum { 179 QCAMERA_SM_STATE_PREVIEW_STOPPED, // preview is stopped 180 QCAMERA_SM_STATE_PREVIEW_READY, // preview started but preview window is not set yet 181 QCAMERA_SM_STATE_PREVIEWING, // previewing 182 QCAMERA_SM_STATE_PREPARE_SNAPSHOT, // prepare snapshot in case aec estimation is 183 // needed for LED flash 184 QCAMERA_SM_STATE_PIC_TAKING, // taking picture (preview stopped) 185 QCAMERA_SM_STATE_RECORDING, // recording (preview running) 186 QCAMERA_SM_STATE_VIDEO_PIC_TAKING, // taking live snapshot during recording (preview running) 187 QCAMERA_SM_STATE_PREVIEW_PIC_TAKING // taking ZSL/live snapshot (recording stopped but preview running) 188 } qcamera_state_enum_t; 189 190 typedef enum 191 { 192 QCAMERA_SM_CMD_TYPE_API, // cmd from API 193 QCAMERA_SM_CMD_TYPE_EVT, // cmd from mm-camera-interface/mm-jpeg-interface event 194 QCAMERA_SM_CMD_TYPE_EXIT, // cmd for exiting statemachine cmdThread 195 QCAMERA_SM_CMD_TYPE_MAX 196 } qcamera_sm_cmd_type_t; 197 198 typedef struct { 199 qcamera_sm_cmd_type_t cmd; // cmd type (where it comes from) 200 qcamera_sm_evt_enum_t evt; // event type 201 void *evt_payload; // ptr to payload 202 } qcamera_sm_cmd_t; 203 204 int32_t stateMachine(qcamera_sm_evt_enum_t evt, void *payload); 205 int32_t procEvtPreviewStoppedState(qcamera_sm_evt_enum_t evt, void *payload); 206 int32_t procEvtPreviewReadyState(qcamera_sm_evt_enum_t evt, void *payload); 207 int32_t procEvtPreviewingState(qcamera_sm_evt_enum_t evt, void *payload); 208 int32_t procEvtPrepareSnapshotState(qcamera_sm_evt_enum_t evt, void *payload); 209 int32_t procEvtPicTakingState(qcamera_sm_evt_enum_t evt, void *payload); 210 int32_t procEvtRecordingState(qcamera_sm_evt_enum_t evt, void *payload); 211 int32_t procEvtVideoPicTakingState(qcamera_sm_evt_enum_t evt, void *payload); 212 int32_t procEvtPreviewPicTakingState(qcamera_sm_evt_enum_t evt, void *payload); 213 214 // main statemachine process routine 215 static void *smEvtProcRoutine(void *data); 216 217 QCamera2HardwareInterface *m_parent; // ptr to HWI 218 qcamera_state_enum_t m_state; // statemachine state 219 QCameraQueue api_queue; // cmd queue for APIs 220 QCameraQueue evt_queue; // cmd queue for evt from mm-camera-intf/mm-jpeg-intf 221 pthread_t cmd_pid; // cmd thread ID 222 cam_semaphore_t cmd_sem; // semaphore for cmd thread 223 }; 224 225 }; // namespace qcamera 226 227 #endif /* __QCAMERA_STATEMACHINE_H__ */ 228