// Copyright (C) 2021 The Android Open Source Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #pragma once // clang-format off #include PATH(android/hardware/audio/FILE_VERSION/IStreamOut.h) // clang-format on #include #include #include #include #include using android::sp; using android::Thread; using android::hardware::EventFlag; using android::hardware::hidl_bitfield; using android::hardware::hidl_string; using android::hardware::hidl_vec; using android::hardware::kSynchronizedReadWrite; using android::hardware::MessageQueue; using android::hardware::Return; using android::hardware::Void; using namespace android::hardware::audio::common::CPP_VERSION; using namespace android::hardware::audio::CPP_VERSION; namespace audio_proxy::service { class BusOutputStream; class WriteThread; typedef void (*EventFlagDeleter)(EventFlag*); #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 class StreamOutImpl : public android::hardware::audio::V7_1::IStreamOut { #else class StreamOutImpl : public IStreamOut { #endif public: using CommandMQ = MessageQueue; using DataMQ = MessageQueue; using StatusMQ = MessageQueue; #if MAJOR_VERSION >= 7 using StreamOutConfig = AudioConfigBase; #else using StreamOutConfig = AudioConfig; #endif StreamOutImpl(std::shared_ptr stream, const StreamOutConfig& config); ~StreamOutImpl() override; std::shared_ptr getOutputStream(); void updateOutputStream(std::shared_ptr stream); // Methods from ::android::hardware::audio::CPP_VERSION::IStream follow. Return getFrameSize() override; Return getFrameCount() override; Return getBufferSize() override; #if MAJOR_VERSION >= 7 Return getSupportedProfiles(getSupportedProfiles_cb _hidl_cb) override; Return setAudioProperties( const AudioConfigBaseOptional& config) override; #else Return getSampleRate() override; Return getSupportedSampleRates( AudioFormat format, getSupportedSampleRates_cb _hidl_cb) override; Return getSupportedChannelMasks( AudioFormat format, getSupportedChannelMasks_cb _hidl_cb) override; Return setSampleRate(uint32_t sampleRateHz) override; Return> getChannelMask() override; Return setChannelMask(hidl_bitfield mask) override; Return getFormat() override; Return getSupportedFormats(getSupportedFormats_cb _hidl_cb) override; Return setFormat(AudioFormat format) override; #endif Return getAudioProperties(getAudioProperties_cb _hidl_cb) override; Return addEffect(uint64_t effectId) override; Return removeEffect(uint64_t effectId) override; Return standby() override; Return getDevices(getDevices_cb _hidl_cb) override; Return setDevices(const hidl_vec& devices) override; Return getParameters(const hidl_vec& context, const hidl_vec& keys, getParameters_cb _hidl_cb) override; Return setParameters( const hidl_vec& context, const hidl_vec& parameters) override; Return setHwAvSync(uint32_t hwAvSync) override; Return close() override; // Methods from ::android::hardware::audio::CPP_VERSION::IStreamOut follow. Return getLatency() override; Return setVolume(float left, float right) override; Return prepareForWriting(uint32_t frameSize, uint32_t framesCount, prepareForWriting_cb _hidl_cb) override; Return getRenderPosition(getRenderPosition_cb _hidl_cb) override; Return getNextWriteTimestamp( getNextWriteTimestamp_cb _hidl_cb) override; Return setCallback(const sp& callback) override; Return clearCallback() override; Return supportsPauseAndResume( supportsPauseAndResume_cb _hidl_cb) override; Return pause() override; Return resume() override; Return supportsDrain() override; Return drain(AudioDrain type) override; Return flush() override; Return getPresentationPosition( getPresentationPosition_cb _hidl_cb) override; Return start() override; Return stop() override; Return createMmapBuffer(int32_t minSizeFrames, createMmapBuffer_cb _hidl_cb) override; Return getMmapPosition(getMmapPosition_cb _hidl_cb) override; #if MAJOR_VERSION >= 7 Return updateSourceMetadata( const SourceMetadata& sourceMetadata) override; #else Return updateSourceMetadata( const SourceMetadata& sourceMetadata) override; #endif Return selectPresentation(int32_t presentationId, int32_t programId) override; #if MAJOR_VERSION >= 6 Return setEventCallback( const sp& callback) override; Return getDualMonoMode(getDualMonoMode_cb _hidl_cb) override; Return setDualMonoMode(DualMonoMode mode) override; Return getAudioDescriptionMixLevel( getAudioDescriptionMixLevel_cb _hidl_cb) override; Return setAudioDescriptionMixLevel(float leveldB) override; Return getPlaybackRateParameters( getPlaybackRateParameters_cb _hidl_cb) override; Return setPlaybackRateParameters( const PlaybackRate& playbackRate) override; #endif #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 Return setLatencyMode( android::hardware::audio::V7_1::LatencyMode mode) override; Return getRecommendedLatencyModes( getRecommendedLatencyModes_cb _hidl_cb) override; Return setLatencyModeCallback( const sp& cb) override; #endif private: uint64_t estimateTotalPlayedFrames() const; // The object is always valid until close is called. std::shared_ptr mStream; const StreamOutConfig mConfig; const uint64_t mBufferSizeBytes; const uint32_t mLatencyMs; std::unique_ptr mCommandMQ; std::unique_ptr mDataMQ; std::unique_ptr mStatusMQ; std::unique_ptr mEventFlag; sp mWriteThread; uint64_t mTotalPlayedFramesSinceStandby = 0; // Whether pause is called. It's used to avoid resuming when not paused. bool mIsPaused = false; }; } // namespace audio_proxy::service