2023-01-07 01:24:50 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2023 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-03-07 03:48:02 +01:00
|
|
|
#include <cmath>
|
|
|
|
|
2023-01-07 01:24:50 +01:00
|
|
|
#define LOG_TAG "AHAL_Stream"
|
|
|
|
#include <android-base/logging.h>
|
2023-03-07 03:48:02 +01:00
|
|
|
#include <audio_utils/clock.h>
|
2023-01-07 01:24:50 +01:00
|
|
|
|
|
|
|
#include "core-impl/Module.h"
|
|
|
|
#include "core-impl/StreamStub.h"
|
|
|
|
|
|
|
|
using aidl::android::hardware::audio::common::SinkMetadata;
|
|
|
|
using aidl::android::hardware::audio::common::SourceMetadata;
|
2023-01-26 00:57:31 +01:00
|
|
|
using aidl::android::media::audio::common::AudioDevice;
|
2023-01-07 01:24:50 +01:00
|
|
|
using aidl::android::media::audio::common::AudioOffloadInfo;
|
2023-02-10 02:52:50 +01:00
|
|
|
using aidl::android::media::audio::common::MicrophoneInfo;
|
2023-01-07 01:24:50 +01:00
|
|
|
|
|
|
|
namespace aidl::android::hardware::audio::core {
|
|
|
|
|
2023-06-22 00:20:31 +02:00
|
|
|
StreamStub::StreamStub(const Metadata& metadata, StreamContext&& context)
|
|
|
|
: StreamCommonImpl(metadata, std::move(context)),
|
2023-06-28 01:39:33 +02:00
|
|
|
mFrameSizeBytes(getContext().getFrameSize()),
|
|
|
|
mSampleRate(getContext().getSampleRate()),
|
|
|
|
mIsAsynchronous(!!getContext().getAsyncCallback()),
|
2023-06-22 00:20:31 +02:00
|
|
|
mIsInput(isInput(metadata)) {}
|
2023-01-07 01:24:50 +01:00
|
|
|
|
2023-06-22 00:20:31 +02:00
|
|
|
::android::status_t StreamStub::init() {
|
2023-06-28 01:39:33 +02:00
|
|
|
mIsInitialized = true;
|
2023-03-07 03:48:02 +01:00
|
|
|
usleep(500);
|
2023-01-07 01:24:50 +01:00
|
|
|
return ::android::OK;
|
|
|
|
}
|
|
|
|
|
2023-06-22 00:20:31 +02:00
|
|
|
::android::status_t StreamStub::drain(StreamDescriptor::DrainMode) {
|
2023-06-28 01:39:33 +02:00
|
|
|
if (!mIsInitialized) {
|
|
|
|
LOG(FATAL) << __func__ << ": must not happen for an uninitialized driver";
|
|
|
|
}
|
2023-03-07 03:48:02 +01:00
|
|
|
usleep(500);
|
2023-01-07 01:24:50 +01:00
|
|
|
return ::android::OK;
|
|
|
|
}
|
|
|
|
|
2023-06-22 00:20:31 +02:00
|
|
|
::android::status_t StreamStub::flush() {
|
2023-06-28 01:39:33 +02:00
|
|
|
if (!mIsInitialized) {
|
|
|
|
LOG(FATAL) << __func__ << ": must not happen for an uninitialized driver";
|
|
|
|
}
|
2023-03-07 03:48:02 +01:00
|
|
|
usleep(500);
|
2023-01-07 01:24:50 +01:00
|
|
|
return ::android::OK;
|
|
|
|
}
|
|
|
|
|
2023-06-22 00:20:31 +02:00
|
|
|
::android::status_t StreamStub::pause() {
|
2023-06-28 01:39:33 +02:00
|
|
|
if (!mIsInitialized) {
|
|
|
|
LOG(FATAL) << __func__ << ": must not happen for an uninitialized driver";
|
|
|
|
}
|
|
|
|
usleep(500);
|
|
|
|
return ::android::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
::android::status_t StreamStub::standby() {
|
|
|
|
if (!mIsInitialized) {
|
|
|
|
LOG(FATAL) << __func__ << ": must not happen for an uninitialized driver";
|
|
|
|
}
|
|
|
|
usleep(500);
|
|
|
|
mIsStandby = true;
|
|
|
|
return ::android::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
::android::status_t StreamStub::start() {
|
|
|
|
if (!mIsInitialized) {
|
|
|
|
LOG(FATAL) << __func__ << ": must not happen for an uninitialized driver";
|
|
|
|
}
|
2023-03-07 03:48:02 +01:00
|
|
|
usleep(500);
|
2023-06-28 01:39:33 +02:00
|
|
|
mIsStandby = false;
|
2023-01-07 01:24:50 +01:00
|
|
|
return ::android::OK;
|
|
|
|
}
|
|
|
|
|
2023-06-22 00:20:31 +02:00
|
|
|
::android::status_t StreamStub::transfer(void* buffer, size_t frameCount, size_t* actualFrameCount,
|
2023-01-07 01:24:50 +01:00
|
|
|
int32_t* latencyMs) {
|
2023-06-28 01:39:33 +02:00
|
|
|
if (!mIsInitialized) {
|
|
|
|
LOG(FATAL) << __func__ << ": must not happen for an uninitialized driver";
|
|
|
|
}
|
|
|
|
if (mIsStandby) {
|
|
|
|
LOG(FATAL) << __func__ << ": must not happen while in standby";
|
|
|
|
}
|
2023-03-07 03:48:02 +01:00
|
|
|
static constexpr float kMicrosPerSecond = MICROS_PER_SECOND;
|
|
|
|
static constexpr float kScaleFactor = .8f;
|
|
|
|
if (mIsAsynchronous) {
|
|
|
|
usleep(500);
|
|
|
|
} else {
|
|
|
|
const size_t delayUs = static_cast<size_t>(
|
|
|
|
std::roundf(kScaleFactor * frameCount * kMicrosPerSecond / mSampleRate));
|
|
|
|
usleep(delayUs);
|
|
|
|
}
|
2023-01-07 01:24:50 +01:00
|
|
|
if (mIsInput) {
|
|
|
|
uint8_t* byteBuffer = static_cast<uint8_t*>(buffer);
|
|
|
|
for (size_t i = 0; i < frameCount * mFrameSizeBytes; ++i) {
|
|
|
|
byteBuffer[i] = std::rand() % 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*actualFrameCount = frameCount;
|
|
|
|
*latencyMs = Module::kLatencyMs;
|
|
|
|
return ::android::OK;
|
|
|
|
}
|
|
|
|
|
2023-06-28 01:39:33 +02:00
|
|
|
void StreamStub::shutdown() {
|
|
|
|
mIsInitialized = false;
|
2023-01-07 01:24:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
StreamInStub::StreamInStub(const SinkMetadata& sinkMetadata, StreamContext&& context,
|
|
|
|
const std::vector<MicrophoneInfo>& microphones)
|
2023-06-22 00:20:31 +02:00
|
|
|
: StreamStub(sinkMetadata, std::move(context)), StreamIn(microphones) {}
|
2023-01-07 01:24:50 +01:00
|
|
|
|
|
|
|
StreamOutStub::StreamOutStub(const SourceMetadata& sourceMetadata, StreamContext&& context,
|
|
|
|
const std::optional<AudioOffloadInfo>& offloadInfo)
|
2023-06-22 00:20:31 +02:00
|
|
|
: StreamStub(sourceMetadata, std::move(context)), StreamOut(offloadInfo) {}
|
2023-01-07 01:24:50 +01:00
|
|
|
|
|
|
|
} // namespace aidl::android::hardware::audio::core
|