Home | History | Annotate | Download | only in default
      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 #include <common/all-versions/IncludeGuard.h>
     18 
     19 #include <atomic>
     20 #include <memory>
     21 
     22 #include <fmq/EventFlag.h>
     23 #include <fmq/MessageQueue.h>
     24 #include <hidl/MQDescriptor.h>
     25 #include <hidl/Status.h>
     26 #include <utils/Thread.h>
     27 
     28 namespace android {
     29 namespace hardware {
     30 namespace audio {
     31 namespace AUDIO_HAL_VERSION {
     32 namespace implementation {
     33 
     34 using ::android::hardware::audio::common::AUDIO_HAL_VERSION::AudioChannelMask;
     35 using ::android::hardware::audio::common::AUDIO_HAL_VERSION::AudioDevice;
     36 using ::android::hardware::audio::common::AUDIO_HAL_VERSION::AudioFormat;
     37 using ::android::hardware::audio::common::AUDIO_HAL_VERSION::AudioSource;
     38 using ::android::hardware::audio::AUDIO_HAL_VERSION::DeviceAddress;
     39 using ::android::hardware::audio::AUDIO_HAL_VERSION::IStream;
     40 using ::android::hardware::audio::AUDIO_HAL_VERSION::IStreamIn;
     41 using ::android::hardware::audio::AUDIO_HAL_VERSION::ParameterValue;
     42 using ::android::hardware::audio::AUDIO_HAL_VERSION::Result;
     43 using ::android::hardware::Return;
     44 using ::android::hardware::Void;
     45 using ::android::hardware::hidl_vec;
     46 using ::android::hardware::hidl_string;
     47 using ::android::sp;
     48 
     49 struct StreamIn : public IStreamIn {
     50     typedef MessageQueue<ReadParameters, kSynchronizedReadWrite> CommandMQ;
     51     typedef MessageQueue<uint8_t, kSynchronizedReadWrite> DataMQ;
     52     typedef MessageQueue<ReadStatus, kSynchronizedReadWrite> StatusMQ;
     53 
     54     StreamIn(const sp<Device>& device, audio_stream_in_t* stream);
     55 
     56     // Methods from ::android::hardware::audio::AUDIO_HAL_VERSION::IStream follow.
     57     Return<uint64_t> getFrameSize() override;
     58     Return<uint64_t> getFrameCount() override;
     59     Return<uint64_t> getBufferSize() override;
     60     Return<uint32_t> getSampleRate() override;
     61 #ifdef AUDIO_HAL_VERSION_2_0
     62     Return<void> getSupportedSampleRates(getSupportedSampleRates_cb _hidl_cb) override;
     63     Return<void> getSupportedChannelMasks(getSupportedChannelMasks_cb _hidl_cb) override;
     64 #endif
     65     Return<void> getSupportedSampleRates(AudioFormat format, getSupportedSampleRates_cb _hidl_cb);
     66     Return<void> getSupportedChannelMasks(AudioFormat format, getSupportedChannelMasks_cb _hidl_cb);
     67     Return<Result> setSampleRate(uint32_t sampleRateHz) override;
     68     Return<AudioChannelBitfield> getChannelMask() override;
     69     Return<Result> setChannelMask(AudioChannelBitfield mask) override;
     70     Return<AudioFormat> getFormat() override;
     71     Return<void> getSupportedFormats(getSupportedFormats_cb _hidl_cb) override;
     72     Return<Result> setFormat(AudioFormat format) override;
     73     Return<void> getAudioProperties(getAudioProperties_cb _hidl_cb) override;
     74     Return<Result> addEffect(uint64_t effectId) override;
     75     Return<Result> removeEffect(uint64_t effectId) override;
     76     Return<Result> standby() override;
     77 #ifdef AUDIO_HAL_VERSION_2_0
     78     Return<AudioDevice> getDevice() override;
     79     Return<Result> setDevice(const DeviceAddress& address) override;
     80     Return<void> getParameters(const hidl_vec<hidl_string>& keys,
     81                                getParameters_cb _hidl_cb) override;
     82     Return<Result> setParameters(const hidl_vec<ParameterValue>& parameters) override;
     83     Return<Result> setConnectedState(const DeviceAddress& address, bool connected) override;
     84 #elif defined(AUDIO_HAL_VERSION_4_0)
     85     Return<void> getDevices(getDevices_cb _hidl_cb) override;
     86     Return<Result> setDevices(const hidl_vec<DeviceAddress>& devices) override;
     87     Return<void> getParameters(const hidl_vec<ParameterValue>& context,
     88                                const hidl_vec<hidl_string>& keys,
     89                                getParameters_cb _hidl_cb) override;
     90     Return<Result> setParameters(const hidl_vec<ParameterValue>& context,
     91                                  const hidl_vec<ParameterValue>& parameters) override;
     92 #endif
     93     Return<Result> setHwAvSync(uint32_t hwAvSync) override;
     94     Return<Result> close() override;
     95 
     96     Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& options) override;
     97 #ifdef AUDIO_HAL_VERSION_2_0
     98     Return<void> debugDump(const hidl_handle& fd) override;
     99 #endif
    100 
    101     // Methods from ::android::hardware::audio::AUDIO_HAL_VERSION::IStreamIn follow.
    102     Return<void> getAudioSource(getAudioSource_cb _hidl_cb) override;
    103     Return<Result> setGain(float gain) override;
    104     Return<void> prepareForReading(uint32_t frameSize, uint32_t framesCount,
    105                                    prepareForReading_cb _hidl_cb) override;
    106     Return<uint32_t> getInputFramesLost() override;
    107     Return<void> getCapturePosition(getCapturePosition_cb _hidl_cb) override;
    108     Return<Result> start() override;
    109     Return<Result> stop() override;
    110     Return<void> createMmapBuffer(int32_t minSizeFrames, createMmapBuffer_cb _hidl_cb) override;
    111     Return<void> getMmapPosition(getMmapPosition_cb _hidl_cb) override;
    112 #ifdef AUDIO_HAL_VERSION_4_0
    113     Return<void> updateSinkMetadata(const SinkMetadata& sinkMetadata) override;
    114     Return<void> getActiveMicrophones(getActiveMicrophones_cb _hidl_cb) override;
    115 #endif
    116 
    117     static Result getCapturePositionImpl(audio_stream_in_t* stream, uint64_t* frames,
    118                                          uint64_t* time);
    119 
    120    private:
    121     bool mIsClosed;
    122     const sp<Device> mDevice;
    123     audio_stream_in_t* mStream;
    124     const sp<Stream> mStreamCommon;
    125     const sp<StreamMmap<audio_stream_in_t>> mStreamMmap;
    126     std::unique_ptr<CommandMQ> mCommandMQ;
    127     std::unique_ptr<DataMQ> mDataMQ;
    128     std::unique_ptr<StatusMQ> mStatusMQ;
    129     EventFlag* mEfGroup;
    130     std::atomic<bool> mStopReadThread;
    131     sp<Thread> mReadThread;
    132 
    133     virtual ~StreamIn();
    134 };
    135 
    136 }  // namespace implementation
    137 }  // namespace AUDIO_HAL_VERSION
    138 }  // namespace audio
    139 }  // namespace hardware
    140 }  // namespace android
    141