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 | |
| 17 | #include <limits> |
| 18 | |
| 19 | #define LOG_TAG "AHAL_StreamAlsa" |
| 20 | #include <android-base/logging.h> |
| 21 | |
| 22 | #include <Utils.h> |
Mikhail Naganov | 704aec4 | 2023-07-13 11:08:29 -0700 | [diff] [blame] | 23 | #include <audio_utils/clock.h> |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 24 | #include <error/expected_utils.h> |
| 25 | |
| 26 | #include "core-impl/StreamAlsa.h" |
| 27 | |
| 28 | namespace aidl::android::hardware::audio::core { |
| 29 | |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame^] | 30 | StreamAlsa::StreamAlsa(const StreamContext& context, const Metadata& metadata, int readWriteRetries) |
| 31 | : StreamCommonImpl(context, metadata), |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 32 | mFrameSizeBytes(getContext().getFrameSize()), |
| 33 | mIsInput(isInput(metadata)), |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 34 | mConfig(alsa::getPcmConfig(getContext(), mIsInput)), |
| 35 | mReadWriteRetries(readWriteRetries) {} |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 36 | |
| 37 | ::android::status_t StreamAlsa::init() { |
| 38 | return mConfig.has_value() ? ::android::OK : ::android::NO_INIT; |
| 39 | } |
| 40 | |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 41 | ::android::status_t StreamAlsa::drain(StreamDescriptor::DrainMode) { |
| 42 | usleep(1000); |
| 43 | return ::android::OK; |
| 44 | } |
| 45 | |
| 46 | ::android::status_t StreamAlsa::flush() { |
| 47 | usleep(1000); |
| 48 | return ::android::OK; |
| 49 | } |
| 50 | |
| 51 | ::android::status_t StreamAlsa::pause() { |
| 52 | usleep(1000); |
| 53 | return ::android::OK; |
| 54 | } |
| 55 | |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 56 | ::android::status_t StreamAlsa::standby() { |
| 57 | mAlsaDeviceProxies.clear(); |
| 58 | return ::android::OK; |
| 59 | } |
| 60 | |
| 61 | ::android::status_t StreamAlsa::start() { |
| 62 | decltype(mAlsaDeviceProxies) alsaDeviceProxies; |
| 63 | for (const auto& device : getDeviceProfiles()) { |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 64 | alsa::DeviceProxy proxy; |
| 65 | if (device.isExternal) { |
| 66 | // Always ask alsa configure as required since the configuration should be supported |
| 67 | // by the connected device. That is guaranteed by `setAudioPortConfig` and |
| 68 | // `setAudioPatch`. |
| 69 | proxy = alsa::openProxyForExternalDevice( |
| 70 | device, const_cast<struct pcm_config*>(&mConfig.value()), |
| 71 | true /*require_exact_match*/); |
| 72 | } else { |
| 73 | proxy = alsa::openProxyForAttachedDevice( |
| 74 | device, const_cast<struct pcm_config*>(&mConfig.value()), |
| 75 | getContext().getBufferSizeInFrames()); |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 76 | } |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 77 | if (!proxy) { |
| 78 | return ::android::NO_INIT; |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 79 | } |
| 80 | alsaDeviceProxies.push_back(std::move(proxy)); |
| 81 | } |
| 82 | mAlsaDeviceProxies = std::move(alsaDeviceProxies); |
| 83 | return ::android::OK; |
| 84 | } |
| 85 | |
| 86 | ::android::status_t StreamAlsa::transfer(void* buffer, size_t frameCount, size_t* actualFrameCount, |
| 87 | int32_t* latencyMs) { |
| 88 | const size_t bytesToTransfer = frameCount * mFrameSizeBytes; |
| 89 | unsigned maxLatency = 0; |
| 90 | if (mIsInput) { |
| 91 | if (mAlsaDeviceProxies.empty()) { |
| 92 | LOG(FATAL) << __func__ << ": no input devices"; |
| 93 | return ::android::NO_INIT; |
| 94 | } |
| 95 | // For input case, only support single device. |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 96 | proxy_read_with_retries(mAlsaDeviceProxies[0].get(), buffer, bytesToTransfer, |
| 97 | mReadWriteRetries); |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 98 | maxLatency = proxy_get_latency(mAlsaDeviceProxies[0].get()); |
| 99 | } else { |
| 100 | for (auto& proxy : mAlsaDeviceProxies) { |
Mikhail Naganov | 422f7e6 | 2023-07-13 16:32:08 -0700 | [diff] [blame] | 101 | proxy_write_with_retries(proxy.get(), buffer, bytesToTransfer, mReadWriteRetries); |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 102 | maxLatency = std::max(maxLatency, proxy_get_latency(proxy.get())); |
| 103 | } |
| 104 | } |
| 105 | *actualFrameCount = frameCount; |
| 106 | maxLatency = std::min(maxLatency, static_cast<unsigned>(std::numeric_limits<int32_t>::max())); |
| 107 | *latencyMs = maxLatency; |
| 108 | return ::android::OK; |
| 109 | } |
| 110 | |
Mikhail Naganov | 704aec4 | 2023-07-13 11:08:29 -0700 | [diff] [blame] | 111 | ::android::status_t StreamAlsa::getPosition(StreamDescriptor::Position* position) { |
| 112 | if (mAlsaDeviceProxies.empty()) { |
| 113 | LOG(FATAL) << __func__ << ": no input devices"; |
| 114 | return ::android::NO_INIT; |
| 115 | } |
| 116 | if (mIsInput) { |
| 117 | if (int ret = proxy_get_capture_position(mAlsaDeviceProxies[0].get(), &position->frames, |
| 118 | &position->timeNs); |
| 119 | ret != 0) { |
| 120 | LOG(WARNING) << __func__ << ": failed to retrieve capture position: " << ret; |
| 121 | return ::android::INVALID_OPERATION; |
| 122 | } |
| 123 | } else { |
| 124 | uint64_t hwFrames; |
| 125 | struct timespec timestamp; |
| 126 | if (int ret = proxy_get_presentation_position(mAlsaDeviceProxies[0].get(), &hwFrames, |
| 127 | ×tamp); |
| 128 | ret == 0) { |
| 129 | if (hwFrames > std::numeric_limits<int64_t>::max()) { |
| 130 | hwFrames -= std::numeric_limits<int64_t>::max(); |
| 131 | } |
| 132 | position->frames = static_cast<int64_t>(hwFrames); |
| 133 | position->timeNs = audio_utils_ns_from_timespec(×tamp); |
| 134 | } else { |
| 135 | LOG(WARNING) << __func__ << ": failed to retrieve presentation position: " << ret; |
| 136 | return ::android::INVALID_OPERATION; |
| 137 | } |
| 138 | } |
| 139 | return ::android::OK; |
| 140 | } |
| 141 | |
Mikhail Naganov | c337a87 | 2023-07-07 12:01:17 -0700 | [diff] [blame] | 142 | void StreamAlsa::shutdown() { |
| 143 | mAlsaDeviceProxies.clear(); |
| 144 | } |
| 145 | |
| 146 | } // namespace aidl::android::hardware::audio::core |