summaryrefslogtreecommitdiffstats
path: root/audio/util.cpp
blob: 746e37de9add2a1f244774fb5b97b5534ce80afc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <log/log.h>
#include <cutils/bitops.h>
#include <system/audio.h>
#include "util.h"

namespace android {
namespace hardware {
namespace audio {
namespace V6_0 {
namespace implementation {
namespace util {

namespace {

const std::array<uint32_t, 8> kSupportedRatesHz = {
    8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000
};

const std::array<hidl_bitfield<AudioChannelMask>, 4> kSupportedInChannelMask = {
    AudioChannelMask::IN_LEFT | 0,
    AudioChannelMask::IN_RIGHT | 0,
    AudioChannelMask::IN_FRONT | 0,
    AudioChannelMask::IN_STEREO | 0,
};

const std::array<hidl_bitfield<AudioChannelMask>, 4> kSupportedOutChannelMask = {
    AudioChannelMask::OUT_FRONT_LEFT | 0,
    AudioChannelMask::OUT_FRONT_RIGHT | 0,
    AudioChannelMask::OUT_FRONT_CENTER | 0,
    AudioChannelMask::OUT_STEREO | 0,
};

const std::array<AudioFormat, 1> kSupportedAudioFormats = {
    AudioFormat::PCM_16_BIT,
};

bool checkSampleRateHz(uint32_t value, uint32_t &suggest) {
    for (const uint32_t supported : kSupportedRatesHz) {
        if (value <= supported) {
            suggest = supported;
            return (value == supported);
        }
    }

    suggest = kSupportedRatesHz.back();
    return false;
}

size_t getBufferSizeFrames(size_t duration_ms, uint32_t sample_rate) {
    return sample_rate * duration_ms / 1000;
}

}  // namespace

MicrophoneInfo getMicrophoneInfo() {
    MicrophoneInfo mic;

    mic.deviceId = "mic_goldfish";
    mic.group = 0;
    mic.indexInTheGroup = 0;
    mic.sensitivity = AUDIO_MICROPHONE_SENSITIVITY_UNKNOWN;
    mic.maxSpl = AUDIO_MICROPHONE_SENSITIVITY_UNKNOWN;
    mic.minSpl = AUDIO_MICROPHONE_SENSITIVITY_UNKNOWN;
    mic.directionality = AudioMicrophoneDirectionality::UNKNOWN;
    mic.position.x = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
    mic.position.y = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
    mic.position.z = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
    mic.orientation.x = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
    mic.orientation.y = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
    mic.orientation.z = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;

    return mic;
}

size_t countChannels(hidl_bitfield<AudioChannelMask> mask) {
    return popcount(mask);
}

size_t getBytesPerSample(AudioFormat format) {
    return audio_bytes_per_sample(static_cast<audio_format_t>(format));
}

bool checkAudioConfig(bool isOut,
                      size_t duration_ms,
                      const AudioConfig &cfg,
                      AudioConfig &suggested) {
    bool valid = checkSampleRateHz(cfg.sampleRateHz, suggested.sampleRateHz);

    if (isOut) {
        if (std::find(kSupportedOutChannelMask.begin(),
                      kSupportedOutChannelMask.end(),
                      cfg.channelMask) == kSupportedOutChannelMask.end()) {
            suggested.channelMask = AudioChannelMask::OUT_STEREO | 0;
            valid = false;
        } else {
            suggested.channelMask = cfg.channelMask;
        }
    } else {
        if (std::find(kSupportedInChannelMask.begin(),
                      kSupportedInChannelMask.end(),
                      cfg.channelMask) == kSupportedInChannelMask.end()) {
            suggested.channelMask = AudioChannelMask::IN_STEREO | 0;
            valid = false;
        } else {
            suggested.channelMask = cfg.channelMask;
        }
    }

    if (std::find(kSupportedAudioFormats.begin(),
                  kSupportedAudioFormats.end(),
                  cfg.format) == kSupportedAudioFormats.end()) {
        suggested.format = AudioFormat::PCM_16_BIT;
        valid = false;
    } else {
        suggested.format = cfg.format;
    }

    suggested.offloadInfo = cfg.offloadInfo;    // don't care

    suggested.frameCount = (cfg.frameCount == 0)
        ? getBufferSizeFrames(duration_ms, suggested.sampleRateHz)
        : cfg.frameCount;

    return valid;
}

StreamPosition::StreamPosition() : mTimestamp(systemTime(SYSTEM_TIME_MONOTONIC)) {}

void StreamPosition::addFrames(uint64_t n) {
    mTimestamp = systemTime(SYSTEM_TIME_MONOTONIC);
    mFrames += n;
}

void StreamPosition::now(const size_t sampleRateHz,
                         uint64_t &frames,
                         nsecs_t &timestamp) const {
    const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
    const uint64_t deltaUs = ns2us(now - mTimestamp);

    frames = mFrames + sampleRateHz * deltaUs / 1000000;
    timestamp = now;
}

void StreamPosition::reset() {
    *this = StreamPosition();
}

}  // namespace util
}  // namespace implementation
}  // namespace V6_0
}  // namespace audio
}  // namespace hardware
}  // namespace android