1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "IAudioPolicyServiceClient"
18 #include <utils/Log.h>
19 
20 #include <stdint.h>
21 #include <sys/types.h>
22 
23 #include <binder/Parcel.h>
24 
25 #include <media/IAudioPolicyServiceClient.h>
26 #include <media/AudioSystem.h>
27 
28 namespace android {
29 
30 enum {
31     PORT_LIST_UPDATE = IBinder::FIRST_CALL_TRANSACTION,
32     PATCH_LIST_UPDATE,
33     MIX_STATE_UPDATE,
34     RECORDING_CONFIGURATION_UPDATE,
35     VOLUME_GROUP_CHANGED,
36 };
37 
38 // ----------------------------------------------------------------------
readAudioConfigBaseFromParcel(const Parcel & data,audio_config_base_t * config)39 inline void readAudioConfigBaseFromParcel(const Parcel& data, audio_config_base_t *config) {
40     config->sample_rate = data.readUint32();
41     config->channel_mask = (audio_channel_mask_t) data.readInt32();
42     config->format = (audio_format_t) data.readInt32();
43 }
44 
writeAudioConfigBaseToParcel(Parcel & data,const audio_config_base_t * config)45 inline void writeAudioConfigBaseToParcel(Parcel& data, const audio_config_base_t *config)
46 {
47     data.writeUint32(config->sample_rate);
48     data.writeInt32((int32_t) config->channel_mask);
49     data.writeInt32((int32_t) config->format);
50 }
51 
readRecordClientInfoFromParcel(const Parcel & data,record_client_info_t * clientInfo)52 inline void readRecordClientInfoFromParcel(const Parcel& data, record_client_info_t *clientInfo) {
53     clientInfo->riid = (audio_unique_id_t) data.readInt32();
54     clientInfo->uid = (uid_t) data.readUint32();
55     clientInfo->session = (audio_session_t) data.readInt32();
56     clientInfo->source = (audio_source_t) data.readInt32();
57     data.read(&clientInfo->port_id, sizeof(audio_port_handle_t));
58     clientInfo->silenced = data.readBool();
59 }
60 
writeRecordClientInfoToParcel(Parcel & data,const record_client_info_t * clientInfo)61 inline void writeRecordClientInfoToParcel(Parcel& data, const record_client_info_t *clientInfo) {
62     data.writeInt32((int32_t) clientInfo->riid);
63     data.writeUint32((uint32_t) clientInfo->uid);
64     data.writeInt32((int32_t) clientInfo->session);
65     data.writeInt32((int32_t) clientInfo->source);
66     data.write(&clientInfo->port_id, sizeof(audio_port_handle_t));
67     data.writeBool(clientInfo->silenced);
68 }
69 
readEffectVectorFromParcel(const Parcel & data,std::vector<effect_descriptor_t> * effects)70 inline void readEffectVectorFromParcel(const Parcel& data,
71                                        std::vector<effect_descriptor_t> *effects) {
72     int32_t numEffects = data.readInt32();
73     for (int32_t i = 0; i < numEffects; i++) {
74         effect_descriptor_t effect;
75         if (data.read(&effect, sizeof(effect_descriptor_t)) != NO_ERROR) {
76             break;
77         }
78         (*effects).push_back(effect);
79     }
80 }
81 
writeEffectVectorToParcel(Parcel & data,std::vector<effect_descriptor_t> effects)82 inline void writeEffectVectorToParcel(Parcel& data, std::vector<effect_descriptor_t> effects) {
83     data.writeUint32((uint32_t) effects.size());
84     for (const auto& effect : effects) {
85         if (data.write(&effect, sizeof(effect_descriptor_t)) != NO_ERROR) {
86             break;
87         }
88     }
89 }
90 
91 // ----------------------------------------------------------------------
92 class BpAudioPolicyServiceClient : public BpInterface<IAudioPolicyServiceClient>
93 {
94 public:
BpAudioPolicyServiceClient(const sp<IBinder> & impl)95     explicit BpAudioPolicyServiceClient(const sp<IBinder>& impl)
96         : BpInterface<IAudioPolicyServiceClient>(impl)
97     {
98     }
99 
onAudioPortListUpdate()100     void onAudioPortListUpdate()
101     {
102         Parcel data, reply;
103         data.writeInterfaceToken(IAudioPolicyServiceClient::getInterfaceDescriptor());
104         remote()->transact(PORT_LIST_UPDATE, data, &reply, IBinder::FLAG_ONEWAY);
105     }
106 
onAudioPatchListUpdate()107     void onAudioPatchListUpdate()
108     {
109         Parcel data, reply;
110         data.writeInterfaceToken(IAudioPolicyServiceClient::getInterfaceDescriptor());
111         remote()->transact(PATCH_LIST_UPDATE, data, &reply, IBinder::FLAG_ONEWAY);
112     }
113 
onAudioVolumeGroupChanged(volume_group_t group,int flags)114     void onAudioVolumeGroupChanged(volume_group_t group, int flags)
115     {
116         Parcel data, reply;
117         data.writeInterfaceToken(IAudioPolicyServiceClient::getInterfaceDescriptor());
118         data.writeUint32(group);
119         data.writeInt32(flags);
120         remote()->transact(VOLUME_GROUP_CHANGED, data, &reply, IBinder::FLAG_ONEWAY);
121     }
122 
onDynamicPolicyMixStateUpdate(String8 regId,int32_t state)123     void onDynamicPolicyMixStateUpdate(String8 regId, int32_t state)
124     {
125         Parcel data, reply;
126         data.writeInterfaceToken(IAudioPolicyServiceClient::getInterfaceDescriptor());
127         data.writeString8(regId);
128         data.writeInt32(state);
129         remote()->transact(MIX_STATE_UPDATE, data, &reply, IBinder::FLAG_ONEWAY);
130     }
131 
onRecordingConfigurationUpdate(int event,const record_client_info_t * clientInfo,const audio_config_base_t * clientConfig,std::vector<effect_descriptor_t> clientEffects,const audio_config_base_t * deviceConfig,std::vector<effect_descriptor_t> effects,audio_patch_handle_t patchHandle,audio_source_t source)132     void onRecordingConfigurationUpdate(int event,
133                                         const record_client_info_t *clientInfo,
134                                         const audio_config_base_t *clientConfig,
135                                         std::vector<effect_descriptor_t> clientEffects,
136                                         const audio_config_base_t *deviceConfig,
137                                         std::vector<effect_descriptor_t> effects,
138                                         audio_patch_handle_t patchHandle,
139                                         audio_source_t source) {
140         Parcel data, reply;
141         data.writeInterfaceToken(IAudioPolicyServiceClient::getInterfaceDescriptor());
142         data.writeInt32(event);
143         writeRecordClientInfoToParcel(data, clientInfo);
144         writeAudioConfigBaseToParcel(data, clientConfig);
145         writeEffectVectorToParcel(data, clientEffects);
146         writeAudioConfigBaseToParcel(data, deviceConfig);
147         writeEffectVectorToParcel(data, effects);
148         data.writeInt32(patchHandle);
149         data.writeInt32((int32_t) source);
150         remote()->transact(RECORDING_CONFIGURATION_UPDATE, data, &reply, IBinder::FLAG_ONEWAY);
151     }
152 };
153 
154 IMPLEMENT_META_INTERFACE(AudioPolicyServiceClient, "android.media.IAudioPolicyServiceClient");
155 
156 // ----------------------------------------------------------------------
157 
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)158 status_t BnAudioPolicyServiceClient::onTransact(
159     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
160 {
161     switch (code) {
162     case PORT_LIST_UPDATE: {
163             CHECK_INTERFACE(IAudioPolicyServiceClient, data, reply);
164             onAudioPortListUpdate();
165             return NO_ERROR;
166         } break;
167     case PATCH_LIST_UPDATE: {
168             CHECK_INTERFACE(IAudioPolicyServiceClient, data, reply);
169             onAudioPatchListUpdate();
170             return NO_ERROR;
171         } break;
172     case VOLUME_GROUP_CHANGED: {
173             CHECK_INTERFACE(IAudioPolicyServiceClient, data, reply);
174             volume_group_t group = static_cast<volume_group_t>(data.readUint32());
175             int flags = data.readInt32();
176             onAudioVolumeGroupChanged(group, flags);
177             return NO_ERROR;
178         } break;
179     case MIX_STATE_UPDATE: {
180             CHECK_INTERFACE(IAudioPolicyServiceClient, data, reply);
181             String8 regId = data.readString8();
182             int32_t state = data.readInt32();
183             onDynamicPolicyMixStateUpdate(regId, state);
184             return NO_ERROR;
185         } break;
186     case RECORDING_CONFIGURATION_UPDATE: {
187             CHECK_INTERFACE(IAudioPolicyServiceClient, data, reply);
188             int event = (int) data.readInt32();
189             record_client_info_t clientInfo;
190             audio_config_base_t clientConfig;
191             audio_config_base_t deviceConfig;
192             readRecordClientInfoFromParcel(data, &clientInfo);
193             readAudioConfigBaseFromParcel(data, &clientConfig);
194             std::vector<effect_descriptor_t> clientEffects;
195             readEffectVectorFromParcel(data, &clientEffects);
196             readAudioConfigBaseFromParcel(data, &deviceConfig);
197             std::vector<effect_descriptor_t> effects;
198             readEffectVectorFromParcel(data, &effects);
199             audio_patch_handle_t patchHandle = (audio_patch_handle_t) data.readInt32();
200             audio_source_t source = (audio_source_t) data.readInt32();
201             onRecordingConfigurationUpdate(event, &clientInfo, &clientConfig, clientEffects,
202                                            &deviceConfig, effects, patchHandle, source);
203             return NO_ERROR;
204         } break;
205     default:
206         return BBinder::onTransact(code, data, reply, flags);
207     }
208 }
209 
210 // ----------------------------------------------------------------------------
211 
212 } // namespace android
213