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 #ifndef LOG_TAG
16 #define LOG_TAG "PolicyProviderStub"
17 #endif
18 
19 #include "policy_provider_stub.h"
20 #include "audio_service_log.h"
21 #include "audio_errors.h"
22 
23 namespace OHOS {
24 namespace AudioStandard {
CheckInterfaceToken(MessageParcel & data)25 bool PolicyProviderStub::CheckInterfaceToken(MessageParcel &data)
26 {
27     static auto localDescriptor = IPolicyProviderIpc::GetDescriptor();
28     auto remoteDescriptor = data.ReadInterfaceToken();
29     CHECK_AND_RETURN_RET_LOG(remoteDescriptor == localDescriptor, false, "CheckInterFfaceToken failed.");
30     return true;
31 }
32 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)33 int PolicyProviderStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
34     MessageOption &option)
35 {
36     bool ret = CheckInterfaceToken(data);
37     CHECK_AND_RETURN_RET(ret, AUDIO_ERR);
38     if (code >= IPolicyProviderMsg::POLICY_PROVIDER_MAX_MSG) {
39         AUDIO_WARNING_LOG("OnRemoteRequest unsupported request code:%{public}d.", code);
40         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
41     }
42     switch (code) {
43         case GET_DEVICE_INFO:
44             return HandleGetProcessDeviceInfo(data, reply);
45         case INIT_VOLUME_MAP:
46             return HandleInitSharedVolume(data, reply);
47         case SET_WAKEUP_ADUIO_CAPTURER:
48             return HandleSetWakeupCapturer(data, reply);
49         case SET_AUDIO_CAPTURER:
50             return HandleSetCapturer(data, reply);
51         case REMOVE_WAKEUP_CAPUTER:
52             return HandleWakeupCapturerRemoved(data, reply);
53         case IS_ABS_VOLUME_SUPPORTED:
54             return HandleIsAbsVolumeSupported(data, reply);
55         case OFFLOAD_GET_RENDER_POSITION:
56             return HandleOffloadGetRenderPosition(data, reply);
57         case GET_AND_SAVE_CLIENT_TYPE:
58             return HandleGetAndSaveClientType(data, reply);
59         case GET_MAX_RENDERER_INSTANCES:
60             return HandleGetMaxRendererInstances(data, reply);
61         case ACTIVATE_CONCURRENCY_FROM_SERVER:
62             return HandleConcurrencyFromServer(data, reply);
63         default:
64             AUDIO_WARNING_LOG("OnRemoteRequest unsupported request code:%{public}d.", code);
65             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
66     }
67 }
68 
HandleGetProcessDeviceInfo(MessageParcel & data,MessageParcel & reply)69 int32_t PolicyProviderStub::HandleGetProcessDeviceInfo(MessageParcel &data, MessageParcel &reply)
70 {
71     AudioProcessConfig config;
72     int32_t ret = ProcessConfig::ReadConfigFromParcel(config, data);
73     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "ReadConfigFromParcel failed %{public}d", ret);
74     bool flag = data.ReadBool();
75     DeviceInfo deviceInfo;
76     ret = GetProcessDeviceInfo(config, flag, deviceInfo);
77     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "GetProcessDeviceInfo failed %{public}d", ret);
78     deviceInfo.Marshalling(reply);
79     return AUDIO_OK;
80 }
81 
HandleInitSharedVolume(MessageParcel & data,MessageParcel & reply)82 int32_t PolicyProviderStub::HandleInitSharedVolume(MessageParcel &data, MessageParcel &reply)
83 {
84     (void)data;
85     std::shared_ptr<AudioSharedMemory> buffer = nullptr;
86     int32_t ret = InitSharedVolume(buffer);
87     if (ret == SUCCESS && buffer != nullptr) {
88         ret = AudioSharedMemory::WriteToParcel(buffer, reply);
89     } else {
90         AUDIO_ERR_LOG("error: ResolveBuffer failed.");
91         return AUDIO_INVALID_PARAM;
92     }
93     return ret;
94 }
95 
HandleSetWakeupCapturer(MessageParcel & data,MessageParcel & reply)96 int32_t PolicyProviderStub::HandleSetWakeupCapturer(MessageParcel &data, MessageParcel &reply)
97 {
98     AudioProcessConfig config;
99     int32_t ret = ProcessConfig::ReadConfigFromParcel(config, data);
100     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "ReadConfigFromParcel failed %{public}d", ret);
101     ret = SetWakeUpAudioCapturerFromAudioServer(config);
102     reply.WriteInt32(ret);
103     return AUDIO_OK;
104 }
105 
HandleSetCapturer(MessageParcel & data,MessageParcel & reply)106 int32_t PolicyProviderStub::HandleSetCapturer(MessageParcel &data, MessageParcel &reply)
107 {
108     AudioCapturerInfo capturerInfo;
109     AudioStreamInfo streamInfo;
110     uint32_t sessionId;
111     capturerInfo.Unmarshalling(data);
112     streamInfo.Unmarshalling(data);
113     data.ReadUint32(sessionId);
114     int32_t ret = NotifyCapturerAdded(capturerInfo, streamInfo, sessionId);
115     reply.WriteInt32(ret);
116     return AUDIO_OK;
117 }
118 
HandleWakeupCapturerRemoved(MessageParcel & data,MessageParcel & reply)119 int32_t PolicyProviderStub::HandleWakeupCapturerRemoved(MessageParcel &data, MessageParcel &reply)
120 {
121     int32_t ret = NotifyWakeUpCapturerRemoved();
122     reply.WriteInt32(ret);
123     return AUDIO_OK;
124 }
125 
HandleIsAbsVolumeSupported(MessageParcel & data,MessageParcel & reply)126 int32_t PolicyProviderStub::HandleIsAbsVolumeSupported(MessageParcel &data, MessageParcel &reply)
127 {
128     bool ret = IsAbsVolumeSupported();
129     reply.WriteBool(ret);
130     return AUDIO_OK;
131 }
132 
HandleOffloadGetRenderPosition(MessageParcel & data,MessageParcel & reply)133 int32_t PolicyProviderStub::HandleOffloadGetRenderPosition(MessageParcel &data, MessageParcel &reply)
134 {
135     uint32_t delayValue = 0;
136     uint64_t sendDataSize = 0;
137     uint32_t timeStamp = 0;
138     int32_t ret = OffloadGetRenderPosition(delayValue, sendDataSize, timeStamp);
139     reply.WriteInt32(ret);
140     reply.WriteUint32(delayValue);
141     reply.WriteUint64(sendDataSize);
142     reply.WriteUint32(timeStamp);
143     return AUDIO_OK;
144 }
145 
HandleGetAndSaveClientType(MessageParcel & data,MessageParcel & reply)146 int32_t PolicyProviderStub::HandleGetAndSaveClientType(MessageParcel &data, MessageParcel &reply)
147 {
148     uint32_t uid = data.ReadUint32();
149     std::string bundleName = data.ReadString();
150     int32_t ret = GetAndSaveClientType(uid, bundleName);
151     reply.WriteInt32(ret);
152     return AUDIO_OK;
153 }
154 
HandleGetMaxRendererInstances(MessageParcel & data,MessageParcel & reply)155 int32_t PolicyProviderStub::HandleGetMaxRendererInstances(MessageParcel &data, MessageParcel &reply)
156 {
157     int32_t ret = GetMaxRendererInstances();
158     reply.WriteInt32(ret);
159     return AUDIO_OK;
160 }
161 
HandleConcurrencyFromServer(MessageParcel & data,MessageParcel & reply)162 int32_t PolicyProviderStub::HandleConcurrencyFromServer(MessageParcel &data, MessageParcel &reply)
163 {
164     AudioPipeType incomingPipe = static_cast<AudioPipeType>(data.ReadInt32());
165     int32_t ret = ActivateConcurrencyFromServer(incomingPipe);
166     reply.WriteInt32(ret);
167     return AUDIO_OK;
168 }
169 
~PolicyProviderWrapper()170 PolicyProviderWrapper::~PolicyProviderWrapper()
171 {
172     policyWorker_ = nullptr;
173 }
174 
PolicyProviderWrapper(IPolicyProvider * policyWorker)175 PolicyProviderWrapper::PolicyProviderWrapper(IPolicyProvider *policyWorker) : policyWorker_(policyWorker)
176 {
177 }
178 
GetProcessDeviceInfo(const AudioProcessConfig & config,bool lockFlag,DeviceInfo & deviceInfo)179 int32_t PolicyProviderWrapper::GetProcessDeviceInfo(const AudioProcessConfig &config, bool lockFlag,
180     DeviceInfo &deviceInfo)
181 {
182     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
183     return policyWorker_->GetProcessDeviceInfo(config, lockFlag, deviceInfo);
184 }
185 
InitSharedVolume(std::shared_ptr<AudioSharedMemory> & buffer)186 int32_t PolicyProviderWrapper::InitSharedVolume(std::shared_ptr<AudioSharedMemory> &buffer)
187 {
188     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
189     return policyWorker_->InitSharedVolume(buffer);
190 }
191 
SetWakeUpAudioCapturerFromAudioServer(const AudioProcessConfig & config)192 int32_t PolicyProviderWrapper::SetWakeUpAudioCapturerFromAudioServer(const AudioProcessConfig &config)
193 {
194     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
195     return policyWorker_->SetWakeUpAudioCapturerFromAudioServer(config);
196 }
197 
NotifyCapturerAdded(AudioCapturerInfo capturerInfo,AudioStreamInfo streamInfo,uint32_t sessionId)198 int32_t PolicyProviderWrapper::NotifyCapturerAdded(AudioCapturerInfo capturerInfo, AudioStreamInfo streamInfo,
199     uint32_t sessionId)
200 {
201     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
202     return policyWorker_->NotifyCapturerAdded(capturerInfo, streamInfo, sessionId);
203 }
204 
NotifyWakeUpCapturerRemoved()205 int32_t PolicyProviderWrapper::NotifyWakeUpCapturerRemoved()
206 {
207     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
208     return policyWorker_->NotifyWakeUpCapturerRemoved();
209 }
210 
IsAbsVolumeSupported()211 bool PolicyProviderWrapper::IsAbsVolumeSupported()
212 {
213     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
214     return policyWorker_->IsAbsVolumeSupported();
215 }
216 
OffloadGetRenderPosition(uint32_t & delayValue,uint64_t & sendDataSize,uint32_t & timeStamp)217 int32_t PolicyProviderWrapper::OffloadGetRenderPosition(uint32_t &delayValue, uint64_t &sendDataSize,
218     uint32_t &timeStamp)
219 {
220     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
221     return policyWorker_->OffloadGetRenderPosition(delayValue, sendDataSize, timeStamp);
222 }
223 
GetAndSaveClientType(uint32_t uid,const std::string & bundleName)224 int32_t PolicyProviderWrapper::GetAndSaveClientType(uint32_t uid, const std::string &bundleName)
225 {
226     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
227     return policyWorker_->GetAndSaveClientType(uid, bundleName);
228 }
229 
GetMaxRendererInstances()230 int32_t PolicyProviderWrapper::GetMaxRendererInstances()
231 {
232     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
233     return policyWorker_->GetMaxRendererInstances();
234 }
235 
ActivateConcurrencyFromServer(AudioPipeType incomingPipe)236 int32_t PolicyProviderWrapper::ActivateConcurrencyFromServer(AudioPipeType incomingPipe)
237 {
238     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
239     return policyWorker_->ActivateConcurrencyFromServer(incomingPipe);
240 }
241 } // namespace AudioStandard
242 } // namespace OHOS
243