[398] | 1 | //simple.c: |
---|
| 2 | |
---|
[706] | 3 | /* |
---|
[4708] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011 |
---|
[706] | 5 | * The code (may) include prototypes and comments (and maybe |
---|
| 6 | * other code fragements) from libpulse*. They are mostly copyrighted by: |
---|
| 7 | * Lennart Poettering <poettering@users.sourceforge.net> and |
---|
| 8 | * Pierre Ossman <drzeus@drzeus.cx> |
---|
| 9 | * |
---|
| 10 | * This file is part of libroarpulse a part of RoarAudio, |
---|
| 11 | * a cross-platform sound system for both, home and professional use. |
---|
| 12 | * See README for details. |
---|
| 13 | * |
---|
| 14 | * This file is free software; you can redistribute it and/or modify |
---|
| 15 | * it under the terms of the GNU General Public License version 3 |
---|
| 16 | * as published by the Free Software Foundation. |
---|
| 17 | * |
---|
| 18 | * RoarAudio is distributed in the hope that it will be useful, |
---|
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 21 | * GNU General Public License for more details. |
---|
| 22 | * |
---|
| 23 | * You should have received a copy of the GNU General Public License |
---|
| 24 | * along with this software; see the file COPYING. If not, write to |
---|
[3517] | 25 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 26 | * Boston, MA 02110-1301, USA. |
---|
[706] | 27 | * |
---|
| 28 | * NOTE for everyone want's to change something and send patches: |
---|
| 29 | * read README and HACKING! There a addition information on |
---|
| 30 | * the license of this document you need to read before you send |
---|
| 31 | * any patches. |
---|
| 32 | * |
---|
| 33 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 34 | * or libpulse*: |
---|
| 35 | * The libs libroaresd, libroararts and libroarpulse link this libroar |
---|
| 36 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 37 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 38 | */ |
---|
| 39 | |
---|
[398] | 40 | #include <libroarpulse/libroarpulse.h> |
---|
| 41 | |
---|
[4960] | 42 | #define _seterr() if ( error != NULL ) { *error = roar_pa_raerror2paerror(err); } |
---|
| 43 | |
---|
[402] | 44 | /** Create a new connection to the server */ |
---|
| 45 | pa_simple* pa_simple_new( |
---|
| 46 | const char *server, /**< Server name, or NULL for default */ |
---|
| 47 | const char *name, /**< A descriptive name for this client (application name, ...) */ |
---|
| 48 | pa_stream_direction_t dir, /**< Open this stream for recording or playback? */ |
---|
| 49 | const char *dev, /**< Sink (resp. source) name, or NULL for default */ |
---|
| 50 | const char *stream_name, /**< A descriptive name for this client (application name, song title, ...) */ |
---|
| 51 | const pa_sample_spec *ss, /**< The sample type to use */ |
---|
| 52 | const pa_channel_map *map, /**< The channel map to use, or NULL for default */ |
---|
| 53 | const pa_buffer_attr *attr, /**< Buffering attributes, or NULL for default */ |
---|
| 54 | int *error /**< A pointer where the error code is stored when the routine returns NULL. It is OK to pass NULL here. */ |
---|
| 55 | ) { |
---|
[4960] | 56 | struct roar_keyval kv[1]; |
---|
| 57 | roar_vs_t * vss = NULL; |
---|
[3384] | 58 | struct roar_audio_info info; |
---|
[416] | 59 | int roar_dir; |
---|
[4960] | 60 | int err = ROAR_ERROR_NONE; |
---|
[406] | 61 | |
---|
[5270] | 62 | if ( dev != NULL || map != NULL || attr != NULL ) |
---|
| 63 | return NULL; |
---|
| 64 | |
---|
[406] | 65 | if ( dir == PA_STREAM_PLAYBACK ) { |
---|
[416] | 66 | roar_dir = ROAR_DIR_PLAY; |
---|
[406] | 67 | } else if ( dir == PA_STREAM_RECORD ) { |
---|
[416] | 68 | roar_dir = ROAR_DIR_RECORD; |
---|
[406] | 69 | } else { |
---|
| 70 | return NULL; |
---|
| 71 | } |
---|
| 72 | |
---|
[3384] | 73 | if ( roar_pa_sspec2auinfo(&info, ss) == -1 ) { |
---|
| 74 | return NULL; |
---|
| 75 | } |
---|
[410] | 76 | |
---|
[3389] | 77 | server = roar_pa_find_server((char*)server); |
---|
[418] | 78 | |
---|
[4960] | 79 | vss = roar_vs_new(server, name, &err); |
---|
| 80 | if ( vss == NULL ) { |
---|
| 81 | _seterr(); |
---|
[416] | 82 | return NULL; |
---|
| 83 | } |
---|
| 84 | |
---|
[4960] | 85 | if ( roar_vs_stream(vss, &info, roar_dir, &err) == -1 ) { |
---|
| 86 | roar_vs_close(vss, ROAR_VS_TRUE, NULL); |
---|
| 87 | _seterr(); |
---|
[416] | 88 | return NULL; |
---|
| 89 | } |
---|
| 90 | |
---|
[3848] | 91 | if ( stream_name != NULL && stream_name[0] != 0 ) { |
---|
[4960] | 92 | kv[0].key = "DESCRIPTION"; |
---|
[4961] | 93 | kv[0].value = (char*)stream_name; |
---|
[416] | 94 | |
---|
[4960] | 95 | roar_vs_meta(vss, kv, 1, NULL); |
---|
[418] | 96 | } |
---|
[406] | 97 | |
---|
[4960] | 98 | return (pa_simple*) vss; |
---|
[402] | 99 | } |
---|
| 100 | |
---|
| 101 | /** Close and free the connection to the server. The connection objects becomes invalid when this is called. */ |
---|
| 102 | void pa_simple_free(pa_simple *s) { |
---|
[3848] | 103 | if ( s == NULL ) |
---|
[404] | 104 | return; |
---|
| 105 | |
---|
[4960] | 106 | roar_vs_close((roar_vs_t*)s, ROAR_VS_FALSE, NULL); |
---|
[402] | 107 | } |
---|
| 108 | |
---|
| 109 | /** Write some data to the server */ |
---|
| 110 | int pa_simple_write(pa_simple *s, const void*data, size_t length, int *error) { |
---|
[4960] | 111 | int ret; |
---|
| 112 | int err = ROAR_ERROR_NONE; |
---|
[3848] | 113 | |
---|
| 114 | if ( s == NULL ) |
---|
[404] | 115 | return -1; |
---|
| 116 | |
---|
[4960] | 117 | ret = roar_vs_write((roar_vs_t*)s, data, length, &err); |
---|
| 118 | |
---|
| 119 | _seterr(); |
---|
| 120 | |
---|
| 121 | return ret; |
---|
[402] | 122 | } |
---|
| 123 | |
---|
| 124 | /** Wait until all data already written is played by the daemon */ |
---|
| 125 | int pa_simple_drain(pa_simple *s, int *error) { |
---|
[4960] | 126 | int ret; |
---|
| 127 | int err = ROAR_ERROR_NONE; |
---|
[3848] | 128 | |
---|
| 129 | if ( s == NULL ) |
---|
[404] | 130 | return -1; |
---|
| 131 | |
---|
[4960] | 132 | ret = roar_vs_sync((roar_vs_t*)s, ROAR_VS_WAIT, &err); |
---|
[405] | 133 | |
---|
[4960] | 134 | _seterr(); |
---|
| 135 | |
---|
| 136 | return ret; |
---|
[402] | 137 | } |
---|
| 138 | |
---|
| 139 | /** Read some data from the server */ |
---|
| 140 | int pa_simple_read(pa_simple *s, void*data, size_t length, int *error) { |
---|
[4960] | 141 | int ret; |
---|
| 142 | int err = ROAR_ERROR_NONE; |
---|
[3848] | 143 | |
---|
| 144 | if ( s == NULL ) |
---|
[404] | 145 | return -1; |
---|
| 146 | |
---|
[4960] | 147 | ret = roar_vs_read((roar_vs_t*)s, data, length, &err); |
---|
| 148 | |
---|
| 149 | _seterr(); |
---|
| 150 | |
---|
| 151 | return ret; |
---|
[402] | 152 | } |
---|
| 153 | |
---|
| 154 | /** Return the playback latency. \since 0.5 */ |
---|
| 155 | pa_usec_t pa_simple_get_latency(pa_simple *s, int *error) { |
---|
[4960] | 156 | roar_mus_t ret; |
---|
| 157 | int err = ROAR_ERROR_NONE; |
---|
[3848] | 158 | |
---|
| 159 | if ( s == NULL ) |
---|
[404] | 160 | return -1; |
---|
| 161 | |
---|
[5239] | 162 | ret = roar_vs_latency((roar_vs_t*)s, ROAR_VS_BACKEND_DEFAULT, ROAR_VS_WAIT, &err); |
---|
[4960] | 163 | |
---|
| 164 | if ( ret < 0 ) |
---|
| 165 | ret *= -1; |
---|
| 166 | |
---|
| 167 | if ( ret == 0 && err != ROAR_ERROR_NONE ) { |
---|
| 168 | _seterr(); |
---|
| 169 | return -1; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | return ret; |
---|
[402] | 173 | } |
---|
| 174 | |
---|
| 175 | /** Flush the playback buffer. \since 0.5 */ |
---|
| 176 | int pa_simple_flush(pa_simple *s, int *error) { |
---|
[4960] | 177 | int ret; |
---|
| 178 | int err = ROAR_ERROR_NONE; |
---|
[3848] | 179 | |
---|
| 180 | if ( s == NULL ) |
---|
[404] | 181 | return -1; |
---|
| 182 | |
---|
[4960] | 183 | ret = roar_vs_sync((roar_vs_t*)s, ROAR_VS_NOWAIT, &err); |
---|
| 184 | |
---|
| 185 | _seterr(); |
---|
| 186 | |
---|
| 187 | return ret; |
---|
[402] | 188 | } |
---|
| 189 | |
---|
[398] | 190 | //ll |
---|