source: roaraudio/libroar/vs.c @ 4174:4d5ffa10c7fe

Last change on this file since 4174:4d5ffa10c7fe was 4174:4d5ffa10c7fe, checked in by phi, 14 years ago

added basic code for the vs API

File size: 5.2 KB
Line 
1//vs.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010
5 *
6 *  This file is part of libroar 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 *  libroar 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 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38#define FLAG_NONE     0x0000
39#define FLAG_STREAM   0x0001
40
41#define _seterr(x) do { if ( error != NULL ) *error = (x); } while(1)
42
43struct roar_vs {
44 int flags;
45 struct roar_connection con_store;
46 struct roar_connection * con;
47 struct roar_stream       stream;
48 struct roar_vio_calls    vio;
49};
50
51const char * roar_vs_strerr(int error) {
52 const struct {
53  int err;
54  const char * msg;
55 } msgs[] = {
56  {ROAR_ERROR_NONE,    "none"},
57  {ROAR_ERROR_PERM,    "perm"},
58  {ROAR_ERROR_NOENT,   "noent"},
59  {ROAR_ERROR_BADMSG,  "badmsg"},
60  {ROAR_ERROR_BUSY,    "busy"},
61  {ROAR_ERROR_CONNREFUSED, "connrefused"},
62  {ROAR_ERROR_NOSYS,   "nosys"},
63  {ROAR_ERROR_NOTSUP,  "notsup"},
64  {ROAR_ERROR_PIPE,    "pipe"},
65  {ROAR_ERROR_PROTO,   "proto"},
66  {ROAR_ERROR_RANGE,   "range"},
67  {ROAR_ERROR_MSGSIZE, "msgsize"},
68  {ROAR_ERROR_NOMEM,   "nomem"},
69  {ROAR_ERROR_INVAL,   "inval"},
70  {-1, NULL}
71 };
72 int i;
73
74 for (i = 0; msgs[i].msg != NULL; i++)
75  if ( msgs[i].err == error )
76   return msgs[i].msg;
77
78 return "(unknown)";
79}
80
81static roar_vs_t * roar_vs_init(int * error) {
82 roar_vs_t * vss = roar_mm_malloc(sizeof(roar_vs_t));
83
84 if ( vss == NULL ) {
85  _seterr(ROAR_ERROR_NOMEM);
86  return NULL;
87 }
88
89 memset(vss, 0, sizeof(roar_vs_t));
90
91 return vss;
92}
93
94roar_vs_t * roar_vs_new_from_con(struct roar_connection * con, int * error) {
95 roar_vs_t * vss = roar_vs_init(error);
96
97 if ( vss == NULL )
98  return NULL;
99
100 vss->con = con;
101
102 return vss;
103}
104
105roar_vs_t * roar_vs_new(const char * server, const char * name, int * error) {
106 roar_vs_t * vss = roar_vs_init(error);
107 int ret;
108
109 if ( vss == NULL )
110  return NULL;
111
112 vss->con = &(vss->con_store);
113
114 ret = roar_simple_connect(vss->con, (char*)server, (char*)name);
115
116 if ( ret == -1 ) {
117  roar_vs_close(vss, ROAR_VS_TRUE, NULL);
118  _seterr(ROAR_ERROR_UNKNOWN);
119  return NULL;
120 }
121
122 return vss;
123}
124
125int roar_vs_stream(roar_vs_t * vss, const struct roar_audio_info * info, int dir, int * error) {
126 int ret;
127
128 if ( vss->flags & FLAG_STREAM ) {
129  _seterr(ROAR_ERROR_INVAL);
130  return -1;
131 }
132
133 ret = roar_vio_simple_new_stream_obj(&(vss->vio), vss->con, &(vss->stream),
134                                      info->rate, info->channels, info->bits, info->codec,
135                                      dir
136                                     );
137
138 if ( ret == -1 ) {
139  _seterr(ROAR_ERROR_UNKNOWN);
140  return -1;
141 }
142
143 vss->flags |= FLAG_STREAM;
144
145 return 0;
146}
147
148roar_vs_t * roar_vs_new_simple(const char * server, const char * name, int rate, int channels, int codec, int bits, int dir, int * error) {
149 roar_vs_t * vss = roar_vs_new(server, name, error);
150 struct roar_audio_info info;
151 int ret;
152
153 if (vss == NULL)
154  return NULL;
155
156 memset(&info, 0, sizeof(info));
157
158 info.rate     = rate;
159 info.channels = channels;
160 info.codec    = codec;
161 info.bits     = bits;
162
163 ret = roar_vs_stream(vss, &info, dir, error);
164
165 if (ret == -1) {
166  roar_vs_close(vss, ROAR_VS_TRUE, NULL);
167  return NULL;
168 }
169
170 return vss;
171}
172
173int roar_vs_close(roar_vs_t * vss, int killit, int * error) {
174 if ( vss->flags & FLAG_STREAM ) {
175  if ( killit ) {
176   roar_kick(vss->con, ROAR_OT_STREAM, roar_stream_get_id(&(vss->stream)));
177  }
178
179  roar_vio_close(&(vss->vio));
180 }
181
182 if ( vss->con == &(vss->con_store) ) {
183  roar_disconnect(vss->con);
184 }
185
186 roar_mm_free(vss);
187 return 0;
188}
189
190ssize_t roar_vs_write(roar_vs_t * vss, const void * buf, size_t len, int * error) {
191 ssize_t ret;
192
193 if ( !(vss->flags & FLAG_STREAM) ) {
194  _seterr(ROAR_ERROR_INVAL);
195  return -1;
196 }
197
198 ret = roar_vio_write(&(vss->vio), (void*)buf, len);
199
200 if ( ret == -1 ) {
201  _seterr(ROAR_ERROR_UNKNOWN);
202 }
203
204 return ret;
205}
206
207ssize_t roar_vs_read (roar_vs_t * vss,       void * buf, size_t len, int * error) {
208 ssize_t ret;
209
210 if ( !(vss->flags & FLAG_STREAM) ) {
211  _seterr(ROAR_ERROR_INVAL);
212  return -1;
213 }
214
215 ret = roar_vio_read(&(vss->vio), buf, len);
216
217 if ( ret == -1 ) {
218  _seterr(ROAR_ERROR_UNKNOWN);
219 }
220
221 return ret;
222}
223
224
225//ll
Note: See TracBrowser for help on using the repository browser.