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 <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/Module.h"
24 #include "core-impl/StreamStub.h"
25
26 using aidl::android::hardware::audio::common::SinkMetadata;
27 using aidl::android::hardware::audio::common::SourceMetadata;
28 using aidl::android::media::audio::common::AudioDevice;
29 using aidl::android::media::audio::common::AudioOffloadInfo;
30 using aidl::android::media::audio::common::MicrophoneInfo;
31
32 namespace aidl::android::hardware::audio::core {
33
StreamStub(StreamContext * context,const Metadata & metadata)34 StreamStub::StreamStub(StreamContext* context, const Metadata& metadata)
35 : StreamCommonImpl(context, metadata),
36 mBufferSizeFrames(getContext().getBufferSizeInFrames()),
37 mFrameSizeBytes(getContext().getFrameSize()),
38 mSampleRate(getContext().getSampleRate()),
39 mIsAsynchronous(!!getContext().getAsyncCallback()),
40 mIsInput(isInput(metadata)) {}
41
init()42 ::android::status_t StreamStub::init() {
43 mIsInitialized = true;
44 return ::android::OK;
45 }
46
drain(StreamDescriptor::DrainMode)47 ::android::status_t StreamStub::drain(StreamDescriptor::DrainMode) {
48 if (!mIsInitialized) {
49 LOG(FATAL) << __func__ << ": must not happen for an uninitialized driver";
50 }
51 if (!mIsInput) {
52 if (!mIsAsynchronous) {
53 static constexpr float kMicrosPerSecond = MICROS_PER_SECOND;
54 const size_t delayUs = static_cast<size_t>(
55 std::roundf(mBufferSizeFrames * kMicrosPerSecond / mSampleRate));
56 usleep(delayUs);
57 } else {
58 usleep(500);
59 }
60 }
61 return ::android::OK;
62 }
63
flush()64 ::android::status_t StreamStub::flush() {
65 if (!mIsInitialized) {
66 LOG(FATAL) << __func__ << ": must not happen for an uninitialized driver";
67 }
68 return ::android::OK;
69 }
70
pause()71 ::android::status_t StreamStub::pause() {
72 if (!mIsInitialized) {
73 LOG(FATAL) << __func__ << ": must not happen for an uninitialized driver";
74 }
75 return ::android::OK;
76 }
77
standby()78 ::android::status_t StreamStub::standby() {
79 if (!mIsInitialized) {
80 LOG(FATAL) << __func__ << ": must not happen for an uninitialized driver";
81 }
82 usleep(500);
83 mIsStandby = true;
84 return ::android::OK;
85 }
86
start()87 ::android::status_t StreamStub::start() {
88 if (!mIsInitialized) {
89 LOG(FATAL) << __func__ << ": must not happen for an uninitialized driver";
90 }
91 usleep(500);
92 mIsStandby = false;
93 return ::android::OK;
94 }
95
transfer(void * buffer,size_t frameCount,size_t * actualFrameCount,int32_t *)96 ::android::status_t StreamStub::transfer(void* buffer, size_t frameCount, size_t* actualFrameCount,
97 int32_t*) {
98 if (!mIsInitialized) {
99 LOG(FATAL) << __func__ << ": must not happen for an uninitialized driver";
100 }
101 if (mIsStandby) {
102 LOG(FATAL) << __func__ << ": must not happen while in standby";
103 }
104 static constexpr float kMicrosPerSecond = MICROS_PER_SECOND;
105 static constexpr float kScaleFactor = .8f;
106 if (mIsAsynchronous) {
107 usleep(500);
108 } else {
109 const size_t delayUs = static_cast<size_t>(
110 std::roundf(kScaleFactor * frameCount * kMicrosPerSecond / mSampleRate));
111 usleep(delayUs);
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 *actualFrameCount = frameCount;
120 return ::android::OK;
121 }
122
shutdown()123 void StreamStub::shutdown() {
124 mIsInitialized = false;
125 }
126
StreamInStub(StreamContext && context,const SinkMetadata & sinkMetadata,const std::vector<MicrophoneInfo> & microphones)127 StreamInStub::StreamInStub(StreamContext&& context, const SinkMetadata& sinkMetadata,
128 const std::vector<MicrophoneInfo>& microphones)
129 : StreamIn(std::move(context), microphones), StreamStub(&mContextInstance, sinkMetadata) {}
130
StreamOutStub(StreamContext && context,const SourceMetadata & sourceMetadata,const std::optional<AudioOffloadInfo> & offloadInfo)131 StreamOutStub::StreamOutStub(StreamContext&& context, const SourceMetadata& sourceMetadata,
132 const std::optional<AudioOffloadInfo>& offloadInfo)
133 : StreamOut(std::move(context), offloadInfo), StreamStub(&mContextInstance, sourceMetadata) {}
134
135 } // namespace aidl::android::hardware::audio::core
136