1 /***************************************************************************
2 pulseaudio.c - description
4 begin : Thu Feb 04 2010
5 copyright : (C) 2010 by Tristin Celestin
6 email : cetris1@umbc.edu
7 comment : Much of this was taken from simple.c, in the pulseaudio
9 ***************************************************************************/
10 /***************************************************************************
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. See also the license.txt file for *
16 * additional informations. *
18 ***************************************************************************/
26 #include "externals.h"
27 #include <pulse/pulseaudio.h>
29 ////////////////////////////////////////////////////////////////////////
31 ////////////////////////////////////////////////////////////////////////
34 pa_threaded_mainloop *mainloop;
43 unsigned int frequency;
44 unsigned int latency_in_msec;
47 ////////////////////////////////////////////////////////////////////////
49 ////////////////////////////////////////////////////////////////////////
51 static Device device = {
58 static Settings settings = {
60 .latency_in_msec = 20,
63 // the number of bytes written in SoundFeedStreamData
64 const int mixlen = 3240;
66 // used to calculate how much space is used in the buffer, for debugging purposes
69 ////////////////////////////////////////////////////////////////////////
70 // CALLBACKS FOR THREADED MAINLOOP
71 ////////////////////////////////////////////////////////////////////////
72 static void context_state_cb (pa_context *context, void *userdata)
74 Device *dev = userdata;
76 if ((context == NULL) || (dev == NULL))
79 switch (pa_context_get_state (context))
81 case PA_CONTEXT_READY:
82 case PA_CONTEXT_TERMINATED:
83 case PA_CONTEXT_FAILED:
84 pa_threaded_mainloop_signal (dev->mainloop, 0);
87 case PA_CONTEXT_UNCONNECTED:
88 case PA_CONTEXT_CONNECTING:
89 case PA_CONTEXT_AUTHORIZING:
90 case PA_CONTEXT_SETTING_NAME:
95 static void stream_state_cb (pa_stream *stream, void * userdata)
97 Device *dev = userdata;
99 if ((stream == NULL) || (dev == NULL))
102 switch (pa_stream_get_state (stream))
104 case PA_STREAM_READY:
105 case PA_STREAM_FAILED:
106 case PA_STREAM_TERMINATED:
107 pa_threaded_mainloop_signal (dev->mainloop, 0);
110 case PA_STREAM_UNCONNECTED:
111 case PA_STREAM_CREATING:
116 static void stream_latency_update_cb (pa_stream *stream, void *userdata)
118 Device *dev = userdata;
120 if ((stream == NULL) || (dev == NULL))
123 pa_threaded_mainloop_signal (dev->mainloop, 0);
126 static void stream_request_cb (pa_stream *stream, size_t length, void *userdata)
128 Device *dev = userdata;
130 if ((stream == NULL) || (dev == NULL))
132 pa_threaded_mainloop_signal (dev->mainloop, 0);
135 ////////////////////////////////////////////////////////////////////////
137 ////////////////////////////////////////////////////////////////////////
139 void SetupSound (void)
143 // Acquire mainloop ///////////////////////////////////////////////////////
144 device.mainloop = pa_threaded_mainloop_new ();
145 if (device.mainloop == NULL)
147 fprintf (stderr, "Could not acquire PulseAudio main loop\n");
151 // Acquire context ////////////////////////////////////////////////////////
152 device.api = pa_threaded_mainloop_get_api (device.mainloop);
153 device.context = pa_context_new (device.api, "PCSX");
154 pa_context_set_state_callback (device.context, context_state_cb, &device);
156 if (device.context == NULL)
158 fprintf (stderr, "Could not acquire PulseAudio device context\n");
162 // Connect to PulseAudio server ///////////////////////////////////////////
163 if (pa_context_connect (device.context, NULL, 0, NULL) < 0)
165 error_number = pa_context_errno (device.context);
166 fprintf (stderr, "Could not connect to PulseAudio server: %s\n", pa_strerror(error_number));
170 // Run mainloop until sever context is ready //////////////////////////////
171 pa_threaded_mainloop_lock (device.mainloop);
172 if (pa_threaded_mainloop_start (device.mainloop) < 0)
174 fprintf (stderr, "Could not start mainloop\n");
178 pa_context_state_t context_state;
179 context_state = pa_context_get_state (device.context);
180 while (context_state != PA_CONTEXT_READY)
182 context_state = pa_context_get_state (device.context);
183 if (! PA_CONTEXT_IS_GOOD (context_state))
185 error_number = pa_context_errno (device.context);
186 fprintf (stderr, "Context state is not good: %s\n", pa_strerror (error_number));
189 else if (context_state == PA_CONTEXT_READY)
192 fprintf (stderr, "PulseAudio context state is %d\n", context_state);
193 pa_threaded_mainloop_wait (device.mainloop);
196 // Set sample spec ////////////////////////////////////////////////////////
197 device.spec.format = PA_SAMPLE_S16NE;
198 device.spec.channels = 2;
199 device.spec.rate = settings.frequency;
201 pa_buffer_attr buffer_attributes;
202 buffer_attributes.tlength = pa_bytes_per_second (& device.spec) / 5;
203 buffer_attributes.maxlength = buffer_attributes.tlength * 3;
204 buffer_attributes.minreq = buffer_attributes.tlength / 3;
205 buffer_attributes.prebuf = buffer_attributes.tlength;
207 //maxlength = buffer_attributes.maxlength;
208 //fprintf (stderr, "Total space: %u\n", buffer_attributes.maxlength);
209 //fprintf (stderr, "Minimum request size: %u\n", buffer_attributes.minreq);
210 //fprintf (stderr, "Bytes needed before playback: %u\n", buffer_attributes.prebuf);
211 //fprintf (stderr, "Target buffer size: %lu\n", buffer_attributes.tlength);
213 // Acquire new stream using spec //////////////////////////////////////////
214 device.stream = pa_stream_new (device.context, "PCSX", &device.spec, NULL);
215 if (device.stream == NULL)
217 error_number = pa_context_errno (device.context);
218 fprintf (stderr, "Could not acquire new PulseAudio stream: %s\n", pa_strerror (error_number));
222 // Set callbacks for server events ////////////////////////////////////////
223 pa_stream_set_state_callback (device.stream, stream_state_cb, &device);
224 pa_stream_set_write_callback (device.stream, stream_request_cb, &device);
225 pa_stream_set_latency_update_callback (device.stream, stream_latency_update_cb, &device);
227 // Ready stream for playback //////////////////////////////////////////////
228 pa_stream_flags_t flags = (pa_stream_flags_t) (PA_STREAM_ADJUST_LATENCY | PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE);
229 //pa_stream_flags_t flags = (pa_stream_flags_t) (PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE | PA_STREAM_EARLY_REQUESTS);
230 if (pa_stream_connect_playback (device.stream, NULL, &buffer_attributes, flags, NULL, NULL) < 0)
232 pa_context_errno (device.context);
233 fprintf (stderr, "Could not connect for playback: %s\n", pa_strerror (error_number));
237 // Run mainloop until stream is ready /////////////////////////////////////
238 pa_stream_state_t stream_state;
239 stream_state = pa_stream_get_state (device.stream);
240 while (stream_state != PA_STREAM_READY)
242 stream_state = pa_stream_get_state (device.stream);
244 if (stream_state == PA_STREAM_READY)
247 else if (! PA_STREAM_IS_GOOD (stream_state))
249 error_number = pa_context_errno (device.context);
250 fprintf (stderr, "Stream state is not good: %s\n", pa_strerror (error_number));
254 fprintf (stderr, "PulseAudio stream state is %d\n", stream_state);
255 pa_threaded_mainloop_wait (device.mainloop);
258 pa_threaded_mainloop_unlock (device.mainloop);
260 fprintf (stderr, "PulseAudio should be connected\n");
264 ////////////////////////////////////////////////////////////////////////
266 ////////////////////////////////////////////////////////////////////////
267 void RemoveSound (void)
269 if (device.mainloop != NULL)
270 pa_threaded_mainloop_stop (device.mainloop);
272 // Release in reverse order of acquisition
273 if (device.stream != NULL)
275 pa_stream_unref (device.stream);
276 device.stream = NULL;
279 if (device.context != NULL)
281 pa_context_disconnect (device.context);
282 pa_context_unref (device.context);
283 device.context = NULL;
286 if (device.mainloop != NULL)
288 pa_threaded_mainloop_free (device.mainloop);
289 device.mainloop = NULL;
294 ////////////////////////////////////////////////////////////////////////
295 // GET BYTES BUFFERED
296 ////////////////////////////////////////////////////////////////////////
298 unsigned long SoundGetBytesBuffered (void)
305 if ((device.mainloop == NULL) || (device.api == NULL) || ( device.context == NULL) || (device.stream == NULL))
308 pa_threaded_mainloop_lock (device.mainloop);
309 free_space = pa_stream_writable_size (device.stream);
310 pa_threaded_mainloop_unlock (device.mainloop);
312 //fprintf (stderr, "Free space: %d\n", free_space);
313 //fprintf (stderr, "Used space: %d\n", maxlength - free_space);
314 if (free_space < mixlen * 3)
316 // Don't buffer anymore, just play
317 //fprintf (stderr, "Not buffering.\n");
323 //fprintf (stderr, "Buffering.\n");
328 ////////////////////////////////////////////////////////////////////////
330 ////////////////////////////////////////////////////////////////////////
332 void SoundFeedStreamData (unsigned char *pSound, long lBytes)
337 if (device.mainloop != NULL)
339 pa_threaded_mainloop_lock (device.mainloop);
340 if (pa_stream_write (device.stream, pSound, lBytes, NULL, 0LL, PA_SEEK_RELATIVE) < 0)
342 fprintf (stderr, "Could not perform write\n");
346 //fprintf (stderr, "Wrote %d bytes\n", lBytes);
347 pa_threaded_mainloop_unlock (device.mainloop);