1 // Copyright (C) 2021 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #pragma once 16 17 // clang-format off 18 #include PATH(android/hardware/audio/FILE_VERSION/IStreamOut.h) 19 // clang-format on 20 21 #include <fmq/EventFlag.h> 22 #include <fmq/MessageQueue.h> 23 #include <hidl/MQDescriptor.h> 24 #include <hidl/Status.h> 25 #include <utils/Thread.h> 26 27 using android::sp; 28 using android::Thread; 29 using android::hardware::EventFlag; 30 using android::hardware::hidl_bitfield; 31 using android::hardware::hidl_string; 32 using android::hardware::hidl_vec; 33 using android::hardware::kSynchronizedReadWrite; 34 using android::hardware::MessageQueue; 35 using android::hardware::Return; 36 using android::hardware::Void; 37 using namespace android::hardware::audio::common::CPP_VERSION; 38 using namespace android::hardware::audio::CPP_VERSION; 39 40 namespace audio_proxy::service { 41 42 class BusOutputStream; 43 class WriteThread; 44 45 typedef void (*EventFlagDeleter)(EventFlag*); 46 47 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 48 class StreamOutImpl : public android::hardware::audio::V7_1::IStreamOut { 49 #else 50 class StreamOutImpl : public IStreamOut { 51 #endif 52 public: 53 using CommandMQ = MessageQueue<WriteCommand, kSynchronizedReadWrite>; 54 using DataMQ = MessageQueue<uint8_t, kSynchronizedReadWrite>; 55 using StatusMQ = MessageQueue<WriteStatus, kSynchronizedReadWrite>; 56 57 #if MAJOR_VERSION >= 7 58 using StreamOutConfig = AudioConfigBase; 59 #else 60 using StreamOutConfig = AudioConfig; 61 #endif 62 63 StreamOutImpl(std::shared_ptr<BusOutputStream> stream, 64 const StreamOutConfig& config); 65 ~StreamOutImpl() override; 66 67 std::shared_ptr<BusOutputStream> getOutputStream(); 68 void updateOutputStream(std::shared_ptr<BusOutputStream> stream); 69 70 // Methods from ::android::hardware::audio::CPP_VERSION::IStream follow. 71 Return<uint64_t> getFrameSize() override; 72 Return<uint64_t> getFrameCount() override; 73 Return<uint64_t> getBufferSize() override; 74 75 #if MAJOR_VERSION >= 7 76 Return<void> getSupportedProfiles(getSupportedProfiles_cb _hidl_cb) override; 77 Return<Result> setAudioProperties( 78 const AudioConfigBaseOptional& config) override; 79 #else 80 Return<uint32_t> getSampleRate() override; 81 Return<void> getSupportedSampleRates( 82 AudioFormat format, getSupportedSampleRates_cb _hidl_cb) override; 83 Return<void> getSupportedChannelMasks( 84 AudioFormat format, getSupportedChannelMasks_cb _hidl_cb) override; 85 Return<Result> setSampleRate(uint32_t sampleRateHz) override; 86 Return<hidl_bitfield<AudioChannelMask>> getChannelMask() override; 87 Return<Result> setChannelMask(hidl_bitfield<AudioChannelMask> mask) override; 88 Return<AudioFormat> getFormat() override; 89 Return<void> getSupportedFormats(getSupportedFormats_cb _hidl_cb) override; 90 Return<Result> setFormat(AudioFormat format) override; 91 #endif 92 93 Return<void> getAudioProperties(getAudioProperties_cb _hidl_cb) override; 94 Return<Result> addEffect(uint64_t effectId) override; 95 Return<Result> removeEffect(uint64_t effectId) override; 96 Return<Result> standby() override; 97 Return<void> getDevices(getDevices_cb _hidl_cb) override; 98 Return<Result> setDevices(const hidl_vec<DeviceAddress>& devices) override; 99 Return<void> getParameters(const hidl_vec<ParameterValue>& context, 100 const hidl_vec<hidl_string>& keys, 101 getParameters_cb _hidl_cb) override; 102 Return<Result> setParameters( 103 const hidl_vec<ParameterValue>& context, 104 const hidl_vec<ParameterValue>& parameters) override; 105 Return<Result> setHwAvSync(uint32_t hwAvSync) override; 106 Return<Result> close() override; 107 108 // Methods from ::android::hardware::audio::CPP_VERSION::IStreamOut follow. 109 Return<uint32_t> getLatency() override; 110 Return<Result> setVolume(float left, float right) override; 111 Return<void> prepareForWriting(uint32_t frameSize, uint32_t framesCount, 112 prepareForWriting_cb _hidl_cb) override; 113 Return<void> getRenderPosition(getRenderPosition_cb _hidl_cb) override; 114 Return<void> getNextWriteTimestamp( 115 getNextWriteTimestamp_cb _hidl_cb) override; 116 Return<Result> setCallback(const sp<IStreamOutCallback>& callback) override; 117 Return<Result> clearCallback() override; 118 Return<void> supportsPauseAndResume( 119 supportsPauseAndResume_cb _hidl_cb) override; 120 Return<Result> pause() override; 121 Return<Result> resume() override; 122 Return<bool> supportsDrain() override; 123 Return<Result> drain(AudioDrain type) override; 124 Return<Result> flush() override; 125 Return<void> getPresentationPosition( 126 getPresentationPosition_cb _hidl_cb) override; 127 Return<Result> start() override; 128 Return<Result> stop() override; 129 Return<void> createMmapBuffer(int32_t minSizeFrames, 130 createMmapBuffer_cb _hidl_cb) override; 131 Return<void> getMmapPosition(getMmapPosition_cb _hidl_cb) override; 132 #if MAJOR_VERSION >= 7 133 Return<Result> updateSourceMetadata( 134 const SourceMetadata& sourceMetadata) override; 135 #else 136 Return<void> updateSourceMetadata( 137 const SourceMetadata& sourceMetadata) override; 138 #endif 139 Return<Result> selectPresentation(int32_t presentationId, 140 int32_t programId) override; 141 142 #if MAJOR_VERSION >= 6 143 Return<Result> setEventCallback( 144 const sp<IStreamOutEventCallback>& callback) override; 145 Return<void> getDualMonoMode(getDualMonoMode_cb _hidl_cb) override; 146 Return<Result> setDualMonoMode(DualMonoMode mode) override; 147 Return<void> getAudioDescriptionMixLevel( 148 getAudioDescriptionMixLevel_cb _hidl_cb) override; 149 Return<Result> setAudioDescriptionMixLevel(float leveldB) override; 150 Return<void> getPlaybackRateParameters( 151 getPlaybackRateParameters_cb _hidl_cb) override; 152 Return<Result> setPlaybackRateParameters( 153 const PlaybackRate& playbackRate) override; 154 #endif 155 156 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 157 Return<Result> setLatencyMode( 158 android::hardware::audio::V7_1::LatencyMode mode) override; 159 Return<void> getRecommendedLatencyModes( 160 getRecommendedLatencyModes_cb _hidl_cb) override; 161 Return<Result> setLatencyModeCallback( 162 const sp<android::hardware::audio::V7_1::IStreamOutLatencyModeCallback>& 163 cb) override; 164 #endif 165 166 private: 167 uint64_t estimateTotalPlayedFrames() const; 168 169 // The object is always valid until close is called. 170 std::shared_ptr<BusOutputStream> mStream; 171 const StreamOutConfig mConfig; 172 173 const uint64_t mBufferSizeBytes; 174 const uint32_t mLatencyMs; 175 176 std::unique_ptr<CommandMQ> mCommandMQ; 177 std::unique_ptr<DataMQ> mDataMQ; 178 std::unique_ptr<StatusMQ> mStatusMQ; 179 std::unique_ptr<EventFlag, EventFlagDeleter> mEventFlag; 180 sp<WriteThread> mWriteThread; 181 182 uint64_t mTotalPlayedFramesSinceStandby = 0; 183 184 // Whether pause is called. It's used to avoid resuming when not paused. 185 bool mIsPaused = false; 186 }; 187 188 } // namespace audio_proxy::service 189