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::Unmarshalling(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     audioDeviceRefinerCallback_ = callback;
87     std::shared_ptr<AudioDeviceRefiner> audioDeviceRefinerCallback = audioDeviceRefinerCallback_.lock();
88     CHECK_AND_RETURN_LOG(audioDeviceRefinerCallback != nullptr, "audioDeviceRefinerCallback_ is nullptr");
89 }
90 
OnAudioOutputDeviceRefinedInternal(MessageParcel & data,MessageParcel & reply)91 void AudioRoutingManagerListenerStub::OnAudioOutputDeviceRefinedInternal(MessageParcel &data, MessageParcel &reply)
92 {
93     std::vector<std::unique_ptr<AudioDeviceDescriptor>> descs;
94     int32_t size = data.ReadInt32();
95     CHECK_AND_RETURN_LOG(size < PREFERRED_DEVICE_VALID_SIZE, "get invalid size : %{public}d", size);
96     for (int32_t i = 0; i < size; i++) {
97         descs.push_back(make_unique<AudioDeviceDescriptor>(AudioDeviceDescriptor::Unmarshalling(data)));
98     }
99     RouterType routerType = static_cast<RouterType>(data.ReadInt32());
100     StreamUsage streamUsage = static_cast<StreamUsage>(data.ReadInt32());
101     int32_t clientUid = data.ReadInt32();
102     AudioPipeType audioPipeType = static_cast<AudioPipeType>(data.ReadInt32());
103 
104     int32_t result = OnAudioOutputDeviceRefined(descs, routerType, streamUsage, clientUid, audioPipeType);
105     if (result == SUCCESS) {
106         reply.WriteInt32(result);
107         reply.WriteInt32(descs.size());
108         for (auto &desc : descs) {
109             desc->Marshalling(reply);
110         }
111     } else {
112         reply.WriteInt32(result);
113     }
114 }
115 
OnAudioInputDeviceRefinedInternal(MessageParcel & data,MessageParcel & reply)116 void AudioRoutingManagerListenerStub::OnAudioInputDeviceRefinedInternal(MessageParcel &data, MessageParcel &reply)
117 {
118     std::vector<std::unique_ptr<AudioDeviceDescriptor>> descs;
119     int32_t size = data.ReadInt32();
120     CHECK_AND_RETURN_LOG(size < PREFERRED_DEVICE_VALID_SIZE, "get invalid size : %{public}d", size);
121     for (int32_t i = 0; i < size; i++) {
122         descs.push_back(make_unique<AudioDeviceDescriptor>(AudioDeviceDescriptor::Unmarshalling(data)));
123     }
124     RouterType routerType = static_cast<RouterType>(data.ReadInt32());
125     SourceType sourceType = static_cast<SourceType>(data.ReadInt32());
126     int32_t clientUid = data.ReadInt32();
127     AudioPipeType audioPipeType = static_cast<AudioPipeType>(data.ReadInt32());
128 
129     int32_t result = OnAudioInputDeviceRefined(descs, routerType, sourceType, clientUid, audioPipeType);
130     if (result == SUCCESS) {
131         reply.WriteInt32(result);
132         reply.WriteInt32(descs.size());
133         for (auto &desc : descs) {
134             desc->Marshalling(reply);
135         }
136     } else {
137         reply.WriteInt32(result);
138     }
139 }
140 
OnAudioOutputDeviceRefined(std::vector<std::unique_ptr<AudioDeviceDescriptor>> & descs,RouterType routerType,StreamUsage streamUsage,int32_t clientUid,AudioPipeType audioPipeType)141 int32_t AudioRoutingManagerListenerStub::OnAudioOutputDeviceRefined(
142     std::vector<std::unique_ptr<AudioDeviceDescriptor>> &descs, RouterType routerType, StreamUsage streamUsage,
143     int32_t clientUid, AudioPipeType audioPipeType)
144 {
145     std::shared_ptr<AudioDeviceRefiner> audioDeviceRefinerCallback = audioDeviceRefinerCallback_.lock();
146     CHECK_AND_RETURN_RET_LOG(audioDeviceRefinerCallback != nullptr,
147         ERR_CALLBACK_NOT_REGISTERED, "audioDeviceRefinerCallback_ is nullptr");
148 
149     return audioDeviceRefinerCallback->OnAudioOutputDeviceRefined(descs, routerType, streamUsage, clientUid,
150         audioPipeType);
151 }
152 
OnAudioInputDeviceRefined(std::vector<std::unique_ptr<AudioDeviceDescriptor>> & descs,RouterType routerType,SourceType sourceType,int32_t clientUid,AudioPipeType audioPipeType)153 int32_t AudioRoutingManagerListenerStub::OnAudioInputDeviceRefined(
154     std::vector<std::unique_ptr<AudioDeviceDescriptor>> &descs, RouterType routerType, SourceType sourceType,
155     int32_t clientUid, AudioPipeType audioPipeType)
156 {
157     std::shared_ptr<AudioDeviceRefiner> audioDeviceRefinerCallback = audioDeviceRefinerCallback_.lock();
158     CHECK_AND_RETURN_RET_LOG(audioDeviceRefinerCallback != nullptr, ERR_CALLBACK_NOT_REGISTERED,
159         "audioDeviceRefinerCallback_ is nullptr");
160 
161     return audioDeviceRefinerCallback->OnAudioInputDeviceRefined(descs, routerType,
162         sourceType, clientUid, audioPipeType);
163 }
164 
165 } // namespace AudioStandard
166 } // namespace OHOS
167