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