Files
CyberGril/samples/companion/tools/driver_audio.py
T

36 lines
1.4 KiB
Python

"""Pending audio before RTP timestamps: only type=1 synthetic idle is expendable."""
import queue
class PendingAudio(queue.Queue):
"""Keep at most three un-timestamped synthetic blocks; preserve speech/events.
One render producer, one RTP consumer. Real speech (including zero PCM and
custom type>1 audio) uses bounded Queue backpressure, never a drop policy.
"""
silence_limit = 3
def __init__(self):
super().__init__(maxsize=100)
self.dropped_silence = 0
self.high_water = 0
def put(self, item, block=True, timeout=None):
with self.not_full:
# Trim before assigning pts, never inside the RTP consumer. Event
# markers remain observable even if a producer puts one on silence.
idle = [i for i, (_, event, kind) in enumerate(self.queue) if kind == 1 and not event]
incoming_idle = item[2] == 1 and not item[1]
excess = len(idle) + int(incoming_idle) - self.silence_limit
for i in reversed(idle[:max(0, excess)]):
del self.queue[i]
self.unfinished_tasks -= 1
self.dropped_silence += 1
if excess > 0:
self.not_full.notify_all()
super().put(item, block=block, timeout=timeout)
with self.mutex:
self.high_water = max(self.high_water, len(self.queue))