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 <android/hardware/audio/6.0/IStreamIn.h>
19 #include <android/hardware/audio/6.0/IDevice.h>
20 #include "stream_common.h"
21 #include "io_thread.h"
22 #include "primary_device.h"
23 
24 namespace android {
25 namespace hardware {
26 namespace audio {
27 namespace V6_0 {
28 namespace implementation {
29 
30 using ::android::sp;
31 using ::android::hardware::hidl_bitfield;
32 using ::android::hardware::hidl_string;
33 using ::android::hardware::hidl_vec;
34 using ::android::hardware::Return;
35 using namespace ::android::hardware::audio::common::V6_0;
36 using namespace ::android::hardware::audio::V6_0;
37 
38 struct StreamIn : public IStreamIn {
39     StreamIn(sp<PrimaryDevice> dev,
40              int32_t ioHandle,
41              const DeviceAddress& device,
42              const AudioConfig& config,
43              hidl_bitfield<AudioInputFlag> flags,
44              const SinkMetadata& sinkMetadata);
45     ~StreamIn();
46 
47     // IStream
48     Return<uint64_t> getFrameSize() override;
49     Return<uint64_t> getFrameCount() override;
50     Return<uint64_t> getBufferSize() override;
51     Return<uint32_t> getSampleRate() override;
52     Return<void> getSupportedSampleRates(AudioFormat format, getSupportedSampleRates_cb _hidl_cb) override;
53     Return<Result> setSampleRate(uint32_t sampleRateHz) override;
54     Return<hidl_bitfield<AudioChannelMask>> getChannelMask() override;
55     Return<void> getSupportedChannelMasks(AudioFormat format, getSupportedChannelMasks_cb _hidl_cb) override;
56     Return<Result> setChannelMask(hidl_bitfield<AudioChannelMask> mask) override;
57     Return<AudioFormat> getFormat() override;
58     Return<void> getSupportedFormats(getSupportedFormats_cb _hidl_cb) override;
59     Return<Result> setFormat(AudioFormat format) override;
60     Return<void> getAudioProperties(getAudioProperties_cb _hidl_cb) override;
61     Return<Result> addEffect(uint64_t effectId) override;
62     Return<Result> removeEffect(uint64_t effectId) override;
63     Return<Result> standby() override;
64     Return<void> getDevices(getDevices_cb _hidl_cb) override;
65     Return<Result> setDevices(const hidl_vec<DeviceAddress>& devices) override;
66     Return<Result> setHwAvSync(uint32_t hwAvSync) override;
67     Return<void> getParameters(const hidl_vec<ParameterValue>& context,
68                                const hidl_vec<hidl_string>& keys,
69                                getParameters_cb _hidl_cb) override;
70     Return<Result> setParameters(const hidl_vec<ParameterValue>& context,
71                                  const hidl_vec<ParameterValue>& parameters) override;
72     Return<Result> start() override;
73     Return<Result> stop() override;
74     Return<void> createMmapBuffer(int32_t minSizeFrames, createMmapBuffer_cb _hidl_cb) override;
75     Return<void> getMmapPosition(getMmapPosition_cb _hidl_cb) override;
76     Return<Result> close() override;
77 
78     // IStreamIn
79     Return<void> getAudioSource(getAudioSource_cb _hidl_cb) override;
80     Return<Result> setGain(float gain) override;
81     Return<void> updateSinkMetadata(const SinkMetadata& sinkMetadata) override;
82     Return<void> prepareForReading(uint32_t frameSize, uint32_t framesCount,
83                                    prepareForReading_cb _hidl_cb) override;
84     Return<uint32_t> getInputFramesLost() override;
85     Return<void> getCapturePosition(getCapturePosition_cb _hidl_cb) override;
86     Return<void> getActiveMicrophones(getActiveMicrophones_cb _hidl_cb) override;
87     Return<Result> setMicrophoneDirection(MicrophoneDirection direction) override;
88     Return<Result> setMicrophoneFieldDimension(float zoom) override;
89 
getDeviceAddressStreamIn90     const DeviceAddress &getDeviceAddress() const { return mCommon.m_device; }
getAudioConfigStreamIn91     const AudioConfig &getAudioConfig() const { return mCommon.m_config; }
getAudioOutputFlagsStreamIn92     const hidl_bitfield<AudioOutputFlag> &getAudioOutputFlags() const { return mCommon.m_flags; }
93 
getFrameCounterStreamIn94     uint64_t &getFrameCounter() { return mFrames; }
95     void setMicMute(bool mute);
addInputFramesLostStreamIn96     void addInputFramesLost(size_t n) { mInputFramesLost += n; }
getEffectiveVolumeStreamIn97     float getEffectiveVolume() const { return mEffectiveVolume; }
98 
99 private:
100     Result closeImpl(bool fromDctor);
101 
102     sp<PrimaryDevice> mDev;
103     const StreamCommon mCommon;
104     const SinkMetadata mSinkMetadata;
105     std::unique_ptr<IOThread> mReadThread;
106 
107     // The count is not reset to zero when output enters standby.
108     uint64_t mFrames = 0;
109 
110     std::atomic<uint32_t> mInputFramesLost = 0;
111     std::atomic<float> mEffectiveVolume = 1.0f;
112 };
113 
114 }  // namespace implementation
115 }  // namespace V6_0
116 }  // namespace audio
117 }  // namespace hardware
118 }  // namespace android
119