2001-12-05 12:12:00 +00:00
|
|
|
/*
|
2009-01-26 15:06:44 +00:00
|
|
|
* NAS audio output driver
|
2001-12-05 12:12:00 +00:00
|
|
|
*
|
2009-01-26 15:06:44 +00:00
|
|
|
* copyright (c) 2001 Tobias Diedrich <ranma@gmx.at>
|
2001-12-05 12:12:00 +00:00
|
|
|
*
|
2009-01-26 15:06:44 +00:00
|
|
|
* Based on the libaudiooss parts rewritten by me, which were
|
|
|
|
* originally based on the NAS output plugin for XMMS.
|
|
|
|
*
|
|
|
|
* XMMS plugin by Willem Monsuwe
|
2001-12-05 12:12:00 +00:00
|
|
|
* adapted for libaudiooss by Jon Trulson
|
2007-07-09 19:50:36 +00:00
|
|
|
* further modified by Erik Inge Bolsø
|
2009-01-26 15:06:44 +00:00
|
|
|
* largely rewritten and used for this ao driver by Tobias Diedrich
|
|
|
|
*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer 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 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2001-12-05 12:12:00 +00:00
|
|
|
*
|
2009-01-26 15:06:44 +00:00
|
|
|
* MPlayer 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 MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2002-10-06 17:30:38 +00:00
|
|
|
* Theory of operation:
|
|
|
|
*
|
|
|
|
* The NAS consists of two parts, a server daemon and a client.
|
2002-10-13 22:00:15 +00:00
|
|
|
* We setup the server to use a buffer of size bytes_per_second
|
|
|
|
* with a low watermark of buffer_size - NAS_FRAG_SIZE.
|
2002-10-06 17:30:38 +00:00
|
|
|
* Upon starting the flow the server will generate a buffer underrun
|
|
|
|
* event and the event handler will fill the buffer for the first time.
|
|
|
|
* Now the server will generate a lowwater event when the server buffer
|
|
|
|
* falls below the low watermark value. The event handler gets called
|
|
|
|
* again and refills the buffer by the number of bytes requested by the
|
|
|
|
* server (usually a multiple of 4096). To prevent stuttering on
|
|
|
|
* startup (start of playing, seeks, unpausing) the client buffer should
|
|
|
|
* be bigger than the server buffer. (For debugging we also do some
|
|
|
|
* accounting of what we think how much of the server buffer is filled)
|
2001-12-05 12:12:00 +00:00
|
|
|
*/
|
|
|
|
|
2002-10-13 22:00:15 +00:00
|
|
|
#include <unistd.h>
|
2001-12-03 01:13:48 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2002-10-13 22:00:15 +00:00
|
|
|
#include <string.h>
|
2001-12-03 01:13:48 +00:00
|
|
|
#include <pthread.h>
|
2005-06-17 02:22:34 +00:00
|
|
|
#include <limits.h>
|
2001-12-03 01:13:48 +00:00
|
|
|
#include <audio/audiolib.h>
|
|
|
|
|
2005-01-12 22:07:44 +00:00
|
|
|
#include "config.h"
|
2004-12-07 02:24:15 +00:00
|
|
|
#include "mp_msg.h"
|
2002-10-06 17:31:36 +00:00
|
|
|
|
2001-12-03 01:13:48 +00:00
|
|
|
#include "audio_out.h"
|
|
|
|
#include "audio_out_internal.h"
|
2004-12-27 17:30:15 +00:00
|
|
|
#include "libaf/af_format.h"
|
2001-12-03 01:13:48 +00:00
|
|
|
|
2008-08-24 13:36:04 +00:00
|
|
|
/* NAS_FRAG_SIZE must be a power-of-two value */
|
2002-02-20 22:45:48 +00:00
|
|
|
#define NAS_FRAG_SIZE 4096
|
2001-12-03 01:13:48 +00:00
|
|
|
|
2009-01-01 11:51:52 +00:00
|
|
|
static const char * const nas_event_types[] = {
|
2002-02-20 22:45:48 +00:00
|
|
|
"Undefined",
|
|
|
|
"Undefined",
|
|
|
|
"ElementNotify",
|
|
|
|
"GrabNotify",
|
|
|
|
"MonitorNotify",
|
|
|
|
"BucketNotify",
|
|
|
|
"DeviceNotify"
|
|
|
|
};
|
|
|
|
|
2009-01-01 11:51:52 +00:00
|
|
|
static const char * const nas_elementnotify_kinds[] = {
|
2002-02-20 22:45:48 +00:00
|
|
|
"LowWater",
|
|
|
|
"HighWater",
|
|
|
|
"State",
|
|
|
|
"Unknown"
|
|
|
|
};
|
|
|
|
|
2009-01-01 11:51:52 +00:00
|
|
|
static const char * const nas_states[] = {
|
2002-02-20 22:45:48 +00:00
|
|
|
"Stop",
|
|
|
|
"Start",
|
|
|
|
"Pause",
|
|
|
|
"Any"
|
|
|
|
};
|
|
|
|
|
2009-01-01 11:51:52 +00:00
|
|
|
static const char * const nas_reasons[] = {
|
2002-02-20 22:45:48 +00:00
|
|
|
"User",
|
|
|
|
"Underrun",
|
|
|
|
"Overrun",
|
|
|
|
"EOF",
|
|
|
|
"Watermark",
|
|
|
|
"Hardware",
|
|
|
|
"Any"
|
|
|
|
};
|
|
|
|
|
2009-01-01 11:51:52 +00:00
|
|
|
static const char* nas_reason(unsigned int reason)
|
2002-02-20 22:45:48 +00:00
|
|
|
{
|
|
|
|
if (reason > 6) reason = 6;
|
|
|
|
return nas_reasons[reason];
|
|
|
|
}
|
|
|
|
|
2009-01-01 11:51:52 +00:00
|
|
|
static const char* nas_elementnotify_kind(unsigned int kind)
|
2002-02-20 22:45:48 +00:00
|
|
|
{
|
|
|
|
if (kind > 2) kind = 3;
|
|
|
|
return nas_elementnotify_kinds[kind];
|
|
|
|
}
|
|
|
|
|
2009-01-01 11:51:52 +00:00
|
|
|
static const char* nas_event_type(unsigned int type) {
|
2002-02-20 22:45:48 +00:00
|
|
|
if (type > 6) type = 0;
|
|
|
|
return nas_event_types[type];
|
|
|
|
}
|
|
|
|
|
2009-01-01 11:51:52 +00:00
|
|
|
static const char* nas_state(unsigned int state) {
|
2002-02-20 22:45:48 +00:00
|
|
|
if (state>3) state = 3;
|
|
|
|
return nas_states[state];
|
|
|
|
}
|
|
|
|
|
2009-03-06 19:43:12 +00:00
|
|
|
static const ao_info_t info =
|
2001-12-03 01:13:48 +00:00
|
|
|
{
|
|
|
|
"NAS audio output",
|
|
|
|
"nas",
|
2005-06-17 02:22:34 +00:00
|
|
|
"Tobias Diedrich <ranma+mplayer@tdiedrich.de>",
|
2001-12-03 01:13:48 +00:00
|
|
|
""
|
|
|
|
};
|
|
|
|
|
2002-02-20 22:45:48 +00:00
|
|
|
struct ao_nas_data {
|
|
|
|
AuServer *aud;
|
|
|
|
AuFlowID flow;
|
|
|
|
AuDeviceID dev;
|
2004-03-13 21:54:35 +00:00
|
|
|
AuFixedPoint gain;
|
2002-02-20 22:45:48 +00:00
|
|
|
|
2002-10-13 22:00:15 +00:00
|
|
|
unsigned int state;
|
2002-10-06 17:30:38 +00:00
|
|
|
int expect_underrun;
|
2001-12-03 01:13:48 +00:00
|
|
|
|
2008-08-23 08:14:37 +00:00
|
|
|
char *client_buffer;
|
|
|
|
char *server_buffer;
|
2005-06-17 02:22:34 +00:00
|
|
|
unsigned int client_buffer_size;
|
|
|
|
unsigned int client_buffer_used;
|
|
|
|
unsigned int server_buffer_size;
|
|
|
|
unsigned int server_buffer_used;
|
2002-02-20 22:45:48 +00:00
|
|
|
pthread_mutex_t buffer_mutex;
|
|
|
|
|
|
|
|
pthread_t event_thread;
|
|
|
|
int stop_thread;
|
|
|
|
};
|
2001-12-03 01:13:48 +00:00
|
|
|
|
2002-02-20 22:45:48 +00:00
|
|
|
static struct ao_nas_data *nas_data;
|
2001-12-03 01:13:48 +00:00
|
|
|
|
|
|
|
LIBAO_EXTERN(nas)
|
|
|
|
|
2006-07-09 03:42:35 +00:00
|
|
|
static void nas_print_error(AuServer *aud, const char *prefix, AuStatus as)
|
2001-12-03 01:13:48 +00:00
|
|
|
{
|
2002-02-20 22:45:48 +00:00
|
|
|
char s[100];
|
|
|
|
AuGetErrorText(aud, as, s, 100);
|
2002-10-06 17:31:36 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_ERR, "ao_nas: %s: returned status %d (%s)\n", prefix, as, s);
|
2001-12-03 01:13:48 +00:00
|
|
|
}
|
|
|
|
|
2008-08-23 08:14:37 +00:00
|
|
|
static int nas_readBuffer(struct ao_nas_data *nas_data, unsigned int num)
|
2001-12-03 01:13:48 +00:00
|
|
|
{
|
2002-02-20 22:45:48 +00:00
|
|
|
AuStatus as;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&nas_data->buffer_mutex);
|
2002-10-06 17:31:36 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_DBG2, "ao_nas: nas_readBuffer(): num=%d client=%d/%d server=%d/%d\n",
|
2001-12-03 01:13:48 +00:00
|
|
|
num,
|
2002-02-20 22:45:48 +00:00
|
|
|
nas_data->client_buffer_used, nas_data->client_buffer_size,
|
|
|
|
nas_data->server_buffer_used, nas_data->server_buffer_size);
|
2001-12-03 01:13:48 +00:00
|
|
|
|
2002-02-20 22:45:48 +00:00
|
|
|
if (nas_data->client_buffer_used == 0) {
|
2002-10-06 17:31:36 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_DBG2, "ao_nas: buffer is empty, nothing read.\n");
|
2002-02-20 22:45:48 +00:00
|
|
|
pthread_mutex_unlock(&nas_data->buffer_mutex);
|
2001-12-03 01:13:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-06-17 02:22:34 +00:00
|
|
|
if (num > nas_data->client_buffer_used)
|
2002-02-20 22:45:48 +00:00
|
|
|
num = nas_data->client_buffer_used;
|
|
|
|
|
2002-09-20 18:26:54 +00:00
|
|
|
/*
|
|
|
|
* It is not appropriate to call AuWriteElement() here because the
|
|
|
|
* buffer is locked and delays writing to the network will cause
|
|
|
|
* other threads to block waiting for buffer_mutex. Instead the
|
2005-06-17 02:22:34 +00:00
|
|
|
* data is copied to "server_buffer" and written to the network
|
2002-09-20 18:26:54 +00:00
|
|
|
* outside of the locked section of code.
|
|
|
|
*
|
|
|
|
* (Note: Rather than these two buffers, a single circular buffer
|
|
|
|
* could eliminate the memcpy/memmove steps.)
|
|
|
|
*/
|
2005-06-17 02:22:34 +00:00
|
|
|
/* make sure we don't overflow the buffer */
|
|
|
|
if (num > nas_data->server_buffer_size)
|
|
|
|
num = nas_data->server_buffer_size;
|
2002-09-20 18:26:54 +00:00
|
|
|
memcpy(nas_data->server_buffer, nas_data->client_buffer, num);
|
|
|
|
|
|
|
|
nas_data->client_buffer_used -= num;
|
|
|
|
nas_data->server_buffer_used += num;
|
|
|
|
memmove(nas_data->client_buffer, nas_data->client_buffer + num, nas_data->client_buffer_used);
|
|
|
|
pthread_mutex_unlock(&nas_data->buffer_mutex);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now write the new buffer to the network.
|
|
|
|
*/
|
|
|
|
AuWriteElement(nas_data->aud, nas_data->flow, 0, num, nas_data->server_buffer, AuFalse, &as);
|
2002-02-20 22:45:48 +00:00
|
|
|
if (as != AuSuccess)
|
|
|
|
nas_print_error(nas_data->aud, "nas_readBuffer(): AuWriteElement", as);
|
2001-12-03 01:13:48 +00:00
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2008-08-23 08:14:37 +00:00
|
|
|
static int nas_writeBuffer(struct ao_nas_data *nas_data, void *data, unsigned int len)
|
2001-12-03 01:13:48 +00:00
|
|
|
{
|
2002-02-20 22:45:48 +00:00
|
|
|
pthread_mutex_lock(&nas_data->buffer_mutex);
|
2002-10-06 17:31:36 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_DBG2, "ao_nas: nas_writeBuffer(): len=%d client=%d/%d server=%d/%d\n",
|
2002-02-20 22:45:48 +00:00
|
|
|
len, nas_data->client_buffer_used, nas_data->client_buffer_size,
|
|
|
|
nas_data->server_buffer_used, nas_data->server_buffer_size);
|
2001-12-03 01:13:48 +00:00
|
|
|
|
2005-06-17 02:22:34 +00:00
|
|
|
/* make sure we don't overflow the buffer */
|
|
|
|
if (len > nas_data->client_buffer_size - nas_data->client_buffer_used)
|
|
|
|
len = nas_data->client_buffer_size - nas_data->client_buffer_used;
|
2002-02-20 22:45:48 +00:00
|
|
|
memcpy(nas_data->client_buffer + nas_data->client_buffer_used, data, len);
|
|
|
|
nas_data->client_buffer_used += len;
|
2001-12-03 01:13:48 +00:00
|
|
|
|
2002-02-20 22:45:48 +00:00
|
|
|
pthread_mutex_unlock(&nas_data->buffer_mutex);
|
2005-06-17 02:22:34 +00:00
|
|
|
|
|
|
|
return len;
|
2001-12-03 01:13:48 +00:00
|
|
|
}
|
|
|
|
|
2002-02-20 22:45:48 +00:00
|
|
|
static int nas_empty_event_queue(struct ao_nas_data *nas_data)
|
2001-12-03 01:13:48 +00:00
|
|
|
{
|
2002-02-20 22:45:48 +00:00
|
|
|
AuEvent ev;
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
while (AuScanForTypedEvent(nas_data->aud, AuEventsQueuedAfterFlush,
|
|
|
|
AuTrue, AuEventTypeElementNotify, &ev)) {
|
|
|
|
AuDispatchEvent(nas_data->aud, &ev);
|
|
|
|
result = 1;
|
2001-12-03 01:13:48 +00:00
|
|
|
}
|
2002-02-20 22:45:48 +00:00
|
|
|
return result;
|
2001-12-03 01:13:48 +00:00
|
|
|
}
|
|
|
|
|
2002-02-20 22:45:48 +00:00
|
|
|
static void *nas_event_thread_start(void *data)
|
2001-12-03 01:13:48 +00:00
|
|
|
{
|
2002-02-20 22:45:48 +00:00
|
|
|
struct ao_nas_data *nas_data = data;
|
2001-12-03 01:13:48 +00:00
|
|
|
|
2002-02-20 22:45:48 +00:00
|
|
|
do {
|
2002-10-13 22:00:15 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_DBG2,
|
|
|
|
"ao_nas: event thread heartbeat (state=%s)\n",
|
|
|
|
nas_state(nas_data->state));
|
2002-02-20 22:45:48 +00:00
|
|
|
nas_empty_event_queue(nas_data);
|
2002-10-13 22:00:15 +00:00
|
|
|
usleep(1000);
|
2002-02-20 22:45:48 +00:00
|
|
|
} while (!nas_data->stop_thread);
|
2002-10-13 22:00:15 +00:00
|
|
|
|
|
|
|
return NULL;
|
2001-12-03 01:13:48 +00:00
|
|
|
}
|
|
|
|
|
2002-02-20 22:45:48 +00:00
|
|
|
static AuBool nas_error_handler(AuServer* aud, AuErrorEvent* ev)
|
2001-12-03 01:13:48 +00:00
|
|
|
{
|
|
|
|
char s[100];
|
|
|
|
AuGetErrorText(aud, ev->error_code, s, 100);
|
2002-10-06 17:31:36 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_ERR, "ao_nas: error [%s]\n"
|
2001-12-03 01:13:48 +00:00
|
|
|
"error_code: %d\n"
|
|
|
|
"request_code: %d\n"
|
|
|
|
"minor_code: %d\n",
|
|
|
|
s,
|
|
|
|
ev->error_code,
|
|
|
|
ev->request_code,
|
|
|
|
ev->minor_code);
|
|
|
|
|
|
|
|
return AuTrue;
|
|
|
|
}
|
|
|
|
|
2002-02-20 22:45:48 +00:00
|
|
|
static AuBool nas_event_handler(AuServer *aud, AuEvent *ev, AuEventHandlerRec *hnd)
|
|
|
|
{
|
|
|
|
AuElementNotifyEvent *event = (AuElementNotifyEvent *) ev;
|
|
|
|
struct ao_nas_data *nas_data = hnd->data;
|
|
|
|
|
2002-10-13 22:00:15 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_DBG2, "ao_nas: event_handler(): type %s kind %s state %s->%s reason %s numbytes %d expect_underrun %d\n",
|
|
|
|
nas_event_type(event->type),
|
|
|
|
nas_elementnotify_kind(event->kind),
|
|
|
|
nas_state(event->prev_state),
|
|
|
|
nas_state(event->cur_state),
|
|
|
|
nas_reason(event->reason),
|
2008-08-23 08:14:37 +00:00
|
|
|
(int)event->num_bytes,
|
2002-10-13 22:00:15 +00:00
|
|
|
nas_data->expect_underrun);
|
|
|
|
|
2005-06-17 02:22:34 +00:00
|
|
|
if (event->num_bytes > INT_MAX) {
|
|
|
|
mp_msg(MSGT_AO, MSGL_ERR, "ao_nas: num_bytes > 2GB, server buggy?\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event->num_bytes > nas_data->server_buffer_used)
|
|
|
|
event->num_bytes = nas_data->server_buffer_used;
|
2002-10-13 22:00:15 +00:00
|
|
|
nas_data->server_buffer_used -= event->num_bytes;
|
|
|
|
|
|
|
|
switch (event->reason) {
|
|
|
|
case AuReasonWatermark:
|
|
|
|
nas_readBuffer(nas_data, event->num_bytes);
|
|
|
|
break;
|
|
|
|
case AuReasonUnderrun:
|
|
|
|
// buffer underrun -> refill buffer
|
|
|
|
nas_data->server_buffer_used = 0;
|
|
|
|
if (nas_data->expect_underrun) {
|
|
|
|
nas_data->expect_underrun = 0;
|
|
|
|
} else {
|
2004-03-13 21:54:35 +00:00
|
|
|
static int hint = 1;
|
2002-10-13 22:00:15 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_WARN,
|
|
|
|
"ao_nas: Buffer underrun.\n");
|
2004-03-13 21:54:35 +00:00
|
|
|
if (hint) {
|
|
|
|
hint = 0;
|
|
|
|
mp_msg(MSGT_AO, MSGL_HINT,
|
|
|
|
"Possible reasons are:\n"
|
|
|
|
"1) Network congestion.\n"
|
|
|
|
"2) Your NAS server is too slow.\n"
|
|
|
|
"Try renicing your nasd to e.g. -15.\n");
|
|
|
|
}
|
2002-10-13 22:00:15 +00:00
|
|
|
}
|
|
|
|
if (nas_readBuffer(nas_data,
|
|
|
|
nas_data->server_buffer_size -
|
|
|
|
nas_data->server_buffer_used) != 0) {
|
|
|
|
event->cur_state = AuStateStart;
|
2002-02-20 22:45:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-10-13 22:00:15 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_DBG2,
|
|
|
|
"ao_nas: Can't refill buffer, stopping flow.\n");
|
2008-08-23 08:14:37 +00:00
|
|
|
AuStopFlow(aud, nas_data->flow, NULL);
|
2002-02-20 22:45:48 +00:00
|
|
|
break;
|
2002-10-13 22:00:15 +00:00
|
|
|
default:
|
2002-02-20 22:45:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-10-13 22:00:15 +00:00
|
|
|
nas_data->state=event->cur_state;
|
2002-02-20 22:45:48 +00:00
|
|
|
return AuTrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static AuDeviceID nas_find_device(AuServer *aud, int nch)
|
2001-12-03 01:13:48 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < AuServerNumDevices(aud); i++) {
|
|
|
|
AuDeviceAttributes *dev = AuServerDevice(aud, i);
|
|
|
|
if ((AuDeviceKind(dev) == AuComponentKindPhysicalOutput) &&
|
|
|
|
AuDeviceNumTracks(dev) == nch) {
|
|
|
|
return AuDeviceIdentifier(dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return AuNone;
|
|
|
|
}
|
|
|
|
|
2002-10-13 22:00:15 +00:00
|
|
|
static unsigned int nas_aformat_to_auformat(unsigned int *format)
|
2001-12-03 01:13:48 +00:00
|
|
|
{
|
2002-10-13 22:00:15 +00:00
|
|
|
switch (*format) {
|
2004-12-27 17:30:15 +00:00
|
|
|
case AF_FORMAT_U8:
|
2002-10-13 22:00:15 +00:00
|
|
|
return AuFormatLinearUnsigned8;
|
2004-12-27 17:30:15 +00:00
|
|
|
case AF_FORMAT_S8:
|
2002-10-13 22:00:15 +00:00
|
|
|
return AuFormatLinearSigned8;
|
2004-12-27 17:30:15 +00:00
|
|
|
case AF_FORMAT_U16_LE:
|
2002-10-13 22:00:15 +00:00
|
|
|
return AuFormatLinearUnsigned16LSB;
|
2004-12-27 17:30:15 +00:00
|
|
|
case AF_FORMAT_U16_BE:
|
2002-10-13 22:00:15 +00:00
|
|
|
return AuFormatLinearUnsigned16MSB;
|
2004-12-27 17:30:15 +00:00
|
|
|
case AF_FORMAT_S16_LE:
|
2002-10-13 22:00:15 +00:00
|
|
|
return AuFormatLinearSigned16LSB;
|
2004-12-27 17:30:15 +00:00
|
|
|
case AF_FORMAT_S16_BE:
|
2002-10-13 22:00:15 +00:00
|
|
|
return AuFormatLinearSigned16MSB;
|
2004-12-27 17:30:15 +00:00
|
|
|
case AF_FORMAT_MU_LAW:
|
2002-10-13 22:00:15 +00:00
|
|
|
return AuFormatULAW8;
|
2004-03-13 21:54:35 +00:00
|
|
|
default:
|
2004-12-27 17:30:15 +00:00
|
|
|
*format=AF_FORMAT_S16_NE;
|
2004-03-13 21:54:35 +00:00
|
|
|
return nas_aformat_to_auformat(format);
|
2001-12-03 01:13:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// to set/get/query special features/parameters
|
2003-03-21 16:42:50 +00:00
|
|
|
static int control(int cmd, void *arg)
|
2002-11-24 23:13:15 +00:00
|
|
|
{
|
2004-03-13 21:54:35 +00:00
|
|
|
AuElementParameters aep;
|
2002-11-24 23:13:15 +00:00
|
|
|
AuStatus as;
|
2003-03-14 17:19:19 +00:00
|
|
|
int retval = CONTROL_UNKNOWN;
|
|
|
|
|
2002-11-24 23:13:15 +00:00
|
|
|
ao_control_vol_t *vol = (ao_control_vol_t *)arg;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case AOCONTROL_GET_VOLUME:
|
|
|
|
|
2004-03-13 21:54:35 +00:00
|
|
|
vol->right = (float)nas_data->gain/AU_FIXED_POINT_SCALE*50;
|
2002-11-24 23:13:15 +00:00
|
|
|
vol->left = vol->right;
|
|
|
|
|
2008-08-23 08:14:37 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_DBG2, "ao_nas: AOCONTROL_GET_VOLUME: %.2f\n", vol->right);
|
2003-03-14 17:19:19 +00:00
|
|
|
retval = CONTROL_OK;
|
|
|
|
break;
|
2002-11-24 23:13:15 +00:00
|
|
|
|
|
|
|
case AOCONTROL_SET_VOLUME:
|
|
|
|
/*
|
|
|
|
* kn: we should have vol->left == vol->right but i don't
|
|
|
|
* know if something can change it outside of ao_nas
|
|
|
|
* so i take the mean of both values.
|
|
|
|
*/
|
2004-03-13 21:54:35 +00:00
|
|
|
nas_data->gain = AU_FIXED_POINT_SCALE*((vol->left+vol->right)/2)/50;
|
2008-08-23 08:14:37 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_DBG2, "ao_nas: AOCONTROL_SET_VOLUME: %.2f\n", (vol->left+vol->right)/2);
|
2004-03-13 21:54:35 +00:00
|
|
|
|
|
|
|
aep.parameters[AuParmsMultiplyConstantConstant]=nas_data->gain;
|
|
|
|
aep.flow = nas_data->flow;
|
|
|
|
aep.element_num = 1;
|
|
|
|
aep.num_parameters = AuParmsMultiplyConstant;
|
2002-11-24 23:13:15 +00:00
|
|
|
|
2004-03-13 21:54:35 +00:00
|
|
|
AuSetElementParameters(nas_data->aud, 1, &aep, &as);
|
2002-11-24 23:13:15 +00:00
|
|
|
if (as != AuSuccess) {
|
|
|
|
nas_print_error(nas_data->aud,
|
2004-03-13 21:54:35 +00:00
|
|
|
"control(): AuSetElementParameters", as);
|
2003-03-14 17:19:19 +00:00
|
|
|
retval = CONTROL_ERROR;
|
|
|
|
} else retval = CONTROL_OK;
|
|
|
|
break;
|
2002-11-24 23:13:15 +00:00
|
|
|
};
|
2003-03-14 17:19:19 +00:00
|
|
|
|
|
|
|
return retval;
|
2001-12-03 01:13:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// open & setup audio device
|
|
|
|
// return: 1=success 0=fail
|
|
|
|
static int init(int rate,int channels,int format,int flags)
|
|
|
|
{
|
|
|
|
AuElement elms[3];
|
|
|
|
AuStatus as;
|
2002-10-13 22:00:15 +00:00
|
|
|
unsigned char auformat = nas_aformat_to_auformat(&format);
|
2002-02-20 22:45:48 +00:00
|
|
|
int bytes_per_sample = channels * AuSizeofFormat(auformat);
|
2002-10-13 22:00:15 +00:00
|
|
|
int buffer_size;
|
2001-12-03 01:13:48 +00:00
|
|
|
char *server;
|
|
|
|
|
2008-08-23 08:14:37 +00:00
|
|
|
(void)flags; /* shut up 'unused parameter' warning */
|
|
|
|
|
2002-02-20 22:45:48 +00:00
|
|
|
nas_data=malloc(sizeof(struct ao_nas_data));
|
2002-05-18 09:15:49 +00:00
|
|
|
memset(nas_data, 0, sizeof(struct ao_nas_data));
|
2002-02-20 22:45:48 +00:00
|
|
|
|
2002-10-06 17:31:36 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_V, "ao2: %d Hz %d chans %s\n",rate,channels,
|
2004-12-28 19:11:14 +00:00
|
|
|
af_fmt2str_short(format));
|
2001-12-03 01:13:48 +00:00
|
|
|
|
2002-10-13 22:00:15 +00:00
|
|
|
ao_data.format = format;
|
2001-12-03 01:13:48 +00:00
|
|
|
ao_data.samplerate = rate;
|
|
|
|
ao_data.channels = channels;
|
2002-02-20 22:45:48 +00:00
|
|
|
ao_data.outburst = NAS_FRAG_SIZE;
|
2001-12-03 01:13:48 +00:00
|
|
|
ao_data.bps = rate * bytes_per_sample;
|
2002-10-13 22:00:15 +00:00
|
|
|
buffer_size = ao_data.bps; /* buffer 1 second */
|
|
|
|
/*
|
|
|
|
* round up to multiple of NAS_FRAG_SIZE
|
|
|
|
* divide by 3 first because of 2:1 split
|
|
|
|
*/
|
|
|
|
buffer_size = (buffer_size/3 + NAS_FRAG_SIZE-1) & ~(NAS_FRAG_SIZE-1);
|
|
|
|
ao_data.buffersize = buffer_size*3;
|
|
|
|
|
|
|
|
nas_data->client_buffer_size = buffer_size*2;
|
|
|
|
nas_data->client_buffer = malloc(nas_data->client_buffer_size);
|
|
|
|
nas_data->server_buffer_size = buffer_size;
|
|
|
|
nas_data->server_buffer = malloc(nas_data->server_buffer_size);
|
2001-12-03 01:13:48 +00:00
|
|
|
|
|
|
|
if (!bytes_per_sample) {
|
2002-10-06 17:31:36 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_ERR, "ao_nas: init(): Zero bytes per sample -> nosound\n");
|
2001-12-03 01:13:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-10-07 01:50:49 +00:00
|
|
|
if (!(server = getenv("AUDIOSERVER")) &&
|
|
|
|
!(server = getenv("DISPLAY"))) {
|
|
|
|
mp_msg(MSGT_AO, MSGL_ERR, "ao_nas: init(): AUDIOSERVER environment variable not set -> nosound\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2001-12-03 01:13:48 +00:00
|
|
|
|
2002-10-06 17:31:36 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_V, "ao_nas: init(): Using audioserver %s\n", server);
|
2001-12-03 01:13:48 +00:00
|
|
|
|
2002-02-20 22:45:48 +00:00
|
|
|
nas_data->aud = AuOpenServer(server, 0, NULL, 0, NULL, NULL);
|
2002-10-07 01:50:49 +00:00
|
|
|
if (!nas_data->aud) {
|
2002-10-06 17:31:36 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_ERR, "ao_nas: init(): Can't open nas audio server -> nosound\n");
|
2001-12-03 01:13:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-19 16:28:59 +00:00
|
|
|
while (channels>0) {
|
2002-10-07 01:50:49 +00:00
|
|
|
nas_data->dev = nas_find_device(nas_data->aud, channels);
|
|
|
|
if (nas_data->dev != AuNone &&
|
2002-10-13 22:00:15 +00:00
|
|
|
((nas_data->flow = AuCreateFlow(nas_data->aud, NULL)) != 0))
|
2002-10-07 01:50:49 +00:00
|
|
|
break;
|
|
|
|
channels--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nas_data->flow == 0) {
|
|
|
|
mp_msg(MSGT_AO, MSGL_ERR, "ao_nas: init(): Can't find a suitable output device -> nosound\n");
|
2002-02-20 22:45:48 +00:00
|
|
|
AuCloseServer(nas_data->aud);
|
|
|
|
nas_data->aud = 0;
|
2001-12-03 01:13:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-10-13 22:00:15 +00:00
|
|
|
AuMakeElementImportClient(elms, rate, auformat, channels, AuTrue,
|
|
|
|
buffer_size / bytes_per_sample,
|
|
|
|
(buffer_size - NAS_FRAG_SIZE) /
|
|
|
|
bytes_per_sample, 0, NULL);
|
2004-03-13 21:54:35 +00:00
|
|
|
nas_data->gain = AuFixedPointFromFraction(1, 1);
|
|
|
|
AuMakeElementMultiplyConstant(elms+1, 0, nas_data->gain);
|
|
|
|
AuMakeElementExportDevice(elms+2, 1, nas_data->dev, rate,
|
2001-12-03 01:13:48 +00:00
|
|
|
AuUnlimitedSamples, 0, NULL);
|
2004-03-13 21:54:35 +00:00
|
|
|
AuSetElements(nas_data->aud, nas_data->flow, AuTrue, sizeof(elms)/sizeof(*elms), elms, &as);
|
2002-10-13 22:00:15 +00:00
|
|
|
if (as != AuSuccess) {
|
2002-02-20 22:45:48 +00:00
|
|
|
nas_print_error(nas_data->aud, "init(): AuSetElements", as);
|
2002-10-13 22:00:15 +00:00
|
|
|
AuCloseServer(nas_data->aud);
|
|
|
|
nas_data->aud = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2002-02-20 22:45:48 +00:00
|
|
|
AuRegisterEventHandler(nas_data->aud, AuEventHandlerIDMask |
|
2001-12-03 01:13:48 +00:00
|
|
|
AuEventHandlerTypeMask,
|
2002-02-20 22:45:48 +00:00
|
|
|
AuEventTypeElementNotify, nas_data->flow,
|
|
|
|
nas_event_handler, (AuPointer) nas_data);
|
|
|
|
AuSetErrorHandler(nas_data->aud, nas_error_handler);
|
2002-10-13 22:00:15 +00:00
|
|
|
nas_data->state=AuStateStop;
|
2002-10-06 17:30:38 +00:00
|
|
|
nas_data->expect_underrun=0;
|
2001-12-03 01:13:48 +00:00
|
|
|
|
2002-02-20 22:45:48 +00:00
|
|
|
pthread_mutex_init(&nas_data->buffer_mutex, NULL);
|
|
|
|
pthread_create(&nas_data->event_thread, NULL, &nas_event_thread_start, nas_data);
|
2001-12-03 01:13:48 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// close audio device
|
2004-04-06 17:55:36 +00:00
|
|
|
static void uninit(int immed){
|
2002-02-20 22:45:48 +00:00
|
|
|
|
2002-10-13 22:00:15 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_DBG3, "ao_nas: uninit()\n");
|
2002-10-06 17:30:38 +00:00
|
|
|
|
2002-10-13 22:00:15 +00:00
|
|
|
nas_data->expect_underrun = 1;
|
2005-02-27 23:06:32 +00:00
|
|
|
if (!immed)
|
2002-10-13 22:00:15 +00:00
|
|
|
while (nas_data->state != AuStateStop) usleep(1000);
|
2002-02-20 22:45:48 +00:00
|
|
|
nas_data->stop_thread = 1;
|
|
|
|
pthread_join(nas_data->event_thread, NULL);
|
|
|
|
AuCloseServer(nas_data->aud);
|
|
|
|
nas_data->aud = 0;
|
|
|
|
free(nas_data->client_buffer);
|
2002-09-20 18:26:54 +00:00
|
|
|
free(nas_data->server_buffer);
|
2001-12-03 01:13:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop playing and empty buffers (for seeking/pause)
|
2006-02-09 14:08:03 +00:00
|
|
|
static void reset(void){
|
2002-02-20 22:45:48 +00:00
|
|
|
AuStatus as;
|
|
|
|
|
2002-10-13 22:00:15 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_DBG3, "ao_nas: reset()\n");
|
2002-10-06 17:30:38 +00:00
|
|
|
|
2002-02-20 22:45:48 +00:00
|
|
|
pthread_mutex_lock(&nas_data->buffer_mutex);
|
|
|
|
nas_data->client_buffer_used = 0;
|
2002-10-13 22:00:15 +00:00
|
|
|
pthread_mutex_unlock(&nas_data->buffer_mutex);
|
|
|
|
while (nas_data->state != AuStateStop) {
|
2002-02-20 22:45:48 +00:00
|
|
|
AuStopFlow(nas_data->aud, nas_data->flow, &as);
|
|
|
|
if (as != AuSuccess)
|
|
|
|
nas_print_error(nas_data->aud, "reset(): AuStopFlow", as);
|
2002-10-13 22:00:15 +00:00
|
|
|
usleep(1000);
|
2001-12-03 01:13:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop playing, keep buffers (for pause)
|
2006-02-09 14:08:03 +00:00
|
|
|
static void audio_pause(void)
|
2001-12-03 01:13:48 +00:00
|
|
|
{
|
2002-02-20 22:45:48 +00:00
|
|
|
AuStatus as;
|
2002-10-13 22:00:15 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_DBG3, "ao_nas: audio_pause()\n");
|
2002-02-20 22:45:48 +00:00
|
|
|
|
2002-10-13 22:00:15 +00:00
|
|
|
AuStopFlow(nas_data->aud, nas_data->flow, &as);
|
2001-12-03 01:13:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// resume playing, after audio_pause()
|
2006-02-09 14:08:03 +00:00
|
|
|
static void audio_resume(void)
|
2001-12-03 01:13:48 +00:00
|
|
|
{
|
2002-02-20 22:45:48 +00:00
|
|
|
AuStatus as;
|
|
|
|
|
2002-10-13 22:00:15 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_DBG3, "ao_nas: audio_resume()\n");
|
2002-02-20 22:45:48 +00:00
|
|
|
|
|
|
|
AuStartFlow(nas_data->aud, nas_data->flow, &as);
|
|
|
|
if (as != AuSuccess)
|
2002-10-13 22:00:15 +00:00
|
|
|
nas_print_error(nas_data->aud,
|
|
|
|
"play(): AuStartFlow", as);
|
2001-12-03 01:13:48 +00:00
|
|
|
}
|
|
|
|
|
2002-02-20 22:45:48 +00:00
|
|
|
|
2001-12-03 01:13:48 +00:00
|
|
|
// return: how many bytes can be played without blocking
|
2006-02-09 14:08:03 +00:00
|
|
|
static int get_space(void)
|
2001-12-03 01:13:48 +00:00
|
|
|
{
|
|
|
|
int result;
|
2002-02-20 22:45:48 +00:00
|
|
|
|
2002-10-13 22:00:15 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_DBG3, "ao_nas: get_space()\n");
|
2001-12-03 01:13:48 +00:00
|
|
|
|
2002-02-20 22:45:48 +00:00
|
|
|
pthread_mutex_lock(&nas_data->buffer_mutex);
|
|
|
|
result = nas_data->client_buffer_size - nas_data->client_buffer_used;
|
|
|
|
pthread_mutex_unlock(&nas_data->buffer_mutex);
|
2001-12-03 01:13:48 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// plays 'len' bytes of 'data'
|
|
|
|
// it should round it down to outburst*n
|
|
|
|
// return: number of bytes played
|
2002-02-20 22:45:48 +00:00
|
|
|
static int play(void* data,int len,int flags)
|
|
|
|
{
|
2008-08-24 13:36:04 +00:00
|
|
|
int written, maxbursts = 0, playbursts = 0;
|
2002-02-20 22:45:48 +00:00
|
|
|
AuStatus as;
|
|
|
|
|
2002-10-13 22:00:15 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_DBG3,
|
|
|
|
"ao_nas: play(%p, %d, %d)\n",
|
|
|
|
data, len, flags);
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
return 0;
|
2002-02-20 22:45:48 +00:00
|
|
|
|
2008-08-24 13:36:04 +00:00
|
|
|
if (!(flags & AOPLAY_FINAL_CHUNK)) {
|
|
|
|
pthread_mutex_lock(&nas_data->buffer_mutex);
|
|
|
|
maxbursts = (nas_data->client_buffer_size -
|
|
|
|
nas_data->client_buffer_used) / ao_data.outburst;
|
|
|
|
playbursts = len / ao_data.outburst;
|
|
|
|
len = (playbursts > maxbursts ? maxbursts : playbursts) *
|
|
|
|
ao_data.outburst;
|
|
|
|
pthread_mutex_unlock(&nas_data->buffer_mutex);
|
2008-08-15 17:14:53 +00:00
|
|
|
}
|
|
|
|
|
2008-08-24 13:36:04 +00:00
|
|
|
/*
|
|
|
|
* If AOPLAY_FINAL_CHUNK is set, we did not actually check len fits
|
|
|
|
* into the available buffer space, but mplayer.c shouldn't give us
|
|
|
|
* more to play than we report to it by get_space(), so this should be
|
|
|
|
* fine.
|
|
|
|
*/
|
|
|
|
written = nas_writeBuffer(nas_data, data, len);
|
2002-10-06 17:30:38 +00:00
|
|
|
|
2002-10-13 22:00:15 +00:00
|
|
|
if (nas_data->state != AuStateStart &&
|
2008-08-24 13:36:04 +00:00
|
|
|
(maxbursts == playbursts ||
|
|
|
|
flags & AOPLAY_FINAL_CHUNK)) {
|
2002-10-13 22:00:15 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_DBG2, "ao_nas: play(): Starting flow.\n");
|
2002-10-06 17:30:38 +00:00
|
|
|
nas_data->expect_underrun = 1;
|
|
|
|
AuStartFlow(nas_data->aud, nas_data->flow, &as);
|
|
|
|
if (as != AuSuccess)
|
|
|
|
nas_print_error(nas_data->aud, "play(): AuStartFlow", as);
|
|
|
|
}
|
|
|
|
|
2008-08-24 13:36:04 +00:00
|
|
|
return written;
|
2001-12-03 01:13:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// return: delay in seconds between first and last sample in buffer
|
2006-02-09 14:08:03 +00:00
|
|
|
static float get_delay(void)
|
2001-12-03 01:13:48 +00:00
|
|
|
{
|
|
|
|
float result;
|
2002-02-20 22:45:48 +00:00
|
|
|
|
2002-10-13 22:00:15 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_DBG3, "ao_nas: get_delay()\n");
|
2002-02-20 22:45:48 +00:00
|
|
|
|
|
|
|
pthread_mutex_lock(&nas_data->buffer_mutex);
|
|
|
|
result = ((float)(nas_data->client_buffer_used +
|
|
|
|
nas_data->server_buffer_used)) /
|
|
|
|
(float)ao_data.bps;
|
|
|
|
pthread_mutex_unlock(&nas_data->buffer_mutex);
|
2001-12-03 01:13:48 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|