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_listener_proxy.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, "ScreenCaptureListenerProxy"};
22 }
23 
24 namespace OHOS {
25 namespace Media {
ScreenCaptureListenerProxy(const sptr<IRemoteObject> & impl)26 ScreenCaptureListenerProxy::ScreenCaptureListenerProxy(const sptr<IRemoteObject> &impl)
27     : IRemoteProxy<IStandardScreenCaptureListener>(impl)
28 {
29     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
30 }
31 
~ScreenCaptureListenerProxy()32 ScreenCaptureListenerProxy::~ScreenCaptureListenerProxy()
33 {
34     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
35 }
36 
OnError(ScreenCaptureErrorType errorType,int32_t errorCode)37 void ScreenCaptureListenerProxy::OnError(ScreenCaptureErrorType errorType, int32_t errorCode)
38 {
39     MessageParcel data;
40     MessageParcel reply;
41     MessageOption option(MessageOption::TF_ASYNC);
42 
43     bool token = data.WriteInterfaceToken(ScreenCaptureListenerProxy::GetDescriptor());
44     CHECK_AND_RETURN_LOG(token, "Failed to write descriptor!");
45 
46     data.WriteInt32(errorType);
47     data.WriteInt32(errorCode);
48     int error = Remote()->SendRequest(ScreenCaptureListenerMsg::ON_ERROR, data, reply, option);
49     CHECK_AND_RETURN_LOG(error == MSERR_OK, "on error failed, error: %{public}d", error);
50 }
51 
OnAudioBufferAvailable(bool isReady,AudioCaptureSourceType type)52 void ScreenCaptureListenerProxy::OnAudioBufferAvailable(bool isReady, AudioCaptureSourceType type)
53 {
54     MessageParcel data;
55     MessageParcel reply;
56     MessageOption option(MessageOption::TF_ASYNC);
57 
58     bool token = data.WriteInterfaceToken(ScreenCaptureListenerProxy::GetDescriptor());
59     CHECK_AND_RETURN_LOG(token, "Failed to write descriptor!");
60 
61     data.WriteBool(isReady);
62     data.WriteInt32(type);
63     int error = Remote()->SendRequest(ScreenCaptureListenerMsg::ON_AUDIO_AVAILABLE, data, reply, option);
64     CHECK_AND_RETURN_LOG(error == MSERR_OK, "OnAudioBufferAvailable failed, error: %{public}d", error);
65 }
66 
OnVideoBufferAvailable(bool isReady)67 void ScreenCaptureListenerProxy::OnVideoBufferAvailable(bool isReady)
68 {
69     MessageParcel data;
70     MessageParcel reply;
71     MessageOption option(MessageOption::TF_ASYNC);
72 
73     bool token = data.WriteInterfaceToken(ScreenCaptureListenerProxy::GetDescriptor());
74     CHECK_AND_RETURN_LOG(token, "Failed to write descriptor!");
75 
76     data.WriteBool(isReady);
77     int error = Remote()->SendRequest(ScreenCaptureListenerMsg::ON_VIDEO_AVAILABLE, data, reply, option);
78     CHECK_AND_RETURN_LOG(error == MSERR_OK, "OnVideoBufferAvailable failed, error: %{public}d", error);
79 }
80 
OnStateChange(AVScreenCaptureStateCode stateCode)81 void ScreenCaptureListenerProxy::OnStateChange(AVScreenCaptureStateCode stateCode)
82 {
83     MessageParcel data;
84     MessageParcel reply;
85     MessageOption option(MessageOption::TF_ASYNC);
86 
87     bool token = data.WriteInterfaceToken(ScreenCaptureListenerProxy::GetDescriptor());
88     CHECK_AND_RETURN_LOG(token, "Failed to write descriptor!");
89 
90     data.WriteInt32(stateCode);
91     int error = Remote()->SendRequest(ScreenCaptureListenerMsg::ON_STAGE_CHANGE, data, reply, option);
92     CHECK_AND_RETURN_LOG(error == MSERR_OK, "OnStateChange failed, error: %{public}d, stateCode: %{public}d",
93         error, stateCode);
94 }
95 
ScreenCaptureListenerCallback(const sptr<IStandardScreenCaptureListener> & listener)96 ScreenCaptureListenerCallback::ScreenCaptureListenerCallback(const sptr<IStandardScreenCaptureListener> &listener)
97     : listener_(listener)
98 {
99     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
100 }
101 
~ScreenCaptureListenerCallback()102 ScreenCaptureListenerCallback::~ScreenCaptureListenerCallback()
103 {
104     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
105 }
106 
OnError(ScreenCaptureErrorType errorType,int32_t errorCode)107 void ScreenCaptureListenerCallback::OnError(ScreenCaptureErrorType errorType, int32_t errorCode)
108 {
109     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances isStopped:%{public}d, errorType:%{public}d, errorCode:%{public}d",
110         FAKE_POINTER(this), isStopped_.load(), errorType, errorCode);
111     CHECK_AND_RETURN(isStopped_ == false);
112     if (listener_ != nullptr) {
113         listener_->OnError(errorType, errorCode);
114     }
115 }
116 
OnAudioBufferAvailable(bool isReady,AudioCaptureSourceType type)117 void ScreenCaptureListenerCallback::OnAudioBufferAvailable(bool isReady, AudioCaptureSourceType type)
118 {
119     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances isStopped:%{public}d, isReady:%{public}d, type:%{public}d",
120         FAKE_POINTER(this), isStopped_.load(), isReady, type);
121     CHECK_AND_RETURN(isStopped_ == false);
122     if (listener_ != nullptr) {
123         listener_->OnAudioBufferAvailable(isReady, type);
124     }
125 }
126 
OnVideoBufferAvailable(bool isReady)127 void ScreenCaptureListenerCallback::OnVideoBufferAvailable(bool isReady)
128 {
129     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances isStopped:%{public}d, isReady:%{public}d",
130         FAKE_POINTER(this), isStopped_.load(), isReady);
131     CHECK_AND_RETURN(isStopped_ == false);
132     if (listener_ != nullptr) {
133         listener_->OnVideoBufferAvailable(isReady);
134     }
135 }
136 
OnStateChange(AVScreenCaptureStateCode stateCode)137 void ScreenCaptureListenerCallback::OnStateChange(AVScreenCaptureStateCode stateCode)
138 {
139     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances isStopped:%{public}d, stateCode:%{public}d",
140         FAKE_POINTER(this), isStopped_.load(), stateCode);
141     if (listener_ != nullptr) {
142         listener_->OnStateChange(stateCode);
143     }
144 }
145 } // namespace Media
146 } // namespace OHOS