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_STREAMIN_H
18 #define ANDROID_HARDWARE_AUDIO_STREAMIN_H
19 
20 #include PATH(android/hardware/audio/FILE_VERSION/IStreamIn.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 StreamIn : public IStreamIn {
49     typedef MessageQueue<ReadParameters, kSynchronizedReadWrite> CommandMQ;
50     typedef MessageQueue<uint8_t, kSynchronizedReadWrite> DataMQ;
51     typedef MessageQueue<ReadStatus, kSynchronizedReadWrite> StatusMQ;
52 
53     StreamIn(const sp<Device>& device, audio_stream_in_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 #if MAJOR_VERSION <= 6
60     Return<uint32_t> getSampleRate() override;
61 #if MAJOR_VERSION == 2
62     Return<void> getSupportedSampleRates(getSupportedSampleRates_cb _hidl_cb) override;
63     Return<void> getSupportedChannelMasks(getSupportedChannelMasks_cb _hidl_cb) override;
64 #endif
65     Return<void> getSupportedSampleRates(AudioFormat format, getSupportedSampleRates_cb _hidl_cb);
66     Return<void> getSupportedChannelMasks(AudioFormat format, getSupportedChannelMasks_cb _hidl_cb);
67     Return<Result> setSampleRate(uint32_t sampleRateHz) override;
68     Return<AudioChannelBitfield> getChannelMask() override;
69     Return<Result> setChannelMask(AudioChannelBitfield mask) override;
70     Return<AudioFormat> getFormat() override;
71     Return<void> getSupportedFormats(getSupportedFormats_cb _hidl_cb) override;
72     Return<Result> setFormat(AudioFormat format) override;
73 #else
74     Return<void> getSupportedProfiles(getSupportedProfiles_cb _hidl_cb) override;
75     Return<Result> setAudioProperties(const AudioConfigBaseOptional& config) override;
76 #endif  // MAJOR_VERSION <= 6
77     Return<void> getAudioProperties(getAudioProperties_cb _hidl_cb) override;
78     Return<Result> addEffect(uint64_t effectId) override;
79     Return<Result> removeEffect(uint64_t effectId) override;
80     Return<Result> standby() override;
81 #if MAJOR_VERSION == 2
82     Return<AudioDevice> getDevice() override;
83     Return<Result> setDevice(const DeviceAddress& address) override;
84     Return<void> getParameters(const hidl_vec<hidl_string>& keys,
85                                getParameters_cb _hidl_cb) override;
86     Return<Result> setParameters(const hidl_vec<ParameterValue>& parameters) override;
87     Return<Result> setConnectedState(const DeviceAddress& address, bool connected) override;
88 #elif MAJOR_VERSION >= 4
89     Return<void> getDevices(getDevices_cb _hidl_cb) override;
90     Return<Result> setDevices(const hidl_vec<DeviceAddress>& devices) override;
91     Return<void> getParameters(const hidl_vec<ParameterValue>& context,
92                                const hidl_vec<hidl_string>& keys,
93                                getParameters_cb _hidl_cb) override;
94     Return<Result> setParameters(const hidl_vec<ParameterValue>& context,
95                                  const hidl_vec<ParameterValue>& parameters) override;
96 #endif
97     Return<Result> setHwAvSync(uint32_t hwAvSync) override;
98     Return<Result> close() override;
99 
100     Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& options) override;
101 #if MAJOR_VERSION == 2
102     Return<void> debugDump(const hidl_handle& fd) override;
103 #endif
104 
105     // Methods from ::android::hardware::audio::CPP_VERSION::IStreamIn follow.
106     Return<void> getAudioSource(getAudioSource_cb _hidl_cb) override;
107     Return<Result> setGain(float gain) override;
108     Return<void> prepareForReading(uint32_t frameSize, uint32_t framesCount,
109                                    prepareForReading_cb _hidl_cb) override;
110     Return<uint32_t> getInputFramesLost() override;
111     Return<void> getCapturePosition(getCapturePosition_cb _hidl_cb) override;
112     Return<Result> start() override;
113     Return<Result> stop() override;
114     Return<void> createMmapBuffer(int32_t minSizeFrames, createMmapBuffer_cb _hidl_cb) override;
115     Return<void> getMmapPosition(getMmapPosition_cb _hidl_cb) override;
116 #if MAJOR_VERSION >= 4
117 #if MAJOR_VERSION <= 6
118     Return<void> updateSinkMetadata(const SinkMetadata& sinkMetadata) override;
119 #else
120     Return<Result> updateSinkMetadata(const SinkMetadata& sinkMetadata) override;
121 #endif
122     Return<void> getActiveMicrophones(getActiveMicrophones_cb _hidl_cb) override;
123 #endif  // MAJOR_VERSION >= 4
124 #if MAJOR_VERSION >= 5
125     Return<Result> setMicrophoneDirection(MicrophoneDirection direction) override;
126     Return<Result> setMicrophoneFieldDimension(float zoom) override;
127 #endif
128     static Result getCapturePositionImpl(audio_stream_in_t* stream, uint64_t* frames,
129                                          uint64_t* time);
130 
131   private:
132 #if MAJOR_VERSION >= 4
133     Result doUpdateSinkMetadata(const SinkMetadata& sinkMetadata);
134 #if MAJOR_VERSION >= 7
135     Result doUpdateSinkMetadataV7(const SinkMetadata& sinkMetadata);
136 #endif
137 #endif  // MAJOR_VERSION >= 4
138 
139     const sp<Device> mDevice;
140     audio_stream_in_t* mStream;
141     const sp<Stream> mStreamCommon;
142     const sp<StreamMmap<audio_stream_in_t>> mStreamMmap;
143     std::unique_ptr<CommandMQ> mCommandMQ;
144     std::unique_ptr<DataMQ> mDataMQ;
145     std::unique_ptr<StatusMQ> mStatusMQ;
146     EventFlag* mEfGroup;
147     std::atomic<bool> mStopReadThread;
148     sp<Thread> mReadThread;
149 
150     virtual ~StreamIn();
151 };
152 
153 }  // namespace implementation
154 }  // namespace CPP_VERSION
155 }  // namespace audio
156 }  // namespace hardware
157 }  // namespace android
158 
159 #endif  // ANDROID_HARDWARE_AUDIO_STREAMIN_H
160