blob: ad189864017dc2eabb8fe1698fd62b50bf7810e6 [file] [log] [blame]
Mikhail Naganov10548292016-10-31 10:39:47 -07001/*
2 * Copyright (C) 2016 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 "StreamInHAL"
Mikhail Naganovb29438e2016-12-22 09:21:34 -080018//#define LOG_NDEBUG 0
Mikhail Naganov10548292016-10-31 10:39:47 -070019
Yifan Hongf9d30342016-11-30 13:45:34 -080020#include <android/log.h>
Mikhail Naganovb29438e2016-12-22 09:21:34 -080021#include <hardware/audio.h>
22#include <mediautils/SchedulingPolicyService.h>
Mikhail Naganov10548292016-10-31 10:39:47 -070023
24#include "StreamIn.h"
25
Mikhail Naganovb29438e2016-12-22 09:21:34 -080026using ::android::hardware::audio::V2_0::MessageQueueFlagBits;
27
Mikhail Naganov10548292016-10-31 10:39:47 -070028namespace android {
29namespace hardware {
30namespace audio {
31namespace V2_0 {
32namespace implementation {
33
Mikhail Naganovb29438e2016-12-22 09:21:34 -080034namespace {
35
36class ReadThread : public Thread {
37 public:
38 // ReadThread's lifespan never exceeds StreamIn's lifespan.
39 ReadThread(std::atomic<bool>* stop,
40 audio_stream_in_t* stream,
41 StreamIn::DataMQ* dataMQ,
42 StreamIn::StatusMQ* statusMQ,
43 EventFlag* efGroup,
44 ThreadPriority threadPriority)
45 : Thread(false /*canCallJava*/),
46 mStop(stop),
47 mStream(stream),
48 mDataMQ(dataMQ),
49 mStatusMQ(statusMQ),
50 mEfGroup(efGroup),
51 mThreadPriority(threadPriority),
52 mBuffer(new uint8_t[dataMQ->getQuantumCount()]) {
53 }
54 virtual ~ReadThread() {}
55
56 status_t readyToRun() override;
57
58 private:
59 std::atomic<bool>* mStop;
60 audio_stream_in_t* mStream;
61 StreamIn::DataMQ* mDataMQ;
62 StreamIn::StatusMQ* mStatusMQ;
63 EventFlag* mEfGroup;
64 ThreadPriority mThreadPriority;
65 std::unique_ptr<uint8_t[]> mBuffer;
66
67 bool threadLoop() override;
68};
69
70status_t ReadThread::readyToRun() {
71 if (mThreadPriority != ThreadPriority::NORMAL) {
72 int err = requestPriority(
73 getpid(), getTid(), static_cast<int>(mThreadPriority), true /*asynchronous*/);
74 ALOGW_IF(err, "failed to set priority %d for pid %d tid %d; error %d",
75 static_cast<int>(mThreadPriority), getpid(), getTid(), err);
76 }
77 return OK;
78}
79
80bool ReadThread::threadLoop() {
81 // This implementation doesn't return control back to the Thread until it decides to stop,
82 // as the Thread uses mutexes, and this can lead to priority inversion.
83 while(!std::atomic_load_explicit(mStop, std::memory_order_acquire)) {
84 // TODO: Remove manual event flag handling once blocking MQ is implemented. b/33815422
85 uint32_t efState = 0;
86 mEfGroup->wait(static_cast<uint32_t>(MessageQueueFlagBits::NOT_FULL), &efState, NS_PER_SEC);
87 if (!(efState & static_cast<uint32_t>(MessageQueueFlagBits::NOT_FULL))) {
88 continue; // Nothing to do.
89 }
90
91 const size_t availToWrite = mDataMQ->availableToWrite();
92 ssize_t readResult = mStream->read(mStream, &mBuffer[0], availToWrite);
93 Result retval = Result::OK;
94 uint64_t read = 0;
95 if (readResult >= 0) {
96 read = readResult;
97 if (!mDataMQ->write(&mBuffer[0], readResult)) {
98 ALOGW("data message queue write failed");
99 }
100 } else {
101 retval = Stream::analyzeStatus("read", readResult);
102 }
103 IStreamIn::ReadStatus status = { retval, read };
104 if (!mStatusMQ->write(&status)) {
105 ALOGW("status message queue write failed");
106 }
107 mEfGroup->wake(static_cast<uint32_t>(MessageQueueFlagBits::NOT_EMPTY));
108 }
109
110 return false;
111}
112
113} // namespace
114
Mikhail Naganov10548292016-10-31 10:39:47 -0700115StreamIn::StreamIn(audio_hw_device_t* device, audio_stream_in_t* stream)
Mikhail Naganovb29438e2016-12-22 09:21:34 -0800116 : mIsClosed(false), mDevice(device), mStream(stream),
Eric Laurent7deb7da2016-12-15 19:15:45 -0800117 mStreamCommon(new Stream(&stream->common)),
Mikhail Naganovb29438e2016-12-22 09:21:34 -0800118 mStreamMmap(new StreamMmap<audio_stream_in_t>(stream)),
119 mEfGroup(nullptr), mStopReadThread(false) {
Mikhail Naganov10548292016-10-31 10:39:47 -0700120}
121
122StreamIn::~StreamIn() {
Mikhail Naganovb29438e2016-12-22 09:21:34 -0800123 close();
Mikhail Naganov10548292016-10-31 10:39:47 -0700124 mStream = nullptr;
125 mDevice = nullptr;
126}
127
128// Methods from ::android::hardware::audio::V2_0::IStream follow.
129Return<uint64_t> StreamIn::getFrameSize() {
130 return audio_stream_in_frame_size(mStream);
131}
132
133Return<uint64_t> StreamIn::getFrameCount() {
134 return mStreamCommon->getFrameCount();
135}
136
137Return<uint64_t> StreamIn::getBufferSize() {
138 return mStreamCommon->getBufferSize();
139}
140
141Return<uint32_t> StreamIn::getSampleRate() {
142 return mStreamCommon->getSampleRate();
143}
144
145Return<void> StreamIn::getSupportedSampleRates(getSupportedSampleRates_cb _hidl_cb) {
146 return mStreamCommon->getSupportedSampleRates(_hidl_cb);
147}
148
149Return<Result> StreamIn::setSampleRate(uint32_t sampleRateHz) {
150 return mStreamCommon->setSampleRate(sampleRateHz);
151}
152
153Return<AudioChannelMask> StreamIn::getChannelMask() {
154 return mStreamCommon->getChannelMask();
155}
156
157Return<void> StreamIn::getSupportedChannelMasks(getSupportedChannelMasks_cb _hidl_cb) {
158 return mStreamCommon->getSupportedChannelMasks(_hidl_cb);
159}
160
161Return<Result> StreamIn::setChannelMask(AudioChannelMask mask) {
162 return mStreamCommon->setChannelMask(mask);
163}
164
165Return<AudioFormat> StreamIn::getFormat() {
166 return mStreamCommon->getFormat();
167}
168
169Return<void> StreamIn::getSupportedFormats(getSupportedFormats_cb _hidl_cb) {
170 return mStreamCommon->getSupportedFormats(_hidl_cb);
171}
172
173Return<Result> StreamIn::setFormat(AudioFormat format) {
174 return mStreamCommon->setFormat(format);
175}
176
177Return<void> StreamIn::getAudioProperties(getAudioProperties_cb _hidl_cb) {
178 return mStreamCommon->getAudioProperties(_hidl_cb);
179}
180
181Return<Result> StreamIn::addEffect(uint64_t effectId) {
182 return mStreamCommon->addEffect(effectId);
183}
184
185Return<Result> StreamIn::removeEffect(uint64_t effectId) {
186 return mStreamCommon->removeEffect(effectId);
187}
188
189Return<Result> StreamIn::standby() {
190 return mStreamCommon->standby();
191}
192
193Return<AudioDevice> StreamIn::getDevice() {
194 return mStreamCommon->getDevice();
195}
196
197Return<Result> StreamIn::setDevice(const DeviceAddress& address) {
198 return mStreamCommon->setDevice(address);
199}
200
201Return<Result> StreamIn::setConnectedState(const DeviceAddress& address, bool connected) {
202 return mStreamCommon->setConnectedState(address, connected);
203}
204
205Return<Result> StreamIn::setHwAvSync(uint32_t hwAvSync) {
206 return mStreamCommon->setHwAvSync(hwAvSync);
207}
208
209Return<void> StreamIn::getParameters(const hidl_vec<hidl_string>& keys, getParameters_cb _hidl_cb) {
210 return mStreamCommon->getParameters(keys, _hidl_cb);
211}
212
213Return<Result> StreamIn::setParameters(const hidl_vec<ParameterValue>& parameters) {
214 return mStreamCommon->setParameters(parameters);
215}
216
Martijn Coenen70b9a152016-11-18 15:29:32 +0100217Return<void> StreamIn::debugDump(const hidl_handle& fd) {
Mikhail Naganov10548292016-10-31 10:39:47 -0700218 return mStreamCommon->debugDump(fd);
219}
220
Eric Laurent7deb7da2016-12-15 19:15:45 -0800221Return<Result> StreamIn::start() {
222 return mStreamMmap->start();
223}
224
225Return<Result> StreamIn::stop() {
226 return mStreamMmap->stop();
227}
228
229Return<void> StreamIn::createMmapBuffer(int32_t minSizeFrames, createMmapBuffer_cb _hidl_cb) {
230 return mStreamMmap->createMmapBuffer(
231 minSizeFrames, audio_stream_in_frame_size(mStream), _hidl_cb);
232}
233
234Return<void> StreamIn::getMmapPosition(getMmapPosition_cb _hidl_cb) {
235 return mStreamMmap->getMmapPosition(_hidl_cb);
236}
Mikhail Naganov10548292016-10-31 10:39:47 -0700237
Mikhail Naganovb29438e2016-12-22 09:21:34 -0800238Return<Result> StreamIn::close() {
239 if (mIsClosed) return Result::INVALID_STATE;
240 mIsClosed = true;
241 if (mReadThread.get()) {
242 mStopReadThread.store(true, std::memory_order_release);
243 status_t status = mReadThread->requestExitAndWait();
244 ALOGE_IF(status, "read thread exit error: %s", strerror(-status));
245 }
246 if (mEfGroup) {
247 status_t status = EventFlag::deleteEventFlag(&mEfGroup);
248 ALOGE_IF(status, "read MQ event flag deletion error: %s", strerror(-status));
249 }
250 mDevice->close_input_stream(mDevice, mStream);
251 return Result::OK;
252}
253
Mikhail Naganov10548292016-10-31 10:39:47 -0700254// Methods from ::android::hardware::audio::V2_0::IStreamIn follow.
255Return<void> StreamIn::getAudioSource(getAudioSource_cb _hidl_cb) {
256 int halSource;
257 Result retval = mStreamCommon->getParam(AudioParameter::keyInputSource, &halSource);
258 AudioSource source(AudioSource::DEFAULT);
259 if (retval == Result::OK) {
260 source = AudioSource(halSource);
261 }
262 _hidl_cb(retval, source);
263 return Void();
264}
265
266Return<Result> StreamIn::setGain(float gain) {
Eric Laurent7deb7da2016-12-15 19:15:45 -0800267 return Stream::analyzeStatus("set_gain", mStream->set_gain(mStream, gain));
Mikhail Naganov10548292016-10-31 10:39:47 -0700268}
269
Mikhail Naganovb29438e2016-12-22 09:21:34 -0800270Return<void> StreamIn::prepareForReading(
271 uint32_t frameSize, uint32_t framesCount, ThreadPriority threadPriority,
272 prepareForReading_cb _hidl_cb) {
273 status_t status;
274 // Create message queues.
275 if (mDataMQ) {
276 ALOGE("the client attempts to call prepareForReading twice");
277 _hidl_cb(Result::INVALID_STATE,
Hridya Valsaraju790db102017-01-10 08:58:23 -0800278 DataMQ::Descriptor(), StatusMQ::Descriptor());
Mikhail Naganovb29438e2016-12-22 09:21:34 -0800279 return Void();
Mikhail Naganov10548292016-10-31 10:39:47 -0700280 }
Mikhail Naganovb29438e2016-12-22 09:21:34 -0800281 std::unique_ptr<DataMQ> tempDataMQ(
282 new DataMQ(frameSize * framesCount, true /* EventFlag */));
283 std::unique_ptr<StatusMQ> tempStatusMQ(new StatusMQ(1));
284 if (!tempDataMQ->isValid() || !tempStatusMQ->isValid()) {
285 ALOGE_IF(!tempDataMQ->isValid(), "data MQ is invalid");
286 ALOGE_IF(!tempStatusMQ->isValid(), "status MQ is invalid");
287 _hidl_cb(Result::INVALID_ARGUMENTS,
Hridya Valsaraju790db102017-01-10 08:58:23 -0800288 DataMQ::Descriptor(), StatusMQ::Descriptor());
Mikhail Naganovb29438e2016-12-22 09:21:34 -0800289 return Void();
290 }
291 // TODO: Remove event flag management once blocking MQ is implemented. b/33815422
292 status = EventFlag::createEventFlag(tempDataMQ->getEventFlagWord(), &mEfGroup);
293 if (status != OK || !mEfGroup) {
294 ALOGE("failed creating event flag for data MQ: %s", strerror(-status));
295 _hidl_cb(Result::INVALID_ARGUMENTS,
Hridya Valsaraju790db102017-01-10 08:58:23 -0800296 DataMQ::Descriptor(), StatusMQ::Descriptor());
Mikhail Naganovb29438e2016-12-22 09:21:34 -0800297 return Void();
298 }
299
300 // Create and launch the thread.
301 mReadThread = new ReadThread(
302 &mStopReadThread,
303 mStream,
304 tempDataMQ.get(),
305 tempStatusMQ.get(),
306 mEfGroup,
307 threadPriority);
308 status = mReadThread->run("reader", PRIORITY_URGENT_AUDIO);
309 if (status != OK) {
310 ALOGW("failed to start reader thread: %s", strerror(-status));
311 _hidl_cb(Result::INVALID_ARGUMENTS,
Hridya Valsaraju790db102017-01-10 08:58:23 -0800312 DataMQ::Descriptor(), StatusMQ::Descriptor());
Mikhail Naganovb29438e2016-12-22 09:21:34 -0800313 return Void();
314 }
315
316 mDataMQ = std::move(tempDataMQ);
317 mStatusMQ = std::move(tempStatusMQ);
318 _hidl_cb(Result::OK, *mDataMQ->getDesc(), *mStatusMQ->getDesc());
Mikhail Naganov10548292016-10-31 10:39:47 -0700319 return Void();
320}
321
322Return<uint32_t> StreamIn::getInputFramesLost() {
323 return mStream->get_input_frames_lost(mStream);
324}
325
326Return<void> StreamIn::getCapturePosition(getCapturePosition_cb _hidl_cb) {
327 Result retval(Result::NOT_SUPPORTED);
328 uint64_t frames = 0, time = 0;
329 if (mStream->get_capture_position != NULL) {
330 int64_t halFrames, halTime;
Eric Laurent7deb7da2016-12-15 19:15:45 -0800331 retval = Stream::analyzeStatus(
Mikhail Naganov10548292016-10-31 10:39:47 -0700332 "get_capture_position",
Mikhail Naganovee901e32017-01-12 09:28:52 -0800333 mStream->get_capture_position(mStream, &halFrames, &halTime),
334 // HAL may have a stub function, always returning ENOSYS, don't
335 // spam the log in this case.
336 ENOSYS);
Mikhail Naganov10548292016-10-31 10:39:47 -0700337 if (retval == Result::OK) {
338 frames = halFrames;
339 time = halTime;
340 }
341 }
342 _hidl_cb(retval, frames, time);
343 return Void();
344}
345
346} // namespace implementation
347} // namespace V2_0
348} // namespace audio
349} // namespace hardware
350} // namespace android