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     GET_STRATEGY_FOR_STREAM,
     49     GET_OUTPUT_FOR_EFFECT,
     50     REGISTER_EFFECT,
     51     UNREGISTER_EFFECT
     52 };
     53 
     54 class BpAudioPolicyService : public BpInterface<IAudioPolicyService>
     55 {
     56 public:
     57     BpAudioPolicyService(const sp<IBinder>& impl)
     58         : BpInterface<IAudioPolicyService>(impl)
     59     {
     60     }
     61 
     62     virtual status_t setDeviceConnectionState(
     63                                     AudioSystem::audio_devices device,
     64                                     AudioSystem::device_connection_state state,
     65                                     const char *device_address)
     66     {
     67         Parcel data, reply;
     68         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
     69         data.writeInt32(static_cast <uint32_t>(device));
     70         data.writeInt32(static_cast <uint32_t>(state));
     71         data.writeCString(device_address);
     72         remote()->transact(SET_DEVICE_CONNECTION_STATE, data, &reply);
     73         return static_cast <status_t> (reply.readInt32());
     74     }
     75 
     76     virtual AudioSystem::device_connection_state getDeviceConnectionState(
     77                                     AudioSystem::audio_devices device,
     78                                     const char *device_address)
     79     {
     80         Parcel data, reply;
     81         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
     82         data.writeInt32(static_cast <uint32_t>(device));
     83         data.writeCString(device_address);
     84         remote()->transact(GET_DEVICE_CONNECTION_STATE, data, &reply);
     85         return static_cast <AudioSystem::device_connection_state>(reply.readInt32());
     86     }
     87 
     88     virtual status_t setPhoneState(int state)
     89     {
     90         Parcel data, reply;
     91         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
     92         data.writeInt32(state);
     93         remote()->transact(SET_PHONE_STATE, data, &reply);
     94         return static_cast <status_t> (reply.readInt32());
     95     }
     96 
     97     virtual status_t setRingerMode(uint32_t mode, uint32_t mask)
     98     {
     99         Parcel data, reply;
    100         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    101         data.writeInt32(mode);
    102         data.writeInt32(mask);
    103         remote()->transact(SET_RINGER_MODE, data, &reply);
    104         return static_cast <status_t> (reply.readInt32());
    105     }
    106 
    107     virtual status_t setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
    108     {
    109         Parcel data, reply;
    110         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    111         data.writeInt32(static_cast <uint32_t>(usage));
    112         data.writeInt32(static_cast <uint32_t>(config));
    113         remote()->transact(SET_FORCE_USE, data, &reply);
    114         return static_cast <status_t> (reply.readInt32());
    115     }
    116 
    117     virtual AudioSystem::forced_config getForceUse(AudioSystem::force_use usage)
    118     {
    119         Parcel data, reply;
    120         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    121         data.writeInt32(static_cast <uint32_t>(usage));
    122         remote()->transact(GET_FORCE_USE, data, &reply);
    123         return static_cast <AudioSystem::forced_config> (reply.readInt32());
    124     }
    125 
    126     virtual audio_io_handle_t getOutput(
    127                                         AudioSystem::stream_type stream,
    128                                         uint32_t samplingRate,
    129                                         uint32_t format,
    130                                         uint32_t channels,
    131                                         AudioSystem::output_flags flags)
    132     {
    133         Parcel data, reply;
    134         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    135         data.writeInt32(static_cast <uint32_t>(stream));
    136         data.writeInt32(samplingRate);
    137         data.writeInt32(static_cast <uint32_t>(format));
    138         data.writeInt32(channels);
    139         data.writeInt32(static_cast <uint32_t>(flags));
    140         remote()->transact(GET_OUTPUT, data, &reply);
    141         return static_cast <audio_io_handle_t> (reply.readInt32());
    142     }
    143 
    144     virtual status_t startOutput(audio_io_handle_t output,
    145                                  AudioSystem::stream_type stream,
    146                                  int session)
    147     {
    148         Parcel data, reply;
    149         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    150         data.writeInt32(output);
    151         data.writeInt32(stream);
    152         data.writeInt32(session);
    153         remote()->transact(START_OUTPUT, data, &reply);
    154         return static_cast <status_t> (reply.readInt32());
    155     }
    156 
    157     virtual status_t stopOutput(audio_io_handle_t output,
    158                                 AudioSystem::stream_type stream,
    159                                 int session)
    160     {
    161         Parcel data, reply;
    162         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    163         data.writeInt32(output);
    164         data.writeInt32(stream);
    165         data.writeInt32(session);
    166         remote()->transact(STOP_OUTPUT, data, &reply);
    167         return static_cast <status_t> (reply.readInt32());
    168     }
    169 
    170     virtual void releaseOutput(audio_io_handle_t output)
    171     {
    172         Parcel data, reply;
    173         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    174         data.writeInt32(output);
    175         remote()->transact(RELEASE_OUTPUT, data, &reply);
    176     }
    177 
    178     virtual audio_io_handle_t getInput(
    179                                     int inputSource,
    180                                     uint32_t samplingRate,
    181                                     uint32_t format,
    182                                     uint32_t channels,
    183                                     AudioSystem::audio_in_acoustics acoustics)
    184     {
    185         Parcel data, reply;
    186         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    187         data.writeInt32(inputSource);
    188         data.writeInt32(samplingRate);
    189         data.writeInt32(static_cast <uint32_t>(format));
    190         data.writeInt32(channels);
    191         data.writeInt32(static_cast <uint32_t>(acoustics));
    192         remote()->transact(GET_INPUT, data, &reply);
    193         return static_cast <audio_io_handle_t> (reply.readInt32());
    194     }
    195 
    196     virtual status_t startInput(audio_io_handle_t input)
    197     {
    198         Parcel data, reply;
    199         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    200         data.writeInt32(input);
    201         remote()->transact(START_INPUT, data, &reply);
    202         return static_cast <status_t> (reply.readInt32());
    203     }
    204 
    205     virtual status_t stopInput(audio_io_handle_t input)
    206     {
    207         Parcel data, reply;
    208         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    209         data.writeInt32(input);
    210         remote()->transact(STOP_INPUT, data, &reply);
    211         return static_cast <status_t> (reply.readInt32());
    212     }
    213 
    214     virtual void releaseInput(audio_io_handle_t input)
    215     {
    216         Parcel data, reply;
    217         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    218         data.writeInt32(input);
    219         remote()->transact(RELEASE_INPUT, data, &reply);
    220     }
    221 
    222     virtual status_t initStreamVolume(AudioSystem::stream_type stream,
    223                                     int indexMin,
    224                                     int indexMax)
    225     {
    226         Parcel data, reply;
    227         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    228         data.writeInt32(static_cast <uint32_t>(stream));
    229         data.writeInt32(indexMin);
    230         data.writeInt32(indexMax);
    231         remote()->transact(INIT_STREAM_VOLUME, data, &reply);
    232         return static_cast <status_t> (reply.readInt32());
    233     }
    234 
    235     virtual status_t setStreamVolumeIndex(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         data.writeInt32(index);
    241         remote()->transact(SET_STREAM_VOLUME, data, &reply);
    242         return static_cast <status_t> (reply.readInt32());
    243     }
    244 
    245     virtual status_t getStreamVolumeIndex(AudioSystem::stream_type stream, int *index)
    246     {
    247         Parcel data, reply;
    248         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    249         data.writeInt32(static_cast <uint32_t>(stream));
    250         remote()->transact(GET_STREAM_VOLUME, data, &reply);
    251         int lIndex = reply.readInt32();
    252         if (index) *index = lIndex;
    253         return static_cast <status_t> (reply.readInt32());
    254     }
    255 
    256     virtual uint32_t getStrategyForStream(AudioSystem::stream_type stream)
    257     {
    258         Parcel data, reply;
    259         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    260         data.writeInt32(static_cast <uint32_t>(stream));
    261         remote()->transact(GET_STRATEGY_FOR_STREAM, data, &reply);
    262         return reply.readInt32();
    263     }
    264 
    265     virtual audio_io_handle_t getOutputForEffect(effect_descriptor_t *desc)
    266     {
    267         Parcel data, reply;
    268         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    269         data.write(desc, sizeof(effect_descriptor_t));
    270         remote()->transact(GET_OUTPUT_FOR_EFFECT, data, &reply);
    271         return static_cast <audio_io_handle_t> (reply.readInt32());
    272     }
    273 
    274     virtual status_t registerEffect(effect_descriptor_t *desc,
    275                                         audio_io_handle_t output,
    276                                         uint32_t strategy,
    277                                         int session,
    278                                         int id)
    279     {
    280         Parcel data, reply;
    281         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    282         data.write(desc, sizeof(effect_descriptor_t));
    283         data.writeInt32(output);
    284         data.writeInt32(strategy);
    285         data.writeInt32(session);
    286         data.writeInt32(id);
    287         remote()->transact(REGISTER_EFFECT, data, &reply);
    288         return static_cast <status_t> (reply.readInt32());
    289     }
    290 
    291     virtual status_t unregisterEffect(int id)
    292     {
    293         Parcel data, reply;
    294         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
    295         data.writeInt32(id);
    296         remote()->transact(UNREGISTER_EFFECT, data, &reply);
    297         return static_cast <status_t> (reply.readInt32());
    298     }
    299 
    300 };
    301 
    302 IMPLEMENT_META_INTERFACE(AudioPolicyService, "android.media.IAudioPolicyService");
    303 
    304 // ----------------------------------------------------------------------
    305 
    306 
    307 status_t BnAudioPolicyService::onTransact(
    308     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
    309 {
    310     switch(code) {
    311         case SET_DEVICE_CONNECTION_STATE: {
    312             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    313             AudioSystem::audio_devices device =
    314                     static_cast <AudioSystem::audio_devices>(data.readInt32());
    315             AudioSystem::device_connection_state state =
    316                     static_cast <AudioSystem::device_connection_state>(data.readInt32());
    317             const char *device_address = data.readCString();
    318             reply->writeInt32(static_cast<uint32_t> (setDeviceConnectionState(device,
    319                                                                               state,
    320                                                                               device_address)));
    321             return NO_ERROR;
    322         } break;
    323 
    324         case GET_DEVICE_CONNECTION_STATE: {
    325             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    326             AudioSystem::audio_devices device =
    327                     static_cast<AudioSystem::audio_devices> (data.readInt32());
    328             const char *device_address = data.readCString();
    329             reply->writeInt32(static_cast<uint32_t> (getDeviceConnectionState(device,
    330                                                                               device_address)));
    331             return NO_ERROR;
    332         } break;
    333 
    334         case SET_PHONE_STATE: {
    335             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    336             reply->writeInt32(static_cast <uint32_t>(setPhoneState(data.readInt32())));
    337             return NO_ERROR;
    338         } break;
    339 
    340         case SET_RINGER_MODE: {
    341             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    342             uint32_t mode = data.readInt32();
    343             uint32_t mask = data.readInt32();
    344             reply->writeInt32(static_cast <uint32_t>(setRingerMode(mode, mask)));
    345             return NO_ERROR;
    346         } break;
    347 
    348         case SET_FORCE_USE: {
    349             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    350             AudioSystem::force_use usage = static_cast <AudioSystem::force_use>(data.readInt32());
    351             AudioSystem::forced_config config =
    352                     static_cast <AudioSystem::forced_config>(data.readInt32());
    353             reply->writeInt32(static_cast <uint32_t>(setForceUse(usage, config)));
    354             return NO_ERROR;
    355         } break;
    356 
    357         case GET_FORCE_USE: {
    358             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    359             AudioSystem::force_use usage = static_cast <AudioSystem::force_use>(data.readInt32());
    360             reply->writeInt32(static_cast <uint32_t>(getForceUse(usage)));
    361             return NO_ERROR;
    362         } break;
    363 
    364         case GET_OUTPUT: {
    365             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    366             AudioSystem::stream_type stream =
    367                     static_cast <AudioSystem::stream_type>(data.readInt32());
    368             uint32_t samplingRate = data.readInt32();
    369             uint32_t format = data.readInt32();
    370             uint32_t channels = data.readInt32();
    371             AudioSystem::output_flags flags =
    372                     static_cast <AudioSystem::output_flags>(data.readInt32());
    373 
    374             audio_io_handle_t output = getOutput(stream,
    375                                                  samplingRate,
    376                                                  format,
    377                                                  channels,
    378                                                  flags);
    379             reply->writeInt32(static_cast <int>(output));
    380             return NO_ERROR;
    381         } break;
    382 
    383         case START_OUTPUT: {
    384             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    385             audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
    386             uint32_t stream = data.readInt32();
    387             int session = data.readInt32();
    388             reply->writeInt32(static_cast <uint32_t>(startOutput(output,
    389                                                                  (AudioSystem::stream_type)stream,
    390                                                                  session)));
    391             return NO_ERROR;
    392         } break;
    393 
    394         case STOP_OUTPUT: {
    395             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    396             audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
    397             uint32_t stream = data.readInt32();
    398             int session = data.readInt32();
    399             reply->writeInt32(static_cast <uint32_t>(stopOutput(output,
    400                                                                 (AudioSystem::stream_type)stream,
    401                                                                 session)));
    402             return NO_ERROR;
    403         } break;
    404 
    405         case RELEASE_OUTPUT: {
    406             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    407             audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
    408             releaseOutput(output);
    409             return NO_ERROR;
    410         } break;
    411 
    412         case GET_INPUT: {
    413             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    414             int inputSource = data.readInt32();
    415             uint32_t samplingRate = data.readInt32();
    416             uint32_t format = data.readInt32();
    417             uint32_t channels = data.readInt32();
    418             AudioSystem::audio_in_acoustics acoustics =
    419                     static_cast <AudioSystem::audio_in_acoustics>(data.readInt32());
    420             audio_io_handle_t input = getInput(inputSource,
    421                                                samplingRate,
    422                                                format,
    423                                                channels,
    424                                                acoustics);
    425             reply->writeInt32(static_cast <int>(input));
    426             return NO_ERROR;
    427         } break;
    428 
    429         case START_INPUT: {
    430             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    431             audio_io_handle_t input = static_cast <audio_io_handle_t>(data.readInt32());
    432             reply->writeInt32(static_cast <uint32_t>(startInput(input)));
    433             return NO_ERROR;
    434         } break;
    435 
    436         case STOP_INPUT: {
    437             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    438             audio_io_handle_t input = static_cast <audio_io_handle_t>(data.readInt32());
    439             reply->writeInt32(static_cast <uint32_t>(stopInput(input)));
    440             return NO_ERROR;
    441         } break;
    442 
    443         case RELEASE_INPUT: {
    444             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    445             audio_io_handle_t input = static_cast <audio_io_handle_t>(data.readInt32());
    446             releaseInput(input);
    447             return NO_ERROR;
    448         } break;
    449 
    450         case INIT_STREAM_VOLUME: {
    451             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    452             AudioSystem::stream_type stream =
    453                     static_cast <AudioSystem::stream_type>(data.readInt32());
    454             int indexMin = data.readInt32();
    455             int indexMax = data.readInt32();
    456             reply->writeInt32(static_cast <uint32_t>(initStreamVolume(stream, indexMin,indexMax)));
    457             return NO_ERROR;
    458         } break;
    459 
    460         case SET_STREAM_VOLUME: {
    461             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    462             AudioSystem::stream_type stream =
    463                     static_cast <AudioSystem::stream_type>(data.readInt32());
    464             int index = data.readInt32();
    465             reply->writeInt32(static_cast <uint32_t>(setStreamVolumeIndex(stream, index)));
    466             return NO_ERROR;
    467         } break;
    468 
    469         case GET_STREAM_VOLUME: {
    470             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    471             AudioSystem::stream_type stream =
    472                     static_cast <AudioSystem::stream_type>(data.readInt32());
    473             int index;
    474             status_t status = getStreamVolumeIndex(stream, &index);
    475             reply->writeInt32(index);
    476             reply->writeInt32(static_cast <uint32_t>(status));
    477             return NO_ERROR;
    478         } break;
    479 
    480         case GET_STRATEGY_FOR_STREAM: {
    481             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    482             AudioSystem::stream_type stream =
    483                     static_cast <AudioSystem::stream_type>(data.readInt32());
    484             reply->writeInt32(getStrategyForStream(stream));
    485             return NO_ERROR;
    486         } break;
    487 
    488         case GET_OUTPUT_FOR_EFFECT: {
    489             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    490             effect_descriptor_t desc;
    491             data.read(&desc, sizeof(effect_descriptor_t));
    492             audio_io_handle_t output = getOutputForEffect(&desc);
    493             reply->writeInt32(static_cast <int>(output));
    494             return NO_ERROR;
    495         } break;
    496 
    497         case REGISTER_EFFECT: {
    498             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    499             effect_descriptor_t desc;
    500             data.read(&desc, sizeof(effect_descriptor_t));
    501             audio_io_handle_t output = data.readInt32();
    502             uint32_t strategy = data.readInt32();
    503             int session = data.readInt32();
    504             int id = data.readInt32();
    505             reply->writeInt32(static_cast <int32_t>(registerEffect(&desc,
    506                                                                    output,
    507                                                                    strategy,
    508                                                                    session,
    509                                                                    id)));
    510             return NO_ERROR;
    511         } break;
    512 
    513         case UNREGISTER_EFFECT: {
    514             CHECK_INTERFACE(IAudioPolicyService, data, reply);
    515             int id = data.readInt32();
    516             reply->writeInt32(static_cast <int32_t>(unregisterEffect(id)));
    517             return NO_ERROR;
    518         } break;
    519 
    520         default:
    521             return BBinder::onTransact(code, data, reply, flags);
    522     }
    523 }
    524 
    525 // ----------------------------------------------------------------------------
    526 
    527 }; // namespace android
    528