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_STREAMOUT_H 18 #define ANDROID_HARDWARE_AUDIO_STREAMOUT_H 19 20 #include PATH(android/hardware/audio/FILE_VERSION/IStreamOut.h) 21 22 #include "Device.h" 23 #include "Stream.h" 24 25 #include <atomic> 26 #include <memory> 27 28 #include <fmq/EventFlag.h> 29 #include <fmq/MessageQueue.h> 30 #include <hidl/MQDescriptor.h> 31 #include <hidl/Status.h> 32 #include <mediautils/Synchronization.h> 33 #include <utils/Thread.h> 34 35 namespace android { 36 namespace hardware { 37 namespace audio { 38 namespace CPP_VERSION { 39 namespace implementation { 40 41 using ::android::sp; 42 using ::android::hardware::hidl_string; 43 using ::android::hardware::hidl_vec; 44 using ::android::hardware::Return; 45 using ::android::hardware::Void; 46 using namespace ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION; 47 using namespace ::android::hardware::audio::CORE_TYPES_CPP_VERSION; 48 using namespace ::android::hardware::audio::CPP_VERSION; 49 50 struct StreamOut : public IStreamOut { 51 typedef MessageQueue<WriteCommand, kSynchronizedReadWrite> CommandMQ; 52 typedef MessageQueue<uint8_t, kSynchronizedReadWrite> DataMQ; 53 typedef MessageQueue<WriteStatus, kSynchronizedReadWrite> StatusMQ; 54 55 StreamOut(const sp<Device>& device, audio_stream_out_t* stream); 56 57 // Methods from ::android::hardware::audio::CPP_VERSION::IStream follow. 58 Return<uint64_t> getFrameSize() override; 59 Return<uint64_t> getFrameCount() override; 60 Return<uint64_t> getBufferSize() override; 61 #if MAJOR_VERSION <= 6 62 Return<uint32_t> getSampleRate() override; 63 #if MAJOR_VERSION == 2 64 Return<void> getSupportedSampleRates(getSupportedSampleRates_cb _hidl_cb) override; 65 Return<void> getSupportedChannelMasks(getSupportedChannelMasks_cb _hidl_cb) override; 66 #endif 67 Return<void> getSupportedSampleRates(AudioFormat format, getSupportedSampleRates_cb _hidl_cb); 68 Return<void> getSupportedChannelMasks(AudioFormat format, getSupportedChannelMasks_cb _hidl_cb); 69 Return<Result> setSampleRate(uint32_t sampleRateHz) override; 70 Return<AudioChannelBitfield> getChannelMask() override; 71 Return<Result> setChannelMask(AudioChannelBitfield mask) override; 72 Return<AudioFormat> getFormat() override; 73 Return<void> getSupportedFormats(getSupportedFormats_cb _hidl_cb) override; 74 Return<Result> setFormat(AudioFormat format) override; 75 #else 76 Return<void> getSupportedProfiles(getSupportedProfiles_cb _hidl_cb) override; 77 Return<Result> setAudioProperties(const AudioConfigBaseOptional& config) override; 78 #endif // MAJOR_VERSION <= 6 79 Return<void> getAudioProperties(getAudioProperties_cb _hidl_cb) override; 80 Return<Result> addEffect(uint64_t effectId) override; 81 Return<Result> removeEffect(uint64_t effectId) override; 82 Return<Result> standby() override; 83 #if MAJOR_VERSION == 2 84 Return<AudioDevice> getDevice() override; 85 Return<Result> setDevice(const DeviceAddress& address) override; 86 Return<void> getParameters(const hidl_vec<hidl_string>& keys, 87 getParameters_cb _hidl_cb) override; 88 Return<Result> setParameters(const hidl_vec<ParameterValue>& parameters) override; 89 Return<Result> setConnectedState(const DeviceAddress& address, bool connected) override; 90 #elif MAJOR_VERSION >= 4 91 Return<void> getDevices(getDevices_cb _hidl_cb) override; 92 Return<Result> setDevices(const hidl_vec<DeviceAddress>& devices) override; 93 Return<void> getParameters(const hidl_vec<ParameterValue>& context, 94 const hidl_vec<hidl_string>& keys, 95 getParameters_cb _hidl_cb) override; 96 Return<Result> setParameters(const hidl_vec<ParameterValue>& context, 97 const hidl_vec<ParameterValue>& parameters) override; 98 #endif 99 Return<Result> setHwAvSync(uint32_t hwAvSync) override; 100 Return<Result> close() override; 101 102 Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& options) override; 103 #if MAJOR_VERSION == 2 104 Return<void> debugDump(const hidl_handle& fd) override; 105 #endif 106 107 // Methods from ::android::hardware::audio::CPP_VERSION::IStreamOut follow. 108 Return<uint32_t> getLatency() override; 109 Return<Result> setVolume(float left, float right) override; 110 Return<void> prepareForWriting(uint32_t frameSize, uint32_t framesCount, 111 prepareForWriting_cb _hidl_cb) override; 112 Return<void> getRenderPosition(getRenderPosition_cb _hidl_cb) override; 113 Return<void> getNextWriteTimestamp(getNextWriteTimestamp_cb _hidl_cb) override; 114 Return<Result> setCallback(const sp<IStreamOutCallback>& callback) override; 115 Return<Result> clearCallback() override; 116 Return<void> supportsPauseAndResume(supportsPauseAndResume_cb _hidl_cb) override; 117 Return<Result> pause() override; 118 Return<Result> resume() override; 119 Return<bool> supportsDrain() override; 120 Return<Result> drain(AudioDrain type) override; 121 Return<Result> flush() override; 122 Return<void> getPresentationPosition(getPresentationPosition_cb _hidl_cb) override; 123 Return<Result> start() override; 124 Return<Result> stop() override; 125 Return<void> createMmapBuffer(int32_t minSizeFrames, createMmapBuffer_cb _hidl_cb) override; 126 Return<void> getMmapPosition(getMmapPosition_cb _hidl_cb) override; 127 #if MAJOR_VERSION >= 4 128 Return<Result> selectPresentation(int32_t presentationId, int32_t programId) override; 129 #if MAJOR_VERSION <= 6 130 Return<void> updateSourceMetadata(const SourceMetadata& sourceMetadata) override; 131 #else 132 Return<Result> updateSourceMetadata(const SourceMetadata& sourceMetadata) override; 133 #endif 134 #endif // MAJOR_VERSION >= 4 135 #if MAJOR_VERSION >= 6 136 Return<void> getDualMonoMode(getDualMonoMode_cb _hidl_cb) override; 137 Return<Result> setDualMonoMode(DualMonoMode mode) override; 138 Return<void> getAudioDescriptionMixLevel(getAudioDescriptionMixLevel_cb _hidl_cb) override; 139 Return<Result> setAudioDescriptionMixLevel(float leveldB) override; 140 Return<void> getPlaybackRateParameters(getPlaybackRateParameters_cb _hidl_cb) override; 141 Return<Result> setPlaybackRateParameters(const PlaybackRate& playbackRate) override; 142 #endif 143 144 static Result getPresentationPositionImpl(audio_stream_out_t* stream, uint64_t* frames, 145 TimeSpec* timeStamp); 146 147 #if MAJOR_VERSION >= 6 148 Return<Result> setEventCallback(const sp<IStreamOutEventCallback>& callback) override; 149 #endif 150 151 private: 152 #if MAJOR_VERSION >= 4 153 Result doUpdateSourceMetadata(const SourceMetadata& sourceMetadata); 154 #if MAJOR_VERSION >= 7 155 Result doUpdateSourceMetadataV7(const SourceMetadata& sourceMetadata); 156 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 157 Return<Result> setLatencyMode(LatencyMode mode) override; 158 Return<void> getRecommendedLatencyModes(getRecommendedLatencyModes_cb _hidl_cb) override; 159 Return<Result> setLatencyModeCallback( 160 const sp<IStreamOutLatencyModeCallback>& callback) override; 161 #endif 162 #endif 163 #endif // MAJOR_VERSION >= 4 164 165 const sp<Device> mDevice; 166 audio_stream_out_t* mStream; 167 const sp<Stream> mStreamCommon; 168 const sp<StreamMmap<audio_stream_out_t>> mStreamMmap; 169 mediautils::atomic_sp<IStreamOutCallback> mCallback; // for non-blocking write and drain 170 #if MAJOR_VERSION >= 6 171 mediautils::atomic_sp<IStreamOutEventCallback> mEventCallback; 172 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 173 mediautils::atomic_sp<IStreamOutLatencyModeCallback> mLatencyModeCallback; 174 #endif 175 #endif 176 std::unique_ptr<CommandMQ> mCommandMQ; 177 std::unique_ptr<DataMQ> mDataMQ; 178 std::unique_ptr<StatusMQ> mStatusMQ; 179 EventFlag* mEfGroup; 180 std::atomic<bool> mStopWriteThread; 181 sp<Thread> mWriteThread; 182 183 virtual ~StreamOut(); 184 185 static int asyncCallback(stream_callback_event_t event, void* param, void* cookie); 186 187 #if MAJOR_VERSION >= 6 188 static int asyncEventCallback(stream_event_callback_type_t event, void* param, void* cookie); 189 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 190 static void latencyModeCallback(audio_latency_mode_t* modes, size_t num_modes, void* cookie); 191 #endif 192 #endif 193 }; 194 195 } // namespace implementation 196 } // namespace CPP_VERSION 197 } // namespace audio 198 } // namespace hardware 199 } // namespace android 200 201 #endif // ANDROID_HARDWARE_AUDIO_STREAMOUT_H 202