• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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         default:
75             return OnMiddleCodeRemoteRequestExt(code, data, reply, option);
76     }
77 }
78 
OnMiddleCodeRemoteRequestExt(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)79 int IpcStreamStub::OnMiddleCodeRemoteRequestExt(uint32_t code, MessageParcel &data, MessageParcel &reply,
80     MessageOption &option)
81 {
82     switch (code) {
83         case ON_REGISTER_THREAD_PRIORITY:
84             return HandleRegisterThreadPriority(data, reply);
85         case ON_SET_DEFAULT_OUTPUT_DEVICE:
86             return HandleSetDefaultOutputDevice(data, reply);
87         case ON_SET_SOURCE_DURATION:
88             return HandleSetSourceDuration(data, reply);
89         default:
90             AUDIO_WARNING_LOG("OnRemoteRequest unsupported request code:%{public}d.", code);
91             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
92     }
93 }
94 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)95 int IpcStreamStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
96 {
97     if (!CheckInterfaceToken(data)) {
98         return AUDIO_ERR;
99     }
100     Trace trace("IpcStream::Handle::" + std::to_string(code));
101     if (code >= IpcStreamMsg::IPC_STREAM_MAX_MSG) {
102         AUDIO_WARNING_LOG("OnRemoteRequest unsupported request code:%{public}d.", code);
103         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
104     }
105     switch (code) {
106         case ON_REGISTER_STREAM_LISTENER:
107             return HandleRegisterStreamListener(data, reply);
108         case ON_RESOLVE_BUFFER:
109             return HandleResolveBuffer(data, reply);
110         case ON_UPDATE_POSITION:
111             return HandleUpdatePosition(data, reply);
112         case ON_GET_AUDIO_SESSIONID:
113             return HandleGetAudioSessionID(data, reply);
114         case ON_START:
115             return HandleStart(data, reply);
116         case ON_PAUSE:
117             return HandlePause(data, reply);
118         case ON_STOP:
119             return HandleStop(data, reply);
120         case ON_RELEASE:
121             return HandleRelease(data, reply);
122         case ON_FLUSH:
123             return HandleFlush(data, reply);
124         case ON_DRAIN:
125             return HandleDrain(data, reply);
126         case ON_UPDATA_PLAYBACK_CAPTURER_CONFIG:
127             return HandleUpdatePlaybackCaptureConfig(data, reply);
128         case OH_GET_AUDIO_TIME:
129             return HandleGetAudioTime(data, reply);
130         case OH_GET_AUDIO_POSITION:
131             return HandleGetAudioPosition(data, reply);
132         case ON_GET_LATENCY:
133             return HandleGetLatency(data, reply);
134         case ON_SET_RATE:
135             return HandleSetRate(data, reply);
136         default:
137             return OnMiddleCodeRemoteRequest(code, data, reply, option);
138     }
139 }
140 
HandleRegisterStreamListener(MessageParcel & data,MessageParcel & reply)141 int32_t IpcStreamStub::HandleRegisterStreamListener(MessageParcel &data, MessageParcel &reply)
142 {
143     sptr<IRemoteObject> object = data.ReadRemoteObject();
144     if (object == nullptr) {
145         AUDIO_ERR_LOG("IpcStreamStub: HandleRegisterProcessCb obj is null");
146         reply.WriteInt32(AUDIO_INVALID_PARAM);
147         return AUDIO_INVALID_PARAM;
148     }
149     reply.WriteInt32(RegisterStreamListener(object));
150     return AUDIO_OK;
151 }
152 
HandleResolveBuffer(MessageParcel & data,MessageParcel & reply)153 int32_t IpcStreamStub::HandleResolveBuffer(MessageParcel &data, MessageParcel &reply)
154 {
155     (void)data;
156     std::shared_ptr<OHAudioBuffer> buffer;
157     int32_t ret = ResolveBuffer(buffer);
158     reply.WriteInt32(ret);
159     if (ret == AUDIO_OK && buffer != nullptr) {
160         OHAudioBuffer::WriteToParcel(buffer, reply);
161     } else {
162         AUDIO_ERR_LOG("error: ResolveBuffer failed.");
163         return AUDIO_INVALID_PARAM;
164     }
165 
166     return AUDIO_OK;
167 }
168 
HandleUpdatePosition(MessageParcel & data,MessageParcel & reply)169 int32_t IpcStreamStub::HandleUpdatePosition(MessageParcel &data, MessageParcel &reply)
170 {
171     (void)data;
172     reply.WriteInt32(UpdatePosition());
173     return AUDIO_OK;
174 }
175 
HandleGetAudioSessionID(MessageParcel & data,MessageParcel & reply)176 int32_t IpcStreamStub::HandleGetAudioSessionID(MessageParcel &data, MessageParcel &reply)
177 {
178     (void)data;
179     uint32_t sessionId = 0;
180     reply.WriteInt32(GetAudioSessionID(sessionId));
181     reply.WriteUint32(sessionId);
182 
183     return AUDIO_OK;
184 }
185 
HandleStart(MessageParcel & data,MessageParcel & reply)186 int32_t IpcStreamStub::HandleStart(MessageParcel &data, MessageParcel &reply)
187 {
188     (void)data;
189     reply.WriteInt32(Start());
190     return AUDIO_OK;
191 }
192 
HandlePause(MessageParcel & data,MessageParcel & reply)193 int32_t IpcStreamStub::HandlePause(MessageParcel &data, MessageParcel &reply)
194 {
195     (void)data;
196     reply.WriteInt32(Pause());
197     return AUDIO_OK;
198 }
199 
HandleStop(MessageParcel & data,MessageParcel & reply)200 int32_t IpcStreamStub::HandleStop(MessageParcel &data, MessageParcel &reply)
201 {
202     (void)data;
203     reply.WriteInt32(Stop());
204     return AUDIO_OK;
205 }
HandleRelease(MessageParcel & data,MessageParcel & reply)206 int32_t IpcStreamStub::HandleRelease(MessageParcel &data, MessageParcel &reply)
207 {
208     (void)data;
209     reply.WriteInt32(Release());
210     return AUDIO_OK;
211 }
212 
HandleFlush(MessageParcel & data,MessageParcel & reply)213 int32_t IpcStreamStub::HandleFlush(MessageParcel &data, MessageParcel &reply)
214 {
215     (void)data;
216     reply.WriteInt32(Flush());
217     return AUDIO_OK;
218 }
219 
HandleDrain(MessageParcel & data,MessageParcel & reply)220 int32_t IpcStreamStub::HandleDrain(MessageParcel &data, MessageParcel &reply)
221 {
222     bool stopFlag = data.ReadBool();
223     AUDIO_INFO_LOG("stopFlag:%{public}d", stopFlag);
224     reply.WriteInt32(Drain(stopFlag));
225     return AUDIO_OK;
226 }
227 
HandleUpdatePlaybackCaptureConfig(MessageParcel & data,MessageParcel & reply)228 int32_t IpcStreamStub::HandleUpdatePlaybackCaptureConfig(MessageParcel &data, MessageParcel &reply)
229 {
230 #ifdef HAS_FEATURE_INNERCAPTURER
231     AudioPlaybackCaptureConfig config;
232     int32_t ret = ProcessConfig::ReadInnerCapConfigFromParcel(config, data);
233     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, AUDIO_ERR, "Read config failed");
234 
235     reply.WriteInt32(UpdatePlaybackCaptureConfig(config));
236 
237     return AUDIO_OK;
238 #else
239     return ERROR;
240 #endif
241 }
242 
HandleGetAudioTime(MessageParcel & data,MessageParcel & reply)243 int32_t IpcStreamStub::HandleGetAudioTime(MessageParcel &data, MessageParcel &reply)
244 {
245     (void)data;
246     uint64_t framePos = 0;
247     uint64_t timestamp = 0;
248     reply.WriteInt32(GetAudioTime(framePos, timestamp));
249     reply.WriteUint64(framePos);
250     reply.WriteUint64(timestamp);
251     return AUDIO_OK;
252 }
253 
HandleGetAudioPosition(MessageParcel & data,MessageParcel & reply)254 int32_t IpcStreamStub::HandleGetAudioPosition(MessageParcel &data, MessageParcel &reply)
255 {
256     (void)data;
257     uint64_t framePos = 0;
258     uint64_t timestamp = 0;
259     uint64_t latency = 0;
260     reply.WriteInt32(GetAudioPosition(framePos, timestamp, latency));
261     reply.WriteUint64(framePos);
262     reply.WriteUint64(timestamp);
263     reply.WriteUint64(latency);
264     return AUDIO_OK;
265 }
266 
HandleGetLatency(MessageParcel & data,MessageParcel & reply)267 int32_t IpcStreamStub::HandleGetLatency(MessageParcel &data, MessageParcel &reply)
268 {
269     (void)data;
270     uint64_t latency = 0;
271     reply.WriteInt32(GetLatency(latency));
272     reply.WriteUint64(latency);
273 
274     return AUDIO_OK;
275 }
276 
HandleSetRate(MessageParcel & data,MessageParcel & reply)277 int32_t IpcStreamStub::HandleSetRate(MessageParcel &data, MessageParcel &reply)
278 {
279     int32_t rate = data.ReadInt32();
280     reply.WriteInt32(SetRate(rate));
281 
282     return AUDIO_OK;
283 }
284 
HandleGetRate(MessageParcel & data,MessageParcel & reply)285 int32_t IpcStreamStub::HandleGetRate(MessageParcel &data, MessageParcel &reply)
286 {
287     (void)data;
288     int32_t rate = -1;
289     reply.WriteInt32(GetRate(rate));
290     reply.WriteInt32(rate);
291 
292     return AUDIO_OK;
293 }
294 
HandleSetLowPowerVolume(MessageParcel & data,MessageParcel & reply)295 int32_t IpcStreamStub::HandleSetLowPowerVolume(MessageParcel &data, MessageParcel &reply)
296 {
297     float lowPowerVolume = data.ReadFloat();
298     reply.WriteInt32(SetLowPowerVolume(lowPowerVolume));
299 
300     return AUDIO_OK;
301 }
302 
HandleGetLowPowerVolume(MessageParcel & data,MessageParcel & reply)303 int32_t IpcStreamStub::HandleGetLowPowerVolume(MessageParcel &data, MessageParcel &reply)
304 {
305     (void)data;
306     float lowPowerVolume = 0.0;
307     reply.WriteInt32(GetLowPowerVolume(lowPowerVolume));
308     reply.WriteFloat(lowPowerVolume);
309 
310     return AUDIO_OK;
311 }
312 
HandleSetAudioEffectMode(MessageParcel & data,MessageParcel & reply)313 int32_t IpcStreamStub::HandleSetAudioEffectMode(MessageParcel &data, MessageParcel &reply)
314 {
315     int32_t effectMode = data.ReadInt32();
316     reply.WriteInt32(SetAudioEffectMode(effectMode));
317 
318     return AUDIO_OK;
319 }
320 
HandleGetAudioEffectMode(MessageParcel & data,MessageParcel & reply)321 int32_t IpcStreamStub::HandleGetAudioEffectMode(MessageParcel &data, MessageParcel &reply)
322 {
323     (void)data;
324     int32_t effectMode = -1;
325     reply.WriteInt32(GetAudioEffectMode(effectMode));
326     reply.WriteInt32(effectMode);
327 
328     return AUDIO_OK;
329 }
330 
HandleSetPrivacyType(MessageParcel & data,MessageParcel & reply)331 int32_t IpcStreamStub::HandleSetPrivacyType(MessageParcel &data, MessageParcel &reply)
332 {
333     int32_t privacyType = data.ReadInt32();
334     reply.WriteInt32(SetPrivacyType(privacyType));
335 
336     return AUDIO_OK;
337 }
338 
HandleGetPrivacyType(MessageParcel & data,MessageParcel & reply)339 int32_t IpcStreamStub::HandleGetPrivacyType(MessageParcel &data, MessageParcel &reply)
340 {
341     (void)data;
342     int32_t privacyType = -1;
343     reply.WriteInt32(GetPrivacyType(privacyType));
344     reply.WriteInt32(privacyType);
345 
346     return AUDIO_OK;
347 }
348 
HandleSetOffloadMode(MessageParcel & data,MessageParcel & reply)349 int32_t IpcStreamStub::HandleSetOffloadMode(MessageParcel &data, MessageParcel &reply)
350 {
351     int32_t state = data.ReadInt32();
352     bool isAppBack = data.ReadBool();
353     reply.WriteInt32(SetOffloadMode(state, isAppBack));
354 
355     return AUDIO_OK;
356 }
357 
HandleUnsetOffloadMode(MessageParcel & data,MessageParcel & reply)358 int32_t IpcStreamStub::HandleUnsetOffloadMode(MessageParcel &data, MessageParcel &reply)
359 {
360     reply.WriteInt32(UnsetOffloadMode());
361 
362     return AUDIO_OK;
363 }
364 
HandleGetOffloadApproximatelyCacheTime(MessageParcel & data,MessageParcel & reply)365 int32_t IpcStreamStub::HandleGetOffloadApproximatelyCacheTime(MessageParcel &data, MessageParcel &reply)
366 {
367     uint64_t timestamp = data.ReadUint64();
368     uint64_t paWriteIndex = data.ReadUint64();
369     uint64_t cacheTimeDsp = data.ReadUint64();
370     uint64_t cacheTimePa = data.ReadUint64();
371     reply.WriteInt32(GetOffloadApproximatelyCacheTime(timestamp, paWriteIndex, cacheTimeDsp, cacheTimePa));
372     reply.WriteUint64(timestamp);
373     reply.WriteUint64(paWriteIndex);
374     reply.WriteUint64(cacheTimeDsp);
375     reply.WriteUint64(cacheTimePa);
376 
377     return AUDIO_OK;
378 }
379 
HandleUpdateSpatializationState(MessageParcel & data,MessageParcel & reply)380 int32_t IpcStreamStub::HandleUpdateSpatializationState(MessageParcel &data, MessageParcel &reply)
381 {
382     bool spatializationEnabled = data.ReadBool();
383     bool headTrackingEnabled = data.ReadBool();
384     reply.WriteInt32(UpdateSpatializationState(spatializationEnabled, headTrackingEnabled));
385     return AUDIO_OK;
386 }
387 
HandleGetStreamManagerType(MessageParcel & data,MessageParcel & reply)388 int32_t IpcStreamStub::HandleGetStreamManagerType(MessageParcel &data, MessageParcel &reply)
389 {
390     (void)data;
391     reply.WriteInt32(GetStreamManagerType());
392     return AUDIO_OK;
393 }
394 
HandleSetSilentModeAndMixWithOthers(MessageParcel & data,MessageParcel & reply)395 int32_t IpcStreamStub::HandleSetSilentModeAndMixWithOthers(MessageParcel &data, MessageParcel &reply)
396 {
397     bool on = data.ReadBool();
398     reply.WriteInt32(SetSilentModeAndMixWithOthers(on));
399 
400     return AUDIO_OK;
401 }
402 
HandleSetClientVolume(MessageParcel & data,MessageParcel & reply)403 int32_t IpcStreamStub::HandleSetClientVolume(MessageParcel &data, MessageParcel &reply)
404 {
405     reply.WriteInt32(SetClientVolume());
406     return AUDIO_OK;
407 }
408 
HandleSetMute(MessageParcel & data,MessageParcel & reply)409 int32_t IpcStreamStub::HandleSetMute(MessageParcel &data, MessageParcel &reply)
410 {
411     bool isMute = data.ReadBool();
412     reply.WriteInt32(SetMute(isMute));
413     return AUDIO_OK;
414 }
415 
HandleSetDuckFactor(MessageParcel & data,MessageParcel & reply)416 int32_t IpcStreamStub::HandleSetDuckFactor(MessageParcel &data, MessageParcel &reply)
417 {
418     float duckFactor = data.ReadFloat();
419     reply.WriteInt32(SetDuckFactor(duckFactor));
420     return AUDIO_OK;
421 }
422 
HandleRegisterThreadPriority(MessageParcel & data,MessageParcel & reply)423 int32_t IpcStreamStub::HandleRegisterThreadPriority(MessageParcel &data, MessageParcel &reply)
424 {
425     uint32_t tid = data.ReadUint32();
426     std::string bundleName = data.ReadString();
427     reply.WriteInt32(RegisterThreadPriority(tid, bundleName));
428     return AUDIO_OK;
429 }
430 
HandleSetDefaultOutputDevice(MessageParcel & data,MessageParcel & reply)431 int32_t IpcStreamStub::HandleSetDefaultOutputDevice(MessageParcel &data, MessageParcel &reply)
432 {
433     int32_t deviceType = data.ReadInt32();
434     reply.WriteInt32(SetDefaultOutputDevice(static_cast<OHOS::AudioStandard::DeviceType>(deviceType)));
435     return AUDIO_OK;
436 }
437 
HandleSetSourceDuration(MessageParcel & data,MessageParcel & reply)438 int32_t IpcStreamStub::HandleSetSourceDuration(MessageParcel &data, MessageParcel &reply)
439 {
440     int64_t sourceDuration = data.ReadInt64();
441     reply.WriteInt32(SetSourceDuration(sourceDuration));
442     return AUDIO_OK;
443 }
444 } // namespace AudioStandard
445 } // namespace OHOS
446