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 <RoutingStrategy.h>
21 #include <utils/Errors.h>
22 #include <utils/Timers.h>
23 #include <utils/KeyedVector.h>
24 #include <system/audio.h>
25 
26 namespace android {
27 
28 class IOProfile;
29 class AudioMix;
30 class AudioPolicyClientInterface;
31 
32 // descriptor for audio outputs. Used to maintain current configuration of each opened audio output
33 // and keep track of the usage of this output by each audio stream type.
34 class AudioOutputDescriptor: public AudioPortConfig
35 {
36 public:
37     AudioOutputDescriptor(const sp<AudioPort>& port,
38                           AudioPolicyClientInterface *clientInterface);
~AudioOutputDescriptor()39     virtual ~AudioOutputDescriptor() {}
40 
41     status_t    dump(int fd);
42     void        log(const char* indent);
43 
44     audio_port_handle_t getId() const;
45     virtual audio_devices_t device() const;
46     virtual bool sharesHwModuleWith(const sp<AudioOutputDescriptor> outputDesc);
47     virtual audio_devices_t supportedDevices();
isDuplicated()48     virtual bool isDuplicated() const { return false; }
latency()49     virtual uint32_t latency() { return 0; }
50     virtual bool isFixedVolume(audio_devices_t device);
subOutput1()51     virtual sp<AudioOutputDescriptor> subOutput1() { return 0; }
subOutput2()52     virtual sp<AudioOutputDescriptor> subOutput2() { return 0; }
53     virtual bool setVolume(float volume,
54                            audio_stream_type_t stream,
55                            audio_devices_t device,
56                            uint32_t delayMs,
57                            bool force);
58     virtual void changeRefCount(audio_stream_type_t stream, int delta);
59 
60     bool isActive(uint32_t inPastMs = 0) const;
61     bool isStreamActive(audio_stream_type_t stream,
62                         uint32_t inPastMs = 0,
63                         nsecs_t sysTime = 0) const;
64 
65     virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
66                            const struct audio_port_config *srcConfig = NULL) const;
getAudioPort()67     virtual sp<AudioPort> getAudioPort() const { return mPort; }
68     virtual void toAudioPort(struct audio_port *port) const;
69 
70     audio_module_handle_t getModuleHandle() const;
71 
72     sp<AudioPort>       mPort;
73     audio_devices_t mDevice;                   // current device this output is routed to
74     audio_patch_handle_t mPatchHandle;
75     uint32_t mRefCount[AUDIO_STREAM_CNT]; // number of streams of each type using this output
76     nsecs_t mStopTime[AUDIO_STREAM_CNT];
77     float mCurVolume[AUDIO_STREAM_CNT];   // current stream volume in dB
78     int mMuteCount[AUDIO_STREAM_CNT];     // mute request counter
79     bool mStrategyMutedByDevice[NUM_STRATEGIES]; // strategies muted because of incompatible
80                                         // device selection. See checkDeviceMuteStrategies()
81     AudioPolicyClientInterface *mClientInterface;
82 
83 protected:
84     audio_port_handle_t mId;
85 };
86 
87 // Audio output driven by a software mixer in audio flinger.
88 class SwAudioOutputDescriptor: public AudioOutputDescriptor
89 {
90 public:
91     SwAudioOutputDescriptor(const sp<IOProfile>& profile,
92                             AudioPolicyClientInterface *clientInterface);
~SwAudioOutputDescriptor()93     virtual ~SwAudioOutputDescriptor() {}
94 
95     status_t    dump(int fd);
96 
97     void setIoHandle(audio_io_handle_t ioHandle);
98 
99     virtual audio_devices_t device() const;
100     virtual bool sharesHwModuleWith(const sp<AudioOutputDescriptor> outputDesc);
101     virtual audio_devices_t supportedDevices();
102     virtual uint32_t latency();
isDuplicated()103     virtual bool isDuplicated() const { return (mOutput1 != NULL && mOutput2 != NULL); }
104     virtual bool isFixedVolume(audio_devices_t device);
subOutput1()105     virtual sp<AudioOutputDescriptor> subOutput1() { return mOutput1; }
subOutput2()106     virtual sp<AudioOutputDescriptor> subOutput2() { return mOutput2; }
107     virtual void changeRefCount(audio_stream_type_t stream, int delta);
108     virtual bool setVolume(float volume,
109                            audio_stream_type_t stream,
110                            audio_devices_t device,
111                            uint32_t delayMs,
112                            bool force);
113 
114     virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
115                            const struct audio_port_config *srcConfig = NULL) const;
116     virtual void toAudioPort(struct audio_port *port) const;
117 
118     const sp<IOProfile> mProfile;          // I/O profile this output derives from
119     audio_io_handle_t mIoHandle;           // output handle
120     uint32_t mLatency;                  //
121     audio_output_flags_t mFlags;   //
122     AudioMix *mPolicyMix;             // non NULL when used by a dynamic policy
123     sp<SwAudioOutputDescriptor> mOutput1;    // used by duplicated outputs: first output
124     sp<SwAudioOutputDescriptor> mOutput2;    // used by duplicated outputs: second output
125     uint32_t mDirectOpenCount; // number of clients using this output (direct outputs only)
126     uint32_t mGlobalRefCount;  // non-stream-specific ref count
127 };
128 
129 class SwAudioOutputCollection :
130         public DefaultKeyedVector< audio_io_handle_t, sp<SwAudioOutputDescriptor> >
131 {
132 public:
133     bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
134 
135     /**
136      * return whether a stream is playing remotely, override to change the definition of
137      * local/remote playback, used for instance by notification manager to not make
138      * media players lose audio focus when not playing locally
139      * For the base implementation, "remotely" means playing during screen mirroring which
140      * uses an output for playback with a non-empty, non "0" address.
141      */
142     bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
143 
144     /**
145      * returns the A2DP output handle if it is open or 0 otherwise
146      */
147     audio_io_handle_t getA2dpOutput() const;
148 
149     sp<SwAudioOutputDescriptor> getOutputFromId(audio_port_handle_t id) const;
150 
151     sp<SwAudioOutputDescriptor> getPrimaryOutput() const;
152 
153     /**
154      * return true if any output is playing anything besides the stream to ignore
155      */
156     bool isAnyOutputActive(audio_stream_type_t streamToIgnore) const;
157 
158     audio_devices_t getSupportedDevices(audio_io_handle_t handle) const;
159 
160     status_t dump(int fd) const;
161 };
162 
163 }; // namespace android
164