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 StreamIntent type,std::shared_ptr<IPipelineCore> & p,std::shared_ptr<CaptureMessageOperator> & m)20 StreamStillCapture::StreamStillCapture(const int32_t id,
21 const StreamIntent 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 std::lock_guard<std::mutex> l(offlineLock_);
52 if (state_ == STREAM_STATE_OFFLINE) {
53 return RC_OK;
54 }
55
56 return StreamBase::Capture(request);
57 }
58
ChangeToOfflineStream(std::shared_ptr<OfflineStream> offlineStream)59 RetCode StreamStillCapture::ChangeToOfflineStream(std::shared_ptr<OfflineStream> offlineStream)
60 {
61 auto context = std::make_shared<OfflineStreamContext>();
62 CHECK_IF_PTR_NULL_RETURN_VALUE(context, RC_ERROR);
63 context->streamInfo = streamConfig_;
64 context->tunnel = tunnel_;
65 CHECK_IF_PTR_NULL_RETURN_VALUE(context->tunnel, RC_ERROR);
66 context->bufferPool = bufferPool_;
67 CHECK_IF_PTR_NULL_RETURN_VALUE(context->bufferPool, RC_ERROR);
68 context->pipeline = pipeline_;
69 CHECK_IF_PTR_NULL_RETURN_VALUE(context->pipeline.lock(), RC_ERROR);
70
71 {
72 std::unique_lock<std::mutex> l(wtLock_);
73 waitingList_.clear();
74 }
75
76 std::lock_guard<std::mutex> offlineStreamLock(offlineLock_);
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 RetCode rc = offlineStream->Init(context);
86 if (rc != RC_OK) {
87 CAMERA_LOGE("offline stream [id:%{public}d] init failed", streamId_);
88 return RC_ERROR;
89 }
90
91 return RC_OK;
92 }
93
StopStream()94 RetCode StreamStillCapture::StopStream()
95 {
96 CHECK_IF_PTR_NULL_RETURN_VALUE(pipeline_, RC_ERROR);
97 if (state_ == STREAM_STATE_IDLE) {
98 CAMERA_LOGI("stream [id:%{public}d], no need to stop", streamId_);
99 return RC_OK;
100 }
101 CAMERA_LOGI("stop stream [id:%{public}d] begin", streamId_);
102
103 if (state_ != STREAM_STATE_OFFLINE) {
104 state_ = STREAM_STATE_IDLE;
105 }
106
107 tunnel_->NotifyStop();
108 cv_.notify_one();
109 if (handler_ != nullptr) {
110 handler_->join();
111 }
112
113 if (!waitingList_.empty()) {
114 auto request = waitingList_.front();
115 if (request != nullptr && request->IsContinous()) {
116 request->Cancel();
117 }
118 }
119 {
120 std::unique_lock<std::mutex> l(wtLock_);
121 waitingList_.clear();
122 }
123
124 RetCode rc = pipeline_->Flush({streamId_});
125 if (rc != RC_OK) {
126 CAMERA_LOGE("stream [id:%{public}d], pipeline flush failed", streamId_);
127 return RC_ERROR;
128 }
129
130 if (state_ != STREAM_STATE_OFFLINE) {
131 CAMERA_LOGI("stream [id:%{public}d] is waiting buffers returned", streamId_);
132 tunnel_->WaitForAllBufferReturned();
133 CAMERA_LOGI("stream [id:%{public}d], all buffers are returned.", streamId_);
134 }
135
136 rc = pipeline_->Stop({streamId_});
137 if (rc != RC_OK) {
138 CAMERA_LOGE("stream [id:%{public}d], pipeline stop failed", streamId_);
139 return RC_ERROR;
140 }
141
142 CAMERA_LOGI("stop stream [id:%{public}d] end", streamId_);
143 isFirstRequest = true;
144
145 if (state_ != STREAM_STATE_OFFLINE) {
146 inTransitList_.clear();
147 tunnel_->CleanBuffers();
148 bufferPool_->ClearBuffers();
149 }
150 return RC_OK;
151 }
152
IsRunning() const153 bool StreamStillCapture::IsRunning() const
154 {
155 return state_ == STREAM_STATE_BUSY || state_ == STREAM_STATE_OFFLINE;
156 }
157
158 REGISTERSTREAM(StreamStillCapture, {"STILL_CAPTURE"});
159 } // namespace OHOS::Camera
160