• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "audio_routing_manager_listener_stub.h"
17 
18 #include "audio_errors.h"
19 #include "audio_routing_manager.h"
20 #include "audio_log.h"
21 
22 namespace OHOS {
23 namespace AudioStandard {
AudioRoutingManagerListenerStub()24 AudioRoutingManagerListenerStub::AudioRoutingManagerListenerStub()
25 {
26     AUDIO_DEBUG_LOG("AudioRoutingManagerListenerStub Instance create");
27 }
28 
~AudioRoutingManagerListenerStub()29 AudioRoutingManagerListenerStub::~AudioRoutingManagerListenerStub()
30 {
31     AUDIO_DEBUG_LOG("AudioRoutingManagerListenerStub Instance destroy");
32 }
33 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)34 int AudioRoutingManagerListenerStub::OnRemoteRequest(
35     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
36 {
37     if (data.ReadInterfaceToken() != GetDescriptor()) {
38         AUDIO_ERR_LOG("AudioRingerModeUpdateListenerStub: ReadInterfaceToken failed");
39         return -1;
40     }
41     switch (code) {
42         case ON_MIC_STATE_UPDATED: {
43             MicStateChangeEvent micStateChangeEvent = {};
44 
45             micStateChangeEvent.mute = data.ReadBool();
46             OnMicStateUpdated(micStateChangeEvent);
47 
48             return AUDIO_OK;
49         }
50         case ON_ACTIVE_OUTPUT_DEVICE_UPDATED: {
51             std::vector<sptr<AudioDeviceDescriptor>> deviceInfo;
52             int32_t size = data.ReadInt32();
53             AUDIO_INFO_LOG("Entered %{public}s ,desc size:%{public}d", __func__, size);
54             for (int32_t i = 0; i < size; i++) {
55                 deviceInfo.push_back(AudioDeviceDescriptor::Unmarshalling(data));
56             }
57             OnPreferredOutputDeviceUpdated(deviceInfo);
58 
59             return AUDIO_OK;
60         }
61         case ON_ACTIVE_INPUT_DEVICE_UPDATED: {
62             std::vector<sptr<AudioDeviceDescriptor>> deviceInfo;
63             int32_t size = data.ReadInt32();
64             for (int32_t i = 0; i < size; i++) {
65                 deviceInfo.push_back(AudioDeviceDescriptor::Unmarshalling(data));
66             }
67             OnPreferredInputDeviceUpdated(deviceInfo);
68 
69             return AUDIO_OK;
70         }
71         default: {
72             AUDIO_ERR_LOG("default case, need check AudioListenerStub");
73             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
74         }
75     }
76 }
77 
OnMicStateUpdated(const MicStateChangeEvent & micStateChangeEvent)78 void AudioRoutingManagerListenerStub::OnMicStateUpdated(const MicStateChangeEvent &micStateChangeEvent)
79 {
80     AUDIO_DEBUG_LOG("AudioPolicyManagerLiternerStub OnMicStateChange start");
81     std::shared_ptr<AudioManagerMicStateChangeCallback> micStateChangedCallback = micStateChangeCallback_.lock();
82 
83     if (micStateChangedCallback == nullptr) {
84         AUDIO_ERR_LOG("OnMicStateUpdated: micStateChangeCallback_ or micStateChangeEvent is nullptr");
85         return;
86     }
87 
88     micStateChangedCallback->OnMicStateUpdated(micStateChangeEvent);
89 }
90 
SetMicStateChangeCallback(const std::weak_ptr<AudioManagerMicStateChangeCallback> & cb)91 void AudioRoutingManagerListenerStub::SetMicStateChangeCallback(
92     const std::weak_ptr<AudioManagerMicStateChangeCallback> &cb)
93 {
94     micStateChangeCallback_ = cb;
95 }
96 
OnPreferredOutputDeviceUpdated(const std::vector<sptr<AudioDeviceDescriptor>> & desc)97 void AudioRoutingManagerListenerStub::OnPreferredOutputDeviceUpdated(
98     const std::vector<sptr<AudioDeviceDescriptor>> &desc)
99 {
100     std::shared_ptr<AudioPreferredOutputDeviceChangeCallback> activeOutputDeviceChangeCallback =
101         activeOutputDeviceChangeCallback_.lock();
102 
103     if (activeOutputDeviceChangeCallback == nullptr) {
104         AUDIO_ERR_LOG("OnPreferredOutputDeviceUpdated: activeOutputDeviceChangeCallback_ is nullptr");
105         return;
106     }
107 
108     activeOutputDeviceChangeCallback->OnPreferredOutputDeviceUpdated(desc);
109 }
110 
OnPreferredInputDeviceUpdated(const std::vector<sptr<AudioDeviceDescriptor>> & desc)111 void AudioRoutingManagerListenerStub::OnPreferredInputDeviceUpdated(
112     const std::vector<sptr<AudioDeviceDescriptor>> &desc)
113 {
114     std::shared_ptr<AudioPreferredInputDeviceChangeCallback> activeInputDeviceChangeCallback =
115         activeInputDeviceChangeCallback_.lock();
116 
117     if (activeInputDeviceChangeCallback == nullptr) {
118         AUDIO_ERR_LOG("OnPreferredInputDeviceUpdated: activeInputDeviceChangeCallback_ is nullptr");
119         return;
120     }
121 
122     activeInputDeviceChangeCallback->OnPreferredInputDeviceUpdated(desc);
123 }
124 
SetPreferredOutputDeviceChangeCallback(const std::weak_ptr<AudioPreferredOutputDeviceChangeCallback> & cb)125 void AudioRoutingManagerListenerStub::SetPreferredOutputDeviceChangeCallback(
126     const std::weak_ptr<AudioPreferredOutputDeviceChangeCallback> &cb)
127 {
128     activeOutputDeviceChangeCallback_ = cb;
129 }
130 
SetPreferredInputDeviceChangeCallback(const std::weak_ptr<AudioPreferredInputDeviceChangeCallback> & callback)131 void AudioRoutingManagerListenerStub::SetPreferredInputDeviceChangeCallback(
132     const std::weak_ptr<AudioPreferredInputDeviceChangeCallback> &callback)
133 {
134     activeInputDeviceChangeCallback_ = callback;
135 }
136 } // namespace AudioStandard
137 } // namespace OHOS
138