1 /* 2 * Copyright (C) 2018 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_STREAMIN_H 18 #define ANDROID_HARDWARE_AUDIO_STREAMIN_H 19 20 // clang-format off 21 #include PATH(android/hardware/audio/CORE_TYPES_FILE_VERSION/IStreamIn.h) 22 // clang-format on 23 24 #include "Device.h" 25 #include "Stream.h" 26 27 #include <atomic> 28 #include <memory> 29 30 #include <fmq/EventFlag.h> 31 #include <fmq/MessageQueue.h> 32 #include <hidl/MQDescriptor.h> 33 #include <hidl/Status.h> 34 #include <utils/Thread.h> 35 36 namespace android { 37 namespace hardware { 38 namespace audio { 39 namespace CPP_VERSION { 40 namespace implementation { 41 42 using ::android::sp; 43 using ::android::hardware::hidl_string; 44 using ::android::hardware::hidl_vec; 45 using ::android::hardware::Return; 46 using ::android::hardware::Void; 47 using namespace ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION; 48 using namespace ::android::hardware::audio::CORE_TYPES_CPP_VERSION; 49 using namespace ::android::hardware::audio::CPP_VERSION; 50 51 struct StreamIn : public IStreamIn { 52 typedef MessageQueue<ReadParameters, kSynchronizedReadWrite> CommandMQ; 53 typedef MessageQueue<uint8_t, kSynchronizedReadWrite> DataMQ; 54 typedef MessageQueue<ReadStatus, kSynchronizedReadWrite> StatusMQ; 55 56 StreamIn(const sp<Device>& device, audio_stream_in_t* stream); 57 58 // Methods from ::android::hardware::audio::CPP_VERSION::IStream follow. 59 Return<uint64_t> getFrameSize() override; 60 Return<uint64_t> getFrameCount() override; 61 Return<uint64_t> getBufferSize() override; 62 #if MAJOR_VERSION <= 6 63 Return<uint32_t> getSampleRate() override; 64 #if MAJOR_VERSION == 2 65 Return<void> getSupportedSampleRates(getSupportedSampleRates_cb _hidl_cb) override; 66 Return<void> getSupportedChannelMasks(getSupportedChannelMasks_cb _hidl_cb) override; 67 #endif 68 Return<void> getSupportedSampleRates(AudioFormat format, getSupportedSampleRates_cb _hidl_cb); 69 Return<void> getSupportedChannelMasks(AudioFormat format, getSupportedChannelMasks_cb _hidl_cb); 70 Return<Result> setSampleRate(uint32_t sampleRateHz) override; 71 Return<AudioChannelBitfield> getChannelMask() override; 72 Return<Result> setChannelMask(AudioChannelBitfield mask) override; 73 Return<AudioFormat> getFormat() override; 74 Return<void> getSupportedFormats(getSupportedFormats_cb _hidl_cb) override; 75 Return<Result> setFormat(AudioFormat format) override; 76 #else 77 Return<void> getSupportedProfiles(getSupportedProfiles_cb _hidl_cb) override; 78 Return<Result> setAudioProperties(const AudioConfigBaseOptional& config) override; 79 #endif // MAJOR_VERSION <= 6 80 Return<void> getAudioProperties(getAudioProperties_cb _hidl_cb) override; 81 Return<Result> addEffect(uint64_t effectId) override; 82 Return<Result> removeEffect(uint64_t effectId) override; 83 Return<Result> standby() override; 84 #if MAJOR_VERSION == 2 85 Return<AudioDevice> getDevice() override; 86 Return<Result> setDevice(const DeviceAddress& address) override; 87 Return<void> getParameters(const hidl_vec<hidl_string>& keys, 88 getParameters_cb _hidl_cb) override; 89 Return<Result> setParameters(const hidl_vec<ParameterValue>& parameters) override; 90 Return<Result> setConnectedState(const DeviceAddress& address, bool connected) override; 91 #elif MAJOR_VERSION >= 4 92 Return<void> getDevices(getDevices_cb _hidl_cb) override; 93 Return<Result> setDevices(const hidl_vec<DeviceAddress>& devices) override; 94 Return<void> getParameters(const hidl_vec<ParameterValue>& context, 95 const hidl_vec<hidl_string>& keys, 96 getParameters_cb _hidl_cb) override; 97 Return<Result> setParameters(const hidl_vec<ParameterValue>& context, 98 const hidl_vec<ParameterValue>& parameters) override; 99 #endif 100 Return<Result> setHwAvSync(uint32_t hwAvSync) override; 101 Return<Result> close() override; 102 103 Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& options) override; 104 #if MAJOR_VERSION == 2 105 Return<void> debugDump(const hidl_handle& fd) override; 106 #endif 107 108 // Methods from ::android::hardware::audio::CPP_VERSION::IStreamIn follow. 109 Return<void> getAudioSource(getAudioSource_cb _hidl_cb) override; 110 Return<Result> setGain(float gain) override; 111 Return<void> prepareForReading(uint32_t frameSize, uint32_t framesCount, 112 prepareForReading_cb _hidl_cb) override; 113 Return<uint32_t> getInputFramesLost() override; 114 Return<void> getCapturePosition(getCapturePosition_cb _hidl_cb) override; 115 Return<Result> start() override; 116 Return<Result> stop() override; 117 Return<void> createMmapBuffer(int32_t minSizeFrames, createMmapBuffer_cb _hidl_cb) override; 118 Return<void> getMmapPosition(getMmapPosition_cb _hidl_cb) override; 119 #if MAJOR_VERSION >= 4 120 #if MAJOR_VERSION <= 6 121 Return<void> updateSinkMetadata(const SinkMetadata& sinkMetadata) override; 122 #else 123 Return<Result> updateSinkMetadata(const SinkMetadata& sinkMetadata) override; 124 #endif 125 Return<void> getActiveMicrophones(getActiveMicrophones_cb _hidl_cb) override; 126 #endif // MAJOR_VERSION >= 4 127 #if MAJOR_VERSION >= 5 128 Return<Result> setMicrophoneDirection(MicrophoneDirection direction) override; 129 Return<Result> setMicrophoneFieldDimension(float zoom) override; 130 #endif 131 static Result getCapturePositionImpl(audio_stream_in_t* stream, uint64_t* frames, 132 uint64_t* time); 133 134 private: 135 #if MAJOR_VERSION >= 4 136 Result doUpdateSinkMetadata(const SinkMetadata& sinkMetadata); 137 #if MAJOR_VERSION >= 7 138 Result doUpdateSinkMetadataV7(const SinkMetadata& sinkMetadata); 139 #endif 140 #endif // MAJOR_VERSION >= 4 141 142 const sp<Device> mDevice; 143 audio_stream_in_t* mStream; 144 const sp<Stream> mStreamCommon; 145 const sp<StreamMmap<audio_stream_in_t>> mStreamMmap; 146 std::unique_ptr<CommandMQ> mCommandMQ; 147 std::unique_ptr<DataMQ> mDataMQ; 148 std::unique_ptr<StatusMQ> mStatusMQ; 149 EventFlag* mEfGroup; 150 std::atomic<bool> mStopReadThread; 151 sp<Thread> mReadThread; 152 153 virtual ~StreamIn(); 154 }; 155 156 } // namespace implementation 157 } // namespace CPP_VERSION 158 } // namespace audio 159 } // namespace hardware 160 } // namespace android 161 162 #endif // ANDROID_HARDWARE_AUDIO_STREAMIN_H 163