Debian Codegraph @

src/libout123/modules/alib.c

raw
1 /*
2 alib: audio output for HP-UX using alib
3
4 copyright ?-2006 by the mpg123 project - free software under the terms of the LGPL 2.1
5 see COPYING and AUTHORS files in distribution or http://mpg123.org
6 initially written by Erwan Ducroquet
7 based on source code from HP (Audio SDK)
8 */
9
10 /*
11 *
12 * for mpg123 :
13 * hpux:
14 * $(MAKE) \
15 * CC=cc \
16 * LDFLAGS=-L/opt/audio/lib \
17 * AUDIO_LIB=-lAlib \
18 * OBJECTS=decode.o INT123_dct64.o \
19 * CFLAGS=-Ae +O3 -DREAL_IS_FLOAT -D_HPUX_SOURCE -DHPUX -I/opt/audio/include \
20 * mpg123
21 */
22
23
24 /*
25 * For the user :
26 * If you launch mpg123 on a XTerm with sound capabilities, it's OK
27 * Else, you have to set the environment variable "AUDIO" to the name of
28 * an HP Xterm with sound card.
29 */
30
31 /**************************************************************************/
32
33 #include "../out123_int.h"
34
35 #include <fcntl.h>
36
37 #include <sys/socket.h>
38 #include <netdb.h>
39 #include <netinet/in.h>
40 #include <netinet/tcp.h>
41 #include <errno.h>
42
43
44 #include <Alib.h> /* /opt/audio/include */
45 #include <CUlib.h> /* /opt/audio/include */
46
47 #include "../../common/debug.h"
48
49 /**************************************************************************/
50
51
52 /* FIXME: These globals should be moved into a structure */
53 static Audio *audioServer = (Audio *) NULL;
54 static struct protoent *tcpProtocolEntry;
55 static ATransID xid;
56
57 static void printAudioError(Audio *audio,char *message,int errorCode) {
58 char errorbuff[132];
59 AGetErrorText(audio, errorCode, errorbuff, 131);
60 error2("%s: %s", message, errorbuff);
61 }
62 static long myHandler(Audio *audio,AErrorEvent *err_event) {
63 printAudioError( audio, "Audio error", err_event->error_code );
64 /* we cannot just do random exists, that messes terminal up
65 need proper error propagation in that case for future, setting intflag or such */
66 /* exit(1); */
67 }
68
69 /**************************************************************************/
70
71 /*
72 * Set the fn element of ai
73 * Use ao->rate and ao->channels
74 * Doesn't set any volume
75 */
76
77 /* return on error leaves stuff dirty here... */
78 static int open_alib(out123_handle *ao)
79 {
80 AudioAttributes Attribs;
81 AudioAttrMask AttribsMask;
82 AGainEntry gainEntry[4];
83 SSPlayParams playParams;
84 SStream audioStream;
85 AErrorHandler prevHandler;
86 char server[1];
87 int i;
88 long status;
89
90 if (audioServer) {
91 error("openAudio: audio already open");
92 return -1;
93 }
94
95 prevHandler = ASetErrorHandler(myHandler);
96
97 server[0] = '\0';
98 audioServer = AOpenAudio( server, NULL );
99 if (audioServer==NULL) {
100 error("Error: could not open audio\n");
101 return -1;
102 }
103
104 ao->fn = socket( AF_INET, SOCK_STREAM, 0 );
105 if(ao->fn<0) {
106 error("Socket creation failed");
107 return -1;
108 }
109
110 Attribs.type = ATSampled;
111 Attribs.attr.sampled_attr.sampling_rate = ao->rate;
112 Attribs.attr.sampled_attr.channels = ao->channels;
113 Attribs.attr.sampled_attr.data_format = ADFLin16;
114 AttribsMask = ASSamplingRateMask | ASChannelsMask | ASDataFormatMask;
115
116 gainEntry[0].gain = AUnityGain;
117 gainEntry[0].u.o.out_ch = AOCTMono;
118 gainEntry[0].u.o.out_dst = AODTDefaultOutput;
119
120 playParams.gain_matrix.type = AGMTOutput; /* gain matrix */
121 playParams.gain_matrix.num_entries = 1;
122 playParams.gain_matrix.gain_entries = gainEntry;
123 playParams.play_volume = AUnityGain; /* play volume */
124 playParams.priority = APriorityNormal; /* normal priority */
125 playParams.event_mask = 0; /* don't solicit any events */
126
127 xid=APlaySStream(audioServer,AttribsMask,&Attribs,
128 &playParams,&audioStream,NULL);
129
130 status=connect(ao->fn,
131 (struct sockaddr *) &audioStream.tcp_sockaddr,
132 sizeof(struct sockaddr_in) );
133 if (status<0) {
134 error("Connect failed");
135 return -1;
136 }
137
138 i=-1;
139 tcpProtocolEntry=getprotobyname("tcp");
140 setsockopt(ao->fn,tcpProtocolEntry->p_proto,TCP_NODELAY,&i,sizeof(i));
141
142 return ao->fn;
143 }
144
145 /**************************************************************************/
146
147 static int close_alib(out123_handle *ao)
148 {
149 close(ao->fn);
150 ASetCloseDownMode( audioServer, AKeepTransactions, NULL );
151 ACloseAudio( audioServer, NULL );
152 audioServer = (Audio *) NULL;
153 return 0;
154 }
155
156 /**************************************************************************/
157
158 /*
159 * very simple
160 * deserv to be inline
161 */
162
163 static int write_alib(out123_handle *ao,unsigned char *buf,int len)
164 {
165 return write(ao->fn,buf,len*2);
166 }
167
168 /**************************************************************************/
169
170 static int get_formats_alib(out123_handle *ao)
171 {
172 return MPG123_ENC_SIGNED_16;
173 }
174
175 static void flush_alib(out123_handle *ao)
176 {
177 }
178
179
180 static int init_alib(out123_handle* ao)
181 {
182 if (ao==NULL) return -1;
183
184 /* Set callbacks */
185 ao->open = open_alib;
186 ao->flush = flush_alib;
187 ao->write = write_alib;
188 ao->get_formats = get_formats_alib;
189 ao->close = close_alib;
190
191 /* Success */
192 return 0;
193 }
194
195
196
197 /*
198 Module information data structure
199 */
200 mpg123_module_t mpg123_output_module_info = {
201 /* api_version */ MPG123_MODULE_API_VERSION,
202 /* name */ "alib",
203 /* description */ "Output audio HP-UX using alib.",
204 /* revision */ "$Rev:$",
205 /* handle */ NULL,
206
207 /* init_output */ init_alib,
208 };
209