summaryrefslogtreecommitdiff
path: root/cras/src/server/cras_alsa_mixer.h
blob: a6b490f0aadaa4e2bf2ae7ccea155a3a72e2226f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef _CRAS_ALSA_MIXER_H
#define _CRAS_ALSA_MIXER_H

#include <alsa/asoundlib.h>
#include <iniparser.h>

#include "cras_types.h"

/* cras_alsa_mixer represents the alsa mixer interface for an alsa card.  It
 * houses the volume and mute controls as well as playback switches for
 * headphones and mic.
 */

struct mixer_control;
struct cras_alsa_mixer;
struct cras_volume_curve;
struct cras_card_config;
struct mixer_name;
struct ucm_section;

/* Creates a cras_alsa_mixer instance for the given alsa device.
 * Args:
 *    card_name - Name of the card to open a mixer for.  This is an alsa name of
 *      the form "hw:X" where X ranges from 0 to 31 inclusive.
 * Returns:
 *    A pointer to the newly created cras_alsa_mixer which must later be freed
 *    by calling cras_alsa_mixer_destroy. The control in the mixer is not added
 *    yet.
 */
struct cras_alsa_mixer *cras_alsa_mixer_create(const char *card_name);

/* Adds controls to a cras_alsa_mixer from the given UCM section.
 * Args:
 *    cmix - A pointer to cras_alsa_mixer.
 *    section - A UCM section.
 * Returns:
 *    0 on success. Negative error code otherwise.
 */
int cras_alsa_mixer_add_controls_in_section(
		struct cras_alsa_mixer *cmix,
		struct ucm_section *section);

/* Adds controls to a cras_alsa_mixer instance by name matching.
 * Args:
 *    cmix - A pointer to cras_alsa_mixer.
 *    extra_controls - A list array of extra mixer control names, always added.
 *    coupled_controls - A list of coupled mixer control names.
 * Returns:
 *    0 on success. Other error code if error happens.
 */
int cras_alsa_mixer_add_controls_by_name_matching(
		struct cras_alsa_mixer *cmix,
		struct mixer_name *extra_controls,
		struct mixer_name *coupled_controls);

/* Destroys a cras_alsa_mixer that was returned from cras_alsa_mixer_create.
 * Args:
 *    cras_mixer - The cras_alsa_mixer pointer returned from
 *        cras_alsa_mixer_create.
 */
void cras_alsa_mixer_destroy(struct cras_alsa_mixer *cras_mixer);

/* Returns if the mixer has any main volume control. */
int cras_alsa_mixer_has_main_volume(const struct cras_alsa_mixer *cras_mixer);

/* Returns if the mixer control supports volume adjust. */
int cras_alsa_mixer_has_volume(const struct mixer_control *mixer_control);

/* Sets the output volume for the device associated with this mixer.
 * Args:
 *    cras_mixer - The mixer to set the volume on.
 *    dBFS - The volume level as dB * 100.  dB is a normally a negative quantity
 *      specifying how much to attenuate.
 *    mixer_output - The mixer output to set if not all attenuation can be
 *      obtained from the main controls.  Can be null.
 */
void cras_alsa_mixer_set_dBFS(struct cras_alsa_mixer *cras_mixer,
			      long dBFS,
			      struct mixer_control *mixer_output);

/* Gets the volume range of the mixer in dB.
 * Args:
 *    cras_mixer - The mixer to get the volume range.
 */
long cras_alsa_mixer_get_dB_range(struct cras_alsa_mixer *cras_mixer);

/* Gets the volume range of the mixer output in dB.
 * Args:
 *    mixer_output - The mixer output to get the volume range.
 */
long cras_alsa_mixer_get_output_dB_range(
		struct mixer_control *mixer_output);

/* Sets the capture gain for the device associated with this mixer.
 * Args:
 *    cras_mixer - The mixer to set the volume on.
 *    dBFS - The capture gain level as dB * 100.  dB can be a positive or a
 *    negative quantity specifying how much gain or attenuation to apply.
 *    mixer_input - The specific mixer control for input node, can be null.
 */
void cras_alsa_mixer_set_capture_dBFS(struct cras_alsa_mixer *cras_mixer,
				      long dBFS,
				      struct mixer_control* mixer_input);

/* Gets the minimum allowed setting for capture gain.
 * Args:
 *    cmix - The mixer to set the capture gain on.
 *    mixer_input - The additional input mixer control, mainly specified
 *      in ucm config. Can be null.
 * Returns:
 *    The minimum allowed capture gain in dBFS * 100.
 */
