1 /*
2 **
3 ** Copyright 2014, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 #ifndef ANDROID_ALSA_UTILS_H
19 #define ANDROID_ALSA_UTILS_H
20 
21 #include <fcntl.h>
22 #include <stdint.h>
23 #include <hardware/audio.h>
24 
25 #define kHDMI_ALSADeviceName    "IntelHDMI"
26 
27 #ifdef __cplusplus
28 extern "C"
29 #endif
30 int find_alsa_card_by_name(const char* name);
31 
32 #ifdef __cplusplus
33 #include <utils/Vector.h>
34 #include <utils/Mutex.h>
35 #include <utils/String8.h>
36 
37 namespace android {
38 
39 class HDMIAudioCaps {
40   public:
41     enum AudFormat {
42         kFmtInvalid = 0,
43         kFmtLPCM,
44         kFmtAC3,
45         kFmtMP1,
46         kFmtMP1L3,
47         kFmtMP2,
48         kFmtAACLC,
49         kFmtDTS,
50         kFmtATRAC,
51         kFmtDSD,
52         kFmtEAC3,
53         kFmtDTSHD,
54         kFmtMLP,
55         kFmtDST,
56         kFmtWMAPRO,
57         kFmtRefCxt,
58         kFmtHEAAC,
59         kFmtHEAAC2,
60         kFmtMPGSUR
61     };
62 
63     enum SRMask {
64         kSR_32000 = (1 << 5),
65         kSR_44100 = (1 << 6),
66         kSR_48000 = (1 << 7),
67         kSR_88200 = (1 << 9),
68         kSR_96000 = (1 << 10),
69         kSR_176400 = (1 << 11),
70         kSR_192000 = (1 << 12),
71     };
72 
73     enum BPSMask {
74         kBPS_16bit = (1 << 17),
75         kBPS_20bit = (1 << 18),
76         kBPS_24bit = (1 << 19),
77     };
78 
79     enum SAMask {
80         kSA_FLFR   = (1 <<  0), // Front Left/Right
81         kSA_LFE    = (1 <<  1), // LFE (aka, subwoofer)
82         kSA_FC     = (1 <<  2), // Front Center
83         kSA_RLRR   = (1 <<  3), // Rear Left/Right
84         kSA_RC     = (1 <<  4), // Rear Center
85         kSA_FLCFRC = (1 <<  5), // Front Left/Right Center
86         kSA_RLCRRC = (1 <<  6), // Rear Left/Right Center
87         kSA_FLWFRW = (1 <<  7), // Front Left/Right Wide
88         kSA_FLHFRH = (1 <<  8), // Front Left/Right High
89         kSA_TC     = (1 <<  9), // Top Center (overhead)
90         kSA_FCH    = (1 << 10), // Front Center High
91     };
92 
93     typedef struct {
94         AudFormat fmt;
95         uint32_t  max_ch;
96         uint32_t  sr_bitmask;
97         uint32_t  bps_bitmask;
98         uint32_t  comp_bitrate;
99     } Mode;
100 
101     HDMIAudioCaps();
~HDMIAudioCaps()102     ~HDMIAudioCaps() { reset(); }
103 
104     bool loadCaps(int ALSADeviceID);
105     void reset();
106     void getRatesForAF(String8& rates);
107     void getFmtsForAF(String8& fmts);
108     void getChannelMasksForAF(String8& masks);
109     bool supportsFormat(audio_format_t format,
110                                       uint32_t sampleRate,
111                                       uint32_t channelCount,
112                                       bool isIec958NonAudio);
113 
basicAudioSupport()114     bool basicAudioSupport() const { return mBasicAudioSupported; }
speakerAllocation()115     uint16_t speakerAllocation() const { return mSpeakerAlloc; }
modeCnt()116     size_t modeCnt() const { return mModes.size(); }
getMode(size_t ndx)117     const Mode& getMode(size_t ndx) const { return mModes[ndx]; }
118 
119     static const char* fmtToString(AudFormat fmt);
120     static uint32_t srMaskToSR(uint32_t mask);
121     static uint32_t bpsMaskToBPS(uint32_t mask);
122     static const char* saMaskToString(uint32_t mask);
123 
124   private:
125     Mutex mLock;
126     bool mBasicAudioSupported;
127     uint16_t mSpeakerAlloc;
128     Vector<Mode> mModes;
129 
130     void reset_l();
131     ssize_t getMaxChModeNdx_l();
132     static bool sanityCheckMode(const Mode& m);
133 };
134 }  // namespace android
135 #endif  // __cplusplus
136 #endif  // ANDROID_ALSA_UTILS_H
137