blob: 7b544a19a9ff89eba82f77a94a8ae5bc9ef7a88c [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 Naganovcce8e5f2022-09-13 01:20:45 +0000109 if (static_cast<int32_t>(command.code) == StreamContext::COMMAND_EXIT &&
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000110 command.fmqByteCount == mInternalCommandCookie) {
111 LOG(DEBUG) << __func__ << ": received EXIT command";
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000112 setClosed();
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000113 // This is an internal command, no need to reply.
114 return Status::EXIT;
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000115 } else if (command.code == StreamDescriptor::CommandCode::START && command.fmqByteCount >= 0) {
116 LOG(DEBUG) << __func__ << ": received START read command";
117 if (mState == StreamDescriptor::State::STANDBY ||
118 mState == StreamDescriptor::State::DRAINING) {
119 populateReply(&reply, mIsConnected);
120 mState = mState == StreamDescriptor::State::STANDBY ? StreamDescriptor::State::IDLE
121 : StreamDescriptor::State::ACTIVE;
122 } else {
123 LOG(WARNING) << __func__ << ": START command can not be handled in the state "
124 << toString(mState);
125 reply.status = STATUS_INVALID_OPERATION;
126 }
127 } else if (command.code == StreamDescriptor::CommandCode::BURST && command.fmqByteCount >= 0) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000128 LOG(DEBUG) << __func__ << ": received BURST read command for " << command.fmqByteCount
129 << " bytes";
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000130 if (mState == StreamDescriptor::State::IDLE || mState == StreamDescriptor::State::ACTIVE ||
131 mState == StreamDescriptor::State::PAUSED ||
132 mState == StreamDescriptor::State::DRAINING) {
133 if (!read(command.fmqByteCount, &reply)) {
134 mState = StreamDescriptor::State::ERROR;
135 }
136 if (mState == StreamDescriptor::State::IDLE ||
137 mState == StreamDescriptor::State::PAUSED) {
138 mState = StreamDescriptor::State::ACTIVE;
139 } else if (mState == StreamDescriptor::State::DRAINING) {
140 // To simplify the reference code, we assume that the read operation
141 // has consumed all the data remaining in the hardware buffer.
142 // TODO: Provide parametrization on the duration of draining to test
143 // handling of commands during the 'DRAINING' state.
144 mState = StreamDescriptor::State::STANDBY;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000145 }
146 } else {
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000147 LOG(WARNING) << __func__ << ": BURST command can not be handled in the state "
148 << toString(mState);
149 reply.status = STATUS_INVALID_OPERATION;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000150 }
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000151 } else if (command.code == StreamDescriptor::CommandCode::DRAIN && command.fmqByteCount == 0) {
152 LOG(DEBUG) << __func__ << ": received DRAIN read command";
153 if (mState == StreamDescriptor::State::ACTIVE) {
154 usleep(1000); // Simulate a blocking call into the driver.
155 populateReply(&reply, mIsConnected);
156 // Can switch the state to ERROR if a driver error occurs.
157 mState = StreamDescriptor::State::DRAINING;
158 } else {
159 LOG(WARNING) << __func__ << ": DRAIN command can not be handled in the state "
160 << toString(mState);
161 reply.status = STATUS_INVALID_OPERATION;
162 }
163 } else if (command.code == StreamDescriptor::CommandCode::PAUSE && command.fmqByteCount == 0) {
164 LOG(DEBUG) << __func__ << ": received PAUSE read command";
165 if (mState == StreamDescriptor::State::ACTIVE) {
166 usleep(1000); // Simulate a blocking call into the driver.
167 populateReply(&reply, mIsConnected);
168 // Can switch the state to ERROR if a driver error occurs.
169 mState = StreamDescriptor::State::PAUSED;
170 } else {
171 LOG(WARNING) << __func__ << ": PAUSE command can not be handled in the state "
172 << toString(mState);
173 reply.status = STATUS_INVALID_OPERATION;
174 }
175 } else if (command.code == StreamDescriptor::CommandCode::FLUSH && command.fmqByteCount == 0) {
176 LOG(DEBUG) << __func__ << ": received FLUSH read command";
177 if (mState == StreamDescriptor::State::PAUSED) {
178 usleep(1000); // Simulate a blocking call into the driver.
179 populateReply(&reply, mIsConnected);
180 // Can switch the state to ERROR if a driver error occurs.
181 mState = StreamDescriptor::State::STANDBY;
182 } else {
183 LOG(WARNING) << __func__ << ": FLUSH command can not be handled in the state "
184 << toString(mState);
185 reply.status = STATUS_INVALID_OPERATION;
186 }
187 } else if (command.code == StreamDescriptor::CommandCode::STANDBY &&
188 command.fmqByteCount == 0) {
189 LOG(DEBUG) << __func__ << ": received STANDBY read command";
190 if (mState == StreamDescriptor::State::IDLE) {
191 usleep(1000); // Simulate a blocking call into the driver.
192 populateReply(&reply, mIsConnected);
193 // Can switch the state to ERROR if a driver error occurs.
194 mState = StreamDescriptor::State::STANDBY;
195 } else {
196 LOG(WARNING) << __func__ << ": FLUSH command can not be handled in the state "
197 << toString(mState);
198 reply.status = STATUS_INVALID_OPERATION;
199 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000200 } else {
201 LOG(WARNING) << __func__ << ": invalid command (" << command.toString()
202 << ") or count: " << command.fmqByteCount;
203 reply.status = STATUS_BAD_VALUE;
204 }
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000205 reply.state = mState;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000206 LOG(DEBUG) << __func__ << ": writing reply " << reply.toString();
207 if (!mReplyMQ->writeBlocking(&reply, 1)) {
208 LOG(ERROR) << __func__ << ": writing of reply " << reply.toString() << " to MQ failed";
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000209 mState = StreamDescriptor::State::ERROR;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000210 return Status::ABORT;
211 }
212 return Status::CONTINUE;
213}
214
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000215bool StreamInWorkerLogic::read(size_t clientSize, StreamDescriptor::Reply* reply) {
216 // Can switch the state to ERROR if a driver error occurs.
217 const size_t byteCount = std::min({clientSize, mDataMQ->availableToWrite(), mDataBufferSize});
218 const bool isConnected = mIsConnected;
219 bool fatal = false;
220 // Simulate reading of data, or provide zeroes if the stream is not connected.
221 for (size_t i = 0; i < byteCount; ++i) {
222 using buffer_type = decltype(mDataBuffer)::element_type;
223 constexpr int kBufferValueRange = std::numeric_limits<buffer_type>::max() -
224 std::numeric_limits<buffer_type>::min() + 1;
225 mDataBuffer[i] = isConnected ? (std::rand() % kBufferValueRange) +
226 std::numeric_limits<buffer_type>::min()
227 : 0;
228 }
229 usleep(3000); // Simulate a blocking call into the driver.
230 // Set 'fatal = true' if a driver error occurs.
231 if (bool success = byteCount > 0 ? mDataMQ->write(&mDataBuffer[0], byteCount) : true; success) {
232 LOG(DEBUG) << __func__ << ": writing of " << byteCount << " bytes into data MQ"
233 << " succeeded; connected? " << isConnected;
234 // Frames are provided and counted regardless of connection status.
235 reply->fmqByteCount += byteCount;
236 mFrameCount += byteCount / mFrameSize;
237 populateReply(reply, isConnected);
238 } else {
239 LOG(WARNING) << __func__ << ": writing of " << byteCount << " bytes of data to MQ failed";
240 reply->status = STATUS_NOT_ENOUGH_DATA;
241 }
242 reply->latencyMs = Module::kLatencyMs;
243 return !fatal;
244}
245
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000246const std::string StreamOutWorkerLogic::kThreadName = "writer";
247
248StreamOutWorkerLogic::Status StreamOutWorkerLogic::cycle() {
249 StreamDescriptor::Command command{};
250 if (!mCommandMQ->readBlocking(&command, 1)) {
251 LOG(ERROR) << __func__ << ": reading of command from MQ failed";
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000252 mState = StreamDescriptor::State::ERROR;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000253 return Status::ABORT;
254 }
255 StreamDescriptor::Reply reply{};
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000256 if (static_cast<int32_t>(command.code) == StreamContext::COMMAND_EXIT &&
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000257 command.fmqByteCount == mInternalCommandCookie) {
258 LOG(DEBUG) << __func__ << ": received EXIT command";
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000259 setClosed();
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000260 // This is an internal command, no need to reply.
261 return Status::EXIT;
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000262 } else if (command.code == StreamDescriptor::CommandCode::START && command.fmqByteCount >= 0) {
263 LOG(DEBUG) << __func__ << ": received START read command";
264 switch (mState) {
265 case StreamDescriptor::State::STANDBY:
266 mState = StreamDescriptor::State::IDLE;
267 break;
268 case StreamDescriptor::State::PAUSED:
269 mState = StreamDescriptor::State::ACTIVE;
270 break;
271 case StreamDescriptor::State::DRAIN_PAUSED:
272 mState = StreamDescriptor::State::PAUSED;
273 break;
274 default:
275 LOG(WARNING) << __func__ << ": START command can not be handled in the state "
276 << toString(mState);
277 reply.status = STATUS_INVALID_OPERATION;
278 }
279 if (reply.status != STATUS_INVALID_OPERATION) {
280 populateReply(&reply, mIsConnected);
281 }
282 } else if (command.code == StreamDescriptor::CommandCode::BURST && command.fmqByteCount >= 0) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000283 LOG(DEBUG) << __func__ << ": received BURST write command for " << command.fmqByteCount
284 << " bytes";
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000285 if (mState != StreamDescriptor::State::ERROR) { // BURST can be handled in all valid states
286 if (!write(command.fmqByteCount, &reply)) {
287 mState = StreamDescriptor::State::ERROR;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000288 }
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000289 if (mState == StreamDescriptor::State::STANDBY ||
290 mState == StreamDescriptor::State::DRAIN_PAUSED) {
291 mState = StreamDescriptor::State::PAUSED;
292 } else if (mState == StreamDescriptor::State::IDLE ||
293 mState == StreamDescriptor::State::DRAINING) {
294 mState = StreamDescriptor::State::ACTIVE;
295 } // When in 'ACTIVE' and 'PAUSED' do not need to change the state.
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000296 } else {
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000297 LOG(WARNING) << __func__ << ": BURST command can not be handled in the state "
298 << toString(mState);
299 reply.status = STATUS_INVALID_OPERATION;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000300 }
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000301 } else if (command.code == StreamDescriptor::CommandCode::DRAIN && command.fmqByteCount == 0) {
302 LOG(DEBUG) << __func__ << ": received DRAIN write command";
303 if (mState == StreamDescriptor::State::ACTIVE) {
304 usleep(1000); // Simulate a blocking call into the driver.
305 populateReply(&reply, mIsConnected);
306 // Can switch the state to ERROR if a driver error occurs.
307 mState = StreamDescriptor::State::IDLE;
308 // Since there is no actual hardware that would be draining the buffer,
309 // in order to simplify the reference code, we assume that draining
310 // happens instantly, thus skipping the 'DRAINING' state.
311 // TODO: Provide parametrization on the duration of draining to test
312 // handling of commands during the 'DRAINING' state.
313 } else {
314 LOG(WARNING) << __func__ << ": DRAIN command can not be handled in the state "
315 << toString(mState);
316 reply.status = STATUS_INVALID_OPERATION;
317 }
318 } else if (command.code == StreamDescriptor::CommandCode::STANDBY &&
319 command.fmqByteCount == 0) {
320 LOG(DEBUG) << __func__ << ": received STANDBY write command";
321 if (mState == StreamDescriptor::State::IDLE) {
322 usleep(1000); // Simulate a blocking call into the driver.
323 populateReply(&reply, mIsConnected);
324 // Can switch the state to ERROR if a driver error occurs.
325 mState = StreamDescriptor::State::STANDBY;
326 } else {
327 LOG(WARNING) << __func__ << ": STANDBY command can not be handled in the state "
328 << toString(mState);
329 reply.status = STATUS_INVALID_OPERATION;
330 }
331 } else if (command.code == StreamDescriptor::CommandCode::PAUSE && command.fmqByteCount == 0) {
332 LOG(DEBUG) << __func__ << ": received PAUSE write command";
333 if (mState == StreamDescriptor::State::ACTIVE ||
334 mState == StreamDescriptor::State::DRAINING) {
335 populateReply(&reply, mIsConnected);
336 mState = mState == StreamDescriptor::State::ACTIVE
337 ? StreamDescriptor::State::PAUSED
338 : StreamDescriptor::State::DRAIN_PAUSED;
339 } else {
340 LOG(WARNING) << __func__ << ": PAUSE command can not be handled in the state "
341 << toString(mState);
342 reply.status = STATUS_INVALID_OPERATION;
343 }
344 } else if (command.code == StreamDescriptor::CommandCode::FLUSH && command.fmqByteCount == 0) {
345 LOG(DEBUG) << __func__ << ": received FLUSH write command";
346 if (mState == StreamDescriptor::State::PAUSED ||
347 mState == StreamDescriptor::State::DRAIN_PAUSED) {
348 populateReply(&reply, mIsConnected);
349 mState = StreamDescriptor::State::IDLE;
350 } else {
351 LOG(WARNING) << __func__ << ": FLUSH command can not be handled in the state "
352 << toString(mState);
353 reply.status = STATUS_INVALID_OPERATION;
354 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000355 } else {
356 LOG(WARNING) << __func__ << ": invalid command (" << command.toString()
357 << ") or count: " << command.fmqByteCount;
358 reply.status = STATUS_BAD_VALUE;
359 }
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000360 reply.state = mState;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000361 LOG(DEBUG) << __func__ << ": writing reply " << reply.toString();
362 if (!mReplyMQ->writeBlocking(&reply, 1)) {
363 LOG(ERROR) << __func__ << ": writing of reply " << reply.toString() << " to MQ failed";
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000364 mState = StreamDescriptor::State::ERROR;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000365 return Status::ABORT;
366 }
367 return Status::CONTINUE;
368}
369
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000370bool StreamOutWorkerLogic::write(size_t clientSize, StreamDescriptor::Reply* reply) {
371 const size_t readByteCount = mDataMQ->availableToRead();
372 // Amount of data that the HAL module is going to actually use.
373 const size_t byteCount = std::min({clientSize, readByteCount, mDataBufferSize});
374 bool fatal = false;
375 if (bool success = readByteCount > 0 ? mDataMQ->read(&mDataBuffer[0], readByteCount) : true) {
376 const bool isConnected = mIsConnected;
377 LOG(DEBUG) << __func__ << ": reading of " << readByteCount << " bytes from data MQ"
378 << " succeeded; connected? " << isConnected;
379 // Frames are consumed and counted regardless of connection status.
380 reply->fmqByteCount += byteCount;
381 mFrameCount += byteCount / mFrameSize;
382 populateReply(reply, isConnected);
383 usleep(3000); // Simulate a blocking call into the driver.
384 // Set 'fatal = true' if a driver error occurs.
385 } else {
386 LOG(WARNING) << __func__ << ": reading of " << readByteCount
387 << " bytes of data from MQ failed";
388 reply->status = STATUS_NOT_ENOUGH_DATA;
389 }
390 reply->latencyMs = Module::kLatencyMs;
391 return !fatal;
392}
393
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000394template <class Metadata, class StreamWorker>
395StreamCommon<Metadata, StreamWorker>::~StreamCommon() {
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000396 if (!isClosed()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000397 LOG(ERROR) << __func__ << ": stream was not closed prior to destruction, resource leak";
398 stopWorker();
399 // The worker and the context should clean up by themselves via destructors.
400 }
401}
402
403template <class Metadata, class StreamWorker>
404ndk::ScopedAStatus StreamCommon<Metadata, StreamWorker>::close() {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000405 LOG(DEBUG) << __func__;
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000406 if (!isClosed()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000407 stopWorker();
408 LOG(DEBUG) << __func__ << ": joining the worker thread...";
409 mWorker.stop();
410 LOG(DEBUG) << __func__ << ": worker thread joined";
411 mContext.reset();
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000412 mWorker.setClosed();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000413 return ndk::ScopedAStatus::ok();
414 } else {
415 LOG(ERROR) << __func__ << ": stream was already closed";
416 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
417 }
418}
419
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000420template <class Metadata, class StreamWorker>
421void StreamCommon<Metadata, StreamWorker>::stopWorker() {
422 if (auto commandMQ = mContext.getCommandMQ(); commandMQ != nullptr) {
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000423 LOG(DEBUG) << __func__ << ": asking the worker to exit...";
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000424 StreamDescriptor::Command cmd;
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000425 cmd.code = StreamDescriptor::CommandCode(StreamContext::COMMAND_EXIT);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000426 cmd.fmqByteCount = mContext.getInternalCommandCookie();
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000427 // Note: never call 'pause' and 'resume' methods of StreamWorker
428 // in the HAL implementation. These methods are to be used by
429 // the client side only. Preventing the worker loop from running
430 // on the HAL side can cause a deadlock.
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000431 if (!commandMQ->writeBlocking(&cmd, 1)) {
432 LOG(ERROR) << __func__ << ": failed to write exit command to the MQ";
433 }
434 LOG(DEBUG) << __func__ << ": done";
435 }
436}
437
438template <class Metadata, class StreamWorker>
439ndk::ScopedAStatus StreamCommon<Metadata, StreamWorker>::updateMetadata(const Metadata& metadata) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000440 LOG(DEBUG) << __func__;
Mikhail Naganovcce8e5f2022-09-13 01:20:45 +0000441 if (!isClosed()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000442 mMetadata = metadata;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000443 return ndk::ScopedAStatus::ok();
444 }
445 LOG(ERROR) << __func__ << ": stream was closed";
446 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
447}
448
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000449StreamIn::StreamIn(const SinkMetadata& sinkMetadata, StreamContext context)
450 : StreamCommon<SinkMetadata, StreamInWorker>(sinkMetadata, std::move(context)) {
451 LOG(DEBUG) << __func__;
452}
453
454StreamOut::StreamOut(const SourceMetadata& sourceMetadata, StreamContext context,
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000455 const std::optional<AudioOffloadInfo>& offloadInfo)
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000456 : StreamCommon<SourceMetadata, StreamOutWorker>(sourceMetadata, std::move(context)),
457 mOffloadInfo(offloadInfo) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000458 LOG(DEBUG) << __func__;
459}
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000460
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000461} // namespace aidl::android::hardware::audio::core