long cras_alsa_mixer_get_minimum_capture_gain(
                struct cras_alsa_mixer *cmix,
		struct mixer_control *mixer_input);

/* Gets the maximum allowed setting for capture gain.
 * Args:
 *    cmix - The mixer to set the capture gain on.
 *    mixer_input - The additional input mixer control, mainly specified
 *      in ucm config. Can be null.
 * Returns:
 *    The maximum allowed capture gain in dBFS * 100.
 */
long cras_alsa_mixer_get_maximum_capture_gain(
		struct cras_alsa_mixer *cmix,
		struct mixer_control *mixer_input);

/* Sets the playback switch for the device.
 * Args:
 *    cras_mixer - Mixer to set the volume in.
 *    muted - 1 if muted, 0 if not.
 *    mixer_output - The mixer output to mute if no master mute.
 */
void cras_alsa_mixer_set_mute(struct cras_alsa_mixer *cras_mixer,
			      int muted,
			      struct mixer_control *mixer_output);

/* Sets the capture switch for the device.
 * Args:
 *    cras_mixer - Mixer to set the volume in.
 *    muted - 1 if muted, 0 if not.
 *    mixer_input - The mixer input to mute if no master mute.
 */
void cras_alsa_mixer_set_capture_mute(struct cras_alsa_mixer *cras_mixer,
				      int muted,
				      struct mixer_control *mixer_input);

/* Invokes the provided callback once for each output (input).
 * The callback will be provided with a reference to the control
 * that can be queried to see what the control supports.
 * Args:
 *    cras_mixer - Mixer to set the volume in.
 *    cb - Function to call for each output (input).
 *    cb_arg - Argument to pass to cb.
 */
typedef void (*cras_alsa_mixer_control_callback)(
		struct mixer_control *control, void *arg);
void cras_alsa_mixer_list_outputs(struct cras_alsa_mixer *cras_mixer,
				  cras_alsa_mixer_control_callback cb,
				  void *cb_arg);

void cras_alsa_mixer_list_inputs(struct cras_alsa_mixer *cras_mixer,
				 cras_alsa_mixer_control_callback cb,
				 void *cb_arg);

/* Gets the name of a given control. */
const char *cras_alsa_mixer_get_control_name(
		const struct mixer_control *control);

/* Returns the mixer control matching the given direction and name.
 * Args:
 *    cras_mixer - Mixer to search for a control.
 *    dir - Control's direction (OUTPUT or INPUT).
 *    name - Name to search for.
 *    create_missing - When non-zero, attempt to create a new control with
 *		       the given name.
 * Returns:
 *    A pointer to the matching mixer control, or NULL if none found.
 */
struct mixer_control *cras_alsa_mixer_get_control_matching_name(
		struct cras_alsa_mixer *cras_mixer,
		enum CRAS_STREAM_DIRECTION dir, const char *name,
		int create_missing);

/* Returns the mixer control associated with the given section.
 * The control is the one that matches 'mixer_name', or if that is not defined
 * then it will be the control matching 'section->name', based on the
 * coupled mixer controls.
 * Args:
 *    cras_mixer - Mixer to search for a control.
 *    section - Associated UCM section.
 * Returns:
 *    A pointer to the associated mixer control, or NULL if none found.
 */
struct mixer_control *cras_alsa_mixer_get_control_for_section(
		struct cras_alsa_mixer *cras_mixer,
		const struct ucm_section *section);

/* Finds the output that matches the given string.  Used to match Jacks to Mixer
 * elements.
 * Args:
 *    cras_mixer - Mixer to search for a control.
 *    name - The name to match against the controls.
 * Returns:
 *    A pointer to the output with a mixer control that matches "name".
 */
struct mixer_control *cras_alsa_mixer_get_output_matching_name(
		struct cras_alsa_mixer *cras_mixer,
		const char *name);

/* Finds the mixer control for that matches the control name of input control
 * name specified in ucm config.
 * Args:
 *    cras_mixer - Mixer to search for a control.
 *    name - Name of the control to search for.
 * Returns:
 *    A pointer to the input with a mixer control that matches "name".
 */
struct mixer_control *cras_alsa_mixer_get_input_matching_name(
		struct cras_alsa_mixer *cras_mixer,
		const char *name);

/* Sets the given output active or inactive. */
int cras_alsa_mixer_set_output_active_state(
		struct mixer_control *output,
		int active);

#endif /* _CRAS_ALSA_MIXER_H */