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