annotate libao2/ao_nas.c @ 4642:e534c7dc0cc0

check for overflow in new_sh_video and new_sh_audio
author alex
date Sun, 10 Feb 2002 18:09:20 +0000
parents 8818c12743a8
children 080cf3df3e4e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3336
8818c12743a8 patch by Tobias Diedrich <td@informatik.uni-hannover.de>
pl
parents: 3276
diff changeset
1 /*
8818c12743a8 patch by Tobias Diedrich <td@informatik.uni-hannover.de>
pl
parents: 3276
diff changeset
2 * NAS output plugin for mplayer
8818c12743a8 patch by Tobias Diedrich <td@informatik.uni-hannover.de>
pl
parents: 3276
diff changeset
3 *
8818c12743a8 patch by Tobias Diedrich <td@informatik.uni-hannover.de>
pl
parents: 3276
diff changeset
4 * based on the libaudiooss parts rewritten by me, which were
8818c12743a8 patch by Tobias Diedrich <td@informatik.uni-hannover.de>
pl
parents: 3276
diff changeset
5 * originally based on the NAS output plugin for xmms.
8818c12743a8 patch by Tobias Diedrich <td@informatik.uni-hannover.de>
pl
parents: 3276
diff changeset
6 *
8818c12743a8 patch by Tobias Diedrich <td@informatik.uni-hannover.de>
pl
parents: 3276
diff changeset
7 * xmms plugin by Willem Monsuwe
8818c12743a8 patch by Tobias Diedrich <td@informatik.uni-hannover.de>
pl
parents: 3276
diff changeset
8 * adapted for libaudiooss by Jon Trulson
8818c12743a8 patch by Tobias Diedrich <td@informatik.uni-hannover.de>
pl
parents: 3276
diff changeset
9 * further modified by Erik Inge Bolsų
8818c12743a8 patch by Tobias Diedrich <td@informatik.uni-hannover.de>
pl
parents: 3276
diff changeset
10 * largely rewritten and used for this
8818c12743a8 patch by Tobias Diedrich <td@informatik.uni-hannover.de>
pl
parents: 3276
diff changeset
11 * plugin by Tobias Diedrich
8818c12743a8 patch by Tobias Diedrich <td@informatik.uni-hannover.de>
pl
parents: 3276
diff changeset
12 *
8818c12743a8 patch by Tobias Diedrich <td@informatik.uni-hannover.de>
pl
parents: 3276
diff changeset
13 */
8818c12743a8 patch by Tobias Diedrich <td@informatik.uni-hannover.de>
pl
parents: 3276
diff changeset
14
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
15 #include <stdio.h>
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
16 #include <stdlib.h>
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
17 #include <pthread.h>
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
18 #include <audio/audiolib.h>
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
19
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
20 #include "audio_out.h"
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
21 #include "audio_out_internal.h"
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
22 #include "afmt.h"
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
23
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
24 #define FRAG_SIZE 4096
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
25 #define FRAG_COUNT 8
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
26 #define BUFFER_SIZE FRAG_SIZE * FRAG_COUNT
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
27
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
28 #define NAS_DEBUG 0
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
29
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
30 #if NAS_DEBUG == 1
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
31 #define DPRINTF(format, args...) fprintf(stderr, format, ## args); \
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
32 fflush(stderr)
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
33 #else
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
34 #define DPRINTF(format, args...)
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
35 #endif
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
36
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
37 static ao_info_t info =
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
38 {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
39 "NAS audio output",
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
40 "nas",
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
41 "Tobias Diedrich",
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
42 ""
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
43 };
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
44
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
45 static AuServer* aud;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
46 static AuFlowID flow;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
47 static AuDeviceID dev;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
48
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
49 static void *client_buffer;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
50 static int client_buffer_size = BUFFER_SIZE;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
51 static int client_buffer_used;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
52 static int server_buffer_size = BUFFER_SIZE;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
53 static int server_buffer_used;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
54 static pthread_mutex_t buffer_mutex = PTHREAD_MUTEX_INITIALIZER;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
55
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
56 pthread_t event_thread;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
57 static int stop_thread;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
58
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
59 LIBAO_EXTERN(nas)
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
60
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
61 static void wait_for_event()
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
62 {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
63 AuEvent ev;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
64
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
65 AuNextEvent(aud, AuTrue, &ev);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
66 AuDispatchEvent(aud, &ev);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
67 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
68
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
69 static int readBuffer(int num)
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
70 {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
71 pthread_mutex_lock(&buffer_mutex);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
72 DPRINTF("readBuffer(): num=%d client=%d/%d server=%d/%d\n",
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
73 num,
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
74 client_buffer_used, client_buffer_size,
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
75 server_buffer_used, server_buffer_size);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
76
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
77 if (client_buffer_used == 0) {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
78 DPRINTF("buffer is empty, nothing read.\n");
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
79 pthread_mutex_unlock(&buffer_mutex);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
80 return 0;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
81 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
82 if (client_buffer_used < num)
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
83 num = client_buffer_used;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
84
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
85 AuWriteElement(aud, flow, 0, num, client_buffer, AuFalse, NULL);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
86 client_buffer_used -= num;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
87 server_buffer_used += num;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
88 memmove(client_buffer, client_buffer + num, client_buffer_used);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
89 pthread_mutex_unlock(&buffer_mutex);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
90
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
91 return num;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
92 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
93
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
94 static void writeBuffer(void *data, int len)
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
95 {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
96 pthread_mutex_lock(&buffer_mutex);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
97 DPRINTF("writeBuffer(): len=%d client=%d/%d server=%d/%d\n",
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
98 len, client_buffer_used, client_buffer_size,
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
99 server_buffer_used, server_buffer_size);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
100
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
101 memcpy(client_buffer + client_buffer_used, data, len);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
102 client_buffer_used += len;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
103
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
104 pthread_mutex_unlock(&buffer_mutex);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
105 if (server_buffer_used < server_buffer_size)
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
106 readBuffer(server_buffer_size - server_buffer_used);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
107 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
108
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
109
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
110 static void *event_thread_start(void *data)
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
111 {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
112 while (!stop_thread) {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
113 wait_for_event();
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
114 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
115 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
116
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
117 static AuBool event_handler(AuServer *aud, AuEvent *ev, AuEventHandlerRec *hnd)
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
118 {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
119 switch (ev->type) {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
120 case AuEventTypeElementNotify: {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
121 AuElementNotifyEvent *event = (AuElementNotifyEvent *) ev;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
122 DPRINTF("event_handler(): kind %d state %d->%d reason %d numbytes %d\n",
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
123 event->kind,
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
124 event->prev_state,
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
125 event->cur_state,
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
126 event->reason,
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
127 event->num_bytes);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
128
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
129 switch (event->kind) {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
130 case AuElementNotifyKindLowWater:
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
131 server_buffer_used -= event->num_bytes;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
132 readBuffer(event->num_bytes);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
133 break;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
134 case AuElementNotifyKindState:
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
135 if ((event->cur_state == AuStatePause) &&
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
136 (event->reason != AuReasonUser)) {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
137 // buffer underrun -> refill buffer
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
138 server_buffer_used = 0;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
139 readBuffer(server_buffer_size - server_buffer_used);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
140 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
141 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
142 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
143 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
144 return AuTrue;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
145 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
146
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
147 static AuBool error_handler(AuServer* aud, AuErrorEvent* ev)
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
148 {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
149 char s[100];
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
150 AuGetErrorText(aud, ev->error_code, s, 100);
3336
8818c12743a8 patch by Tobias Diedrich <td@informatik.uni-hannover.de>
pl
parents: 3276
diff changeset
151 fprintf(stderr,"ao_nas: error [%s]\n"
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
152 "error_code: %d\n"
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
153 "request_code: %d\n"
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
154 "minor_code: %d\n",
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
155 s,
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
156 ev->error_code,
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
157 ev->request_code,
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
158 ev->minor_code);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
159 fflush(stderr);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
160
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
161 return AuTrue;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
162 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
163
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
164 static AuDeviceID find_device(int nch)
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
165 {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
166 int i;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
167 for (i = 0; i < AuServerNumDevices(aud); i++) {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
168 AuDeviceAttributes *dev = AuServerDevice(aud, i);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
169 if ((AuDeviceKind(dev) == AuComponentKindPhysicalOutput) &&
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
170 AuDeviceNumTracks(dev) == nch) {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
171 return AuDeviceIdentifier(dev);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
172 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
173 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
174 return AuNone;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
175 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
176
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
177 static unsigned char aformat_to_auformat(unsigned int format)
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
178 {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
179 switch (format) {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
180 case AFMT_U8: return AuFormatLinearUnsigned8;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
181 case AFMT_S8: return AuFormatLinearSigned8;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
182 case AFMT_U16_LE: return AuFormatLinearUnsigned16LSB;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
183 case AFMT_U16_BE: return AuFormatLinearUnsigned16MSB;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
184 case AFMT_S16_LE: return AuFormatLinearSigned16LSB;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
185 case AFMT_S16_BE: return AuFormatLinearSigned16MSB;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
186 case AFMT_MU_LAW: return AuFormatULAW8;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
187 default: return 0;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
188 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
189 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
190
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
191 // to set/get/query special features/parameters
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
192 static int control(int cmd,int arg){
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
193 return -1;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
194 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
195
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
196 // open & setup audio device
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
197 // return: 1=success 0=fail
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
198 static int init(int rate,int channels,int format,int flags)
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
199 {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
200 AuElement elms[3];
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
201 AuStatus as;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
202 unsigned char auformat = aformat_to_auformat(format);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
203 int bytes_per_sample;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
204 char *server;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
205
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
206 printf("ao2: %d Hz %d chans %s\n",rate,channels,
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
207 audio_out_format_name(format));
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
208
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
209 if (!auformat) {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
210 printf("Unsupported format -> nosound\n");
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
211 return 0;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
212 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
213
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
214 client_buffer = malloc(BUFFER_SIZE);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
215 bytes_per_sample = channels * AuSizeofFormat(auformat);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
216
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
217 ao_data.samplerate = rate;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
218 ao_data.channels = channels;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
219 ao_data.buffersize = BUFFER_SIZE * 2;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
220 ao_data.outburst = FRAG_SIZE;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
221 ao_data.bps = rate * bytes_per_sample;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
222
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
223 if (!bytes_per_sample) {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
224 printf("Zero bytes per sample -> nosound\n");
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
225 return 0;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
226 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
227
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
228 if (!(server = getenv("AUDIOSERVER")))
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
229 server = getenv("DISPLAY");
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
230
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
231 if (!server) // default to tcp/localhost:8000
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
232 server = "tcp/localhost:8000";
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
233
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
234 printf("Using audioserver %s\n", server);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
235
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
236 aud = AuOpenServer(server, 0, NULL, 0, NULL, NULL);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
237 if (!aud){
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
238 printf("Can't open nas audio server -> nosound\n");
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
239 return 0;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
240 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
241
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
242 dev = find_device(channels);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
243 if ((dev == AuNone) || (!(flow = AuCreateFlow(aud, NULL)))) {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
244 printf("Can't find a device serving that many channels -> nosound\n");
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
245 AuCloseServer(aud);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
246 aud = 0;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
247 return 0;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
248 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
249
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
250 AuMakeElementImportClient(elms, rate, auformat, channels, AuTrue,
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
251 BUFFER_SIZE / bytes_per_sample,
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
252 (BUFFER_SIZE - FRAG_SIZE) / bytes_per_sample,
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
253 0, NULL);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
254 AuMakeElementExportDevice(elms+1, 0, dev, rate,
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
255 AuUnlimitedSamples, 0, NULL);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
256 AuSetElements(aud, flow, AuTrue, 2, elms, &as);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
257 if (as != AuSuccess)
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
258 printf("AuSetElements returned status %d!\n", as);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
259 AuRegisterEventHandler(aud, AuEventHandlerIDMask |
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
260 AuEventHandlerTypeMask,
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
261 AuEventTypeElementNotify, flow,
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
262 event_handler, (AuPointer) NULL);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
263 AuSetErrorHandler(aud, error_handler);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
264 AuStartFlow(aud, flow, &as);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
265 if (as != AuSuccess)
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
266 printf("AuSetElements returned status %d!\n", as);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
267
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
268 /*
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
269 * Wait for first buffer underrun event
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
270 *
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
271 * For some weird reason we get a buffer underrun event if we
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
272 * don't fill the server buffer fast enough after staring the
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
273 * flow. So we just wait for it to happen to be in a sane state.
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
274 */
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
275 wait_for_event();
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
277 pthread_create(&event_thread, NULL, &event_thread_start, NULL);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
278
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
279 return 1;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
280 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
281
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
282 // close audio device
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
283 static void uninit(){
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
284 stop_thread = 1;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
285 pthread_join(event_thread, NULL);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
286 AuStopFlow(aud, flow, NULL);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
287 AuCloseServer(aud);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
288 aud = 0;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
289 free(client_buffer);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
290 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
291
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
292 // stop playing and empty buffers (for seeking/pause)
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
293 static void reset(){
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
294 pthread_mutex_lock(&buffer_mutex);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
295 client_buffer_used = 0;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
296 pthread_mutex_unlock(&buffer_mutex);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
297 while (server_buffer_used > 0) {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
298 usleep(1000);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
299 // DPRINTF("used=%d\n", server_buffer_used);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
300 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
301 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
302
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
303 // stop playing, keep buffers (for pause)
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
304 static void audio_pause()
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
305 {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
306 // for now, just call reset();
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
307 reset();
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
308 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
309
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
310 // resume playing, after audio_pause()
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
311 static void audio_resume()
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
312 {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
313 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
314
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
315 // return: how many bytes can be played without blocking
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
316 static int get_space()
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
317 {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
318 int result;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
319
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
320 pthread_mutex_lock(&buffer_mutex);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
321 result = client_buffer_size - client_buffer_used;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
322 pthread_mutex_unlock(&buffer_mutex);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
323
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
324 return result;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
325 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
326
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
327 // plays 'len' bytes of 'data'
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
328 // it should round it down to outburst*n
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
329 // return: number of bytes played
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
330 static int play(void* data,int len,int flags){
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
331 writeBuffer(data, len);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
332 // printf("ao_nas: wrote %d bytes of audio data\n", len);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
333 return len;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
334 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
335
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
336 // return: delay in seconds between first and last sample in buffer
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
337 static float get_delay()
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
338 {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
339 float result;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
340
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
341 pthread_mutex_lock(&buffer_mutex);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
342 result = ((float)(client_buffer_used + server_buffer_used)) /
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
343 (float)ao_data.bps;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
344 pthread_mutex_unlock(&buffer_mutex);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
345
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
346 return result;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
347 }