• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-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 "camera_impl.h"
17 #include "media_log.h"
18 #include "camera_device_client.h"
19 #include <cstdio>
20 
21 using namespace std;
22 namespace OHOS {
23 namespace Media {
CameraImpl(std::string & id,const CameraAbility * ability,const CameraInfo * info)24 CameraImpl::CameraImpl(std::string &id, const CameraAbility *ability, const CameraInfo *info)
25 {
26     id_ = id;
27     ability_ = ability;
28     info_ = info;
29 }
30 
GetCameraId()31 string CameraImpl::GetCameraId()
32 {
33     return id_;
34 }
35 
GetCameraConfig() const36 const CameraConfig *CameraImpl::GetCameraConfig() const
37 {
38     return config_;
39 }
40 
GetFrameConfig(int32_t type)41 FrameConfig *CameraImpl::GetFrameConfig(int32_t type)
42 {
43     for (auto i : frameConfigs_) {
44         if (i->GetFrameConfigType() == type) {
45             return i;
46         }
47     }
48     return nullptr;
49 }
50 
Configure(CameraConfig & config)51 void CameraImpl::Configure(CameraConfig &config)
52 {
53     if (config_ != nullptr) {
54         return;
55     }
56 
57     if (config.GetFrameStateCb() == nullptr || config.GetEventHandler() == nullptr) {
58         return;
59     }
60 
61     int32_t ret = deviceClient_->SetCameraConfig(config);
62     if (ret != MEDIA_OK) {
63         MEDIA_ERR_LOG("Set camera config failed in cameraImpl.");
64         return;
65     }
66     config_ = &config;
67 }
68 
OnConfigured(int32_t ret,CameraConfig & config)69 void CameraImpl::OnConfigured(int32_t ret, CameraConfig &config)
70 {
71     if (ret != MEDIA_OK) {
72         handler_->Post([this, ret] { this->stateCb_->OnConfigureFailed(this->GetCameraId(), ret); });
73         return;
74     }
75     handler_->Post([this] { this->stateCb_->OnConfigured(*this); });
76 }
77 
Release()78 void CameraImpl::Release()
79 {
80     if (config_ != nullptr) {
81         delete config_;
82         config_ = nullptr;
83     }
84     if (deviceClient_ == nullptr) {
85         return;
86     }
87     deviceClient_->Release();
88     if (handler_ == nullptr) {
89         return;
90     }
91     handler_->Post([this] { this->stateCb_->OnReleased(*this); });
92 }
93 
TriggerLoopingCapture(FrameConfig & fc)94 int32_t CameraImpl::TriggerLoopingCapture(FrameConfig &fc)
95 {
96     if (config_ == nullptr) {
97         MEDIA_ERR_LOG("Cannot find available configuration, configure the camera first.");
98         return MEDIA_INVALID_PARAM;
99     }
100     int32_t type = fc.GetFrameConfigType();
101     if (type == FRAME_CONFIG_CAPTURE) {
102         MEDIA_ERR_LOG("looping capture not support FRAME_CONFIG_CAPTURE");
103         return MEDIA_ERR;
104     }
105     FrameConfig *curFc = GetFrameConfig(fc.GetFrameConfigType());
106     if (curFc != nullptr) {
107         MEDIA_ERR_LOG("Frame config of the input type is already existed.");
108         return MEDIA_ERR;
109     }
110     if (deviceClient_ == nullptr) {
111         return MEDIA_ERR;
112     }
113     int32_t ret = deviceClient_->TriggerLoopingCapture(fc);
114     if (ret != MEDIA_OK) {
115         MEDIA_ERR_LOG("Camera device start looping capture failed.(ret=%d)", ret);
116         return MEDIA_ERR;
117     }
118     frameConfigs_.emplace_back(&fc);
119     return MEDIA_OK;
120 }
121 
StopLoopingCapture()122 void CameraImpl::StopLoopingCapture()
123 {
124     if (deviceClient_ == nullptr) {
125         return;
126     }
127     deviceClient_->StopLoopingCapture();
128     if (config_ == nullptr) {
129         return;
130     }
131     FrameStateCallback *fsc = config_->GetFrameStateCb();
132     if (fsc == nullptr) {
133         return;
134     }
135     EventHandler *eventhdl = config_->GetEventHandler();
136     if (eventhdl == nullptr) {
137         return;
138     }
139 
140     for (auto i : frameConfigs_) {
141         eventhdl->Post([fsc, this, i] {
142             FrameResult frameResult;
143             fsc->OnFrameFinished(*this, *i, frameResult);
144         });
145     }
146     frameConfigs_.clear();
147 }
148 
TriggerSingleCapture(FrameConfig & fc)149 int32_t CameraImpl::TriggerSingleCapture(FrameConfig &fc)
150 {
151     if (config_ == nullptr) {
152         MEDIA_ERR_LOG("Cannot find available configuration, configure the camera first.");
153         return MEDIA_INVALID_PARAM;
154     }
155     if (deviceClient_ == nullptr) {
156         MEDIA_ERR_LOG("Cannot find available configuration, configure the camera first.");
157         return MEDIA_INVALID_PARAM;
158     }
159     if (fc.GetFrameConfigType() != FRAME_CONFIG_CAPTURE) {
160         MEDIA_ERR_LOG("single capture only support FRAME_CONFIG_CAPTURE");
161         return MEDIA_ERR;
162     }
163     int32_t ret = deviceClient_->TriggerSingleCapture(dynamic_cast<FrameConfig &>(fc));
164     if (ret != MEDIA_OK) {
165         return MEDIA_ERR;
166     }
167     return MEDIA_OK;
168 }
169 
GetAbility()170 const CameraAbility *CameraImpl::GetAbility()
171 {
172     return ability_;
173 }
174 
GetInfo()175 const CameraInfo *CameraImpl::GetInfo()
176 {
177     return info_;
178 }
179 
OnCreate(string cameraId)180 void CameraImpl::OnCreate(string cameraId)
181 {
182     deviceClient_ = CameraDeviceClient::GetInstance();
183     if (deviceClient_ == nullptr) {
184         return;
185     }
186     deviceClient_->SetCameraId(cameraId);
187     deviceClient_->SetCameraImpl(this);
188     deviceClient_->SetCameraCallback();
189     if (stateCb_ == nullptr || handler_ == nullptr) {
190         return;
191     }
192     handler_->Post([this] { this->stateCb_->OnCreated(*this); });
193 }
194 
OnFrameFinished(int32_t ret,FrameConfig & fc)195 void CameraImpl::OnFrameFinished(int32_t ret, FrameConfig &fc)
196 {
197     FrameStateCallback *fsc = config_->GetFrameStateCb();
198     if (fsc == nullptr) {
199         return;
200     }
201     EventHandler *eventhdl = config_->GetEventHandler();
202     if (eventhdl == nullptr) {
203         return;
204     }
205     if (ret != MEDIA_OK) {
206         eventhdl->Post([fsc, this, &fc] {
207             FrameResult frameResult;
208             fsc->OnFrameError(*this, fc, -1, frameResult);
209         });
210         return;
211     }
212     eventhdl->Post([fsc, this, &fc] {
213         FrameResult frameResult;
214         fsc->OnFrameFinished(*this, fc, frameResult);
215     });
216 }
217 
OnCreateFailed()218 void CameraImpl::OnCreateFailed()
219 {
220     if (stateCb_ == nullptr || handler_ == nullptr) {
221         return;
222     }
223     handler_->Post([this] { this->stateCb_->OnCreateFailed(id_, MEDIA_ERR); });
224 }
225 
RegistCb(CameraStateCallback & callback,EventHandler & handler)226 void CameraImpl::RegistCb(CameraStateCallback &callback, EventHandler &handler)
227 {
228     handler_ = &handler;
229     stateCb_ = &callback;
230 }
231 } // namespace Media
232 } // namespace OHOS