--- libwavesync-orig/chunk_player.py	2020-09-04 02:54:37.103466272 +0200
+++ libwavesync/chunk_player.py	2020-09-11 16:11:45.312535851 +0200
@@ -2,13 +2,60 @@
 import random
 from time import time
 from libwavesync import time_machine
+import pyaudio
+
+from collections import deque
+from time import sleep
+
+
+#PLAY_CALLBACK=True
+
+QUEUE_MAX=12
+q=deque(maxlen=QUEUE_MAX)
+
+# critical for callback variety; recived packet size must be the same as in initialization call
+glob_chunk_size=367*4
+
+glob_stream_break=0
+
+
+def snd_callback(in_data, frame_count, time_info, status):
+#        print("callback called",status,buf_chunk_wait,pyaudio.paContinue, pyaudio.paComplete,len(buf_chunk))
+        # if queue is empty, return artificial silence
+
+        if glob_stream_break > 0:
+            return (b'\x00'*glob_chunk_size,pyaudio.paComplete)
+        urun=0
+        while len(q)==0:
+                if glob_stream_break > 0:
+                    return (b'\x00'*glob_chunk_size, pyaudio.paComplete)
+                sleep(0.010)
+                urun+=1
+                if urun == 3 and len(q)==0: print('queue underrun')
+
+        #if len(q)==0: return (b'\x00'*glob_chunk_size,pyaudio.paContinue)
+        # dirty hack: help empty overly long queue by dropping extra data
+        if len(q)>12:
+            print('qdrop')
+            _=q.popleft()
+        return (q.popleft(), pyaudio.paContinue)
+
 
 
 class ChunkPlayer:
     "Play received audio and keep sync"
 
+
+
+
+
     def __init__(self, chunk_queue, receiver, tolerance_ms,
-                 buffer_size, device_index):
+                 buffer_size, device_index, use_callback):
+
+        # playback mode
+        #global PLAY_CALLBACK
+        self.PLAY_CALLBACK=use_callback
+
         # Our data source
         self.chunk_queue = chunk_queue
 
@@ -45,6 +92,7 @@
         self.frame_size = None
         self.chunk_frames = None
 
+
     def clear_state(self):
         "Clear player queue"
         self.silence_to_insert = 0
@@ -78,8 +126,15 @@
 
         # Open stream
         cfg = self.audio_config
+        global glob_chunk_size
+        glob_chunk_size=cfg.chunk_size
 
-        frames_per_buffer = self.buffer_size
+#       for callback: MUST BE ACTUALLY WORKING NUMBER - otherwise the stream will fail with both stream.is_active() and stream.is_stopped() as false
+        if self.PLAY_CALLBACK:
+            frames_per_buffer = int(cfg.chunk_size/cfg.frame_size)
+        else:
+            frames_per_buffer = self.buffer_size
+            #AudioConfig 44100Hz 16bits 2ch latency=4000ms sink=-250ms size chunk=1468 frame=4
 
         if self.device_index == -1:
             # We are tested. Don't open stream, but calculate chunk_frames.
@@ -90,6 +145,7 @@
             if self.device_index is None:
                 config = self.pyaudio.get_host_api_info_by_index(0)
                 device_index = config['defaultOutputDevice']
+                print('CONFIG:',config)
                 print("Using default output device index", device_index)
             else:
                 device_index = self.device_index
@@ -99,13 +155,24 @@
                 if cfg.sample == 24
                 else pyaudio.paInt16
             )
-            stream = self.pyaudio.open(output=True,
-                                       channels=cfg.channels,
-                                       rate=cfg.rate,
-                                       format=audio_format,
-                                       frames_per_buffer=frames_per_buffer,
-                                       output_device_index=device_index)
+
+            if self.PLAY_CALLBACK:
+                stream = self.pyaudio.open(output=True,
+                                           channels=cfg.channels,
+                                           rate=cfg.rate,
+                                           format=audio_format,
+                                           frames_per_buffer=frames_per_buffer,
+                                           output_device_index=device_index,
+                                           stream_callback=snd_callback)
+            else:
+                stream = self.pyaudio.open(output=True,
+                                           channels=cfg.channels,
+                                           rate=cfg.rate,
+                                           format=audio_format,
+                                           frames_per_buffer=frames_per_buffer,
+                                           output_device_index=device_index)
             self.stream = stream
