blob: ecb3c78a6540cc41b06aa25a6256e8b1a27862d7 [file] [log] [blame]
Mikhail Naganovc337a872023-07-07 12:01:17 -07001/*
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>
23#include <error/expected_utils.h>
24
25#include "core-impl/StreamAlsa.h"
26
27namespace aidl::android::hardware::audio::core {
28
29StreamAlsa::StreamAlsa(const Metadata& metadata, StreamContext&& context)
30 : StreamCommonImpl(metadata, std::move(context)),
31 mFrameSizeBytes(getContext().getFrameSize()),
32 mIsInput(isInput(metadata)),
33 mConfig(alsa::getPcmConfig(getContext(), mIsInput)) {}
34
35::android::status_t StreamAlsa::init() {
36 return mConfig.has_value() ? ::android::OK : ::android::NO_INIT;
37}
38
39::android::status_t StreamAlsa::standby() {
40 mAlsaDeviceProxies.clear();
41 return ::android::OK;
42}
43
44::android::status_t StreamAlsa::start() {
45 decltype(mAlsaDeviceProxies) alsaDeviceProxies;
46 for (const auto& device : getDeviceProfiles()) {
47 auto profile = alsa::readAlsaDeviceInfo(device);
48 if (!profile.has_value()) {
49 LOG(ERROR) << __func__ << ": unable to read device info, device address=" << device;
50 return ::android::UNKNOWN_ERROR;
51 }
52
53 auto proxy = alsa::makeDeviceProxy();
54 // Always ask for alsa configure as required since the configuration should be supported
55 // by the connected device. That is guaranteed by `setAudioPortConfig` and `setAudioPatch`.
56 if (int err = proxy_prepare(proxy.get(), &profile.value(),
57 const_cast<struct pcm_config*>(&mConfig.value()),
58 true /*require_exact_match*/);
59 err != 0) {
60 LOG(ERROR) << __func__ << ": fail to prepare for device address=" << device
61 << " error=" << err;
62 return ::android::UNKNOWN_ERROR;
63 }
64 if (int err = proxy_open(proxy.get()); err != 0) {
65 LOG(ERROR) << __func__ << ": failed to open device, address=" << device
66 << " error=" << err;
67 return ::android::UNKNOWN_ERROR;
68 }
69 alsaDeviceProxies.push_back(std::move(proxy));
70 }
71 mAlsaDeviceProxies = std::move(alsaDeviceProxies);
72 return ::android::OK;
73}
74
75::android::status_t StreamAlsa::transfer(void* buffer, size_t frameCount, size_t* actualFrameCount,
76 int32_t* latencyMs) {
77 const size_t bytesToTransfer = frameCount * mFrameSizeBytes;
78 unsigned maxLatency = 0;
79 if (mIsInput) {
80 if (mAlsaDeviceProxies.empty()) {
81 LOG(FATAL) << __func__ << ": no input devices";
82 return ::android::NO_INIT;
83 }
84 // For input case, only support single device.
85 proxy_read(mAlsaDeviceProxies[0].get(), buffer, bytesToTransfer);
86 maxLatency = proxy_get_latency(mAlsaDeviceProxies[0].get());
87 } else {
88 for (auto& proxy : mAlsaDeviceProxies) {
89 proxy_write(proxy.get(), buffer, bytesToTransfer);
90 maxLatency = std::max(maxLatency, proxy_get_latency(proxy.get()));
91 }
92 }
93 *actualFrameCount = frameCount;
94 maxLatency = std::min(maxLatency, static_cast<unsigned>(std::numeric_limits<int32_t>::max()));
95 *latencyMs = maxLatency;
96 return ::android::OK;
97}
98
99void StreamAlsa::shutdown() {
100 mAlsaDeviceProxies.clear();
101}
102
103} // namespace aidl::android::hardware::audio::core