• 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 #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         case REMOVE_AUDIO_CAPTURER:
64             return HandleNotifyCapturerRemoved(data, reply);
65         case SET_DEFAULT_OUTPUT_DEVICE:
66             return HandleSetDefaultOutputDevice(data, reply);
67 #ifdef HAS_FEATURE_INNERCAPTURER
68         case LOAD_MODERN_INNER_CAPTURE_SINK:
69             return HandleLoadModernInnerCapSink(data, reply);
70         case UNLOAD_MODERN_INNER_CAPTURE_SINK:
71             return HandleUnloadModernInnerCapSink(data, reply);
72 #endif
73         default:
74             AUDIO_WARNING_LOG("OnRemoteRequest unsupported request code:%{public}d.", code);
75             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
76     }
77 }
78 
HandleGetProcessDeviceInfo(MessageParcel & data,MessageParcel & reply)79 int32_t PolicyProviderStub::HandleGetProcessDeviceInfo(MessageParcel &data, MessageParcel &reply)
80 {
81     AudioProcessConfig config;
82     int32_t ret = ProcessConfig::ReadConfigFromParcel(config, data);
83     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "ReadConfigFromParcel failed %{public}d", ret);
84     bool flag = data.ReadBool();
85     AudioDeviceDescriptor deviceInfo(AudioDeviceDescriptor::DEVICE_INFO);
86     ret = GetProcessDeviceInfo(config, flag, deviceInfo);
87     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "GetProcessDeviceInfo failed %{public}d", ret);
88     deviceInfo.Marshalling(reply);
89     return AUDIO_OK;
90 }
91 
HandleInitSharedVolume(MessageParcel & data,MessageParcel & reply)92 int32_t PolicyProviderStub::HandleInitSharedVolume(MessageParcel &data, MessageParcel &reply)
93 {
94     (void)data;
95     std::shared_ptr<AudioSharedMemory> buffer = nullptr;
96     int32_t ret = InitSharedVolume(buffer);
97     if (ret == SUCCESS && buffer != nullptr) {
98         ret = AudioSharedMemory::WriteToParcel(buffer, reply);
99     } else {
100         AUDIO_ERR_LOG("error: ResolveBuffer failed.");
101         return AUDIO_INVALID_PARAM;
102     }
103     return ret;
104 }
105 
HandleSetWakeupCapturer(MessageParcel & data,MessageParcel & reply)106 int32_t PolicyProviderStub::HandleSetWakeupCapturer(MessageParcel &data, MessageParcel &reply)
107 {
108     AudioProcessConfig config;
109     int32_t ret = ProcessConfig::ReadConfigFromParcel(config, data);
110     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_OPERATION_FAILED, "ReadConfigFromParcel failed %{public}d", ret);
111     ret = SetWakeUpAudioCapturerFromAudioServer(config);
112     reply.WriteInt32(ret);
113     return AUDIO_OK;
114 }
115 
HandleSetCapturer(MessageParcel & data,MessageParcel & reply)116 int32_t PolicyProviderStub::HandleSetCapturer(MessageParcel &data, MessageParcel &reply)
117 {
118     AudioCapturerInfo capturerInfo;
119     AudioStreamInfo streamInfo;
120     uint32_t sessionId;
121     capturerInfo.Unmarshalling(data);
122     streamInfo.Unmarshalling(data);
123     data.ReadUint32(sessionId);
124     int32_t ret = NotifyCapturerAdded(capturerInfo, streamInfo, sessionId);
125     reply.WriteInt32(ret);
126     return AUDIO_OK;
127 }
128 
HandleWakeupCapturerRemoved(MessageParcel & data,MessageParcel & reply)129 int32_t PolicyProviderStub::HandleWakeupCapturerRemoved(MessageParcel &data, MessageParcel &reply)
130 {
131     int32_t ret = NotifyWakeUpCapturerRemoved();
132     reply.WriteInt32(ret);
133     return AUDIO_OK;
134 }
135 
HandleIsAbsVolumeSupported(MessageParcel & data,MessageParcel & reply)136 int32_t PolicyProviderStub::HandleIsAbsVolumeSupported(MessageParcel &data, MessageParcel &reply)
137 {
138     bool ret = IsAbsVolumeSupported();
139     reply.WriteBool(ret);
140     return AUDIO_OK;
141 }
142 
HandleOffloadGetRenderPosition(MessageParcel & data,MessageParcel & reply)143 int32_t PolicyProviderStub::HandleOffloadGetRenderPosition(MessageParcel &data, MessageParcel &reply)
144 {
145     uint32_t delayValue = 0;
146     uint64_t sendDataSize = 0;
147     uint32_t timeStamp = 0;
148     int32_t ret = OffloadGetRenderPosition(delayValue, sendDataSize, timeStamp);
149     reply.WriteInt32(ret);
150     reply.WriteUint32(delayValue);
151     reply.WriteUint64(sendDataSize);
152     reply.WriteUint32(timeStamp);
153     return AUDIO_OK;
154 }
155 
HandleGetAndSaveClientType(MessageParcel & data,MessageParcel & reply)156 int32_t PolicyProviderStub::HandleGetAndSaveClientType(MessageParcel &data, MessageParcel &reply)
157 {
158     uint32_t uid = data.ReadUint32();
159     std::string bundleName = data.ReadString();
160     int32_t ret = GetAndSaveClientType(uid, bundleName);
161     reply.WriteInt32(ret);
162     return AUDIO_OK;
163 }
164 
HandleGetMaxRendererInstances(MessageParcel & data,MessageParcel & reply)165 int32_t PolicyProviderStub::HandleGetMaxRendererInstances(MessageParcel &data, MessageParcel &reply)
166 {
167     int32_t ret = GetMaxRendererInstances();
168     reply.WriteInt32(ret);
169     return AUDIO_OK;
170 }
171 
HandleConcurrencyFromServer(MessageParcel & data,MessageParcel & reply)172 int32_t PolicyProviderStub::HandleConcurrencyFromServer(MessageParcel &data, MessageParcel &reply)
173 {
174     AudioPipeType incomingPipe = static_cast<AudioPipeType>(data.ReadInt32());
175     int32_t ret = ActivateConcurrencyFromServer(incomingPipe);
176     reply.WriteInt32(ret);
177     return AUDIO_OK;
178 }
179 
HandleNotifyCapturerRemoved(MessageParcel & data,MessageParcel & reply)180 int32_t PolicyProviderStub::HandleNotifyCapturerRemoved(MessageParcel &data, MessageParcel &reply)
181 {
182     uint64_t sessionId = data.ReadUint64();
183     int32_t ret = NotifyCapturerRemoved(sessionId);
184     reply.WriteInt32(ret);
185     return AUDIO_OK;
186 }
187 
HandleSetDefaultOutputDevice(MessageParcel & data,MessageParcel & reply)188 int32_t PolicyProviderStub::HandleSetDefaultOutputDevice(MessageParcel &data, MessageParcel &reply)
189 {
190     int32_t deviceType = data.ReadInt32();
191     uint32_t sessionID = data.ReadUint32();
192     int32_t streamUsage = data.ReadInt32();
193     bool isRunning = data.ReadBool();
194     reply.WriteInt32(SetDefaultOutputDevice(static_cast<OHOS::AudioStandard::DeviceType>(deviceType),
195         sessionID, static_cast<OHOS::AudioStandard::StreamUsage>(streamUsage), isRunning));
196     return AUDIO_OK;
197 }
198 
199 #ifdef HAS_FEATURE_INNERCAPTURER
HandleLoadModernInnerCapSink(MessageParcel & data,MessageParcel & reply)200 int32_t PolicyProviderStub::HandleLoadModernInnerCapSink(MessageParcel &data, MessageParcel &reply)
201 {
202     int32_t innerCapId = data.ReadInt32();
203     reply.WriteInt32(LoadModernInnerCapSink(innerCapId));
204     return AUDIO_OK;
205 }
206 
HandleUnloadModernInnerCapSink(MessageParcel & data,MessageParcel & reply)207 int32_t PolicyProviderStub::HandleUnloadModernInnerCapSink(MessageParcel &data, MessageParcel &reply)
208 {
209     int32_t innerCapId = data.ReadInt32();
210     reply.WriteInt32(UnloadModernInnerCapSink(innerCapId));
211     return AUDIO_OK;
212 }
213 #endif
~PolicyProviderWrapper()214 PolicyProviderWrapper::~PolicyProviderWrapper()
215 {
216     policyWorker_ = nullptr;
217 }
218 
PolicyProviderWrapper(IPolicyProvider * policyWorker)219 PolicyProviderWrapper::PolicyProviderWrapper(IPolicyProvider *policyWorker) : policyWorker_(policyWorker)
220 {
221 }
222 
GetProcessDeviceInfo(const AudioProcessConfig & config,bool lockFlag,AudioDeviceDescriptor & deviceInfo)223 int32_t PolicyProviderWrapper::GetProcessDeviceInfo(const AudioProcessConfig &config, bool lockFlag,
224     AudioDeviceDescriptor &deviceInfo)
225 {
226     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
227     return policyWorker_->GetProcessDeviceInfo(config, lockFlag, deviceInfo);
228 }
229 
InitSharedVolume(std::shared_ptr<AudioSharedMemory> & buffer)230 int32_t PolicyProviderWrapper::InitSharedVolume(std::shared_ptr<AudioSharedMemory> &buffer)
231 {
232     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
233     return policyWorker_->InitSharedVolume(buffer);
234 }
235 
SetWakeUpAudioCapturerFromAudioServer(const AudioProcessConfig & config)236 int32_t PolicyProviderWrapper::SetWakeUpAudioCapturerFromAudioServer(const AudioProcessConfig &config)
237 {
238     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
239     return policyWorker_->SetWakeUpAudioCapturerFromAudioServer(config);
240 }
241 
NotifyCapturerAdded(AudioCapturerInfo capturerInfo,AudioStreamInfo streamInfo,uint32_t sessionId)242 int32_t PolicyProviderWrapper::NotifyCapturerAdded(AudioCapturerInfo capturerInfo, AudioStreamInfo streamInfo,
243     uint32_t sessionId)
244 {
245     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
246     return policyWorker_->NotifyCapturerAdded(capturerInfo, streamInfo, sessionId);
247 }
248 
NotifyWakeUpCapturerRemoved()249 int32_t PolicyProviderWrapper::NotifyWakeUpCapturerRemoved()
250 {
251     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
252     return policyWorker_->NotifyWakeUpCapturerRemoved();
253 }
254 
IsAbsVolumeSupported()255 bool PolicyProviderWrapper::IsAbsVolumeSupported()
256 {
257     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
258     return policyWorker_->IsAbsVolumeSupported();
259 }
260 
OffloadGetRenderPosition(uint32_t & delayValue,uint64_t & sendDataSize,uint32_t & timeStamp)261 int32_t PolicyProviderWrapper::OffloadGetRenderPosition(uint32_t &delayValue, uint64_t &sendDataSize,
262     uint32_t &timeStamp)
263 {
264     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
265     return policyWorker_->OffloadGetRenderPosition(delayValue, sendDataSize, timeStamp);
266 }
267 
GetAndSaveClientType(uint32_t uid,const std::string & bundleName)268 int32_t PolicyProviderWrapper::GetAndSaveClientType(uint32_t uid, const std::string &bundleName)
269 {
270     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
271     return policyWorker_->GetAndSaveClientType(uid, bundleName);
272 }
273 
GetMaxRendererInstances()274 int32_t PolicyProviderWrapper::GetMaxRendererInstances()
275 {
276     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
277     return policyWorker_->GetMaxRendererInstances();
278 }
279 
ActivateConcurrencyFromServer(AudioPipeType incomingPipe)280 int32_t PolicyProviderWrapper::ActivateConcurrencyFromServer(AudioPipeType incomingPipe)
281 {
282     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
283     return policyWorker_->ActivateConcurrencyFromServer(incomingPipe);
284 }
285 
NotifyCapturerRemoved(uint64_t sessionId)286 int32_t PolicyProviderWrapper::NotifyCapturerRemoved(uint64_t sessionId)
287 {
288     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
289     return policyWorker_->NotifyCapturerRemoved(sessionId);
290 }
SetDefaultOutputDevice(const DeviceType defaultOutputDevice,const uint32_t sessionID,const StreamUsage streamUsage,bool isRunning)291 int32_t PolicyProviderWrapper::SetDefaultOutputDevice(const DeviceType defaultOutputDevice,
292     const uint32_t sessionID, const StreamUsage streamUsage, bool isRunning)
293 {
294     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
295     return policyWorker_->SetDefaultOutputDevice(defaultOutputDevice, sessionID, streamUsage, isRunning);
296 }
297 
298 #ifdef HAS_FEATURE_INNERCAPTURER
LoadModernInnerCapSink(int32_t innerCapId)299 int32_t PolicyProviderWrapper::LoadModernInnerCapSink(int32_t innerCapId)
300 {
301     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
302     return policyWorker_->LoadModernInnerCapSink(innerCapId);
303 }
304 
UnloadModernInnerCapSink(int32_t innerCapId)305 int32_t PolicyProviderWrapper::UnloadModernInnerCapSink(int32_t innerCapId)
306 {
307     CHECK_AND_RETURN_RET_LOG(policyWorker_ != nullptr, AUDIO_INIT_FAIL, "policyWorker_ is null");
308     return policyWorker_->UnloadModernInnerCapSink(innerCapId);
309 }
310 #endif
311 } // namespace AudioStandard
312 } // namespace OHOS
313