Mikhail Naganov | f429c03 | 2023-01-07 00:24:50 +0000 | [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 | |
Mikhail Naganov | e48e573 | 2023-03-06 18:48:02 -0800 | [diff] [blame] | 17 | #include <cmath> |
| 18 | |
Mikhail Naganov | f429c03 | 2023-01-07 00:24:50 +0000 | [diff] [blame] | 19 | #define LOG_TAG "AHAL_Stream" |
| 20 | #include <android-base/logging.h> |
Mikhail Naganov | e48e573 | 2023-03-06 18:48:02 -0800 | [diff] [blame] | 21 | #include <audio_utils/clock.h> |
Mikhail Naganov | f429c03 | 2023-01-07 00:24:50 +0000 | [diff] [blame] | 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; |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 28 | using aidl::android::media::audio::common::AudioDevice; |
Mikhail Naganov | f429c03 | 2023-01-07 00:24:50 +0000 | [diff] [blame] | 29 | using aidl::android::media::audio::common::AudioOffloadInfo; |
Mikhail Naganov | 6725ef5 | 2023-02-09 17:52:50 -0800 | [diff] [blame] | 30 | using aidl::android::media::audio::common::MicrophoneInfo; |
Mikhail Naganov | f429c03 | 2023-01-07 00:24:50 +0000 | [diff] [blame] | 31 | |
| 32 | namespace aidl::android::hardware::audio::core { |
| 33 | |
| 34 | DriverStub::DriverStub(const StreamContext& context, bool isInput) |
Mikhail Naganov | e48e573 | 2023-03-06 18:48:02 -0800 | [diff] [blame] | 35 | : mFrameSizeBytes(context.getFrameSize()), |
| 36 | mSampleRate(context.getSampleRate()), |
| 37 | mIsAsynchronous(!!context.getAsyncCallback()), |
| 38 | mIsInput(isInput) {} |
Mikhail Naganov | f429c03 | 2023-01-07 00:24:50 +0000 | [diff] [blame] | 39 | |
| 40 | ::android::status_t DriverStub::init() { |
Mikhail Naganov | e48e573 | 2023-03-06 18:48:02 -0800 | [diff] [blame] | 41 | usleep(500); |
Mikhail Naganov | f429c03 | 2023-01-07 00:24:50 +0000 | [diff] [blame] | 42 | return ::android::OK; |
| 43 | } |
| 44 | |
| 45 | ::android::status_t DriverStub::drain(StreamDescriptor::DrainMode) { |
Mikhail Naganov | e48e573 | 2023-03-06 18:48:02 -0800 | [diff] [blame] | 46 | usleep(500); |
Mikhail Naganov | f429c03 | 2023-01-07 00:24:50 +0000 | [diff] [blame] | 47 | return ::android::OK; |
| 48 | } |
| 49 | |
| 50 | ::android::status_t DriverStub::flush() { |
Mikhail Naganov | e48e573 | 2023-03-06 18:48:02 -0800 | [diff] [blame] | 51 | usleep(500); |
Mikhail Naganov | f429c03 | 2023-01-07 00:24:50 +0000 | [diff] [blame] | 52 | return ::android::OK; |
| 53 | } |
| 54 | |
| 55 | ::android::status_t DriverStub::pause() { |
Mikhail Naganov | e48e573 | 2023-03-06 18:48:02 -0800 | [diff] [blame] | 56 | usleep(500); |
Mikhail Naganov | f429c03 | 2023-01-07 00:24:50 +0000 | [diff] [blame] | 57 | return ::android::OK; |
| 58 | } |
| 59 | |
| 60 | ::android::status_t DriverStub::transfer(void* buffer, size_t frameCount, size_t* actualFrameCount, |
| 61 | int32_t* latencyMs) { |
Mikhail Naganov | e48e573 | 2023-03-06 18:48:02 -0800 | [diff] [blame] | 62 | static constexpr float kMicrosPerSecond = MICROS_PER_SECOND; |
| 63 | static constexpr float kScaleFactor = .8f; |
| 64 | if (mIsAsynchronous) { |
| 65 | usleep(500); |
| 66 | } else { |
| 67 | const size_t delayUs = static_cast<size_t>( |
| 68 | std::roundf(kScaleFactor * frameCount * kMicrosPerSecond / mSampleRate)); |
| 69 | usleep(delayUs); |
| 70 | } |
Mikhail Naganov | f429c03 | 2023-01-07 00:24:50 +0000 | [diff] [blame] | 71 | if (mIsInput) { |
| 72 | uint8_t* byteBuffer = static_cast<uint8_t*>(buffer); |
| 73 | for (size_t i = 0; i < frameCount * mFrameSizeBytes; ++i) { |
| 74 | byteBuffer[i] = std::rand() % 255; |
| 75 | } |
| 76 | } |
| 77 | *actualFrameCount = frameCount; |
| 78 | *latencyMs = Module::kLatencyMs; |
| 79 | return ::android::OK; |
| 80 | } |
| 81 | |
| 82 | ::android::status_t DriverStub::standby() { |
Mikhail Naganov | e48e573 | 2023-03-06 18:48:02 -0800 | [diff] [blame] | 83 | usleep(500); |
Mikhail Naganov | f429c03 | 2023-01-07 00:24:50 +0000 | [diff] [blame] | 84 | return ::android::OK; |
| 85 | } |
| 86 | |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 87 | ::android::status_t DriverStub::setConnectedDevices( |
| 88 | const std::vector<AudioDevice>& connectedDevices __unused) { |
Mikhail Naganov | e48e573 | 2023-03-06 18:48:02 -0800 | [diff] [blame] | 89 | usleep(500); |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 90 | return ::android::OK; |
| 91 | } |
| 92 | |
Mikhail Naganov | f429c03 | 2023-01-07 00:24:50 +0000 | [diff] [blame] | 93 | // static |
| 94 | ndk::ScopedAStatus StreamInStub::createInstance(const SinkMetadata& sinkMetadata, |
| 95 | StreamContext&& context, |
| 96 | const std::vector<MicrophoneInfo>& microphones, |
| 97 | std::shared_ptr<StreamIn>* result) { |
| 98 | std::shared_ptr<StreamIn> stream = |
| 99 | ndk::SharedRefBase::make<StreamInStub>(sinkMetadata, std::move(context), microphones); |
| 100 | if (auto status = initInstance(stream); !status.isOk()) { |
| 101 | return status; |
| 102 | } |
| 103 | *result = std::move(stream); |
| 104 | return ndk::ScopedAStatus::ok(); |
| 105 | } |
| 106 | |
| 107 | StreamInStub::StreamInStub(const SinkMetadata& sinkMetadata, StreamContext&& context, |
| 108 | const std::vector<MicrophoneInfo>& microphones) |
| 109 | : StreamIn( |
| 110 | sinkMetadata, std::move(context), |
| 111 | [](const StreamContext& ctx) -> DriverInterface* { |
| 112 | return new DriverStub(ctx, true /*isInput*/); |
| 113 | }, |
| 114 | [](const StreamContext& ctx, DriverInterface* driver) -> StreamWorkerInterface* { |
| 115 | // The default worker implementation is used. |
| 116 | return new StreamInWorker(ctx, driver); |
| 117 | }, |
| 118 | microphones) {} |
| 119 | |
| 120 | // static |
| 121 | ndk::ScopedAStatus StreamOutStub::createInstance(const SourceMetadata& sourceMetadata, |
| 122 | StreamContext&& context, |
| 123 | const std::optional<AudioOffloadInfo>& offloadInfo, |
| 124 | std::shared_ptr<StreamOut>* result) { |
| 125 | std::shared_ptr<StreamOut> stream = ndk::SharedRefBase::make<StreamOutStub>( |
| 126 | sourceMetadata, std::move(context), offloadInfo); |
| 127 | if (auto status = initInstance(stream); !status.isOk()) { |
| 128 | return status; |
| 129 | } |
| 130 | *result = std::move(stream); |
| 131 | return ndk::ScopedAStatus::ok(); |
| 132 | } |
| 133 | |
| 134 | StreamOutStub::StreamOutStub(const SourceMetadata& sourceMetadata, StreamContext&& context, |
| 135 | const std::optional<AudioOffloadInfo>& offloadInfo) |
| 136 | : StreamOut( |
| 137 | sourceMetadata, std::move(context), |
| 138 | [](const StreamContext& ctx) -> DriverInterface* { |
| 139 | return new DriverStub(ctx, false /*isInput*/); |
| 140 | }, |
| 141 | [](const StreamContext& ctx, DriverInterface* driver) -> StreamWorkerInterface* { |
| 142 | // The default worker implementation is used. |
| 143 | return new StreamOutWorker(ctx, driver); |
| 144 | }, |
| 145 | offloadInfo) {} |
| 146 | |
| 147 | } // namespace aidl::android::hardware::audio::core |