jiabin | 783c48b | 2023-02-28 18:28:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 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 | |
| 19 | #include <map> |
| 20 | #include <memory> |
| 21 | #include <mutex> |
| 22 | #include <optional> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include <android-base/thread_annotations.h> |
| 27 | #include <android/binder_auto_utils.h> |
| 28 | |
| 29 | extern "C" { |
| 30 | #include <tinyalsa/mixer.h> |
| 31 | } |
| 32 | |
| 33 | namespace aidl::android::hardware::audio::core::usb { |
| 34 | |
| 35 | class MixerControl { |
| 36 | public: |
| 37 | explicit MixerControl(struct mixer_ctl* ctl); |
| 38 | |
| 39 | unsigned int getNumValues() const; |
| 40 | int getMaxValue() const; |
| 41 | int getMinValue() const; |
| 42 | int setArray(const void* array, size_t count); |
| 43 | |
| 44 | private: |
| 45 | std::mutex mLock; |
| 46 | // The mixer_ctl object is owned by ALSA and will be released when the mixer is closed. |
| 47 | struct mixer_ctl* mCtl GUARDED_BY(mLock); |
| 48 | const unsigned int mNumValues; |
| 49 | const int mMinValue; |
| 50 | const int mMaxValue; |
| 51 | }; |
| 52 | |
| 53 | class AlsaMixer { |
| 54 | public: |
| 55 | explicit AlsaMixer(struct mixer* mixer); |
| 56 | |
| 57 | ~AlsaMixer(); |
| 58 | |
| 59 | bool isValid() const { return mMixer != nullptr; } |
| 60 | |
| 61 | ndk::ScopedAStatus setMasterMute(bool muted); |
| 62 | ndk::ScopedAStatus setMasterVolume(float volume); |
| 63 | ndk::ScopedAStatus setVolumes(std::vector<float> volumes); |
| 64 | |
| 65 | private: |
| 66 | enum Control { |
| 67 | MASTER_SWITCH, |
| 68 | MASTER_VOLUME, |
| 69 | HW_VOLUME, |
| 70 | }; |
| 71 | using ControlNamesAndExpectedCtlType = std::pair<std::string, enum mixer_ctl_type>; |
| 72 | static const std::map<Control, std::vector<ControlNamesAndExpectedCtlType>> kPossibleControls; |
| 73 | static std::map<Control, std::shared_ptr<MixerControl>> initializeMixerControls( |
| 74 | struct mixer* mixer); |
| 75 | |
| 76 | // The mixer object is owned by ALSA and will be released when the mixer is closed. |
| 77 | struct mixer* mMixer; |
| 78 | // `mMixerControls` will only be initialized in constructor. After that, it wil only be |
| 79 | // read but not be modified. |
| 80 | const std::map<Control, std::shared_ptr<MixerControl>> mMixerControls; |
| 81 | }; |
| 82 | |
| 83 | class UsbAlsaMixerControl { |
| 84 | public: |
| 85 | static UsbAlsaMixerControl& getInstance(); |
| 86 | |
| 87 | void setDeviceConnectionState(int card, bool masterMuted, float masterVolume, bool connected); |
| 88 | |
| 89 | // Master volume settings will be applied to all sound cards, it is only set by the |
| 90 | // USB module. |
| 91 | ndk::ScopedAStatus setMasterMute(bool muted); |
| 92 | ndk::ScopedAStatus setMasterVolume(float volume); |
| 93 | // The volume settings can be different on sound cards. It is controlled by streams. |
| 94 | ndk::ScopedAStatus setVolumes(int card, std::vector<float> volumes); |
| 95 | |
| 96 | private: |
| 97 | std::shared_ptr<AlsaMixer> getAlsaMixer(int card); |
| 98 | std::map<int, std::shared_ptr<AlsaMixer>> getAlsaMixers(); |
| 99 | |
| 100 | std::mutex mLock; |
| 101 | // A map whose key is the card number and value is a shared pointer to corresponding |
| 102 | // AlsaMixer object. |
| 103 | std::map<int, std::shared_ptr<AlsaMixer>> mMixerControls GUARDED_BY(mLock); |
| 104 | }; |
| 105 | |
| 106 | } // namespace aidl::android::hardware::audio::core::usb |