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 | |
| 40 | ::android::status_t StreamAlsa::init() { |
| 41 | return mConfig.has_value() ? ::android::OK : ::android::NO_INIT; |
| 42 | } |
| 43 | |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 44 | ::android::status_t StreamAlsa::drain(StreamDescriptor::DrainMode) { |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame] | 45 | if (!mIsInput) { |
| 46 | static constexpr float kMicrosPerSecond = MICROS_PER_SECOND; |
| 47 | const size_t delayUs = static_cast<size_t>( |
| 48 | std::roundf(mBufferSizeFrames * kMicrosPerSecond / mSampleRate)); |
| 49 | usleep(delayUs); |
| 50 | } |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 51 | return ::android::OK; |
| 52 | } |
| 53 | |
| 54 | ::android::status_t StreamAlsa::flush() { |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 55 | return ::android::OK; |
| 56 | } |
| 57 | |
| 58 | ::android::status_t StreamAlsa::pause() { |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 59 | return ::android::OK; |
| 60 | } |
| 61 | |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 62 | ::android::status_t StreamAlsa::standby() { |
| 63 | mAlsaDeviceProxies.clear(); |
| 64 | return ::android::OK; |
| 65 | } |
| 66 | |
| 67 | ::android::status_t StreamAlsa::start() { |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame] | 68 | if (!mAlsaDeviceProxies.empty()) { |
| 69 | // This is a resume after a pause. |
| 70 | return ::android::OK; |
| 71 | } |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 72 | decltype(mAlsaDeviceProxies) alsaDeviceProxies; |
| 73 | for (const auto& device : getDeviceProfiles()) { |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 74 | alsa::DeviceProxy proxy; |
| 75 | if (device.isExternal) { |
| 76 | // Always ask alsa configure as required since the configuration should be supported |
| 77 | // by the connected device. That is guaranteed by `setAudioPortConfig` and |
| 78 | // `setAudioPatch`. |
| 79 | proxy = alsa::openProxyForExternalDevice( |
| 80 | device, const_cast<struct pcm_config*>(&mConfig.value()), |
| 81 | true /*require_exact_match*/); |
| 82 | } else { |
| 83 | proxy = alsa::openProxyForAttachedDevice( |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame] | 84 | device, const_cast<struct pcm_config*>(&mConfig.value()), mBufferSizeFrames); |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 85 | } |
Mikhail Naganov | 5d2bfba | 2023-09-21 17:40:56 -0700 | [diff] [blame] | 86 | if (proxy.get() == nullptr) { |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 87 | return ::android::NO_INIT; |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 88 | } |
| 89 | alsaDeviceProxies.push_back(std::move(proxy)); |
| 90 | } |
| 91 | mAlsaDeviceProxies = std::move(alsaDeviceProxies); |
| 92 | return ::android::OK; |
| 93 | } |
| 94 | |
| 95 | ::android::status_t StreamAlsa::transfer(void* buffer, size_t frameCount, size_t* actualFrameCount, |
| 96 | int32_t* latencyMs) { |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame] | 97 | if (mAlsaDeviceProxies.empty()) { |
| 98 | LOG(FATAL) << __func__ << ": no opened devices"; |
| 99 | return ::android::NO_INIT; |
| 100 | } |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 101 | const size_t bytesToTransfer = frameCount * mFrameSizeBytes; |
| 102 | unsigned maxLatency = 0; |
| 103 | if (mIsInput) { |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 104 | // For input case, only support single device. |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 105 | proxy_read_with_retries(mAlsaDeviceProxies[0].get(), buffer, bytesToTransfer, |
| 106 | mReadWriteRetries); |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 107 | maxLatency = proxy_get_latency(mAlsaDeviceProxies[0].get()); |
| 108 | } else { |
| 109 | for (auto& proxy : mAlsaDeviceProxies) { |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 110 | proxy_write_with_retries(proxy.get(), buffer, bytesToTransfer, mReadWriteRetries); |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 111 | maxLatency = std::max(maxLatency, proxy_get_latency(proxy.get())); |
| 112 | } |
| 113 | } |
| 114 | *actualFrameCount = frameCount; |
| 115 | maxLatency = std::min(maxLatency, static_cast<unsigned>(std::numeric_limits<int32_t>::max())); |
| 116 | *latencyMs = maxLatency; |
| 117 | return ::android::OK; |
| 118 | } |
| 119 | |
Mikhail Naganov | 459b733 | 2023-08-03 10:26:21 -0700 | [diff] [blame] | 120 | ::android::status_t StreamAlsa::refinePosition(StreamDescriptor::Position* position) { |
Mikhail Naganov | 704aec4 | 2023-07-13 11:08:29 -0700 | [diff] [blame] | 121 | if (mAlsaDeviceProxies.empty()) { |
Mikhail Naganov | 1350187 | 2023-10-18 16:15:46 -0700 | [diff] [blame^] | 122 | LOG(WARNING) << __func__ << ": no opened devices"; |
Mikhail Naganov | 704aec4 | 2023-07-13 11:08:29 -0700 | [diff] [blame] | 123 | return ::android::NO_INIT; |
| 124 | } |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame] | 125 | // Since the proxy can only count transferred frames since its creation, |
| 126 | // we override its counter value with ours and let it to correct for buffered frames. |
| 127 | alsa::resetTransferredFrames(mAlsaDeviceProxies[0], position->frames); |
Mikhail Naganov | 704aec4 | 2023-07-13 11:08:29 -0700 | [diff] [blame] | 128 | if (mIsInput) { |
| 129 | if (int ret = proxy_get_capture_position(mAlsaDeviceProxies[0].get(), &position->frames, |
| 130 | &position->timeNs); |
| 131 | ret != 0) { |
| 132 | LOG(WARNING) << __func__ << ": failed to retrieve capture position: " << ret; |
| 133 | return ::android::INVALID_OPERATION; |
| 134 | } |
| 135 | } else { |
| 136 | uint64_t hwFrames; |
| 137 | struct timespec timestamp; |
| 138 | if (int ret = proxy_get_presentation_position(mAlsaDeviceProxies[0].get(), &hwFrames, |
| 139 | ×tamp); |
| 140 | ret == 0) { |
| 141 | if (hwFrames > std::numeric_limits<int64_t>::max()) { |
| 142 | hwFrames -= std::numeric_limits<int64_t>::max(); |
| 143 | } |
| 144 | position->frames = static_cast<int64_t>(hwFrames); |
| 145 | position->timeNs = audio_utils_ns_from_timespec(×tamp); |
| 146 | } else { |
| 147 | LOG(WARNING) << __func__ << ": failed to retrieve presentation position: " << ret; |
| 148 | return ::android::INVALID_OPERATION; |
| 149 | } |
| 150 | } |
| 151 | return ::android::OK; |
| 152 | } |
| 153 | |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 154 | void StreamAlsa::shutdown() { |
| 155 | mAlsaDeviceProxies.clear(); |
| 156 | } |
| 157 | |
| 158 | } // namespace aidl::android::hardware::audio::core |