]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/pulse.c
SDL: simplification of detection and requirement of 1.2.10
[vlc] / modules / audio_output / pulse.c
index d1e5d505e1790e683733101a31a9a7afa3e38944..d8670175a9716217bdac5144156973aea49917f0 100644 (file)
@@ -76,6 +76,7 @@ if (!p_sys->context || pa_context_get_state(p_sys->context) != PA_CONTEXT_READY
         goto label; \
     }  \
 } while(0);
+
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
@@ -89,6 +90,7 @@ static void stream_request_cb(pa_stream *s, size_t length, void *userdata);
 static void stream_latency_update_cb(pa_stream *s, void *userdata);
 static void success_cb(pa_stream *s, int sucess, void *userdata);
 static void uninit(aout_instance_t *p_aout);
+
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
@@ -101,6 +103,7 @@ vlc_module_begin ()
     add_shortcut( "pulseaudio" )
     add_shortcut( "pa" )
     set_callbacks( Open, Close )
+    linked_with_a_crap_library_which_uses_atexit()
 vlc_module_end ()
 
 /*****************************************************************************
@@ -116,40 +119,76 @@ static int Open ( vlc_object_t *p_this )
     struct pa_channel_map map;
 
     /* Allocate structures */
-    p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
+    p_aout->output.p_sys = p_sys = calloc( 1, sizeof( aout_sys_t ) );
     if( p_sys == NULL )
         return VLC_ENOMEM;
-    memset( p_sys, 0, sizeof( aout_sys_t ) );
 
     PULSE_DEBUG( "Pulse start initialization");
 
-    ss.rate = p_aout->output.output.i_rate;
-    ss.channels = 2;
+    ss.channels = aout_FormatNbChannels( &p_aout->output.output ); /* Get the input stream channel count */
+
+    /* Setup the pulse audio stream based on the input stream count */
+    switch(ss.channels)
+    {
+        case 8:
+            p_aout->output.output.i_physical_channels
+                = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
+                | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
+                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
+                | AOUT_CHAN_LFE;
+            break;
+        case 6:
+            p_aout->output.output.i_physical_channels
+                = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
+                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
+                | AOUT_CHAN_LFE;
+            break;
+
+        case 4:
+            p_aout->output.output.i_physical_channels
+                = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
+                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
+            break;
+
+        case 2:
+            p_aout->output.output.i_physical_channels
+                = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
+            break;
 
-    ss.format = PA_SAMPLE_S16LE;
-    p_aout->output.output.i_physical_channels =
-            AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
-    p_aout->output.output.i_format = AOUT_FMT_S16_NE;
+        case 1:
+            p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
+            break;
+
+        default:
+            msg_Err(p_aout,"Invalid number of channels");
+        goto fail;
+    }
+
+    /* Add a quick command line info message */
+    msg_Info(p_aout, "No. of Audio Channels: %d", ss.channels);
+
+    ss.rate = p_aout->output.output.i_rate;
+    ss.format = PA_SAMPLE_FLOAT32NE;
+    p_aout->output.output.i_format = VLC_CODEC_FL32;
 
     if (!pa_sample_spec_valid(&ss)) {
         msg_Err(p_aout,"Invalid sample spec");
         goto fail;
     }
-    
-    a.maxlength = pa_bytes_per_second(&ss)/4/pa_frame_size(&ss);
-    a.tlength = a.maxlength*9/10;
-    a.prebuf = a.tlength/2;
-    a.minreq = a.tlength/10;
 
-    a.maxlength *= pa_frame_size(&ss);
-    a.tlength *= pa_frame_size(&ss);
-    a.prebuf *= pa_frame_size(&ss);
-    a.minreq *= pa_frame_size(&ss);
+    /* Reduce overall latency to 200mS to reduce audible clicks
+     * Also pulse minreq and internal buffers are now 20mS which reduces resampling
+     */
+    a.tlength = pa_bytes_per_second(&ss)/5;
+    a.maxlength = a.tlength * 2;
+    a.prebuf = a.tlength / 2;
+    a.minreq = a.tlength / 10;
 
+    /* Buffer size is 20mS */
     p_sys->buffer_size = a.minreq;
 
-    pa_channel_map_init_stereo(&map);
-
+    /* Initialise the speaker map setup above */
+    pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_ALSA);
 
     if (!(p_sys->mainloop = pa_threaded_mainloop_new())) {
         msg_Err(p_aout, "Failed to allocate main loop");
@@ -173,7 +212,7 @@ static int Open ( vlc_object_t *p_this )
     PULSE_DEBUG( "Pulse after context connect");
 
     pa_threaded_mainloop_lock(p_sys->mainloop);
-    
+
     if (pa_threaded_mainloop_start(p_sys->mainloop) < 0) {
         msg_Err(p_aout, "Failed to start main loop");
         goto unlock_and_fail;
@@ -200,7 +239,7 @@ static int Open ( vlc_object_t *p_this )
     pa_stream_set_write_callback(p_sys->stream, stream_request_cb, p_aout);
     pa_stream_set_latency_update_callback(p_sys->stream, stream_latency_update_cb, p_aout);
 
-    if (pa_stream_connect_playback(p_sys->stream, NULL, &a, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL) < 0) {
+    if (pa_stream_connect_playback(p_sys->stream, NULL, &a, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE|PA_STREAM_ADJUST_LATENCY, NULL, NULL) < 0) {
         msg_Err(p_aout, "Failed to connect stream: %s", pa_strerror(pa_context_errno(p_sys->context)));
         goto unlock_and_fail;
     }
@@ -408,16 +447,11 @@ static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
                 latency = 0;
 
             }
+
             PULSE_DEBUG( "Pulse stream request latency=%"PRId64"", latency);
             next_date = mdate() + latency;
 
-
             if(p_sys->start_date < next_date + AOUT_PTS_TOLERANCE ){
-    /*
-                  vlc_mutex_lock( &p_aout->output_fifo_lock );
-                p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
-                vlc_mutex_unlock( &p_aout->output_fifo_lock );
-    */
                 p_buffer = aout_OutputNextBuffer( p_aout, next_date, 0);
             }
         }