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 "stream_still_capture.h"
17 #include "offline_stream.h"
18
19 namespace OHOS::Camera {
StreamStillCapture(const int32_t id,const VdiStreamIntent type,std::shared_ptr<IPipelineCore> & p,std::shared_ptr<CaptureMessageOperator> & m)20 StreamStillCapture::StreamStillCapture(const int32_t id,
21 const VdiStreamIntent type,
22 std::shared_ptr<IPipelineCore>& p,
23 std::shared_ptr<CaptureMessageOperator>& m)
24 : StreamBase(id, type, p, m)
25 {
26 CAMERA_LOGV("enter");
27 }
28
~StreamStillCapture()29 StreamStillCapture::~StreamStillCapture()
30 {
31 CAMERA_LOGV("enter");
32 }
33
HandleResult(std::shared_ptr<IBuffer> & buffer)34 void StreamStillCapture::HandleResult(std::shared_ptr<IBuffer>& buffer)
35 {
36 if (state_ == STREAM_STATE_OFFLINE) {
37 std::lock_guard<std::mutex> l(offlineLock_);
38 auto stream = offlineStream.lock();
39 if (stream == nullptr) {
40 return;
41 }
42 stream->ReceiveOfflineBuffer(buffer);
43 }
44
45 StreamBase::HandleResult(buffer);
46 return;
47 }
48
Capture(const std::shared_ptr<CaptureRequest> & request)49 RetCode StreamStillCapture::Capture(const std::shared_ptr<CaptureRequest>& request)
50 {
51 if (state_ == STREAM_STATE_OFFLINE) {
52 return RC_OK;
53 }
54
55 CAMERA_LOGD("start StreamStillCapture::Capture! ");
56 CameraHalTimeSysevent::WriteTimeStatisicEvent(CameraHalTimeSysevent::GetEventName(TIME_OF_CAPTURE));
57 return StreamBase::Capture(request);
58 }
59
ChangeToOfflineStream(std::shared_ptr<OfflineStream> offlineStream)60 RetCode StreamStillCapture::ChangeToOfflineStream(std::shared_ptr<OfflineStream> offlineStream)
61 {
62 auto context = std::make_shared<OfflineStreamContext>();
63 CHECK_IF_PTR_NULL_RETURN_VALUE(context, RC_ERROR);
64 context->streamInfo = streamConfig_;
65 context->tunnel = tunnel_;
66 CHECK_IF_PTR_NULL_RETURN_VALUE(context->tunnel, RC_ERROR);
67 context->bufferPool = bufferPool_;
68 CHECK_IF_PTR_NULL_RETURN_VALUE(context->bufferPool, RC_ERROR);
69 context->pipeline = pipeline_;
70 CHECK_IF_PTR_NULL_RETURN_VALUE(context->pipeline.lock(), RC_ERROR);
71
72 {
73 std::unique_lock<std::mutex> l(wtLock_);
74 waitingList_.clear();
75 }
76
77 {
78 std::lock_guard<std::mutex> inTransitListLock(tsLock_);
79 context->restRequests = inTransitList_;
80 state_ = STREAM_STATE_OFFLINE;
81 CAMERA_LOGI("there is/are %{public}u request(s) left in stream %{public}d.",
82 context->restRequests.size(), streamId_);
83 }
84
85 std::lock_guard<std::mutex> offlineStreamLock(offlineLock_);
86 RetCode rc = offlineStream->Init(context);
87 if (rc != RC_OK) {
88 CAMERA_LOGE("offline stream [id:%{public}d] init failed", streamId_);
89 return RC_ERROR;
90 }
91
92 return RC_OK;
93 }
94
StopStream()95 RetCode StreamStillCapture::StopStream()
96 {
97 CHECK_IF_PTR_NULL_RETURN_VALUE(pipeline_, RC_ERROR);
98 if (state_ == STREAM_STATE_IDLE) {
99 CAMERA_LOGI("stream [id:%{public}d], no need to stop", streamId_);
100 return RC_OK;
101 }
102 CAMERA_LOGI("stop stream [id:%{public}d] begin", streamId_);
103
104 if (state_ != STREAM_STATE_OFFLINE) {
105 state_ = STREAM_STATE_IDLE;
106 }
107
108 tunnel_->NotifyStop();
109 cv_.notify_one();
110 if (handler_ != nullptr) {
111 handler_->join();
112 }
113
114 if (!waitingList_.empty()) {
115 auto request = waitingList_.front();
116 if (request != nullptr && request->IsContinous()) {
117 request->Cancel();
118 }
119 }
120 {
121 std::unique_lock<std::mutex> l(wtLock_);
122 waitingList_.clear();
123 }
124
125 RetCode rc = pipeline_->Flush({streamId_});
126 if (rc != RC_OK) {
127 CAMERA_LOGE("stream [id:%{public}d], pipeline flush failed", streamId_);
128 return RC_ERROR;
129 }
130
131 if (state_ != STREAM_STATE_OFFLINE) {
132 CAMERA_LOGI("stream [id:%{public}d] is waiting buffers returned", streamId_);
133 tunnel_->WaitForAllBufferReturned();
134 CAMERA_LOGI("stream [id:%{public}d], all buffers are returned.", streamId_);
135 }
136
137 rc = pipeline_->Stop({streamId_});
138 if (rc != RC_OK) {
139 CAMERA_LOGE("stream [id:%{public}d], pipeline stop failed", streamId_);
140 return RC_ERROR;
141 }
142
143 CAMERA_LOGI("stop stream [id:%{public}d] end", streamId_);
144 isFirstRequest = true;
145
146 if (state_ != STREAM_STATE_OFFLINE) {
147 inTransitList_.clear();
148 tunnel_->CleanBuffers();
149 bufferPool_->ClearBuffers();
150 }
151 return RC_OK;
152 }
153
IsRunning() const154 bool StreamStillCapture::IsRunning() const
155 {
156 return state_ == STREAM_STATE_BUSY || state_ == STREAM_STATE_OFFLINE;
157 }
158
159 REGISTERSTREAM(StreamStillCapture, {"STILL_CAPTURE"});
160 } // namespace OHOS::Camera
161