1 /*
2 * Copyright (c) 2021-2022 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 "hcapture_session.h"
17 #include "camera_log.h"
18 #include "iconsumer_surface.h"
19 #include "ipc_skeleton.h"
20 #include "metadata_utils.h"
21 #include "bundle_mgr_interface.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24
25 using namespace OHOS::AppExecFwk;
26 using namespace OHOS::AAFwk;
27
28 namespace OHOS {
29 namespace CameraStandard {
30 static std::map<int32_t, sptr<HCaptureSession>> session_;
31 static std::mutex sessionLock_;
32 static std::map<CaptureSessionState, std::string> sessionState_ = {
33 {CaptureSessionState::SESSION_INIT, "Init"},
34 {CaptureSessionState::SESSION_CONFIG_INPROGRESS, "Config_In-progress"},
35 {CaptureSessionState::SESSION_CONFIG_COMMITTED, "Committed"}
36 };
GetClientBundle(int uid)37 static std::string GetClientBundle(int uid)
38 {
39 std::string bundleName = "";
40 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
41 if (samgr == nullptr) {
42 MEDIA_ERR_LOG("Get ability manager failed");
43 return bundleName;
44 }
45
46 sptr<IRemoteObject> object = samgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
47 if (object == nullptr) {
48 MEDIA_DEBUG_LOG("object is NULL.");
49 return bundleName;
50 }
51
52 sptr<AppExecFwk::IBundleMgr> bms = iface_cast<AppExecFwk::IBundleMgr>(object);
53 if (bms == nullptr) {
54 MEDIA_DEBUG_LOG("bundle manager service is NULL.");
55 return bundleName;
56 }
57
58 auto result = bms->GetNameForUid(uid, bundleName);
59 if (result != ERR_OK) {
60 MEDIA_ERR_LOG("GetBundleNameForUid fail");
61 return "";
62 }
63 MEDIA_INFO_LOG("bundle name is %{public}s ", bundleName.c_str());
64
65 return bundleName;
66 }
67
HCaptureSession(sptr<HCameraHostManager> cameraHostManager,sptr<StreamOperatorCallback> streamOperatorCb,const uint32_t callingTokenId)68 HCaptureSession::HCaptureSession(sptr<HCameraHostManager> cameraHostManager,
69 sptr<StreamOperatorCallback> streamOperatorCb, const uint32_t callingTokenId)
70 : cameraHostManager_(cameraHostManager), streamOperatorCallback_(streamOperatorCb),
71 sessionCallback_(nullptr), deviceOperatorsCallback_(nullptr)
72 {
73 pid_ = IPCSkeleton::GetCallingPid();
74 uid_ = IPCSkeleton::GetCallingUid();
75 MEDIA_DEBUG_LOG("HCaptureSession: camera stub services(%{public}zu) pid(%{public}d).", session_.size(), pid_);
76 std::map<int32_t, sptr<HCaptureSession>> oldSessions;
77 for (auto it = session_.begin(); it != session_.end(); it++) {
78 sptr<HCaptureSession> session = it->second;
79 oldSessions[it->first] = session;
80 }
81
82 for (auto it = oldSessions.begin(); it != oldSessions.end(); it++) {
83 if (it->second != nullptr) {
84 sptr<HCaptureSession> session = it->second;
85 sptr<HCameraDevice> disconnectDevice;
86 if (pid_ != session->GetPid()) {
87 continue;
88 }
89 int32_t rc = session->GetCameraDevice(disconnectDevice);
90 if (rc == CAMERA_OK) {
91 disconnectDevice->OnError(DEVICE_PREEMPT, 0);
92 }
93 MEDIA_ERR_LOG("HCaptureSession::HCaptureSession doesn't support multiple sessions per pid");
94 session->Release(it->first);
95 }
96 }
97 std::lock_guard<std::mutex> lock(sessionLock_);
98 session_[pid_] = this;
99 callerToken_ = callingTokenId;
100 MEDIA_DEBUG_LOG("HCaptureSession: camera stub services(%{public}zu).", session_.size());
101 }
102
~HCaptureSession()103 HCaptureSession::~HCaptureSession()
104 {
105 deviceOperatorsCallback_ = nullptr;
106 }
107
GetPid()108 pid_t HCaptureSession::GetPid()
109 {
110 return pid_;
111 }
112
CloseDevice(sptr<HCameraDevice> & device)113 void HCaptureSession::CloseDevice(sptr<HCameraDevice> &device)
114 {
115 if (device != nullptr && deviceOperatorsCallback_.promote() != nullptr) {
116 deviceOperatorsCallback_->DeviceClose(device->GetCameraId(), pid_);
117 }
118 }
119
BeginConfig()120 int32_t HCaptureSession::BeginConfig()
121 {
122 CAMERA_SYNC_TRACE;
123 if (curState_ == CaptureSessionState::SESSION_CONFIG_INPROGRESS) {
124 MEDIA_ERR_LOG("HCaptureSession::BeginConfig Already in config inprogress state!");
125 return CAMERA_INVALID_STATE;
126 }
127 std::lock_guard<std::mutex> lock(sessionLock_);
128 prevState_ = curState_;
129 curState_ = CaptureSessionState::SESSION_CONFIG_INPROGRESS;
130 tempCameraDevices_.clear();
131 tempStreams_.clear();
132 deletedStreamIds_.clear();
133 return CAMERA_OK;
134 }
135
AddInput(sptr<ICameraDeviceService> cameraDevice)136 int32_t HCaptureSession::AddInput(sptr<ICameraDeviceService> cameraDevice)
137 {
138 CAMERA_SYNC_TRACE;
139 sptr<HCameraDevice> localCameraDevice = nullptr;
140
141 if (cameraDevice == nullptr) {
142 MEDIA_ERR_LOG("HCaptureSession::AddInput cameraDevice is null");
143 return CAMERA_INVALID_ARG;
144 }
145 std::lock_guard<std::mutex> lock(sessionLock_);
146 if (curState_ != CaptureSessionState::SESSION_CONFIG_INPROGRESS) {
147 MEDIA_ERR_LOG("HCaptureSession::AddInput Need to call BeginConfig before adding input");
148 return CAMERA_INVALID_STATE;
149 }
150 if (!tempCameraDevices_.empty() || (cameraDevice_ != nullptr && !cameraDevice_->IsReleaseCameraDevice())) {
151 MEDIA_ERR_LOG("HCaptureSession::AddInput Only one input is supported");
152 return CAMERA_INVALID_SESSION_CFG;
153 }
154 localCameraDevice = static_cast<HCameraDevice*>(cameraDevice.GetRefPtr());
155 if (cameraDevice_ == localCameraDevice && cameraDevice_ != nullptr) {
156 cameraDevice_->SetReleaseCameraDevice(false);
157 } else {
158 tempCameraDevices_.emplace_back(localCameraDevice);
159 CAMERA_SYSEVENT_STATISTIC(CreateMsg("CaptureSession::AddInput"));
160 }
161
162 sptr<OHOS::HDI::Camera::V1_1::IStreamOperator> streamOperator;
163 int32_t rc = localCameraDevice->GetStreamOperator(streamOperatorCallback_, streamOperator);
164 if (rc != CAMERA_OK) {
165 MEDIA_ERR_LOG("HCaptureSession::GetCameraDevice GetStreamOperator returned %{public}d", rc);
166 CloseDevice(localCameraDevice);
167 return rc;
168 }
169 return CAMERA_OK;
170 }
171
AddOutputStream(sptr<HStreamCommon> stream)172 int32_t HCaptureSession::AddOutputStream(sptr<HStreamCommon> stream)
173 {
174 std::lock_guard<std::mutex> lock(sessionLock_);
175 if (stream == nullptr) {
176 MEDIA_ERR_LOG("HCaptureSession::AddOutputStream stream is null");
177 return CAMERA_INVALID_ARG;
178 }
179 if (curState_ != CaptureSessionState::SESSION_CONFIG_INPROGRESS) {
180 MEDIA_ERR_LOG("HCaptureSession::AddOutputStream Need to call BeginConfig before adding output");
181 return CAMERA_INVALID_STATE;
182 }
183 if (std::find(tempStreams_.begin(), tempStreams_.end(), stream) != tempStreams_.end()) {
184 MEDIA_ERR_LOG("HCaptureSession::AddOutputStream Adding same output multiple times in tempStreams_");
185 return CAMERA_INVALID_SESSION_CFG;
186 }
187 int32_t rc = FindRepeatStream(stream);
188 if (rc != STREAM_NOT_FOUNT) {
189 return rc;
190 }
191 if (stream) {
192 stream->SetReleaseStream(false);
193 }
194 tempStreams_.emplace_back(stream);
195 return CAMERA_OK;
196 }
197
FindRepeatStream(sptr<HStreamCommon> stream)198 int32_t HCaptureSession::FindRepeatStream(sptr<HStreamCommon> stream)
199 {
200 std::lock_guard<std::mutex> lock(streamsLock_);
201 auto it = std::find(streams_.begin(), streams_.end(), stream);
202 if (it != streams_.end()) {
203 if (stream && stream->IsReleaseStream()) {
204 stream->SetReleaseStream(false);
205 auto it2 = std::find(deletedStreamIds_.begin(), deletedStreamIds_.end(), stream->GetStreamId());
206 if (it2 != deletedStreamIds_.end()) {
207 deletedStreamIds_.erase(it2);
208 }
209 return CAMERA_OK;
210 } else {
211 MEDIA_ERR_LOG("HCaptureSession::AddOutputStream Adding same output multiple times in streams_");
212 return CAMERA_INVALID_SESSION_CFG;
213 }
214 }
215 return STREAM_NOT_FOUNT;
216 }
217
AddOutput(StreamType streamType,sptr<IStreamCommon> stream)218 int32_t HCaptureSession::AddOutput(StreamType streamType, sptr<IStreamCommon> stream)
219 {
220 int32_t rc = CAMERA_INVALID_ARG;
221 if (stream == nullptr) {
222 MEDIA_ERR_LOG("HCaptureSession::AddOutput stream is null");
223 return rc;
224 }
225 // Temp hack to fix the library linking issue
226 sptr<IConsumerSurface> captureSurface = IConsumerSurface::Create();
227
228 if (streamType == StreamType::CAPTURE) {
229 rc = AddOutputStream(static_cast<HStreamCapture *>(stream.GetRefPtr()));
230 } else if (streamType == StreamType::REPEAT) {
231 rc = AddOutputStream(static_cast<HStreamRepeat *>(stream.GetRefPtr()));
232 } else if (streamType == StreamType::METADATA) {
233 rc = AddOutputStream(static_cast<HStreamMetadata *>(stream.GetRefPtr()));
234 }
235 CAMERA_SYSEVENT_STATISTIC(CreateMsg("CaptureSession::AddOutput with %d", streamType));
236 MEDIA_INFO_LOG("CaptureSession::AddOutput with with %{public}d, rc = %{public}d", streamType, rc);
237 return rc;
238 }
239
RemoveInput(sptr<ICameraDeviceService> cameraDevice)240 int32_t HCaptureSession::RemoveInput(sptr<ICameraDeviceService> cameraDevice)
241 {
242 if (cameraDevice == nullptr) {
243 MEDIA_ERR_LOG("HCaptureSession::RemoveInput cameraDevice is null");
244 return CAMERA_INVALID_ARG;
245 }
246 if (curState_ != CaptureSessionState::SESSION_CONFIG_INPROGRESS) {
247 MEDIA_ERR_LOG("HCaptureSession::RemoveInput Need to call BeginConfig before removing input");
248 return CAMERA_INVALID_STATE;
249 }
250 std::lock_guard<std::mutex> lock(sessionLock_);
251 sptr<HCameraDevice> localCameraDevice;
252 localCameraDevice = static_cast<HCameraDevice*>(cameraDevice.GetRefPtr());
253 auto it = std::find(tempCameraDevices_.begin(), tempCameraDevices_.end(), localCameraDevice);
254 if (it != tempCameraDevices_.end()) {
255 tempCameraDevices_.erase(it);
256 } else if (cameraDevice_ != nullptr && cameraDevice_ == localCameraDevice) {
257 cameraDevice_->SetReleaseCameraDevice(true);
258 } else {
259 MEDIA_ERR_LOG("HCaptureSession::RemoveInput Invalid camera device");
260 return CAMERA_INVALID_SESSION_CFG;
261 }
262 CAMERA_SYSEVENT_STATISTIC(CreateMsg("CaptureSession::RemoveInput"));
263 return CAMERA_OK;
264 }
265
RemoveOutputStream(sptr<HStreamCommon> stream)266 int32_t HCaptureSession::RemoveOutputStream(sptr<HStreamCommon> stream)
267 {
268 if (stream == nullptr) {
269 MEDIA_ERR_LOG("HCaptureSession::RemoveOutputStream stream is null");
270 return CAMERA_INVALID_ARG;
271 }
272 if (curState_ != CaptureSessionState::SESSION_CONFIG_INPROGRESS) {
273 MEDIA_ERR_LOG("HCaptureSession::RemoveOutputStream Need to call BeginConfig before removing output");
274 return CAMERA_INVALID_STATE;
275 }
276 auto it = std::find(tempStreams_.begin(), tempStreams_.end(), stream);
277 if (it != tempStreams_.end()) {
278 tempStreams_.erase(it);
279 } else {
280 std::lock_guard<std::mutex> lock(streamsLock_);
281 it = std::find(streams_.begin(), streams_.end(), stream);
282 if (it != streams_.end()) {
283 if (stream && !stream->IsReleaseStream()) {
284 deletedStreamIds_.emplace_back(stream->GetStreamId());
285 stream->SetReleaseStream(true);
286 }
287 } else {
288 MEDIA_ERR_LOG("HCaptureSession::RemoveOutputStream Invalid output");
289 return CAMERA_INVALID_SESSION_CFG;
290 }
291 }
292 return CAMERA_OK;
293 }
294
RemoveOutput(StreamType streamType,sptr<IStreamCommon> stream)295 int32_t HCaptureSession::RemoveOutput(StreamType streamType, sptr<IStreamCommon> stream)
296 {
297 int32_t rc = CAMERA_INVALID_ARG;
298 if (stream == nullptr) {
299 MEDIA_ERR_LOG("HCaptureSession::RemoveOutput stream is null");
300 return rc;
301 }
302 std::lock_guard<std::mutex> lock(sessionLock_);
303 if (streamType == StreamType::CAPTURE) {
304 rc = RemoveOutputStream(static_cast<HStreamCapture *>(stream.GetRefPtr()));
305 } else if (streamType == StreamType::REPEAT) {
306 rc = RemoveOutputStream(static_cast<HStreamRepeat *>(stream.GetRefPtr()));
307 } else if (streamType == StreamType::METADATA) {
308 rc = RemoveOutputStream(static_cast<HStreamMetadata *>(stream.GetRefPtr()));
309 }
310 CAMERA_SYSEVENT_STATISTIC(CreateMsg("CaptureSession::RemoveOutput with %d", streamType));
311 return rc;
312 }
313
ValidateSessionInputs()314 int32_t HCaptureSession::ValidateSessionInputs()
315 {
316 if (tempCameraDevices_.empty() && (cameraDevice_ == nullptr || cameraDevice_->IsReleaseCameraDevice())) {
317 MEDIA_ERR_LOG("HCaptureSession::ValidateSessionInputs No inputs present");
318 return CAMERA_INVALID_SESSION_CFG;
319 }
320 return CAMERA_OK;
321 }
322
ValidateSessionOutputs()323 int32_t HCaptureSession::ValidateSessionOutputs()
324 {
325 if (tempStreams_.size() + streams_.size() - deletedStreamIds_.size() == 0) {
326 MEDIA_ERR_LOG("HCaptureSession::ValidateSessionOutputs No outputs present");
327 return CAMERA_INVALID_SESSION_CFG;
328 }
329 return CAMERA_OK;
330 }
331
GetCameraDevice(sptr<HCameraDevice> & device)332 int32_t HCaptureSession::GetCameraDevice(sptr<HCameraDevice> &device)
333 {
334 if (cameraDevice_ != nullptr && !cameraDevice_->IsReleaseCameraDevice()) {
335 MEDIA_DEBUG_LOG("HCaptureSession::GetCameraDevice Camera device has not changed");
336 device = cameraDevice_;
337 return CAMERA_OK;
338 } else if (!tempCameraDevices_.empty()) {
339 device = tempCameraDevices_[0];
340 return CAMERA_OK;
341 }
342
343 MEDIA_ERR_LOG("HCaptureSession::GetCameraDevice Failed because don't have camera device");
344 return CAMERA_INVALID_STATE;
345 }
346
GetCurrentStreamInfos(sptr<HCameraDevice> & device,std::shared_ptr<OHOS::Camera::CameraMetadata> & deviceSettings,std::vector<StreamInfo_V1_1> & streamInfos)347 int32_t HCaptureSession::GetCurrentStreamInfos(sptr<HCameraDevice> &device,
348 std::shared_ptr<OHOS::Camera::CameraMetadata> &deviceSettings,
349 std::vector<StreamInfo_V1_1> &streamInfos)
350 {
351 int32_t rc;
352 int32_t streamId = streamId_;
353 bool isNeedLink;
354 StreamInfo_V1_1 curStreamInfo;
355 sptr<OHOS::HDI::Camera::V1_1::IStreamOperator> streamOperator;
356 if (device != nullptr) {
357 streamOperator = device->GetStreamOperator();
358 }
359 isNeedLink = (device != cameraDevice_);
360 std::lock_guard<std::mutex> lock(streamsLock_);
361 sptr<HStreamCommon> curStream;
362 for (auto item = streams_.begin(); item != streams_.end(); ++item) {
363 curStream = *item;
364 if (curStream && curStream->IsReleaseStream()) {
365 continue;
366 }
367 if (curStream && isNeedLink) {
368 rc = curStream->LinkInput(streamOperator, deviceSettings, streamId);
369 if (rc != CAMERA_OK) {
370 MEDIA_ERR_LOG("HCaptureSession::GetCurrentStreamInfos() Failed to link Output, %{public}d", rc);
371 return rc;
372 }
373 streamId++;
374 }
375 if (curStream) {
376 curStream->SetStreamInfo(curStreamInfo);
377 }
378 streamInfos.push_back(curStreamInfo);
379 }
380 if (streamId != streamId_) {
381 streamId_ = streamId;
382 }
383 return CAMERA_OK;
384 }
385
CreateAndCommitStreams(sptr<HCameraDevice> & device,std::shared_ptr<OHOS::Camera::CameraMetadata> & deviceSettings,std::vector<StreamInfo_V1_1> & streamInfos)386 int32_t HCaptureSession::CreateAndCommitStreams(sptr<HCameraDevice> &device,
387 std::shared_ptr<OHOS::Camera::CameraMetadata> &deviceSettings,
388 std::vector<StreamInfo_V1_1> &streamInfos)
389 {
390 CamRetCode hdiRc = HDI::Camera::V1_0::NO_ERROR;
391 StreamInfo_V1_1 curStreamInfo;
392 sptr<OHOS::HDI::Camera::V1_1::IStreamOperator> streamOperator;
393 if (device != nullptr) {
394 streamOperator = device->GetStreamOperator();
395 }
396 if (streamOperator != nullptr && !streamInfos.empty()) {
397 uint32_t major;
398 uint32_t minor;
399 streamOperator->GetVersion(major, minor);
400 MEDIA_INFO_LOG("streamOperator GetVersion major:%d, minor:%d", major, minor);
401 if (major == 1 && minor == 1) {
402 hdiRc = (CamRetCode)(streamOperator->CreateStreams_V1_1(streamInfos));
403 } else {
404 std::vector<StreamInfo> streamInfos_V1_0;
405 for (auto streamInfo : streamInfos) {
406 streamInfos_V1_0.emplace_back(streamInfo.v1_0);
407 }
408 hdiRc = (CamRetCode)(streamOperator->CreateStreams(streamInfos_V1_0));
409 }
410 } else {
411 MEDIA_INFO_LOG("HCaptureSession::CreateAndCommitStreams(), No new streams to create");
412 }
413 if (streamOperator != nullptr && hdiRc == HDI::Camera::V1_0::NO_ERROR) {
414 std::vector<uint8_t> setting;
415 OHOS::Camera::MetadataUtils::ConvertMetadataToVec(deviceSettings, setting);
416 hdiRc = (CamRetCode)(streamOperator->CommitStreams(NORMAL, setting));
417 if (hdiRc != HDI::Camera::V1_0::NO_ERROR) {
418 MEDIA_ERR_LOG("HCaptureSession::CreateAndCommitStreams(), Failed to commit %{public}d", hdiRc);
419 std::vector<int32_t> streamIds;
420 for (auto item = streamInfos.begin(); item != streamInfos.end(); ++item) {
421 curStreamInfo = *item;
422 streamIds.emplace_back(curStreamInfo.v1_0.streamId_);
423 }
424 MEDIA_DEBUG_LOG("HCaptureSession::CreateAndCommitStreams() streamIds size() = %{public}zu",
425 streamIds.size());
426 for (size_t i = 0; i < streamIds.size(); i++) {
427 MEDIA_DEBUG_LOG("HCaptureSession::CreateAndCommitStreams() streamIds[%{public}zu] = %{public}d",
428 i, streamIds.at(i));
429 }
430 if (!streamIds.empty() && streamOperator->ReleaseStreams(streamIds) != HDI::Camera::V1_0::NO_ERROR) {
431 MEDIA_ERR_LOG("HCaptureSession::CreateAndCommitStreams(), Failed to release streams");
432 }
433 }
434 }
435 return HdiToServiceError(hdiRc);
436 }
437
CheckAndCommitStreams(sptr<HCameraDevice> & device,std::shared_ptr<OHOS::Camera::CameraMetadata> & deviceSettings,std::vector<StreamInfo_V1_1> & allStreamInfos,std::vector<StreamInfo_V1_1> & newStreamInfos)438 int32_t HCaptureSession::CheckAndCommitStreams(sptr<HCameraDevice> &device,
439 std::shared_ptr<OHOS::Camera::CameraMetadata> &deviceSettings,
440 std::vector<StreamInfo_V1_1> &allStreamInfos,
441 std::vector<StreamInfo_V1_1> &newStreamInfos)
442 {
443 return CreateAndCommitStreams(device, deviceSettings, newStreamInfos);
444 }
445
DeleteReleasedStream()446 void HCaptureSession::DeleteReleasedStream()
447 {
448 auto matchFunction = [](const auto& curStream) { return curStream->IsReleaseStream();};
449 captureStreams_.erase(std::remove_if(captureStreams_.begin(), captureStreams_.end(), matchFunction),
450 captureStreams_.end());
451 repeatStreams_.erase(std::remove_if(repeatStreams_.begin(), repeatStreams_.end(), matchFunction),
452 repeatStreams_.end());
453 metadataStreams_.erase(std::remove_if(metadataStreams_.begin(), metadataStreams_.end(), matchFunction),
454 metadataStreams_.end());
455 {
456 std::lock_guard<std::mutex> lock(streamsLock_);
457 sptr<HStreamCommon> curStream;
458 for (auto item = streams_.begin(); item != streams_.end(); ++item) {
459 curStream = *item;
460 if (curStream && curStream->IsReleaseStream()) {
461 curStream->Release();
462 streams_.erase(item--);
463 }
464 }
465 }
466 }
467
RestorePreviousState(sptr<HCameraDevice> & device,bool isCreateReleaseStreams)468 void HCaptureSession::RestorePreviousState(sptr<HCameraDevice> &device, bool isCreateReleaseStreams)
469 {
470 std::vector<StreamInfo_V1_1> streamInfos;
471 StreamInfo_V1_1 streamInfo;
472 std::shared_ptr<OHOS::Camera::CameraMetadata> settings;
473 sptr<HStreamCommon> curStream;
474
475 MEDIA_DEBUG_LOG("HCaptureSession::RestorePreviousState, Restore to previous state");
476 {
477 std::lock_guard<std::mutex> lock(streamsLock_);
478 for (auto item = streams_.begin(); item != streams_.end(); ++item) {
479 curStream = *item;
480 if (curStream && isCreateReleaseStreams && curStream->IsReleaseStream()) {
481 curStream->SetStreamInfo(streamInfo);
482 streamInfos.push_back(streamInfo);
483 }
484 if (curStream) {
485 curStream->SetReleaseStream(false);
486 }
487 }
488 }
489 for (auto item = tempStreams_.begin(); item != tempStreams_.end(); ++item) {
490 curStream = *item;
491 if (curStream) {
492 curStream->Release();
493 }
494 }
495 tempStreams_.clear();
496 deletedStreamIds_.clear();
497 tempCameraDevices_.clear();
498 if (device != nullptr) {
499 device->SetReleaseCameraDevice(false);
500 if (isCreateReleaseStreams) {
501 settings = device->GetSettings();
502 if (settings != nullptr) {
503 CreateAndCommitStreams(device, settings, streamInfos);
504 }
505 }
506 }
507 curState_ = prevState_;
508 }
509
UpdateSessionConfig(sptr<HCameraDevice> & device)510 void HCaptureSession::UpdateSessionConfig(sptr<HCameraDevice> &device)
511 {
512 DeleteReleasedStream();
513 deletedStreamIds_.clear();
514 {
515 std::lock_guard<std::mutex> lock(streamsLock_);
516 sptr<HStreamCommon> curStream;
517 for (auto item = tempStreams_.begin(); item != tempStreams_.end(); ++item) {
518 curStream = *item;
519 if (curStream && curStream->GetStreamType() == StreamType::REPEAT) {
520 repeatStreams_.emplace_back(curStream);
521 } else if (curStream && curStream->GetStreamType() == StreamType::CAPTURE) {
522 captureStreams_.emplace_back(curStream);
523 } else if (curStream && curStream->GetStreamType() == StreamType::METADATA) {
524 metadataStreams_.emplace_back(curStream);
525 }
526 streams_.emplace_back(curStream);
527 }
528 }
529 tempStreams_.clear();
530 if (streamOperatorCallback_ == nullptr) {
531 MEDIA_ERR_LOG("HCaptureSession::UpdateSessionConfig streamOperatorCallback_ is nullptr");
532 return;
533 }
534 streamOperatorCallback_->SetCaptureSession(this);
535 cameraDevice_ = device;
536 curState_ = CaptureSessionState::SESSION_CONFIG_COMMITTED;
537 }
538
HandleCaptureOuputsConfig(sptr<HCameraDevice> & device)539 int32_t HCaptureSession::HandleCaptureOuputsConfig(sptr<HCameraDevice> &device)
540 {
541 int32_t rc;
542 int32_t streamId;
543 std::vector<StreamInfo_V1_1> newStreamInfos;
544 std::vector<StreamInfo_V1_1> allStreamInfos;
545 StreamInfo_V1_1 curStreamInfo;
546 std::shared_ptr<OHOS::Camera::CameraMetadata> settings;
547 sptr<OHOS::HDI::Camera::V1_1::IStreamOperator> streamOperator;
548 sptr<HStreamCommon> curStream;
549 if (device != nullptr) {
550 settings = device->GetSettings();
551 }
552 if (device == nullptr || settings == nullptr) {
553 return CAMERA_UNKNOWN_ERROR;
554 }
555
556 rc = GetCurrentStreamInfos(device, settings, allStreamInfos);
557 if (rc != CAMERA_OK) {
558 MEDIA_ERR_LOG("HCaptureSession::HandleCaptureOuputsConfig() Failed to get streams info, %{public}d", rc);
559 return rc;
560 }
561
562 if (cameraDevice_ != device) {
563 newStreamInfos = allStreamInfos;
564 }
565
566 streamOperator = device->GetStreamOperator();
567 streamId = streamId_;
568 for (auto item = tempStreams_.begin(); item != tempStreams_.end(); ++item) {
569 curStream = *item;
570 if (curStream == nullptr) {
571 MEDIA_ERR_LOG("HCaptureSession::HandleCaptureOuputsConfig() curStream is null");
572 return CAMERA_UNKNOWN_ERROR;
573 }
574 MEDIA_INFO_LOG("HandleCaptureOuputsConfig link Output with streamId %{public}d", streamId);
575 rc = curStream->LinkInput(streamOperator, settings, streamId);
576 if (rc != CAMERA_OK) {
577 MEDIA_ERR_LOG("HCaptureSession::HandleCaptureOuputsConfig() Failed to link Output, %{public}d", rc);
578 return rc;
579 }
580 curStream->SetStreamInfo(curStreamInfo);
581 newStreamInfos.push_back(curStreamInfo);
582 allStreamInfos.push_back(curStreamInfo);
583 streamId++;
584 }
585
586 rc = CheckAndCommitStreams(device, settings, allStreamInfos, newStreamInfos);
587 if (rc == CAMERA_OK) {
588 streamId_ = streamId;
589 }
590 return rc;
591 }
592
CommitConfig()593 int32_t HCaptureSession::CommitConfig()
594 {
595 sptr<HCameraDevice> device = nullptr;
596
597 if (curState_ != CaptureSessionState::SESSION_CONFIG_INPROGRESS) {
598 MEDIA_ERR_LOG("HCaptureSession::CommitConfig() Need to call BeginConfig before committing configuration");
599 return CAMERA_INVALID_STATE;
600 }
601
602 std::lock_guard<std::mutex> lock(sessionLock_);
603 int32_t rc = ValidateSessionInputs();
604 if (rc != CAMERA_OK) {
605 return rc;
606 }
607 rc = ValidateSessionOutputs();
608 if (rc != CAMERA_OK) {
609 return rc;
610 }
611
612 rc = GetCameraDevice(device);
613 if ((rc == CAMERA_OK) && (device == cameraDevice_) && !deletedStreamIds_.empty()) {
614 rc = HdiToServiceError((CamRetCode)(device->GetStreamOperator()->ReleaseStreams(deletedStreamIds_)));
615 }
616
617 if (rc != CAMERA_OK) {
618 MEDIA_ERR_LOG("HCaptureSession::CommitConfig() Failed to commit config. camera device rc: %{public}d", rc);
619 if (device != nullptr && device != cameraDevice_) {
620 CloseDevice(device);
621 }
622 RestorePreviousState(cameraDevice_, false);
623 return rc;
624 }
625
626 rc = HandleCaptureOuputsConfig(device);
627 if (rc != CAMERA_OK) {
628 MEDIA_ERR_LOG("HCaptureSession::CommitConfig() Failed to commit config. rc: %{public}d", rc);
629 if (device != nullptr && device != cameraDevice_) {
630 CloseDevice(device);
631 }
632 RestorePreviousState(cameraDevice_, !deletedStreamIds_.empty());
633 return rc;
634 }
635 if (device != nullptr) {
636 int32_t pid = IPCSkeleton::GetCallingPid();
637 int32_t uid = IPCSkeleton::GetCallingUid();
638 POWERMGR_SYSEVENT_CAMERA_CONNECT(pid, uid, device->GetCameraId().c_str(),
639 GetClientBundle(uid));
640 }
641
642 if (cameraDevice_ != nullptr && device != cameraDevice_) {
643 CloseDevice(cameraDevice_);
644 cameraDevice_ = nullptr;
645 }
646 UpdateSessionConfig(device);
647 return rc;
648 }
649
GetSessionState(CaptureSessionState & sessionState)650 int32_t HCaptureSession::GetSessionState(CaptureSessionState &sessionState)
651 {
652 int32_t rc = CAMERA_OK;
653 sessionState = curState_;
654 return rc;
655 }
656
Start()657 int32_t HCaptureSession::Start()
658 {
659 uint32_t callerToken = IPCSkeleton::GetCallingTokenID();
660 if (callerToken_ != callerToken) {
661 MEDIA_ERR_LOG("Failed to Start Session, createSession token is : %{public}d, now token is %{public}d",
662 callerToken_, callerToken);
663 return CAMERA_OPERATION_NOT_ALLOWED;
664 }
665 if (curState_ != CaptureSessionState::SESSION_CONFIG_COMMITTED) {
666 MEDIA_ERR_LOG("HCaptureSession::Start(), Invalid session state: %{public}d", curState_);
667 return CAMERA_INVALID_STATE;
668 }
669 std::lock_guard<std::mutex> lock(sessionLock_);
670 if (IsValidTokenId(callerToken)) {
671 if (!Security::AccessToken::PrivacyKit::IsAllowedUsingPermission(callerToken, OHOS_PERMISSION_CAMERA)) {
672 MEDIA_ERR_LOG("Start session is not allowed!");
673 return CAMERA_ALLOC_ERROR;
674 }
675 StartUsingPermissionCallback(callerToken, OHOS_PERMISSION_CAMERA);
676 RegisterPermissionCallback(callerToken, OHOS_PERMISSION_CAMERA);
677 }
678 sptr<HStreamRepeat> curStreamRepeat;
679 int32_t rc = CAMERA_OK;
680 for (auto item = repeatStreams_.begin(); item != repeatStreams_.end(); ++item) {
681 curStreamRepeat = static_cast<HStreamRepeat *>((*item).GetRefPtr());
682 if (!curStreamRepeat->IsVideo()) {
683 rc = curStreamRepeat->Start();
684 if (rc != CAMERA_OK) {
685 MEDIA_ERR_LOG("HCaptureSession::Start(), Failed to start preview, rc: %{public}d", rc);
686 break;
687 }
688 }
689 }
690 return rc;
691 }
692
Stop()693 int32_t HCaptureSession::Stop()
694 {
695 int32_t rc = CAMERA_OK;
696 sptr<HStreamRepeat> curStreamRepeat;
697 if (curState_ != CaptureSessionState::SESSION_CONFIG_COMMITTED) {
698 return CAMERA_INVALID_STATE;
699 }
700 std::lock_guard<std::mutex> lock(sessionLock_);
701 for (auto item = repeatStreams_.begin(); item != repeatStreams_.end(); ++item) {
702 curStreamRepeat = static_cast<HStreamRepeat *>((*item).GetRefPtr());
703 if (!curStreamRepeat->IsVideo()) {
704 rc = curStreamRepeat->Stop();
705 if (rc != CAMERA_OK) {
706 MEDIA_ERR_LOG("HCaptureSession::Stop(), Failed to stop preview, rc: %{public}d", rc);
707 break;
708 }
709 }
710 }
711
712 return rc;
713 }
714
ClearCaptureSession(pid_t pid)715 void HCaptureSession::ClearCaptureSession(pid_t pid)
716 {
717 MEDIA_DEBUG_LOG("ClearCaptureSession: camera stub services(%{public}zu) pid(%{public}d).", session_.size(), pid);
718 auto it = session_.find(pid);
719 if (it != session_.end()) {
720 session_.erase(it);
721 }
722 MEDIA_DEBUG_LOG("ClearCaptureSession: camera stub services(%{public}zu).", session_.size());
723 }
724
ReleaseStreams()725 void HCaptureSession::ReleaseStreams()
726 {
727 std::vector<int32_t> streamIds;
728 {
729 std::lock_guard<std::mutex> lock(streamsLock_);
730 sptr<HStreamCommon> curStream;
731 for (auto item = streams_.begin(); item != streams_.end(); ++item) {
732 curStream = *item;
733 if (curStream) {
734 streamIds.emplace_back(curStream->GetStreamId());
735 curStream->Release();
736 }
737 }
738 MEDIA_DEBUG_LOG("HCaptureSession::ReleaseStreams() streamIds size() = %{public}zu", streamIds.size());
739 for (size_t i = 0; i < streamIds.size(); i++) {
740 MEDIA_DEBUG_LOG("HCaptureSession::ReleaseStreams() streamIds[%{public}zu] = %{public}d",
741 i, streamIds.at(i));
742 }
743 streams_.clear();
744 }
745 repeatStreams_.clear();
746 captureStreams_.clear();
747 metadataStreams_.clear();
748 if ((cameraDevice_ != nullptr) && (cameraDevice_->GetStreamOperator() != nullptr) && !streamIds.empty()) {
749 cameraDevice_->GetStreamOperator()->ReleaseStreams(streamIds);
750 }
751 }
752
ReleaseInner()753 int32_t HCaptureSession::ReleaseInner()
754 {
755 MEDIA_INFO_LOG("HCaptureSession::ReleaseInner enter");
756 return Release(pid_);
757 }
758
Release(pid_t pid)759 int32_t HCaptureSession::Release(pid_t pid)
760 {
761 std::lock_guard<std::mutex> lock(sessionLock_);
762 pid = pid != 0 ? pid : IPCSkeleton::GetCallingPid();
763 MEDIA_INFO_LOG("HCaptureSession::Release pid(%{public}d).", pid);
764 auto it = session_.find(pid);
765 if (it == session_.end()) {
766 MEDIA_DEBUG_LOG("HCaptureSession::Release session for pid(%{public}d) already released.", pid);
767 return CAMERA_OK;
768 }
769 ReleaseStreams();
770 tempCameraDevices_.clear();
771 if (streamOperatorCallback_ != nullptr) {
772 streamOperatorCallback_->SetCaptureSession(nullptr);
773 streamOperatorCallback_ = nullptr;
774 }
775 if (cameraDevice_ != nullptr) {
776 CloseDevice(cameraDevice_);
777 POWERMGR_SYSEVENT_CAMERA_DISCONNECT(cameraDevice_->GetCameraId().c_str());
778 cameraDevice_ = nullptr;
779 }
780 if (IsValidTokenId(callerToken_)) {
781 StopUsingPermissionCallback(callerToken_, OHOS_PERMISSION_CAMERA);
782 UnregisterPermissionCallback(callerToken_);
783 }
784 tempStreams_.clear();
785 ClearCaptureSession(pid);
786 sessionCallback_ = nullptr;
787 cameraHostManager_ = nullptr;
788 return CAMERA_OK;
789 }
790
RegisterPermissionCallback(const uint32_t callingTokenId,const std::string permissionName)791 void HCaptureSession::RegisterPermissionCallback(const uint32_t callingTokenId, const std::string permissionName)
792 {
793 Security::AccessToken::PermStateChangeScope scopeInfo;
794 scopeInfo.permList = {permissionName};
795 scopeInfo.tokenIDs = {callingTokenId};
796 callbackPtr_ = std::make_shared<PermissionStatusChangeCb>(scopeInfo);
797 callbackPtr_->SetCaptureSession(this);
798 MEDIA_DEBUG_LOG("after tokenId:%{public}d register", callingTokenId);
799 int32_t res = Security::AccessToken::AccessTokenKit::RegisterPermStateChangeCallback(callbackPtr_);
800 if (res != CAMERA_OK) {
801 MEDIA_ERR_LOG("RegisterPermStateChangeCallback failed.");
802 }
803 }
804
UnregisterPermissionCallback(const uint32_t callingTokenId)805 void HCaptureSession::UnregisterPermissionCallback(const uint32_t callingTokenId)
806 {
807 if (callbackPtr_ == nullptr) {
808 MEDIA_ERR_LOG("callbackPtr_ is null.");
809 return;
810 }
811 int32_t res = Security::AccessToken::AccessTokenKit::UnRegisterPermStateChangeCallback(callbackPtr_);
812 if (res != CAMERA_OK) {
813 MEDIA_ERR_LOG("UnRegisterPermStateChangeCallback failed.");
814 }
815 if (callbackPtr_) {
816 callbackPtr_->SetCaptureSession(nullptr);
817 callbackPtr_ = nullptr;
818 }
819 MEDIA_DEBUG_LOG("after tokenId:%{public}d unregister", callingTokenId);
820 }
821
DestroyStubObjectForPid(pid_t pid)822 void HCaptureSession::DestroyStubObjectForPid(pid_t pid)
823 {
824 MEDIA_DEBUG_LOG("camera stub services(%{public}zu) pid(%{public}d).", session_.size(), pid);
825 sptr<HCaptureSession> session;
826
827 auto it = session_.find(pid);
828 if (it != session_.end()) {
829 if (it->second != nullptr) {
830 session = it->second;
831 session->Release(pid);
832 }
833 }
834 MEDIA_DEBUG_LOG("camera stub services(%{public}zu).", session_.size());
835 }
836
SetCallback(sptr<ICaptureSessionCallback> & callback)837 int32_t HCaptureSession::SetCallback(sptr<ICaptureSessionCallback> &callback)
838 {
839 if (callback == nullptr) {
840 MEDIA_ERR_LOG("HCaptureSession::SetCallback callback is null");
841 return CAMERA_INVALID_ARG;
842 }
843 std::lock_guard<std::mutex> lock(sessionCallbackLock_);
844 sessionCallback_ = callback;
845 return CAMERA_OK;
846 }
847
GetSessionState()848 std::string HCaptureSession::GetSessionState()
849 {
850 std::map<CaptureSessionState, std::string>::const_iterator iter =
851 sessionState_.find(curState_);
852 if (iter != sessionState_.end()) {
853 return iter->second;
854 }
855 return nullptr;
856 }
857
CameraSessionSummary(std::string & dumpString)858 void HCaptureSession::CameraSessionSummary(std::string& dumpString)
859 {
860 dumpString += "# Number of Camera clients:[" + std::to_string(session_.size()) + "]:\n";
861 }
862
dumpSessions(std::string & dumpString)863 void HCaptureSession::dumpSessions(std::string& dumpString)
864 {
865 for (auto it = session_.begin(); it != session_.end(); it++) {
866 if (it->second != nullptr) {
867 sptr<HCaptureSession> session = it->second;
868 dumpString += "No. of sessions for client:[" + std::to_string(1) + "]:\n";
869 session->dumpSessionInfo(dumpString);
870 }
871 }
872 }
873
dumpSessionInfo(std::string & dumpString)874 void HCaptureSession::dumpSessionInfo(std::string& dumpString)
875 {
876 dumpString += "Client pid:[" + std::to_string(pid_)
877 + "] Client uid:[" + std::to_string(uid_) + "]:\n";
878 dumpString += "session state:[" + GetSessionState() + "]:\n";
879 if (cameraDevice_ != nullptr) {
880 dumpString += "session Camera Id:[" + cameraDevice_->GetCameraId() + "]:\n";
881 dumpString += "session Camera release status:["
882 + std::to_string(cameraDevice_->IsReleaseCameraDevice()) + "]:\n";
883 }
884 for (const auto& stream : captureStreams_) {
885 stream->DumpStreamInfo(dumpString);
886 }
887 for (const auto& stream : repeatStreams_) {
888 stream->DumpStreamInfo(dumpString);
889 }
890 for (const auto& stream : metadataStreams_) {
891 stream->DumpStreamInfo(dumpString);
892 }
893 }
894
StartUsingPermissionCallback(const uint32_t callingTokenId,const std::string permissionName)895 void HCaptureSession::StartUsingPermissionCallback(const uint32_t callingTokenId, const std::string permissionName)
896 {
897 if (cameraUseCallbackPtr_) {
898 MEDIA_ERR_LOG("has StartUsingPermissionCallback!");
899 return;
900 }
901 cameraUseCallbackPtr_ = std::make_shared<CameraUseStateChangeCb>();
902 cameraUseCallbackPtr_->SetCaptureSession(this);
903 int32_t res = Security::AccessToken::PrivacyKit::StartUsingPermission(
904 callingTokenId, permissionName, cameraUseCallbackPtr_);
905 MEDIA_DEBUG_LOG("after StartUsingPermissionCallback tokenId:%{public}d", callingTokenId);
906 if (res != CAMERA_OK) {
907 MEDIA_ERR_LOG("StartUsingPermissionCallback failed.");
908 }
909 }
910
StopUsingPermissionCallback(const uint32_t callingTokenId,const std::string permissionName)911 void HCaptureSession::StopUsingPermissionCallback(const uint32_t callingTokenId, const std::string permissionName)
912 {
913 MEDIA_DEBUG_LOG("enter StopUsingPermissionCallback tokenId:%{public}d", callingTokenId);
914 int32_t res = Security::AccessToken::PrivacyKit::StopUsingPermission(callingTokenId, permissionName);
915 if (res != CAMERA_OK) {
916 MEDIA_ERR_LOG("StopUsingPermissionCallback failed.");
917 }
918 if (cameraUseCallbackPtr_) {
919 cameraUseCallbackPtr_->SetCaptureSession(nullptr);
920 cameraUseCallbackPtr_ = nullptr;
921 }
922 }
923
SetDeviceOperatorsCallback(wptr<IDeviceOperatorsCallback> callback)924 int32_t HCaptureSession::SetDeviceOperatorsCallback(wptr<IDeviceOperatorsCallback> callback)
925 {
926 if (callback.promote() == nullptr) {
927 MEDIA_ERR_LOG("HCameraDevice::SetDeviceOperatorsCallback callback is null");
928 return CAMERA_INVALID_ARG;
929 }
930 deviceOperatorsCallback_ = callback;
931 return CAMERA_OK;
932 }
933
~PermissionStatusChangeCb()934 PermissionStatusChangeCb::~PermissionStatusChangeCb()
935 {
936 captureSession_ = nullptr;
937 }
938
SetCaptureSession(sptr<HCaptureSession> captureSession)939 void PermissionStatusChangeCb::SetCaptureSession(sptr<HCaptureSession> captureSession)
940 {
941 captureSession_ = captureSession;
942 }
943
PermStateChangeCallback(Security::AccessToken::PermStateChangeInfo & result)944 void PermissionStatusChangeCb::PermStateChangeCallback(Security::AccessToken::PermStateChangeInfo& result)
945 {
946 auto item = captureSession_.promote();
947 if ((result.permStateChangeType == 0) && (item != nullptr)) {
948 item->ReleaseInner();
949 }
950 }
951
~CameraUseStateChangeCb()952 CameraUseStateChangeCb::~CameraUseStateChangeCb()
953 {
954 captureSession_ = nullptr;
955 }
956
SetCaptureSession(sptr<HCaptureSession> captureSession)957 void CameraUseStateChangeCb::SetCaptureSession(sptr<HCaptureSession> captureSession)
958 {
959 captureSession_ = captureSession;
960 }
961
StateChangeNotify(Security::AccessToken::AccessTokenID tokenId,bool isShowing)962 void CameraUseStateChangeCb::StateChangeNotify(Security::AccessToken::AccessTokenID tokenId, bool isShowing)
963 {
964 MEDIA_INFO_LOG("enter CameraUseStateChangeNotify tokenId:%{public}d", tokenId);
965 auto item = captureSession_.promote();
966 if ((isShowing == false) && (item != nullptr)) {
967 item->ReleaseInner();
968 }
969 }
970
971
StreamOperatorCallback(sptr<HCaptureSession> session)972 StreamOperatorCallback::StreamOperatorCallback(sptr<HCaptureSession> session)
973 {
974 captureSession_ = session;
975 }
976
~StreamOperatorCallback()977 StreamOperatorCallback::~StreamOperatorCallback()
978 {
979 captureSession_ = nullptr;
980 }
981
GetStreamByStreamID(int32_t streamId)982 sptr<HStreamCommon> StreamOperatorCallback::GetStreamByStreamID(int32_t streamId)
983 {
984 sptr<HStreamCommon> result = nullptr;
985 if (captureSession_ != nullptr) {
986 std::lock_guard<std::mutex> lock(captureSession_->streamsLock_);
987
988 std::vector<sptr<HStreamCommon>>::iterator it = std::find_if(captureSession_->streams_.begin(),
989 captureSession_->streams_.end(),
990 [&streamId](const auto& item) { return item->GetStreamId() == streamId; });
991 if (it == captureSession_->streams_.end()) {
992 return result;
993 }
994 result = *it;
995 }
996 return result;
997 }
998
OnCaptureStarted(int32_t captureId,const std::vector<int32_t> & streamIds)999 int32_t StreamOperatorCallback::OnCaptureStarted(int32_t captureId,
1000 const std::vector<int32_t> &streamIds)
1001 {
1002 MEDIA_DEBUG_LOG("StreamOperatorCallback::OnCaptureStarted");
1003 sptr<HStreamCommon> curStream;
1004 std::lock_guard<std::mutex> lock(cbMutex_);
1005 for (auto item = streamIds.begin(); item != streamIds.end(); ++item) {
1006 curStream = GetStreamByStreamID(*item);
1007 if (curStream == nullptr) {
1008 MEDIA_ERR_LOG("StreamOperatorCallback::OnCaptureStarted StreamId: %{public}d not found", *item);
1009 return CAMERA_INVALID_ARG;
1010 } else if (curStream->GetStreamType() == StreamType::REPEAT) {
1011 static_cast<HStreamRepeat *>(curStream.GetRefPtr())->OnFrameStarted();
1012 } else if (curStream->GetStreamType() == StreamType::CAPTURE) {
1013 static_cast<HStreamCapture *>(curStream.GetRefPtr())->OnCaptureStarted(captureId);
1014 }
1015 }
1016 return CAMERA_OK;
1017 }
1018
OnCaptureEnded(int32_t captureId,const std::vector<CaptureEndedInfo> & infos)1019 int32_t StreamOperatorCallback::OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo>& infos)
1020 {
1021 MEDIA_DEBUG_LOG("StreamOperatorCallback::OnCaptureEnded");
1022 sptr<HStreamCommon> curStream;
1023 CaptureEndedInfo captureInfo;
1024 std::lock_guard<std::mutex> lock(cbMutex_);
1025 for (auto item = infos.begin(); item != infos.end(); ++item) {
1026 captureInfo = *item;
1027 curStream = GetStreamByStreamID(captureInfo.streamId_);
1028 if (curStream == nullptr) {
1029 MEDIA_ERR_LOG("StreamOperatorCallback::OnCaptureEnded StreamId: %{public}d not found."
1030 " Framecount: %{public}d", captureInfo.streamId_, captureInfo.frameCount_);
1031 return CAMERA_INVALID_ARG;
1032 } else if (curStream->GetStreamType() == StreamType::REPEAT) {
1033 static_cast<HStreamRepeat *>(curStream.GetRefPtr())->OnFrameEnded(captureInfo.frameCount_);
1034 } else if (curStream->GetStreamType() == StreamType::CAPTURE) {
1035 static_cast<HStreamCapture *>(curStream.GetRefPtr())->OnCaptureEnded(captureId, captureInfo.frameCount_);
1036 }
1037 }
1038 return CAMERA_OK;
1039 }
1040
OnCaptureError(int32_t captureId,const std::vector<CaptureErrorInfo> & infos)1041 int32_t StreamOperatorCallback::OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo>& infos)
1042 {
1043 MEDIA_DEBUG_LOG("StreamOperatorCallback::OnCaptureError");
1044 sptr<HStreamCommon> curStream;
1045 CaptureErrorInfo errInfo;
1046 std::lock_guard<std::mutex> lock(cbMutex_);
1047 for (auto item = infos.begin(); item != infos.end(); ++item) {
1048 errInfo = *item;
1049 curStream = GetStreamByStreamID(errInfo.streamId_);
1050 if (curStream == nullptr) {
1051 MEDIA_ERR_LOG("StreamOperatorCallback::OnCaptureError StreamId: %{public}d not found."
1052 " Error: %{public}d", errInfo.streamId_, errInfo.error_);
1053 return CAMERA_INVALID_ARG;
1054 } else if (curStream->GetStreamType() == StreamType::REPEAT) {
1055 static_cast<HStreamRepeat *>(curStream.GetRefPtr())->OnFrameError(errInfo.error_);
1056 } else if (curStream->GetStreamType() == StreamType::CAPTURE) {
1057 static_cast<HStreamCapture *>(curStream.GetRefPtr())->OnCaptureError(captureId, errInfo.error_);
1058 }
1059 }
1060 return CAMERA_OK;
1061 }
1062
OnFrameShutter(int32_t captureId,const std::vector<int32_t> & streamIds,uint64_t timestamp)1063 int32_t StreamOperatorCallback::OnFrameShutter(int32_t captureId,
1064 const std::vector<int32_t> &streamIds,
1065 uint64_t timestamp)
1066 {
1067 MEDIA_DEBUG_LOG("StreamOperatorCallback::OnFrameShutter");
1068 sptr<HStreamCommon> curStream;
1069 std::lock_guard<std::mutex> lock(cbMutex_);
1070 for (auto item = streamIds.begin(); item != streamIds.end(); ++item) {
1071 curStream = GetStreamByStreamID(*item);
1072 if ((curStream != nullptr) && (curStream->GetStreamType() == StreamType::CAPTURE)) {
1073 static_cast<HStreamCapture *>(curStream.GetRefPtr())->OnFrameShutter(captureId, timestamp);
1074 } else {
1075 MEDIA_ERR_LOG("StreamOperatorCallback::OnFrameShutter StreamId: %{public}d not found", *item);
1076 return CAMERA_INVALID_ARG;
1077 }
1078 }
1079 return CAMERA_OK;
1080 }
1081
SetCaptureSession(sptr<HCaptureSession> captureSession)1082 void StreamOperatorCallback::SetCaptureSession(sptr<HCaptureSession> captureSession)
1083 {
1084 std::lock_guard<std::mutex> lock(cbMutex_);
1085 captureSession_ = captureSession;
1086 }
1087 } // namespace CameraStandard
1088 } // namespace OHOS
1089