source: roaraudio/roard/driver_artsc.c

tip
Last change on this file was 6052:d48765b2475e, checked in by phi, 7 years ago

updated copyright headers

File size: 4.9 KB
Line 
1//driver_artsc.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2011-2015
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_LIBARTSC
29
30#define INIT     +1
31#define SHUTDOWN -1
32
33static int driver_artsc_init_shutdown (int action) {
34 static int state = 0;
35
36 if ( action != INIT && action != SHUTDOWN )
37  return -1;
38
39 if ( state == 0 && action == INIT ) {
40  if ( arts_init() != 0 )
41   return -1;
42 } else if ( state == 1 && action == SHUTDOWN ) {
43  arts_free();
44 }
45
46 state += action;
47
48 return 0;
49}
50
51int     driver_artsc_open_vio(struct roar_vio_calls * inst, char * device, struct roar_audio_info * info, int fh, struct roar_stream_server * sstream) {
52 struct roar_artsc * self = NULL;
53 int autoconfig           = 0;
54
55 (void)sstream;
56
57 ROAR_WARN("The aRtsc driver is obsolete/broken, use another!");
58 ROAR_DBG("driver_artsc_open_vio(*) = ?");
59
60 if ( fh != -1 || device != NULL )
61  return -1;
62
63 if ( sstream != NULL ) {
64  autoconfig = streams_get_flag(ROAR_STREAM(sstream)->id, ROAR_FLAG_AUTOCONF);
65 }
66
67 ROAR_DBG("driver_artsc_open_vio(*) = ?");
68
69 self = roar_mm_malloc(sizeof(struct roar_artsc));
70
71 if ( self == NULL )
72  return -1;
73
74 memset(self, 0, sizeof(struct roar_artsc));
75
76 ROAR_DBG("driver_artsc_open_vio(*) = ?");
77
78 if ( driver_artsc_init_shutdown(INIT) == -1 ) {
79  roar_mm_free(self);
80  return -1;
81 }
82
83 ROAR_DBG("driver_artsc_open_vio(*) = ?");
84
85 if ( info->codec != ROAR_CODEC_DEFAULT ) {
86  if ( autoconfig ) {
87   info->codec = ROAR_CODEC_DEFAULT;
88  } else {
89   return -1;
90  }
91 }
92
93 if ( info->bits != 16 ) {
94  if ( autoconfig ) {
95   info->bits = 16;
96  }
97 }
98
99 self->info   = info;
100 self->stream = arts_play_stream(info->rate, info->bits, info->channels, "roard");
101
102 ROAR_DBG("driver_artsc_open_vio(*) = ?");
103
104 if ( self->stream == NULL && autoconfig ) {
105  info->bits   = 16;
106  self->stream = arts_play_stream(info->rate, info->bits, info->channels, "roard");
107 }
108
109 ROAR_DBG("driver_artsc_open_vio(*) = ?");
110
111 if ( self->stream == NULL ) {
112  roar_mm_free(self);
113  driver_artsc_init_shutdown(SHUTDOWN);
114  ROAR_DBG("driver_artsc_open_vio(*) = -1");
115  return -1;
116 }
117
118 ROAR_DBG("driver_artsc_open_vio(*) = ?");
119
120 memset(inst, 0, sizeof(struct roar_vio_calls));
121 inst->flags    = ROAR_VIO_FLAGS_NONE;
122 inst->refc     = 1;
123
124 inst->inst     = (void*) self;
125 inst->write    = driver_artsc_write;
126 inst->close    = driver_artsc_close;
127 inst->ctl      = driver_artsc_ctl;
128
129 ROAR_DBG("driver_artsc_open_vio(*) = 0");
130 return 0;
131}
132
133ssize_t driver_artsc_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
134 struct roar_artsc * self = vio->inst;
135 int ret;
136
137 ROAR_DBG("driver_artsc_write(*) = ?");
138
139 ret = arts_write(self->stream, buf, count);
140
141 if ( ret >= 0 ) {
142  ROAR_DBG("driver_artsc_write(*) = %i", ret);
143  return ret;
144 }
145
146 ROAR_DBG("driver_artsc_write(*) = -1");
147 return -1;
148}
149
150int     driver_artsc_close   (struct roar_vio_calls * vio) {
151 struct roar_artsc * self = vio->inst;
152
153 arts_close_stream(self->stream);
154
155 roar_mm_free(self);
156
157 return driver_artsc_init_shutdown(SHUTDOWN);
158}
159
160int     driver_artsc_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) {
161 struct roar_artsc * self = vio->inst;
162 ssize_t bps;
163 int ret;
164 uint_least32_t tmp;
165
166 switch (cmd) {
167  case ROAR_VIO_CTL_GET_DBLKSIZE:
168    ret = arts_stream_get(self->stream, ARTS_P_PACKET_SIZE);
169    if ( ret > 0 ) {
170     *(uint_least32_t *)data = ret;
171     return 0;
172    }
173   break;
174  case ROAR_VIO_CTL_GET_DELAY:
175    ret = arts_stream_get(self->stream, ARTS_P_TOTAL_LATENCY);
176    if ( ret < 0 )
177     return -1;
178    bps = roar_info2bitspersec(self->info);
179    if ( bps < 0 )
180     return -1;
181
182    tmp  = bps / (ssize_t)8;
183    tmp *= ret;
184    tmp /= (uint_least32_t)1000;
185
186    *(uint_least32_t *)data = tmp;
187
188    return 0;
189  case ROAR_VIO_CTL_NONBLOCK:
190    switch (*(int*)data) {
191     case ROAR_SOCKET_NONBLOCK:
192       ret = arts_stream_set(self->stream, ARTS_P_BLOCKING, 0);
193       if ( ret == 0 )
194        return 0;
195       return -1;
196      break;
197     case ROAR_SOCKET_BLOCK:
198       ret = arts_stream_set(self->stream, ARTS_P_BLOCKING, 1);
199       if ( ret == 1 )
200        return 0;
201       return -1;
202      break;
203    }
204   break;
205  default:
206    return -1;
207 }
208
209 return -1;
210}
211
212#endif
213
214//ll
Note: See TracBrowser for help on using the repository browser.