source: roaraudio/roard/hwmixer.c @ 4387:e8a33de6f3e5

Last change on this file since 4387:e8a33de6f3e5 was 4387:e8a33de6f3e5, checked in by phi, 14 years ago

pass kv-ed subnames to hwmixer drivers

File size: 6.7 KB
Line 
1//hwmixer.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010
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
31struct 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
42static int __true (void) { return 0; }
43
44struct hwmixer g_hwmixers[] = {
45 {"oss",  "OSS Mixer", "/dev/mixer*", FLAG_FHSEC, hwmixer_oss_open, hwmixer_oss_close, hwmixer_oss_set_vol, hwmixer_oss_get_vol},
46 {"dstr", "Write to DSTR",       "/some/file", FLAG_NONE,  hwmixer_dstr_open, hwmixer_dstr_close, hwmixer_dstr_set_vol, NULL},
47 {"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},
48 {NULL,   NULL, NULL, FLAG_NONE, NULL, NULL, NULL, NULL}
49};
50
51void print_hwmixerlist (void) {
52 struct hwmixer * mixer;
53 int i;
54
55 printf("  Source   Flag Subsys - Description (devices)\n");
56 printf("------------------------------------------------------\n");
57
58 for (i = 0; (mixer = &(g_hwmixers[i]))->name != NULL; i++) {
59  printf("  %-9s %c   Mixer  - %s (devices: %s)\n",
60         mixer->name,
61         mixer->flags & FLAG_FHSEC ? 's' : ' ',
62         mixer->desc,
63         mixer->devs == NULL ? "(none)" : mixer->devs
64        );
65 }
66}
67
68void hwmixer_setup_info(struct hwmixer_stream * mstream) {
69 struct roar_stream_server * ss;
70
71 streams_get(mstream->stream, &ss);
72
73 memset(&(ROAR_STREAM(ss)->info), 0, sizeof(ROAR_STREAM(ss)->info));
74
75 streams_set_dir(mstream->stream, ROAR_DIR_MIXING, 1);
76
77 streams_set_flag(mstream->stream, ROAR_FLAG_HWMIXER);
78}
79
80int hwmixer_open(int basestream, char * drv, char * dev, int fh, char * basename, char * subnames) {
81 struct roar_stream_server * ss;
82 struct roar_keyval * subnamekv = NULL;
83 struct hwmixer * mixer = NULL;
84 struct hwmixer_stream * stream;
85 ssize_t subnamekvlen = 0;
86 int i;
87 int ret;
88
89 for (i = 0; g_hwmixers[i].name != NULL; i++) {
90  if ( !strcmp(g_hwmixers[i].name, drv) ) {
91   mixer = &(g_hwmixers[i]);
92   break;
93  }
94 }
95
96 if ( mixer == NULL ) {
97  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);
98  return -1;
99 }
100
101 if ( mixer->open == NULL ) {
102  return -1;
103 }
104
105 if ( fh != -1 && !(mixer->flags & FLAG_FHSEC) ) {
106  return -1;
107 }
108
109 stream = roar_mm_malloc(sizeof(struct hwmixer_stream));
110 if ( stream == NULL )
111  return -1;
112
113 memset(stream, 0, sizeof(struct hwmixer_stream));
114
115 stream->hwmixer    = mixer;
116 stream->basestream = basestream;
117 stream->stream     = basestream;
118 stream->baseud     = NULL;
119 stream->ud         = NULL;
120
121 if ( basename == NULL ) {
122  streams_set_name(basestream, "Hardware Mixer");
123 } else {
124  streams_set_name(basestream, basename);
125 }
126
127 hwmixer_setup_info(stream);
128
129 if ( subnames != NULL ) {
130  subnamekvlen = roar_keyval_split(&subnamekv, subnames, ",;", "=:", 0);
131 }
132
133 ret = mixer->open(stream, drv, dev, fh, basename, subnamekv, subnamekvlen);
134
135 if ( subnamekv != NULL )
136  roar_mm_free(subnamekv);
137
138 if ( ret == -1 ) {
139  roar_mm_free(stream);
140  return -1;
141 }
142
143 streams_set_mixerstream(basestream, stream);
144
145 // try to get in sync with HW mixer.
146 // if possible read hw mixer state.
147 // if not possible write to force a sync value.
148 if ( streams_get(basestream, &ss) == 0 ) {
149  if ( mixer->get_vol != NULL ) {
150   hwmixer_get_volume(basestream, ss, stream, &(ss->mixer));
151  } else {
152   hwmixer_set_volume(basestream, ss, stream, &(ss->mixer));
153  }
154 }
155
156 return 0;
157}
158
159int hwmixer_close(int stream) {
160 struct hwmixer_stream * mstream = streams_get_mixerstream(stream);
161
162 if ( mstream == NULL )
163  return 0;
164
165 if ( mstream->hwmixer->close != NULL )
166  mstream->hwmixer->close(mstream);
167
168 roar_mm_free(mstream);
169
170 return 0;
171}
172
173int hwmixer_set_volume(int id, struct roar_stream_server * ss, struct hwmixer_stream * mstream, struct roar_mixer_settings *
174settings) {
175 if ( mstream->hwmixer->set_vol != NULL )
176  return mstream->hwmixer->set_vol(mstream, ROAR_STREAM(ss)->info.channels, HWMIXER_MODE_SET, settings);
177
178 return 0;
179}
180
181int hwmixer_get_volume(int id, struct roar_stream_server * ss, struct hwmixer_stream * mstream, struct roar_mixer_settings *
182settings) {
183 if ( mstream->hwmixer->get_vol != NULL )
184  return mstream->hwmixer->get_vol(mstream, ROAR_STREAM(ss)->info.channels, HWMIXER_MODE_ASK, settings);
185
186 return 0;
187}
188
189struct hwmixer_stream * hwmixer_substream_new(struct hwmixer_stream * parent) {
190 struct roar_stream_server * ss;
191 struct hwmixer_stream * stream;
192 int id;
193
194 ROAR_DBG("hwmixer_substream_new(parent=%p) = ?", parent);
195
196 if ( parent == NULL )
197  return NULL;
198
199 ROAR_DBG("hwmixer_substream_new(parent=%p) = ?", parent);
200
201 stream = roar_mm_malloc(sizeof(struct hwmixer_stream));
202 if ( stream == NULL )
203  return NULL;
204
205 ROAR_DBG("hwmixer_substream_new(parent=%p) = ?", parent);
206
207 if ( (id = streams_new_virtual(parent->basestream, &ss)) == -1 ) {
208  roar_mm_free(stream);
209  return NULL;
210 }
211
212 memset(stream, 0, sizeof(struct hwmixer_stream));
213
214 stream->hwmixer    = parent->hwmixer;
215 stream->basestream = parent->basestream;
216 stream->stream     = id;
217 stream->baseud     = parent->baseud;
218 stream->ud         = NULL;
219
220 hwmixer_setup_info(stream);
221
222 streams_set_mixerstream(id, stream);
223
224 ROAR_DBG("hwmixer_substream_new(parent=%p) = %p", parent, stream);
225
226 return stream;
227}
228
229//ll
Note: See TracBrowser for help on using the repository browser.