• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
24 namespace android {
25 namespace hardware {
26 namespace audio {
27 namespace V6_0 {
28 namespace implementation {
29 
30 using ::android::sp;
31 using ::android::hardware::hidl_bitfield;
32 using ::android::hardware::hidl_string;
33 using ::android::hardware::hidl_vec;
34 using ::android::hardware::Return;
35 using namespace ::android::hardware::audio::common::V6_0;
36 using namespace ::android::hardware::audio::V6_0;
37 
38 struct StreamOut : public IStreamOut {
39     StreamOut(sp<IDevice> dev,
40               void (*unrefDevice)(IDevice*),
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     static constexpr int16_t kVolumeDenominator = 1 << 14;
49 
50     // IStream
51     Return<uint64_t> getFrameSize() override;
52     Return<uint64_t> getFrameCount() override;
53     Return<uint64_t> getBufferSize() override;
54     Return<uint32_t> getSampleRate() override;
55     Return<void> getSupportedSampleRates(AudioFormat format, getSupportedSampleRates_cb _hidl_cb) override;
56     Return<Result> setSampleRate(uint32_t sampleRateHz) override;
57     Return<hidl_bitfield<AudioChannelMask>> getChannelMask() override;
58     Return<void> getSupportedChannelMasks(AudioFormat format, getSupportedChannelMasks_cb _hidl_cb) override;
59     Return<Result> setChannelMask(hidl_bitfield<AudioChannelMask> mask) override;
60     Return<AudioFormat> getFormat() override;
61     Return<void> getSupportedFormats(getSupportedFormats_cb _hidl_cb) override;
62     Return<Result> setFormat(AudioFormat format) override;
63     Return<void> getAudioProperties(getAudioProperties_cb _hidl_cb) override;
64     Return<Result> addEffect(uint64_t effectId) override;
65     Return<Result> removeEffect(uint64_t effectId) override;
66     Return<Result> standby() override;
67     Return<void> getDevices(getDevices_cb _hidl_cb) override;
68     Return<Result> setDevices(const hidl_vec<DeviceAddress>& devices) override;
69     Return<Result> setHwAvSync(uint32_t hwAvSync) override;
70     Return<void> getParameters(const hidl_vec<ParameterValue>& context,
71                                const hidl_vec<hidl_string>& keys,
72                                getParameters_cb _hidl_cb) override;
73     Return<Result> setParameters(const hidl_vec<ParameterValue>& context,
74                                  const hidl_vec<ParameterValue>& parameters) override;
75     Return<Result> start() override;
76     Return<Result> stop() override;
77     Return<void> createMmapBuffer(int32_t minSizeFrames, createMmapBuffer_cb _hidl_cb) override;
78     Return<void> getMmapPosition(getMmapPosition_cb _hidl_cb) override;
79     Return<Result> close() override;
80 
81     // IStreamOut
82     Return<uint32_t> getLatency() override;
83     Return<Result> setVolume(float left, float right) override;
84     Return<void> updateSourceMetadata(const SourceMetadata& sourceMetadata) override;
85     Return<void> prepareForWriting(uint32_t frameSize, uint32_t framesCount,
86                                    prepareForWriting_cb _hidl_cb) override;
87     Return<void> getRenderPosition(getRenderPosition_cb _hidl_cb) override;
88     Return<void> getNextWriteTimestamp(getNextWriteTimestamp_cb _hidl_cb) override;
89     Return<Result> setCallback(const sp<IStreamOutCallback>& callback) override;
90     Return<Result> clearCallback() override;
91     Return<Result> setEventCallback(const sp<IStreamOutEventCallback>& callback) override;
92     Return<void> supportsPauseAndResume(supportsPauseAndResume_cb _hidl_cb) override;
93     Return<Result> pause() override;
94     Return<Result> resume() override;
95     Return<bool> supportsDrain() override;
96     Return<Result> drain(AudioDrain type) override;
97     Return<Result> flush() override;
98     Return<void> getPresentationPosition(getPresentationPosition_cb _hidl_cb) override;
99     Return<Result> selectPresentation(int32_t presentationId, int32_t programId) override;
100     Return<void> getDualMonoMode(getDualMonoMode_cb _hidl_cb) override;
101     Return<Result> setDualMonoMode(DualMonoMode mode) override;
102     Return<void> getAudioDescriptionMixLevel(getAudioDescriptionMixLevel_cb _hidl_cb) override;
103     Return<Result> setAudioDescriptionMixLevel(float leveldB) override;
104     Return<void> getPlaybackRateParameters(getPlaybackRateParameters_cb _hidl_cb) override;
105     Return<Result> setPlaybackRateParameters(const PlaybackRate &playbackRate) override;
106 
getVolumeNumeratorStreamOut107     int16_t getVolumeNumerator() const { return mVolumeNumerator; };
getDeviceAddressStreamOut108     const DeviceAddress &getDeviceAddress() const { return mCommon.m_device; }
getAudioConfigStreamOut109     const AudioConfig &getAudioConfig() const { return mCommon.m_config; }
getAudioOutputFlagsStreamOut110     const hidl_bitfield<AudioOutputFlag> &getAudioOutputFlags() const { return mCommon.m_flags; }
111 
getFrameCounterStreamOut112     uint64_t &getFrameCounter() { return mFrames; }
113 
114 private:
115     Result closeImpl(bool fromDctor);
116 
117     sp<IDevice> mDev;
118     void (* const mUnrefDevice)(IDevice*);
119     const StreamCommon mCommon;
120     const SourceMetadata mSourceMetadata;
121     std::unique_ptr<IOThread> mWriteThread;
122     std::atomic<int16_t> mVolumeNumerator = kVolumeDenominator;
123 
124     // The count is not reset to zero when output enters standby.
125     uint64_t mFrames = 0;
126 };
127 
128 }  // namespace implementation
129 }  // namespace V6_0
130 }  // namespace audio
131 }  // namespace hardware
132 }  // namespace android
133