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