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 <AudioGain.h>
20 #include <VolumeCurve.h>
21 #include <AudioPort.h>
22 #include <AudioPatch.h>
23 #include <DeviceDescriptor.h>
24 #include <IOProfile.h>
25 #include <HwModule.h>
26 #include <AudioInputDescriptor.h>
27 #include <AudioOutputDescriptor.h>
28 #include <AudioPolicyMix.h>
29 #include <EffectDescriptor.h>
30 #include <SoundTriggerSession.h>
31 #include <SessionRoute.h>
32 
33 namespace android {
34 
35 class AudioPolicyConfig
36 {
37 public:
38     AudioPolicyConfig(HwModuleCollection &hwModules,
39                       DeviceVector &availableOutputDevices,
40                       DeviceVector &availableInputDevices,
41                       sp<DeviceDescriptor> &defaultOutputDevices,
42                       bool &isSpeakerDrcEnabled,
43                       VolumeCurvesCollection *volumes = nullptr)
mHwModules(hwModules)44         : mHwModules(hwModules),
45           mAvailableOutputDevices(availableOutputDevices),
46           mAvailableInputDevices(availableInputDevices),
47           mDefaultOutputDevices(defaultOutputDevices),
48           mVolumeCurves(volumes),
49           mIsSpeakerDrcEnabled(isSpeakerDrcEnabled)
50     {}
51 
setVolumes(const VolumeCurvesCollection & volumes)52     void setVolumes(const VolumeCurvesCollection &volumes)
53     {
54         if (mVolumeCurves != nullptr) {
55             *mVolumeCurves = volumes;
56         }
57     }
58 
setHwModules(const HwModuleCollection & hwModules)59     void setHwModules(const HwModuleCollection &hwModules)
60     {
61         mHwModules = hwModules;
62     }
63 
addAvailableDevice(const sp<DeviceDescriptor> & availableDevice)64     void addAvailableDevice(const sp<DeviceDescriptor> &availableDevice)
65     {
66         if (audio_is_output_device(availableDevice->type())) {
67             mAvailableOutputDevices.add(availableDevice);
68         } else if (audio_is_input_device(availableDevice->type())) {
69             mAvailableInputDevices.add(availableDevice);
70         }
71     }
72 
addAvailableInputDevices(const DeviceVector & availableInputDevices)73     void addAvailableInputDevices(const DeviceVector &availableInputDevices)
74     {
75         mAvailableInputDevices.add(availableInputDevices);
76     }
77 
addAvailableOutputDevices(const DeviceVector & availableOutputDevices)78     void addAvailableOutputDevices(const DeviceVector &availableOutputDevices)
79     {
80         mAvailableOutputDevices.add(availableOutputDevices);
81     }
82 
setSpeakerDrcEnabled(bool isSpeakerDrcEnabled)83     void setSpeakerDrcEnabled(bool isSpeakerDrcEnabled)
84     {
85         mIsSpeakerDrcEnabled = isSpeakerDrcEnabled;
86     }
87 
getHwModules()88     const HwModuleCollection getHwModules() const { return mHwModules; }
89 
getAvailableInputDevices()90     const DeviceVector &getAvailableInputDevices() const
91     {
92         return mAvailableInputDevices;
93     }
94 
getAvailableOutputDevices()95     const DeviceVector &getAvailableOutputDevices() const
96     {
97         return mAvailableOutputDevices;
98     }
99 
setDefaultOutputDevice(const sp<DeviceDescriptor> & defaultDevice)100     void setDefaultOutputDevice(const sp<DeviceDescriptor> &defaultDevice)
101     {
102         mDefaultOutputDevices = defaultDevice;
103     }
104 
getDefaultOutputDevice()105     const sp<DeviceDescriptor> &getDefaultOutputDevice() const { return mDefaultOutputDevices; }
106 
setDefault(void)107     void setDefault(void)
108     {
109         mDefaultOutputDevices = new DeviceDescriptor(AUDIO_DEVICE_OUT_SPEAKER);
110         sp<HwModule> module;
111         sp<DeviceDescriptor> defaultInputDevice = new DeviceDescriptor(AUDIO_DEVICE_IN_BUILTIN_MIC);
112         mAvailableOutputDevices.add(mDefaultOutputDevices);
113         mAvailableInputDevices.add(defaultInputDevice);
114 
115         module = new HwModule("primary");
116 
117         sp<OutputProfile> outProfile;
118         outProfile = new OutputProfile(String8("primary"));
119         outProfile->attach(module);
120         outProfile->addAudioProfile(
121                 new AudioProfile(AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 44100));
122         outProfile->addSupportedDevice(mDefaultOutputDevices);
123         outProfile->setFlags(AUDIO_OUTPUT_FLAG_PRIMARY);
124         module->mOutputProfiles.add(outProfile);
125 
126         sp<InputProfile> inProfile;
127         inProfile = new InputProfile(String8("primary"));
128         inProfile->attach(module);
129         inProfile->addAudioProfile(
130                 new AudioProfile(AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_MONO, 8000));
131         inProfile->addSupportedDevice(defaultInputDevice);
132         module->mInputProfiles.add(inProfile);
133 
134         mHwModules.add(module);
135     }
136 
137 private:
138     HwModuleCollection &mHwModules; /**< Collection of Module, with Profiles, i.e. Mix Ports. */
139     DeviceVector &mAvailableOutputDevices;
140     DeviceVector &mAvailableInputDevices;
141     sp<DeviceDescriptor> &mDefaultOutputDevices;
142     VolumeCurvesCollection *mVolumeCurves;
143     bool &mIsSpeakerDrcEnabled;
144 };
145 
146 }; // namespace android
147