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 "IpcStreamStub"
17 #endif
18
19 #include "ipc_stream_stub.h"
20 #include "audio_service_log.h"
21 #include "audio_errors.h"
22 #include "audio_process_config.h"
23 #include "audio_utils.h"
24
25 namespace OHOS {
26 namespace AudioStandard {
CheckInterfaceToken(MessageParcel & data)27 bool IpcStreamStub::CheckInterfaceToken(MessageParcel &data)
28 {
29 static auto localDescriptor = IpcStream::GetDescriptor();
30 auto remoteDescriptor = data.ReadInterfaceToken();
31 if (remoteDescriptor != localDescriptor) {
32 AUDIO_ERR_LOG("CheckInterFfaceToken failed.");
33 return false;
34 }
35 return true;
36 }
37
OnMiddleCodeRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)38 int IpcStreamStub::OnMiddleCodeRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
39 MessageOption &option)
40 {
41 switch (code) {
42 case ON_GET_RATE:
43 return HandleGetRate(data, reply);
44 case ON_SET_LOWPOWER_VOLUME:
45 return HandleSetLowPowerVolume(data, reply);
46 case ON_GET_LOWPOWER_VOLUME:
47 return HandleGetLowPowerVolume(data, reply);
48 case ON_SET_EFFECT_MODE:
49 return HandleSetAudioEffectMode(data, reply);
50 case ON_GET_EFFECT_MODE:
51 return HandleGetAudioEffectMode(data, reply);
52 case ON_SET_PRIVACY_TYPE:
53 return HandleSetPrivacyType(data, reply);
54 case ON_GET_PRIVACY_TYPE:
55 return HandleGetPrivacyType(data, reply);
56 case ON_SET_OFFLOAD_MODE:
57 return HandleSetOffloadMode(data, reply);
58 case ON_UNSET_OFFLOAD_MODE:
59 return HandleUnsetOffloadMode(data, reply);
60 case ON_GET_OFFLOAD_APPROXIMATELY_CACHE_TIME:
61 return HandleGetOffloadApproximatelyCacheTime(data, reply);
62 case ON_UPDATE_SPATIALIZATION_STATE:
63 return HandleUpdateSpatializationState(data, reply);
64 case ON_GET_STREAM_MANAGER_TYPE:
65 return HandleGetStreamManagerType(data, reply);
66 case ON_SET_SILENT_MODE_AND_MIX_WITH_OTHERS:
67 return HandleSetSilentModeAndMixWithOthers(data, reply);
68 case ON_SET_CLIENT_VOLUME:
69 return HandleSetClientVolume(data, reply);
70 case ON_SET_MUTE:
71 return HandleSetMute(data, reply);
72 case ON_SET_DUCK_FACTOR:
73 return HandleSetDuckFactor(data, reply);
74 case ON_REGISTER_THREAD_PRIORITY:
75 return HandleRegisterThreadPriority(data, reply);
76 default:
77 AUDIO_WARNING_LOG("OnRemoteRequest unsupported request code:%{public}d.", code);
78 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
79 }
80 }
81
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)82 int IpcStreamStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
83 {
84 if (!CheckInterfaceToken(data)) {
85 return AUDIO_ERR;
86 }
87 Trace trace("IpcStream::Handle::" + std::to_string(code));
88 if (code >= IpcStreamMsg::IPC_STREAM_MAX_MSG) {
89 AUDIO_WARNING_LOG("OnRemoteRequest unsupported request code:%{public}d.", code);
90 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
91 }
92 switch (code) {
93 case ON_REGISTER_STREAM_LISTENER:
94 return HandleRegisterStreamListener(data, reply);
95 case ON_RESOLVE_BUFFER:
96 return HandleResolveBuffer(data, reply);
97 case ON_UPDATE_POSITION:
98 return HandleUpdatePosition(data, reply);
99 case ON_GET_AUDIO_SESSIONID:
100 return HandleGetAudioSessionID(data, reply);
101 case ON_START:
102 return HandleStart(data, reply);
103 case ON_PAUSE:
104 return HandlePause(data, reply);
105 case ON_STOP:
106 return HandleStop(data, reply);
107 case ON_RELEASE:
108 return HandleRelease(data, reply);
109 case ON_FLUSH:
110 return HandleFlush(data, reply);
111 case ON_DRAIN:
112 return HandleDrain(data, reply);
113 case ON_UPDATA_PLAYBACK_CAPTURER_CONFIG:
114 return HandleUpdatePlaybackCaptureConfig(data, reply);
115 case OH_GET_AUDIO_TIME:
116 return HandleGetAudioTime(data, reply);
117 case OH_GET_AUDIO_POSITION:
118 return HandleGetAudioPosition(data, reply);
119 case ON_GET_LATENCY:
120 return HandleGetLatency(data, reply);
121 case ON_SET_RATE:
122 return HandleSetRate(data, reply);
123 default:
124 return OnMiddleCodeRemoteRequest(code, data, reply, option);
125 }
126 }
127
HandleRegisterStreamListener(MessageParcel & data,MessageParcel & reply)128 int32_t IpcStreamStub::HandleRegisterStreamListener(MessageParcel &data, MessageParcel &reply)
129 {
130 sptr<IRemoteObject> object = data.ReadRemoteObject();
131 if (object == nullptr) {
132 AUDIO_ERR_LOG("IpcStreamStub: HandleRegisterProcessCb obj is null");
133 reply.WriteInt32(AUDIO_INVALID_PARAM);
134 return AUDIO_INVALID_PARAM;
135 }
136 reply.WriteInt32(RegisterStreamListener(object));
137 return AUDIO_OK;
138 }
139
HandleResolveBuffer(MessageParcel & data,MessageParcel & reply)140 int32_t IpcStreamStub::HandleResolveBuffer(MessageParcel &data, MessageParcel &reply)
141 {
142 (void)data;
143 std::shared_ptr<OHAudioBuffer> buffer;
144 int32_t ret = ResolveBuffer(buffer);
145 reply.WriteInt32(ret);
146 if (ret == AUDIO_OK && buffer != nullptr) {
147 OHAudioBuffer::WriteToParcel(buffer, reply);
148 } else {
149 AUDIO_ERR_LOG("error: ResolveBuffer failed.");
150 return AUDIO_INVALID_PARAM;
151 }
152
153 return AUDIO_OK;
154 }
155
HandleUpdatePosition(MessageParcel & data,MessageParcel & reply)156 int32_t IpcStreamStub::HandleUpdatePosition(MessageParcel &data, MessageParcel &reply)
157 {
158 (void)data;
159 reply.WriteInt32(UpdatePosition());
160 return AUDIO_OK;
161 }
162
HandleGetAudioSessionID(MessageParcel & data,MessageParcel & reply)163 int32_t IpcStreamStub::HandleGetAudioSessionID(MessageParcel &data, MessageParcel &reply)
164 {
165 (void)data;
166 uint32_t sessionId = 0;
167 reply.WriteInt32(GetAudioSessionID(sessionId));
168 reply.WriteUint32(sessionId);
169
170 return AUDIO_OK;
171 }
172
HandleStart(MessageParcel & data,MessageParcel & reply)173 int32_t IpcStreamStub::HandleStart(MessageParcel &data, MessageParcel &reply)
174 {
175 (void)data;
176 reply.WriteInt32(Start());
177 return AUDIO_OK;
178 }
179
HandlePause(MessageParcel & data,MessageParcel & reply)180 int32_t IpcStreamStub::HandlePause(MessageParcel &data, MessageParcel &reply)
181 {
182 (void)data;
183 reply.WriteInt32(Pause());
184 return AUDIO_OK;
185 }
186
HandleStop(MessageParcel & data,MessageParcel & reply)187 int32_t IpcStreamStub::HandleStop(MessageParcel &data, MessageParcel &reply)
188 {
189 (void)data;
190 reply.WriteInt32(Stop());
191 return AUDIO_OK;
192 }
HandleRelease(MessageParcel & data,MessageParcel & reply)193 int32_t IpcStreamStub::HandleRelease(MessageParcel &data, MessageParcel &reply)
194 {
195 (void)data;
196 reply.WriteInt32(Release());
197 return AUDIO_OK;
198 }
199
HandleFlush(MessageParcel & data,MessageParcel & reply)200 int32_t IpcStreamStub::HandleFlush(MessageParcel &data, MessageParcel &reply)
201 {
202 (void)data;
203 reply.WriteInt32(Flush());
204 return AUDIO_OK;
205 }
206
HandleDrain(MessageParcel & data,MessageParcel & reply)207 int32_t IpcStreamStub::HandleDrain(MessageParcel &data, MessageParcel &reply)
208 {
209 bool stopFlag = data.ReadBool();
210 AUDIO_INFO_LOG("stopFlag:%{public}d", stopFlag);
211 reply.WriteInt32(Drain(stopFlag));
212 return AUDIO_OK;
213 }
214
HandleUpdatePlaybackCaptureConfig(MessageParcel & data,MessageParcel & reply)215 int32_t IpcStreamStub::HandleUpdatePlaybackCaptureConfig(MessageParcel &data, MessageParcel &reply)
216 {
217 AudioPlaybackCaptureConfig config;
218 int32_t ret = ProcessConfig::ReadInnerCapConfigFromParcel(config, data);
219 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, AUDIO_ERR, "Read config failed");
220
221 reply.WriteInt32(UpdatePlaybackCaptureConfig(config));
222
223 return AUDIO_OK;
224 }
225
HandleGetAudioTime(MessageParcel & data,MessageParcel & reply)226 int32_t IpcStreamStub::HandleGetAudioTime(MessageParcel &data, MessageParcel &reply)
227 {
228 (void)data;
229 uint64_t framePos = 0;
230 uint64_t timestamp = 0;
231 reply.WriteInt32(GetAudioTime(framePos, timestamp));
232 reply.WriteUint64(framePos);
233 reply.WriteUint64(timestamp);
234 return AUDIO_OK;
235 }
236
HandleGetAudioPosition(MessageParcel & data,MessageParcel & reply)237 int32_t IpcStreamStub::HandleGetAudioPosition(MessageParcel &data, MessageParcel &reply)
238 {
239 (void)data;
240 uint64_t framePos = 0;
241 uint64_t timestamp = 0;
242 uint64_t latency = 0;
243 reply.WriteInt32(GetAudioPosition(framePos, timestamp, latency));
244 reply.WriteUint64(framePos);
245 reply.WriteUint64(timestamp);
246 reply.WriteUint64(latency);
247 return AUDIO_OK;
248 }
249
HandleGetLatency(MessageParcel & data,MessageParcel & reply)250 int32_t IpcStreamStub::HandleGetLatency(MessageParcel &data, MessageParcel &reply)
251 {
252 (void)data;
253 uint64_t latency = 0;
254 reply.WriteInt32(GetLatency(latency));
255 reply.WriteUint64(latency);
256
257 return AUDIO_OK;
258 }
259
HandleSetRate(MessageParcel & data,MessageParcel & reply)260 int32_t IpcStreamStub::HandleSetRate(MessageParcel &data, MessageParcel &reply)
261 {
262 int32_t rate = data.ReadInt32();
263 reply.WriteInt32(SetRate(rate));
264
265 return AUDIO_OK;
266 }
267
HandleGetRate(MessageParcel & data,MessageParcel & reply)268 int32_t IpcStreamStub::HandleGetRate(MessageParcel &data, MessageParcel &reply)
269 {
270 (void)data;
271 int32_t rate = -1;
272 reply.WriteInt32(GetRate(rate));
273 reply.WriteInt32(rate);
274
275 return AUDIO_OK;
276 }
277
HandleSetLowPowerVolume(MessageParcel & data,MessageParcel & reply)278 int32_t IpcStreamStub::HandleSetLowPowerVolume(MessageParcel &data, MessageParcel &reply)
279 {
280 float lowPowerVolume = data.ReadFloat();
281 reply.WriteInt32(SetLowPowerVolume(lowPowerVolume));
282
283 return AUDIO_OK;
284 }
285
HandleGetLowPowerVolume(MessageParcel & data,MessageParcel & reply)286 int32_t IpcStreamStub::HandleGetLowPowerVolume(MessageParcel &data, MessageParcel &reply)
287 {
288 (void)data;
289 float lowPowerVolume = 0.0;
290 reply.WriteInt32(GetLowPowerVolume(lowPowerVolume));
291 reply.WriteFloat(lowPowerVolume);
292
293 return AUDIO_OK;
294 }
295
HandleSetAudioEffectMode(MessageParcel & data,MessageParcel & reply)296 int32_t IpcStreamStub::HandleSetAudioEffectMode(MessageParcel &data, MessageParcel &reply)
297 {
298 int32_t effectMode = data.ReadInt32();
299 reply.WriteInt32(SetAudioEffectMode(effectMode));
300
301 return AUDIO_OK;
302 }
303
HandleGetAudioEffectMode(MessageParcel & data,MessageParcel & reply)304 int32_t IpcStreamStub::HandleGetAudioEffectMode(MessageParcel &data, MessageParcel &reply)
305 {
306 (void)data;
307 int32_t effectMode = -1;
308 reply.WriteInt32(GetAudioEffectMode(effectMode));
309 reply.WriteInt32(effectMode);
310
311 return AUDIO_OK;
312 }
313
HandleSetPrivacyType(MessageParcel & data,MessageParcel & reply)314 int32_t IpcStreamStub::HandleSetPrivacyType(MessageParcel &data, MessageParcel &reply)
315 {
316 int32_t privacyType = data.ReadInt32();
317 reply.WriteInt32(SetPrivacyType(privacyType));
318
319 return AUDIO_OK;
320 }
321
HandleGetPrivacyType(MessageParcel & data,MessageParcel & reply)322 int32_t IpcStreamStub::HandleGetPrivacyType(MessageParcel &data, MessageParcel &reply)
323 {
324 (void)data;
325 int32_t privacyType = -1;
326 reply.WriteInt32(GetPrivacyType(privacyType));
327 reply.WriteInt32(privacyType);
328
329 return AUDIO_OK;
330 }
331
HandleSetOffloadMode(MessageParcel & data,MessageParcel & reply)332 int32_t IpcStreamStub::HandleSetOffloadMode(MessageParcel &data, MessageParcel &reply)
333 {
334 int32_t state = data.ReadInt32();
335 bool isAppBack = data.ReadBool();
336 reply.WriteInt32(SetOffloadMode(state, isAppBack));
337
338 return AUDIO_OK;
339 }
340
HandleUnsetOffloadMode(MessageParcel & data,MessageParcel & reply)341 int32_t IpcStreamStub::HandleUnsetOffloadMode(MessageParcel &data, MessageParcel &reply)
342 {
343 reply.WriteInt32(UnsetOffloadMode());
344
345 return AUDIO_OK;
346 }
347
HandleGetOffloadApproximatelyCacheTime(MessageParcel & data,MessageParcel & reply)348 int32_t IpcStreamStub::HandleGetOffloadApproximatelyCacheTime(MessageParcel &data, MessageParcel &reply)
349 {
350 uint64_t timestamp = data.ReadUint64();
351 uint64_t paWriteIndex = data.ReadUint64();
352 uint64_t cacheTimeDsp = data.ReadUint64();
353 uint64_t cacheTimePa = data.ReadUint64();
354 reply.WriteInt32(GetOffloadApproximatelyCacheTime(timestamp, paWriteIndex, cacheTimeDsp, cacheTimePa));
355 reply.WriteUint64(timestamp);
356 reply.WriteUint64(paWriteIndex);
357 reply.WriteUint64(cacheTimeDsp);
358 reply.WriteUint64(cacheTimePa);
359
360 return AUDIO_OK;
361 }
362
HandleUpdateSpatializationState(MessageParcel & data,MessageParcel & reply)363 int32_t IpcStreamStub::HandleUpdateSpatializationState(MessageParcel &data, MessageParcel &reply)
364 {
365 bool spatializationEnabled = data.ReadBool();
366 bool headTrackingEnabled = data.ReadBool();
367 reply.WriteInt32(UpdateSpatializationState(spatializationEnabled, headTrackingEnabled));
368 return AUDIO_OK;
369 }
370
HandleGetStreamManagerType(MessageParcel & data,MessageParcel & reply)371 int32_t IpcStreamStub::HandleGetStreamManagerType(MessageParcel &data, MessageParcel &reply)
372 {
373 (void)data;
374 reply.WriteInt32(GetStreamManagerType());
375 return AUDIO_OK;
376 }
377
HandleSetSilentModeAndMixWithOthers(MessageParcel & data,MessageParcel & reply)378 int32_t IpcStreamStub::HandleSetSilentModeAndMixWithOthers(MessageParcel &data, MessageParcel &reply)
379 {
380 bool on = data.ReadBool();
381 reply.WriteInt32(SetSilentModeAndMixWithOthers(on));
382
383 return AUDIO_OK;
384 }
385
HandleSetClientVolume(MessageParcel & data,MessageParcel & reply)386 int32_t IpcStreamStub::HandleSetClientVolume(MessageParcel &data, MessageParcel &reply)
387 {
388 (void)data;
389 reply.WriteInt32(SetClientVolume());
390 return AUDIO_OK;
391 }
392
HandleSetMute(MessageParcel & data,MessageParcel & reply)393 int32_t IpcStreamStub::HandleSetMute(MessageParcel &data, MessageParcel &reply)
394 {
395 bool isMute = data.ReadBool();
396 reply.WriteInt32(SetMute(isMute));
397 return AUDIO_OK;
398 }
399
HandleSetDuckFactor(MessageParcel & data,MessageParcel & reply)400 int32_t IpcStreamStub::HandleSetDuckFactor(MessageParcel &data, MessageParcel &reply)
401 {
402 float duckFactor = data.ReadFloat();
403 reply.WriteInt32(SetDuckFactor(duckFactor));
404 return AUDIO_OK;
405 }
406
HandleRegisterThreadPriority(MessageParcel & data,MessageParcel & reply)407 int32_t IpcStreamStub::HandleRegisterThreadPriority(MessageParcel &data, MessageParcel &reply)
408 {
409 uint32_t tid = data.ReadUint32();
410 std::string bundleName = data.ReadString();
411 reply.WriteInt32(RegisterThreadPriority(tid, bundleName));
412 return AUDIO_OK;
413 }
414 } // namespace AudioStandard
415 } // namespace OHOS
416