blob: 17c7febdd106552b8e30421282778df213a585f7 [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>
Mikhail Naganov704aec42023-07-13 11:08:29 -070023#include <audio_utils/clock.h>
Mikhail Naganovc337a872023-07-07 12:01:17 -070024#include <error/expected_utils.h>
25
26#include "core-impl/StreamAlsa.h"
27
28namespace aidl::android::hardware::audio::core {
29
30StreamAlsa::StreamAlsa(const Metadata& metadata, StreamContext&& context)
31 : StreamCommonImpl(metadata, std::move(context)),
32 mFrameSizeBytes(getContext().getFrameSize()),
33 mIsInput(isInput(metadata)),
34 mConfig(alsa::getPcmConfig(getContext(), mIsInput)) {}
35
36::android::status_t StreamAlsa::init() {
37 return mConfig.has_value() ? ::android::OK : ::android::NO_INIT;
38}
39
40::android::status_t StreamAlsa::standby() {
41 mAlsaDeviceProxies.clear();
42 return ::android::OK;
43}
44
45::android::status_t StreamAlsa::start() {
46 decltype(mAlsaDeviceProxies) alsaDeviceProxies;
47 for (const auto& device : getDeviceProfiles()) {
48 auto profile = alsa::readAlsaDeviceInfo(device);
49 if (!profile.has_value()) {
50 LOG(ERROR) << __func__ << ": unable to read device info, device address=" << device;
51 return ::android::UNKNOWN_ERROR;
52 }
53
54 auto proxy = alsa::makeDeviceProxy();
55 // Always ask for alsa configure as required since the configuration should be supported
56 // by the connected device. That is guaranteed by `setAudioPortConfig` and `setAudioPatch`.
57 if (int err = proxy_prepare(proxy.get(), &profile.value(),
58 const_cast<struct pcm_config*>(&mConfig.value()),
59 true /*require_exact_match*/);
60 err != 0) {
61 LOG(ERROR) << __func__ << ": fail to prepare for device address=" << device
62 << " error=" << err;
63 return ::android::UNKNOWN_ERROR;
64 }
65 if (int err = proxy_open(proxy.get()); err != 0) {
66 LOG(ERROR) << __func__ << ": failed to open device, address=" << device
67 << " error=" << err;
68 return ::android::UNKNOWN_ERROR;
69 }
70 alsaDeviceProxies.push_back(std::move(proxy));
71 }
72 mAlsaDeviceProxies = std::move(alsaDeviceProxies);
73 return ::android::OK;
74}
75
76::android::status_t StreamAlsa::transfer(void* buffer, size_t frameCount, size_t* actualFrameCount,
77 int32_t* latencyMs) {
78 const size_t bytesToTransfer = frameCount * mFrameSizeBytes;
79 unsigned maxLatency = 0;
80 if (mIsInput) {
81 if (mAlsaDeviceProxies.empty()) {
82 LOG(FATAL) << __func__ << ": no input devices";
83 return ::android::NO_INIT;
84 }
85 // For input case, only support single device.
86 proxy_read(mAlsaDeviceProxies[0].get(), buffer, bytesToTransfer);
87 maxLatency = proxy_get_latency(mAlsaDeviceProxies[0].get());
88 } else {
89 for (auto& proxy : mAlsaDeviceProxies) {
90 proxy_write(proxy.get(), buffer, bytesToTransfer);
91 maxLatency = std::max(maxLatency, proxy_get_latency(proxy.get()));
92 }
93 }
94 *actualFrameCount = frameCount;
95 maxLatency = std::min(maxLatency, static_cast<unsigned>(std::numeric_limits<int32_t>::max()));
96 *latencyMs = maxLatency;
97 return ::android::OK;
98}
99
Mikhail Naganov704aec42023-07-13 11:08:29 -0700100::android::status_t StreamAlsa::getPosition(StreamDescriptor::Position* position) {
101 if (mAlsaDeviceProxies.empty()) {
102 LOG(FATAL) << __func__ << ": no input devices";
103 return ::android::NO_INIT;
104 }
105 if (mIsInput) {
106 if (int ret = proxy_get_capture_position(mAlsaDeviceProxies[0].get(), &position->frames,
107 &position->timeNs);
108 ret != 0) {
109 LOG(WARNING) << __func__ << ": failed to retrieve capture position: " << ret;
110 return ::android::INVALID_OPERATION;
111 }
112 } else {
113 uint64_t hwFrames;
114 struct timespec timestamp;
115 if (int ret = proxy_get_presentation_position(mAlsaDeviceProxies[0].get(), &hwFrames,
116 &timestamp);
117 ret == 0) {
118 if (hwFrames > std::numeric_limits<int64_t>::max()) {
119 hwFrames -= std::numeric_limits<int64_t>::max();
120 }
121 position->frames = static_cast<int64_t>(hwFrames);
122 position->timeNs = audio_utils_ns_from_timespec(&timestamp);
123 } else {
124 LOG(WARNING) << __func__ << ": failed to retrieve presentation position: " << ret;
125 return ::android::INVALID_OPERATION;
126 }
127 }
128 return ::android::OK;
129}
130
Mikhail Naganovc337a872023-07-07 12:01:17 -0700131void StreamAlsa::shutdown() {
132 mAlsaDeviceProxies.clear();
133}
134
135} // namespace aidl::android::hardware::audio::core