• 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 OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "ScreenCaptureServiceStub"};
25 }
26 
27 namespace OHOS {
28 namespace Media {
Create()29 sptr<ScreenCaptureServiceStub> ScreenCaptureServiceStub::Create()
30 {
31     sptr<ScreenCaptureServiceStub> screenCaptureStub = new(std::nothrow) ScreenCaptureServiceStub();
32     CHECK_AND_RETURN_RET_LOG(screenCaptureStub != nullptr, nullptr, "failed to new ScreenCaptureServiceStub");
33 
34     int32_t ret = screenCaptureStub->Init();
35     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "failed to screenCapture stub init");
36     return screenCaptureStub;
37 }
38 
ScreenCaptureServiceStub()39 ScreenCaptureServiceStub::ScreenCaptureServiceStub()
40 {
41     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
42 }
43 
~ScreenCaptureServiceStub()44 ScreenCaptureServiceStub::~ScreenCaptureServiceStub()
45 {
46     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
47 }
48 
Init()49 int32_t ScreenCaptureServiceStub::Init()
50 {
51     screenCaptureServer_ = ScreenCaptureServer::Create();
52     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_NO_MEMORY,
53         "failed to create ScreenCaptureServer Service");
54     screenCaptureStubFuncs_[SET_LISTENER_OBJ] = &ScreenCaptureServiceStub::SetListenerObject;
55     screenCaptureStubFuncs_[RELEASE] = &ScreenCaptureServiceStub::Release;
56     screenCaptureStubFuncs_[SET_MIC_ENABLE] = &ScreenCaptureServiceStub::SetMicrophoneEnabled;
57     screenCaptureStubFuncs_[SET_CAPTURE_MODE] = &ScreenCaptureServiceStub::SetCaptureMode;
58     screenCaptureStubFuncs_[INIT_AUDIO_CAP] = &ScreenCaptureServiceStub::InitAudioCap;
59     screenCaptureStubFuncs_[INIT_VIDEO_CAP] = &ScreenCaptureServiceStub::InitVideoCap;
60     screenCaptureStubFuncs_[START_SCREEN_CAPTURE] = &ScreenCaptureServiceStub::StartScreenCapture;
61     screenCaptureStubFuncs_[STOP_SCREEN_CAPTURE] = &ScreenCaptureServiceStub::StopScreenCapture;
62     screenCaptureStubFuncs_[ACQUIRE_AUDIO_BUF] = &ScreenCaptureServiceStub::AcquireAudioBuffer;
63     screenCaptureStubFuncs_[ACQUIRE_VIDEO_BUF] = &ScreenCaptureServiceStub::AcquireVideoBuffer;
64     screenCaptureStubFuncs_[RELEASE_AUDIO_BUF] = &ScreenCaptureServiceStub::ReleaseAudioBuffer;
65     screenCaptureStubFuncs_[RELEASE_VIDEO_BUF] = &ScreenCaptureServiceStub::ReleaseVideoBuffer;
66     screenCaptureStubFuncs_[DESTROY] = &ScreenCaptureServiceStub::DestroyStub;
67 
68     return MSERR_OK;
69 }
70 
DestroyStub()71 int32_t ScreenCaptureServiceStub::DestroyStub()
72 {
73     screenCaptureServer_ = nullptr;
74     MediaServerManager::GetInstance().DestroyStubObject(MediaServerManager::SCREEN_CAPTURE, AsObject());
75     return MSERR_OK;
76 }
77 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)78 int ScreenCaptureServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
79     MessageOption &option)
80 {
81     MEDIA_LOGD("Stub: OnRemoteRequest of code: %{public}u is received", code);
82 
83     auto remoteDescriptor = data.ReadInterfaceToken();
84     if (ScreenCaptureServiceStub::GetDescriptor() != remoteDescriptor) {
85         MEDIA_LOGE("Invalid descriptor");
86         return MSERR_INVALID_OPERATION;
87     }
88 
89     auto itFunc = screenCaptureStubFuncs_.find(code);
90     if (itFunc != screenCaptureStubFuncs_.end()) {
91         auto memberFunc = itFunc->second;
92         if (memberFunc != nullptr) {
93             int32_t ret = (this->*memberFunc)(data, reply);
94             if (ret != MSERR_OK) {
95                 MEDIA_LOGE("Calling memberFunc is failed.");
96             }
97             return MSERR_OK;
98         }
99     }
100     MEDIA_LOGW("ScreenCaptureServiceStub: no member func supporting, applying default process");
101 
102     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
103 }
104 
SetCaptureMode(CaptureMode captureMode)105 int32_t ScreenCaptureServiceStub::SetCaptureMode(CaptureMode captureMode)
106 {
107     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
108         "screen capture server is nullptr");
109     return screenCaptureServer_->SetCaptureMode(captureMode);
110 }
111 
InitAudioCap(AudioCaptureInfo audioInfo)112 int32_t ScreenCaptureServiceStub::InitAudioCap(AudioCaptureInfo audioInfo)
113 {
114     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
115         "screen capture server is nullptr");
116     return screenCaptureServer_->InitAudioCap(audioInfo);
117 }
118 
InitVideoCap(VideoCaptureInfo videoInfo)119 int32_t ScreenCaptureServiceStub::InitVideoCap(VideoCaptureInfo videoInfo)
120 {
121     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
122         "screen capture server is nullptr");
123     return screenCaptureServer_->InitVideoCap(videoInfo);
124 }
125 
StartScreenCapture()126 int32_t ScreenCaptureServiceStub::StartScreenCapture()
127 {
128     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
129         "screen capture server is nullptr");
130     return screenCaptureServer_->StartScreenCapture();
131 }
132 
StopScreenCapture()133 int32_t ScreenCaptureServiceStub::StopScreenCapture()
134 {
135     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
136         "screen capture server is nullptr");
137     return screenCaptureServer_->StopScreenCapture();
138 }
139 
SetListenerObject(const sptr<IRemoteObject> & object)140 int32_t ScreenCaptureServiceStub::SetListenerObject(const sptr<IRemoteObject> &object)
141 {
142     CHECK_AND_RETURN_RET_LOG(object != nullptr, MSERR_NO_MEMORY, "set listener object is nullptr");
143 
144     sptr<IStandardScreenCaptureListener> listener = iface_cast<IStandardScreenCaptureListener>(object);
145     CHECK_AND_RETURN_RET_LOG(listener != nullptr, MSERR_NO_MEMORY, "failed to convert IStandardScreenCaptureListener");
146 
147     std::shared_ptr<ScreenCaptureCallBack> callback = std::make_shared<ScreenCaptureListenerCallback>(listener);
148     CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_NO_MEMORY, "failed to new ScreenCaptureCallBack");
149 
150     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_NO_MEMORY, "recorder server is nullptr");
151     (void)screenCaptureServer_->SetScreenCaptureCallback(callback);
152     return MSERR_OK;
153 }
154 
SetMicrophoneEnabled(bool isMicrophone)155 int32_t ScreenCaptureServiceStub::SetMicrophoneEnabled(bool isMicrophone)
156 {
157     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
158         "screen capture server is nullptr");
159     return screenCaptureServer_->SetMicrophoneEnabled(isMicrophone);
160 }
161 
AcquireAudioBuffer(std::shared_ptr<AudioBuffer> & audioBuffer,AudioCaptureSourceType type)162 int32_t ScreenCaptureServiceStub::AcquireAudioBuffer(std::shared_ptr<AudioBuffer> &audioBuffer,
163                                                      AudioCaptureSourceType type)
164 {
165     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
166         "screen capture server is nullptr");
167     return screenCaptureServer_->AcquireAudioBuffer(audioBuffer, type);
168 }
169 
AcquireVideoBuffer(sptr<OHOS::SurfaceBuffer> & surfaceBuffer,int32_t & fence,int64_t & timestamp,OHOS::Rect & damage)170 int32_t ScreenCaptureServiceStub::AcquireVideoBuffer(sptr<OHOS::SurfaceBuffer> &surfaceBuffer, int32_t &fence,
171                                                      int64_t &timestamp, OHOS::Rect &damage)
172 {
173     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
174         "screen capture server is nullptr");
175     return screenCaptureServer_->AcquireVideoBuffer(surfaceBuffer, fence, timestamp, damage);
176 }
177 
ReleaseAudioBuffer(AudioCaptureSourceType type)178 int32_t ScreenCaptureServiceStub::ReleaseAudioBuffer(AudioCaptureSourceType type)
179 {
180     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
181         "screen capture server is nullptr");
182     return screenCaptureServer_->ReleaseAudioBuffer(type);
183 }
184 
ReleaseVideoBuffer()185 int32_t ScreenCaptureServiceStub::ReleaseVideoBuffer()
186 {
187     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, false,
188         "screen capture server is nullptr");
189     return screenCaptureServer_->ReleaseVideoBuffer();
190 }
191 
SetMicrophoneEnabled(MessageParcel & data,MessageParcel & reply)192 int32_t ScreenCaptureServiceStub::SetMicrophoneEnabled(MessageParcel &data, MessageParcel &reply)
193 {
194     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
195         "screen capture server is nullptr");
196     (void)data;
197     bool setMicEnable = data.ReadBool();
198     int32_t ret = SetMicrophoneEnabled(setMicEnable);
199     reply.WriteInt32(ret);
200     return MSERR_OK;
201 }
202 
SetCaptureMode(MessageParcel & data,MessageParcel & reply)203 int32_t ScreenCaptureServiceStub::SetCaptureMode(MessageParcel &data, MessageParcel &reply)
204 {
205     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
206         "screen capture server is nullptr");
207     (void)data;
208     CaptureMode mode = static_cast<CaptureMode>(data.ReadInt32());
209     int32_t ret = SetCaptureMode(mode);
210     reply.WriteInt32(ret);
211     return MSERR_OK;
212 }
213 
InitAudioCap(MessageParcel & data,MessageParcel & reply)214 int32_t ScreenCaptureServiceStub::InitAudioCap(MessageParcel &data, MessageParcel &reply)
215 {
216     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
217         "screen capture server is nullptr");
218     (void)data;
219     AudioCaptureInfo audioInfo;
220     audioInfo.audioSampleRate = data.ReadInt32();
221     audioInfo.audioChannels = data.ReadInt32();
222     audioInfo.audioSource = static_cast<AudioCaptureSourceType>(data.ReadInt32());
223     int32_t ret = InitAudioCap(audioInfo);
224     reply.WriteInt32(ret);
225     return MSERR_OK;
226 }
227 
InitVideoCap(MessageParcel & data,MessageParcel & reply)228 int32_t ScreenCaptureServiceStub::InitVideoCap(MessageParcel &data, MessageParcel &reply)
229 {
230     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
231         "screen capture server is nullptr");
232     (void)data;
233     VideoCaptureInfo videoInfo;
234     videoInfo.displayId = data.ReadUint64();
235     int32_t size = data.ReadInt32();
236     if (size > 0) {
237         for (auto i = 0; i < size; i++) {
238             videoInfo.taskIDs.push_back(data.ReadInt32());
239         }
240     }
241     videoInfo.videoFrameWidth = data.ReadInt32();
242     videoInfo.videoFrameHeight = data.ReadInt32();
243     videoInfo.videoSource = static_cast<VideoSourceType>(data.ReadInt32());
244     int32_t ret = InitVideoCap(videoInfo);
245     reply.WriteInt32(ret);
246     return MSERR_OK;
247 }
248 
StartScreenCapture(MessageParcel & data,MessageParcel & reply)249 int32_t ScreenCaptureServiceStub::StartScreenCapture(MessageParcel &data, MessageParcel &reply)
250 {
251     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
252         "screen capture server is nullptr");
253     (void)data;
254     int32_t ret = StartScreenCapture();
255     reply.WriteInt32(ret);
256     return MSERR_OK;
257 }
258 
StopScreenCapture(MessageParcel & data,MessageParcel & reply)259 int32_t ScreenCaptureServiceStub::StopScreenCapture(MessageParcel &data, MessageParcel &reply)
260 {
261     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
262         "screen capture server is nullptr");
263     (void)data;
264     int32_t ret = StopScreenCapture();
265     reply.WriteInt32(ret);
266     return MSERR_OK;
267 }
268 
AcquireAudioBuffer(MessageParcel & data,MessageParcel & reply)269 int32_t ScreenCaptureServiceStub::AcquireAudioBuffer(MessageParcel &data, MessageParcel &reply)
270 {
271     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
272         "screen capture server is nullptr");
273     (void)data;
274     std::shared_ptr<AudioBuffer> audioBuffer;
275     AudioCaptureSourceType type = static_cast<AudioCaptureSourceType>(data.ReadInt32());
276     int32_t ret = AcquireAudioBuffer(audioBuffer, type);
277     reply.WriteInt32(ret);
278     if (ret == MSERR_OK) {
279         reply.WriteInt32(audioBuffer->length);
280         if ((audioBuffer->buffer != nullptr)&&(audioBuffer->length > 0)) {
281             reply.WriteBuffer(audioBuffer->buffer, audioBuffer->length);
282         }
283         reply.WriteInt32(audioBuffer->sourcetype);
284         reply.WriteInt64(audioBuffer->timestamp);
285     }
286     return MSERR_OK;
287 }
288 
AcquireVideoBuffer(MessageParcel & data,MessageParcel & reply)289 int32_t ScreenCaptureServiceStub::AcquireVideoBuffer(MessageParcel &data, MessageParcel &reply)
290 {
291     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
292         "screen capture server is nullptr");
293     (void)data;
294     int32_t fence = 0;
295     int64_t timestamp = 0;
296     OHOS::Rect damage;
297     sptr<OHOS::SurfaceBuffer> videoBuffer = nullptr;
298     int32_t ret = AcquireVideoBuffer(videoBuffer, fence, timestamp, damage);
299     reply.WriteInt32(ret);
300     if (ret == MSERR_OK) {
301         if (videoBuffer != nullptr) {
302             videoBuffer->WriteToMessageParcel(reply);
303         }
304         reply.WriteInt32(fence);
305         reply.WriteInt64(timestamp);
306         reply.WriteInt32(damage.x);
307         reply.WriteInt32(damage.y);
308         reply.WriteInt32(damage.w);
309         reply.WriteInt32(damage.h);
310     }
311     return MSERR_OK;
312 }
313 
ReleaseAudioBuffer(MessageParcel & data,MessageParcel & reply)314 int32_t ScreenCaptureServiceStub::ReleaseAudioBuffer(MessageParcel &data, MessageParcel &reply)
315 {
316     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
317         "screen capture server is nullptr");
318     (void)data;
319     AudioCaptureSourceType type = static_cast<AudioCaptureSourceType>(data.ReadInt32());
320     int32_t ret = ReleaseAudioBuffer(type);
321     reply.WriteInt32(ret);
322     return MSERR_OK;
323 }
324 
ReleaseVideoBuffer(MessageParcel & data,MessageParcel & reply)325 int32_t ScreenCaptureServiceStub::ReleaseVideoBuffer(MessageParcel &data, MessageParcel &reply)
326 {
327     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, MSERR_INVALID_STATE,
328         "screen capture server is nullptr");
329     (void)data;
330     int32_t ret = ReleaseVideoBuffer();
331     reply.WriteInt32(ret);
332     return MSERR_OK;
333 }
SetListenerObject(MessageParcel & data,MessageParcel & reply)334 int32_t ScreenCaptureServiceStub::SetListenerObject(MessageParcel &data, MessageParcel &reply)
335 {
336     sptr<IRemoteObject> object = data.ReadRemoteObject();
337     reply.WriteInt32(SetListenerObject(object));
338     return MSERR_OK;
339 }
340 
Release()341 void ScreenCaptureServiceStub::Release()
342 {
343     CHECK_AND_RETURN_LOG(screenCaptureServer_ != nullptr, "screen capture server is nullptr");
344     return screenCaptureServer_->Release();
345 }
346 
Release(MessageParcel & data,MessageParcel & reply)347 int32_t ScreenCaptureServiceStub::Release(MessageParcel &data, MessageParcel &reply)
348 {
349     (void)data;
350     (void)reply;
351     Release();
352     return MSERR_OK;
353 }
354 
DestroyStub(MessageParcel & data,MessageParcel & reply)355 int32_t ScreenCaptureServiceStub::DestroyStub(MessageParcel &data, MessageParcel &reply)
356 {
357     (void)data;
358     reply.WriteInt32(DestroyStub());
359     return MSERR_OK;
360 }
361 } // namespace Media
362 } // namespace OHOS