Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 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 | |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame] | 17 | #include <cmath> |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 18 | #include <limits> |
| 19 | |
| 20 | #define LOG_TAG "AHAL_StreamAlsa" |
| 21 | #include <android-base/logging.h> |
| 22 | |
| 23 | #include <Utils.h> |
Mikhail Naganov | 704aec4 | 2023-07-13 11:08:29 -0700 | [diff] [blame] | 24 | #include <audio_utils/clock.h> |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 25 | #include <error/expected_utils.h> |
| 26 | |
| 27 | #include "core-impl/StreamAlsa.h" |
| 28 | |
| 29 | namespace aidl::android::hardware::audio::core { |
| 30 | |
Mikhail Naganov | 1eedc13 | 2023-07-21 17:45:28 -0700 | [diff] [blame] | 31 | StreamAlsa::StreamAlsa(StreamContext* context, const Metadata& metadata, int readWriteRetries) |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 32 | : StreamCommonImpl(context, metadata), |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame] | 33 | mBufferSizeFrames(getContext().getBufferSizeInFrames()), |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 34 | mFrameSizeBytes(getContext().getFrameSize()), |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame] | 35 | mSampleRate(getContext().getSampleRate()), |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 36 | mIsInput(isInput(metadata)), |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 37 | mConfig(alsa::getPcmConfig(getContext(), mIsInput)), |
| 38 | mReadWriteRetries(readWriteRetries) {} |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 39 | |
Mikhail Naganov | 0413d07 | 2024-08-15 14:12:39 -0700 | [diff] [blame] | 40 | StreamAlsa::~StreamAlsa() { |
| 41 | cleanupWorker(); |
| 42 | } |
| 43 | |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 44 | ::android::status_t StreamAlsa::init() { |
| 45 | return mConfig.has_value() ? ::android::OK : ::android::NO_INIT; |
| 46 | } |
| 47 | |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 48 | ::android::status_t StreamAlsa::drain(StreamDescriptor::DrainMode) { |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame] | 49 | if (!mIsInput) { |
| 50 | static constexpr float kMicrosPerSecond = MICROS_PER_SECOND; |
| 51 | const size_t delayUs = static_cast<size_t>( |
| 52 | std::roundf(mBufferSizeFrames * kMicrosPerSecond / mSampleRate)); |
| 53 | usleep(delayUs); |
| 54 | } |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 55 | return ::android::OK; |
| 56 | } |
| 57 | |
| 58 | ::android::status_t StreamAlsa::flush() { |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 59 | return ::android::OK; |
| 60 | } |
| 61 | |
| 62 | ::android::status_t StreamAlsa::pause() { |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 63 | return ::android::OK; |
| 64 | } |
| 65 | |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 66 | ::android::status_t StreamAlsa::standby() { |
| 67 | mAlsaDeviceProxies.clear(); |
| 68 | return ::android::OK; |
| 69 | } |
| 70 | |
| 71 | ::android::status_t StreamAlsa::start() { |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame] | 72 | if (!mAlsaDeviceProxies.empty()) { |
| 73 | // This is a resume after a pause. |
| 74 | return ::android::OK; |
| 75 | } |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 76 | decltype(mAlsaDeviceProxies) alsaDeviceProxies; |
| 77 | for (const auto& device : getDeviceProfiles()) { |
Weilin Xu | 29e5168 | 2024-10-02 17:16:32 +0000 | [diff] [blame] | 78 | if ((device.direction == PCM_OUT && mIsInput) || |
| 79 | (device.direction == PCM_IN && !mIsInput)) { |
| 80 | continue; |
| 81 | } |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 82 | alsa::DeviceProxy proxy; |
| 83 | if (device.isExternal) { |
| 84 | // Always ask alsa configure as required since the configuration should be supported |
| 85 | // by the connected device. That is guaranteed by `setAudioPortConfig` and |
| 86 | // `setAudioPatch`. |
| 87 | proxy = alsa::openProxyForExternalDevice( |
| 88 | device, const_cast<struct pcm_config*>(&mConfig.value()), |
| 89 | true /*require_exact_match*/); |
| 90 | } else { |
| 91 | proxy = alsa::openProxyForAttachedDevice( |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame] | 92 | device, const_cast<struct pcm_config*>(&mConfig.value()), mBufferSizeFrames); |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 93 | } |
Mikhail Naganov | 5d2bfba | 2023-09-21 17:40:56 -0700 | [diff] [blame] | 94 | if (proxy.get() == nullptr) { |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 95 | return ::android::NO_INIT; |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 96 | } |
| 97 | alsaDeviceProxies.push_back(std::move(proxy)); |
| 98 | } |
Weilin Xu | 29e5168 | 2024-10-02 17:16:32 +0000 | [diff] [blame] | 99 | if (alsaDeviceProxies.empty()) { |
| 100 | return ::android::NO_INIT; |
| 101 | } |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 102 | mAlsaDeviceProxies = std::move(alsaDeviceProxies); |
| 103 | return ::android::OK; |
| 104 | } |
| 105 | |
| 106 | ::android::status_t StreamAlsa::transfer(void* buffer, size_t frameCount, size_t* actualFrameCount, |
| 107 | int32_t* latencyMs) { |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame] | 108 | if (mAlsaDeviceProxies.empty()) { |
| 109 | LOG(FATAL) << __func__ << ": no opened devices"; |
| 110 | return ::android::NO_INIT; |
| 111 | } |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 112 | const size_t bytesToTransfer = frameCount * mFrameSizeBytes; |
| 113 | unsigned maxLatency = 0; |
| 114 | if (mIsInput) { |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 115 | // For input case, only support single device. |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 116 | proxy_read_with_retries(mAlsaDeviceProxies[0].get(), buffer, bytesToTransfer, |
| 117 | mReadWriteRetries); |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 118 | maxLatency = proxy_get_latency(mAlsaDeviceProxies[0].get()); |
| 119 | } else { |
Weilin Xu | a33bb5e | 2024-10-02 17:16:42 +0000 | [diff] [blame] | 120 | alsa::applyGain(buffer, mGain, bytesToTransfer, mConfig.value().format, mConfig->channels); |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 121 | for (auto& proxy : mAlsaDeviceProxies) { |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 122 | proxy_write_with_retries(proxy.get(), buffer, bytesToTransfer, mReadWriteRetries); |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 123 | maxLatency = std::max(maxLatency, proxy_get_latency(proxy.get())); |
| 124 | } |
| 125 | } |
| 126 | *actualFrameCount = frameCount; |
| 127 | maxLatency = std::min(maxLatency, static_cast<unsigned>(std::numeric_limits<int32_t>::max())); |
| 128 | *latencyMs = maxLatency; |
| 129 | return ::android::OK; |
| 130 | } |
| 131 | |
Mikhail Naganov | 459b733 | 2023-08-03 10:26:21 -0700 | [diff] [blame] | 132 | ::android::status_t StreamAlsa::refinePosition(StreamDescriptor::Position* position) { |
Mikhail Naganov | 704aec4 | 2023-07-13 11:08:29 -0700 | [diff] [blame] | 133 | if (mAlsaDeviceProxies.empty()) { |
Mikhail Naganov | 1350187 | 2023-10-18 16:15:46 -0700 | [diff] [blame] | 134 | LOG(WARNING) << __func__ << ": no opened devices"; |
Mikhail Naganov | 704aec4 | 2023-07-13 11:08:29 -0700 | [diff] [blame] | 135 | return ::android::NO_INIT; |
| 136 | } |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame] | 137 | // Since the proxy can only count transferred frames since its creation, |
| 138 | // we override its counter value with ours and let it to correct for buffered frames. |
| 139 | alsa::resetTransferredFrames(mAlsaDeviceProxies[0], position->frames); |
Mikhail Naganov | 704aec4 | 2023-07-13 11:08:29 -0700 | [diff] [blame] | 140 | if (mIsInput) { |
| 141 | if (int ret = proxy_get_capture_position(mAlsaDeviceProxies[0].get(), &position->frames, |
| 142 | &position->timeNs); |
| 143 | ret != 0) { |
| 144 | LOG(WARNING) << __func__ << ": failed to retrieve capture position: " << ret; |
| 145 | return ::android::INVALID_OPERATION; |
| 146 | } |
| 147 | } else { |
| 148 | uint64_t hwFrames; |
| 149 | struct timespec timestamp; |
| 150 | if (int ret = proxy_get_presentation_position(mAlsaDeviceProxies[0].get(), &hwFrames, |
| 151 | ×tamp); |
| 152 | ret == 0) { |
| 153 | if (hwFrames > std::numeric_limits<int64_t>::max()) { |
| 154 | hwFrames -= std::numeric_limits<int64_t>::max(); |
| 155 | } |
| 156 | position->frames = static_cast<int64_t>(hwFrames); |
| 157 | position->timeNs = audio_utils_ns_from_timespec(×tamp); |
| 158 | } else { |
| 159 | LOG(WARNING) << __func__ << ": failed to retrieve presentation position: " << ret; |
| 160 | return ::android::INVALID_OPERATION; |
| 161 | } |
| 162 | } |
| 163 | return ::android::OK; |
| 164 | } |
| 165 | |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 166 | void StreamAlsa::shutdown() { |
| 167 | mAlsaDeviceProxies.clear(); |
| 168 | } |
| 169 | |
Weilin Xu | a33bb5e | 2024-10-02 17:16:42 +0000 | [diff] [blame] | 170 | ndk::ScopedAStatus StreamAlsa::setGain(float gain) { |
| 171 | mGain = gain; |
| 172 | return ndk::ScopedAStatus::ok(); |
| 173 | } |
| 174 | |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 175 | } // namespace aidl::android::hardware::audio::core |