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
28namespace android {
29
30enum {
31    PORT_LIST_UPDATE = IBinder::FIRST_CALL_TRANSACTION,
32    PATCH_LIST_UPDATE,
33    MIX_STATE_UPDATE,
34    RECORDING_CONFIGURATION_UPDATE
35};
36
37// ----------------------------------------------------------------------
38inline void readAudioConfigBaseFromParcel(const Parcel& data, audio_config_base_t *config) {
39    config->sample_rate = data.readUint32();
40    config->channel_mask = (audio_channel_mask_t) data.readInt32();
41    config->format = (audio_format_t) data.readInt32();
42}
43
44inline void writeAudioConfigBaseToParcel(Parcel& data, const audio_config_base_t *config)
45{
46    data.writeUint32(config->sample_rate);
47    data.writeInt32((int32_t) config->channel_mask);
48    data.writeInt32((int32_t) config->format);
49}
50
51// ----------------------------------------------------------------------
52class BpAudioPolicyServiceClient : public BpInterface<IAudioPolicyServiceClient>
53{
54public:
55    BpAudioPolicyServiceClient(const sp<IBinder>& impl)
56        : BpInterface<IAudioPolicyServiceClient>(impl)
57    {
58    }
59
60    void onAudioPortListUpdate()
61    {
62        Parcel data, reply;
63        data.writeInterfaceToken(IAudioPolicyServiceClient::getInterfaceDescriptor());
64        remote()->transact(PORT_LIST_UPDATE, data, &reply, IBinder::FLAG_ONEWAY);
65    }
66
67    void onAudioPatchListUpdate()
68    {
69        Parcel data, reply;
70        data.writeInterfaceToken(IAudioPolicyServiceClient::getInterfaceDescriptor());
71        remote()->transact(PATCH_LIST_UPDATE, data, &reply, IBinder::FLAG_ONEWAY);
72    }
73
74    void onDynamicPolicyMixStateUpdate(String8 regId, int32_t state)
75    {
76        Parcel data, reply;
77        data.writeInterfaceToken(IAudioPolicyServiceClient::getInterfaceDescriptor());
78        data.writeString8(regId);
79        data.writeInt32(state);
80        remote()->transact(MIX_STATE_UPDATE, data, &reply, IBinder::FLAG_ONEWAY);
81    }
82
83    void onRecordingConfigurationUpdate(int event, audio_session_t session,
84            audio_source_t source, const audio_config_base_t *clientConfig,
85            const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle) {
86        Parcel data, reply;
87        data.writeInterfaceToken(IAudioPolicyServiceClient::getInterfaceDescriptor());
88        data.writeInt32(event);
89        data.writeInt32(session);
90        data.writeInt32(source);
91        writeAudioConfigBaseToParcel(data, clientConfig);
92        writeAudioConfigBaseToParcel(data, deviceConfig);
93        data.writeInt32(patchHandle);
94        remote()->transact(RECORDING_CONFIGURATION_UPDATE, data, &reply, IBinder::FLAG_ONEWAY);
95    }
96};
97
98IMPLEMENT_META_INTERFACE(AudioPolicyServiceClient, "android.media.IAudioPolicyServiceClient");
99
100// ----------------------------------------------------------------------
101
102status_t BnAudioPolicyServiceClient::onTransact(
103    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
104{
105    switch (code) {
106    case PORT_LIST_UPDATE: {
107            CHECK_INTERFACE(IAudioPolicyServiceClient, data, reply);
108            onAudioPortListUpdate();
109            return NO_ERROR;
110        } break;
111    case PATCH_LIST_UPDATE: {
112            CHECK_INTERFACE(IAudioPolicyServiceClient, data, reply);
113            onAudioPatchListUpdate();
114            return NO_ERROR;
115        } break;
116    case MIX_STATE_UPDATE: {
117            CHECK_INTERFACE(IAudioPolicyServiceClient, data, reply);
118            String8 regId = data.readString8();
119            int32_t state = data.readInt32();
120            onDynamicPolicyMixStateUpdate(regId, state);
121            return NO_ERROR;
122        } break;
123    case RECORDING_CONFIGURATION_UPDATE: {
124            CHECK_INTERFACE(IAudioPolicyServiceClient, data, reply);
125            int event = (int) data.readInt32();
126            audio_session_t session = (audio_session_t) data.readInt32();
127            audio_source_t source = (audio_source_t) data.readInt32();
128            audio_config_base_t clientConfig;
129            audio_config_base_t deviceConfig;
130            readAudioConfigBaseFromParcel(data, &clientConfig);
131            readAudioConfigBaseFromParcel(data, &deviceConfig);
132            audio_patch_handle_t patchHandle = (audio_patch_handle_t) data.readInt32();
133            onRecordingConfigurationUpdate(event, session, source, &clientConfig, &deviceConfig,
134                    patchHandle);
135            return NO_ERROR;
136        } break;
137    default:
138        return BBinder::onTransact(code, data, reply, flags);
139    }
140}
141
142// ----------------------------------------------------------------------------
143
144} // namespace android
145