1 /*
2  * Copyright (C) 2020 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 #pragma once
18 #include <atomic>
19 #include <android/hardware/audio/6.0/IStreamOut.h>
20 #include <android/hardware/audio/6.0/IDevice.h>
21 #include "stream_common.h"
22 #include "io_thread.h"
23 #include "primary_device.h"
24 
25 namespace android {
26 namespace hardware {
27 namespace audio {
28 namespace V6_0 {
29 namespace implementation {
30 
31 using ::android::sp;
32 using ::android::hardware::hidl_bitfield;
33 using ::android::hardware::hidl_string;
34 using ::android::hardware::hidl_vec;
35 using ::android::hardware::Return;
36 using namespace ::android::hardware::audio::common::V6_0;
37 using namespace ::android::hardware::audio::V6_0;
38 
39 struct StreamOut : public IStreamOut {
40     StreamOut(sp<PrimaryDevice> dev,
41               int32_t ioHandle,
42               const DeviceAddress& device,
43               const AudioConfig& config,
44               hidl_bitfield<AudioOutputFlag> flags,
45               const SourceMetadata& sourceMetadata);
46     ~StreamOut();
47 
48     // IStream
49     Return<uint64_t> getFrameSize() override;
50     Return<uint64_t> getFrameCount() override;
51     Return<uint64_t> getBufferSize() override;
52     Return<uint32_t> getSampleRate() override;
53     Return<void> getSupportedSampleRates(AudioFormat format, getSupportedSampleRates_cb _hidl_cb) override;
54     Return<Result> setSampleRate(uint32_t sampleRateHz) override;
55     Return<hidl_bitfield<AudioChannelMask>> getChannelMask() override;
56     Return<void> getSupportedChannelMasks(AudioFormat format, getSupportedChannelMasks_cb _hidl_cb) override;
57     Return<Result> setChannelMask(hidl_bitfield<AudioChannelMask> mask) override;
58     Return<AudioFormat> getFormat() override;
59     Return<void> getSupportedFormats(getSupportedFormats_cb _hidl_cb) override;
60     Return<Result> setFormat(AudioFormat format) override;
61     Return<void> getAudioProperties(getAudioProperties_cb _hidl_cb) override;
62     Return<Result> addEffect(uint64_t effectId) override;
63     Return<Result> removeEffect(uint64_t effectId) override;
64     Return<Result> standby() override;
65     Return<void> getDevices(getDevices_cb _hidl_cb) override;
66     Return<Result> setDevices(const hidl_vec<DeviceAddress>& devices) override;
67     Return<Result> setHwAvSync(uint32_t hwAvSync) override;
68     Return<void> getParameters(const hidl_vec<ParameterValue>& context,
69                                const hidl_vec<hidl_string>& keys,
70                                getParameters_cb _hidl_cb) override;
71     Return<Result> setParameters(const hidl_vec<ParameterValue>& context,
72                                  const hidl_vec<ParameterValue>& parameters) override;
73     Return<Result> start() override;
74     Return<Result> stop() override;
75     Return<void> createMmapBuffer(int32_t minSizeFrames, createMmapBuffer_cb _hidl_cb) override;
76     Return<void> getMmapPosition(getMmapPosition_cb _hidl_cb) override;
77     Return<Result> close() override;
78 
79     // IStreamOut
80     Return<uint32_t> getLatency() override;
81     Return<Result> setVolume(float left, float right) override;
82     Return<void> updateSourceMetadata(const SourceMetadata& sourceMetadata) override;
83     Return<void> prepareForWriting(uint32_t frameSize, uint32_t framesCount,
84                                    prepareForWriting_cb _hidl_cb) override;
85     Return<void> getRenderPosition(getRenderPosition_cb _hidl_cb) override;
86     Return<void> getNextWriteTimestamp(getNextWriteTimestamp_cb _hidl_cb) override;
87     Return<Result> setCallback(const sp<IStreamOutCallback>& callback) override;
88     Return<Result> clearCallback() override;
89     Return<void> supportsPauseAndResume(supportsPauseAndResume_cb _hidl_cb) override;
90     Return<Result> pause() override;
91     Return<Result> resume() override;
92     Return<bool> supportsDrain() override;
93     Return<Result> drain(AudioDrain type) override;
94     Return<Result> flush() override;
95     Return<void> getPresentationPosition(getPresentationPosition_cb _hidl_cb) override;
96     Return<Result> selectPresentation(int32_t presentationId, int32_t programId) override;
97 
98     void setMasterVolume(float volume);
getEffectiveVolumeStreamOut99     float getEffectiveVolume() const { return mEffectiveVolume; }
getDeviceAddressStreamOut100     const DeviceAddress &getDeviceAddress() const { return mCommon.m_device; }
getAudioConfigStreamOut101     const AudioConfig &getAudioConfig() const { return mCommon.m_config; }
getAudioOutputFlagsStreamOut102     const hidl_bitfield<AudioOutputFlag> &getAudioOutputFlags() const { return mCommon.m_flags; }
103 
getFrameCounterStreamOut104     uint64_t &getFrameCounter() { return mFrames; }
105 
106 private:
107     Result closeImpl(bool fromDctor);
108     void updateEffectiveVolumeLocked();
109 
110     sp<PrimaryDevice> mDev;
111     const StreamCommon mCommon;
112     const SourceMetadata mSourceMetadata;
113     std::unique_ptr<IOThread> mWriteThread;
114 
115     float mMasterVolume = 1.0f;  // requires mMutex
116     float mStreamVolume = 1.0f;  // requires mMutex
117     std::atomic<float> mEffectiveVolume = 1.0f;
118     std::mutex mMutex;
119 
120     // The count is not reset to zero when output enters standby.
121     uint64_t mFrames = 0;
122 };
123 
124 }  // namespace implementation
125 }  // namespace V6_0
126 }  // namespace audio
127 }  // namespace hardware
128 }  // namespace android
129