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