1 /*
2 * Copyright (c) 2021-2022 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 "output/capture_output.h"
17 #include <memory>
18 #include <mutex>
19
20 #include "camera_log.h"
21 #include "capture_session.h"
22
23 namespace OHOS {
24 namespace CameraStandard {
25 static const char* g_captureOutputTypeString[CAPTURE_OUTPUT_TYPE_MAX] = {"Preview", "Photo", "Video", "Metadata"};
26
CaptureOutput(CaptureOutputType outputType,StreamType streamType,sptr<IBufferProducer> bufferProducer,sptr<IStreamCommon> stream)27 CaptureOutput::CaptureOutput(CaptureOutputType outputType, StreamType streamType, sptr<IBufferProducer> bufferProducer,
28 sptr<IStreamCommon> stream)
29 : outputType_(outputType), streamType_(streamType), stream_(stream), bufferProducer_(bufferProducer)
30 {}
31
CaptureOutput(CaptureOutputType outputType,StreamType streamType,sptr<IStreamCommon> stream)32 CaptureOutput::CaptureOutput(CaptureOutputType outputType, StreamType streamType, sptr<IStreamCommon> stream)
33 : outputType_(outputType), streamType_(streamType), stream_(stream)
34 {}
35
RegisterStreamBinderDied()36 void CaptureOutput::RegisterStreamBinderDied()
37 {
38 auto stream = GetStream();
39 CHECK_RETURN(stream == nullptr);
40 sptr<IRemoteObject> object = stream->AsObject();
41 CHECK_RETURN(object == nullptr);
42 std::lock_guard<std::mutex> lock(deathRecipientMutex_);
43 if (deathRecipient_ == nullptr) {
44 deathRecipient_ = new (std::nothrow) CameraDeathRecipient(0);
45 CHECK_RETURN_ELOG(deathRecipient_ == nullptr, "failed to new CameraDeathRecipient.");
46 deathRecipient_->SetNotifyCb([this](pid_t pid) { OnCameraServerDied(pid); });
47 }
48
49 bool result = object->AddDeathRecipient(deathRecipient_);
50 CHECK_RETURN_ELOG(!result, "failed to add deathRecipient");
51 }
52
GetBufferProducer()53 sptr<IBufferProducer> CaptureOutput::GetBufferProducer()
54 {
55 std::lock_guard<std::mutex> lock(bufferProducerMutex_);
56 return bufferProducer_;
57 }
58
GetPhotoSurfaceId()59 std::string CaptureOutput::GetPhotoSurfaceId()
60 {
61 std::lock_guard<std::mutex> lock(bufferProducerMutex_);
62 return surfaceId_;
63 }
64
UnregisterStreamBinderDied()65 void CaptureOutput::UnregisterStreamBinderDied()
66 {
67 std::lock_guard<std::mutex> lock(deathRecipientMutex_);
68 CHECK_RETURN(deathRecipient_ == nullptr);
69 auto stream = GetStream();
70 if (stream != nullptr) {
71 stream->AsObject()->RemoveDeathRecipient(deathRecipient_);
72 deathRecipient_ = nullptr;
73 }
74 }
75
OnCameraServerDied(pid_t pid)76 void CaptureOutput::OnCameraServerDied(pid_t pid)
77 {
78 CameraServerDied(pid);
79 UnregisterStreamBinderDied();
80 }
81
~CaptureOutput()82 CaptureOutput::~CaptureOutput()
83 {
84 UnregisterStreamBinderDied();
85 }
86
GetOutputType()87 CaptureOutputType CaptureOutput::GetOutputType()
88 {
89 return outputType_;
90 }
91
GetOutputTypeString()92 const char* CaptureOutput::GetOutputTypeString()
93 {
94 return g_captureOutputTypeString[outputType_];
95 }
96
GetStreamType()97 StreamType CaptureOutput::GetStreamType()
98 {
99 return streamType_;
100 }
101
IsStreamCreated()102 bool CaptureOutput::IsStreamCreated()
103 {
104 std::lock_guard<std::mutex> lock(streamMutex_);
105 return stream_ != nullptr;
106 }
107
GetStream()108 sptr<IStreamCommon> CaptureOutput::GetStream()
109 {
110 std::lock_guard<std::mutex> lock(streamMutex_);
111 return stream_;
112 }
113
SetStream(sptr<IStreamCommon> stream)114 void CaptureOutput::SetStream(sptr<IStreamCommon> stream)
115 {
116 {
117 std::lock_guard<std::mutex> lock(streamMutex_);
118 stream_ = stream;
119 }
120 RegisterStreamBinderDied();
121 }
122
GetSession()123 sptr<CaptureSession> CaptureOutput::GetSession()
124 {
125 std::lock_guard<std::mutex> lock(sessionMutex_);
126 return session_.promote();
127 }
128
SetSession(wptr<CaptureSession> captureSession)129 void CaptureOutput::SetSession(wptr<CaptureSession> captureSession)
130 {
131 std::lock_guard<std::mutex> lock(sessionMutex_);
132 session_ = captureSession;
133 }
134
Release()135 int32_t CaptureOutput::Release()
136 {
137 {
138 UnregisterStreamBinderDied();
139 std::lock_guard<std::mutex> lock(streamMutex_);
140 stream_ = nullptr;
141 }
142 SetSession(nullptr);
143 return 0;
144 }
145
SetPhotoProfile(Profile & profile)146 void CaptureOutput::SetPhotoProfile(Profile& profile)
147 {
148 std::lock_guard<std::mutex> lock(photoProfileMutex_);
149 photoProfile_ = std::make_shared<Profile>(profile);
150 }
151
GetPhotoProfile()152 std::shared_ptr<Profile> CaptureOutput::GetPhotoProfile()
153 {
154 std::lock_guard<std::mutex> lock(photoProfileMutex_);
155 return photoProfile_;
156 }
157
SetPreviewProfile(Profile & profile)158 void CaptureOutput::SetPreviewProfile(Profile& profile)
159 {
160 std::lock_guard<std::mutex> lock(previewProfileMutex_);
161 previewProfile_ = std::make_shared<Profile>(profile);
162 }
163
GetPreviewProfile()164 std::shared_ptr<Profile> CaptureOutput::GetPreviewProfile()
165 {
166 std::lock_guard<std::mutex> lock(previewProfileMutex_);
167 return previewProfile_;
168 }
169
SetVideoProfile(VideoProfile & videoProfile)170 void CaptureOutput::SetVideoProfile(VideoProfile& videoProfile)
171 {
172 std::lock_guard<std::mutex> lock(videoProfileMutex_);
173 videoProfile_ = std::make_shared<VideoProfile>(videoProfile);
174 }
175
GetVideoProfile()176 std::shared_ptr<VideoProfile> CaptureOutput::GetVideoProfile()
177 {
178 std::lock_guard<std::mutex> lock(videoProfileMutex_);
179 return videoProfile_;
180 }
181
SetDepthProfile(DepthProfile & depthProfile)182 void CaptureOutput::SetDepthProfile(DepthProfile& depthProfile)
183 {
184 std::lock_guard<std::mutex> lock(depthProfileMutex_);
185 depthProfile_ = std::make_shared<DepthProfile>(depthProfile);
186 }
187
GetDepthProfile()188 std::shared_ptr<DepthProfile> CaptureOutput::GetDepthProfile()
189 {
190 std::lock_guard<std::mutex> lock(depthProfileMutex_);
191 return depthProfile_;
192 }
193
ClearProfiles()194 void CaptureOutput::ClearProfiles()
195 {
196 {
197 std::lock_guard<std::mutex> lock(previewProfileMutex_);
198 previewProfile_ = nullptr;
199 }
200 {
201 std::lock_guard<std::mutex> lock(photoProfileMutex_);
202 photoProfile_ = nullptr;
203 }
204 {
205 std::lock_guard<std::mutex> lock(videoProfileMutex_);
206 videoProfile_ = nullptr;
207 }
208 }
209
AddTag(Tag tag)210 void CaptureOutput::AddTag(Tag tag)
211 {
212 std::lock_guard<std::mutex> lock(tagsMutex_);
213 tags_.insert(tag);
214 }
215
RemoveTag(Tag tag)216 void CaptureOutput::RemoveTag(Tag tag)
217 {
218 std::lock_guard<std::mutex> lock(tagsMutex_);
219 tags_.erase(tag);
220 }
221
IsTagSetted(Tag tag)222 bool CaptureOutput::IsTagSetted(Tag tag)
223 {
224 std::lock_guard<std::mutex> lock(tagsMutex_);
225 return tags_.find(tag) != tags_.end();
226 }
227
IsHasEnableOfflinePhoto()228 bool CaptureOutput::IsHasEnableOfflinePhoto()
229 {
230 return mIsHasEnableOfflinePhoto_;
231 }
232 } // CameraStandard
233 } // OHOS
234