1 /*
2 * Copyright (C) 2024 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 <cmath>
18
19 #define LOG_TAG "AHAL_Stream"
20 #include <android-base/logging.h>
21 #include <audio_utils/clock.h>
22
23 #include "core-impl/DriverStubImpl.h"
24
25 namespace aidl::android::hardware::audio::core {
26
DriverStubImpl(const StreamContext & context)27 DriverStubImpl::DriverStubImpl(const StreamContext& context)
28 : mBufferSizeFrames(context.getBufferSizeInFrames()),
29 mFrameSizeBytes(context.getFrameSize()),
30 mSampleRate(context.getSampleRate()),
31 mIsAsynchronous(!!context.getAsyncCallback()),
32 mIsInput(context.isInput()) {}
33
init()34 ::android::status_t DriverStubImpl::init() {
35 mIsInitialized = true;
36 return ::android::OK;
37 }
38
drain(StreamDescriptor::DrainMode)39 ::android::status_t DriverStubImpl::drain(StreamDescriptor::DrainMode) {
40 if (!mIsInitialized) {
41 LOG(FATAL) << __func__ << ": must not happen for an uninitialized driver";
42 }
43 if (!mIsInput) {
44 if (!mIsAsynchronous) {
45 static constexpr float kMicrosPerSecond = MICROS_PER_SECOND;
46 const size_t delayUs = static_cast<size_t>(
47 std::roundf(mBufferSizeFrames * kMicrosPerSecond / mSampleRate));
48 usleep(delayUs);
49 } else {
50 usleep(500);
51 }
52 }
53 return ::android::OK;
54 }
55
flush()56 ::android::status_t DriverStubImpl::flush() {
57 if (!mIsInitialized) {
58 LOG(FATAL) << __func__ << ": must not happen for an uninitialized driver";
59 }
60 return ::android::OK;
61 }
62
pause()63 ::android::status_t DriverStubImpl::pause() {
64 if (!mIsInitialized) {
65 LOG(FATAL) << __func__ << ": must not happen for an uninitialized driver";
66 }
67 return ::android::OK;
68 }
69
standby()70 ::android::status_t DriverStubImpl::standby() {
71 if (!mIsInitialized) {
72 LOG(FATAL) << __func__ << ": must not happen for an uninitialized driver";
73 }
74 mIsStandby = true;
75 return ::android::OK;
76 }
77
start()78 ::android::status_t DriverStubImpl::start() {
79 if (!mIsInitialized) {
80 LOG(FATAL) << __func__ << ": must not happen for an uninitialized driver";
81 }
82 mIsStandby = false;
83 mStartTimeNs = ::android::uptimeNanos();
84 mFramesSinceStart = 0;
85 return ::android::OK;
86 }
87
transfer(void * buffer,size_t frameCount,size_t * actualFrameCount,int32_t *)88 ::android::status_t DriverStubImpl::transfer(void* buffer, size_t frameCount,
89 size_t* actualFrameCount, int32_t*) {
90 if (!mIsInitialized) {
91 LOG(FATAL) << __func__ << ": must not happen for an uninitialized driver";
92 }
93 if (mIsStandby) {
94 LOG(FATAL) << __func__ << ": must not happen while in standby";
95 }
96 *actualFrameCount = frameCount;
97 if (mIsAsynchronous) {
98 usleep(500);
99 } else {
100 mFramesSinceStart += *actualFrameCount;
101 const long bufferDurationUs = (*actualFrameCount) * MICROS_PER_SECOND / mSampleRate;
102 const auto totalDurationUs =
103 (::android::uptimeNanos() - mStartTimeNs) / NANOS_PER_MICROSECOND;
104 const long totalOffsetUs =
105 mFramesSinceStart * MICROS_PER_SECOND / mSampleRate - totalDurationUs;
106 LOG(VERBOSE) << __func__ << ": totalOffsetUs " << totalOffsetUs;
107 if (totalOffsetUs > 0) {
108 const long sleepTimeUs = std::min(totalOffsetUs, bufferDurationUs);
109 LOG(VERBOSE) << __func__ << ": sleeping for " << sleepTimeUs << " us";
110 usleep(sleepTimeUs);
111 }
112 }
113 if (mIsInput) {
114 uint8_t* byteBuffer = static_cast<uint8_t*>(buffer);
115 for (size_t i = 0; i < frameCount * mFrameSizeBytes; ++i) {
116 byteBuffer[i] = std::rand() % 255;
117 }
118 }
119 return ::android::OK;
120 }
121
shutdown()122 void DriverStubImpl::shutdown() {
123 mIsInitialized = false;
124 }
125
126 } // namespace aidl::android::hardware::audio::core
127