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