Home | History | Annotate | Download | only in libmedia
      1 /*
      2 **
      3 ** Copyright 2009, The Android Open Source Project
      4 **
      5 ** Licensed under the Apache License, Version 2.0 (the "License");
      6 ** you may not use this file except in compliance with the License.
      7 ** You may obtain a copy of the License at
      8 **
      9 **     http://www.apache.org/licenses/LICENSE-2.0
     10 **
     11 ** Unless required by applicable law or agreed to in writing, software
     12 ** distributed under the License is distributed on an "AS IS" BASIS,
     13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 ** See the License for the specific language governing permissions and
     15 ** limitations under the License.
     16 */
     17 
     18 #define LOG_TAG "IAudioPolicyService"
     19 #include <utils/Log.h>
     20 
     21 #include <stdint.h>
     22 #include <sys/types.h>
     23 
     24 #include <binder/Parcel.h>
     25 
     26 #include <media/IAudioPolicyService.h>
     27 
     28 namespace android {
     29 
     30 enum {
     31     SET_DEVICE_CONNECTION_STATE = IBinder::FIRST_CALL_TRANSACTION,
     32     GET_DEVICE_CONNECTION_STATE,
     33     SET_PHONE_STATE,
     34     SET_RINGER_MODE,
     35     SET_FORCE_USE,
     36     GET_FORCE_USE,
     37     GET_OUTPUT,
     38     START_OUTPUT,
     39     STOP_OUTPUT,
     40     RELEASE_OUTPUT,
     41     GET_INPUT,
     42     START_INPUT,
     43     STOP_INPUT,
     44     RELEASE_INPUT,
     45     INIT_STREAM_VOLUME,
     46     SET_STREAM_VOLUME,
     47     GET_STREAM_VOLUME
     48 };
     49 
     50 class BpAudioPolicyService : public BpInterface<IAudioPolicyService>
     51 {
     52 public:
     53     BpAudioPolicyService(const sp<IBinder>& impl)
     54         : BpInterface<IAudioPolicyService>(impl)
     55     {
     56     }
     57 
     58     virtual status_t setDeviceConnectionState(
     59                                     AudioSystem::audio_devices device,
     60                                     AudioSystem::device_connection_state state,
     61                                     const char *device_address)
     62     {
     63         Parcel data, reply;
     64         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
     65         data.writeInt32(static_cast <uint32_t>(device));
     66         data.writeInt32(static_cast <uint32_t>(state));
     67         data.writeCString(device_address);
     68         remote()->transact(SET_DEVICE_CONNECTION_STATE, data, &reply);
     69         return static_cast <status_t> (reply.readInt32());
     70     }
     71 
     72     virtual AudioSystem::device_connection_state getDeviceConnectionState(
     73                                     AudioSystem::audio_devices device,
     74                                     const char *device_address)
     75     {
     76         Parcel data, reply;
     77         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
     78         data.writeInt32(static_cast <uint32_t>(device));
     79         data.writeCString(device_address);
     80         remote()->transact(GET_DEVICE_CONNECTION_STATE, data, &reply);
     81         return static_cast <AudioSystem::device_connection_state>(reply.readInt32());
     82     }
     83 
     84     virtual status_t setPhoneState(int state)
     85     {
     86         Parcel data, reply;
     87         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
     88         data.writeInt32(state);
     89         remote()->transact(SET_PHONE_STATE, data, &reply);
     90         return static_cast <status_t> (reply.readInt32());
     91     }
     92 
     93     virtual status_t setRingerMode(uint32_t mode, uint32_t mask)
     94     {
     95         Parcel data, reply;
     96         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
     97         data.writeInt32(mode);
     98         data.writeInt32(mask);
     99         remote()->transact(SET_RINGER_MODE, data, &reply);
    100         return static_cast <status_t> (reply.readInt32());
    101     }
    102 
    103     virtual status_t setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
    104     {
    105         Parcel data, reply;
    106         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    107         data.writeInt32(static_cast <uint32_t>(usage));
    108         data.writeInt32(static_cast <uint32_t>(config));
    109         remote()->transact(SET_FORCE_USE, data, &reply);
    110         return static_cast <status_t> (reply.readInt32());
    111     }
    112 
    113     virtual AudioSystem::forced_config getForceUse(AudioSystem::force_use usage)
    114     {
    115         Parcel data, reply;
    116         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    117         data.writeInt32(static_cast <uint32_t>(usage));
    118         remote()->transact(GET_FORCE_USE, data, &reply);
    119         return static_cast <AudioSystem::forced_config> (reply.readInt32());
    120     }
    121 
    122     virtual audio_io_handle_t getOutput(
    123                                         AudioSystem::stream_type stream,
    124                                         uint32_t samplingRate,
    125                                         uint32_t format,
    126                                         uint32_t channels,
    127                                         AudioSystem::output_flags flags)
    128     {
    129         Parcel data, reply;
    130         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    131         data.writeInt32(static_cast <uint32_t>(stream));
    132         data.writeInt32(samplingRate);
    133         data.writeInt32(static_cast <uint32_t>(format));
    134         data.writeInt32(channels);
    135         data.writeInt32(static_cast <uint32_t>(flags));
    136         remote()->transact(GET_OUTPUT, data, &reply);
    137         return static_cast <audio_io_handle_t> (reply.readInt32());
    138     }
    139 
    140     virtual status_t startOutput(audio_io_handle_t output, AudioSystem::stream_type stream)
    141     {
    142         Parcel data, reply;
    143         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    144         data.writeInt32(output);
    145         data.writeInt32(stream);
    146         remote()->transact(START_OUTPUT, data, &reply);
    147         return static_cast <status_t> (reply.readInt32());
    148     }
    149 
    150     virtual status_t stopOutput(audio_io_handle_t output, AudioSystem::stream_type stream)
    151     {
    152         Parcel data, reply;
    153         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    154         data.writeInt32(output);
    155         data.writeInt32(stream);
    156         remote()->transact(STOP_OUTPUT, data, &reply);
    157         return static_cast <status_t> (reply.readInt32());
    158     }
    159 
    160     virtual void releaseOutput(audio_io_handle_t output)
    161     {
    162         Parcel data, reply;
    163         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    164         data.writeInt32(output);
    165         remote()->transact(RELEASE_OUTPUT, data, &reply);
    166     }
    167 
    168     virtual audio_io_handle_t getInput(
    169                                     int inputSource,
    170                                     uint32_t samplingRate,
    171                                     uint32_t format,
    172                                     uint32_t channels,
    173                                     AudioSystem::audio_in_acoustics acoustics)
    174     {
    175         Parcel data, reply;
    176         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    177         data.writeInt32(inputSource);
    178         data.writeInt32(samplingRate);
    179         data.writeInt32(static_cast <uint32_t>(format));
    180         data.writeInt32(channels);
    181         data.writeInt32(static_cast <uint32_t>(acoustics));
    182         remote()->transact(GET_INPUT, data, &reply);
    183         return static_cast <audio_io_handle_t> (reply.readInt32());
    184     }
    185 
    186     virtual status_t startInput(audio_io_handle_t input)
    187     {
    188         Parcel data, reply;
    189         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    190         data.writeInt32(input);
    191         remote()->transact(START_INPUT, data, &reply);
    192         return static_cast <status_t> (reply.readInt32());
    193     }
    194 
    195     virtual status_t stopInput(audio_io_handle_t input)
    196     {
    197         Parcel data, reply;
    198         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    199         data.writeInt32(input);
    200         remote()->transact(STOP_INPUT, data, &reply);
    201         return static_cast <status_t> (reply.readInt32());
    202     }
    203 
    204     virtual void releaseInput(audio_io_handle_t input)
    205     {
    206         Parcel data, reply;
    207         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    208         data.writeInt32(input);
    209         remote()->transact(RELEASE_INPUT, data, &reply);
    210     }
    211 
    212     virtual status_t initStreamVolume(AudioSystem::stream_type stream,
    213                                     int indexMin,
    214                                     int indexMax)
    215     {
    216         Parcel data, reply;
    217         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    218         data.writeInt32(static_cast <uint32_t>(stream));
    219         data.writeInt32(indexMin);
    220         data.writeInt32(indexMax);
    221         remote()->transact(INIT_STREAM_VOLUME, data, &reply);
    222         return static_cast <status_t> (reply.readInt32());
    223     }
    224 
    225     virtual status_t setStreamVolumeIndex(AudioSystem::stream_type stream, int index)
    226     {
    227         Parcel data, reply;
    228         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    229         data.writeInt32(static_cast <uint32_t>(stream));
    230         data.writeInt32(index);
    231         remote()->transact(SET_STREAM_VOLUME, data, &reply);
    232         return static_cast <status_t> (reply.readInt32());
    233     }
    234 
    235     virtual status_t getStreamVolumeIndex(AudioSystem::stream_type stream, int *index)
    236     {
    237         Parcel data, reply;
    238         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    239         data.writeInt32(static_cast <uint32_t>(stream));
    240         remote()->transact(GET_STREAM_VOLUME, data, &reply);
    241         int lIndex = reply.readInt32();
    242         if (index) *index = lIndex;
    243         return static_cast <status_t> (reply.readInt32());
    244     }
    245 };
    246 
    247 IMPLEMENT_META_INTERFACE(AudioPolicyService, "android.media.IAudioPolicyService");
    248 
    249 // ----------------------------------------------------------------------
    250 
    251 
    252 status_t BnAudioPolicyService::onTransact(
    253     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
    254 {
    255     switch(code) {
    256         case SET_DEVICE_CONNECTION_STATE: {
    257             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    258             AudioSystem::audio_devices device = static_cast <AudioSystem::audio_devices>(data.readInt32());
    259             AudioSystem::device_connection_state state = static_cast <AudioSystem::device_connection_state>(data.readInt32());
    260             const char *device_address = data.readCString();
    261             reply->writeInt32(static_cast <uint32_t>(setDeviceConnectionState(device, state, device_address)));
    262             return NO_ERROR;
    263         } break;
    264 
    265         case GET_DEVICE_CONNECTION_STATE: {
    266             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    267             AudioSystem::audio_devices device = static_cast <AudioSystem::audio_devices>(data.readInt32());
    268             const char *device_address = data.readCString();
    269             reply->writeInt32(static_cast <uint32_t>(getDeviceConnectionState(device, device_address)));
    270             return NO_ERROR;
    271         } break;
    272 
    273         case SET_PHONE_STATE: {
    274             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    275             reply->writeInt32(static_cast <uint32_t>(setPhoneState(data.readInt32())));
    276             return NO_ERROR;
    277         } break;
    278 
    279         case SET_RINGER_MODE: {
    280             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    281             uint32_t mode = data.readInt32();
    282             uint32_t mask = data.readInt32();
    283             reply->writeInt32(static_cast <uint32_t>(setRingerMode(mode, mask)));
    284             return NO_ERROR;
    285         } break;
    286 
    287         case SET_FORCE_USE: {
    288             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    289             AudioSystem::force_use usage = static_cast <AudioSystem::force_use>(data.readInt32());
    290             AudioSystem::forced_config config = static_cast <AudioSystem::forced_config>(data.readInt32());
    291             reply->writeInt32(static_cast <uint32_t>(setForceUse(usage, config)));
    292             return NO_ERROR;
    293         } break;
    294 
    295         case GET_FORCE_USE: {
    296             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    297             AudioSystem::force_use usage = static_cast <AudioSystem::force_use>(data.readInt32());
    298             reply->writeInt32(static_cast <uint32_t>(getForceUse(usage)));
    299             return NO_ERROR;
    300         } break;
    301 
    302         case GET_OUTPUT: {
    303             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    304             AudioSystem::stream_type stream = static_cast <AudioSystem::stream_type>(data.readInt32());
    305             uint32_t samplingRate = data.readInt32();
    306             uint32_t format = data.readInt32();
    307             uint32_t channels = data.readInt32();
    308             AudioSystem::output_flags flags = static_cast <AudioSystem::output_flags>(data.readInt32());
    309 
    310             audio_io_handle_t output = getOutput(stream,
    311                                                  samplingRate,
    312                                                  format,
    313                                                  channels,
    314                                                  flags);
    315             reply->writeInt32(static_cast <int>(output));
    316             return NO_ERROR;
    317         } break;
    318 
    319         case START_OUTPUT: {
    320             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    321             audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
    322             uint32_t stream = data.readInt32();
    323             reply->writeInt32(static_cast <uint32_t>(startOutput(output, (AudioSystem::stream_type)stream)));
    324             return NO_ERROR;
    325         } break;
    326 
    327         case STOP_OUTPUT: {
    328             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    329             audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
    330             uint32_t stream = data.readInt32();
    331             reply->writeInt32(static_cast <uint32_t>(stopOutput(output, (AudioSystem::stream_type)stream)));
    332             return NO_ERROR;
    333         } break;
    334 
    335         case RELEASE_OUTPUT: {
    336             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    337             audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
    338             releaseOutput(output);
    339             return NO_ERROR;
    340         } break;
    341 
    342         case GET_INPUT: {
    343             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    344             int inputSource = data.readInt32();
    345             uint32_t samplingRate = data.readInt32();
    346             uint32_t format = data.readInt32();
    347             uint32_t channels = data.readInt32();
    348             AudioSystem::audio_in_acoustics acoustics = static_cast <AudioSystem::audio_in_acoustics>(data.readInt32());
    349             audio_io_handle_t input = getInput(inputSource,
    350                                                samplingRate,
    351                                                format,
    352                                                channels,
    353                                                acoustics);
    354             reply->writeInt32(static_cast <int>(input));
    355             return NO_ERROR;
    356         } break;
    357 
    358         case START_INPUT: {
    359             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    360             audio_io_handle_t input = static_cast <audio_io_handle_t>(data.readInt32());
    361             reply->writeInt32(static_cast <uint32_t>(startInput(input)));
    362             return NO_ERROR;
    363         } break;
    364 
    365         case STOP_INPUT: {
    366             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    367             audio_io_handle_t input = static_cast <audio_io_handle_t>(data.readInt32());
    368             reply->writeInt32(static_cast <uint32_t>(stopInput(input)));
    369             return NO_ERROR;
    370         } break;
    371 
    372         case RELEASE_INPUT: {
    373             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    374             audio_io_handle_t input = static_cast <audio_io_handle_t>(data.readInt32());
    375             releaseInput(input);
    376             return NO_ERROR;
    377         } break;
    378 
    379         case INIT_STREAM_VOLUME: {
    380             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    381             AudioSystem::stream_type stream = static_cast <AudioSystem::stream_type>(data.readInt32());
    382             int indexMin = data.readInt32();
    383             int indexMax = data.readInt32();
    384             reply->writeInt32(static_cast <uint32_t>(initStreamVolume(stream, indexMin,indexMax)));
    385             return NO_ERROR;
    386         } break;
    387 
    388         case SET_STREAM_VOLUME: {
    389             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    390             AudioSystem::stream_type stream = static_cast <AudioSystem::stream_type>(data.readInt32());
    391             int index = data.readInt32();
    392             reply->writeInt32(static_cast <uint32_t>(setStreamVolumeIndex(stream, index)));
    393             return NO_ERROR;
    394         } break;
    395 
    396         case GET_STREAM_VOLUME: {
    397             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    398             AudioSystem::stream_type stream = static_cast <AudioSystem::stream_type>(data.readInt32());
    399             int index;
    400             status_t status = getStreamVolumeIndex(stream, &index);
    401             reply->writeInt32(index);
    402             reply->writeInt32(static_cast <uint32_t>(status));
    403             return NO_ERROR;
    404         } break;
    405 
    406         default:
    407             return BBinder::onTransact(code, data, reply, flags);
    408     }
    409 }
    410 
    411 // ----------------------------------------------------------------------------
    412 
    413 }; // namespace android
    414