1 /*
2  * Copyright (C) 2018 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 #ifndef ANDROID_HARDWARE_AUDIO_STREAMOUT_H
18 #define ANDROID_HARDWARE_AUDIO_STREAMOUT_H
19 
20 #include PATH(android/hardware/audio/FILE_VERSION/IStreamOut.h)
21 
22 #include "Device.h"
23 #include "Stream.h"
24 
25 #include <atomic>
26 #include <memory>
27 
28 #include <fmq/EventFlag.h>
29 #include <fmq/MessageQueue.h>
30 #include <hidl/MQDescriptor.h>
31 #include <hidl/Status.h>
32 #include <mediautils/Synchronization.h>
33 #include <utils/Thread.h>
34 
35 namespace android {
36 namespace hardware {
37 namespace audio {
38 namespace CPP_VERSION {
39 namespace implementation {
40 
41 using ::android::sp;
42 using ::android::hardware::hidl_string;
43 using ::android::hardware::hidl_vec;
44 using ::android::hardware::Return;
45 using ::android::hardware::Void;
46 using namespace ::android::hardware::audio::common::CPP_VERSION;
47 using namespace ::android::hardware::audio::CPP_VERSION;
48 
49 struct StreamOut : public IStreamOut {
50     typedef MessageQueue<WriteCommand, kSynchronizedReadWrite> CommandMQ;
51     typedef MessageQueue<uint8_t, kSynchronizedReadWrite> DataMQ;
52     typedef MessageQueue<WriteStatus, kSynchronizedReadWrite> StatusMQ;
53 
54     StreamOut(const sp<Device>& device, audio_stream_out_t* stream);
55 
56     // Methods from ::android::hardware::audio::CPP_VERSION::IStream follow.
57     Return<uint64_t> getFrameSize() override;
58     Return<uint64_t> getFrameCount() override;
59     Return<uint64_t> getBufferSize() override;
60 #if MAJOR_VERSION <= 6
61     Return<uint32_t> getSampleRate() override;
62 #if MAJOR_VERSION == 2
63     Return<void> getSupportedSampleRates(getSupportedSampleRates_cb _hidl_cb) override;
64     Return<void> getSupportedChannelMasks(getSupportedChannelMasks_cb _hidl_cb) override;
65 #endif
66     Return<void> getSupportedSampleRates(AudioFormat format, getSupportedSampleRates_cb _hidl_cb);
67     Return<void> getSupportedChannelMasks(AudioFormat format, getSupportedChannelMasks_cb _hidl_cb);
68     Return<Result> setSampleRate(uint32_t sampleRateHz) override;
69     Return<AudioChannelBitfield> getChannelMask() override;
70     Return<Result> setChannelMask(AudioChannelBitfield mask) override;
71     Return<AudioFormat> getFormat() override;
72     Return<void> getSupportedFormats(getSupportedFormats_cb _hidl_cb) override;
73     Return<Result> setFormat(AudioFormat format) override;
74 #else
75     Return<void> getSupportedProfiles(getSupportedProfiles_cb _hidl_cb) override;
76     Return<Result> setAudioProperties(const AudioConfigBaseOptional& config) override;
77 #endif  // MAJOR_VERSION <= 6
78     Return<void> getAudioProperties(getAudioProperties_cb _hidl_cb) override;
79     Return<Result> addEffect(uint64_t effectId) override;
80     Return<Result> removeEffect(uint64_t effectId) override;
81     Return<Result> standby() override;
82 #if MAJOR_VERSION == 2
83     Return<AudioDevice> getDevice() override;
84     Return<Result> setDevice(const DeviceAddress& address) override;
85     Return<void> getParameters(const hidl_vec<hidl_string>& keys,
86                                getParameters_cb _hidl_cb) override;
87     Return<Result> setParameters(const hidl_vec<ParameterValue>& parameters) override;
88     Return<Result> setConnectedState(const DeviceAddress& address, bool connected) override;
89 #elif MAJOR_VERSION >= 4
90     Return<void> getDevices(getDevices_cb _hidl_cb) override;
91     Return<Result> setDevices(const hidl_vec<DeviceAddress>& devices) override;
92     Return<void> getParameters(const hidl_vec<ParameterValue>& context,
93                                const hidl_vec<hidl_string>& keys,
94                                getParameters_cb _hidl_cb) override;
95     Return<Result> setParameters(const hidl_vec<ParameterValue>& context,
96                                  const hidl_vec<ParameterValue>& parameters) override;
97 #endif
98     Return<Result> setHwAvSync(uint32_t hwAvSync) override;
99     Return<Result> close() override;
100 
101     Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& options) override;
102 #if MAJOR_VERSION == 2
103     Return<void> debugDump(const hidl_handle& fd) override;
104 #endif
105 
106     // Methods from ::android::hardware::audio::CPP_VERSION::IStreamOut follow.
107     Return<uint32_t> getLatency() override;
108     Return<Result> setVolume(float left, float right) override;
109     Return<void> prepareForWriting(uint32_t frameSize, uint32_t framesCount,
110                                    prepareForWriting_cb _hidl_cb) override;
111     Return<void> getRenderPosition(getRenderPosition_cb _hidl_cb) override;
112     Return<void> getNextWriteTimestamp(getNextWriteTimestamp_cb _hidl_cb) override;
113     Return<Result> setCallback(const sp<IStreamOutCallback>& callback) override;
114     Return<Result> clearCallback() override;
115     Return<void> supportsPauseAndResume(supportsPauseAndResume_cb _hidl_cb) override;
116     Return<Result> pause() override;
117     Return<Result> resume() override;
118     Return<bool> supportsDrain() override;
119     Return<Result> drain(AudioDrain type) override;
120     Return<Result> flush() override;
121     Return<void> getPresentationPosition(getPresentationPosition_cb _hidl_cb) override;
122     Return<Result> start() override;
123     Return<Result> stop() override;
124     Return<void> createMmapBuffer(int32_t minSizeFrames, createMmapBuffer_cb _hidl_cb) override;
125     Return<void> getMmapPosition(getMmapPosition_cb _hidl_cb) override;
126 #if MAJOR_VERSION >= 4
127     Return<Result> selectPresentation(int32_t presentationId, int32_t programId) override;
128 #if MAJOR_VERSION <= 6
129     Return<void> updateSourceMetadata(const SourceMetadata& sourceMetadata) override;
130 #else
131     Return<Result> updateSourceMetadata(const SourceMetadata& sourceMetadata) override;
132 #endif
133 #endif  // MAJOR_VERSION >= 4
134 #if MAJOR_VERSION >= 6
135     Return<void> getDualMonoMode(getDualMonoMode_cb _hidl_cb) override;
136     Return<Result> setDualMonoMode(DualMonoMode mode) override;
137     Return<void> getAudioDescriptionMixLevel(getAudioDescriptionMixLevel_cb _hidl_cb) override;
138     Return<Result> setAudioDescriptionMixLevel(float leveldB) override;
139     Return<void> getPlaybackRateParameters(getPlaybackRateParameters_cb _hidl_cb) override;
140     Return<Result> setPlaybackRateParameters(const PlaybackRate& playbackRate) override;
141 #endif
142 
143     static Result getPresentationPositionImpl(audio_stream_out_t* stream, uint64_t* frames,
144                                               TimeSpec* timeStamp);
145 
146 #if MAJOR_VERSION >= 6
147     Return<Result> setEventCallback(const sp<IStreamOutEventCallback>& callback) override;
148 #endif
149 
150   private:
151 #if MAJOR_VERSION >= 4
152     Result doUpdateSourceMetadata(const SourceMetadata& sourceMetadata);
153 #if MAJOR_VERSION >= 7
154     Result doUpdateSourceMetadataV7(const SourceMetadata& sourceMetadata);
155 #endif
156 #endif  // MAJOR_VERSION >= 4
157 
158     const sp<Device> mDevice;
159     audio_stream_out_t* mStream;
160     const sp<Stream> mStreamCommon;
161     const sp<StreamMmap<audio_stream_out_t>> mStreamMmap;
162     mediautils::atomic_sp<IStreamOutCallback> mCallback;  // for non-blocking write and drain
163 #if MAJOR_VERSION >= 6
164     mediautils::atomic_sp<IStreamOutEventCallback> mEventCallback;
165 #endif
166     std::unique_ptr<CommandMQ> mCommandMQ;
167     std::unique_ptr<DataMQ> mDataMQ;
168     std::unique_ptr<StatusMQ> mStatusMQ;
169     EventFlag* mEfGroup;
170     std::atomic<bool> mStopWriteThread;
171     sp<Thread> mWriteThread;
172 
173     virtual ~StreamOut();
174 
175     static int asyncCallback(stream_callback_event_t event, void* param, void* cookie);
176 
177 #if MAJOR_VERSION >= 6
178     static int asyncEventCallback(stream_event_callback_type_t event, void* param, void* cookie);
179 #endif
180 };
181 
182 }  // namespace implementation
183 }  // namespace CPP_VERSION
184 }  // namespace audio
185 }  // namespace hardware
186 }  // namespace android
187 
188 #endif  // ANDROID_HARDWARE_AUDIO_STREAMOUT_H
189