+            print('audio config:',self.audio_config)
 
         self.chunk_frames = self.audio_config.chunk_size / cfg.frame_size
 
@@ -116,6 +183,7 @@
         self.stream = None
 
 
+
     async def chunk_player(self):
         "Reads asynchronously chunks from the list and plays them"
 
@@ -124,17 +192,51 @@
         # Chunk/s stat
         recent_start = time()
         recent = 0
+        mark=0
 
         mid_tolerance_s = self.tolerance_ms / 2 / 1000
         one_ms = 1/1000.0
+        if self.PLAY_CALLBACK:
+            wait_if_greater=one_ms*10
+        else:
+            wait_if_greater=one_ms
 
-        max_delay = 5
+#        max_delay = 5
+        max_delay = 85
 
-        while not self.stop:
+        global glob_stream_break
+        # get initial NTP sync, thrice to be more accurate
+        time_machine.ntp_getoffset()
+        time_machine.ntp_getoffset()
+        time_machine.ntp_runtimer()
+
+        try:
+          while not self.stop:
             if not self.chunk_queue.chunk_list:
 
                 if self.audio_config is not None:
                     print("Queue empty - waiting")
+                    # reopen stream - cheat for resyncing the audio queue on termux/android; will it work?
+                    if self.PLAY_CALLBACK:
+                        q.clear()
+                        print('RESYNC - stream reopen');
+                        glob_stream_break=1
+                        await asyncio.sleep(0.030)
+                        self.stream.stop_stream()
+                        print('RESYNC - stream wait');
+                        await asyncio.sleep(0.005)
+                        print('RESYNC - stream restarting');
+                        glob_stream_break=0
+                        self.stream.start_stream()
+                        print('RESYNC - stream restarted');
+
+#                    if self.stream:
+#                        print('closing audio device')
+#                        self._close_stream()
+#                        print('closed audio device')
+#                        await asyncio.sleep(10)
+#                    print('opening audio device')
+#                    self._open_stream()
 
                 self.chunk_queue.chunk_available.clear()
                 await self.chunk_queue.chunk_available.wait()
@@ -146,6 +248,10 @@
                     print("Got stream flowing. q_len=%d" % len(self.chunk_queue.chunk_list))
                 continue
 
+
+#            for i in self.chunk_queue.chunk_list:
+#                if i[0]==1: print(i[1][0])
+
             cmd, item = self.chunk_queue.chunk_list.popleft()
 
             if cmd == self.chunk_queue.CMD_CFG:
@@ -155,9 +261,12 @@
                 if self.stream:
                     self._close_stream()
                 self._open_stream()
-                # Calculate maximum sensible delay in given configuration
+                # Calculate maximum sensible delay in given configuration, in seconds
                 max_delay = (2000 + self.audio_config.sink_latency_ms +
                              self.audio_config.latency_ms) / 1000
+                #print("max_delay calculated:",max_delay) # Shad's mod
+                #max_delay=60 # Shad's mod for testing on lousy-sync android
+                #max_delay=15 # Shad's mod for testing on lousy-sync android
                 print("Assuming maximum chunk delay of %.2fms in this setup" % (max_delay * 1000))
                 continue
             elif cmd == self.chunk_queue.CMD_DROPS:
@@ -175,8 +284,20 @@
                 # No output, no playing.
                 continue
 
+            # for packet time difference
+            oldmark=mark
             mark, chunk = item
