<html>
    <head>
      <base href="https://bugs.freedesktop.org/">
    </head>
    <body><span class="vcard"><a class="email" href="mailto:Kalyanov.Dmitry@gmail.com" title="Dmitry Kalyanov <Kalyanov.Dmitry@gmail.com>"> <span class="fn">Dmitry Kalyanov</span></a>
</span> changed
          <a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - bluetooth audio out of sync when connection temporarily drops"
   href="https://bugs.freedesktop.org/show_bug.cgi?id=58746">bug 58746</a>
          <br>
             <table border="1" cellspacing="0" cellpadding="8">
          <tr>
            <th>What</th>
            <th>Removed</th>
            <th>Added</th>
          </tr>

         <tr>
           <td style="text-align:right;">CC</td>
           <td>
                
           </td>
           <td>Kalyanov.Dmitry@gmail.com
           </td>
         </tr></table>
      <p>
        <div>
            <b><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - bluetooth audio out of sync when connection temporarily drops"
   href="https://bugs.freedesktop.org/show_bug.cgi?id=58746#c8">Comment # 8</a>
              on <a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - bluetooth audio out of sync when connection temporarily drops"
   href="https://bugs.freedesktop.org/show_bug.cgi?id=58746">bug 58746</a>
              from <span class="vcard"><a class="email" href="mailto:Kalyanov.Dmitry@gmail.com" title="Dmitry Kalyanov <Kalyanov.Dmitry@gmail.com>"> <span class="fn">Dmitry Kalyanov</span></a>
</span></b>
        <pre>Created <span class=""><a href="attachment.cgi?id=125291" name="attach_125291" title="Proof-of-concept patch">attachment 125291</a> <a href="attachment.cgi?id=125291&action=edit" title="Proof-of-concept patch">[details]</a></span> <a href='page.cgi?id=splinter.html&bug=58746&attachment=125291'>[review]</a>
Proof-of-concept patch

I've solved this issue for myself - I've been using pulseaudio with my changes
for several months now with no major problems.

I've noticed that every time signal degrades audio gets more out of sync - up
to about 10-15 seconds (if I remember correctly).

I've debugged bluez5 pulseaudio module and suspect that the problem lies in
buffering for bluetooth socket. Here's my analysis:

1) Pulseaudio detects BT signal drop when write() on bluetooth socket returns
EAGAIN (i.e., when the buffer is full).
2) Bluetooth socket buffer is quite big (by default)
3) When pulseaudio stops sending audio packets to BT socket the buffer still
contains a lot of packets
4) pulseaudio considers those packets as successfully sent - but they aren't
5) BT connection seems to never be able to "catch up" with the amount of
buffered packets and audio becomes out-of-sync.

So here's my patch. The main change is to decrease the buffer size as much as
possible. I've experimented and found out that settings buffer size to 2x-5x of
packet size works best for me. This ensures that audio lag won't accumulate
after BT signal degradation while preventing audio skipping due to buffer
underruns. Audio still may skip (sometimes several times in a row) - but
without the lag after BT signal restores.

Unfortunately with this patch bluetooth microphone (headset profile) won't work
- since I don't use one and couldn't test it. I hope that someone would be able
to pick it up and make into a form that would be possible to merge in master
branch.

The changes are contained in attached path on github:
<a href="https://github.com/dmitryvk/pulseaudio/commit/12b13c75d3a9b377e0f7de7c86116e3af41ce5ee">https://github.com/dmitryvk/pulseaudio/commit/12b13c75d3a9b377e0f7de7c86116e3af41ce5ee</a>.
Patch was developed against Pulseaudio-8.0 but it works with Pulseaudio-9.0.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are the QA Contact for the bug.</li>
          <li>You are the assignee for the bug.</li>
      </ul>
    </body>
</html>