1 /*
2  * Copyright (c) 2021 - 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 "offline_stream_operator_vdi_impl.h"
17 #include "watchdog.h"
18 
19 namespace OHOS::Camera {
OfflineStreamOperatorVdiImpl()20 OfflineStreamOperatorVdiImpl::OfflineStreamOperatorVdiImpl()
21 {
22     CAMERA_LOGV("ctor, instance");
23 }
24 
~OfflineStreamOperatorVdiImpl()25 OfflineStreamOperatorVdiImpl::~OfflineStreamOperatorVdiImpl()
26 {
27     CAMERA_LOGV("dtor, instance");
28     offlineStreamMap_.clear();
29 }
30 
CancelCapture(int32_t captureId)31 int32_t OfflineStreamOperatorVdiImpl::CancelCapture(int32_t captureId)
32 {
33     CHECK_IF_EQUAL_RETURN_VALUE(captureId < 0, true, INVALID_ARGUMENT);
34 
35     PLACE_A_SELFKILL_WATCHDOG;
36     DFX_LOCAL_HITRACE_BEGIN;
37 
38     std::shared_ptr<OfflineStream> stream = FindStreamByCaptureId(captureId);
39     if (stream == nullptr) {
40         CAMERA_LOGD("can't find stream by captureId %{public}d, buffer all returned.", captureId);
41         return VDI::Camera::V1_0::NO_ERROR;
42     }
43     RetCode ret = stream->CancelCapture(captureId);
44     if (ret != RC_OK) {
45         CAMERA_LOGE("cancel captureId %{public}d failed", captureId);
46         return DEVICE_ERROR;
47     }
48 
49     DFX_LOCAL_HITRACE_END;
50     return VDI::Camera::V1_0::NO_ERROR;
51 }
52 
ReleaseStreams(const std::vector<int32_t> & streamIds)53 int32_t OfflineStreamOperatorVdiImpl::ReleaseStreams(const std::vector<int32_t> &streamIds)
54 {
55     PLACE_A_SELFKILL_WATCHDOG;
56     DFX_LOCAL_HITRACE_BEGIN;
57 
58     for (auto it : streamIds) {
59         if (offlineStreamMap_.find(it) != offlineStreamMap_.end()) {
60             RetCode ret = offlineStreamMap_[it]->Release();
61             if (ret != RC_OK) {
62                 CAMERA_LOGE("release stream %{public}d failed", it);
63             }
64             {
65                 std::lock_guard<std::mutex> l(lock_);
66                 offlineStreamMap_.erase(it);
67             }
68         }
69     }
70 
71     DFX_LOCAL_HITRACE_END;
72     return VDI::Camera::V1_0::NO_ERROR;
73 }
74 
Release()75 int32_t OfflineStreamOperatorVdiImpl::Release()
76 {
77     PLACE_A_SELFKILL_WATCHDOG;
78     DFX_LOCAL_HITRACE_BEGIN;
79 
80     {
81         std::lock_guard<std::mutex> l(lock_);
82         for (auto it = offlineStreamMap_.begin(); it != offlineStreamMap_.end(); it++) {
83             it->second->Release();
84         }
85 
86         offlineStreamMap_.clear();
87     }
88 
89     DFX_LOCAL_HITRACE_END;
90     return VDI::Camera::V1_0::NO_ERROR;
91 }
92 
CommitOfflineStream(const std::shared_ptr<OfflineStream> & of)93 RetCode OfflineStreamOperatorVdiImpl::CommitOfflineStream(const std::shared_ptr<OfflineStream> &of)
94 {
95     CHECK_IF_PTR_NULL_RETURN_VALUE(of, RC_ERROR);
96     {
97         std::lock_guard<std::mutex> l(lock_);
98         offlineStreamMap_[of->GetStreamId()] = of;
99     }
100     return RC_OK;
101 }
102 
FindStreamByCaptureId(int32_t captureId)103 std::shared_ptr<OfflineStream> OfflineStreamOperatorVdiImpl::FindStreamByCaptureId(int32_t captureId)
104 {
105     std::shared_ptr<OfflineStream> stream = nullptr;
106     {
107         std::lock_guard<std::mutex> l(lock_);
108         for (auto it = offlineStreamMap_.begin(); it != offlineStreamMap_.end(); it++) {
109             if (it->second->CheckCaptureIdExist(captureId)) {
110                 stream = it->second;
111             }
112         }
113     }
114     return stream;
115 }
116 } // end namespace OHOS::Camera
117