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
16 #include "audio_errors.h"
17 #include "audio_log.h"
18 #include "audio_policy_manager_listener_stub.h"
19
20 namespace OHOS {
21 namespace AudioStandard {
AudioPolicyManagerListenerStub()22 AudioPolicyManagerListenerStub::AudioPolicyManagerListenerStub()
23 {
24 AUDIO_DEBUG_LOG("AudioPolicyManagerLiternerStub Instance create");
25 }
26
~AudioPolicyManagerListenerStub()27 AudioPolicyManagerListenerStub::~AudioPolicyManagerListenerStub()
28 {
29 AUDIO_DEBUG_LOG("AudioPolicyManagerListenerStub Instance complete");
30 }
31
ReadInterruptEventParams(MessageParcel & data,InterruptEventInternal & interruptEvent)32 void AudioPolicyManagerListenerStub::ReadInterruptEventParams(MessageParcel &data,
33 InterruptEventInternal &interruptEvent)
34 {
35 interruptEvent.eventType = static_cast<InterruptType>(data.ReadInt32());
36 interruptEvent.forceType = static_cast<InterruptForceType>(data.ReadInt32());
37 interruptEvent.hintType = static_cast<InterruptHint>(data.ReadInt32());
38 interruptEvent.duckVolume = data.ReadFloat();
39 }
40
ReadAudioDeviceChangeData(MessageParcel & data,DeviceChangeAction & devChange)41 void AudioPolicyManagerListenerStub::ReadAudioDeviceChangeData(MessageParcel &data, DeviceChangeAction &devChange)
42 {
43 std::vector<sptr<AudioDeviceDescriptor>> deviceChangeDesc = {};
44
45 int32_t type = data.ReadInt32();
46 int32_t flag = data.ReadInt32();
47 int32_t size = data.ReadInt32();
48
49 for (int32_t i = 0; i < size; i++) {
50 deviceChangeDesc.push_back(AudioDeviceDescriptor::Unmarshalling(data));
51 }
52
53 devChange.type = static_cast<DeviceChangeType>(type);
54 devChange.flag = static_cast<DeviceFlag>(flag);
55 devChange.deviceDescriptors = deviceChangeDesc;
56 }
57
ReadAudioFocusInfoChangeData(MessageParcel & data,std::list<std::pair<AudioInterrupt,AudioFocuState>> & focusInfoList)58 void AudioPolicyManagerListenerStub::ReadAudioFocusInfoChangeData(MessageParcel &data,
59 std::list<std::pair<AudioInterrupt, AudioFocuState>> &focusInfoList)
60 {
61 std::pair<AudioInterrupt, AudioFocuState> focusInfo = {};
62 int32_t size = data.ReadInt32();
63
64 for (int32_t i = 0; i < size; i++) {
65 focusInfo.first.streamUsage = static_cast<StreamUsage>(data.ReadInt32());
66 focusInfo.first.contentType = static_cast<ContentType>(data.ReadInt32());
67 focusInfo.first.audioFocusType.streamType = static_cast<AudioStreamType>(data.ReadInt32());
68 focusInfo.first.audioFocusType.sourceType = static_cast<SourceType>(data.ReadInt32());
69 focusInfo.first.audioFocusType.isPlay = data.ReadBool();
70 focusInfo.first.sessionID = data.ReadInt32();
71 focusInfo.first.pauseWhenDucked = data.ReadBool();
72 focusInfo.first.mode = static_cast<InterruptMode>(data.ReadInt32());
73 focusInfo.second = static_cast<AudioFocuState>(data.ReadInt32());
74
75 focusInfoList.push_back(focusInfo);
76 }
77 }
78
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)79 int AudioPolicyManagerListenerStub::OnRemoteRequest(
80 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
81 {
82 if (data.ReadInterfaceToken() != GetDescriptor()) {
83 AUDIO_ERR_LOG("AudioPolicyManagerListenerStub: ReadInterfaceToken failed");
84 return -1;
85 }
86 switch (code) {
87 case ON_INTERRUPT: {
88 InterruptEventInternal interruptEvent = {};
89 ReadInterruptEventParams(data, interruptEvent);
90 // To be modified by enqueuing the interrupt action scheduler
91 OnInterrupt(interruptEvent);
92 return AUDIO_OK;
93 }
94 case ON_DEVICE_CHANGED: {
95 DeviceChangeAction deviceChangeAction = {};
96 ReadAudioDeviceChangeData(data, deviceChangeAction);
97 OnDeviceChange(deviceChangeAction);
98 return AUDIO_OK;
99 }
100 case ON_FOCUS_INFO_CHANGED: {
101 std::list<std::pair<AudioInterrupt, AudioFocuState>> focusInfoList = {};
102 ReadAudioFocusInfoChangeData(data, focusInfoList);
103 OnAudioFocusInfoChange(focusInfoList);
104 return AUDIO_OK;
105 }
106 default: {
107 AUDIO_ERR_LOG("default case, need check AudioListenerStub");
108 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
109 }
110 }
111 }
112
OnInterrupt(const InterruptEventInternal & interruptEvent)113 void AudioPolicyManagerListenerStub::OnInterrupt(const InterruptEventInternal &interruptEvent)
114 {
115 AUDIO_DEBUG_LOG("AudioPolicyManagerLiternerStub OnInterrupt start");
116 std::shared_ptr<AudioInterruptCallback> cb = callback_.lock();
117 if (cb != nullptr) {
118 cb->OnInterrupt(interruptEvent);
119 } else {
120 AUDIO_ERR_LOG("AudioPolicyManagerListenerStub: callback_ is nullptr");
121 }
122 }
123
OnDeviceChange(const DeviceChangeAction & deviceChangeAction)124 void AudioPolicyManagerListenerStub::OnDeviceChange(const DeviceChangeAction &deviceChangeAction)
125 {
126 AUDIO_DEBUG_LOG("AudioPolicyManagerLiternerStub OnDeviceChange start");
127 std::shared_ptr<AudioManagerDeviceChangeCallback> deviceChangedCallback = deviceChangeCallback_.lock();
128
129 if (deviceChangedCallback == nullptr) {
130 AUDIO_ERR_LOG("OnDeviceChange: deviceChangeCallback_ or deviceChangeAction is nullptr");
131 return;
132 }
133
134 deviceChangedCallback->OnDeviceChange(deviceChangeAction);
135 }
136
OnAudioFocusInfoChange(const std::list<std::pair<AudioInterrupt,AudioFocuState>> & focusInfoList)137 void AudioPolicyManagerListenerStub::OnAudioFocusInfoChange(
138 const std::list<std::pair<AudioInterrupt, AudioFocuState>> &focusInfoList)
139 {
140 AUDIO_DEBUG_LOG("AudioPolicyManagerLiternerStub OnFocusInfoChange start");
141 std::shared_ptr<AudioFocusInfoChangeCallback> focunIfoChangedCallback = focusInfoChangeCallback_.lock();
142 if (focunIfoChangedCallback == nullptr) {
143 AUDIO_ERR_LOG("OnFocusInfoChange: deviceChangeCallback_ or focunIfoChangedCallback is nullptr");
144 return;
145 }
146
147 focunIfoChangedCallback->OnAudioFocusInfoChange(focusInfoList);
148 }
149
SetInterruptCallback(const std::weak_ptr<AudioInterruptCallback> & callback)150 void AudioPolicyManagerListenerStub::SetInterruptCallback(const std::weak_ptr<AudioInterruptCallback> &callback)
151 {
152 callback_ = callback;
153 }
154
SetDeviceChangeCallback(const std::weak_ptr<AudioManagerDeviceChangeCallback> & cb)155 void AudioPolicyManagerListenerStub::SetDeviceChangeCallback(const std::weak_ptr<AudioManagerDeviceChangeCallback> &cb)
156 {
157 deviceChangeCallback_ = cb;
158 }
159
SetFocusInfoChangeCallback(const std::weak_ptr<AudioFocusInfoChangeCallback> & cb)160 void AudioPolicyManagerListenerStub::SetFocusInfoChangeCallback(const std::weak_ptr<AudioFocusInfoChangeCallback> &cb)
161 {
162 focusInfoChangeCallback_ = cb;
163 }
164 } // namespace AudioStandard
165 } // namespace OHOS
166