• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_service_stub.h"
17 #include "media_server_manager.h"
18 #include "media_log.h"
19 #include "media_errors.h"
20 #include "avsharedmemory_ipc.h"
21 #include "screen_capture_listener_proxy.h"
22 
23 namespace {
24 constexpr int MAX_WINDOWS_LEN = 1000;
25 constexpr int MAX_FILTER_CONTENTS_COUNT = 1000;
26 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_SCREENCAPTURE, "ScreenCaptureServiceStub"};
27 }
28 
29 namespace OHOS {
30 namespace Media {
Create()31 sptr<ScreenCaptureServiceStub> ScreenCaptureServiceStub::Create()
32 {
33     sptr<ScreenCaptureServiceStub> screenCaptureStub = new(std::nothrow) ScreenCaptureServiceStub();
34     CHECK_AND_RETURN_RET_LOG(screenCaptureStub != nullptr, nullptr, "failed to new ScreenCaptureServiceStub");
35 
36     int32_t ret = screenCaptureStub->Init();
37     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "failed to screenCapture stub init");
38     return screenCaptureStub;
39 }
40 
ScreenCaptureServiceStub()41 ScreenCaptureServiceStub::ScreenCaptureServiceStub()
42 {
43     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
44 }
45 
~ScreenCaptureServiceStub()46 ScreenCaptureServiceStub::~ScreenCaptureServiceStub()
47 {
48     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
49 }
50 
Init()51 int32_t ScreenCaptureServiceStub::Init()
52 {
53     screenCaptureServer_ = ScreenCaptureServer::Create();
54     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_NO_MEMORY,
55         "failed to create ScreenCaptureServer Service");
56     screenCaptureStubFuncs_[SET_LISTENER_OBJ] = &ScreenCaptureServiceStub::SetListenerObject;
57     screenCaptureStubFuncs_[RELEASE] = &ScreenCaptureServiceStub::Release;
58     screenCaptureStubFuncs_[SET_MIC_ENABLE] = &ScreenCaptureServiceStub::SetMicrophoneEnabled;
59     screenCaptureStubFuncs_[SET_SCREEN_ROTATION] = &ScreenCaptureServiceStub::SetCanvasRotation;
60     screenCaptureStubFuncs_[RESIZE_CANVAS] = &ScreenCaptureServiceStub::ResizeCanvas;
61     screenCaptureStubFuncs_[SKIP_PRIVACY] = &ScreenCaptureServiceStub::SkipPrivacyMode;
62     screenCaptureStubFuncs_[SET_CAPTURE_MODE] = &ScreenCaptureServiceStub::SetCaptureMode;
63     screenCaptureStubFuncs_[SET_DATA_TYPE] = &ScreenCaptureServiceStub::SetDataType;
64     screenCaptureStubFuncs_[SET_RECORDER_INFO] = &ScreenCaptureServiceStub::SetRecorderInfo;
65     screenCaptureStubFuncs_[SET_OUTPUT_FILE] = &ScreenCaptureServiceStub::SetOutputFile;
66     screenCaptureStubFuncs_[INIT_AUDIO_ENC_INFO] = &ScreenCaptureServiceStub::InitAudioEncInfo;
67     screenCaptureStubFuncs_[INIT_AUDIO_CAP] = &ScreenCaptureServiceStub::InitAudioCap;
68     screenCaptureStubFuncs_[INIT_VIDEO_ENC_INFO] = &ScreenCaptureServiceStub::InitVideoEncInfo;
69     screenCaptureStubFuncs_[INIT_VIDEO_CAP] = &ScreenCaptureServiceStub::InitVideoCap;
70     screenCaptureStubFuncs_[START_SCREEN_CAPTURE] = &ScreenCaptureServiceStub::StartScreenCapture;
71     screenCaptureStubFuncs_[START_SCREEN_CAPTURE_WITH_SURFACE] =
72         &ScreenCaptureServiceStub::StartScreenCaptureWithSurface;
73     screenCaptureStubFuncs_[STOP_SCREEN_CAPTURE] = &ScreenCaptureServiceStub::StopScreenCapture;
74     screenCaptureStubFuncs_[ACQUIRE_AUDIO_BUF] = &ScreenCaptureServiceStub::AcquireAudioBuffer;
75     screenCaptureStubFuncs_[ACQUIRE_VIDEO_BUF] = &ScreenCaptureServiceStub::AcquireVideoBuffer;
76     screenCaptureStubFuncs_[RELEASE_AUDIO_BUF] = &ScreenCaptureServiceStub::ReleaseAudioBuffer;
77     screenCaptureStubFuncs_[RELEASE_VIDEO_BUF] = &ScreenCaptureServiceStub::ReleaseVideoBuffer;
78     screenCaptureStubFuncs_[DESTROY] = &ScreenCaptureServiceStub::DestroyStub;
79     screenCaptureStubFuncs_[EXCLUDE_CONTENT] = &ScreenCaptureServiceStub::ExcludeContent;
80 
81     return MSERR_OK;
82 }
83 
DestroyStub()84 int32_t ScreenCaptureServiceStub::DestroyStub()
85 {
86     screenCaptureServer_ = nullptr;
87     MediaServerManager::GetInstance().DestroyStubObject(MediaServerManager::SCREEN_CAPTURE, AsObject());
88     return MSERR_OK;
89 }
90 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)91 int ScreenCaptureServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
92     MessageOption &option)
93 {
94     MEDIA_LOGD("Stub: OnRemoteRequest of code: %{public}u is received", code);
95 
96     auto remoteDescriptor = data.ReadInterfaceToken();
97     if (ScreenCaptureServiceStub::GetDescriptor() != remoteDescriptor) {
98         MEDIA_LOGE("Invalid descriptor");
99         return MSERR_INVALID_OPERATION;
100     }
101 
102     auto itFunc = screenCaptureStubFuncs_.find(code);
103     if (itFunc != screenCaptureStubFuncs_.end()) {
104         auto memberFunc = itFunc->second;
105         if (memberFunc != nullptr) {
106             int32_t ret = (this->*memberFunc)(data, reply);
107             if (ret != MSERR_OK) {
108                 MEDIA_LOGE("Calling memberFunc is failed.");
109             }
110             return MSERR_OK;
111         }
112     }
113     MEDIA_LOGW("ScreenCaptureServiceStub: no member func supporting, applying default process");
114 
115     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
116 }
117 
SetCaptureMode(CaptureMode captureMode)118 int32_t ScreenCaptureServiceStub::SetCaptureMode(CaptureMode captureMode)
119 {
120     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
121         "screen capture server is nullptr");
122     return screenCaptureServer_->SetCaptureMode(captureMode);
123 }
124 
SetDataType(DataType dataType)125 int32_t ScreenCaptureServiceStub::SetDataType(DataType dataType)
126 {
127     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
128         "screen capture server is nullptr");
129     return screenCaptureServer_->SetDataType(dataType);
130 }
131 
SetRecorderInfo(RecorderInfo recorderInfo)132 int32_t ScreenCaptureServiceStub::SetRecorderInfo(RecorderInfo recorderInfo)
133 {
134     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
135         "screen capture server is nullptr");
136     return screenCaptureServer_->SetRecorderInfo(recorderInfo);
137 }
138 
SetOutputFile(int32_t fd)139 int32_t ScreenCaptureServiceStub::SetOutputFile(int32_t fd)
140 {
141     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
142         "screen capture server is nullptr");
143     return screenCaptureServer_->SetOutputFile(fd);
144 }
145 
InitAudioEncInfo(AudioEncInfo audioEncInfo)146 int32_t ScreenCaptureServiceStub::InitAudioEncInfo(AudioEncInfo audioEncInfo)
147 {
148     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
149         "screen capture server is nullptr");
150     return screenCaptureServer_->InitAudioEncInfo(audioEncInfo);
151 }
152 
InitAudioCap(AudioCaptureInfo audioInfo)153 int32_t ScreenCaptureServiceStub::InitAudioCap(AudioCaptureInfo audioInfo)
154 {
155     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
156         "screen capture server is nullptr");
157     return screenCaptureServer_->InitAudioCap(audioInfo);
158 }
159 
InitVideoEncInfo(VideoEncInfo videoEncInfo)160 int32_t ScreenCaptureServiceStub::InitVideoEncInfo(VideoEncInfo videoEncInfo)
161 {
162     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
163         "screen capture server is nullptr");
164     return screenCaptureServer_->InitVideoEncInfo(videoEncInfo);
165 }
166 
InitVideoCap(VideoCaptureInfo videoInfo)167 int32_t ScreenCaptureServiceStub::InitVideoCap(VideoCaptureInfo videoInfo)
168 {
169     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
170         "screen capture server is nullptr");
171     return screenCaptureServer_->InitVideoCap(videoInfo);
172 }
173 
StartScreenCapture(bool isPrivacyAuthorityEnabled)174 int32_t ScreenCaptureServiceStub::StartScreenCapture(bool isPrivacyAuthorityEnabled)
175 {
176     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
177         "screen capture server is nullptr");
178     return screenCaptureServer_->StartScreenCapture(isPrivacyAuthorityEnabled);
179 }
180 
StartScreenCaptureWithSurface(sptr<Surface> surface,bool isPrivacyAuthorityEnabled)181 int32_t ScreenCaptureServiceStub::StartScreenCaptureWithSurface(sptr<Surface> surface, bool isPrivacyAuthorityEnabled)
182 {
183     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
184         "screen capture server is nullptr");
185 
186     return screenCaptureServer_->StartScreenCaptureWithSurface(surface, isPrivacyAuthorityEnabled);
187 }
188 
StopScreenCapture()189 int32_t ScreenCaptureServiceStub::StopScreenCapture()
190 {
191     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
192         "screen capture server is nullptr");
193     return screenCaptureServer_->StopScreenCapture();
194 }
195 
SetListenerObject(const sptr<IRemoteObject> & object)196 int32_t ScreenCaptureServiceStub::SetListenerObject(const sptr<IRemoteObject> &object)
197 {
198     CHECK_AND_RETURN_RET_LOG(object != nullptr, MSERR_NO_MEMORY, "set listener object is nullptr");
199 
200     sptr<IStandardScreenCaptureListener> listener = iface_cast<IStandardScreenCaptureListener>(object);
201     CHECK_AND_RETURN_RET_LOG(listener != nullptr, MSERR_NO_MEMORY, "failed to convert IStandardScreenCaptureListener");
202 
203     std::shared_ptr<ScreenCaptureCallBack> callback = std::make_shared<ScreenCaptureListenerCallback>(listener);
204     CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_NO_MEMORY, "failed to new ScreenCaptureCallBack");
205 
206     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_NO_MEMORY, "screen capture server is nullptr");
207     (void)screenCaptureServer_->SetScreenCaptureCallback(callback);
208     return MSERR_OK;
209 }
210 
ExcludeContent(ScreenCaptureContentFilter & contentFilter)211 int32_t ScreenCaptureServiceStub::ExcludeContent(ScreenCaptureContentFilter &contentFilter)
212 {
213     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
214         "screen capture server is nullptr");
215     return screenCaptureServer_->ExcludeContent(contentFilter);
216 }
217 
SetMicrophoneEnabled(bool isMicrophone)218 int32_t ScreenCaptureServiceStub::SetMicrophoneEnabled(bool isMicrophone)
219 {
220     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
221         "screen capture server is nullptr");
222     return screenCaptureServer_->SetMicrophoneEnabled(isMicrophone);
223 }
224 
SetCanvasRotation(bool canvasRotation)225 int32_t ScreenCaptureServiceStub::SetCanvasRotation(bool canvasRotation)
226 {
227     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
228                              "screen capture server is nullptr");
229     return screenCaptureServer_->SetCanvasRotation(canvasRotation);
230 }
231 
ResizeCanvas(int32_t width,int32_t height)232 int32_t ScreenCaptureServiceStub::ResizeCanvas(int32_t width, int32_t height)
233 {
234     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
235                              "screen capture server is nullptr");
236     return screenCaptureServer_->ResizeCanvas(width, height);
237 }
238 
SkipPrivacyMode(std::vector<uint64_t> & windowIDsVec)239 int32_t ScreenCaptureServiceStub::SkipPrivacyMode(std::vector<uint64_t> &windowIDsVec)
240 {
241     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
242                              "screen capture server is nullptr");
243     return screenCaptureServer_->SkipPrivacyMode(windowIDsVec);
244 }
245 
AcquireAudioBuffer(std::shared_ptr<AudioBuffer> & audioBuffer,AudioCaptureSourceType type)246 int32_t ScreenCaptureServiceStub::AcquireAudioBuffer(std::shared_ptr<AudioBuffer> &audioBuffer,
247                                                      AudioCaptureSourceType type)
248 {
249     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
250         "screen capture server is nullptr");
251     return screenCaptureServer_->AcquireAudioBuffer(audioBuffer, type);
252 }
253 
AcquireVideoBuffer(sptr<OHOS::SurfaceBuffer> & surfaceBuffer,int32_t & fence,int64_t & timestamp,OHOS::Rect & damage)254 int32_t ScreenCaptureServiceStub::AcquireVideoBuffer(sptr<OHOS::SurfaceBuffer> &surfaceBuffer, int32_t &fence,
255                                                      int64_t &timestamp, OHOS::Rect &damage)
256 {
257     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
258         "screen capture server is nullptr");
259     return screenCaptureServer_->AcquireVideoBuffer(surfaceBuffer, fence, timestamp, damage);
260 }
261 
ReleaseAudioBuffer(AudioCaptureSourceType type)262 int32_t ScreenCaptureServiceStub::ReleaseAudioBuffer(AudioCaptureSourceType type)
263 {
264     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
265         "screen capture server is nullptr");
266     return screenCaptureServer_->ReleaseAudioBuffer(type);
267 }
268 
ReleaseVideoBuffer()269 int32_t ScreenCaptureServiceStub::ReleaseVideoBuffer()
270 {
271     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
272         "screen capture server is nullptr");
273     return screenCaptureServer_->ReleaseVideoBuffer();
274 }
275 
ExcludeContent(MessageParcel & data,MessageParcel & reply)276 int32_t ScreenCaptureServiceStub::ExcludeContent(MessageParcel &data, MessageParcel &reply)
277 {
278     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
279         "screen capture server is nullptr");
280     ScreenCaptureContentFilter contentFilter;
281     int32_t size = data.ReadInt32();
282     CHECK_AND_RETURN_RET_LOG(size < MAX_FILTER_CONTENTS_COUNT, MSERR_INVALID_STATE,
283                              "content filter size is exceed max range");
284     for (int32_t i = 0; i < size; i++) {
285         contentFilter.filteredAudioContents.insert(
286             static_cast<AVScreenCaptureFilterableAudioContent>(data.ReadInt32()));
287     }
288     int32_t windowIdSize = data.ReadInt32();
289     CHECK_AND_RETURN_RET_LOG(windowIdSize < MAX_FILTER_CONTENTS_COUNT, MSERR_INVALID_STATE,
290                              "windowID size is exceed max range");
291     if (windowIdSize > 0) {
292         std::vector<uint64_t> vec;
293         for (int32_t i = 0; i < windowIdSize; i++) {
294             vec.push_back(data.ReadUint64());
295         }
296         contentFilter.windowIDsVec = vec;
297     }
298     int32_t ret = ExcludeContent(contentFilter);
299     reply.WriteInt32(ret);
300     return MSERR_OK;
301 }
302 
SetMicrophoneEnabled(MessageParcel & data,MessageParcel & reply)303 int32_t ScreenCaptureServiceStub::SetMicrophoneEnabled(MessageParcel &data, MessageParcel &reply)
304 {
305     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
306         "screen capture server is nullptr");
307     bool setMicEnable = data.ReadBool();
308     int32_t ret = SetMicrophoneEnabled(setMicEnable);
309     reply.WriteInt32(ret);
310     return MSERR_OK;
311 }
312 
SetCanvasRotation(MessageParcel & data,MessageParcel & reply)313 int32_t ScreenCaptureServiceStub::SetCanvasRotation(MessageParcel &data, MessageParcel &reply)
314 {
315     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
316                              "screen capture server is nullptr");
317     bool canvasRotation = data.ReadBool();
318     int32_t ret = SetCanvasRotation(canvasRotation);
319     reply.WriteInt32(ret);
320     return MSERR_OK;
321 }
322 
ResizeCanvas(MessageParcel & data,MessageParcel & reply)323 int32_t ScreenCaptureServiceStub::ResizeCanvas(MessageParcel &data, MessageParcel &reply)
324 {
325     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
326                              "screen capture server is nullptr");
327     int32_t width = data.ReadInt32();
328     int32_t height = data.ReadInt32();
329     int32_t ret = ResizeCanvas(width, height);
330     reply.WriteInt32(ret);
331     return MSERR_OK;
332 }
333 
SkipPrivacyMode(MessageParcel & data,MessageParcel & reply)334 int32_t ScreenCaptureServiceStub::SkipPrivacyMode(MessageParcel &data, MessageParcel &reply)
335 {
336     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
337                              "screen capture server is nullptr");
338     int32_t windowIdSize = data.ReadInt32();
339     CHECK_AND_RETURN_RET_LOG(windowIdSize < MAX_FILTER_CONTENTS_COUNT, MSERR_INVALID_STATE,
340                              "windowID size is exceed max range");
341     if (windowIdSize >= 0) {
342         std::vector<uint64_t> vec;
343         for (int32_t i = 0; i < windowIdSize; i++) {
344             vec.push_back(data.ReadUint64());
345         }
346         int32_t ret = SkipPrivacyMode(vec);
347         reply.WriteInt32(ret);
348     }
349     return MSERR_OK;
350 }
351 
SetCaptureMode(MessageParcel & data,MessageParcel & reply)352 int32_t ScreenCaptureServiceStub::SetCaptureMode(MessageParcel &data, MessageParcel &reply)
353 {
354     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
355         "screen capture server is nullptr");
356     CaptureMode mode = static_cast<CaptureMode>(data.ReadInt32());
357     int32_t ret = SetCaptureMode(mode);
358     reply.WriteInt32(ret);
359     return MSERR_OK;
360 }
361 
SetDataType(MessageParcel & data,MessageParcel & reply)362 int32_t ScreenCaptureServiceStub::SetDataType(MessageParcel &data, MessageParcel &reply)
363 {
364     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
365         "screen capture server is nullptr");
366     DataType dataType = static_cast<DataType>(data.ReadInt32());
367     int32_t ret = SetDataType(dataType);
368     reply.WriteInt32(ret);
369     return MSERR_OK;
370 }
371 
SetRecorderInfo(MessageParcel & data,MessageParcel & reply)372 int32_t ScreenCaptureServiceStub::SetRecorderInfo(MessageParcel &data, MessageParcel &reply)
373 {
374     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
375         "screen capture server is nullptr");
376     RecorderInfo recorderInfo;
377     recorderInfo.url = data.ReadString();
378     recorderInfo.fileFormat = data.ReadString();
379     int32_t ret = SetRecorderInfo(recorderInfo);
380     reply.WriteInt32(ret);
381     return MSERR_OK;
382 }
383 
SetOutputFile(MessageParcel & data,MessageParcel & reply)384 int32_t ScreenCaptureServiceStub::SetOutputFile(MessageParcel &data, MessageParcel &reply)
385 {
386     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
387         "screen capture server is nullptr");
388     int32_t fd = data.ReadFileDescriptor();
389     int32_t ret = SetOutputFile(fd);
390     reply.WriteInt32(ret);
391     (void)::close(fd);
392     return MSERR_OK;
393 }
394 
InitAudioEncInfo(MessageParcel & data,MessageParcel & reply)395 int32_t ScreenCaptureServiceStub::InitAudioEncInfo(MessageParcel &data, MessageParcel &reply)
396 {
397     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
398         "screen capture server is nullptr");
399     AudioEncInfo audioEncInfo;
400     audioEncInfo.audioBitrate = data.ReadInt32();
401     audioEncInfo.audioCodecformat = static_cast<AudioCodecFormat>(data.ReadInt32());
402     int32_t ret = InitAudioEncInfo(audioEncInfo);
403     reply.WriteInt32(ret);
404     return MSERR_OK;
405 }
406 
InitAudioCap(MessageParcel & data,MessageParcel & reply)407 int32_t ScreenCaptureServiceStub::InitAudioCap(MessageParcel &data, MessageParcel &reply)
408 {
409     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
410         "screen capture server is nullptr");
411     AudioCaptureInfo audioInfo;
412     audioInfo.audioSampleRate = data.ReadInt32();
413     audioInfo.audioChannels = data.ReadInt32();
414     audioInfo.audioSource = static_cast<AudioCaptureSourceType>(data.ReadInt32());
415     int32_t ret = InitAudioCap(audioInfo);
416     reply.WriteInt32(ret);
417     return MSERR_OK;
418 }
419 
InitVideoEncInfo(MessageParcel & data,MessageParcel & reply)420 int32_t ScreenCaptureServiceStub::InitVideoEncInfo(MessageParcel &data, MessageParcel &reply)
421 {
422     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
423         "screen capture server is nullptr");
424     VideoEncInfo videoEncInfo;
425     videoEncInfo.videoCodec = static_cast<VideoCodecFormat>(data.ReadInt32());
426     videoEncInfo.videoBitrate = data.ReadInt32();
427     videoEncInfo.videoFrameRate = data.ReadInt32();
428     int32_t ret = InitVideoEncInfo(videoEncInfo);
429     reply.WriteInt32(ret);
430     return MSERR_OK;
431 }
432 
InitVideoCap(MessageParcel & data,MessageParcel & reply)433 int32_t ScreenCaptureServiceStub::InitVideoCap(MessageParcel &data, MessageParcel &reply)
434 {
435     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
436         "screen capture server is nullptr");
437     VideoCaptureInfo videoInfo;
438     videoInfo.displayId = data.ReadUint64();
439     int32_t size = data.ReadInt32();
440     size = size >= MAX_WINDOWS_LEN ? MAX_WINDOWS_LEN : size;
441     if (size > 0) {
442         for (auto i = 0; i < size; i++) {
443             videoInfo.taskIDs.push_back(data.ReadInt32());
444         }
445     }
446     videoInfo.videoFrameWidth = data.ReadInt32();
447     videoInfo.videoFrameHeight = data.ReadInt32();
448     videoInfo.videoSource = static_cast<VideoSourceType>(data.ReadInt32());
449     int32_t ret = InitVideoCap(videoInfo);
450     reply.WriteInt32(ret);
451     return MSERR_OK;
452 }
453 
StartScreenCapture(MessageParcel & data,MessageParcel & reply)454 int32_t ScreenCaptureServiceStub::StartScreenCapture(MessageParcel &data, MessageParcel &reply)
455 {
456     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
457         "screen capture server is nullptr");
458     bool isPrivacyAuthorityEnabled = data.ReadBool();
459     int32_t ret = StartScreenCapture(isPrivacyAuthorityEnabled);
460     reply.WriteInt32(ret);
461     return MSERR_OK;
462 }
463 
StartScreenCaptureWithSurface(MessageParcel & data,MessageParcel & reply)464 int32_t ScreenCaptureServiceStub::StartScreenCaptureWithSurface(MessageParcel &data, MessageParcel &reply)
465 {
466     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
467         "screen capture server is nullptr");
468 
469     sptr<IRemoteObject> object = data.ReadRemoteObject();
470     CHECK_AND_RETURN_RET_LOG(object != nullptr, MSERR_NO_MEMORY,
471         "ScreenCaptureServiceProxy StartScreenCaptureWithSurface object is nullptr");
472 
473     sptr<IBufferProducer> producer = iface_cast<IBufferProducer>(object);
474     CHECK_AND_RETURN_RET_LOG(producer != nullptr, MSERR_NO_MEMORY, "failed to convert object to producer");
475 
476     sptr<Surface> surface = Surface::CreateSurfaceAsProducer(producer);
477     CHECK_AND_RETURN_RET_LOG(surface != nullptr, MSERR_NO_MEMORY, "failed to create surface");
478 
479     bool isPrivacyAuthorityEnabled = data.ReadBool();
480     int32_t ret = StartScreenCaptureWithSurface(surface, isPrivacyAuthorityEnabled);
481     reply.WriteInt32(ret);
482     return MSERR_OK;
483 }
484 
StopScreenCapture(MessageParcel & data,MessageParcel & reply)485 int32_t ScreenCaptureServiceStub::StopScreenCapture(MessageParcel &data, MessageParcel &reply)
486 {
487     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
488         "screen capture server is nullptr");
489     (void)data;
490     int32_t ret = StopScreenCapture();
491     reply.WriteInt32(ret);
492     return MSERR_OK;
493 }
494 
AcquireAudioBuffer(MessageParcel & data,MessageParcel & reply)495 int32_t ScreenCaptureServiceStub::AcquireAudioBuffer(MessageParcel &data, MessageParcel &reply)
496 {
497     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
498         "screen capture server is nullptr");
499     std::shared_ptr<AudioBuffer> audioBuffer;
500     AudioCaptureSourceType type = static_cast<AudioCaptureSourceType>(data.ReadInt32());
501     int32_t ret = AcquireAudioBuffer(audioBuffer, type);
502     reply.WriteInt32(ret);
503     if (ret == MSERR_OK) {
504         reply.WriteInt32(audioBuffer->length);
505         if ((audioBuffer->buffer != nullptr)&&(audioBuffer->length > 0)) {
506             reply.WriteBuffer(audioBuffer->buffer, audioBuffer->length);
507         }
508         reply.WriteInt32(audioBuffer->sourcetype);
509         reply.WriteInt64(audioBuffer->timestamp);
510     }
511     return MSERR_OK;
512 }
513 
AcquireVideoBuffer(MessageParcel & data,MessageParcel & reply)514 int32_t ScreenCaptureServiceStub::AcquireVideoBuffer(MessageParcel &data, MessageParcel &reply)
515 {
516     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
517         "screen capture server is nullptr");
518     (void)data;
519     int32_t fence = -1; // init value start here
520     int64_t timestamp = 0;
521     OHOS::Rect damage;
522     sptr<OHOS::SurfaceBuffer> videoBuffer = nullptr;
523     int32_t ret = AcquireVideoBuffer(videoBuffer, fence, timestamp, damage);
524     reply.WriteInt32(ret);
525     if (ret == MSERR_OK) {
526         if (videoBuffer != nullptr) {
527             videoBuffer->WriteToMessageParcel(reply);
528         }
529         reply.WriteInt32(fence); // return to App client
530         reply.WriteInt64(timestamp);
531         reply.WriteInt32(damage.x);
532         reply.WriteInt32(damage.y);
533         reply.WriteInt32(damage.w);
534         reply.WriteInt32(damage.h);
535     }
536     return MSERR_OK;
537 }
538 
ReleaseAudioBuffer(MessageParcel & data,MessageParcel & reply)539 int32_t ScreenCaptureServiceStub::ReleaseAudioBuffer(MessageParcel &data, MessageParcel &reply)
540 {
541     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
542         "screen capture server is nullptr");
543     AudioCaptureSourceType type = static_cast<AudioCaptureSourceType>(data.ReadInt32());
544     int32_t ret = ReleaseAudioBuffer(type);
545     reply.WriteInt32(ret);
546     return MSERR_OK;
547 }
548 
ReleaseVideoBuffer(MessageParcel & data,MessageParcel & reply)549 int32_t ScreenCaptureServiceStub::ReleaseVideoBuffer(MessageParcel &data, MessageParcel &reply)
550 {
551     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
552         "screen capture server is nullptr");
553     (void)data;
554     int32_t ret = ReleaseVideoBuffer();
555     reply.WriteInt32(ret);
556     return MSERR_OK;
557 }
SetListenerObject(MessageParcel & data,MessageParcel & reply)558 int32_t ScreenCaptureServiceStub::SetListenerObject(MessageParcel &data, MessageParcel &reply)
559 {
560     sptr<IRemoteObject> object = data.ReadRemoteObject();
561     reply.WriteInt32(SetListenerObject(object));
562     return MSERR_OK;
563 }
564 
Release()565 void ScreenCaptureServiceStub::Release()
566 {
567     CHECK_AND_RETURN_LOG(screenCaptureServer_ != nullptr, "screen capture server is nullptr");
568     return screenCaptureServer_->Release();
569 }
570 
Release(MessageParcel & data,MessageParcel & reply)571 int32_t ScreenCaptureServiceStub::Release(MessageParcel &data, MessageParcel &reply)
572 {
573     (void)data;
574     (void)reply;
575     Release();
576     return MSERR_OK;
577 }
578 
DestroyStub(MessageParcel & data,MessageParcel & reply)579 int32_t ScreenCaptureServiceStub::DestroyStub(MessageParcel &data, MessageParcel &reply)
580 {
581     (void)data;
582     reply.WriteInt32(DestroyStub());
583     return MSERR_OK;
584 }
585 } // namespace Media
586 } // namespace OHOS
587