Home | History | Annotate | Download | only in include
      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 #pragma once
     18 
     19 #include <unordered_map>
     20 #include <unordered_set>
     21 
     22 #include <AudioGain.h>
     23 #include <AudioPort.h>
     24 #include <AudioPatch.h>
     25 #include <DeviceDescriptor.h>
     26 #include <IOProfile.h>
     27 #include <HwModule.h>
     28 #include <AudioInputDescriptor.h>
     29 #include <AudioOutputDescriptor.h>
     30 #include <AudioPolicyMix.h>
     31 #include <EffectDescriptor.h>
     32 #include <SoundTriggerSession.h>
     33 
     34 namespace android {
     35 
     36 class AudioPolicyConfig
     37 {
     38 public:
     39     AudioPolicyConfig(HwModuleCollection &hwModules,
     40                       DeviceVector &availableOutputDevices,
     41                       DeviceVector &availableInputDevices,
     42                       sp<DeviceDescriptor> &defaultOutputDevice)
     43         : mHwModules(hwModules),
     44           mAvailableOutputDevices(availableOutputDevices),
     45           mAvailableInputDevices(availableInputDevices),
     46           mDefaultOutputDevice(defaultOutputDevice),
     47           mIsSpeakerDrcEnabled(false)
     48     {}
     49 
     50     const std::string& getSource() const {
     51         return mSource;
     52     }
     53 
     54     void setSource(const std::string& file) {
     55         mSource = file;
     56     }
     57 
     58     void setHwModules(const HwModuleCollection &hwModules)
     59     {
     60         mHwModules = hwModules;
     61     }
     62 
     63     void addAvailableDevice(const sp<DeviceDescriptor> &availableDevice)
     64     {
     65         if (audio_is_output_device(availableDevice->type())) {
     66             mAvailableOutputDevices.add(availableDevice);
     67         } else if (audio_is_input_device(availableDevice->type())) {
     68             mAvailableInputDevices.add(availableDevice);
     69         }
     70     }
     71 
     72     void addAvailableInputDevices(const DeviceVector &availableInputDevices)
     73     {
     74         mAvailableInputDevices.add(availableInputDevices);
     75     }
     76 
     77     void addAvailableOutputDevices(const DeviceVector &availableOutputDevices)
     78     {
     79         mAvailableOutputDevices.add(availableOutputDevices);
     80     }
     81 
     82     bool isSpeakerDrcEnabled() const { return mIsSpeakerDrcEnabled; }
     83 
     84     void setSpeakerDrcEnabled(bool isSpeakerDrcEnabled)
     85     {
     86         mIsSpeakerDrcEnabled = isSpeakerDrcEnabled;
     87     }
     88 
     89     const HwModuleCollection getHwModules() const { return mHwModules; }
     90 
     91     const DeviceVector &getAvailableInputDevices() const
     92     {
     93         return mAvailableInputDevices;
     94     }
     95 
     96     const DeviceVector &getAvailableOutputDevices() const
     97     {
     98         return mAvailableOutputDevices;
     99     }
    100 
    101     void setDefaultOutputDevice(const sp<DeviceDescriptor> &defaultDevice)
    102     {
    103         mDefaultOutputDevice = defaultDevice;
    104     }
    105 
    106     const sp<DeviceDescriptor> &getDefaultOutputDevice() const { return mDefaultOutputDevice; }
    107 
    108     void setDefault(void)
    109     {
    110         mSource = "AudioPolicyConfig::setDefault";
    111         mDefaultOutputDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_SPEAKER);
    112         mDefaultOutputDevice->addAudioProfile(AudioProfile::createFullDynamic());
    113         sp<DeviceDescriptor> defaultInputDevice = new DeviceDescriptor(AUDIO_DEVICE_IN_BUILTIN_MIC);
    114         defaultInputDevice->addAudioProfile(AudioProfile::createFullDynamic());
    115         sp<AudioProfile> micProfile = new AudioProfile(
    116                 AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_MONO, 8000);
    117         defaultInputDevice->addAudioProfile(micProfile);
    118         mAvailableOutputDevices.add(mDefaultOutputDevice);
    119         mAvailableInputDevices.add(defaultInputDevice);
    120 
    121         sp<HwModule> module = new HwModule(AUDIO_HARDWARE_MODULE_ID_PRIMARY, 2 /*halVersionMajor*/);
    122         mHwModules.add(module);
    123         mDefaultOutputDevice->attach(module);
    124         defaultInputDevice->attach(module);
    125 
    126         sp<OutputProfile> outProfile = new OutputProfile(String8("primary"));
    127         outProfile->addAudioProfile(
    128                 new AudioProfile(AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 44100));
    129         outProfile->addSupportedDevice(mDefaultOutputDevice);
    130         outProfile->setFlags(AUDIO_OUTPUT_FLAG_PRIMARY);
    131         module->addOutputProfile(outProfile);
    132 
    133         sp<InputProfile> inProfile = new InputProfile(String8("primary"));
    134         inProfile->addAudioProfile(micProfile);
    135         inProfile->addSupportedDevice(defaultInputDevice);
    136         module->addInputProfile(inProfile);
    137 
    138         setDefaultSurroundFormats();
    139     }
    140 
    141     // Surround formats, with an optional list of subformats that are equivalent from users' POV.
    142     using SurroundFormats = std::unordered_map<audio_format_t, std::unordered_set<audio_format_t>>;
    143 
    144     const SurroundFormats &getSurroundFormats() const
    145     {
    146         return mSurroundFormats;
    147     }
    148 
    149     void setSurroundFormats(const SurroundFormats &surroundFormats)
    150     {
    151         mSurroundFormats = surroundFormats;
    152     }
    153 
    154     void setDefaultSurroundFormats()
    155     {
    156         mSurroundFormats = {
    157             {AUDIO_FORMAT_AC3, {}},
    158             {AUDIO_FORMAT_E_AC3, {}},
    159             {AUDIO_FORMAT_DTS, {}},
    160             {AUDIO_FORMAT_DTS_HD, {}},
    161             {AUDIO_FORMAT_AAC_LC, {
    162                     AUDIO_FORMAT_AAC_HE_V1, AUDIO_FORMAT_AAC_HE_V2, AUDIO_FORMAT_AAC_ELD,
    163                     AUDIO_FORMAT_AAC_XHE}},
    164             {AUDIO_FORMAT_DOLBY_TRUEHD, {}},
    165             {AUDIO_FORMAT_E_AC3_JOC, {}},
    166             {AUDIO_FORMAT_AC4, {}}};
    167     }
    168 
    169 private:
    170     std::string mSource;
    171     HwModuleCollection &mHwModules; /**< Collection of Module, with Profiles, i.e. Mix Ports. */
    172     DeviceVector &mAvailableOutputDevices;
    173     DeviceVector &mAvailableInputDevices;
    174     sp<DeviceDescriptor> &mDefaultOutputDevice;
    175     // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
    176     // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
    177     // Note: remove also speaker_drc_enabled from global configuration of XML config file.
    178     bool mIsSpeakerDrcEnabled;
    179     SurroundFormats mSurroundFormats;
    180 };
    181 
    182 } // namespace android
    183