AudioPort.h revision 84332aaa807037baca05340875f2d94fcc519ac4
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 "AudioCollections.h"
20#include "AudioProfile.h"
21#include <utils/String8.h>
22#include <utils/Vector.h>
23#include <utils/RefBase.h>
24#include <utils/Errors.h>
25#include <system/audio.h>
26#include <cutils/config_utils.h>
27
28namespace android {
29
30class HwModule;
31class AudioGain;
32class AudioRoute;
33typedef Vector<sp<AudioGain> > AudioGainCollection;
34
35class AudioPort : public virtual RefBase
36{
37public:
38    AudioPort(const String8& name, audio_port_type_t type,  audio_port_role_t role) :
39        mName(name), mType(type), mRole(role), mFlags(AUDIO_OUTPUT_FLAG_NONE) {}
40
41    virtual ~AudioPort() {}
42
43    void setName(const String8 &name) { mName = name; }
44    const String8 &getName() const { return mName; }
45
46    audio_port_type_t getType() const { return mType; }
47    audio_port_role_t getRole() const { return mRole; }
48
49    virtual const String8 getTagName() const = 0;
50
51    void setGains(const AudioGainCollection &gains) { mGains = gains; }
52    const AudioGainCollection &getGains() const { return mGains; }
53
54    void setFlags(uint32_t flags)
55    {
56        //force direct flag if offload flag is set: offloading implies a direct output stream
57        // and all common behaviors are driven by checking only the direct flag
58        // this should normally be set appropriately in the policy configuration file
59        if (mRole == AUDIO_PORT_ROLE_SOURCE && (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
60            flags |= AUDIO_OUTPUT_FLAG_DIRECT;
61        }
62        mFlags = flags;
63    }
64    uint32_t getFlags() const { return mFlags; }
65
66    virtual void attach(const sp<HwModule>& module);
67    bool isAttached() { return mModule != 0; }
68
69    static audio_port_handle_t getNextUniqueId();
70
71    virtual void toAudioPort(struct audio_port *port) const;
72
73    virtual void importAudioPort(const sp<AudioPort> port);
74
75    void addAudioProfile(const sp<AudioProfile> &profile) { mProfiles.add(profile); }
76
77    void setAudioProfiles(const AudioProfileVector &profiles) { mProfiles = profiles; }
78    AudioProfileVector &getAudioProfiles() { return mProfiles; }
79
80    bool hasValidAudioProfile() const { return mProfiles.hasValidProfile(); }
81
82    bool hasDynamicAudioProfile() const { return mProfiles.hasDynamicProfile(); }
83
84    // searches for an exact match
85    status_t checkExactAudioProfile(uint32_t samplingRate,
86                                    audio_channel_mask_t channelMask,
87                                    audio_format_t format) const
88    {
89        return mProfiles.checkExactProfile(samplingRate, channelMask, format);
90    }
91
92    // searches for a compatible match, currently implemented for input
93    // parameters are input|output, returned value is the best match.
94    status_t checkCompatibleAudioProfile(uint32_t &samplingRate,
95                                         audio_channel_mask_t &channelMask,
96                                         audio_format_t &format) const
97    {
98        return mProfiles.checkCompatibleProfile(samplingRate, channelMask, format, mType, mRole);
99    }
100
101    void clearAudioProfiles() { return mProfiles.clearProfiles(); }
102
103    status_t checkGain(const struct audio_gain_config *gainConfig, int index) const;
104
105    void pickAudioProfile(uint32_t &samplingRate,
106                          audio_channel_mask_t &channelMask,
107                          audio_format_t &format) const;
108
109    static const audio_format_t sPcmFormatCompareTable[];
110
111    static int compareFormats(audio_format_t format1, audio_format_t format2);
112
113    audio_module_handle_t getModuleHandle() const;
114    uint32_t getModuleVersion() const;
115    const char *getModuleName() const;
116
117    bool useInputChannelMask() const
118    {
119        return ((mType == AUDIO_PORT_TYPE_DEVICE) && (mRole == AUDIO_PORT_ROLE_SOURCE)) ||
120                ((mType == AUDIO_PORT_TYPE_MIX) && (mRole == AUDIO_PORT_ROLE_SINK));
121    }
122
123    inline bool isDirectOutput() const
124    {
125        return (mType == AUDIO_PORT_TYPE_MIX) && (mRole == AUDIO_PORT_ROLE_SOURCE) &&
126                (mFlags & (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD));
127    }
128
129    void addRoute(const sp<AudioRoute> &route) { mRoutes.add(route); }
130    const AudioRouteVector &getRoutes() const { return mRoutes; }
131
132    void dump(int fd, int spaces, bool verbose = true) const;
133    void log(const char* indent) const;
134
135    AudioGainCollection mGains; // gain controllers
136    sp<HwModule> mModule;                 // audio HW module exposing this I/O stream
137
138private:
139    void pickChannelMask(audio_channel_mask_t &channelMask, const ChannelsVector &channelMasks) const;
140    void pickSamplingRate(uint32_t &rate,const SampleRateVector &samplingRates) const;
141
142    String8  mName;
143    audio_port_type_t mType;
144    audio_port_role_t mRole;
145    uint32_t mFlags; // attribute flags mask (e.g primary output, direct output...).
146    AudioProfileVector mProfiles; // AudioProfiles supported by this port (format, Rates, Channels)
147    AudioRouteVector mRoutes; // Routes involving this port
148    static volatile int32_t mNextUniqueId;
149};
150
151class AudioPortConfig : public virtual RefBase
152{
153public:
154    AudioPortConfig();
155    virtual ~AudioPortConfig() {}
156
157    status_t applyAudioPortConfig(const struct audio_port_config *config,
158                                  struct audio_port_config *backupConfig = NULL);
159    virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
160                                   const struct audio_port_config *srcConfig = NULL) const = 0;
161    virtual sp<AudioPort> getAudioPort() const = 0;
162    uint32_t mSamplingRate;
163    audio_format_t mFormat;
164    audio_channel_mask_t mChannelMask;
165    struct audio_gain_config mGain;
166};
167
168}; // namespace android
169