blob: 21dc4b6c082215ddf50309219e790b0e687d2dbe [file] [log] [blame]
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001/*
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 Naganovdf5adfd2021-11-11 22:09:22 +000018#include <android-base/logging.h>
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000019#include <utils/SystemClock.h>
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000020
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000021#include "core-impl/Module.h"
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000022#include "core-impl/Stream.h"
23
24using aidl::android::hardware::audio::common::SinkMetadata;
25using aidl::android::hardware::audio::common::SourceMetadata;
26using aidl::android::media::audio::common::AudioOffloadInfo;
27
28namespace aidl::android::hardware::audio::core {
29
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000030void 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 Naganova2c71412022-08-19 21:37:35 +000038 desc->frameSizeBytes = mFrameSize;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000039 desc->bufferSizeFrames =
40 mDataMQ->getQuantumCount() * mDataMQ->getQuantumSize() / mFrameSize;
41 desc->audio.set<StreamDescriptor::AudioBuffer::Tag::fmq>(mDataMQ->dupeDesc());
42 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000043}
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000044
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000045bool 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
65void StreamContext::reset() {
66 mCommandMQ.reset();
67 mReplyMQ.reset();
68 mDataMQ.reset();
69}
70
71std::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 Naganovcce8e5f2022-09-13 01:20:45 +000088void 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 Naganov4f5d3f12022-07-22 23:23:25 +000099const std::string StreamInWorkerLogic::kThreadName = "reader";
100
101StreamInWorkerLogic::Status StreamInWorkerLogic::cycle() {
102 StreamDescriptor::Command command{};
103 if (!mCommandMQ->readBlocking(&command, 1)) {
104 LOG(ERROR) << __func__ << ": reading of command from MQ failed";
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000105 mState = StreamDescriptor::State::ERROR;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000106 return Status::ABORT;
107 }
108 StreamDescriptor::Reply reply{};
Mikhail Naganov98334432022-11-09 02:44:32 +0000109 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 Naganovcce8e5f2022-09-13 01:20:45 +0000121 }
Mikhail Naganov98334432022-11-09 02:44:32 +0000122 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 Naganovcce8e5f2022-09-13 01:20:45 +0000186 mState = StreamDescriptor::State::STANDBY;
Mikhail Naganov98334432022-11-09 02:44:32 +0000187 } else {
188 LOG(WARNING) << __func__ << ": FLUSH command can not be handled in the state "
189 << toString(mState);
190 reply.status = STATUS_INVALID_OPERATION;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000191 }
Mikhail Naganov98334432022-11-09 02:44:32 +0000192 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 Naganov4f5d3f12022-07-22 23:23:25 +0000219 }
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000220 reply.state = mState;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000221 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 Naganovcce8e5f2022-09-13 01:20:45 +0000224 mState = StreamDescriptor::State::ERROR;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000225 return Status::ABORT;
226 }
227 return Status::CONTINUE;
228}
229
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000230bool 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 Naganov4f5d3f12022-07-22 23:23:25 +0000261const std::string StreamOutWorkerLogic::kThreadName = "writer";
262
263StreamOutWorkerLogic::Status StreamOutWorkerLogic::cycle() {
264 StreamDescriptor::Command command{};
265 if (!mCommandMQ->readBlocking(&command, 1)) {
266 LOG(ERROR) << __func__ << ": reading of command from MQ failed";
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000267 mState = StreamDescriptor::State::ERROR;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000268 return Status::ABORT;
269 }
270 StreamDescriptor::Reply reply{};
Mikhail Naganov98334432022-11-09 02:44:32 +0000271 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 Naganovcce8e5f2022-09-13 01:20:45 +0000337 mState = StreamDescriptor::State::IDLE;
Mikhail Naganov98334432022-11-09 02:44:32 +0000338 // 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 Naganovcce8e5f2022-09-13 01:20:45 +0000345 << toString(mState);
346 reply.status = STATUS_INVALID_OPERATION;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000347 }
Mikhail Naganov98334432022-11-09 02:44:32 +0000348 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 Naganovcce8e5f2022-09-13 01:20:45 +0000379 mState == StreamDescriptor::State::DRAIN_PAUSED) {
Mikhail Naganov98334432022-11-09 02:44:32 +0000380 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 Naganov4f5d3f12022-07-22 23:23:25 +0000388 }
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000389 reply.state = mState;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000390 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 Naganovcce8e5f2022-09-13 01:20:45 +0000393 mState = StreamDescriptor::State::ERROR;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000394 return Status::ABORT;
395 }
396 return Status::CONTINUE;
397}
398
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000399bool 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 Naganov4f5d3f12022-07-22 23:23:25 +0000423template <class Metadata, class StreamWorker>
424StreamCommon<Metadata, StreamWorker>::~StreamCommon() {
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000425 if (!isClosed()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000426 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
432template <class Metadata, class StreamWorker>
433ndk::ScopedAStatus StreamCommon<Metadata, StreamWorker>::close() {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000434 LOG(DEBUG) << __func__;
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000435 if (!isClosed()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000436 stopWorker();
437 LOG(DEBUG) << __func__ << ": joining the worker thread...";
438 mWorker.stop();
439 LOG(DEBUG) << __func__ << ": worker thread joined";
440 mContext.reset();
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000441 mWorker.setClosed();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000442 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 Naganov4f5d3f12022-07-22 23:23:25 +0000449template <class Metadata, class StreamWorker>
450void StreamCommon<Metadata, StreamWorker>::stopWorker() {
451 if (auto commandMQ = mContext.getCommandMQ(); commandMQ != nullptr) {
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000452 LOG(DEBUG) << __func__ << ": asking the worker to exit...";
Mikhail Naganov98334432022-11-09 02:44:32 +0000453 auto cmd =
454 StreamDescriptor::Command::make<StreamDescriptor::Command::Tag::hal_reserved_exit>(
455 mContext.getInternalCommandCookie());
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000456 // 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 Naganov4f5d3f12022-07-22 23:23:25 +0000460 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
467template <class Metadata, class StreamWorker>
468ndk::ScopedAStatus StreamCommon<Metadata, StreamWorker>::updateMetadata(const Metadata& metadata) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000469 LOG(DEBUG) << __func__;
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000470 if (!isClosed()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000471 mMetadata = metadata;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000472 return ndk::ScopedAStatus::ok();
473 }
474 LOG(ERROR) << __func__ << ": stream was closed";
475 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
476}
477
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000478StreamIn::StreamIn(const SinkMetadata& sinkMetadata, StreamContext context)
479 : StreamCommon<SinkMetadata, StreamInWorker>(sinkMetadata, std::move(context)) {
480 LOG(DEBUG) << __func__;
481}
482
483StreamOut::StreamOut(const SourceMetadata& sourceMetadata, StreamContext context,
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000484 const std::optional<AudioOffloadInfo>& offloadInfo)
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000485 : StreamCommon<SourceMetadata, StreamOutWorker>(sourceMetadata, std::move(context)),
486 mOffloadInfo(offloadInfo) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000487 LOG(DEBUG) << __func__;
488}
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000489
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000490} // namespace aidl::android::hardware::audio::core