1 | //driver_pulsesimple.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2013 |
---|
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 | #ifdef ROAR_HAVE_LIBPULSE |
---|
29 | |
---|
30 | int driver_pulsesimple_open (struct roar_vio_calls * inst, char * device, struct roar_audio_info * info, int fh, struct roar_stream_server * sstream) { |
---|
31 | struct driver_pulsesimple * self; |
---|
32 | const char * subdev = NULL; |
---|
33 | int pulseerror = -1; |
---|
34 | pa_stream_direction_t dir = PA_STREAM_PLAYBACK; |
---|
35 | pa_sample_spec ss; |
---|
36 | int autoconfig = 0; |
---|
37 | int needauto = 0; |
---|
38 | |
---|
39 | // pa_sample_format_t format; /**< The sample format */ |
---|
40 | |
---|
41 | if ( fh != -1 ) |
---|
42 | return -1; |
---|
43 | |
---|
44 | if ( sstream != NULL ) { |
---|
45 | autoconfig = streams_get_flag(ROAR_STREAM(sstream)->id, ROAR_FLAG_AUTOCONF); |
---|
46 | } |
---|
47 | |
---|
48 | switch (info->codec) { |
---|
49 | case ROAR_CODEC_ALAW: |
---|
50 | ss.format = PA_SAMPLE_ALAW; |
---|
51 | break; |
---|
52 | case ROAR_CODEC_MULAW: |
---|
53 | ss.format = PA_SAMPLE_ULAW; |
---|
54 | break; |
---|
55 | case ROAR_CODEC_PCM_S_LE: |
---|
56 | switch (info->bits) { |
---|
57 | case 16: |
---|
58 | ss.format = PA_SAMPLE_S16LE; |
---|
59 | break; |
---|
60 | default: |
---|
61 | needauto = 1; |
---|
62 | break; |
---|
63 | } |
---|
64 | break; |
---|
65 | case ROAR_CODEC_PCM_S_BE: |
---|
66 | switch (info->bits) { |
---|
67 | case 16: |
---|
68 | ss.format = PA_SAMPLE_S16BE; |
---|
69 | break; |
---|
70 | default: |
---|
71 | needauto = 1; |
---|
72 | break; |
---|
73 | } |
---|
74 | break; |
---|
75 | case ROAR_CODEC_PCM_U_LE: |
---|
76 | case ROAR_CODEC_PCM_U_BE: |
---|
77 | case ROAR_CODEC_PCM_U_PDP: |
---|
78 | if ( info->bits == 8 ) { |
---|
79 | ss.format = PA_SAMPLE_U8; |
---|
80 | } else { |
---|
81 | needauto = 1; |
---|
82 | } |
---|
83 | break; |
---|
84 | default: |
---|
85 | needauto = 1; |
---|
86 | break; |
---|
87 | } |
---|
88 | |
---|
89 | if ( needauto ) { |
---|
90 | if ( !autoconfig ) { |
---|
91 | return -1; |
---|
92 | } |
---|
93 | |
---|
94 | info->bits = 16; |
---|
95 | info->codec = ROAR_CODEC_DEFAULT; |
---|
96 | ss.format = PA_SAMPLE_S16NE; |
---|
97 | } |
---|
98 | |
---|
99 | ss.rate = info->rate; |
---|
100 | ss.channels = info->channels; |
---|
101 | |
---|
102 | if ( (self = roar_mm_malloc(sizeof(struct driver_pulsesimple))) == NULL ) |
---|
103 | return -1; |
---|
104 | |
---|
105 | self->handle = pa_simple_new(device, "roard", dir, subdev, "RoarAudio Sound Server", &ss, NULL, NULL, &pulseerror); |
---|
106 | |
---|
107 | if ( self->handle == NULL && autoconfig ) { |
---|
108 | info->bits = 16; |
---|
109 | info->codec = ROAR_CODEC_DEFAULT; |
---|
110 | ss.format = PA_SAMPLE_S16NE; |
---|
111 | |
---|
112 | self->handle = pa_simple_new(device, "roard", dir, subdev, "RoarAudio Sound Server", &ss, NULL, NULL, &pulseerror); |
---|
113 | } |
---|
114 | |
---|
115 | if ( self->handle == NULL ) { |
---|
116 | ROAR_ERR("driver_pulsesimple_open(inst=%p, device='%s', info=%p, fh=%i, sstream=%p): can not open device: %s", inst, device, info, fh, sstream, pa_strerror(pulseerror)); |
---|
117 | roar_mm_free(self); |
---|
118 | ROAR_DBG("driver_pulsesimple_open(inst=%p, device='%s', info=%p, fh=%i, sstream=%p) = -1", inst, device, info, fh, sstream); |
---|
119 | return -1; |
---|
120 | } |
---|
121 | |
---|
122 | memset(inst, 0, sizeof(struct roar_vio_calls)); |
---|
123 | inst->flags = ROAR_VIO_FLAGS_NONE; |
---|
124 | inst->refc = 1; |
---|
125 | |
---|
126 | inst->inst = self; |
---|
127 | inst->close = driver_pulsesimple_close; |
---|
128 | inst->write = driver_pulsesimple_write; |
---|
129 | inst->sync = driver_pulsesimple_sync; |
---|
130 | inst->ctl = driver_pulsesimple_ctl; |
---|
131 | |
---|
132 | return 0; |
---|
133 | } |
---|
134 | |
---|
135 | int driver_pulsesimple_close (struct roar_vio_calls * vio) { |
---|
136 | struct driver_pulsesimple * self = vio->inst; |
---|
137 | |
---|
138 | ROAR_DBG("driver_pulsesimple_close(vio=%p) = ?", vio); |
---|
139 | |
---|
140 | pa_simple_free(self->handle); |
---|
141 | |
---|
142 | roar_mm_free(self); |
---|
143 | |
---|
144 | return 0; |
---|
145 | } |
---|
146 | |
---|
147 | ssize_t driver_pulsesimple_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
148 | struct driver_pulsesimple * self = vio->inst; |
---|
149 | |
---|
150 | ROAR_DBG("driver_pulsesimple_write(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned int)count); |
---|
151 | |
---|
152 | return pa_simple_write(self->handle, buf, count, NULL) == 0 ? count : -1; |
---|
153 | } |
---|
154 | |
---|
155 | int driver_pulsesimple_sync (struct roar_vio_calls * vio) { |
---|
156 | struct driver_pulsesimple * self = vio->inst; |
---|
157 | |
---|
158 | ROAR_DBG("driver_pulsesimple_sync(vio=%p) = ?", vio); |
---|
159 | |
---|
160 | return pa_simple_drain(self->handle, NULL); |
---|
161 | } |
---|
162 | |
---|
163 | int driver_pulsesimple_ctl (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) { |
---|
164 | struct driver_pulsesimple * self = vio->inst; |
---|
165 | |
---|
166 | (void)self; |
---|
167 | |
---|
168 | ROAR_DBG("driver_pulsesimple_ctl(vio=%p) = ?", vio); |
---|
169 | |
---|
170 | switch (cmd) { |
---|
171 | case ROAR_VIO_CTL_NONBLOCK: |
---|
172 | if ( *(int*)data == ROAR_SOCKET_BLOCK ) |
---|
173 | return 0; |
---|
174 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
175 | return -1; |
---|
176 | break; |
---|
177 | } |
---|
178 | |
---|
179 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
180 | return -1; |
---|
181 | } |
---|
182 | |
---|
183 | #endif |
---|
184 | |
---|
185 | //ll |
---|