Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | #define LOG_TAG "AHAL_Stream" |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 18 | #include <android-base/logging.h> |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 19 | #include <utils/SystemClock.h> |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 20 | |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 21 | #include "core-impl/Module.h" |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 22 | #include "core-impl/Stream.h" |
| 23 | |
| 24 | using aidl::android::hardware::audio::common::SinkMetadata; |
| 25 | using aidl::android::hardware::audio::common::SourceMetadata; |
| 26 | using aidl::android::media::audio::common::AudioOffloadInfo; |
| 27 | |
| 28 | namespace aidl::android::hardware::audio::core { |
| 29 | |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 30 | void StreamContext::fillDescriptor(StreamDescriptor* desc) { |
| 31 | if (mCommandMQ) { |
| 32 | desc->command = mCommandMQ->dupeDesc(); |
| 33 | } |
| 34 | if (mReplyMQ) { |
| 35 | desc->reply = mReplyMQ->dupeDesc(); |
| 36 | } |
| 37 | if (mDataMQ) { |
Mikhail Naganov | a2c7141 | 2022-08-19 21:37:35 +0000 | [diff] [blame] | 38 | desc->frameSizeBytes = mFrameSize; |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 39 | desc->bufferSizeFrames = |
| 40 | mDataMQ->getQuantumCount() * mDataMQ->getQuantumSize() / mFrameSize; |
| 41 | desc->audio.set<StreamDescriptor::AudioBuffer::Tag::fmq>(mDataMQ->dupeDesc()); |
| 42 | } |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 43 | } |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 44 | |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 45 | bool StreamContext::isValid() const { |
| 46 | if (mCommandMQ && !mCommandMQ->isValid()) { |
| 47 | LOG(ERROR) << "command FMQ is invalid"; |
| 48 | return false; |
| 49 | } |
| 50 | if (mReplyMQ && !mReplyMQ->isValid()) { |
| 51 | LOG(ERROR) << "reply FMQ is invalid"; |
| 52 | return false; |
| 53 | } |
| 54 | if (mFrameSize == 0) { |
| 55 | LOG(ERROR) << "frame size is not set"; |
| 56 | return false; |
| 57 | } |
| 58 | if (mDataMQ && !mDataMQ->isValid()) { |
| 59 | LOG(ERROR) << "data FMQ is invalid"; |
| 60 | return false; |
| 61 | } |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | void StreamContext::reset() { |
| 66 | mCommandMQ.reset(); |
| 67 | mReplyMQ.reset(); |
| 68 | mDataMQ.reset(); |
| 69 | } |
| 70 | |
| 71 | std::string StreamWorkerCommonLogic::init() { |
| 72 | if (mCommandMQ == nullptr) return "Command MQ is null"; |
| 73 | if (mReplyMQ == nullptr) return "Reply MQ is null"; |
| 74 | if (mDataMQ == nullptr) return "Data MQ is null"; |
| 75 | if (sizeof(decltype(mDataBuffer)::element_type) != mDataMQ->getQuantumSize()) { |
| 76 | return "Unexpected Data MQ quantum size: " + std::to_string(mDataMQ->getQuantumSize()); |
| 77 | } |
| 78 | mDataBufferSize = mDataMQ->getQuantumCount() * mDataMQ->getQuantumSize(); |
| 79 | mDataBuffer.reset(new (std::nothrow) int8_t[mDataBufferSize]); |
| 80 | if (mDataBuffer == nullptr) { |
| 81 | return "Failed to allocate data buffer for element count " + |
| 82 | std::to_string(mDataMQ->getQuantumCount()) + |
| 83 | ", size in bytes: " + std::to_string(mDataBufferSize); |
| 84 | } |
| 85 | return ""; |
| 86 | } |
| 87 | |
Mikhail Naganov | cce8e5f | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 88 | void StreamWorkerCommonLogic::populateReply(StreamDescriptor::Reply* reply, |
| 89 | bool isConnected) const { |
| 90 | if (isConnected) { |
| 91 | reply->status = STATUS_OK; |
| 92 | reply->observable.frames = mFrameCount; |
| 93 | reply->observable.timeNs = ::android::elapsedRealtimeNano(); |
| 94 | } else { |
| 95 | reply->status = STATUS_NO_INIT; |
| 96 | } |
| 97 | } |
| 98 | |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 99 | const std::string StreamInWorkerLogic::kThreadName = "reader"; |
| 100 | |
| 101 | StreamInWorkerLogic::Status StreamInWorkerLogic::cycle() { |
| 102 | StreamDescriptor::Command command{}; |
| 103 | if (!mCommandMQ->readBlocking(&command, 1)) { |
| 104 | LOG(ERROR) << __func__ << ": reading of command from MQ failed"; |
Mikhail Naganov | cce8e5f | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 105 | mState = StreamDescriptor::State::ERROR; |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 106 | return Status::ABORT; |
| 107 | } |
| 108 | StreamDescriptor::Reply reply{}; |
Mikhail Naganov | 9833443 | 2022-11-09 02:44:32 +0000 | [diff] [blame] | 109 | reply.status = STATUS_BAD_VALUE; |
| 110 | using Tag = StreamDescriptor::Command::Tag; |
| 111 | switch (command.getTag()) { |
| 112 | case Tag::hal_reserved_exit: |
| 113 | if (const int32_t cookie = command.get<Tag::hal_reserved_exit>(); |
| 114 | cookie == mInternalCommandCookie) { |
| 115 | LOG(DEBUG) << __func__ << ": received EXIT command"; |
| 116 | setClosed(); |
| 117 | // This is an internal command, no need to reply. |
| 118 | return Status::EXIT; |
| 119 | } else { |
| 120 | LOG(WARNING) << __func__ << ": EXIT command has a bad cookie: " << cookie; |
Mikhail Naganov | cce8e5f | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 121 | } |
Mikhail Naganov | 9833443 | 2022-11-09 02:44:32 +0000 | [diff] [blame] | 122 | break; |
| 123 | case Tag::start: |
| 124 | LOG(DEBUG) << __func__ << ": received START read command"; |
| 125 | if (mState == StreamDescriptor::State::STANDBY || |
| 126 | mState == StreamDescriptor::State::DRAINING) { |
| 127 | populateReply(&reply, mIsConnected); |
| 128 | mState = mState == StreamDescriptor::State::STANDBY |
| 129 | ? StreamDescriptor::State::IDLE |
| 130 | : StreamDescriptor::State::ACTIVE; |
| 131 | } else { |
| 132 | LOG(WARNING) << __func__ << ": START command can not be handled in the state " |
| 133 | << toString(mState); |
| 134 | reply.status = STATUS_INVALID_OPERATION; |
| 135 | } |
| 136 | break; |
| 137 | case Tag::burst: |
| 138 | if (const int32_t fmqByteCount = command.get<Tag::burst>(); fmqByteCount >= 0) { |
| 139 | LOG(DEBUG) << __func__ << ": received BURST read command for " << fmqByteCount |
| 140 | << " bytes"; |
| 141 | if (mState == StreamDescriptor::State::IDLE || |
| 142 | mState == StreamDescriptor::State::ACTIVE || |
| 143 | mState == StreamDescriptor::State::PAUSED || |
| 144 | mState == StreamDescriptor::State::DRAINING) { |
| 145 | if (!read(fmqByteCount, &reply)) { |
| 146 | mState = StreamDescriptor::State::ERROR; |
| 147 | } |
| 148 | if (mState == StreamDescriptor::State::IDLE || |
| 149 | mState == StreamDescriptor::State::PAUSED) { |
| 150 | mState = StreamDescriptor::State::ACTIVE; |
| 151 | } else if (mState == StreamDescriptor::State::DRAINING) { |
| 152 | // To simplify the reference code, we assume that the read operation |
| 153 | // has consumed all the data remaining in the hardware buffer. |
| 154 | // TODO: Provide parametrization on the duration of draining to test |
| 155 | // handling of commands during the 'DRAINING' state. |
| 156 | mState = StreamDescriptor::State::STANDBY; |
| 157 | } |
| 158 | } else { |
| 159 | LOG(WARNING) << __func__ << ": BURST command can not be handled in the state " |
| 160 | << toString(mState); |
| 161 | reply.status = STATUS_INVALID_OPERATION; |
| 162 | } |
| 163 | } else { |
| 164 | LOG(WARNING) << __func__ << ": invalid burst byte count: " << fmqByteCount; |
| 165 | } |
| 166 | break; |
| 167 | case Tag::drain: |
| 168 | LOG(DEBUG) << __func__ << ": received DRAIN read command"; |
| 169 | if (mState == StreamDescriptor::State::ACTIVE) { |
| 170 | usleep(1000); // Simulate a blocking call into the driver. |
| 171 | populateReply(&reply, mIsConnected); |
| 172 | // Can switch the state to ERROR if a driver error occurs. |
| 173 | mState = StreamDescriptor::State::DRAINING; |
| 174 | } else { |
| 175 | LOG(WARNING) << __func__ << ": DRAIN command can not be handled in the state " |
| 176 | << toString(mState); |
| 177 | reply.status = STATUS_INVALID_OPERATION; |
| 178 | } |
| 179 | break; |
| 180 | case Tag::standby: |
| 181 | LOG(DEBUG) << __func__ << ": received STANDBY read command"; |
| 182 | if (mState == StreamDescriptor::State::IDLE) { |
| 183 | usleep(1000); // Simulate a blocking call into the driver. |
| 184 | populateReply(&reply, mIsConnected); |
| 185 | // Can switch the state to ERROR if a driver error occurs. |
Mikhail Naganov | cce8e5f | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 186 | mState = StreamDescriptor::State::STANDBY; |
Mikhail Naganov | 9833443 | 2022-11-09 02:44:32 +0000 | [diff] [blame] | 187 | } else { |
| 188 | LOG(WARNING) << __func__ << ": FLUSH command can not be handled in the state " |
| 189 | << toString(mState); |
| 190 | reply.status = STATUS_INVALID_OPERATION; |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 191 | } |
Mikhail Naganov | 9833443 | 2022-11-09 02:44:32 +0000 | [diff] [blame] | 192 | break; |
| 193 | case Tag::pause: |
| 194 | LOG(DEBUG) << __func__ << ": received PAUSE read command"; |
| 195 | if (mState == StreamDescriptor::State::ACTIVE) { |
| 196 | usleep(1000); // Simulate a blocking call into the driver. |
| 197 | populateReply(&reply, mIsConnected); |
| 198 | // Can switch the state to ERROR if a driver error occurs. |
| 199 | mState = StreamDescriptor::State::PAUSED; |
| 200 | } else { |
| 201 | LOG(WARNING) << __func__ << ": PAUSE command can not be handled in the state " |
| 202 | << toString(mState); |
| 203 | reply.status = STATUS_INVALID_OPERATION; |
| 204 | } |
| 205 | break; |
| 206 | case Tag::flush: |
| 207 | LOG(DEBUG) << __func__ << ": received FLUSH read command"; |
| 208 | if (mState == StreamDescriptor::State::PAUSED) { |
| 209 | usleep(1000); // Simulate a blocking call into the driver. |
| 210 | populateReply(&reply, mIsConnected); |
| 211 | // Can switch the state to ERROR if a driver error occurs. |
| 212 | mState = StreamDescriptor::State::STANDBY; |
| 213 | } else { |
| 214 | LOG(WARNING) << __func__ << ": FLUSH command can not be handled in the state " |
| 215 | << toString(mState); |
| 216 | reply.status = STATUS_INVALID_OPERATION; |
| 217 | } |
| 218 | break; |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 219 | } |
Mikhail Naganov | cce8e5f | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 220 | reply.state = mState; |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 221 | LOG(DEBUG) << __func__ << ": writing reply " << reply.toString(); |
| 222 | if (!mReplyMQ->writeBlocking(&reply, 1)) { |
| 223 | LOG(ERROR) << __func__ << ": writing of reply " << reply.toString() << " to MQ failed"; |
Mikhail Naganov | cce8e5f | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 224 | mState = StreamDescriptor::State::ERROR; |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 225 | return Status::ABORT; |
| 226 | } |
| 227 | return Status::CONTINUE; |
| 228 | } |
| 229 | |
Mikhail Naganov | cce8e5f | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 230 | bool StreamInWorkerLogic::read(size_t clientSize, StreamDescriptor::Reply* reply) { |
| 231 | // Can switch the state to ERROR if a driver error occurs. |
| 232 | const size_t byteCount = std::min({clientSize, mDataMQ->availableToWrite(), mDataBufferSize}); |
| 233 | const bool isConnected = mIsConnected; |
| 234 | bool fatal = false; |
| 235 | // Simulate reading of data, or provide zeroes if the stream is not connected. |
| 236 | for (size_t i = 0; i < byteCount; ++i) { |
| 237 | using buffer_type = decltype(mDataBuffer)::element_type; |
| 238 | constexpr int kBufferValueRange = std::numeric_limits<buffer_type>::max() - |
| 239 | std::numeric_limits<buffer_type>::min() + 1; |
| 240 | mDataBuffer[i] = isConnected ? (std::rand() % kBufferValueRange) + |
| 241 | std::numeric_limits<buffer_type>::min() |
| 242 | : 0; |
| 243 | } |
| 244 | usleep(3000); // Simulate a blocking call into the driver. |
| 245 | // Set 'fatal = true' if a driver error occurs. |
| 246 | if (bool success = byteCount > 0 ? mDataMQ->write(&mDataBuffer[0], byteCount) : true; success) { |
| 247 | LOG(DEBUG) << __func__ << ": writing of " << byteCount << " bytes into data MQ" |
| 248 | << " succeeded; connected? " << isConnected; |
| 249 | // Frames are provided and counted regardless of connection status. |
| 250 | reply->fmqByteCount += byteCount; |
| 251 | mFrameCount += byteCount / mFrameSize; |
| 252 | populateReply(reply, isConnected); |
| 253 | } else { |
| 254 | LOG(WARNING) << __func__ << ": writing of " << byteCount << " bytes of data to MQ failed"; |
| 255 | reply->status = STATUS_NOT_ENOUGH_DATA; |
| 256 | } |
| 257 | reply->latencyMs = Module::kLatencyMs; |
| 258 | return !fatal; |
| 259 | } |
| 260 | |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 261 | const std::string StreamOutWorkerLogic::kThreadName = "writer"; |
| 262 | |
| 263 | StreamOutWorkerLogic::Status StreamOutWorkerLogic::cycle() { |
| 264 | StreamDescriptor::Command command{}; |
| 265 | if (!mCommandMQ->readBlocking(&command, 1)) { |
| 266 | LOG(ERROR) << __func__ << ": reading of command from MQ failed"; |
Mikhail Naganov | cce8e5f | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 267 | mState = StreamDescriptor::State::ERROR; |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 268 | return Status::ABORT; |
| 269 | } |
| 270 | StreamDescriptor::Reply reply{}; |
Mikhail Naganov | 9833443 | 2022-11-09 02:44:32 +0000 | [diff] [blame] | 271 | reply.status = STATUS_BAD_VALUE; |
| 272 | using Tag = StreamDescriptor::Command::Tag; |
| 273 | switch (command.getTag()) { |
| 274 | case Tag::hal_reserved_exit: |
| 275 | if (const int32_t cookie = command.get<Tag::hal_reserved_exit>(); |
| 276 | cookie == mInternalCommandCookie) { |
| 277 | LOG(DEBUG) << __func__ << ": received EXIT command"; |
| 278 | setClosed(); |
| 279 | // This is an internal command, no need to reply. |
| 280 | return Status::EXIT; |
| 281 | } else { |
| 282 | LOG(WARNING) << __func__ << ": EXIT command has a bad cookie: " << cookie; |
| 283 | } |
| 284 | break; |
| 285 | case Tag::start: |
| 286 | LOG(DEBUG) << __func__ << ": received START write command"; |
| 287 | switch (mState) { |
| 288 | case StreamDescriptor::State::STANDBY: |
| 289 | mState = StreamDescriptor::State::IDLE; |
| 290 | break; |
| 291 | case StreamDescriptor::State::PAUSED: |
| 292 | mState = StreamDescriptor::State::ACTIVE; |
| 293 | break; |
| 294 | case StreamDescriptor::State::DRAIN_PAUSED: |
| 295 | mState = StreamDescriptor::State::PAUSED; |
| 296 | break; |
| 297 | default: |
| 298 | LOG(WARNING) << __func__ << ": START command can not be handled in the state " |
| 299 | << toString(mState); |
| 300 | reply.status = STATUS_INVALID_OPERATION; |
| 301 | } |
| 302 | if (reply.status != STATUS_INVALID_OPERATION) { |
| 303 | populateReply(&reply, mIsConnected); |
| 304 | } |
| 305 | break; |
| 306 | case Tag::burst: |
| 307 | if (const int32_t fmqByteCount = command.get<Tag::burst>(); fmqByteCount >= 0) { |
| 308 | LOG(DEBUG) << __func__ << ": received BURST write command for " << fmqByteCount |
| 309 | << " bytes"; |
| 310 | if (mState != |
| 311 | StreamDescriptor::State::ERROR) { // BURST can be handled in all valid states |
| 312 | if (!write(fmqByteCount, &reply)) { |
| 313 | mState = StreamDescriptor::State::ERROR; |
| 314 | } |
| 315 | if (mState == StreamDescriptor::State::STANDBY || |
| 316 | mState == StreamDescriptor::State::DRAIN_PAUSED) { |
| 317 | mState = StreamDescriptor::State::PAUSED; |
| 318 | } else if (mState == StreamDescriptor::State::IDLE || |
| 319 | mState == StreamDescriptor::State::DRAINING) { |
| 320 | mState = StreamDescriptor::State::ACTIVE; |
| 321 | } // When in 'ACTIVE' and 'PAUSED' do not need to change the state. |
| 322 | } else { |
| 323 | LOG(WARNING) << __func__ << ": BURST command can not be handled in the state " |
| 324 | << toString(mState); |
| 325 | reply.status = STATUS_INVALID_OPERATION; |
| 326 | } |
| 327 | } else { |
| 328 | LOG(WARNING) << __func__ << ": invalid burst byte count: " << fmqByteCount; |
| 329 | } |
| 330 | break; |
| 331 | case Tag::drain: |
| 332 | LOG(DEBUG) << __func__ << ": received DRAIN write command"; |
| 333 | if (mState == StreamDescriptor::State::ACTIVE) { |
| 334 | usleep(1000); // Simulate a blocking call into the driver. |
| 335 | populateReply(&reply, mIsConnected); |
| 336 | // Can switch the state to ERROR if a driver error occurs. |
Mikhail Naganov | cce8e5f | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 337 | mState = StreamDescriptor::State::IDLE; |
Mikhail Naganov | 9833443 | 2022-11-09 02:44:32 +0000 | [diff] [blame] | 338 | // Since there is no actual hardware that would be draining the buffer, |
| 339 | // in order to simplify the reference code, we assume that draining |
| 340 | // happens instantly, thus skipping the 'DRAINING' state. |
| 341 | // TODO: Provide parametrization on the duration of draining to test |
| 342 | // handling of commands during the 'DRAINING' state. |
| 343 | } else { |
| 344 | LOG(WARNING) << __func__ << ": DRAIN command can not be handled in the state " |
Mikhail Naganov | cce8e5f | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 345 | << toString(mState); |
| 346 | reply.status = STATUS_INVALID_OPERATION; |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 347 | } |
Mikhail Naganov | 9833443 | 2022-11-09 02:44:32 +0000 | [diff] [blame] | 348 | break; |
| 349 | case Tag::standby: |
| 350 | LOG(DEBUG) << __func__ << ": received STANDBY write command"; |
| 351 | if (mState == StreamDescriptor::State::IDLE) { |
| 352 | usleep(1000); // Simulate a blocking call into the driver. |
| 353 | populateReply(&reply, mIsConnected); |
| 354 | // Can switch the state to ERROR if a driver error occurs. |
| 355 | mState = StreamDescriptor::State::STANDBY; |
| 356 | } else { |
| 357 | LOG(WARNING) << __func__ << ": STANDBY command can not be handled in the state " |
| 358 | << toString(mState); |
| 359 | reply.status = STATUS_INVALID_OPERATION; |
| 360 | } |
| 361 | break; |
| 362 | case Tag::pause: |
| 363 | LOG(DEBUG) << __func__ << ": received PAUSE write command"; |
| 364 | if (mState == StreamDescriptor::State::ACTIVE || |
| 365 | mState == StreamDescriptor::State::DRAINING) { |
| 366 | populateReply(&reply, mIsConnected); |
| 367 | mState = mState == StreamDescriptor::State::ACTIVE |
| 368 | ? StreamDescriptor::State::PAUSED |
| 369 | : StreamDescriptor::State::DRAIN_PAUSED; |
| 370 | } else { |
| 371 | LOG(WARNING) << __func__ << ": PAUSE command can not be handled in the state " |
| 372 | << toString(mState); |
| 373 | reply.status = STATUS_INVALID_OPERATION; |
| 374 | } |
| 375 | break; |
| 376 | case Tag::flush: |
| 377 | LOG(DEBUG) << __func__ << ": received FLUSH write command"; |
| 378 | if (mState == StreamDescriptor::State::PAUSED || |
Mikhail Naganov | cce8e5f | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 379 | mState == StreamDescriptor::State::DRAIN_PAUSED) { |
Mikhail Naganov | 9833443 | 2022-11-09 02:44:32 +0000 | [diff] [blame] | 380 | populateReply(&reply, mIsConnected); |
| 381 | mState = StreamDescriptor::State::IDLE; |
| 382 | } else { |
| 383 | LOG(WARNING) << __func__ << ": FLUSH command can not be handled in the state " |
| 384 | << toString(mState); |
| 385 | reply.status = STATUS_INVALID_OPERATION; |
| 386 | } |
| 387 | break; |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 388 | } |
Mikhail Naganov | cce8e5f | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 389 | reply.state = mState; |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 390 | LOG(DEBUG) << __func__ << ": writing reply " << reply.toString(); |
| 391 | if (!mReplyMQ->writeBlocking(&reply, 1)) { |
| 392 | LOG(ERROR) << __func__ << ": writing of reply " << reply.toString() << " to MQ failed"; |
Mikhail Naganov | cce8e5f | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 393 | mState = StreamDescriptor::State::ERROR; |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 394 | return Status::ABORT; |
| 395 | } |
| 396 | return Status::CONTINUE; |
| 397 | } |
| 398 | |
Mikhail Naganov | cce8e5f | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 399 | bool StreamOutWorkerLogic::write(size_t clientSize, StreamDescriptor::Reply* reply) { |
| 400 | const size_t readByteCount = mDataMQ->availableToRead(); |
| 401 | // Amount of data that the HAL module is going to actually use. |
| 402 | const size_t byteCount = std::min({clientSize, readByteCount, mDataBufferSize}); |
| 403 | bool fatal = false; |
| 404 | if (bool success = readByteCount > 0 ? mDataMQ->read(&mDataBuffer[0], readByteCount) : true) { |
| 405 | const bool isConnected = mIsConnected; |
| 406 | LOG(DEBUG) << __func__ << ": reading of " << readByteCount << " bytes from data MQ" |
| 407 | << " succeeded; connected? " << isConnected; |
| 408 | // Frames are consumed and counted regardless of connection status. |
| 409 | reply->fmqByteCount += byteCount; |
| 410 | mFrameCount += byteCount / mFrameSize; |
| 411 | populateReply(reply, isConnected); |
| 412 | usleep(3000); // Simulate a blocking call into the driver. |
| 413 | // Set 'fatal = true' if a driver error occurs. |
| 414 | } else { |
| 415 | LOG(WARNING) << __func__ << ": reading of " << readByteCount |
| 416 | << " bytes of data from MQ failed"; |
| 417 | reply->status = STATUS_NOT_ENOUGH_DATA; |
| 418 | } |
| 419 | reply->latencyMs = Module::kLatencyMs; |
| 420 | return !fatal; |
| 421 | } |
| 422 | |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 423 | template <class Metadata, class StreamWorker> |
| 424 | StreamCommon<Metadata, StreamWorker>::~StreamCommon() { |
Mikhail Naganov | cce8e5f | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 425 | if (!isClosed()) { |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 426 | LOG(ERROR) << __func__ << ": stream was not closed prior to destruction, resource leak"; |
| 427 | stopWorker(); |
| 428 | // The worker and the context should clean up by themselves via destructors. |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | template <class Metadata, class StreamWorker> |
| 433 | ndk::ScopedAStatus StreamCommon<Metadata, StreamWorker>::close() { |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 434 | LOG(DEBUG) << __func__; |
Mikhail Naganov | cce8e5f | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 435 | if (!isClosed()) { |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 436 | stopWorker(); |
| 437 | LOG(DEBUG) << __func__ << ": joining the worker thread..."; |
| 438 | mWorker.stop(); |
| 439 | LOG(DEBUG) << __func__ << ": worker thread joined"; |
| 440 | mContext.reset(); |
Mikhail Naganov | cce8e5f | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 441 | mWorker.setClosed(); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 442 | return ndk::ScopedAStatus::ok(); |
| 443 | } else { |
| 444 | LOG(ERROR) << __func__ << ": stream was already closed"; |
| 445 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 446 | } |
| 447 | } |
| 448 | |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 449 | template <class Metadata, class StreamWorker> |
| 450 | void StreamCommon<Metadata, StreamWorker>::stopWorker() { |
| 451 | if (auto commandMQ = mContext.getCommandMQ(); commandMQ != nullptr) { |
Mikhail Naganov | cce8e5f | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 452 | LOG(DEBUG) << __func__ << ": asking the worker to exit..."; |
Mikhail Naganov | 9833443 | 2022-11-09 02:44:32 +0000 | [diff] [blame] | 453 | auto cmd = |
| 454 | StreamDescriptor::Command::make<StreamDescriptor::Command::Tag::hal_reserved_exit>( |
| 455 | mContext.getInternalCommandCookie()); |
Mikhail Naganov | cce8e5f | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 456 | // Note: never call 'pause' and 'resume' methods of StreamWorker |
| 457 | // in the HAL implementation. These methods are to be used by |
| 458 | // the client side only. Preventing the worker loop from running |
| 459 | // on the HAL side can cause a deadlock. |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 460 | if (!commandMQ->writeBlocking(&cmd, 1)) { |
| 461 | LOG(ERROR) << __func__ << ": failed to write exit command to the MQ"; |
| 462 | } |
| 463 | LOG(DEBUG) << __func__ << ": done"; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | template <class Metadata, class StreamWorker> |
| 468 | ndk::ScopedAStatus StreamCommon<Metadata, StreamWorker>::updateMetadata(const Metadata& metadata) { |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 469 | LOG(DEBUG) << __func__; |
Mikhail Naganov | cce8e5f | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 470 | if (!isClosed()) { |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 471 | mMetadata = metadata; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 472 | return ndk::ScopedAStatus::ok(); |
| 473 | } |
| 474 | LOG(ERROR) << __func__ << ": stream was closed"; |
| 475 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 476 | } |
| 477 | |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 478 | StreamIn::StreamIn(const SinkMetadata& sinkMetadata, StreamContext context) |
| 479 | : StreamCommon<SinkMetadata, StreamInWorker>(sinkMetadata, std::move(context)) { |
| 480 | LOG(DEBUG) << __func__; |
| 481 | } |
| 482 | |
| 483 | StreamOut::StreamOut(const SourceMetadata& sourceMetadata, StreamContext context, |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 484 | const std::optional<AudioOffloadInfo>& offloadInfo) |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 485 | : StreamCommon<SourceMetadata, StreamOutWorker>(sourceMetadata, std::move(context)), |
| 486 | mOffloadInfo(offloadInfo) { |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 487 | LOG(DEBUG) << __func__; |
| 488 | } |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 489 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 490 | } // namespace aidl::android::hardware::audio::core |