1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
/* nas.c - Output driver for The Network Audio System (NAS)
* Copyright (C) 2000 Erik Inge Bols <knan@mo.himolde.no>
* Based on the NAS audio module for mpg123.
*
* This file is part of AlsaPlayer.
*
* AlsaPlayer is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* AlsaPlayer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
*/
/* Note: This plugin has a kind of weird structure right now - most of its
* initialization takes place in nas_set_buffer. Sample rate cannot
* be changed while running, a player restart is needed.
*
* The receiving server must support signed 16-bit in the client's
* byte order, stereo.
*
* Alsaplayer converts everything to 16-bit stereo anyway, so this is not
* a specific problem of this plugin :-)
*/
#include "config.h"
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <audio/audiolib.h>
#include <audio/soundlib.h>
#include "output_plugin.h"
#include "alsaplayer_error.h"
typedef struct
{
AuServer *aud;
AuFlowID flow;
AuDeviceAttributes *da;
int numDevices;
char *buf;
AuUint32 buf_size;
AuUint32 buf_cnt;
AuBool data_sent;
AuBool finished;
int freq;
} nas_info;
static nas_info Nas_Info = {NULL,0,NULL,0,NULL,0,0,0,0,44100};
#define NAS_SOUND_LOW_WATER_MARK 25 /* percent */
#define NAS_MAX_FORMAT 10 /* currently, there are 7 supported formats */
static void
nas_sendData(AuServer *aud, nas_info *i, AuUint32 numBytes)
{
if (numBytes < i->buf_cnt) {
AuWriteElement(aud, i->flow, 0, numBytes, i->buf, AuFalse, NULL);
memmove(i->buf, i->buf + numBytes, i->buf_cnt - numBytes);
i->buf_cnt = i->buf_cnt - numBytes;
}
else {
AuWriteElement(aud, i->flow, 0, i->buf_cnt, i->buf,
(numBytes > i->buf_cnt), NULL);
i->buf_cnt = 0;
}
i->data_sent = AuTrue;
}
/* Main routine - remove this and no playback happens. */
static AuBool
nas_eventHandler(AuServer *aud, AuEvent *ev, AuEventHandlerRec *handler)
{
nas_info *i = (nas_info *) handler->data;
switch (ev->type)
{
case AuEventTypeMonitorNotify:
i->finished = AuTrue;
break;
case AuEventTypeElementNotify:
{
AuElementNotifyEvent *event = (AuElementNotifyEvent *) ev;
switch (event->kind)
{
case AuElementNotifyKindLowWater:
nas_sendData(aud, i, event->num_bytes);
break;
case AuElementNotifyKindState:
switch (event->cur_state)
{
case AuStatePause:
if (event->reason != AuReasonUser)
nas_sendData(aud, i, event->num_bytes);
break;
case AuStateStop:
i->finished = AuTrue;
break;
}
}
}
}
return AuTrue;
}
void nas_flush()
{
AuEvent ev;
while ((!Nas_Info.data_sent) && (!Nas_Info.finished)) {
AuNextEvent(Nas_Info.aud, AuTrue, &ev);
AuDispatchEvent(Nas_Info.aud, &ev);
}
Nas_Info.data_sent = AuFalse;
}
static int nas_init()
{
// Always return ok for now
return 1;
}
static int nas_open(const char *device)
{
char *server = NULL;
server = getenv("AUDIOSERVER");
if (server == NULL)
{
fprintf(stderr, "No $AUDIOSERVER, falling back on $DISPLAY\n");
server = getenv("DISPLAY");
if (server == NULL)
{
fprintf(stderr, "No $DISPLAY, falling back on tcp/localhost:8000\n");
server = "tcp/localhost:8000";
}
}
Nas_Info.aud = AuOpenServer(server, 0, NULL, 0, NULL, NULL);
if (Nas_Info.aud == NULL)
{
fprintf(stderr, "NAS server not available\n");
return 0;
}
return 1;
}
static void nas_close()
{
AuCloseServer(Nas_Info.aud);
return;
}
static int nas_write(void *buf,int len)
{
int buf_cnt = 0;
/* alsaplayer_error("nas_write(%x, %d)", buf, len); */
while ((Nas_Info.buf_cnt + (len - buf_cnt)) > Nas_Info.buf_size) {
memcpy(Nas_Info.buf + Nas_Info.buf_cnt,
(int *)buf + buf_cnt,
(Nas_Info.buf_size - Nas_Info.buf_cnt));
buf_cnt += (Nas_Info.buf_size - Nas_Info.buf_cnt);
Nas_Info.buf_cnt += (Nas_Info.buf_size - Nas_Info.buf_cnt);
nas_flush();
}
memcpy(Nas_Info.buf + Nas_Info.buf_cnt,
(int *)buf + buf_cnt,
(len - buf_cnt));
Nas_Info.buf_cnt += (len - buf_cnt);
//nas_flush();
return 1;
}
static int nas_set_buffer(int *fragment_size, int *fragment_count, int *channels)
{
AuDeviceID device = AuNone;
AuElement elements[2];
unsigned char format;
AuUint32 buf_samples;
int i;
/* Sample format hardcoded to 16-bit signed, of native
byte order, stereo */
/* alsaplayer_error("nas_set_buffer(%d, %d, %d)", fragment_size, fragment_count, channels); */
*fragment_count = 3;
if (((char) *(short *)"x")=='x') /* ugly, but painless */
{
format = AuFormatLinearSigned16LSB; /* little endian */
}
else
{
format = AuFormatLinearSigned16MSB; /* big endian */
}
/* look for an physical, stereo, output device */
for (i = 0; i < AuServerNumDevices(Nas_Info.aud); i++)
if
((AuDeviceKind(AuServerDevice(Nas_Info.aud, i)) == AuComponentKindPhysicalOutput)
&&
(AuDeviceNumTracks(AuServerDevice(Nas_Info.aud, i)) == *channels ))
{
device = AuDeviceIdentifier(AuServerDevice(Nas_Info.aud, i));
break;
}
if (device == AuNone) {
fprintf(stderr,
"Couldn't find an output device providing %d channels\n", 2);
exit(1);
}
if (!(Nas_Info.flow = AuCreateFlow(Nas_Info.aud, NULL))) {
fprintf(stderr, "Couldn't create flow\n");
exit(1);
}
buf_samples = *fragment_size * *fragment_count;
/* POSSIBLE FIXME: Rate cannot be changed run-time :-( */
AuMakeElementImportClient(&elements[0], /* element */
(unsigned short) Nas_Info.freq /* ai->rate*/ ,
/* rate */
format, /* format */
*channels /*ai->channels*/ , /* channels */
AuTrue, /* ??? */
buf_samples, /* max samples */
(AuUint32) (buf_samples / 100
* NAS_SOUND_LOW_WATER_MARK),
/* low water mark */
0, /* num actions */
NULL); /* actions */
AuMakeElementExportDevice(&elements[1], /* element */
0, /* input */
device, /* device */
(unsigned short) Nas_Info.freq /*ai->rate*/,
/* rate */
AuUnlimitedSamples, /* num samples */
0, /* num actions */
NULL); /* actions */
AuSetElements(Nas_Info.aud, /* Au server */
Nas_Info.flow, /* flow ID */
AuTrue, /* clocked */
2, /* num elements */
elements, /* elements */
NULL); /* return status */
AuRegisterEventHandler(Nas_Info.aud, /* Au server */
AuEventHandlerIDMask, /* value mask */
0, /* type */
Nas_Info.flow, /* id */
nas_eventHandler, /* callback */
(AuPointer) &Nas_Info); /* data */
Nas_Info.buf_size = buf_samples * *channels * AuSizeofFormat(format);
Nas_Info.buf = (char *) malloc(Nas_Info.buf_size);
if (Nas_Info.buf == NULL) {
fprintf(stderr, "Unable to allocate input/output buffer of size %ld\n",
Nas_Info.buf_size);
exit(1);
}
Nas_Info.buf_cnt = 0;
Nas_Info.data_sent = AuFalse;
Nas_Info.finished = AuFalse;
AuStartFlow(Nas_Info.aud, /* Au server */
Nas_Info.flow, /* id */
NULL); /* status */
return 1;
}
static unsigned int nas_set_sample_rate(unsigned int rate)
{
/* alsaplayer_error("nas_set_sample_rate(%d)", rate); */
/* Must be called before nas_set_buffer, or rate change will be ignored */
Nas_Info.freq = rate;
return rate;
}
output_plugin nas_output;
output_plugin *output_plugin_info(void)
{
memset(&nas_output, 0, sizeof(output_plugin));
nas_output.version = OUTPUT_PLUGIN_VERSION;
nas_output.name = "NAS output v0.2 (30-Mar-2000)";
nas_output.author = "Erik Inge Bols";
nas_output.init = nas_init;
nas_output.open = nas_open;
nas_output.close = nas_close;
nas_output.write = nas_write;
nas_output.set_buffer = nas_set_buffer;
nas_output.set_sample_rate = nas_set_sample_rate;
return &nas_output;
}
|