• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "capture_request.h"
17 #include <sys/time.h>
18 
19 namespace OHOS::Camera {
CaptureRequest(const int32_t id,const int32_t n,CaptureMeta & setting,bool needReport,bool isContinuous)20 CaptureRequest::CaptureRequest(const int32_t id,
21                                const int32_t n,
22                                CaptureMeta& setting,
23                                bool needReport,
24                                bool isContinuous)
25 {
26     semp_ = std::make_unique<RequestSemaphore>(n);
27     semr_ = std::make_unique<RequestSemaphore>(n);
28     captureId_ = id;
29     settings_ = setting;
30     needShutterCallback_ = needReport;
31     isContinuous_ = isContinuous;
32 }
33 
~CaptureRequest()34 CaptureRequest::~CaptureRequest()
35 {
36     CAMERA_LOGV("enter");
37     owners_.clear();
38 }
39 
AddOwner(const std::weak_ptr<IStream> & owner)40 RetCode CaptureRequest::AddOwner(const std::weak_ptr<IStream>& owner)
41 {
42     auto stream = owner.lock();
43     CHECK_IF_PTR_NULL_RETURN_VALUE(stream, RC_ERROR);
44 
45     std::lock_guard<std::mutex> l(lock_);
46     owners_[stream->GetStreamId()] = stream;
47     ownerCount_++;
48 
49     return RC_OK;
50 }
51 
Process(const int32_t id)52 RetCode CaptureRequest::Process(const int32_t id)
53 {
54     auto stream = owners_[id].lock();
55     CHECK_IF_PTR_NULL_RETURN_VALUE(stream, RC_ERROR);
56 
57     semp_->Sync();
58     return stream->Capture(shared_from_this());
59 }
60 
OnResult(const int32_t id)61 RetCode CaptureRequest::OnResult(const int32_t id)
62 {
63     auto stream = owners_[id].lock();
64     CHECK_IF_PTR_NULL_RETURN_VALUE(stream, RC_ERROR);
65 
66     semr_->Sync();
67     return stream->OnFrame(shared_from_this());
68 }
69 
DisableSync()70 void CaptureRequest::DisableSync()
71 {
72     std::lock_guard<std::mutex> l(lock_);
73     ownerCount_ = 1;
74     semp_->needSync = false;
75     semp_->cv_.notify_all();
76     semr_->needSync = false;
77     semr_->cv_.notify_all();
78 }
79 
GetOwnerCount()80 uint32_t CaptureRequest::GetOwnerCount()
81 {
82     std::lock_guard<std::mutex> l(lock_);
83     return ownerCount_;
84 }
85 
Cancel()86 RetCode CaptureRequest::Cancel()
87 {
88     for (auto it : owners_) {
89         auto stream = it.second.lock();
90         if (stream == nullptr) {
91             continue;
92         }
93         needCancel_ = true;
94         stream->CancelRequest(shared_from_this());
95     }
96     return RC_OK;
97 }
98 
GetBeginTime() const99 uint64_t CaptureRequest::GetBeginTime() const
100 {
101     return semp_->timestamp_;
102 }
103 
GetEndTime() const104 uint64_t CaptureRequest::GetEndTime() const
105 {
106     return semr_->timestamp_;
107 }
108 
NeedShutterCallback() const109 bool CaptureRequest::NeedShutterCallback() const
110 {
111     return needShutterCallback_;
112 }
113 
IsContinous() const114 bool CaptureRequest::IsContinous() const
115 {
116     return isContinuous_;
117 }
118 
GetCaptureId() const119 int32_t CaptureRequest::GetCaptureId() const
120 {
121     return captureId_;
122 }
123 
AttachBuffer(std::shared_ptr<IBuffer> & b)124 void CaptureRequest::AttachBuffer(std::shared_ptr<IBuffer>& b)
125 {
126     buffer_ = b;
127 }
128 
GetAttachedBuffer() const129 std::shared_ptr<IBuffer> CaptureRequest::GetAttachedBuffer() const
130 {
131     return buffer_;
132 }
133 
NeedCancel() const134 bool CaptureRequest::NeedCancel() const
135 {
136     return needCancel_.load();
137 }
138 
SetFirstRequest(bool b)139 void CaptureRequest::SetFirstRequest(bool b)
140 {
141     isFirstRequest_ = b;
142 }
143 
IsFirstOne() const144 bool CaptureRequest::IsFirstOne() const
145 {
146     return isFirstRequest_;
147 }
148 
GetCaptureSetting() const149 CaptureMeta CaptureRequest::GetCaptureSetting() const
150 {
151     return settings_;
152 }
153 
RequestSemaphore(const int32_t n)154 CaptureRequest::RequestSemaphore::RequestSemaphore(const int32_t n)
155 {
156     ownerCount_ = n;
157 }
158 
~RequestSemaphore()159 CaptureRequest::RequestSemaphore::~RequestSemaphore()
160 {
161     cv_.notify_all();
162 }
163 
Sync()164 void CaptureRequest::RequestSemaphore::Sync()
165 {
166     if (!needSync) {
167         return;
168     }
169 
170     syncCount_++;
171     if (syncCount_ < ownerCount_) {
172         std::unique_lock<std::mutex> l(sem_);
173         cv_.wait(l);
174     } else {
175         std::unique_lock<std::mutex> l(sem_);
176         syncCount_ = 0;
177         timestamp_ = GenerateTimeStamp();
178         cv_.notify_all();
179     }
180 }
181 
GenerateTimeStamp()182 uint64_t CaptureRequest::RequestSemaphore::GenerateTimeStamp()
183 {
184     struct timeval tv;
185     gettimeofday(&tv, NULL);
186     uint64_t time = static_cast<uint64_t>(tv.tv_sec) * 1000 * 1000 + tv.tv_usec; // 1000:microsecond
187     return time;
188 }
189 } // namespace OHOS::Camera
190