Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 1 | /* |
| 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 Naganov | b29438e | 2016-12-22 09:21:34 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 19 | |
Yifan Hong | f9d3034 | 2016-11-30 13:45:34 -0800 | [diff] [blame] | 20 | #include <android/log.h> |
Mikhail Naganov | b29438e | 2016-12-22 09:21:34 -0800 | [diff] [blame] | 21 | #include <hardware/audio.h> |
| 22 | #include <mediautils/SchedulingPolicyService.h> |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 23 | |
| 24 | #include "StreamIn.h" |
| 25 | |
Mikhail Naganov | b29438e | 2016-12-22 09:21:34 -0800 | [diff] [blame] | 26 | using ::android::hardware::audio::V2_0::MessageQueueFlagBits; |
| 27 | |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | namespace hardware { |
| 30 | namespace audio { |
| 31 | namespace V2_0 { |
| 32 | namespace implementation { |
| 33 | |
Mikhail Naganov | b29438e | 2016-12-22 09:21:34 -0800 | [diff] [blame] | 34 | namespace { |
| 35 | |
| 36 | class 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 | |
| 70 | status_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 | |
| 80 | bool 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 Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 115 | StreamIn::StreamIn(audio_hw_device_t* device, audio_stream_in_t* stream) |
Mikhail Naganov | b29438e | 2016-12-22 09:21:34 -0800 | [diff] [blame] | 116 | : mIsClosed(false), mDevice(device), mStream(stream), |
Eric Laurent | 7deb7da | 2016-12-15 19:15:45 -0800 | [diff] [blame] | 117 | mStreamCommon(new Stream(&stream->common)), |
Mikhail Naganov | b29438e | 2016-12-22 09:21:34 -0800 | [diff] [blame] | 118 | mStreamMmap(new StreamMmap<audio_stream_in_t>(stream)), |
| 119 | mEfGroup(nullptr), mStopReadThread(false) { |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | StreamIn::~StreamIn() { |
Mikhail Naganov | b29438e | 2016-12-22 09:21:34 -0800 | [diff] [blame] | 123 | close(); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 124 | mStream = nullptr; |
| 125 | mDevice = nullptr; |
| 126 | } |
| 127 | |
| 128 | // Methods from ::android::hardware::audio::V2_0::IStream follow. |
| 129 | Return<uint64_t> StreamIn::getFrameSize() { |
| 130 | return audio_stream_in_frame_size(mStream); |
| 131 | } |
| 132 | |
| 133 | Return<uint64_t> StreamIn::getFrameCount() { |
| 134 | return mStreamCommon->getFrameCount(); |
| 135 | } |
| 136 | |
| 137 | Return<uint64_t> StreamIn::getBufferSize() { |
| 138 | return mStreamCommon->getBufferSize(); |
| 139 | } |
| 140 | |
| 141 | Return<uint32_t> StreamIn::getSampleRate() { |
| 142 | return mStreamCommon->getSampleRate(); |
| 143 | } |
| 144 | |
| 145 | Return<void> StreamIn::getSupportedSampleRates(getSupportedSampleRates_cb _hidl_cb) { |
| 146 | return mStreamCommon->getSupportedSampleRates(_hidl_cb); |
| 147 | } |
| 148 | |
| 149 | Return<Result> StreamIn::setSampleRate(uint32_t sampleRateHz) { |
| 150 | return mStreamCommon->setSampleRate(sampleRateHz); |
| 151 | } |
| 152 | |
| 153 | Return<AudioChannelMask> StreamIn::getChannelMask() { |
| 154 | return mStreamCommon->getChannelMask(); |
| 155 | } |
| 156 | |
| 157 | Return<void> StreamIn::getSupportedChannelMasks(getSupportedChannelMasks_cb _hidl_cb) { |
| 158 | return mStreamCommon->getSupportedChannelMasks(_hidl_cb); |
| 159 | } |
| 160 | |
| 161 | Return<Result> StreamIn::setChannelMask(AudioChannelMask mask) { |
| 162 | return mStreamCommon->setChannelMask(mask); |
| 163 | } |
| 164 | |
| 165 | Return<AudioFormat> StreamIn::getFormat() { |
| 166 | return mStreamCommon->getFormat(); |
| 167 | } |
| 168 | |
| 169 | Return<void> StreamIn::getSupportedFormats(getSupportedFormats_cb _hidl_cb) { |
| 170 | return mStreamCommon->getSupportedFormats(_hidl_cb); |
| 171 | } |
| 172 | |
| 173 | Return<Result> StreamIn::setFormat(AudioFormat format) { |
| 174 | return mStreamCommon->setFormat(format); |
| 175 | } |
| 176 | |
| 177 | Return<void> StreamIn::getAudioProperties(getAudioProperties_cb _hidl_cb) { |
| 178 | return mStreamCommon->getAudioProperties(_hidl_cb); |
| 179 | } |
| 180 | |
| 181 | Return<Result> StreamIn::addEffect(uint64_t effectId) { |
| 182 | return mStreamCommon->addEffect(effectId); |
| 183 | } |
| 184 | |
| 185 | Return<Result> StreamIn::removeEffect(uint64_t effectId) { |
| 186 | return mStreamCommon->removeEffect(effectId); |
| 187 | } |
| 188 | |
| 189 | Return<Result> StreamIn::standby() { |
| 190 | return mStreamCommon->standby(); |
| 191 | } |
| 192 | |
| 193 | Return<AudioDevice> StreamIn::getDevice() { |
| 194 | return mStreamCommon->getDevice(); |
| 195 | } |
| 196 | |
| 197 | Return<Result> StreamIn::setDevice(const DeviceAddress& address) { |
| 198 | return mStreamCommon->setDevice(address); |
| 199 | } |
| 200 | |
| 201 | Return<Result> StreamIn::setConnectedState(const DeviceAddress& address, bool connected) { |
| 202 | return mStreamCommon->setConnectedState(address, connected); |
| 203 | } |
| 204 | |
| 205 | Return<Result> StreamIn::setHwAvSync(uint32_t hwAvSync) { |
| 206 | return mStreamCommon->setHwAvSync(hwAvSync); |
| 207 | } |
| 208 | |
| 209 | Return<void> StreamIn::getParameters(const hidl_vec<hidl_string>& keys, getParameters_cb _hidl_cb) { |
| 210 | return mStreamCommon->getParameters(keys, _hidl_cb); |
| 211 | } |
| 212 | |
| 213 | Return<Result> StreamIn::setParameters(const hidl_vec<ParameterValue>& parameters) { |
| 214 | return mStreamCommon->setParameters(parameters); |
| 215 | } |
| 216 | |
Martijn Coenen | 70b9a15 | 2016-11-18 15:29:32 +0100 | [diff] [blame] | 217 | Return<void> StreamIn::debugDump(const hidl_handle& fd) { |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 218 | return mStreamCommon->debugDump(fd); |
| 219 | } |
| 220 | |
Eric Laurent | 7deb7da | 2016-12-15 19:15:45 -0800 | [diff] [blame] | 221 | Return<Result> StreamIn::start() { |
| 222 | return mStreamMmap->start(); |
| 223 | } |
| 224 | |
| 225 | Return<Result> StreamIn::stop() { |
| 226 | return mStreamMmap->stop(); |
| 227 | } |
| 228 | |
| 229 | Return<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 | |
| 234 | Return<void> StreamIn::getMmapPosition(getMmapPosition_cb _hidl_cb) { |
| 235 | return mStreamMmap->getMmapPosition(_hidl_cb); |
| 236 | } |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 237 | |
Mikhail Naganov | b29438e | 2016-12-22 09:21:34 -0800 | [diff] [blame] | 238 | Return<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 Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 254 | // Methods from ::android::hardware::audio::V2_0::IStreamIn follow. |
| 255 | Return<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 | |
| 266 | Return<Result> StreamIn::setGain(float gain) { |
Eric Laurent | 7deb7da | 2016-12-15 19:15:45 -0800 | [diff] [blame] | 267 | return Stream::analyzeStatus("set_gain", mStream->set_gain(mStream, gain)); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Mikhail Naganov | b29438e | 2016-12-22 09:21:34 -0800 | [diff] [blame] | 270 | Return<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 Valsaraju | 790db10 | 2017-01-10 08:58:23 -0800 | [diff] [blame] | 278 | DataMQ::Descriptor(), StatusMQ::Descriptor()); |
Mikhail Naganov | b29438e | 2016-12-22 09:21:34 -0800 | [diff] [blame] | 279 | return Void(); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 280 | } |
Mikhail Naganov | b29438e | 2016-12-22 09:21:34 -0800 | [diff] [blame] | 281 | 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 Valsaraju | 790db10 | 2017-01-10 08:58:23 -0800 | [diff] [blame] | 288 | DataMQ::Descriptor(), StatusMQ::Descriptor()); |
Mikhail Naganov | b29438e | 2016-12-22 09:21:34 -0800 | [diff] [blame] | 289 | 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 Valsaraju | 790db10 | 2017-01-10 08:58:23 -0800 | [diff] [blame] | 296 | DataMQ::Descriptor(), StatusMQ::Descriptor()); |
Mikhail Naganov | b29438e | 2016-12-22 09:21:34 -0800 | [diff] [blame] | 297 | 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 Valsaraju | 790db10 | 2017-01-10 08:58:23 -0800 | [diff] [blame] | 312 | DataMQ::Descriptor(), StatusMQ::Descriptor()); |
Mikhail Naganov | b29438e | 2016-12-22 09:21:34 -0800 | [diff] [blame] | 313 | return Void(); |
| 314 | } |
| 315 | |
| 316 | mDataMQ = std::move(tempDataMQ); |
| 317 | mStatusMQ = std::move(tempStatusMQ); |
| 318 | _hidl_cb(Result::OK, *mDataMQ->getDesc(), *mStatusMQ->getDesc()); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 319 | return Void(); |
| 320 | } |
| 321 | |
| 322 | Return<uint32_t> StreamIn::getInputFramesLost() { |
| 323 | return mStream->get_input_frames_lost(mStream); |
| 324 | } |
| 325 | |
| 326 | Return<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 Laurent | 7deb7da | 2016-12-15 19:15:45 -0800 | [diff] [blame] | 331 | retval = Stream::analyzeStatus( |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 332 | "get_capture_position", |
Mikhail Naganov | ee901e3 | 2017-01-12 09:28:52 -0800 | [diff] [blame] | 333 | 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 Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 337 | 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 |