1 /*
2 * Copyright (c) 2021-2022 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 "AudioPolicyManagerListenerStub"
17 #endif
18
19
20 #include "audio_policy_log.h"
21 #include "audio_policy_manager_listener_stub.h"
22 #include "audio_utils.h"
23
24 namespace OHOS {
25 namespace AudioStandard {
26
27 static const int32_t DEVICE_CHANGE_VALID_SIZE = 128;
28
AudioPolicyManagerListenerStub()29 AudioPolicyManagerListenerStub::AudioPolicyManagerListenerStub()
30 {
31 }
32
~AudioPolicyManagerListenerStub()33 AudioPolicyManagerListenerStub::~AudioPolicyManagerListenerStub()
34 {
35 }
36
ReadInterruptEventParams(MessageParcel & data,InterruptEventInternal & interruptEvent)37 void AudioPolicyManagerListenerStub::ReadInterruptEventParams(MessageParcel &data,
38 InterruptEventInternal &interruptEvent)
39 {
40 interruptEvent.eventType = static_cast<InterruptType>(data.ReadInt32());
41 interruptEvent.forceType = static_cast<InterruptForceType>(data.ReadInt32());
42 interruptEvent.hintType = static_cast<InterruptHint>(data.ReadInt32());
43 interruptEvent.duckVolume = data.ReadFloat();
44 interruptEvent.callbackToApp = data.ReadBool();
45 }
46
ReadAudioDeviceChangeData(MessageParcel & data,DeviceChangeAction & devChange)47 void AudioPolicyManagerListenerStub::ReadAudioDeviceChangeData(MessageParcel &data, DeviceChangeAction &devChange)
48 {
49 std::vector<sptr<AudioDeviceDescriptor>> deviceChangeDesc = {};
50
51 int32_t type = data.ReadInt32();
52 int32_t flag = data.ReadInt32();
53 int32_t size = data.ReadInt32();
54 CHECK_AND_RETURN_LOG(size < DEVICE_CHANGE_VALID_SIZE, "get invalid size : %{public}d", size);
55
56 for (int32_t i = 0; i < size; i++) {
57 deviceChangeDesc.push_back(AudioDeviceDescriptor::Unmarshalling(data));
58 }
59
60 devChange.type = static_cast<DeviceChangeType>(type);
61 devChange.flag = static_cast<DeviceFlag>(flag);
62 devChange.deviceDescriptors = deviceChangeDesc;
63 }
64
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)65 int AudioPolicyManagerListenerStub::OnRemoteRequest(
66 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
67 {
68 CHECK_AND_RETURN_RET_LOG(data.ReadInterfaceToken() == GetDescriptor(), AUDIO_INVALID_PARAM,
69 "ReadInterfaceToken failed");
70 Trace trace("AudioPolicyManagerListenerStub::OnRemoteRequest:" + std::to_string(code));
71 switch (code) {
72 case ON_INTERRUPT: {
73 InterruptEventInternal interruptEvent = {};
74 ReadInterruptEventParams(data, interruptEvent);
75 // To be modified by enqueuing the interrupt action scheduler
76 OnInterrupt(interruptEvent);
77 return AUDIO_OK;
78 }
79 case ON_AVAILABLE_DEVICE_CAHNGE: {
80 AudioDeviceUsage usage = static_cast<AudioDeviceUsage>(data.ReadInt32());
81 DeviceChangeAction deviceChangeAction = {};
82 ReadAudioDeviceChangeData(data, deviceChangeAction);
83 OnAvailableDeviceChange(usage, deviceChangeAction);
84 return AUDIO_OK;
85 }
86 case ON_QUERY_CLIENT_TYPE: {
87 std::string bundleName = data.ReadString();
88 uint32_t uid = data.ReadUint32();
89 OnQueryClientType(bundleName, uid);
90 return AUDIO_OK;
91 }
92 default: {
93 AUDIO_ERR_LOG("default case, need check AudioListenerStub");
94 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
95 }
96 }
97 }
98
OnInterrupt(const InterruptEventInternal & interruptEvent)99 void AudioPolicyManagerListenerStub::OnInterrupt(const InterruptEventInternal &interruptEvent)
100 {
101 std::shared_ptr<AudioInterruptCallback> cb = callback_.lock();
102 if (cb != nullptr) {
103 cb->OnInterrupt(interruptEvent);
104 } else {
105 AUDIO_WARNING_LOG("AudioPolicyManagerListenerStub: callback_ is nullptr");
106 }
107 }
108
OnAvailableDeviceChange(const AudioDeviceUsage usage,const DeviceChangeAction & deviceChangeAction)109 void AudioPolicyManagerListenerStub::OnAvailableDeviceChange(const AudioDeviceUsage usage,
110 const DeviceChangeAction &deviceChangeAction)
111 {
112 std::shared_ptr<AudioManagerAvailableDeviceChangeCallback> availabledeviceChangedCallback =
113 audioAvailableDeviceChangeCallback_.lock();
114
115 CHECK_AND_RETURN_LOG(availabledeviceChangedCallback != nullptr,
116 "OnAvailableDeviceChange: deviceChangeCallback_ or deviceChangeAction is nullptr");
117
118 availabledeviceChangedCallback->OnAvailableDeviceChange(usage, deviceChangeAction);
119 }
120
OnQueryClientType(const std::string & bundleName,uint32_t uid)121 bool AudioPolicyManagerListenerStub::OnQueryClientType(const std::string &bundleName, uint32_t uid)
122 {
123 std::shared_ptr<AudioQueryClientTypeCallback> audioQueryClientTypeCallback =
124 audioQueryClientTypeCallback_.lock();
125
126 CHECK_AND_RETURN_RET_LOG(audioQueryClientTypeCallback != nullptr, false, "queryClientTypeCallback is nullptr");
127 return audioQueryClientTypeCallback->OnQueryClientType(bundleName, uid);
128 }
129
SetInterruptCallback(const std::weak_ptr<AudioInterruptCallback> & callback)130 void AudioPolicyManagerListenerStub::SetInterruptCallback(const std::weak_ptr<AudioInterruptCallback> &callback)
131 {
132 callback_ = callback;
133 }
134
SetAvailableDeviceChangeCallback(const std::weak_ptr<AudioManagerAvailableDeviceChangeCallback> & cb)135 void AudioPolicyManagerListenerStub::SetAvailableDeviceChangeCallback(
136 const std::weak_ptr<AudioManagerAvailableDeviceChangeCallback> &cb)
137 {
138 audioAvailableDeviceChangeCallback_ = cb;
139 }
140
SetQueryClientTypeCallback(const std::weak_ptr<AudioQueryClientTypeCallback> & cb)141 void AudioPolicyManagerListenerStub::SetQueryClientTypeCallback(const std::weak_ptr<AudioQueryClientTypeCallback> &cb)
142 {
143 audioQueryClientTypeCallback_ = cb;
144 }
145 } // namespace AudioStandard
146 } // namespace OHOS
147