1 /* 2 * Copyright (C) 2015 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 <system/audio.h> 20 #include <utils/Errors.h> 21 #include <utils/SortedVector.h> 22 #include <utils/KeyedVector.h> 23 #include "AudioIODescriptorInterface.h" 24 #include "ClientDescriptor.h" 25 #include "DeviceDescriptor.h" 26 #include "EffectDescriptor.h" 27 #include "IOProfile.h" 28 #include "PolicyAudioPort.h" 29 30 namespace android { 31 32 class AudioPolicyMix; 33 class AudioPolicyClientInterface; 34 35 // descriptor for audio inputs. Used to maintain current configuration of each opened audio input 36 // and keep track of the usage of this input. 37 class AudioInputDescriptor: public AudioPortConfig, 38 public PolicyAudioPortConfig, 39 public AudioIODescriptorInterface, 40 public ClientMapHandler<RecordClientDescriptor> 41 { 42 public: 43 AudioInputDescriptor(const sp<IOProfile>& profile, 44 AudioPolicyClientInterface *clientInterface); 45 46 virtual ~AudioInputDescriptor() = default; 47 48 audio_module_handle_t getModuleHandle() const; 49 getDeviceType()50 audio_devices_t getDeviceType() const { return (mDevice != nullptr) ? 51 mDevice->type() : AUDIO_DEVICE_NONE; } getDevice()52 sp<DeviceDescriptor> getDevice() const { return mDevice; } setDevice(const sp<DeviceDescriptor> & device)53 void setDevice(const sp<DeviceDescriptor> &device) { mDevice = device; } supportedDevices()54 DeviceVector supportedDevices() const { 55 return mProfile != nullptr ? mProfile->getSupportedDevices() : DeviceVector(); } 56 57 void dump(String8 *dst) const override; 58 59 audio_io_handle_t mIoHandle = AUDIO_IO_HANDLE_NONE; // input handle 60 wp<AudioPolicyMix> mPolicyMix; // non NULL when used by a dynamic policy 61 const sp<IOProfile> mProfile; // I/O profile this output derives from 62 63 // PolicyAudioPortConfig getPolicyAudioPort()64 virtual sp<PolicyAudioPort> getPolicyAudioPort() const { 65 return mProfile; 66 } 67 68 // AudioPortConfig 69 virtual status_t applyAudioPortConfig(const struct audio_port_config *config, 70 struct audio_port_config *backupConfig = NULL); 71 virtual void toAudioPortConfig(struct audio_port_config *dstConfig, 72 const struct audio_port_config *srcConfig = NULL) const; getAudioPort()73 virtual sp<AudioPort> getAudioPort() const { return mProfile; } 74 75 void toAudioPort(struct audio_port *port) const; 76 void setPreemptedSessions(const SortedVector<audio_session_t>& sessions); 77 SortedVector<audio_session_t> getPreemptedSessions() const; 78 bool hasPreemptedSession(audio_session_t session) const; 79 void clearPreemptedSessions(); isActive()80 bool isActive() const { return mGlobalActiveCount > 0; } 81 bool isSourceActive(audio_source_t source) const; 82 audio_source_t source() const; 83 bool isSoundTrigger() const; 84 sp<RecordClientDescriptor> getHighestPriorityClient() const; 85 audio_attributes_t getHighestPriorityAttributes() const; 86 void setClientActive(const sp<RecordClientDescriptor>& client, bool active); activeCount()87 int32_t activeCount() { return mGlobalActiveCount; } 88 void trackEffectEnabled(const sp<EffectDescriptor> &effect, bool enabled); 89 EffectDescriptorCollection getEnabledEffects() const; 90 EffectDescriptorCollection getActiveEffects() const; // enabled and not suspended 91 // implementation of AudioIODescriptorInterface 92 audio_config_base_t getConfig() const override; 93 audio_patch_handle_t getPatchHandle() const override; 94 void setPatchHandle(audio_patch_handle_t handle) override; 95 96 status_t open(const audio_config_t *config, 97 const sp<DeviceDescriptor> &device, 98 audio_source_t source, 99 audio_input_flags_t flags, 100 audio_io_handle_t *input); 101 // Called when a stream is about to be started. 102 // Note: called after setClientActive(client, true) 103 status_t start(); 104 // Called after a stream is stopped 105 // Note: called after setClientActive(client, false) 106 void stop(); 107 void close(); 108 109 RecordClientVector getClientsForSession(audio_session_t session); 110 RecordClientVector clientsList(bool activeOnly = false, 111 audio_source_t source = AUDIO_SOURCE_DEFAULT, bool preferredDeviceOnly = false) const; 112 113 void setAppState(uid_t uid, app_state_t state); 114 115 // implementation of ClientMapHandler<RecordClientDescriptor> 116 void addClient(const sp<RecordClientDescriptor> &client) override; 117 118 // Go over all active clients and suspend or restore effects according highest priority 119 // active use case 120 void checkSuspendEffects(); 121 122 private: 123 124 void updateClientRecordingConfiguration(int event, const sp<RecordClientDescriptor>& client); 125 126 audio_patch_handle_t mPatchHandle = AUDIO_PATCH_HANDLE_NONE; 127 sp<DeviceDescriptor> mDevice = nullptr; /**< current device this input is routed to */ 128 129 // Because a preemptible capture session can preempt another one, we end up in an endless loop 130 // situation were each session is allowed to restart after being preempted, 131 // thus preempting the other one which restarts and so on. 132 // To avoid this situation, we store which audio session was preempted when 133 // a particular input started and prevent preemption of this active input by this session. 134 // We also inherit sessions from the preempted input to avoid a 3 way preemption loop etc... 135 SortedVector<audio_session_t> mPreemptedSessions; 136 AudioPolicyClientInterface * const mClientInterface; 137 int32_t mGlobalActiveCount = 0; // non-client-specific activity ref count 138 EffectDescriptorCollection mEnabledEffects; 139 }; 140 141 class AudioInputCollection : 142 public DefaultKeyedVector< audio_io_handle_t, sp<AudioInputDescriptor> > 143 { 144 public: 145 bool isSourceActive(audio_source_t source) const; 146 147 sp<AudioInputDescriptor> getInputFromId(audio_port_handle_t id) const; 148 149 // count active capture sessions using one of the specified devices. 150 // ignore devices if empty vector is passed 151 uint32_t activeInputsCountOnDevices(const DeviceVector &devices) const; 152 153 /** 154 * return io handle of active input or 0 if no input is active 155 * Only considers inputs from physical devices (e.g. main mic, headset mic) when 156 * ignoreVirtualInputs is true. 157 */ 158 Vector<sp <AudioInputDescriptor> > getActiveInputs(); 159 160 sp<AudioInputDescriptor> getInputForClient(audio_port_handle_t portId); 161 162 void trackEffectEnabled(const sp<EffectDescriptor> &effect, bool enabled); 163 164 /** 165 * @brief clearSessionRoutesForDevice: when a device is disconnected, and if this device has 166 * been chosen as the preferred device by any client, the policy manager shall 167 * prevent from using this device any more by clearing all the session routes involving this 168 * device. 169 * In other words, the preferred device port id of these clients will be resetted to NONE. 170 * @param disconnectedDevice device to be disconnected 171 */ 172 void clearSessionRoutesForDevice(const sp<DeviceDescriptor> &disconnectedDevice); 173 174 void dump(String8 *dst) const; 175 }; 176 177 178 } // namespace android 179