Home | History | Annotate | Download | only in libaudio
      1 /*
      2  * Copyright (C) 2014 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 "ATVAudioPolicyManager"
     18 //#define LOG_NDEBUG 0
     19 #include <media/AudioParameter.h>
     20 #include <media/mediarecorder.h>
     21 #include <utils/Log.h>
     22 #include <utils/String16.h>
     23 #include <utils/String8.h>
     24 #include <utils/StrongPointer.h>
     25 
     26 #include "AudioHardwareOutput.h"
     27 #include "ATVAudioPolicyManager.h"
     28 
     29 #ifdef REMOTE_CONTROL_INTERFACE
     30 #include <IRemoteControlService.h>
     31 #endif
     32 
     33 
     34 namespace android {
     35 extern AudioHardwareOutput gAudioHardwareOutput;
     36 
     37 // ----------------------------------------------------------------------------
     38 // Common audio policy manager code is implemented in AudioPolicyManager class
     39 // ----------------------------------------------------------------------------
     40 
     41 // ---  class factory
     42 
     43 
     44 extern "C" AudioPolicyInterface* createAudioPolicyManager(
     45         AudioPolicyClientInterface *clientInterface)
     46 {
     47     return new ATVAudioPolicyManager(clientInterface);
     48 }
     49 
     50 extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
     51 {
     52     delete interface;
     53 }
     54 
     55 ATVAudioPolicyManager::ATVAudioPolicyManager(
     56         AudioPolicyClientInterface *clientInterface)
     57     : AudioPolicyManager(clientInterface), mForceSubmixInputSelection(false)
     58 {
     59 }
     60 
     61 float ATVAudioPolicyManager::computeVolume(audio_stream_type_t stream,
     62                                            int index,
     63                                            audio_io_handle_t output,
     64                                            audio_devices_t device)
     65 {
     66     // We only use master volume, so all audio flinger streams
     67     // should be set to maximum
     68     (void)stream;
     69     (void)index;
     70     (void)output;
     71     (void)device;
     72     return 1.0;
     73 }
     74 
     75 status_t ATVAudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
     76                                                          audio_policy_dev_state_t state,
     77                                                          const char *device_address)
     78 {
     79     audio_devices_t tmp = AUDIO_DEVICE_NONE;;
     80     ALOGE("setDeviceConnectionState %08x %x %s", device, state,
     81           device_address ? device_address : "(null)");
     82 
     83     // If the input device is the remote submix and an address starting with "force=" was
     84     // specified, enable "force=1" / disable "force=0" the forced selection of the remote submix
     85     // input device over hardware input devices (e.g RemoteControl).
     86     if (device == AUDIO_DEVICE_IN_REMOTE_SUBMIX && device_address) {
     87         AudioParameter parameters = AudioParameter(String8(device_address));
     88         int forceValue;
     89         if (parameters.getInt(String8("force"), forceValue) == OK) {
     90             mForceSubmixInputSelection = forceValue != 0;
     91         }
     92     }
     93 
     94     if (audio_is_output_device(device)) {
     95       switch (state) {
     96           case AUDIO_POLICY_DEVICE_STATE_AVAILABLE:
     97               tmp = mAvailableOutputDevices.types() | device;
     98               break;
     99 
    100           case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE:
    101               tmp = mAvailableOutputDevices.types() & ~device;
    102               break;
    103           default:
    104               ALOGE("setDeviceConnectionState() invalid state: %x", state);
    105               return BAD_VALUE;
    106       }
    107 
    108       gAudioHardwareOutput.updateRouting(tmp);
    109       tmp = mAvailableOutputDevices.types();
    110     }
    111 
    112     status_t ret = 0;
    113     if (device != AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
    114       ret = AudioPolicyManager::setDeviceConnectionState(
    115                     device, state, device_address);
    116     }
    117 
    118     if (audio_is_output_device(device)) {
    119       if (tmp != mAvailableOutputDevices.types())
    120           gAudioHardwareOutput.updateRouting(mAvailableOutputDevices.types());
    121     }
    122 
    123     return ret;
    124 }
    125 
    126 audio_devices_t ATVAudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
    127 {
    128     uint32_t device = AUDIO_DEVICE_NONE;
    129     bool usePhysRemote = true;
    130 
    131     if (inputSource == AUDIO_SOURCE_VOICE_RECOGNITION) {
    132 #ifdef REMOTE_CONTROL_INTERFACE
    133       // Check if remote is actually connected or we should move on
    134       sp<IRemoteControlService> service = IRemoteControlService::getInstance();
    135       if (service == NULL) {
    136           ALOGV("getDeviceForInputSource No RemoteControl service detected, ignoring");
    137           usePhysRemote = false;
    138       } else if (!service->hasActiveRemote()) {
    139           ALOGV("getDeviceForInputSource No active connected device, passing onto submix");
    140           usePhysRemote = false;
    141       }
    142 #endif
    143       ALOGV("getDeviceForInputSource %s %s", usePhysRemote ? "use physical" : "",
    144           mForceSubmixInputSelection ? "use virtual" : "");
    145       audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() &
    146                                                 ~AUDIO_DEVICE_BIT_IN;
    147       if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET &&
    148             usePhysRemote) {
    149           // User a wired headset (physical remote) if available, connected and active
    150           ALOGV("Wired Headset available");
    151           device = AUDIO_DEVICE_IN_WIRED_HEADSET;
    152       } else if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX &&
    153             mForceSubmixInputSelection) {
    154           // REMOTE_SUBMIX should always be avaible, let's make sure it's being forced at the moment
    155           ALOGV("Virtual remote available");
    156           device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
    157       }
    158     }
    159 
    160     ALOGV("getDeviceForInputSource() input source %d, device %08x", inputSource, device);
    161     return device;
    162 }
    163 
    164 }  // namespace android
    165