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