blob: de9e6f42cdc528a662d88ac655fcf646ffc745b3 [file] [log] [blame]
Mikhail Naganova4513dc2023-07-07 12:01:17 -07001/*
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 <string>
23#include <vector>
24
25#include <android-base/thread_annotations.h>
26#include <android/binder_auto_utils.h>
27
28extern "C" {
29#include <tinyalsa/mixer.h>
30}
31
32namespace aidl::android::hardware::audio::core::alsa {
33
34class MixerControl {
35 public:
36 explicit MixerControl(struct mixer_ctl* ctl);
37
38 unsigned int getNumValues() const;
39 int getMaxValue() const;
40 int getMinValue() const;
41 int setArray(const void* array, size_t count);
42
43 private:
44 std::mutex mLock;
45 // The mixer_ctl object is owned by ALSA and will be released when the mixer is closed.
46 struct mixer_ctl* mCtl GUARDED_BY(mLock);
47 const unsigned int mNumValues;
48 const int mMinValue;
49 const int mMaxValue;
50};
51
52class Mixer {
53 public:
54 explicit Mixer(struct mixer* mixer);
55
56 ~Mixer();
57
58 bool isValid() const { return mMixer != nullptr; }
59
60 ndk::ScopedAStatus setMasterMute(bool muted);
61 ndk::ScopedAStatus setMasterVolume(float volume);
62 ndk::ScopedAStatus setVolumes(const std::vector<float>& volumes);
63
64 private:
65 enum Control {
66 MASTER_SWITCH,
67 MASTER_VOLUME,
68 HW_VOLUME,
69 };
70 using ControlNamesAndExpectedCtlType = std::pair<std::string, enum mixer_ctl_type>;
71 static const std::map<Control, std::vector<ControlNamesAndExpectedCtlType>> kPossibleControls;
72 static std::map<Control, std::shared_ptr<MixerControl>> initializeMixerControls(
73 struct mixer* mixer);
74
75 // The mixer object is owned by ALSA and will be released when the mixer is closed.
76 struct mixer* mMixer;
77 // `mMixerControls` will only be initialized in constructor. After that, it wil only be
78 // read but not be modified.
79 const std::map<Control, std::shared_ptr<MixerControl>> mMixerControls;
80};
81
82} // namespace aidl::android::hardware::audio::core::alsa