1 /*
2 * Copyright (c) 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 "AudioRoutingManagerListenerStub"
17 #endif
18
19 #include "audio_routing_manager_listener_stub.h"
20
21 #include "audio_errors.h"
22 #include "audio_policy_log.h"
23
24 using namespace std;
25
26 namespace OHOS {
27 namespace AudioStandard {
28
29 static const int32_t PREFERRED_DEVICE_VALID_SIZE = 128;
30
AudioRoutingManagerListenerStub()31 AudioRoutingManagerListenerStub::AudioRoutingManagerListenerStub()
32 {
33 }
34
~AudioRoutingManagerListenerStub()35 AudioRoutingManagerListenerStub::~AudioRoutingManagerListenerStub()
36 {
37 }
38
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)39 int AudioRoutingManagerListenerStub::OnRemoteRequest(
40 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
41 {
42 CHECK_AND_RETURN_RET_LOG(data.ReadInterfaceToken() == GetDescriptor(),
43 -1, "AudioRingerModeUpdateListenerStub: ReadInterfaceToken failed");
44 switch (code) {
45 case ON_DISTRIBUTED_ROUTING_ROLE_CHANGE: {
46 sptr<AudioDeviceDescriptor> descriptor = AudioDeviceDescriptor::UnmarshallingPtr(data);
47 CastType type = static_cast<CastType>(data.ReadInt32());
48 OnDistributedRoutingRoleChange(descriptor, type);
49 return AUDIO_OK;
50 }
51 case ON_AUDIO_OUTPUT_DEVICE_REFINERD: {
52 OnAudioOutputDeviceRefinedInternal(data, reply);
53 return AUDIO_OK;
54 }
55 case ON_AUDIO_INPUT_DEVICE_REFINERD: {
56 OnAudioInputDeviceRefinedInternal(data, reply);
57 return AUDIO_OK;
58 }
59 default: {
60 AUDIO_ERR_LOG("default case, need check AudioListenerStub");
61 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
62 }
63 }
64 }
65
OnDistributedRoutingRoleChange(const sptr<AudioDeviceDescriptor> descriptor,const CastType type)66 void AudioRoutingManagerListenerStub::OnDistributedRoutingRoleChange(const sptr<AudioDeviceDescriptor> descriptor,
67 const CastType type)
68 {
69 std::shared_ptr<AudioDistributedRoutingRoleCallback> audioDistributedRoutingRoleCallback =
70 audioDistributedRoutingRoleCallback_.lock();
71
72 CHECK_AND_RETURN_LOG(audioDistributedRoutingRoleCallback != nullptr,
73 "OnDistributedRoutingRoleChange: audioDistributedRoutingRoleCallback_ is nullptr");
74
75 audioDistributedRoutingRoleCallback->OnDistributedRoutingRoleChange(descriptor, type);
76 }
77
SetDistributedRoutingRoleCallback(const std::weak_ptr<AudioDistributedRoutingRoleCallback> & callback)78 void AudioRoutingManagerListenerStub::SetDistributedRoutingRoleCallback(
79 const std::weak_ptr<AudioDistributedRoutingRoleCallback> &callback)
80 {
81 audioDistributedRoutingRoleCallback_ = callback;
82 }
83
SetAudioDeviceRefinerCallback(const std::weak_ptr<AudioDeviceRefiner> & callback)84 void AudioRoutingManagerListenerStub::SetAudioDeviceRefinerCallback(const std::weak_ptr<AudioDeviceRefiner> &callback)
85 {
86 std::lock_guard<std::mutex> lock(deviceRefinerCallbackMutex_);
87 audioDeviceRefinerCallback_ = callback;
88 std::shared_ptr<AudioDeviceRefiner> audioDeviceRefinerCallback = audioDeviceRefinerCallback_.lock();
89 CHECK_AND_RETURN_LOG(audioDeviceRefinerCallback != nullptr, "audioDeviceRefinerCallback_ is nullptr");
90 }
91
OnAudioOutputDeviceRefinedInternal(MessageParcel & data,MessageParcel & reply)92 void AudioRoutingManagerListenerStub::OnAudioOutputDeviceRefinedInternal(MessageParcel &data, MessageParcel &reply)
93 {
94 std::vector<std::unique_ptr<AudioDeviceDescriptor>> descs;
95 int32_t size = data.ReadInt32();
96 CHECK_AND_RETURN_LOG(size < PREFERRED_DEVICE_VALID_SIZE, "get invalid size : %{public}d", size);
97 for (int32_t i = 0; i < size; i++) {
98 descs.push_back(make_unique<AudioDeviceDescriptor>(AudioDeviceDescriptor::UnmarshallingPtr(data)));
99 }
100 RouterType routerType = static_cast<RouterType>(data.ReadInt32());
101 StreamUsage streamUsage = static_cast<StreamUsage>(data.ReadInt32());
102 int32_t clientUid = data.ReadInt32();
103 AudioPipeType audioPipeType = static_cast<AudioPipeType>(data.ReadInt32());
104
105 int32_t result = OnAudioOutputDeviceRefined(descs, routerType, streamUsage, clientUid, audioPipeType);
106 if (result == SUCCESS) {
107 reply.WriteInt32(result);
108 reply.WriteInt32(descs.size());
109 for (auto &desc : descs) {
110 desc->Marshalling(reply);
111 }
112 } else {
113 reply.WriteInt32(result);
114 }
115 }
116
OnAudioInputDeviceRefinedInternal(MessageParcel & data,MessageParcel & reply)117 void AudioRoutingManagerListenerStub::OnAudioInputDeviceRefinedInternal(MessageParcel &data, MessageParcel &reply)
118 {
119 std::vector<std::unique_ptr<AudioDeviceDescriptor>> descs;
120 int32_t size = data.ReadInt32();
121 CHECK_AND_RETURN_LOG(size < PREFERRED_DEVICE_VALID_SIZE, "get invalid size : %{public}d", size);
122 for (int32_t i = 0; i < size; i++) {
123 descs.push_back(make_unique<AudioDeviceDescriptor>(AudioDeviceDescriptor::UnmarshallingPtr(data)));
124 }
125 RouterType routerType = static_cast<RouterType>(data.ReadInt32());
126 SourceType sourceType = static_cast<SourceType>(data.ReadInt32());
127 int32_t clientUid = data.ReadInt32();
128 AudioPipeType audioPipeType = static_cast<AudioPipeType>(data.ReadInt32());
129
130 int32_t result = OnAudioInputDeviceRefined(descs, routerType, sourceType, clientUid, audioPipeType);
131 if (result == SUCCESS) {
132 reply.WriteInt32(result);
133 reply.WriteInt32(descs.size());
134 for (auto &desc : descs) {
135 desc->Marshalling(reply);
136 }
137 } else {
138 reply.WriteInt32(result);
139 }
140 }
141
OnAudioOutputDeviceRefined(std::vector<std::unique_ptr<AudioDeviceDescriptor>> & descs,RouterType routerType,StreamUsage streamUsage,int32_t clientUid,AudioPipeType audioPipeType)142 int32_t AudioRoutingManagerListenerStub::OnAudioOutputDeviceRefined(
143 std::vector<std::unique_ptr<AudioDeviceDescriptor>> &descs, RouterType routerType, StreamUsage streamUsage,
144 int32_t clientUid, AudioPipeType audioPipeType)
145 {
146 std::unique_lock<std::mutex> lock(deviceRefinerCallbackMutex_);
147 std::shared_ptr<AudioDeviceRefiner> audioDeviceRefinerCallback = audioDeviceRefinerCallback_.lock();
148 CHECK_AND_RETURN_RET_LOG(audioDeviceRefinerCallback != nullptr,
149 ERR_CALLBACK_NOT_REGISTERED, "audioDeviceRefinerCallback_ is nullptr");
150 lock.unlock();
151
152 return audioDeviceRefinerCallback->OnAudioOutputDeviceRefined(descs, routerType, streamUsage, clientUid,
153 audioPipeType);
154 }
155
OnAudioInputDeviceRefined(std::vector<std::unique_ptr<AudioDeviceDescriptor>> & descs,RouterType routerType,SourceType sourceType,int32_t clientUid,AudioPipeType audioPipeType)156 int32_t AudioRoutingManagerListenerStub::OnAudioInputDeviceRefined(
157 std::vector<std::unique_ptr<AudioDeviceDescriptor>> &descs, RouterType routerType, SourceType sourceType,
158 int32_t clientUid, AudioPipeType audioPipeType)
159 {
160 std::unique_lock<std::mutex> lock(deviceRefinerCallbackMutex_);
161 std::shared_ptr<AudioDeviceRefiner> audioDeviceRefinerCallback = audioDeviceRefinerCallback_.lock();
162 CHECK_AND_RETURN_RET_LOG(audioDeviceRefinerCallback != nullptr, ERR_CALLBACK_NOT_REGISTERED,
163 "audioDeviceRefinerCallback_ is nullptr");
164 lock.unlock();
165
166 return audioDeviceRefinerCallback->OnAudioInputDeviceRefined(descs, routerType,
167 sourceType, clientUid, audioPipeType);
168 }
169
170 } // namespace AudioStandard
171 } // namespace OHOS
172