1 /*
2  * Copyright (c) 2020 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 #ifndef OHOS_CAMERA_DEVICE_H
17 #define OHOS_CAMERA_DEVICE_H
18 
19 #include <cstdbool>
20 #include <thread>
21 #include <vector>
22 
23 #include "camera_ability.h"
24 #include "camera_config.h"
25 #include "codec_type.h"
26 #include "frame_config.h"
27 #include "surface.h"
28 using namespace std;
29 namespace OHOS {
30 namespace Media {
31 enum LoopState {
32     LOOP_IDLE,
33     LOOP_READY,
34     LOOP_LOOPING,
35     LOOP_STOP,
36     LOOP_ERROR,
37 };
38 const int32_t RECORDER_MAX_NUM = 2;
39 class DeviceAssistant {
40 public:
41     std::thread *thrd_ = nullptr;
42     LoopState state_ = LOOP_IDLE;
43     FrameConfig *fc_ = nullptr;
44     uint32_t cameraId_;
45     uint32_t streamId_;
46 
SetFrameConfig(FrameConfig & fc,uint32_t * streamId)47     virtual int32_t SetFrameConfig(FrameConfig &fc, uint32_t *streamId)
48     {
49         return -1;
50     }
Start(uint32_t streamId)51     virtual int32_t Start(uint32_t streamId)
52     {
53         return -1;
54     }
Stop()55     virtual int32_t Stop()
56     {
57         return -1;
58     }
59 };
60 
61 struct CodecDesc {
62     CODEC_HANDLETYPE vencHdl_;
63     list<Surface *> vencSurfaces_;
64 };
65 
66 class RecordAssistant : public DeviceAssistant {
67 public:
68     int32_t SetFrameConfig(FrameConfig &fc, uint32_t *streamId) override;
69     int32_t Start(uint32_t streamId) override;
70     int32_t Stop() override;
71     void ClearFrameConfig();
72     static int OnVencBufferAvailble(UINTPTR userDate, CodecBuffer *outBuf, int32_t *acquireFd);
73     static CodecCallback recordCodecCb_;
74 private:
75     int32_t SetFrameConfigEnd(int32_t result);
76     vector<CodecDesc> codecInfo_;
77     int32_t streamIdNum_[RECORDER_MAX_NUM] = {-1, -1};
78 };
79 
80 class PreviewAssistant : public DeviceAssistant {
81 public:
82     int32_t SetFrameConfig(FrameConfig &fc, uint32_t *streamId) override;
83     int32_t Start(uint32_t streamId) override;
84     int32_t Stop() override;
85     Surface *capSurface_ = nullptr;
86 private:
87     pthread_t threadId;
88     static void *YuvCopyProcess(void *arg);
89 };
90 
91 class CaptureAssistant : public DeviceAssistant {
92     int32_t SetFrameConfig(FrameConfig &fc, uint32_t *streamId) override;
93     int32_t Start(uint32_t streamId) override;
94     int32_t Stop() override;
95     CODEC_HANDLETYPE vencHdl_ = nullptr;
96     Surface *capSurface_ = nullptr;
97 };
98 
99 class CallbackAssistant : public DeviceAssistant {
100 public:
101     int32_t SetFrameConfig(FrameConfig &fc, uint32_t *streamId) override;
102     int32_t Start(uint32_t streamId) override;
103     int32_t Stop() override;
104     Surface *capSurface_ = nullptr;
105 private:
106     pthread_t threadId;
107     static void *StreamCopyProcess(void *arg);
108 };
109 
110 class CameraDevice {
111 public:
112     CameraDevice();
113     explicit CameraDevice(uint32_t cameraId);
114     virtual ~CameraDevice();
115 
116     int32_t Initialize();
117     int32_t UnInitialize();
118     int32_t SetCameraConfig();
119     int32_t TriggerLoopingCapture(FrameConfig &fc, uint32_t *streamId);
120     void StopLoopingCapture(int32_t type);
121     int32_t TriggerSingleCapture(FrameConfig &fc, uint32_t *streamId);
122     uint32_t GetCameraId();
123 private:
124     uint32_t cameraId;
125     RecordAssistant recordAssistant_;
126     PreviewAssistant previewAssistant_;
127     CaptureAssistant captureAssistant_;
128     CallbackAssistant callbackAssistant_;
129 };
130 } // namespace Media
131 } // namespace OHOS
132 #endif // OHOS_CAMERA_DEVICE_H