1 /*
2  * Copyright (C) 2020 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 #include <log/log.h>
18 #include <fmq/EventFlag.h>
19 #include <fmq/MessageQueue.h>
20 #include <hidl/MQDescriptor.h>
21 #include <hidl/Status.h>
22 #include <utils/ThreadDefs.h>
23 #include <future>
24 #include <thread>
25 #include PATH(APM_XSD_ENUMS_H_FILENAME)
26 #include "stream_in.h"
27 #include "device_port_source.h"
28 #include "deleters.h"
29 #include "audio_ops.h"
30 #include "util.h"
31 #include "debug.h"
32 
33 namespace xsd {
34 using namespace ::android::audio::policy::configuration::CPP_VERSION;
35 }
36 
37 namespace android {
38 namespace hardware {
39 namespace audio {
40 namespace CPP_VERSION {
41 namespace implementation {
42 
43 using ::android::hardware::Void;
44 
45 namespace {
46 
47 struct ReadThread : public IOThread {
48     typedef MessageQueue<IStreamIn::ReadParameters, kSynchronizedReadWrite> CommandMQ;
49     typedef MessageQueue<IStreamIn::ReadStatus, kSynchronizedReadWrite> StatusMQ;
50     typedef MessageQueue<uint8_t, kSynchronizedReadWrite> DataMQ;
51 
ReadThreadandroid::hardware::audio::CPP_VERSION::implementation::__anonb5c4a37d0111::ReadThread52     ReadThread(StreamIn *stream, const size_t bufferSize)
53             : mStream(stream)
54             , mCommandMQ(1)
55             , mStatusMQ(1)
56             , mDataMQ(bufferSize, true /* EventFlag */) {
57         if (!mCommandMQ.isValid()) {
58             ALOGE("ReadThread::%s:%d: mCommandMQ is invalid", __func__, __LINE__);
59             return;
60         }
61         if (!mDataMQ.isValid()) {
62             ALOGE("ReadThread::%s:%d: mDataMQ is invalid", __func__, __LINE__);
63             return;
64         }
65         if (!mStatusMQ.isValid()) {
66             ALOGE("ReadThread::%s:%d: mStatusMQ is invalid", __func__, __LINE__);
67             return;
68         }
69 
70         status_t status;
71 
72         EventFlag* rawEfGroup = nullptr;
73         status = EventFlag::createEventFlag(mDataMQ.getEventFlagWord(), &rawEfGroup);
74         if (status != OK || !rawEfGroup) {
75             ALOGE("ReadThread::%s:%d: rawEfGroup is invalid", __func__, __LINE__);
76             return;
77         } else {
78             mEfGroup.reset(rawEfGroup);
79         }
80 
81         mThread = std::thread(&ReadThread::threadLoop, this);
82     }
83 
~ReadThreadandroid::hardware::audio::CPP_VERSION::implementation::__anonb5c4a37d0111::ReadThread84     ~ReadThread() {
85         if (mThread.joinable()) {
86             requestExit();
87             mThread.join();
88         }
89     }
90 
getEventFlagandroid::hardware::audio::CPP_VERSION::implementation::__anonb5c4a37d0111::ReadThread91     EventFlag *getEventFlag() override {
92         return mEfGroup.get();
93     }
94 
isRunningandroid::hardware::audio::CPP_VERSION::implementation::__anonb5c4a37d0111::ReadThread95     bool isRunning() const {
96         return mThread.joinable();
97     }
98 
getTidandroid::hardware::audio::CPP_VERSION::implementation::__anonb5c4a37d0111::ReadThread99     std::future<pthread_t> getTid() {
100         return mTid.get_future();
101     }
102 
threadLoopandroid::hardware::audio::CPP_VERSION::implementation::__anonb5c4a37d0111::ReadThread103     void threadLoop() {
104         util::setThreadPriority(SP_AUDIO_SYS, PRIORITY_AUDIO);
105         mTid.set_value(pthread_self());
106 
107         while (true) {
108             uint32_t efState = 0;
109             mEfGroup->wait(MessageQueueFlagBits::NOT_FULL | EXIT_REQUEST | STAND_BY_REQUEST,
110                            &efState);
111             if (efState & EXIT_REQUEST) {
112                 return;
113             }
114 
115             if (efState & STAND_BY_REQUEST) {
116                 mSource.reset();
117             }
118 
119             if (efState & (MessageQueueFlagBits::NOT_FULL | 0)) {
120                 if (!mSource) {
121                     mSource = DevicePortSource::create(mDataMQ.getQuantumCount(),
122                                                        mStream->getDeviceAddress(),
123                                                        mStream->getAudioConfig(),
124                                                        mStream->getAudioOutputFlags(),
125                                                        mStream->getFrameCounter());
126                     LOG_ALWAYS_FATAL_IF(!mSource);
127                 }
128 
129                 processCommand();
130             }
131         }
132     }
133 
processCommandandroid::hardware::audio::CPP_VERSION::implementation::__anonb5c4a37d0111::ReadThread134     void processCommand() {
135         IStreamIn::ReadParameters rParameters;
136 
137         if (!mCommandMQ.read(&rParameters)) {
138             return;  // Nothing to do.
139         }
140 
141         IStreamIn::ReadStatus rStatus;
142         switch (rParameters.command) {
143             case IStreamIn::ReadCommand::READ:
144                 rStatus = doRead(rParameters);
145                 break;
146 
147             case IStreamIn::ReadCommand::GET_CAPTURE_POSITION:
148                 rStatus = doGetCapturePosition();
149                 break;
150 
151             default:
152                 ALOGE("ReadThread::%s:%d: Unknown read thread command code %d",
153                       __func__, __LINE__, rParameters.command);
154                 rStatus.retval = FAILURE(Result::NOT_SUPPORTED);
155                 break;
156         }
157 
158         rStatus.replyTo = rParameters.command;
159 
160         if (!mStatusMQ.write(&rStatus)) {
161             ALOGE("ReadThread::%s:%d: status message queue write failed", __func__, __LINE__);
162         }
163 
164         mEfGroup->wake(MessageQueueFlagBits::NOT_EMPTY | 0);
165     }
166 
doReadandroid::hardware::audio::CPP_VERSION::implementation::__anonb5c4a37d0111::ReadThread167     IStreamIn::ReadStatus doRead(const IStreamIn::ReadParameters &rParameters) {
168         struct MQWriter : public IWriter {
169             explicit MQWriter(DataMQ &mq) : dataMQ(mq) {}
170 
171             size_t operator()(const void *dst, size_t sz) override {
172                 if (dataMQ.write(static_cast<const uint8_t *>(dst), sz)) {
173                     totalWritten += sz;
174                     return sz;
175                 } else {
176                     ALOGE("WriteThread::%s:%d: DataMQ::write failed",
177                           __func__, __LINE__);
178                     return 0;
179                 }
180             }
181 
182             size_t totalWritten = 0;
183             DataMQ &dataMQ;
184         };
185 
186         const size_t bytesToRead = std::min(mDataMQ.availableToWrite(),
187                                             static_cast<size_t>(rParameters.params.read));
188 
189         MQWriter writer(mDataMQ);
190         const size_t framesLost =
191             mSource->read(mStream->getEffectiveVolume(), bytesToRead, writer);
192         if (framesLost > 0) {
193             mStream->addInputFramesLost(framesLost);
194         }
195 
196         IStreamIn::ReadStatus status;
197         status.retval = Result::OK;
198         status.reply.read = writer.totalWritten;
199         return status;
200     }
201 
doGetCapturePositionandroid::hardware::audio::CPP_VERSION::implementation::__anonb5c4a37d0111::ReadThread202     IStreamIn::ReadStatus doGetCapturePosition() {
203         IStreamIn::ReadStatus status;
204 
205         status.retval = mSource->getCapturePosition(
206             status.reply.capturePosition.frames,
207             status.reply.capturePosition.time);
208 
209         return status;
210     }
211 
212     StreamIn *const mStream;
213     CommandMQ mCommandMQ;
214     StatusMQ mStatusMQ;
215     DataMQ mDataMQ;
216     std::unique_ptr<EventFlag, deleters::forEventFlag> mEfGroup;
217     std::unique_ptr<DevicePortSource> mSource;
218     std::thread mThread;
219     std::promise<pthread_t> mTid;
220 };
221 
222 } // namespace
223 
StreamIn(sp<Device> dev,int32_t ioHandle,const DeviceAddress & device,const AudioConfig & config,hidl_vec<AudioInOutFlag> flags,const SinkMetadata & sinkMetadata)224 StreamIn::StreamIn(sp<Device> dev,
225                    int32_t ioHandle,
226                    const DeviceAddress& device,
227                    const AudioConfig& config,
228                    hidl_vec<AudioInOutFlag> flags,
229                    const SinkMetadata& sinkMetadata)
230         : mDev(std::move(dev))
231         , mCommon(ioHandle, device, config, std::move(flags))
232         , mSinkMetadata(sinkMetadata) {
233 }
234 
~StreamIn()235 StreamIn::~StreamIn() {
236     closeImpl(true);
237 }
238 
getFrameSize()239 Return<uint64_t> StreamIn::getFrameSize() {
240     return mCommon.getFrameSize();
241 }
242 
getFrameCount()243 Return<uint64_t> StreamIn::getFrameCount() {
244     return mCommon.getFrameCount();
245 }
246 
getBufferSize()247 Return<uint64_t> StreamIn::getBufferSize() {
248     return mCommon.getBufferSize();
249 }
250 
getSupportedProfiles(getSupportedProfiles_cb _hidl_cb)251 Return<void> StreamIn::getSupportedProfiles(getSupportedProfiles_cb _hidl_cb) {
252     mCommon.getSupportedProfiles(_hidl_cb);
253     return Void();
254 }
255 
getAudioProperties(getAudioProperties_cb _hidl_cb)256 Return<void> StreamIn::getAudioProperties(getAudioProperties_cb _hidl_cb) {
257     mCommon.getAudioProperties(_hidl_cb);
258     return Void();
259 }
260 
setAudioProperties(const AudioConfigBaseOptional & config)261 Return<Result> StreamIn::setAudioProperties(const AudioConfigBaseOptional& config) {
262     (void)config;
263     return FAILURE(Result::NOT_SUPPORTED);
264 }
265 
addEffect(uint64_t effectId)266 Return<Result> StreamIn::addEffect(uint64_t effectId) {
267     (void)effectId;
268     return FAILURE(Result::INVALID_ARGUMENTS);
269 }
270 
removeEffect(uint64_t effectId)271 Return<Result> StreamIn::removeEffect(uint64_t effectId) {
272     (void)effectId;
273     return FAILURE(Result::INVALID_ARGUMENTS);
274 }
275 
standby()276 Return<Result> StreamIn::standby() {
277     if (mReadThread) {
278         LOG_ALWAYS_FATAL_IF(!mReadThread->standby());
279     }
280 
281     return Result::OK;
282 }
283 
getDevices(getDevices_cb _hidl_cb)284 Return<void> StreamIn::getDevices(getDevices_cb _hidl_cb) {
285     mCommon.getDevices(_hidl_cb);
286     return Void();
287 }
288 
setDevices(const hidl_vec<DeviceAddress> & devices)289 Return<Result> StreamIn::setDevices(const hidl_vec<DeviceAddress>& devices) {
290     return mCommon.setDevices(devices);
291 }
292 
getParameters(const hidl_vec<ParameterValue> & context,const hidl_vec<hidl_string> & keys,getParameters_cb _hidl_cb)293 Return<void> StreamIn::getParameters(const hidl_vec<ParameterValue>& context,
294                                      const hidl_vec<hidl_string>& keys,
295                                      getParameters_cb _hidl_cb) {
296     (void)context;
297     _hidl_cb((keys.size() > 0) ? Result::NOT_SUPPORTED : Result::OK, {});
298     return Void();
299 }
300 
setParameters(const hidl_vec<ParameterValue> & context,const hidl_vec<ParameterValue> & parameters)301 Return<Result> StreamIn::setParameters(const hidl_vec<ParameterValue>& context,
302                                        const hidl_vec<ParameterValue>& parameters) {
303     (void)context;
304     (void)parameters;
305     return Result::OK;
306 }
307 
setHwAvSync(uint32_t hwAvSync)308 Return<Result> StreamIn::setHwAvSync(uint32_t hwAvSync) {
309     (void)hwAvSync;
310     return FAILURE(Result::NOT_SUPPORTED);
311 }
312 
start()313 Return<Result> StreamIn::start() {
314     return FAILURE(Result::NOT_SUPPORTED);
315 }
316 
stop()317 Return<Result> StreamIn::stop() {
318     return FAILURE(Result::NOT_SUPPORTED);
319 }
320 
createMmapBuffer(int32_t minSizeFrames,createMmapBuffer_cb _hidl_cb)321 Return<void> StreamIn::createMmapBuffer(int32_t minSizeFrames,
322                                         createMmapBuffer_cb _hidl_cb) {
323     (void)minSizeFrames;
324     _hidl_cb(FAILURE(Result::NOT_SUPPORTED), {});
325     return Void();
326 }
327 
getMmapPosition(getMmapPosition_cb _hidl_cb)328 Return<void> StreamIn::getMmapPosition(getMmapPosition_cb _hidl_cb) {
329     _hidl_cb(FAILURE(Result::NOT_SUPPORTED), {});
330     return Void();
331 }
332 
closeImpl(const bool fromDctor)333 Result StreamIn::closeImpl(const bool fromDctor) {
334     if (mDev) {
335         mReadThread.reset();
336         mDev->unrefDevice(this);
337         mDev = nullptr;
338         return Result::OK;
339     } else if (fromDctor) {
340         // closeImpl is always called from the dctor, it is ok if mDev is null,
341         // we don't want to log the error in this case.
342         return Result::OK;
343     } else {
344         return FAILURE(Result::INVALID_STATE);
345     }
346 }
347 
close()348 Return<Result> StreamIn::close() {
349     return closeImpl(false);
350 }
351 
getAudioSource(getAudioSource_cb _hidl_cb)352 Return<void> StreamIn::getAudioSource(getAudioSource_cb _hidl_cb) {
353     _hidl_cb(FAILURE(Result::NOT_SUPPORTED), {});
354     return Void();
355 }
356 
setGain(float gain)357 Return<Result> StreamIn::setGain(float gain) {
358     (void)gain;
359     return FAILURE(Result::NOT_SUPPORTED);
360 }
361 
updateSinkMetadata(const SinkMetadata & sinkMetadata)362 Return<Result> StreamIn::updateSinkMetadata(const SinkMetadata& sinkMetadata) {
363     (void)sinkMetadata;
364     return FAILURE(Result::NOT_SUPPORTED);
365 }
366 
prepareForReading(uint32_t frameSize,uint32_t framesCount,prepareForReading_cb _hidl_cb)367 Return<void> StreamIn::prepareForReading(uint32_t frameSize,
368                                          uint32_t framesCount,
369                                          prepareForReading_cb _hidl_cb) {
370     if (!frameSize || !framesCount || frameSize > 256 || framesCount > (1u << 20)) {
371         _hidl_cb(FAILURE(Result::INVALID_ARGUMENTS), {}, {}, {}, -1);
372         return Void();
373     }
374 
375     if (mReadThread) {  // INVALID_STATE if the method was already called.
376         _hidl_cb(FAILURE(Result::INVALID_STATE), {}, {}, {}, -1);
377         return Void();
378     }
379 
380     auto t = std::make_unique<ReadThread>(this, frameSize * framesCount);
381 
382     if (t->isRunning()) {
383         _hidl_cb(Result::OK,
384                  *(t->mCommandMQ.getDesc()),
385                  *(t->mDataMQ.getDesc()),
386                  *(t->mStatusMQ.getDesc()),
387                  t->getTid().get());
388 
389         mReadThread = std::move(t);
390     } else {
391         _hidl_cb(FAILURE(Result::INVALID_ARGUMENTS), {}, {}, {}, -1);
392     }
393 
394     return Void();
395 }
396 
getInputFramesLost()397 Return<uint32_t> StreamIn::getInputFramesLost() {
398     return 0;
399 }
400 
getCapturePosition(getCapturePosition_cb _hidl_cb)401 Return<void> StreamIn::getCapturePosition(getCapturePosition_cb _hidl_cb) {
402     const auto r = static_cast<ReadThread*>(mReadThread.get());
403     if (!r) {
404         _hidl_cb(FAILURE(Result::INVALID_STATE), {}, {});
405         return Void();
406     }
407 
408     const auto s = r->mSource.get();
409     if (!s) {
410         _hidl_cb(Result::OK, mFrames, systemTime(SYSTEM_TIME_MONOTONIC));
411     } else {
412         uint64_t frames;
413         uint64_t time;
414         const Result r = s->getCapturePosition(frames, time);
415         _hidl_cb(r, frames, time);
416     }
417 
418     return Void();
419 }
420 
getActiveMicrophones(getActiveMicrophones_cb _hidl_cb)421 Return<void> StreamIn::getActiveMicrophones(getActiveMicrophones_cb _hidl_cb) {
422     _hidl_cb(Result::OK, {util::getMicrophoneInfo()});
423     return Void();
424 }
425 
setMicrophoneDirection(MicrophoneDirection direction)426 Return<Result> StreamIn::setMicrophoneDirection(MicrophoneDirection direction) {
427     (void)direction;
428     return Result::NOT_SUPPORTED;
429 }
430 
setMicrophoneFieldDimension(float zoom)431 Return<Result> StreamIn::setMicrophoneFieldDimension(float zoom) {
432     (void)zoom;
433     return Result::NOT_SUPPORTED;
434 }
435 
setMicMute(bool mute)436 void StreamIn::setMicMute(bool mute) {
437     mEffectiveVolume =
438         (mute && (xsd::stringToAudioDevice(getDeviceAddress().deviceType) ==
439                       xsd::AudioDevice::AUDIO_DEVICE_IN_BUILTIN_MIC))
440             ? 0.0f : 1.0f;
441 }
442 
validateDeviceAddress(const DeviceAddress & device)443 bool StreamIn::validateDeviceAddress(const DeviceAddress& device) {
444     return DevicePortSource::validateDeviceAddress(device);
445 }
446 
validateFlags(const hidl_vec<AudioInOutFlag> & flags)447 bool StreamIn::validateFlags(const hidl_vec<AudioInOutFlag>& flags) {
448     return std::all_of(flags.begin(), flags.end(), [](const AudioInOutFlag& flag){
449         return xsd::stringToAudioInOutFlag(flag) != xsd::AudioInOutFlag::UNKNOWN;
450     });
451 }
452 
validateSinkMetadata(const SinkMetadata & sinkMetadata)453 bool StreamIn::validateSinkMetadata(const SinkMetadata& sinkMetadata) {
454     for (const auto& track : sinkMetadata.tracks) {
455         if (xsd::isUnknownAudioSource(track.source)
456                 || xsd::isUnknownAudioChannelMask(track.channelMask)) {
457             return false;
458         }
459         for (const auto& tag : track.tags) {
460             if (!xsd::isVendorExtension(tag)) {
461                 return false;
462             }
463         }
464     }
465     return true;
466 }
467 
468 }  // namespace implementation
469 }  // namespace CPP_VERSION
470 }  // namespace audio
471 }  // namespace hardware
472 }  // namespace android
473