[398] | 1 | //simple.c: |
---|
| 2 | |
---|
[706] | 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
| 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 |
---|
| 25 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 26 | * |
---|
| 27 | * NOTE for everyone want's to change something and send patches: |
---|
| 28 | * read README and HACKING! There a addition information on |
---|
| 29 | * the license of this document you need to read before you send |
---|
| 30 | * any patches. |
---|
| 31 | * |
---|
| 32 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 33 | * or libpulse*: |
---|
| 34 | * The libs libroaresd, libroararts and libroarpulse link this libroar |
---|
| 35 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 36 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 37 | */ |
---|
| 38 | |
---|
[398] | 39 | #include <libroarpulse/libroarpulse.h> |
---|
| 40 | |
---|
[402] | 41 | /** Create a new connection to the server */ |
---|
| 42 | pa_simple* pa_simple_new( |
---|
| 43 | const char *server, /**< Server name, or NULL for default */ |
---|
| 44 | const char *name, /**< A descriptive name for this client (application name, ...) */ |
---|
| 45 | pa_stream_direction_t dir, /**< Open this stream for recording or playback? */ |
---|
| 46 | const char *dev, /**< Sink (resp. source) name, or NULL for default */ |
---|
| 47 | const char *stream_name, /**< A descriptive name for this client (application name, song title, ...) */ |
---|
| 48 | const pa_sample_spec *ss, /**< The sample type to use */ |
---|
| 49 | const pa_channel_map *map, /**< The channel map to use, or NULL for default */ |
---|
| 50 | const pa_buffer_attr *attr, /**< Buffering attributes, or NULL for default */ |
---|
| 51 | int *error /**< A pointer where the error code is stored when the routine returns NULL. It is OK to pass NULL here. */ |
---|
| 52 | ) { |
---|
[406] | 53 | struct roarpulse_simple * s = malloc(sizeof(struct roarpulse_simple)); |
---|
[416] | 54 | int roar_dir; |
---|
[406] | 55 | int codec = -1; |
---|
[416] | 56 | struct roar_meta meta; |
---|
[406] | 57 | |
---|
| 58 | if ( !s ) |
---|
| 59 | return NULL; |
---|
| 60 | |
---|
| 61 | if ( dir == PA_STREAM_PLAYBACK ) { |
---|
[416] | 62 | roar_dir = ROAR_DIR_PLAY; |
---|
[406] | 63 | } else if ( dir == PA_STREAM_RECORD ) { |
---|
[416] | 64 | roar_dir = ROAR_DIR_RECORD; |
---|
[406] | 65 | } else { |
---|
| 66 | free(s); |
---|
| 67 | return NULL; |
---|
| 68 | } |
---|
| 69 | |
---|
[410] | 70 | codec = roar_codec_pulse2roar(ss->format); |
---|
| 71 | |
---|
[418] | 72 | if ( !server ) |
---|
| 73 | server = getenv("PULSE_SERVER"); |
---|
| 74 | |
---|
[416] | 75 | if ( roar_simple_connect(&(s->con), (char*)server, (char*)name) == -1 ) { |
---|
| 76 | free(s); |
---|
| 77 | return NULL; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | s->data_fh = roar_simple_new_stream_obj(&(s->con), &(s->stream), ss->rate, ss->channels, |
---|
| 81 | 16 /* does PulseAudio support something diffrent? */, codec, roar_dir); |
---|
| 82 | |
---|
| 83 | if ( s->data_fh == -1 ) { |
---|
| 84 | roar_disconnect(&(s->con)); |
---|
| 85 | free(s); |
---|
| 86 | return NULL; |
---|
| 87 | } |
---|
| 88 | |
---|
[418] | 89 | if ( stream_name && stream_name[0] != 0 ) { |
---|
| 90 | meta.value = (char*)stream_name; |
---|
| 91 | meta.key[0] = 0; |
---|
| 92 | meta.type = ROAR_META_TYPE_DESCRIPTION; |
---|
[416] | 93 | |
---|
[418] | 94 | roar_stream_meta_set(&(s->con), &(s->stream), ROAR_META_MODE_SET, &meta); |
---|
| 95 | } |
---|
[406] | 96 | |
---|
| 97 | return (pa_simple*) s; |
---|
[402] | 98 | } |
---|
| 99 | |
---|
| 100 | /** Close and free the connection to the server. The connection objects becomes invalid when this is called. */ |
---|
| 101 | void pa_simple_free(pa_simple *s) { |
---|
[404] | 102 | struct roarpulse_simple * ss = (struct roarpulse_simple*) s; |
---|
| 103 | if ( !s ) |
---|
| 104 | return; |
---|
| 105 | |
---|
[405] | 106 | close(ss->data_fh); |
---|
[416] | 107 | roar_disconnect(&(ss->con)); |
---|
[405] | 108 | |
---|
[404] | 109 | free(s); |
---|
[402] | 110 | } |
---|
| 111 | |
---|
| 112 | /** Write some data to the server */ |
---|
| 113 | int pa_simple_write(pa_simple *s, const void*data, size_t length, int *error) { |
---|
[404] | 114 | struct roarpulse_simple * ss = (struct roarpulse_simple*) s; |
---|
| 115 | if ( !s ) |
---|
| 116 | return -1; |
---|
| 117 | |
---|
[411] | 118 | return write(ss->data_fh, (char*) data, length); |
---|
[402] | 119 | } |
---|
| 120 | |
---|
| 121 | /** Wait until all data already written is played by the daemon */ |
---|
| 122 | int pa_simple_drain(pa_simple *s, int *error) { |
---|
[405] | 123 | // struct roarpulse_simple * ss = (struct roarpulse_simple*) s; |
---|
[404] | 124 | if ( !s ) |
---|
| 125 | return -1; |
---|
| 126 | |
---|
[405] | 127 | pa_simple_flush(s, NULL); |
---|
| 128 | |
---|
[402] | 129 | return -1; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | /** Read some data from the server */ |
---|
| 133 | int pa_simple_read(pa_simple *s, void*data, size_t length, int *error) { |
---|
[404] | 134 | struct roarpulse_simple * ss = (struct roarpulse_simple*) s; |
---|
| 135 | if ( !s ) |
---|
| 136 | return -1; |
---|
| 137 | |
---|
[405] | 138 | return read(ss->data_fh, data, length); |
---|
[402] | 139 | } |
---|
| 140 | |
---|
| 141 | /** Return the playback latency. \since 0.5 */ |
---|
| 142 | pa_usec_t pa_simple_get_latency(pa_simple *s, int *error) { |
---|
[404] | 143 | struct roarpulse_simple * ss = (struct roarpulse_simple*) s; |
---|
| 144 | if ( !s ) |
---|
| 145 | return -1; |
---|
| 146 | |
---|
[402] | 147 | return -1; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | /** Flush the playback buffer. \since 0.5 */ |
---|
| 151 | int pa_simple_flush(pa_simple *s, int *error) { |
---|
[404] | 152 | struct roarpulse_simple * ss = (struct roarpulse_simple*) s; |
---|
| 153 | if ( !s ) |
---|
| 154 | return -1; |
---|
| 155 | |
---|
[1171] | 156 | return ROAR_FDATASYNC(ss->data_fh); |
---|
[402] | 157 | } |
---|
| 158 | |
---|
[398] | 159 | //ll |
---|