1 /*
2  * Copyright (C) 2023 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 "screen_capture_client.h"
17 #include "media_log.h"
18 #include "media_errors.h"
19 
20 namespace {
21 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_SCREENCAPTURE, "ScreenCaptureClient"};
22 }
23 
24 namespace OHOS {
25 namespace Media {
Create(const sptr<IStandardScreenCaptureService> & ipcProxy)26 std::shared_ptr<ScreenCaptureClient> ScreenCaptureClient::Create(
27     const sptr<IStandardScreenCaptureService> &ipcProxy)
28 {
29     std::shared_ptr<ScreenCaptureClient> screenCapture = std::make_shared<ScreenCaptureClient>(ipcProxy);
30 
31     CHECK_AND_RETURN_RET_LOG(screenCapture != nullptr, nullptr, "failed to new Screen Capture Client");
32 
33     int32_t ret = screenCapture->CreateListenerObject();
34     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "failed to create listener object");
35 
36     return screenCapture;
37 }
38 
ScreenCaptureClient(const sptr<IStandardScreenCaptureService> & ipcProxy)39 ScreenCaptureClient::ScreenCaptureClient(const sptr<IStandardScreenCaptureService> &ipcProxy)
40     : screenCaptureProxy_(ipcProxy)
41 {
42     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
43 }
44 
CreateListenerObject()45 int32_t ScreenCaptureClient::CreateListenerObject()
46 {
47     std::lock_guard<std::mutex> lock(mutex_);
48     listenerStub_ = new(std::nothrow) ScreenCaptureListenerStub();
49     CHECK_AND_RETURN_RET_LOG(listenerStub_ != nullptr, MSERR_NO_MEMORY,
50         "failed to new ScreenCaptureListenerStub object");
51     CHECK_AND_RETURN_RET_LOG(screenCaptureProxy_ != nullptr, MSERR_NO_MEMORY,
52         "Screen Capture service does not exist.");
53 
54     sptr<IRemoteObject> object = listenerStub_->AsObject();
55     CHECK_AND_RETURN_RET_LOG(object != nullptr, MSERR_NO_MEMORY, "listener object is nullptr");
56 
57     MEDIA_LOGD("SetListenerObject");
58     return screenCaptureProxy_->SetListenerObject(object);
59 }
60 
~ScreenCaptureClient()61 ScreenCaptureClient::~ScreenCaptureClient()
62 {
63     std::lock_guard<std::mutex> lock(mutex_);
64     if (screenCaptureProxy_ != nullptr) {
65         (void)screenCaptureProxy_->DestroyStub();
66     }
67     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
68 }
69 
SetScreenCaptureCallback(const std::shared_ptr<ScreenCaptureCallBack> & callback)70 int32_t ScreenCaptureClient::SetScreenCaptureCallback(const std::shared_ptr<ScreenCaptureCallBack> &callback)
71 {
72     std::lock_guard<std::mutex> lock(mutex_);
73     CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_NO_MEMORY, "input param callback is nullptr.");
74     CHECK_AND_RETURN_RET_LOG(listenerStub_ != nullptr, MSERR_NO_MEMORY, "listenerStub_ is nullptr.");
75     callback_ = callback;
76     MEDIA_LOGD("SetScreenCaptureCallback");
77     listenerStub_->SetScreenCaptureCallback(callback);
78     return MSERR_OK;
79 }
80 
MediaServerDied()81 void ScreenCaptureClient::MediaServerDied()
82 {
83     std::lock_guard<std::mutex> lock(mutex_);
84     screenCaptureProxy_ = nullptr;
85     listenerStub_ = nullptr;
86     if (callback_ != nullptr) {
87         callback_->OnError(SCREEN_CAPTURE_ERROR_INTERNAL, MSERR_SERVICE_DIED);
88     }
89 }
90 
Release()91 void ScreenCaptureClient::Release()
92 {
93     std::lock_guard<std::mutex> lock(mutex_);
94     CHECK_AND_RETURN_LOG(screenCaptureProxy_ != nullptr, "screenCapture service does not exist.");
95     screenCaptureProxy_->Release();
96 }
97 
SetCaptureMode(CaptureMode captureMode)98 int32_t ScreenCaptureClient::SetCaptureMode(CaptureMode captureMode)
99 {
100     std::lock_guard<std::mutex> lock(mutex_);
101     CHECK_AND_RETURN_RET_LOG(screenCaptureProxy_ != nullptr, MSERR_NO_MEMORY, "screenCapture service does not exist.");
102     return screenCaptureProxy_->SetCaptureMode(captureMode);
103 }
104 
SetDataType(DataType dataType)105 int32_t ScreenCaptureClient::SetDataType(DataType dataType)
106 {
107     std::lock_guard<std::mutex> lock(mutex_);
108     CHECK_AND_RETURN_RET_LOG(screenCaptureProxy_ != nullptr, MSERR_NO_MEMORY, "screenCapture service does not exist.");
109     return screenCaptureProxy_->SetDataType(dataType);
110 }
111 
SetRecorderInfo(RecorderInfo recorderInfo)112 int32_t ScreenCaptureClient::SetRecorderInfo(RecorderInfo recorderInfo)
113 {
114     std::lock_guard<std::mutex> lock(mutex_);
115     CHECK_AND_RETURN_RET_LOG(screenCaptureProxy_ != nullptr, MSERR_NO_MEMORY, "screenCapture service does not exist.");
116     return screenCaptureProxy_->SetRecorderInfo(recorderInfo);
117 }
118 
SetOutputFile(int32_t fd)119 int32_t ScreenCaptureClient::SetOutputFile(int32_t fd)
120 {
121     std::lock_guard<std::mutex> lock(mutex_);
122     CHECK_AND_RETURN_RET_LOG(screenCaptureProxy_ != nullptr, MSERR_NO_MEMORY, "screenCapture service does not exist.");
123     return screenCaptureProxy_->SetOutputFile(fd);
124 }
125 
InitAudioEncInfo(AudioEncInfo audioEncInfo)126 int32_t ScreenCaptureClient::InitAudioEncInfo(AudioEncInfo audioEncInfo)
127 {
128     std::lock_guard<std::mutex> lock(mutex_);
129     CHECK_AND_RETURN_RET_LOG(screenCaptureProxy_ != nullptr, MSERR_NO_MEMORY, "screenCapture service does not exist.");
130     return screenCaptureProxy_->InitAudioEncInfo(audioEncInfo);
131 }
132 
InitAudioCap(AudioCaptureInfo audioInfo)133 int32_t ScreenCaptureClient::InitAudioCap(AudioCaptureInfo audioInfo)
134 {
135     std::lock_guard<std::mutex> lock(mutex_);
136     CHECK_AND_RETURN_RET_LOG(screenCaptureProxy_ != nullptr, MSERR_NO_MEMORY, "screenCapture service does not exist.");
137     return screenCaptureProxy_->InitAudioCap(audioInfo);
138 }
139 
InitVideoEncInfo(VideoEncInfo videoEncInfo)140 int32_t ScreenCaptureClient::InitVideoEncInfo(VideoEncInfo videoEncInfo)
141 {
142     std::lock_guard<std::mutex> lock(mutex_);
143     CHECK_AND_RETURN_RET_LOG(screenCaptureProxy_ != nullptr, MSERR_NO_MEMORY, "screenCapture service does not exist.");
144     return screenCaptureProxy_->InitVideoEncInfo(videoEncInfo);
145 }
146 
InitVideoCap(VideoCaptureInfo videoInfo)147 int32_t ScreenCaptureClient::InitVideoCap(VideoCaptureInfo videoInfo)
148 {
149     std::lock_guard<std::mutex> lock(mutex_);
150     CHECK_AND_RETURN_RET_LOG(screenCaptureProxy_ != nullptr, MSERR_NO_MEMORY, "screenCapture service does not exist.");
151     return screenCaptureProxy_->InitVideoCap(videoInfo);
152 }
153 
ExcludeContent(ScreenCaptureContentFilter & contentFilter)154 int32_t ScreenCaptureClient::ExcludeContent(ScreenCaptureContentFilter &contentFilter)
155 {
156     std::lock_guard<std::mutex> lock(mutex_);
157     CHECK_AND_RETURN_RET_LOG(screenCaptureProxy_ != nullptr, MSERR_NO_MEMORY, "screenCapture service does not exist.");
158     return screenCaptureProxy_->ExcludeContent(contentFilter);
159 }
160 
SetMicrophoneEnabled(bool isMicrophone)161 int32_t ScreenCaptureClient::SetMicrophoneEnabled(bool isMicrophone)
162 {
163     std::lock_guard<std::mutex> lock(mutex_);
164     CHECK_AND_RETURN_RET_LOG(screenCaptureProxy_ != nullptr, MSERR_NO_MEMORY, "screenCapture service does not exist.");
165     return screenCaptureProxy_->SetMicrophoneEnabled(isMicrophone);
166 }
167 
SetCanvasRotation(bool canvasRotation)168 int32_t ScreenCaptureClient::SetCanvasRotation(bool canvasRotation)
169 {
170     std::lock_guard<std::mutex> lock(mutex_);
171     CHECK_AND_RETURN_RET_LOG(screenCaptureProxy_ != nullptr, MSERR_NO_MEMORY, "screenCapture service does not exist.");
172     return screenCaptureProxy_->SetCanvasRotation(canvasRotation);
173 }
174 
ResizeCanvas(int32_t width,int32_t height)175 int32_t ScreenCaptureClient::ResizeCanvas(int32_t width, int32_t height)
176 {
177     std::lock_guard<std::mutex> lock(mutex_);
178     CHECK_AND_RETURN_RET_LOG(screenCaptureProxy_ != nullptr, MSERR_NO_MEMORY, "screenCapture service does not exist.");
179     return screenCaptureProxy_->ResizeCanvas(width, height);
180 }
181 
SkipPrivacyMode(std::vector<uint64_t> & windowIDsVec)182 int32_t ScreenCaptureClient::SkipPrivacyMode(std::vector<uint64_t> &windowIDsVec)
183 {
184     std::lock_guard<std::mutex> lock(mutex_);
185     CHECK_AND_RETURN_RET_LOG(screenCaptureProxy_ != nullptr, MSERR_NO_MEMORY, "screenCapture service does not exist.");
186     return screenCaptureProxy_->SkipPrivacyMode(windowIDsVec);
187 }
188 
SetMaxVideoFrameRate(int32_t frameRate)189 int32_t ScreenCaptureClient::SetMaxVideoFrameRate(int32_t frameRate)
190 {
191     std::lock_guard<std::mutex> lock(mutex_);
192     CHECK_AND_RETURN_RET_LOG(screenCaptureProxy_ != nullptr, MSERR_NO_MEMORY, "screenCapture service does not exist.");
193     return screenCaptureProxy_->SetMaxVideoFrameRate(frameRate);
194 }
195 
StartScreenCapture(bool isPrivacyAuthorityEnabled)196 int32_t ScreenCaptureClient::StartScreenCapture(bool isPrivacyAuthorityEnabled)
197 {
198     std::lock_guard<std::mutex> lock(mutex_);
199     CHECK_AND_RETURN_RET_LOG(screenCaptureProxy_ != nullptr, MSERR_NO_MEMORY, "screenCapture service does not exist.");
200     return screenCaptureProxy_->StartScreenCapture(isPrivacyAuthorityEnabled);
201 }
202 
StartScreenCaptureWithSurface(sptr<Surface> surface,bool isPrivacyAuthorityEnabled)203 int32_t ScreenCaptureClient::StartScreenCaptureWithSurface(sptr<Surface> surface, bool isPrivacyAuthorityEnabled)
204 {
205     std::lock_guard<std::mutex> lock(mutex_);
206     CHECK_AND_RETURN_RET_LOG(screenCaptureProxy_ != nullptr, MSERR_NO_MEMORY, "screenCapture service does not exist.");
207     return screenCaptureProxy_->StartScreenCaptureWithSurface(surface, isPrivacyAuthorityEnabled);
208 }
209 
210 
StopScreenCapture()211 int32_t ScreenCaptureClient::StopScreenCapture()
212 {
213     std::lock_guard<std::mutex> lock(mutex_);
214     CHECK_AND_RETURN_RET_LOG(screenCaptureProxy_ != nullptr, MSERR_NO_MEMORY, "screenCapture service does not exist.");
215     return screenCaptureProxy_->StopScreenCapture();
216 }
217 
AcquireAudioBuffer(std::shared_ptr<AudioBuffer> & audioBuffer,AudioCaptureSourceType type)218 int32_t ScreenCaptureClient::AcquireAudioBuffer(std::shared_ptr<AudioBuffer> &audioBuffer, AudioCaptureSourceType type)
219 {
220     std::lock_guard<std::mutex> lock(mutex_);
221     CHECK_AND_RETURN_RET_LOG(screenCaptureProxy_ != nullptr, MSERR_NO_MEMORY, "screenCapture service does not exist.");
222     return screenCaptureProxy_->AcquireAudioBuffer(audioBuffer, type);
223 }
224 
AcquireVideoBuffer(sptr<OHOS::SurfaceBuffer> & surfaceBuffer,int32_t & fence,int64_t & timestamp,OHOS::Rect & damage)225 int32_t ScreenCaptureClient::AcquireVideoBuffer(sptr<OHOS::SurfaceBuffer> &surfaceBuffer, int32_t &fence,
226                                                 int64_t &timestamp, OHOS::Rect &damage)
227 {
228     std::lock_guard<std::mutex> lock(mutex_);
229     CHECK_AND_RETURN_RET_LOG(screenCaptureProxy_ != nullptr, MSERR_NO_MEMORY, "screenCapture service does not exist.");
230     return screenCaptureProxy_->AcquireVideoBuffer(surfaceBuffer, fence, timestamp, damage);
231 }
232 
ReleaseAudioBuffer(AudioCaptureSourceType type)233 int32_t ScreenCaptureClient::ReleaseAudioBuffer(AudioCaptureSourceType type)
234 {
235     std::lock_guard<std::mutex> lock(mutex_);
236     CHECK_AND_RETURN_RET_LOG(screenCaptureProxy_ != nullptr, MSERR_NO_MEMORY, "screenCapture service does not exist.");
237     return screenCaptureProxy_->ReleaseAudioBuffer(type);
238 }
239 
ReleaseVideoBuffer()240 int32_t ScreenCaptureClient::ReleaseVideoBuffer()
241 {
242     std::lock_guard<std::mutex> lock(mutex_);
243     CHECK_AND_RETURN_RET_LOG(screenCaptureProxy_ != nullptr, MSERR_NO_MEMORY, "screenCapture service does not exist.");
244     return screenCaptureProxy_->ReleaseVideoBuffer();
245 }
246 } // namespace Media
247 } // namespace OHOS