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 "AudioPort.h"
20#include "AudioSession.h"
21#include "AudioSessionInfoProvider.h"
22#include <utils/Errors.h>
23#include <system/audio.h>
24#include <utils/SortedVector.h>
25#include <utils/KeyedVector.h>
26
27namespace android {
28
29class IOProfile;
30class AudioMix;
31
32// descriptor for audio inputs. Used to maintain current configuration of each opened audio input
33// and keep track of the usage of this input.
34class AudioInputDescriptor: public AudioPortConfig, public AudioSessionInfoProvider
35{
36public:
37    explicit AudioInputDescriptor(const sp<IOProfile>& profile,
38                                  AudioPolicyClientInterface *clientInterface);
39    audio_port_handle_t getId() const;
40    audio_module_handle_t getModuleHandle() const;
41    uint32_t getOpenRefCount() const;
42
43    status_t    dump(int fd);
44
45    audio_io_handle_t             mIoHandle;       // input handle
46    audio_devices_t               mDevice;         // current device this input is routed to
47    AudioMix                      *mPolicyMix;     // non NULL when used by a dynamic policy
48    const sp<IOProfile>           mProfile;        // I/O profile this output derives from
49
50    virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
51            const struct audio_port_config *srcConfig = NULL) const;
52    virtual sp<AudioPort> getAudioPort() const { return mProfile; }
53    void toAudioPort(struct audio_port *port) const;
54    void setPreemptedSessions(const SortedVector<audio_session_t>& sessions);
55    SortedVector<audio_session_t> getPreemptedSessions() const;
56    bool hasPreemptedSession(audio_session_t session) const;
57    void clearPreemptedSessions();
58    bool isActive() const;
59    bool isSourceActive(audio_source_t source) const;
60    audio_source_t inputSource(bool activeOnly = false) const;
61    bool isSoundTrigger() const;
62    status_t addAudioSession(audio_session_t session,
63                             const sp<AudioSession>& audioSession);
64    status_t removeAudioSession(audio_session_t session);
65    sp<AudioSession> getAudioSession(audio_session_t session) const;
66    AudioSessionCollection getAudioSessions(bool activeOnly) const;
67    size_t getAudioSessionCount(bool activeOnly) const;
68    audio_source_t getHighestPrioritySource(bool activeOnly) const;
69
70    // implementation of AudioSessionInfoProvider
71    virtual audio_config_base_t getConfig() const;
72    virtual audio_patch_handle_t getPatchHandle() const;
73
74    void setPatchHandle(audio_patch_handle_t handle);
75
76    status_t open(const audio_config_t *config,
77                  audio_devices_t device,
78                  const String8& address,
79                  audio_source_t source,
80                  audio_input_flags_t flags,
81                  audio_io_handle_t *input);
82    // Called when a stream is about to be started.
83    // Note: called after AudioSession::changeActiveCount(1)
84    status_t start();
85    // Called after a stream is stopped
86    // Note: called after AudioSession::changeActiveCount(-1)
87    void stop();
88    void close();
89
90private:
91    audio_patch_handle_t          mPatchHandle;
92    audio_port_handle_t           mId;
93    // audio sessions attached to this input
94    AudioSessionCollection        mSessions;
95    // Because a preemptible capture session can preempt another one, we end up in an endless loop
96    // situation were each session is allowed to restart after being preempted,
97    // thus preempting the other one which restarts and so on.
98    // To avoid this situation, we store which audio session was preempted when
99    // a particular input started and prevent preemption of this active input by this session.
100    // We also inherit sessions from the preempted input to avoid a 3 way preemption loop etc...
101    SortedVector<audio_session_t> mPreemptedSessions;
102    AudioPolicyClientInterface *mClientInterface;
103};
104
105class AudioInputCollection :
106        public DefaultKeyedVector< audio_io_handle_t, sp<AudioInputDescriptor> >
107{
108public:
109    bool isSourceActive(audio_source_t source) const;
110
111    sp<AudioInputDescriptor> getInputFromId(audio_port_handle_t id) const;
112
113    // count active capture sessions using one of the specified devices.
114    // ignore devices if AUDIO_DEVICE_IN_DEFAULT is passed
115    uint32_t activeInputsCountOnDevices(audio_devices_t devices = AUDIO_DEVICE_IN_DEFAULT) const;
116
117    /**
118     * return io handle of active input or 0 if no input is active
119     * Only considers inputs from physical devices (e.g. main mic, headset mic) when
120     * ignoreVirtualInputs is true.
121     */
122    Vector<sp <AudioInputDescriptor> > getActiveInputs(bool ignoreVirtualInputs = true);
123
124    audio_devices_t getSupportedDevices(audio_io_handle_t handle) const;
125
126    status_t dump(int fd) const;
127};
128
129
130} // namespace android
131