• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, int spaces, const char* extraInfo) 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_v7 *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;
isMmap()95     bool isMmap() override {
96         if (const auto policyPort = getPolicyAudioPort(); policyPort != nullptr) {
97             if (const auto port = policyPort->asAudioPort(); port != nullptr) {
98                 return port->isMmap();
99             }
100         }
101         return false;
102     }
103 
104     status_t open(const audio_config_t *config,
105                   const sp<DeviceDescriptor> &device,
106                   audio_source_t source,
107                   audio_input_flags_t flags,
108                   audio_io_handle_t *input);
109     // Called when a stream is about to be started.
110     // Note: called after setClientActive(client, true)
111     status_t start();
112     // Called after a stream is stopped
113     // Note: called after setClientActive(client, false)
114     void stop();
115     void close();
116 
117     RecordClientVector getClientsForSession(audio_session_t session);
118     RecordClientVector clientsList(bool activeOnly = false,
119         audio_source_t source = AUDIO_SOURCE_DEFAULT, bool preferredDeviceOnly = false) const;
120 
121     void setAppState(audio_port_handle_t portId, app_state_t state);
122 
123     // implementation of ClientMapHandler<RecordClientDescriptor>
124     void addClient(const sp<RecordClientDescriptor> &client) override;
125 
126     // Go over all active clients and suspend or restore effects according highest priority
127     // active use case
128     void checkSuspendEffects();
129 
130  private:
131 
132     void updateClientRecordingConfiguration(int event, const sp<RecordClientDescriptor>& client);
133 
134     audio_patch_handle_t mPatchHandle = AUDIO_PATCH_HANDLE_NONE;
135     sp<DeviceDescriptor> mDevice = nullptr; /**< current device this input is routed to */
136 
137     // Because a preemptible capture session can preempt another one, we end up in an endless loop
138     // situation were each session is allowed to restart after being preempted,
139     // thus preempting the other one which restarts and so on.
140     // To avoid this situation, we store which audio session was preempted when
141     // a particular input started and prevent preemption of this active input by this session.
142     // We also inherit sessions from the preempted input to avoid a 3 way preemption loop etc...
143     SortedVector<audio_session_t> mPreemptedSessions;
144     AudioPolicyClientInterface * const mClientInterface;
145     int32_t mGlobalActiveCount = 0;  // non-client-specific activity ref count
146     EffectDescriptorCollection mEnabledEffects;
147     audio_input_flags_t& mFlags = AudioPortConfig::mFlags.input;
148 };
149 
150 class AudioInputCollection :
151         public DefaultKeyedVector< audio_io_handle_t, sp<AudioInputDescriptor> >
152 {
153 public:
154     bool isSourceActive(audio_source_t source) const;
155 
156     sp<AudioInputDescriptor> getInputFromId(audio_port_handle_t id) const;
157 
158     // count active capture sessions using one of the specified devices.
159     // ignore devices if empty vector is passed
160     uint32_t activeInputsCountOnDevices(const DeviceVector &devices) const;
161 
162     /**
163      * return io handle of active input or 0 if no input is active
164      * Only considers inputs from physical devices (e.g. main mic, headset mic) when
165      * ignoreVirtualInputs is true.
166      */
167     Vector<sp <AudioInputDescriptor> > getActiveInputs();
168 
169     sp<AudioInputDescriptor> getInputForClient(audio_port_handle_t portId);
170 
171     void trackEffectEnabled(const sp<EffectDescriptor> &effect, bool enabled);
172 
173     /**
174     * @brief clearSessionRoutesForDevice: when a device is disconnected, and if this device has
175     * been chosen as the preferred device by any client, the policy manager shall
176     * prevent from using this device any more by clearing all the session routes involving this
177     * device.
178     * In other words, the preferred device port id of these clients will be resetted to NONE.
179     * @param disconnectedDevice device to be disconnected
180     */
181     void clearSessionRoutesForDevice(const sp<DeviceDescriptor> &disconnectedDevice);
182 
183     void dump(String8 *dst) const;
184 };
185 
186 
187 } // namespace android
188