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