1/* 2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#include "dcamera_hdf_demo.h" 17#include <cstdio> 18#include <getopt.h> 19 20#include "distributed_hardware_log.h" 21 22namespace OHOS { 23namespace DistributedHardware { 24 25static void Usage(FILE* fp) 26{ 27 fprintf(fp, 28 "Options:\n" 29 "-h | --help Print this message\n" 30 "-o | --offline stream offline test\n" 31 "-c | --capture capture one picture\n" 32 "-w | --set WB Set white balance Cloudy\n" 33 "-v | --video capture Video of 10s\n" 34 "-a | --Set AE Set Auto exposure\n" 35 "-e | --Set Metadeta Set Metadata\n" 36 "-f | --Set Flashlight Set flashlight ON 5s OFF\n" 37 "-q | --quit stop preview and quit this app\n"); 38} 39 40const static struct option LONG_OPTIONS[] = { 41 {"help", no_argument, nullptr, 'h'}, {"capture", no_argument, nullptr, 'c'}, 42 {"WB", no_argument, nullptr, 'w'}, {"video", no_argument, nullptr, 'v'}, 43 {"quit", no_argument, nullptr, 'q'}, {"AE", no_argument, nullptr, 'a'}, 44 {"OL", no_argument, nullptr, 'o'}, {"flashlight", no_argument, nullptr, 'f'}, 45 {0, 0, 0, 0} 46}; 47 48static int PutMenuAndGetChr(void) 49{ 50 constexpr uint32_t inputCount = 50; 51 int c = 0; 52 char strs[inputCount]; 53 54 Usage(stdout); 55 DHLOGI("pls input command(input -q exit this app)"); 56 fgets(strs, inputCount, stdin); 57 58 for (uint32_t i = 0; i < inputCount; i++) { 59 if (strs[i] != '-') { 60 c = strs[i]; 61 break; 62 } 63 } 64 65 return c; 66} 67 68static RetCode PreviewOn(int mode, const std::shared_ptr<DcameraHdfDemo>& mainDemo) 69{ 70 RetCode rc = RC_OK; 71 DHLOGI("main test: PreviewOn enter"); 72 73 rc = mainDemo->StartPreviewStream(); 74 if (rc != RC_OK) { 75 DHLOGE("main test: PreviewOn StartPreviewStream error"); 76 return RC_ERROR; 77 } 78 DHLOGI("main test: StartPreviewStream enter"); 79 if (mode == 0) { 80 rc = mainDemo->StartCaptureStream(); 81 if (rc != RC_OK) { 82 DHLOGE("main test: PreviewOn StartCaptureStream error"); 83 return RC_ERROR; 84 } 85 DHLOGI("main test: StartCaptureStream enter"); 86 } else { 87 rc = mainDemo->StartVideoStream(); 88 if (rc != RC_OK) { 89 DHLOGE("main test: PreviewOn StartVideoStream error"); 90 return RC_ERROR; 91 } 92 DHLOGI("main test: StartVideoStream enter"); 93 } 94 rc = mainDemo->CreateStream(); 95 if (rc != RC_OK) { 96 DHLOGE("main test: CreateStream error"); 97 return RC_ERROR; 98 } 99 rc = mainDemo->CaptureON(STREAM_ID_PREVIEW, CAPTURE_ID_PREVIEW, CAPTURE_PREVIEW); 100 if (rc != RC_OK) { 101 DHLOGE("main test: PreviewOn mainDemo->CaptureON() preview error"); 102 return RC_ERROR; 103 } 104 105 DHLOGI("main test: PreviewOn exit"); 106 return RC_OK; 107} 108 109static void PreviewOff(const std::shared_ptr<DcameraHdfDemo>& mainDemo) 110{ 111 DHLOGI("main test: PreviewOff enter"); 112 113 mainDemo->CaptureOff(CAPTURE_ID_PREVIEW, CAPTURE_PREVIEW); 114 mainDemo->ReleaseAllStream(); 115 116 DHLOGI("main test: PreviewOff exit"); 117} 118 119static void FlashLightTest(const std::shared_ptr<DcameraHdfDemo>& mainDemo) 120{ 121 constexpr size_t delayTime = 5; 122 123 PreviewOff(mainDemo); 124 mainDemo->ReleaseCameraDevice(); 125 sleep(1); 126 mainDemo->FlashlightOnOff(true); 127 sleep(delayTime); 128 mainDemo->FlashlightOnOff(false); 129 mainDemo->InitCameraDevice(); 130 PreviewOn(0, mainDemo); 131} 132 133static void OfflineTest(const std::shared_ptr<DcameraHdfDemo>& mainDemo) 134{ 135 RetCode rc = RC_OK; 136 constexpr uint32_t delayTime = 5; 137 PreviewOff(mainDemo); 138 139 mainDemo->StartDualStreams(STREAM_ID_CAPTURE); 140 mainDemo->CaptureOnDualStreams(STREAM_ID_CAPTURE); 141 sleep(1); 142 143 rc = mainDemo->StreamOffline(STREAM_ID_CAPTURE); 144 if (rc != RC_OK) { 145 DHLOGE("main test: mainDemo->StreamOffline error"); 146 } 147 148 sleep(delayTime); 149 mainDemo->InitCameraDevice(); 150 rc = PreviewOn(0, mainDemo); 151 if (rc != RC_OK) { 152 DHLOGE("main test: PreviewOn() error"); 153 } 154} 155 156static void CaptureTest(const std::shared_ptr<DcameraHdfDemo>& mainDemo) 157{ 158 RetCode rc = RC_OK; 159 constexpr size_t delayTime = 5; 160 161 rc = mainDemo->CaptureON(STREAM_ID_CAPTURE, CAPTURE_ID_CAPTURE, CAPTURE_SNAPSHOT); 162 if (rc != RC_OK) { 163 DHLOGE("main test: mainDemo->CaptureON() capture error"); 164 return; 165 } 166 167 sleep(delayTime); 168 rc = mainDemo->CaptureOff(CAPTURE_ID_CAPTURE, CAPTURE_SNAPSHOT); 169 if (rc != RC_OK) { 170 DHLOGE("main test: mainDemo->CaptureOff() capture error"); 171 return; 172 } 173 DHLOGI("main test: CaptureON success"); 174} 175 176static void VideoTest(const std::shared_ptr<DcameraHdfDemo>& mainDemo) 177{ 178 RetCode rc = RC_OK; 179 constexpr size_t delayTime = 5; 180 181 PreviewOff(mainDemo); 182 mainDemo->StartDualStreams(STREAM_ID_VIDEO); 183 mainDemo->CaptureOnDualStreams(STREAM_ID_VIDEO); 184 185 sleep(delayTime); 186 mainDemo->CaptureOff(CAPTURE_ID_PREVIEW, CAPTURE_PREVIEW); 187 mainDemo->CaptureOff(CAPTURE_ID_VIDEO, CAPTURE_VIDEO); 188 mainDemo->ReleaseAllStream(); 189 190 rc = PreviewOn(0, mainDemo); 191 if (rc != RC_OK) { 192 DHLOGE("main test: video PreviewOn() error please -q exit demo"); 193 } 194} 195 196static void SetAwb(const std::shared_ptr<DcameraHdfDemo>& mainDemo, int& awb) 197{ 198 if (awb) { 199 mainDemo->SetAwbMode(OHOS_CAMERA_AWB_MODE_INCANDESCENT); 200 } else { 201 mainDemo->SetAwbMode(OHOS_CAMERA_AWB_MODE_OFF); 202 } 203 awb = !awb; 204} 205 206static void ManuList(const std::shared_ptr<DcameraHdfDemo>& mainDemo, const int argc, char** argv) 207{ 208 int idx, c; 209 int awb = 1; 210 const char *shortOptions = "h:cwvaeqof:"; 211 c = getopt_long(argc, argv, shortOptions, LONG_OPTIONS, &idx); 212 while (1) { 213 switch (c) { 214 case 'h': 215 c = PutMenuAndGetChr(); 216 break; 217 case 'f': 218 FlashLightTest(mainDemo); 219 c = PutMenuAndGetChr(); 220 break; 221 case 'o': 222 OfflineTest(mainDemo); 223 c = PutMenuAndGetChr(); 224 break; 225 case 'c': 226 CaptureTest(mainDemo); 227 c = PutMenuAndGetChr(); 228 break; 229 case 'w': 230 SetAwb(mainDemo, awb); 231 c = PutMenuAndGetChr(); 232 break; 233 case 'a': 234 mainDemo->SetAeExpo(); 235 c = PutMenuAndGetChr(); 236 break; 237 case 'e': 238 mainDemo->SetMetadata(); 239 c = PutMenuAndGetChr(); 240 break; 241 case 'v': 242 VideoTest(mainDemo); 243 c = PutMenuAndGetChr(); 244 break; 245 case 'q': 246 PreviewOff(mainDemo); 247 mainDemo->QuitDemo(); 248 return; 249 default: 250 DHLOGE("main test: command error please retry input command"); 251 c = PutMenuAndGetChr(); 252 break; 253 } 254 } 255} 256 257int main(int argc, char** argv) 258{ 259 RetCode rc = RC_OK; 260 std::cout << "dcamera hdi start" << std::endl; 261 auto mainDemo = std::make_shared<DcameraHdfDemo>(); 262 rc = mainDemo->InitSensors(); 263 if (rc == RC_ERROR) { 264 DHLOGE("main test: mainDemo->InitSensors() error"); 265 return -1; 266 } 267 std::cout << "dcamera InitSensors success" << std::endl; 268 rc = mainDemo->InitCameraDevice(); 269 if (rc == RC_ERROR) { 270 DHLOGE("main test: mainDemo->InitCameraDevice() error"); 271 return -1; 272 } 273 std::cout << "dcamera InitCameraDevice success" << std::endl; 274 mainDemo->SetEnableResult(); 275 276 rc = PreviewOn(0, mainDemo); 277 if (rc != RC_OK) { 278 DHLOGE("main test: PreviewOn() error demo exit"); 279 return -1; 280 } 281 std::cout << "dcamera PreviewOn success" << std::endl; 282 std::cout << "dcamera ManuList start" << std::endl; 283 ManuList(mainDemo, argc, argv); 284 std::cout << "dcamera hdi end" << std::endl; 285 return RC_OK; 286} 287} 288} // namespace OHOS::DistributedHardware 289 290int main(int argc, char** argv) 291{ 292 OHOS::DistributedHardware::main(argc, argv); 293 294 return 0; 295}