1 | //hwmixer.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011 |
---|
5 | * |
---|
6 | * This file is part of roard a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | #include "roard.h" |
---|
27 | |
---|
28 | #define FLAG_NONE 0x0000 |
---|
29 | #define FLAG_FHSEC 0x0001 |
---|
30 | |
---|
31 | struct hwmixer { |
---|
32 | const char * name; |
---|
33 | const char * desc; |
---|
34 | const char * devs; |
---|
35 | int flags; |
---|
36 | int (*open)(struct hwmixer_stream * stream, char * drv, char * dev, int fh, char * basename, struct roar_keyval * subnames, size_t subnamelen); |
---|
37 | int (*close)(struct hwmixer_stream * stream); |
---|
38 | int (*set_vol)(struct hwmixer_stream * stream, int channels, int mode, struct roar_mixer_settings * settings); |
---|
39 | int (*get_vol)(struct hwmixer_stream * stream, int channels, int mode, struct roar_mixer_settings * settings); |
---|
40 | }; |
---|
41 | |
---|
42 | static int __true (void) { return 0; } |
---|
43 | |
---|
44 | struct hwmixer g_hwmixers[] = { |
---|
45 | #if defined(ROAR_HAVE_OSS_BSD) || defined(ROAR_HAVE_OSS) |
---|
46 | {"oss", "OSS Mixer", "/dev/mixer*", FLAG_FHSEC, hwmixer_oss_open, hwmixer_oss_close, hwmixer_oss_set_vol, hwmixer_oss_get_vol}, |
---|
47 | #endif |
---|
48 | {"dstr", "Write to DSTR", "/some/file", FLAG_FHSEC, hwmixer_dstr_open, hwmixer_dstr_close, hwmixer_dstr_set_vol, NULL}, |
---|
49 | {"null", "Null Mixer", NULL, FLAG_NONE, (int (*)(struct hwmixer_stream * stream, char * drv, char * dev, int fh, char * basename, struct roar_keyval * subnames, size_t subnamelen))__true, (int (*)(struct hwmixer_stream * stream))__true, (int (*)(struct hwmixer_stream * stream, int channels, int mode, struct roar_mixer_settings * settings))__true, (int (*)(struct hwmixer_stream * stream, int channels, int mode, struct roar_mixer_settings * settings))__true}, |
---|
50 | {NULL, NULL, NULL, FLAG_NONE, NULL, NULL, NULL, NULL} |
---|
51 | }; |
---|
52 | |
---|
53 | void print_hwmixerlist (void) { |
---|
54 | struct hwmixer * mixer; |
---|
55 | int i; |
---|
56 | |
---|
57 | printf(" Source Flag Subsys - Description (devices)\n"); |
---|
58 | printf("------------------------------------------------------\n"); |
---|
59 | |
---|
60 | for (i = 0; (mixer = &(g_hwmixers[i]))->name != NULL; i++) { |
---|
61 | printf(" %-9s %c Mixer - %s (devices: %s)\n", |
---|
62 | mixer->name, |
---|
63 | mixer->flags & FLAG_FHSEC ? 's' : ' ', |
---|
64 | mixer->desc, |
---|
65 | mixer->devs == NULL ? "(none)" : mixer->devs |
---|
66 | ); |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | void hwmixer_setup_info(struct hwmixer_stream * mstream) { |
---|
71 | struct roar_stream_server * ss; |
---|
72 | |
---|
73 | streams_get(mstream->stream, &ss); |
---|
74 | |
---|
75 | memset(&(ROAR_STREAM(ss)->info), 0, sizeof(ROAR_STREAM(ss)->info)); |
---|
76 | |
---|
77 | streams_set_dir(mstream->stream, ROAR_DIR_MIXING, 1); |
---|
78 | |
---|
79 | streams_set_flag(mstream->stream, ROAR_FLAG_HWMIXER); |
---|
80 | } |
---|
81 | |
---|
82 | int hwmixer_open(int basestream, char * drv, char * dev, int fh, char * basename, char * subnames) { |
---|
83 | struct roar_stream_server * ss; |
---|
84 | struct roar_keyval * subnamekv = NULL; |
---|
85 | struct hwmixer * mixer = NULL; |
---|
86 | struct hwmixer_stream * stream; |
---|
87 | ssize_t subnamekvlen = 0; |
---|
88 | int i; |
---|
89 | int ret; |
---|
90 | |
---|
91 | for (i = 0; g_hwmixers[i].name != NULL; i++) { |
---|
92 | if ( !strcmp(g_hwmixers[i].name, drv) ) { |
---|
93 | mixer = &(g_hwmixers[i]); |
---|
94 | break; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | if ( mixer == NULL ) { |
---|
99 | ROAR_WARN("hwmixer_open(basestream=%i, drv='%s', dev='%s', fh=%i, basename='%s', subnames='%s'): Driver not found.", basestream, drv, dev, fh, basename, subnames); |
---|
100 | return -1; |
---|
101 | } |
---|
102 | |
---|
103 | if ( mixer->open == NULL ) { |
---|
104 | return -1; |
---|
105 | } |
---|
106 | |
---|
107 | if ( fh != -1 && !(mixer->flags & FLAG_FHSEC) ) { |
---|
108 | return -1; |
---|
109 | } |
---|
110 | |
---|
111 | stream = roar_mm_malloc(sizeof(struct hwmixer_stream)); |
---|
112 | if ( stream == NULL ) |
---|
113 | return -1; |
---|
114 | |
---|
115 | memset(stream, 0, sizeof(struct hwmixer_stream)); |
---|
116 | |
---|
117 | stream->hwmixer = mixer; |
---|
118 | stream->basestream = basestream; |
---|
119 | stream->stream = basestream; |
---|
120 | stream->baseud = NULL; |
---|
121 | stream->ud = NULL; |
---|
122 | |
---|
123 | if ( basename == NULL ) { |
---|
124 | streams_set_name(basestream, "Hardware Mixer"); |
---|
125 | } else { |
---|
126 | streams_set_name(basestream, basename); |
---|
127 | } |
---|
128 | |
---|
129 | hwmixer_setup_info(stream); |
---|
130 | |
---|
131 | if ( subnames != NULL ) { |
---|
132 | subnamekvlen = roar_keyval_split(&subnamekv, subnames, ",;", "=:", 0); |
---|
133 | } |
---|
134 | |
---|
135 | ret = mixer->open(stream, drv, dev, fh, basename, subnamekv, subnamekvlen); |
---|
136 | |
---|
137 | if ( subnamekv != NULL ) |
---|
138 | roar_mm_free(subnamekv); |
---|
139 | |
---|
140 | if ( ret == -1 ) { |
---|
141 | roar_mm_free(stream); |
---|
142 | return -1; |
---|
143 | } |
---|
144 | |
---|
145 | streams_set_mixerstream(basestream, stream); |
---|
146 | |
---|
147 | // try to get in sync with HW mixer. |
---|
148 | // if possible read hw mixer state. |
---|
149 | // if not possible write to force a sync value. |
---|
150 | if ( streams_get(basestream, &ss) == 0 ) { |
---|
151 | if ( mixer->get_vol != NULL ) { |
---|
152 | hwmixer_get_volume(basestream, ss, stream, &(ss->mixer)); |
---|
153 | } else { |
---|
154 | hwmixer_set_volume(basestream, ss, stream, &(ss->mixer)); |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | return 0; |
---|
159 | } |
---|
160 | |
---|
161 | int hwmixer_close(int stream) { |
---|
162 | struct hwmixer_stream * mstream = streams_get_mixerstream(stream); |
---|
163 | |
---|
164 | if ( mstream == NULL ) |
---|
165 | return 0; |
---|
166 | |
---|
167 | if ( mstream->hwmixer->close != NULL ) |
---|
168 | mstream->hwmixer->close(mstream); |
---|
169 | |
---|
170 | roar_mm_free(mstream); |
---|
171 | |
---|
172 | return 0; |
---|
173 | } |
---|
174 | |
---|
175 | int hwmixer_set_volume(int id, struct roar_stream_server * ss, struct hwmixer_stream * mstream, struct roar_mixer_settings * |
---|
176 | settings) { |
---|
177 | if ( mstream->hwmixer->set_vol != NULL ) |
---|
178 | return mstream->hwmixer->set_vol(mstream, ROAR_STREAM(ss)->info.channels, HWMIXER_MODE_SET, settings); |
---|
179 | |
---|
180 | return 0; |
---|
181 | } |
---|
182 | |
---|
183 | int hwmixer_get_volume(int id, struct roar_stream_server * ss, struct hwmixer_stream * mstream, struct roar_mixer_settings * |
---|
184 | settings) { |
---|
185 | if ( mstream->hwmixer->get_vol != NULL ) |
---|
186 | return mstream->hwmixer->get_vol(mstream, ROAR_STREAM(ss)->info.channels, HWMIXER_MODE_ASK, settings); |
---|
187 | |
---|
188 | return 0; |
---|
189 | } |
---|
190 | |
---|
191 | struct hwmixer_stream * hwmixer_substream_new(struct hwmixer_stream * parent) { |
---|
192 | struct roar_stream_server * ss; |
---|
193 | struct hwmixer_stream * stream; |
---|
194 | int id; |
---|
195 | |
---|
196 | ROAR_DBG("hwmixer_substream_new(parent=%p) = ?", parent); |
---|
197 | |
---|
198 | if ( parent == NULL ) |
---|
199 | return NULL; |
---|
200 | |
---|
201 | ROAR_DBG("hwmixer_substream_new(parent=%p) = ?", parent); |
---|
202 | |
---|
203 | stream = roar_mm_malloc(sizeof(struct hwmixer_stream)); |
---|
204 | if ( stream == NULL ) |
---|
205 | return NULL; |
---|
206 | |
---|
207 | ROAR_DBG("hwmixer_substream_new(parent=%p) = ?", parent); |
---|
208 | |
---|
209 | if ( (id = streams_new_virtual(parent->basestream, &ss)) == -1 ) { |
---|
210 | roar_mm_free(stream); |
---|
211 | return NULL; |
---|
212 | } |
---|
213 | |
---|
214 | memset(stream, 0, sizeof(struct hwmixer_stream)); |
---|
215 | |
---|
216 | stream->hwmixer = parent->hwmixer; |
---|
217 | stream->basestream = parent->basestream; |
---|
218 | stream->stream = id; |
---|
219 | stream->baseud = parent->baseud; |
---|
220 | stream->ud = NULL; |
---|
221 | |
---|
222 | hwmixer_setup_info(stream); |
---|
223 | |
---|
224 | streams_set_mixerstream(id, stream); |
---|
225 | |
---|
226 | ROAR_DBG("hwmixer_substream_new(parent=%p) = %p", parent, stream); |
---|
227 | |
---|
228 | return stream; |
---|
229 | } |
---|
230 | |
---|
231 | //ll |
---|