Merge pull request #123 from gameblabla/diablofix_hack
[pcsx_rearmed.git] / plugins / dfsound / pulseaudio.c
index e5ffb59..8ffd58f 100644 (file)
@@ -17,14 +17,10 @@ comment              : Much of this was taken from simple.c, in the pulseaudio
  *                                                                         *
  ***************************************************************************/
 
-#include "stdafx.h"
+#include <stdio.h>
 
-#ifdef USEPULSEAUDIO
-
-#define _IN_OSS
-
-#include "externals.h"
 #include <pulse/pulseaudio.h>
+#include "out.h"
 
 ////////////////////////////////////////////////////////////////////////
 // pulseaudio structs
@@ -136,7 +132,7 @@ static void stream_request_cb (pa_stream *stream, size_t length, void *userdata)
 // SETUP SOUND
 ////////////////////////////////////////////////////////////////////////
 
-void SetupSound (void)
+static int pulse_init(void)
 {
      int error_number;
 
@@ -145,7 +141,7 @@ void SetupSound (void)
      if (device.mainloop == NULL)
      {
          fprintf (stderr, "Could not acquire PulseAudio main loop\n");
-         return;
+         return -1;
      }
 
      // Acquire context ////////////////////////////////////////////////////////
@@ -156,7 +152,7 @@ void SetupSound (void)
      if (device.context == NULL)
      {
          fprintf (stderr, "Could not acquire PulseAudio device context\n");
-         return;
+         return -1;
      }
 
      // Connect to PulseAudio server ///////////////////////////////////////////
@@ -164,7 +160,7 @@ void SetupSound (void)
      {
          error_number = pa_context_errno (device.context);
          fprintf (stderr, "Could not connect to PulseAudio server: %s\n", pa_strerror(error_number));
-         return;
+         return -1;
      }
 
      // Run mainloop until sever context is ready //////////////////////////////
@@ -172,7 +168,7 @@ void SetupSound (void)
      if (pa_threaded_mainloop_start (device.mainloop) < 0)
      {
          fprintf (stderr, "Could not start mainloop\n");
-         return;
+         return -1;
      }
 
      pa_context_state_t context_state;
@@ -184,7 +180,7 @@ void SetupSound (void)
          {
               error_number = pa_context_errno (device.context);
               fprintf (stderr, "Context state is not good: %s\n", pa_strerror (error_number));
-              return;
+              return -1;
          }
          else if (context_state == PA_CONTEXT_READY)
               break;
@@ -216,7 +212,7 @@ void SetupSound (void)
      {
          error_number = pa_context_errno (device.context);
          fprintf (stderr, "Could not acquire new PulseAudio stream: %s\n", pa_strerror (error_number));
-         return;
+         return -1;
      }
 
      // Set callbacks for server events ////////////////////////////////////////
@@ -229,9 +225,9 @@ void SetupSound (void)
      //pa_stream_flags_t flags = (pa_stream_flags_t) (PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE | PA_STREAM_EARLY_REQUESTS);
      if (pa_stream_connect_playback (device.stream, NULL, &buffer_attributes, flags, NULL, NULL) < 0)
      {
-         pa_context_errno (device.context);
+         error_number = pa_context_errno (device.context);
          fprintf (stderr, "Could not connect for playback: %s\n", pa_strerror (error_number));
-         return;
+         return -1;
      }
 
      // Run mainloop until stream is ready /////////////////////////////////////
@@ -248,7 +244,7 @@ void SetupSound (void)
          {
               error_number = pa_context_errno (device.context);
               fprintf (stderr, "Stream state is not good: %s\n", pa_strerror (error_number));
-              return;
+              return -1;
          }
          else
               fprintf (stderr, "PulseAudio stream state is %d\n", stream_state);
@@ -258,13 +254,13 @@ void SetupSound (void)
      pa_threaded_mainloop_unlock (device.mainloop);
 
      fprintf  (stderr, "PulseAudio should be connected\n");
-     return;
+     return 0;
 }
 
 ////////////////////////////////////////////////////////////////////////
 // REMOVE SOUND
 ////////////////////////////////////////////////////////////////////////
-void RemoveSound (void)
+static void pulse_finish(void)
 {
      if (device.mainloop != NULL)
          pa_threaded_mainloop_stop (device.mainloop);
@@ -295,15 +291,12 @@ void RemoveSound (void)
 // GET BYTES BUFFERED
 ////////////////////////////////////////////////////////////////////////
 
-unsigned long SoundGetBytesBuffered (void)
+static int pulse_busy(void)
 {
      int free_space;
-     int error_code;
-     long latency;
-     int playing = 0;
 
      if ((device.mainloop == NULL) || (device.api == NULL) || ( device.context == NULL) || (device.stream == NULL))
-         return SOUNDSIZE;
+         return 1;
 
      pa_threaded_mainloop_lock (device.mainloop);
      free_space = pa_stream_writable_size (device.stream);
@@ -315,7 +308,7 @@ unsigned long SoundGetBytesBuffered (void)
      {
          // Don't buffer anymore, just play
          //fprintf (stderr, "Not buffering.\n");
-         return SOUNDSIZE;
+         return 1;
      }
      else 
      {
@@ -329,11 +322,8 @@ unsigned long SoundGetBytesBuffered (void)
 // FEED SOUND DATA
 ////////////////////////////////////////////////////////////////////////
 
-void SoundFeedStreamData (unsigned char *pSound, long lBytes)
+static void pulse_feed(void *pSound, int lBytes)
 {
-     int error_code;
-     int size;
-
      if (device.mainloop != NULL)
      {
          pa_threaded_mainloop_lock (device.mainloop);
@@ -348,4 +338,12 @@ void SoundFeedStreamData (unsigned char *pSound, long lBytes)
          }
      }
 }
-#endif
+
+void out_register_pulse(struct out_driver *drv)
+{
+       drv->name = "pulseaudio";
+       drv->init = pulse_init;
+       drv->finish = pulse_finish;
+       drv->busy = pulse_busy;
+       drv->feed = pulse_feed;
+}