-            desired_time = mark - self.audio_config.sink_latency_ms
+
+            # check to make packets with wrong timing visible; times valid for 44kHz/16bit/2ch
+            if mark-oldmark < 0.0075 or mark-oldmark > 0.0095:
+                print('out-of-spec td: {:.4f}'.format(mark-oldmark))
+
+            # throw out misordered packet
+            if mark < oldmark:
+                print('bad timing ordering, packet skipped')
+                continue
+
+            desired_time = mark - (self.audio_config.sink_latency_ms/1000.0)
 
             # 0) We got the next chunk to be played
             now = time_machine.now()
@@ -212,28 +333,56 @@
                 continue
 
             # If chunk is in the future - wait until it's within the tolerance
-            elif delay > one_ms:
-                to_wait = max(one_ms, delay - one_ms)
-                await asyncio.sleep(to_wait)
+            else:
+                desired_time=desired_time-0.003*len(q)
+                delay = desired_time - now
+                if delay > one_ms:
+                    to_wait = max(one_ms, delay - one_ms)
+                    await asyncio.sleep(to_wait)
 
 
             # Wait until we can write chunk into output buffer. This might
             # delay us too much - the probabilistic dropping mechanism will kick
             # in.
             times = 0
-            while True:
-                buffer_space = self.stream.get_write_available()
-                if buffer_space < self.chunk_frames:
-                    self.stat_output_delays += 1
-                    await asyncio.sleep(one_ms)
-                    times += 1
-                    if times > 200:
-                        print("Hey, the output is STUCK!")
-                        await asyncio.sleep(1)
-                        break
-                    continue
-                self.stream.write(chunk)
-                break
+            buffer_space = 0
+            if not self.PLAY_CALLBACK:
+              while True:
+                # this is for some reason always zero on android/termux!!!
+                    buffer_space = self.stream.get_write_available()
+                    if buffer_space < self.chunk_frames:
+                        self.stat_output_delays += 1
+                        await asyncio.sleep(one_ms)
+                        times += 1
+                        if times > 200:
+                            print("Hey, the output is STUCK! try --callback if persistent")
+                            await asyncio.sleep(1)
+                            break
+                        continue
+                    self.stream.write(chunk)
+                    break
+
+            # callback play, probabilistic dropping of excess chunks to not overload the queue
+            else:
+                ql=len(q)
+                if ql>2:
+                    await asyncio.sleep(0.002)
+                if ql>6:
+                    prob = ql/10+0.2
+                    if random.random() > prob:
+                        s = "DropQ chunk: q_len=%2d delay=%.1fms < 0. tolerance=%.1fms: P=%.2f"
+                        s = s % (len(self.chunk_queue.chunk_list),
+                                 delay * 1000, self.tolerance_ms, prob)
+                        print(s)
+                        self.stat_time_drops += 1
+                        continue
+                q.append(chunk)
+                # guard against stream stops; doesn't seem to be really necessary but better be sure
+                if not self.stream.is_active():
+                   self.stream.stop_stream()
+                   self.stream.start_stream()
+                   print('restarting stream')
+
 
             # Main status line
             if recent > 200:
@@ -252,7 +401,7 @@
                 s = ("STAT: chunks: q_len=%-3d bs=%4.1f "
                      "ch/s=%5.1f "
                      "net lat: %-5.1fms "
-                     "avg_delay=%-5.2f drops: time=%d net=%d out_delay=%d")
+                     "avg_delay=%-5.2f drops: time=%d net=%d out_delay=%d cqlen=%d")
                 s = s % (
                     len(self.chunk_queue.chunk_list),
                     buffer_space / frames_in_chunk,
@@ -262,6 +411,7 @@
                     self.stat_time_drops,
                     network_drops,
                     self.stat_output_delays,
+                    len(q)
                 )
                 print(s)
 
@@ -277,4 +427,9 @@
                         print("WARNING: You either exceeded the speed of "
                               "light or have unsynchronised clocks")
 
-        print("- Finishing chunk player")
+        finally:
+             print("- Finishing chunk player")
+             glob_stream_break=1
+             await asyncio.sleep(0.05)
+             if self.stream: self._close_stream()
+
