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