]> git.sesse.net Git - vlc/commitdiff
auhal: use audio device latency for better a/v synchronization and airplay support
authorDavid Fuhrmann <dfuhrmann@videolan.org>
Wed, 9 Apr 2014 16:56:00 +0000 (18:56 +0200)
committerDavid Fuhrmann <dfuhrmann@videolan.org>
Thu, 10 Apr 2014 09:45:37 +0000 (11:45 +0200)
refs #7127

modules/audio_output/auhal.c

index 5cec0ad5f19d360a9276b419a3d6bb8890b8526a..e512bc39eb6ecbe237de79dfc8f09e54aad6424c 100644 (file)
@@ -117,6 +117,8 @@ struct aout_sys_t
     vlc_cond_t                  cond;
 
     bool                        b_ignore_streams_changed_callback;
+
+    UInt32                      i_device_latency;    /* The time the device needs to process the data. In samples. */
 };
 
 #pragma mark -
@@ -314,6 +316,7 @@ static int Start(audio_output_t *p_aout, audio_sample_format_t *restrict fmt)
     p_sys->b_changed_mixing = false;
     p_sys->i_bytes_per_sample = 0;
     p_sys->b_paused = false;
+    p_sys->i_device_latency = 0;
 
     vlc_mutex_lock(&p_sys->var_lock);
     p_sys->i_selected_dev = p_sys->i_new_selected_dev;
@@ -409,6 +412,22 @@ static int Start(audio_output_t *p_aout, audio_sample_format_t *restrict fmt)
         goto error;
     }
 
+    /* get device latency */
+    AudioObjectPropertyAddress latencyAddress = { kAudioDevicePropertyLatency, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
+    i_param_size = sizeof(p_sys->i_device_latency);
+    err = AudioObjectGetPropertyData(p_sys->i_selected_dev,
+                                     &latencyAddress,
+                                     0,
+                                     NULL,
+                                     &i_param_size,
+                                     &p_sys->i_device_latency);
+    if (err != noErr) {
+        msg_Warn(p_aout, "Cannot get device latency [%4.4s]",
+                 (char *)&err);
+    }
+    msg_Dbg(p_aout, "Current device has a latency of %u frames", p_sys->i_device_latency);
+
+
     bool b_success = false;
 
     /* Check for Digital mode or Analog output mode */
@@ -1424,7 +1443,7 @@ static int TimeGet(audio_output_t *p_aout, mtime_t *delay)
     int32_t availableBytes;
     TPCircularBufferTail(&p_sys->circular_buffer, &availableBytes);
 
-    *delay = (availableBytes / p_sys->i_bytes_per_sample) * CLOCK_FREQ / p_sys->i_rate;
+    *delay = ((availableBytes / p_sys->i_bytes_per_sample) + p_sys->i_device_latency) * CLOCK_FREQ / p_sys->i_rate;
 
     return 0;
 }