| 1 |
/* |
|---|
| 2 |
* Sweep PulseAudio Driver |
|---|
| 3 |
* |
|---|
| 4 |
* Copyright (C) 2008 Martin Szulecki |
|---|
| 5 |
* |
|---|
| 6 |
* This program is free software; you can redistribute it and/or modify |
|---|
| 7 |
* it under the terms of the GNU General Public License as published by |
|---|
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
|---|
| 9 |
* (at your option) any later version. |
|---|
| 10 |
* |
|---|
| 11 |
* This program is distributed in the hope that it will be useful, |
|---|
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 |
* GNU General Public License for more details. |
|---|
| 15 |
* |
|---|
| 16 |
* You should have received a copy of the GNU General Public License |
|---|
| 17 |
* along with this program; if not, write to the Free Software |
|---|
| 18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 19 |
*/ |
|---|
| 20 |
|
|---|
| 21 |
#ifdef HAVE_CONFIG_H |
|---|
| 22 |
# include <config.h> |
|---|
| 23 |
#endif |
|---|
| 24 |
|
|---|
| 25 |
#include <stdio.h> |
|---|
| 26 |
#include <stdlib.h> |
|---|
| 27 |
#include <string.h> |
|---|
| 28 |
#include <sys/time.h> |
|---|
| 29 |
#include <sys/types.h> |
|---|
| 30 |
#include <sys/stat.h> |
|---|
| 31 |
#include <unistd.h> |
|---|
| 32 |
#include <fcntl.h> |
|---|
| 33 |
#include <math.h> |
|---|
| 34 |
#include <sys/ioctl.h> |
|---|
| 35 |
#include <pthread.h> |
|---|
| 36 |
#include <errno.h> |
|---|
| 37 |
|
|---|
| 38 |
#include <sweep/sweep_types.h> |
|---|
| 39 |
#include <sweep/sweep_sample.h> |
|---|
| 40 |
|
|---|
| 41 |
#include "driver.h" |
|---|
| 42 |
#include "pcmio.h" |
|---|
| 43 |
#include "question_dialogs.h" |
|---|
| 44 |
|
|---|
| 45 |
#ifdef DRIVER_PULSEAUDIO |
|---|
| 46 |
#include <pulse/simple.h> |
|---|
| 47 |
#include <pulse/error.h> |
|---|
| 48 |
|
|---|
| 49 |
static sw_handle handle_ro = { |
|---|
| 50 |
0, -1, 0, 0, NULL |
|---|
| 51 |
}; |
|---|
| 52 |
|
|---|
| 53 |
static sw_handle handle_wo = { |
|---|
| 54 |
0, -1, 0, 0, NULL |
|---|
| 55 |
}; |
|---|
| 56 |
|
|---|
| 57 |
static sw_handle handle_rw = { |
|---|
| 58 |
0, -1, 0, 0, NULL |
|---|
| 59 |
}; |
|---|
| 60 |
|
|---|
| 61 |
static GList * |
|---|
| 62 |
pulse_get_names (void) |
|---|
| 63 |
{ |
|---|
| 64 |
GList * names = NULL; |
|---|
| 65 |
|
|---|
| 66 |
names = g_list_append (names, "Default"); |
|---|
| 67 |
|
|---|
| 68 |
return names; |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
static sw_handle * |
|---|
| 72 |
pulse_open (int monitoring, int flags) |
|---|
| 73 |
{ |
|---|
| 74 |
sw_handle * handle = &handle_rw; |
|---|
| 75 |
|
|---|
| 76 |
if (flags == O_RDONLY) { |
|---|
| 77 |
handle = &handle_ro; |
|---|
| 78 |
} else if (flags == O_WRONLY) { |
|---|
| 79 |
handle = &handle_wo; |
|---|
| 80 |
} |
|---|
| 81 |
|
|---|
| 82 |
handle->driver_flags = flags; |
|---|
| 83 |
handle->custom_data = NULL; |
|---|
| 84 |
|
|---|
| 85 |
return handle; |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
static void |
|---|
| 89 |
pulse_setup (sw_handle * handle, sw_format * format) |
|---|
| 90 |
{ |
|---|
| 91 |
struct pa_sample_spec ss; |
|---|
| 92 |
pa_stream_direction_t dir; |
|---|
| 93 |
int error; |
|---|
| 94 |
|
|---|
| 95 |
if (format->channels > PA_CHANNELS_MAX) { |
|---|
| 96 |
fprintf(stderr, __FILE__": pulse_setup(): The maximum number of channels supported is %d, while %d have been requested.\n", PA_CHANNELS_MAX, format->channels); |
|---|
| 97 |
return; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
ss.format = PA_SAMPLE_FLOAT32; |
|---|
| 101 |
ss.rate = format->rate; |
|---|
| 102 |
ss.channels = format->channels; |
|---|
| 103 |
|
|---|
| 104 |
if (handle->driver_flags == O_RDONLY) { |
|---|
| 105 |
dir = PA_STREAM_RECORD; |
|---|
| 106 |
} else if (handle->driver_flags == O_WRONLY) { |
|---|
| 107 |
dir = PA_STREAM_PLAYBACK; |
|---|
| 108 |
} else { |
|---|
| 109 |
return; |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
if (!(handle->custom_data = pa_simple_new(NULL, "Sweep", dir, NULL, "Sweep Stream", &ss, NULL, NULL, &error))) { |
|---|
| 113 |
fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error)); |
|---|
| 114 |
return; |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
handle->driver_rate = ss.rate; |
|---|
| 118 |
handle->driver_channels = ss.channels; |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
static int |
|---|
| 122 |
pulse_wait (sw_handle * handle) |
|---|
| 123 |
{ |
|---|
| 124 |
return 0; |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
static ssize_t |
|---|
| 128 |
pulse_read (sw_handle * handle, sw_audio_t * buf, size_t count) |
|---|
| 129 |
{ |
|---|
| 130 |
struct pa_simple * pa ; |
|---|
| 131 |
int error; |
|---|
| 132 |
size_t byte_count; |
|---|
| 133 |
|
|---|
| 134 |
if ((pa = (struct pa_simple *)handle->custom_data) == NULL) |
|---|
| 135 |
return 0; |
|---|
| 136 |
|
|---|
| 137 |
byte_count = count * sizeof (sw_audio_t); |
|---|
| 138 |
|
|---|
| 139 |
if (pa_simple_read(pa, buf, byte_count, &error) < 0) { |
|---|
| 140 |
fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error)); |
|---|
| 141 |
return 0; |
|---|
| 142 |
} |
|---|
| 143 |
return 1; |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
static ssize_t |
|---|
| 147 |
pulse_write (sw_handle * handle, sw_audio_t * buf, size_t count) |
|---|
| 148 |
{ |
|---|
| 149 |
struct pa_simple * pa ; |
|---|
| 150 |
int error; |
|---|
| 151 |
size_t byte_count; |
|---|
| 152 |
|
|---|
| 153 |
if ((pa = (struct pa_simple *)handle->custom_data) == NULL) |
|---|
| 154 |
return 0; |
|---|
| 155 |
|
|---|
| 156 |
byte_count = count * sizeof (sw_audio_t); |
|---|
| 157 |
|
|---|
| 158 |
if (pa_simple_write(pa, buf, byte_count, &error) < 0) { |
|---|
| 159 |
fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error)); |
|---|
| 160 |
return 0; |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
return 1; |
|---|
| 164 |
} |
|---|
| 165 |
|
|---|
| 166 |
sw_framecount_t |
|---|
| 167 |
pulse_offset (sw_handle * handle) |
|---|
| 168 |
{ |
|---|
| 169 |
return -1; |
|---|
| 170 |
} |
|---|
| 171 |
|
|---|
| 172 |
static void |
|---|
| 173 |
pulse_reset (sw_handle * handle) |
|---|
| 174 |
{ |
|---|
| 175 |
} |
|---|
| 176 |
|
|---|
| 177 |
static void |
|---|
| 178 |
pulse_flush (sw_handle * handle) |
|---|
| 179 |
{ |
|---|
| 180 |
struct pa_simple * pa ; |
|---|
| 181 |
int error; |
|---|
| 182 |
|
|---|
| 183 |
if ((pa = (struct pa_simple *)handle->custom_data) == NULL) |
|---|
| 184 |
return; |
|---|
| 185 |
|
|---|
| 186 |
if (pa_simple_flush(pa, &error) < 0) { |
|---|
| 187 |
fprintf(stderr, __FILE__": pa_simple_flush() failed: %s\n", pa_strerror(error)); |
|---|
| 188 |
} |
|---|
| 189 |
} |
|---|
| 190 |
|
|---|
| 191 |
static void |
|---|
| 192 |
pulse_drain (sw_handle * handle) |
|---|
| 193 |
{ |
|---|
| 194 |
struct pa_simple * pa ; |
|---|
| 195 |
int error; |
|---|
| 196 |
|
|---|
| 197 |
if ((pa = (struct pa_simple *)handle->custom_data) == NULL) |
|---|
| 198 |
return; |
|---|
| 199 |
|
|---|
| 200 |
if (pa_simple_drain(pa, &error) < 0) { |
|---|
| 201 |
fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error)); |
|---|
| 202 |
} |
|---|
| 203 |
} |
|---|
| 204 |
|
|---|
| 205 |
static void |
|---|
| 206 |
pulse_close (sw_handle * handle) |
|---|
| 207 |
{ |
|---|
| 208 |
struct pa_simple * pa ; |
|---|
| 209 |
|
|---|
| 210 |
pulse_drain(handle); |
|---|
| 211 |
|
|---|
| 212 |
if ((pa = (struct pa_simple *)handle->custom_data) == NULL) |
|---|
| 213 |
return; |
|---|
| 214 |
|
|---|
| 215 |
pa_simple_free(pa); |
|---|
| 216 |
handle->custom_data = NULL; |
|---|
| 217 |
} |
|---|
| 218 |
|
|---|
| 219 |
static sw_driver _driver_pulseaudio = { |
|---|
| 220 |
"PulseAudio", |
|---|
| 221 |
pulse_get_names, |
|---|
| 222 |
pulse_open, |
|---|
| 223 |
pulse_setup, |
|---|
| 224 |
pulse_wait, |
|---|
| 225 |
pulse_read, |
|---|
| 226 |
pulse_write, |
|---|
| 227 |
pulse_offset, |
|---|
| 228 |
pulse_reset, |
|---|
| 229 |
pulse_flush, |
|---|
| 230 |
pulse_drain, |
|---|
| 231 |
pulse_close, |
|---|
| 232 |
"pulseaudio_primary_sink", |
|---|
| 233 |
"pulseaudio_monitor_sink", |
|---|
| 234 |
"pulseaudio_log_frags" |
|---|
| 235 |
}; |
|---|
| 236 |
|
|---|
| 237 |
#else |
|---|
| 238 |
|
|---|
| 239 |
static sw_driver _driver_pulseaudio = { |
|---|
| 240 |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
|---|
| 241 |
}; |
|---|
| 242 |
|
|---|
| 243 |
#endif |
|---|
| 244 |
|
|---|
| 245 |
sw_driver * driver_pulseaudio = &_driver_pulseaudio; |
|---|