1 /*
2  * Copyright (c) 2021 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 "ohos_camera_demo.h"
17 #include <cstdio>
18 #include <getopt.h>
19 
20 namespace OHOS::Camera {
Usage(FILE * fp)21 static void Usage(FILE* fp)
22 {
23     fprintf(fp,
24             "Options:\n"
25             "-h | --help          Print this message\n"
26             "-o | --offline       stream offline test\n"
27             "-c | --capture       capture one picture\n"
28             "-w | --set WB        Set white balance Cloudy\n"
29             "-v | --video         capture Video of 10s\n"
30             "-a | --Set AE        Set Auto exposure\n"
31             "-e | --Set Metadeta  Set Metadata\n"
32             "-f | --Set Flashlight        Set flashlight ON 5s OFF\n"
33             "-q | --quit          stop preview and quit this app\n");
34 }
35 
36 const static struct option LONG_OPTIONS[] = {
37     {"help", no_argument, nullptr, 'h'}, {"capture", no_argument, nullptr, 'c'},
38     {"WB", no_argument, nullptr, 'w'}, {"video", no_argument, nullptr, 'v'},
39     {"quit", no_argument, nullptr, 'q'}, {"AE", no_argument, nullptr, 'a'},
40     {"OL", no_argument, nullptr, 'o'}, {"flashlight", no_argument, nullptr, 'f'},
41     {0, 0, 0, 0}
42 };
43 
PutMenuAndGetChr(void)44 static int PutMenuAndGetChr(void)
45 {
46     constexpr uint32_t inputCount = 50;
47     int c = 0;
48     char strs[inputCount];
49 
50     Usage(stdout);
51     CAMERA_LOGD("pls input command(input -q exit this app)\n");
52     fgets(strs, inputCount, stdin);
53 
54     for (int i = 0; i < inputCount; i++) {
55         if (strs[i] != '-') {
56             c = strs[i];
57             break;
58         }
59     }
60 
61     return c;
62 }
63 
PreviewOn(int mode,const std::shared_ptr<OhosCameraDemo> & mainDemo)64 static RetCode PreviewOn(int mode, const std::shared_ptr<OhosCameraDemo>& mainDemo)
65 {
66     RetCode rc = RC_OK;
67     CAMERA_LOGD("main test: PreviewOn enter");
68 
69     rc = mainDemo->StartPreviewStream();
70     if (rc != RC_OK) {
71         CAMERA_LOGE("main test: PreviewOn StartPreviewStream error");
72         return RC_ERROR;
73     }
74 
75     if (mode == 0) {
76         rc = mainDemo->StartCaptureStream();
77         if (rc != RC_OK) {
78             CAMERA_LOGE("main test: PreviewOn StartCaptureStream error");
79             return RC_ERROR;
80         }
81     } else {
82         rc = mainDemo->StartVideoStream();
83         if (rc != RC_OK) {
84             CAMERA_LOGE("main test: PreviewOn StartVideoStream error");
85             return RC_ERROR;
86         }
87     }
88 
89     rc = mainDemo->CaptureON(STREAM_ID_PREVIEW, CAPTURE_ID_PREVIEW, CAPTURE_PREVIEW);
90     if (rc != RC_OK) {
91         CAMERA_LOGE("main test: PreviewOn mainDemo->CaptureON() preview error");
92         return RC_ERROR;
93     }
94 
95     CAMERA_LOGD("main test: PreviewOn exit");
96     return RC_OK;
97 }
98 
PreviewOff(const std::shared_ptr<OhosCameraDemo> & mainDemo)99 static void PreviewOff(const std::shared_ptr<OhosCameraDemo>& mainDemo)
100 {
101     CAMERA_LOGD("main test: PreviewOff enter");
102 
103     mainDemo->CaptureOff(CAPTURE_ID_PREVIEW, CAPTURE_PREVIEW);
104     mainDemo->ReleaseAllStream();
105 
106     CAMERA_LOGD("main test: PreviewOff exit");
107 }
108 
FlashLightTest(const std::shared_ptr<OhosCameraDemo> & mainDemo)109 static void FlashLightTest(const std::shared_ptr<OhosCameraDemo>& mainDemo)
110 {
111     constexpr size_t delayTime = 5;
112 
113     PreviewOff(mainDemo);
114     mainDemo->ReleaseCameraDevice();
115     sleep(1);
116     mainDemo->FlashlightOnOff(true);
117     sleep(delayTime);
118     mainDemo->FlashlightOnOff(false);
119     mainDemo->InitCameraDevice();
120     PreviewOn(0, mainDemo);
121 }
122 
OfflineTest(const std::shared_ptr<OhosCameraDemo> & mainDemo)123 static void OfflineTest(const std::shared_ptr<OhosCameraDemo>& mainDemo)
124 {
125     RetCode rc = RC_OK;
126 
127     PreviewOff(mainDemo);
128 
129     mainDemo->StartDualStreams(STREAM_ID_CAPTURE);
130     mainDemo->CaptureOnDualStreams(STREAM_ID_CAPTURE);
131     sleep(1);
132 
133     rc = mainDemo->StreamOffline(STREAM_ID_CAPTURE);
134     if (rc != RC_OK) {
135         CAMERA_LOGE("main test: mainDemo->StreamOffline error");
136     } else {
137         sleep(1);
138         mainDemo->InitCameraDevice();
139         rc = PreviewOn(0, mainDemo);
140         if (rc != RC_OK) {
141             CAMERA_LOGE("main test: PreviewOn() error");
142         }
143     }
144 }
145 
CaptureTest(const std::shared_ptr<OhosCameraDemo> & mainDemo)146 static void CaptureTest(const std::shared_ptr<OhosCameraDemo>& mainDemo)
147 {
148     RetCode rc = RC_OK;
149     constexpr size_t delayTime = 5;
150 
151     rc = mainDemo->CaptureON(STREAM_ID_CAPTURE, CAPTURE_ID_CAPTURE, CAPTURE_SNAPSHOT);
152     if (rc != RC_OK) {
153         CAMERA_LOGE("main test: mainDemo->CaptureON() capture error");
154         return;
155     }
156 
157     sleep(delayTime);
158     rc = mainDemo->CaptureOff(CAPTURE_ID_CAPTURE, CAPTURE_SNAPSHOT);
159     if (rc != RC_OK) {
160         CAMERA_LOGE("main test: mainDemo->CaptureOff() capture error");
161         return;
162     }
163 }
164 
VideoTest(const std::shared_ptr<OhosCameraDemo> & mainDemo)165 static void VideoTest(const std::shared_ptr<OhosCameraDemo>& mainDemo)
166 {
167     RetCode rc = RC_OK;
168     constexpr size_t delayTime = 5;
169 
170     PreviewOff(mainDemo);
171     mainDemo->StartDualStreams(STREAM_ID_VIDEO);
172     mainDemo->CaptureOnDualStreams(STREAM_ID_VIDEO);
173 
174     sleep(delayTime);
175     mainDemo->CaptureOff(CAPTURE_ID_PREVIEW, CAPTURE_PREVIEW);
176     mainDemo->CaptureOff(CAPTURE_ID_VIDEO, CAPTURE_VIDEO);
177     mainDemo->ReleaseAllStream();
178 
179     rc = PreviewOn(0, mainDemo);
180     if (rc != RC_OK) {
181         CAMERA_LOGE("main test: video PreviewOn() error please -q exit demo");
182     }
183 }
184 
ManuList(const std::shared_ptr<OhosCameraDemo> & mainDemo,const int argc,char ** argv)185 static void ManuList(const std::shared_ptr<OhosCameraDemo>& mainDemo,
186     const int argc, char** argv)
187 {
188     int idx, c;
189     bool isAwb = true;
190     const char *shortOptions = "h:cwvaeqof:";
191     c = getopt_long(argc, argv, shortOptions, LONG_OPTIONS, &idx);
192     while (1) {
193         switch (c) {
194             case 'h':
195                 c = PutMenuAndGetChr();
196                 break;
197             case 'f':
198                 FlashLightTest(mainDemo);
199                 c = PutMenuAndGetChr();
200                 break;
201             case 'o':
202                 OfflineTest(mainDemo);
203                 c = PutMenuAndGetChr();
204                 break;
205             case 'c':
206                 CaptureTest(mainDemo);
207                 c = PutMenuAndGetChr();
208                 break;
209             case 'w':
210                 if (isAwb) {
211                     mainDemo->SetAwbMode(OHOS_CAMERA_AWB_MODE_INCANDESCENT);
212                 } else {
213                     mainDemo->SetAwbMode(OHOS_CAMERA_AWB_MODE_OFF);
214                 }
215                 isAwb = !isAwb;
216                 c = PutMenuAndGetChr();
217                 break;
218             case 'a':
219                 mainDemo->SetAeExpo();
220                 c = PutMenuAndGetChr();
221                 break;
222             case 'e':
223                 mainDemo->SetMetadata();
224                 c = PutMenuAndGetChr();
225                 break;
226             case 'v':
227                 VideoTest(mainDemo);
228                 c = PutMenuAndGetChr();
229                 break;
230             case 'q':
231                 PreviewOff(mainDemo);
232                 mainDemo->QuitDemo();
233                 return;
234             default:
235                 CAMERA_LOGE("main test: command error please retry input command");
236                 c = PutMenuAndGetChr();
237                 break;
238         }
239     }
240 }
241 
main(int argc,char ** argv)242 int main(int argc, char** argv)
243 {
244     RetCode rc = RC_OK;
245 
246     auto mainDemo = std::make_shared<OhosCameraDemo>();
247     rc = mainDemo->InitSensors();
248     if (rc == RC_ERROR) {
249         CAMERA_LOGE("main test: mainDemo->InitSensors() error");
250         return -1;
251     }
252     rc = mainDemo->InitCameraDevice();
253     if (rc == RC_ERROR) {
254         CAMERA_LOGE("main test: mainDemo->InitCameraDevice() error");
255         return -1;
256     }
257     mainDemo->SetEnableResult();
258 
259     rc = PreviewOn(0, mainDemo);
260     if (rc != RC_OK) {
261         CAMERA_LOGE("main test: PreviewOn() error demo exit");
262         return -1;
263     }
264 
265     ManuList(mainDemo, argc, argv);
266 
267     return RC_OK;
268 }
269 } // namespace Camera
270 
main(int argc,char ** argv)271 int main(int argc, char** argv)
272 {
273     OHOS::Camera::main(argc, argv);
274 
275     return 0;
276 }
277