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 PATH(android/hardware/audio/FILE_VERSION/IStreamOut.h)
20 #include PATH(android/hardware/audio/FILE_VERSION/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 CPP_VERSION {
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::COMMON_TYPES_CPP_VERSION;
37 using namespace ::android::hardware::audio::CORE_TYPES_CPP_VERSION;
38 using ::android::hardware::audio::CPP_VERSION::IStreamOut;
39 
40 struct StreamOut : public IStreamOut {
41     StreamOut(sp<Device> dev,
42               int32_t ioHandle,
43               const DeviceAddress& device,
44               const AudioConfig& config,
45               hidl_vec<AudioInOutFlag> flags,
46               const SourceMetadata& sourceMetadata);
47     ~StreamOut();
48 
49     // IStream
50     Return<uint64_t> getFrameSize() override;
51     Return<uint64_t> getFrameCount() override;
52     Return<uint64_t> getBufferSize() override;
53     Return<void> getSupportedProfiles(getSupportedProfiles_cb _hidl_cb) override;
54     Return<void> getAudioProperties(getAudioProperties_cb _hidl_cb) override;
55     Return<Result> setAudioProperties(const AudioConfigBaseOptional& config) override;
56     Return<Result> addEffect(uint64_t effectId) override;
57     Return<Result> removeEffect(uint64_t effectId) override;
58     Return<Result> standby() override;
59     Return<void> getDevices(getDevices_cb _hidl_cb) override;
60     Return<Result> setDevices(const hidl_vec<DeviceAddress>& devices) override;
61     Return<Result> setHwAvSync(uint32_t hwAvSync) override;
62     Return<void> getParameters(const hidl_vec<ParameterValue>& context,
63                                const hidl_vec<hidl_string>& keys,
64                                getParameters_cb _hidl_cb) override;
65     Return<Result> setParameters(const hidl_vec<ParameterValue>& context,
66                                  const hidl_vec<ParameterValue>& parameters) override;
67     Return<Result> start() override;
68     Return<Result> stop() override;
69     Return<void> createMmapBuffer(int32_t minSizeFrames, createMmapBuffer_cb _hidl_cb) override;
70     Return<void> getMmapPosition(getMmapPosition_cb _hidl_cb) override;
71     Return<Result> close() override;
72 
73     // IStreamOut
74     Return<uint32_t> getLatency() override;
75     Return<Result> setVolume(float left, float right) override;
76     Return<Result> updateSourceMetadata(const SourceMetadata& sourceMetadata) override;
77     Return<void> prepareForWriting(uint32_t frameSize, uint32_t framesCount,
78                                    prepareForWriting_cb _hidl_cb) override;
79     Return<void> getRenderPosition(getRenderPosition_cb _hidl_cb) override;
80     Return<void> getNextWriteTimestamp(getNextWriteTimestamp_cb _hidl_cb) override;
81     Return<Result> setCallback(const sp<IStreamOutCallback>& callback) override;
82     Return<Result> clearCallback() override;
83     Return<Result> setEventCallback(const sp<IStreamOutEventCallback>& callback) override;
84     Return<void> supportsPauseAndResume(supportsPauseAndResume_cb _hidl_cb) override;
85     Return<Result> pause() override;
86     Return<Result> resume() override;
87     Return<bool> supportsDrain() override;
88     Return<Result> drain(AudioDrain type) override;
89     Return<Result> flush() override;
90     Return<void> getPresentationPosition(getPresentationPosition_cb _hidl_cb) override;
91     Return<Result> selectPresentation(int32_t presentationId, int32_t programId) override;
92     Return<void> getDualMonoMode(getDualMonoMode_cb _hidl_cb) override;
93     Return<Result> setDualMonoMode(DualMonoMode mode) override;
94     Return<void> getAudioDescriptionMixLevel(getAudioDescriptionMixLevel_cb _hidl_cb) override;
95     Return<Result> setAudioDescriptionMixLevel(float leveldB) override;
96     Return<void> getPlaybackRateParameters(getPlaybackRateParameters_cb _hidl_cb) override;
97     Return<Result> setPlaybackRateParameters(const PlaybackRate &playbackRate) override;
98 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1
99     Return<Result> setLatencyMode(LatencyMode mode) override;
100     Return<void> getRecommendedLatencyModes(getRecommendedLatencyModes_cb _hidl_cb) override;
101     Return<Result> setLatencyModeCallback(
102             const sp<IStreamOutLatencyModeCallback>& callback) override;
103 #endif
104 
105     void setMasterVolume(float volume);
getEffectiveVolumeStreamOut106     float getEffectiveVolume() const { return mEffectiveVolume; }
getDeviceAddressStreamOut107     const DeviceAddress &getDeviceAddress() const { return mCommon.m_device; }
getAudioConfigStreamOut108     const AudioConfig &getAudioConfig() const { return mCommon.m_config; }
getAudioOutputFlagsStreamOut109     const hidl_vec<AudioInOutFlag> &getAudioOutputFlags() const { return mCommon.m_flags; }
110 
111     static bool validateDeviceAddress(const DeviceAddress& device);
112     static bool validateFlags(const hidl_vec<AudioInOutFlag>& flags);
113     static bool validateSourceMetadata(const SourceMetadata& sourceMetadata);
114 
115 private:
116     Result closeImpl(bool fromDctor);
117     void updateEffectiveVolumeLocked();
118 
119     sp<Device> mDev;
120     const StreamCommon mCommon;
121     const SourceMetadata mSourceMetadata;
122     std::unique_ptr<IOThread> mWriteThread;
123 
124     float mMasterVolume = 1.0f;  // requires mMutex
125     float mStreamVolume = 1.0f;  // requires mMutex
126     std::atomic<float> mEffectiveVolume = 1.0f;
127     std::mutex mMutex;
128 };
129 
130 }  // namespace implementation
131 }  // namespace CPP_VERSION
132 }  // namespace audio
133 }  // namespace hardware
134 }  // namespace android
135