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     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::IStreamIn follow.
101     Return<void> getAudioSource(getAudioSource_cb _hidl_cb) override;
102     Return<Result> setGain(float gain) override;
103     Return<void> prepareForReading(uint32_t frameSize, uint32_t framesCount,
104                                    prepareForReading_cb _hidl_cb) override;
105     Return<uint32_t> getInputFramesLost() override;
106     Return<void> getCapturePosition(getCapturePosition_cb _hidl_cb) override;
107     Return<Result> start() override;
108     Return<Result> stop() override;
109     Return<void> createMmapBuffer(int32_t minSizeFrames, createMmapBuffer_cb _hidl_cb) override;
110     Return<void> getMmapPosition(getMmapPosition_cb _hidl_cb) override;
111 #if MAJOR_VERSION >= 4
112     Return<void> updateSinkMetadata(const SinkMetadata& sinkMetadata) override;
113     Return<void> getActiveMicrophones(getActiveMicrophones_cb _hidl_cb) override;
114 #endif
115 #if MAJOR_VERSION >= 5
116     Return<Result> setMicrophoneDirection(MicrophoneDirection direction) override;
117     Return<Result> setMicrophoneFieldDimension(float zoom) override;
118 #endif
119     static Result getCapturePositionImpl(audio_stream_in_t* stream, uint64_t* frames,
120                                          uint64_t* time);
121 
122    private:
123     const sp<Device> mDevice;
124     audio_stream_in_t* mStream;
125     const sp<Stream> mStreamCommon;
126     const sp<StreamMmap<audio_stream_in_t>> mStreamMmap;
127     std::unique_ptr<CommandMQ> mCommandMQ;
128     std::unique_ptr<DataMQ> mDataMQ;
129     std::unique_ptr<StatusMQ> mStatusMQ;
130     EventFlag* mEfGroup;
131     std::atomic<bool> mStopReadThread;
132     sp<Thread> mReadThread;
133 
134     virtual ~StreamIn();
135 };
136 
137 }  // namespace implementation
138 }  // namespace CPP_VERSION
139 }  // namespace audio
140 }  // namespace hardware
141 }  // namespace android
142 
143 #endif  // ANDROID_HARDWARE_AUDIO_STREAMIN_H
144