annotate libao2/ao_nas.c @ 7449:28785e6e6900

"One can cause a permanent hang on a seek, and the other just causes general jerkiness because the writer thread was holding a lock during the time it was writing to the network." patch by Sidik Isani <lksi@cfht.hawaii.edu>
author arpi
date Fri, 20 Sep 2002 18:26:54 +0000
parents 34d5c9a67b94
children 09ccf6147d76
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
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
24 #define NAS_FRAG_SIZE 4096
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
25 #define NAS_FRAG_COUNT 8
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
26 #define NAS_BUFFER_SIZE NAS_FRAG_SIZE * NAS_FRAG_COUNT
3276
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
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
31
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
32 static char *nas_event_types[] = {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
33 "Undefined",
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
34 "Undefined",
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
35 "ElementNotify",
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
36 "GrabNotify",
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
37 "MonitorNotify",
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
38 "BucketNotify",
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
39 "DeviceNotify"
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
40 };
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
41
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
42 static char *nas_elementnotify_kinds[] = {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
43 "LowWater",
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
44 "HighWater",
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
45 "State",
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
46 "Unknown"
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
47 };
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
48
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
49 static char *nas_states[] = {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
50 "Stop",
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
51 "Start",
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
52 "Pause",
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
53 "Any"
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
54 };
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
55
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
56 static char *nas_reasons[] = {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
57 "User",
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
58 "Underrun",
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
59 "Overrun",
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
60 "EOF",
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
61 "Watermark",
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
62 "Hardware",
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
63 "Any"
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
64 };
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
65
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
66 static char* nas_reason(unsigned int reason)
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
67 {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
68 if (reason > 6) reason = 6;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
69 return nas_reasons[reason];
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
70 }
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
71
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
72 static char* nas_elementnotify_kind(unsigned int kind)
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
73 {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
74 if (kind > 2) kind = 3;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
75 return nas_elementnotify_kinds[kind];
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
76 }
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
77
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
78 static char* nas_event_type(unsigned int type) {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
79 if (type > 6) type = 0;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
80 return nas_event_types[type];
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
81 }
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
82
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
83 static char* nas_state(unsigned int state) {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
84 if (state>3) state = 3;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
85 return nas_states[state];
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
86 }
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
87
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
88 #define DPRINTF(format, args...) fprintf(stderr, format, ## args); \
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
89 fflush(stderr)
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
90 #else
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
91 #define DPRINTF(format, args...)
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
92 #endif
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 ao_info_t info =
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 "NAS audio output",
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
97 "nas",
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
98 "Tobias Diedrich",
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
99 ""
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
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
102 struct ao_nas_data {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
103 AuServer *aud;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
104 AuFlowID flow;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
105 AuDeviceID dev;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
106
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
107 int flow_stopped;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
108 int flow_paused;
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
109
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
110 void *client_buffer;
7449
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
111 void *server_buffer;
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
112 int client_buffer_size;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
113 int client_buffer_used;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
114 int server_buffer_size;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
115 int server_buffer_used;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
116 pthread_mutex_t buffer_mutex;
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
117
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
118 pthread_t event_thread;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
119 int stop_thread;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
120 };
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
121
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
122 static struct ao_nas_data *nas_data;
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
123
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
124 LIBAO_EXTERN(nas)
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
125
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
126 static void nas_print_error(AuServer *aud, char *prefix, AuStatus as)
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
127 {
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
128 char s[100];
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
129 AuGetErrorText(aud, as, s, 100);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
130 fprintf(stderr, "ao_nas: %s: returned status %d (%s)\n", prefix, as, s);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
131 fflush(stderr);
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
132 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
133
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
134 static int nas_readBuffer(struct ao_nas_data *nas_data, int num)
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
135 {
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
136 AuStatus as;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
137
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
138 pthread_mutex_lock(&nas_data->buffer_mutex);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
139 DPRINTF("ao_nas: nas_readBuffer(): num=%d client=%d/%d server=%d/%d\n",
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
140 num,
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
141 nas_data->client_buffer_used, nas_data->client_buffer_size,
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
142 nas_data->server_buffer_used, nas_data->server_buffer_size);
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
143
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
144 if (nas_data->client_buffer_used == 0) {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
145 DPRINTF("ao_nas: buffer is empty, nothing read.\n");
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
146 pthread_mutex_unlock(&nas_data->buffer_mutex);
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
147 return 0;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
148 }
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
149 if (nas_data->client_buffer_used < num)
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
150 num = nas_data->client_buffer_used;
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
151
7449
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
152 /*
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
153 * It is not appropriate to call AuWriteElement() here because the
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
154 * buffer is locked and delays writing to the network will cause
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
155 * other threads to block waiting for buffer_mutex. Instead the
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
156 * data is copied to "server_buffer" and written it to the network
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
157 * outside of the locked section of code.
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
158 *
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
159 * (Note: Rather than these two buffers, a single circular buffer
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
160 * could eliminate the memcpy/memmove steps.)
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
161 */
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
162 memcpy(nas_data->server_buffer, nas_data->client_buffer, num);
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
163
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
164 nas_data->client_buffer_used -= num;
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
165 nas_data->server_buffer_used += num;
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
166 memmove(nas_data->client_buffer, nas_data->client_buffer + num, nas_data->client_buffer_used);
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
167 pthread_mutex_unlock(&nas_data->buffer_mutex);
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
168
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
169 /*
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
170 * Now write the new buffer to the network.
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
171 */
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
172 AuWriteElement(nas_data->aud, nas_data->flow, 0, num, nas_data->server_buffer, AuFalse, &as);
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
173 if (as != AuSuccess)
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
174 nas_print_error(nas_data->aud, "nas_readBuffer(): AuWriteElement", as);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
175
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
176 if (nas_data->flow_paused) {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
177 AuPauseFlow(nas_data->aud, nas_data->flow, &as);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
178 if (as != AuSuccess)
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
179 nas_print_error(nas_data->aud, "nas_readBuffer(): AuPauseFlow", as);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
180 }
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
181
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
182 return num;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
183 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
184
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
185 static void nas_writeBuffer(struct ao_nas_data *nas_data, void *data, int len)
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
186 {
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
187 pthread_mutex_lock(&nas_data->buffer_mutex);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
188 DPRINTF("ao_nas: nas_writeBuffer(): len=%d client=%d/%d server=%d/%d\n",
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
189 len, nas_data->client_buffer_used, nas_data->client_buffer_size,
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
190 nas_data->server_buffer_used, nas_data->server_buffer_size);
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
191
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
192 memcpy(nas_data->client_buffer + nas_data->client_buffer_used, data, len);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
193 nas_data->client_buffer_used += len;
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
194
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
195 pthread_mutex_unlock(&nas_data->buffer_mutex);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
196 if (nas_data->server_buffer_used < nas_data->server_buffer_size)
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
197 nas_readBuffer(nas_data, nas_data->server_buffer_size - nas_data->server_buffer_used);
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
198 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
199
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
200 static int nas_empty_event_queue(struct ao_nas_data *nas_data)
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
201 {
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
202 AuEvent ev;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
203 int result = 0;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
204
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
205 while (AuScanForTypedEvent(nas_data->aud, AuEventsQueuedAfterFlush,
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
206 AuTrue, AuEventTypeElementNotify, &ev)) {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
207 AuDispatchEvent(nas_data->aud, &ev);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
208 result = 1;
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
209 }
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
210 return result;
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
211 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
212
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
213 static void *nas_event_thread_start(void *data)
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
214 {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
215 struct ao_nas_data *nas_data = data;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
216 AuEvent ev;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
217 AuBool result;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
218
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
219 do {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
220 nas_empty_event_queue(nas_data);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
221 usleep(10000);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
222 } while (!nas_data->stop_thread);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
223 }
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
224
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
225 static AuBool nas_error_handler(AuServer* aud, AuErrorEvent* ev)
3276
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 char s[100];
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
228 AuGetErrorText(aud, ev->error_code, s, 100);
3336
8818c12743a8 patch by Tobias Diedrich <td@informatik.uni-hannover.de>
pl
parents: 3276
diff changeset
229 fprintf(stderr,"ao_nas: error [%s]\n"
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
230 "error_code: %d\n"
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
231 "request_code: %d\n"
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
232 "minor_code: %d\n",
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
233 s,
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
234 ev->error_code,
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
235 ev->request_code,
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
236 ev->minor_code);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
237 fflush(stderr);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
238
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
239 return AuTrue;
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
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
242 static AuBool nas_event_handler(AuServer *aud, AuEvent *ev, AuEventHandlerRec *hnd)
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
243 {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
244 AuElementNotifyEvent *event = (AuElementNotifyEvent *) ev;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
245 AuStatus as;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
246 struct ao_nas_data *nas_data = hnd->data;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
247
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
248 switch (ev->type) {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
249 case AuEventTypeElementNotify:
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
250 DPRINTF("ao_nas: event_handler(): kind %s state %s->%s reason %s numbytes %d\n",
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
251 nas_elementnotify_kind(event->kind),
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
252 nas_state(event->prev_state),
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
253 nas_state(event->cur_state),
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
254 nas_reason(event->reason),
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
255 event->num_bytes);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
256
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
257 nas_data->server_buffer_used -= event->num_bytes;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
258 if (nas_data->server_buffer_used < 0)
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
259 nas_data->server_buffer_used = 0;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
260
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
261 switch (event->kind) {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
262 case AuElementNotifyKindLowWater:
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
263 nas_readBuffer(nas_data, event->num_bytes);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
264 break;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
265 case AuElementNotifyKindState:
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
266 if (event->cur_state == AuStatePause) {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
267 switch (event->reason) {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
268 case AuReasonUnderrun:
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
269 // buffer underrun -> refill buffer
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
270 nas_data->server_buffer_used = 0;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
271 nas_readBuffer(nas_data, nas_data->server_buffer_size - nas_data->server_buffer_used);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
272 break;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
273 default:
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
274 break;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
275 }
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
276 }
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
277 break;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
278 default: // silently ignored
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
279 break;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
280 }
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
281 break;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
282 default:
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
283 printf("ao_nas: nas_event_handler(): unhandled event type %d\n", ev->type);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
284 break;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
285 }
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
286 return AuTrue;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
287 }
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
288
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
289 static AuDeviceID nas_find_device(AuServer *aud, int nch)
3276
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 int i;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
292 for (i = 0; i < AuServerNumDevices(aud); i++) {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
293 AuDeviceAttributes *dev = AuServerDevice(aud, i);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
294 if ((AuDeviceKind(dev) == AuComponentKindPhysicalOutput) &&
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
295 AuDeviceNumTracks(dev) == nch) {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
296 return AuDeviceIdentifier(dev);
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
297 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
298 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
299 return AuNone;
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
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
302 static unsigned char nas_aformat_to_auformat(unsigned int format)
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
303 {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
304 switch (format) {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
305 case AFMT_U8: return AuFormatLinearUnsigned8;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
306 case AFMT_S8: return AuFormatLinearSigned8;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
307 case AFMT_U16_LE: return AuFormatLinearUnsigned16LSB;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
308 case AFMT_U16_BE: return AuFormatLinearUnsigned16MSB;
5790
d141f1e9cc36 AFMT_AC3 autodetect/fallback to S16
arpi
parents: 4775
diff changeset
309 #ifndef WORDS_BIGENDIAN
d141f1e9cc36 AFMT_AC3 autodetect/fallback to S16
arpi
parents: 4775
diff changeset
310 case AFMT_AC3:
d141f1e9cc36 AFMT_AC3 autodetect/fallback to S16
arpi
parents: 4775
diff changeset
311 #endif
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
312 case AFMT_S16_LE: return AuFormatLinearSigned16LSB;
5790
d141f1e9cc36 AFMT_AC3 autodetect/fallback to S16
arpi
parents: 4775
diff changeset
313 #ifdef WORDS_BIGENDIAN
d141f1e9cc36 AFMT_AC3 autodetect/fallback to S16
arpi
parents: 4775
diff changeset
314 case AFMT_AC3:
d141f1e9cc36 AFMT_AC3 autodetect/fallback to S16
arpi
parents: 4775
diff changeset
315 #endif
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
316 case AFMT_S16_BE: return AuFormatLinearSigned16MSB;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
317 case AFMT_MU_LAW: return AuFormatULAW8;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
318 default: return 0;
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 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
321
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
322 // to set/get/query special features/parameters
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
323 static int control(int cmd,int arg){
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
324 return -1;
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 // open & setup audio device
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
328 // return: 1=success 0=fail
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
329 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
330 {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
331 AuElement elms[3];
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
332 AuStatus as;
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
333 unsigned char auformat = nas_aformat_to_auformat(format);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
334 int bytes_per_sample = channels * AuSizeofFormat(auformat);
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
335 char *server;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
336
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
337 nas_data=malloc(sizeof(struct ao_nas_data));
6114
34d5c9a67b94 Patch by Tobias Diedrich <td@informatik.uni-hannover.de>:
pl
parents: 5790
diff changeset
338 memset(nas_data, 0, sizeof(struct ao_nas_data));
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
339
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
340 printf("ao2: %d Hz %d chans %s\n",rate,channels,
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
341 audio_out_format_name(format));
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
342
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
343 if (!auformat) {
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
344 printf("ao_nas: init(): Unsupported format -> nosound\n");
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
345 return 0;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
346 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
347
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
348 nas_data->client_buffer_size = NAS_BUFFER_SIZE;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
349 nas_data->client_buffer = malloc(nas_data->client_buffer_size);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
350 nas_data->server_buffer_size = NAS_BUFFER_SIZE;
7449
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
351 nas_data->server_buffer = malloc(nas_data->server_buffer_size);
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
352
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
353 ao_data.samplerate = rate;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
354 ao_data.channels = channels;
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
355 ao_data.buffersize = NAS_BUFFER_SIZE * 2;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
356 ao_data.outburst = NAS_FRAG_SIZE;
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
357 ao_data.bps = rate * bytes_per_sample;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
358
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
359 if (!bytes_per_sample) {
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
360 printf("ao_nas: init(): Zero bytes per sample -> nosound\n");
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
361 return 0;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
362 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
363
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
364 if (!(server = getenv("AUDIOSERVER")))
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
365 server = getenv("DISPLAY");
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
366
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
367 if (!server) // default to tcp/localhost:8000
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
368 server = "tcp/localhost:8000";
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
369
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
370 printf("ao_nas: init(): Using audioserver %s\n", server);
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
371
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
372 nas_data->aud = AuOpenServer(server, 0, NULL, 0, NULL, NULL);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
373 if (!nas_data->aud){
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
374 printf("ao_nas: init(): Can't open nas audio server -> nosound\n");
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
375 return 0;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
376 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
377
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
378 nas_data->dev = nas_find_device(nas_data->aud, channels);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
379 if ((nas_data->dev == AuNone) || (!(nas_data->flow = AuCreateFlow(nas_data->aud, NULL)))) {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
380 printf("ao_nas: init(): Can't find a device serving that many channels -> nosound\n");
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
381 AuCloseServer(nas_data->aud);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
382 nas_data->aud = 0;
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
383 return 0;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
384 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
385
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
386 AuMakeElementImportClient(elms, rate, auformat, channels, AuTrue,
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
387 NAS_BUFFER_SIZE / bytes_per_sample,
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
388 (NAS_BUFFER_SIZE - NAS_FRAG_SIZE) / bytes_per_sample,
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
389 0, NULL);
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
390 AuMakeElementExportDevice(elms+1, 0, nas_data->dev, rate,
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
391 AuUnlimitedSamples, 0, NULL);
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
392 AuSetElements(nas_data->aud, nas_data->flow, AuTrue, 2, elms, &as);
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
393 if (as != AuSuccess)
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
394 nas_print_error(nas_data->aud, "init(): AuSetElements", as);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
395 AuRegisterEventHandler(nas_data->aud, AuEventHandlerIDMask |
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
396 AuEventHandlerTypeMask,
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
397 AuEventTypeElementNotify, nas_data->flow,
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
398 nas_event_handler, (AuPointer) nas_data);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
399 AuSetErrorHandler(nas_data->aud, nas_error_handler);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
400 nas_data->flow_stopped=1;
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
401
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
402 pthread_mutex_init(&nas_data->buffer_mutex, NULL);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
403 pthread_create(&nas_data->event_thread, NULL, &nas_event_thread_start, nas_data);
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
404
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
405 return 1;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
406 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
407
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
408 // close audio device
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
409 static void uninit(){
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
410 AuStatus as;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
411
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
412 nas_data->stop_thread = 1;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
413 pthread_join(nas_data->event_thread, NULL);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
414 if (!nas_data->flow_stopped) {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
415 AuStopFlow(nas_data->aud, nas_data->flow, &as);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
416 if (as != AuSuccess)
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
417 nas_print_error(nas_data->aud, "uninit(): AuStopFlow", as);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
418 }
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
419 AuCloseServer(nas_data->aud);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
420 nas_data->aud = 0;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
421 free(nas_data->client_buffer);
7449
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
422 free(nas_data->server_buffer);
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
423 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
424
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
425 // stop playing and empty buffers (for seeking/pause)
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
426 static void reset(){
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
427 AuStatus as;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
428
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
429 pthread_mutex_lock(&nas_data->buffer_mutex);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
430 nas_data->client_buffer_used = 0;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
431 if (!nas_data->flow_stopped) {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
432 AuStopFlow(nas_data->aud, nas_data->flow, &as);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
433 if (as != AuSuccess)
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
434 nas_print_error(nas_data->aud, "reset(): AuStopFlow", as);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
435 nas_data->flow_stopped = 1;
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
436 }
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
437 nas_data->server_buffer_used = 0;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
438 pthread_mutex_unlock(&nas_data->buffer_mutex);
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
439 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
440
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
441 // stop playing, keep buffers (for pause)
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
442 static void audio_pause()
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
443 {
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
444 AuStatus as;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
445
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
446 DPRINTF("ao_nas: audio_pause()\n");
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
447
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
448 nas_data->flow_paused = 1;
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
449 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
450
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
451 // resume playing, after audio_pause()
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
452 static void audio_resume()
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
453 {
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
454 AuStatus as;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
455 AuEvent ev;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
456
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
457 DPRINTF("ao_nas: audio_resume()\n");
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
458
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
459 nas_data->flow_stopped = 0;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
460 nas_data->flow_paused = 0;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
461 AuStartFlow(nas_data->aud, nas_data->flow, &as);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
462 if (as != AuSuccess)
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
463 nas_print_error(nas_data->aud, "play(): AuStartFlow", as);
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
464 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
465
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
466
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
467 // return: how many bytes can be played without blocking
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
468 static int get_space()
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
469 {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
470 int result;
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
471
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
472 DPRINTF("ao_nas: get_space()\n");
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
473
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
474 pthread_mutex_lock(&nas_data->buffer_mutex);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
475 result = nas_data->client_buffer_size - nas_data->client_buffer_used;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
476 pthread_mutex_unlock(&nas_data->buffer_mutex);
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
477
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
478 return result;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
479 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
480
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
481 // plays 'len' bytes of 'data'
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
482 // it should round it down to outburst*n
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
483 // return: number of bytes played
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
484 static int play(void* data,int len,int flags)
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
485 {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
486 int maxbursts, playbursts, writelen;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
487 AuStatus as;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
488
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
489 DPRINTF("ao_nas: play()\n");
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
490
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
491 if (nas_data->flow_stopped) {
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
492 AuEvent ev;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
493
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
494 AuStartFlow(nas_data->aud, nas_data->flow, &as);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
495 if (as != AuSuccess)
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
496 nas_print_error(nas_data->aud, "play(): AuStartFlow", as);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
497 nas_data->flow_stopped = 0;
7449
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
498 /*
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
499 * Do not continue to wait if there is nothing in the server
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
500 * buffer. (The event never happens and mplayer can freeze.)
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
501 */
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
502 while (nas_data->server_buffer_used > 0 &&
28785e6e6900 "One can cause a permanent hang on a seek, and the other just causes
arpi
parents: 6114
diff changeset
503 !nas_empty_event_queue(nas_data)); // wait for first buffer underrun event
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
504 }
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
505
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
506 pthread_mutex_lock(&nas_data->buffer_mutex);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
507 maxbursts = (nas_data->client_buffer_size -
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
508 nas_data->client_buffer_used) / ao_data.outburst;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
509 playbursts = len / ao_data.outburst;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
510 writelen = (playbursts > maxbursts ? maxbursts : playbursts) *
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
511 ao_data.outburst;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
512 pthread_mutex_unlock(&nas_data->buffer_mutex);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
513
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
514 nas_writeBuffer(nas_data, data, writelen);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
515 return writelen;
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
516 }
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
517
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
518 // 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
519 static float get_delay()
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
520 {
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
521 float result;
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
522
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
523 DPRINTF("ao_nas: get_delay()\n");
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
524
4775
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
525 pthread_mutex_lock(&nas_data->buffer_mutex);
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
526 result = ((float)(nas_data->client_buffer_used +
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
527 nas_data->server_buffer_used)) /
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
528 (float)ao_data.bps;
080cf3df3e4e improved event handling, implemented working pause that does not flush all buffers, work around a deadlock in the new threadsafe version 1.5 of libaudio by Tobias Diedrich
atmos4
parents: 3336
diff changeset
529 pthread_mutex_unlock(&nas_data->buffer_mutex);
3276
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
530
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
531 return result;
e279cc05f189 -ao NAS support by Tobias Diedrich <ranma@gmx.at>
arpi
parents:
diff changeset
532 }