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