Skip to content

fix: manage silence for new SSRC with existing user_id #2808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 51 additions & 13 deletions discord/voice_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ def __init__(self, client: Client, channel: abc.Connectable):
self.sink = None
self.starting_time = None
self.stopping_time = None
self.temp_queued_data: dict[int, list] = {}

warn_nacl = not has_nacl
supported_modes: tuple[SupportedModes, ...] = (
Expand Down Expand Up @@ -845,7 +846,7 @@ def recv_audio(self, sink, callback, *args):
# it by user, handles pcm files and
# silence that should be added.

self.user_timestamps: dict[int, tuple[int, float]] = {}
self.user_timestamps: dict[int, tuple[int, int, float]] = {}
self.starting_time = time.perf_counter()
self.first_packet_timestamp: float
while self.recording:
Expand Down Expand Up @@ -873,7 +874,30 @@ def recv_audio(self, sink, callback, *args):

def recv_decoded_audio(self, data: RawData):
# Add silence when they were not being recorded.
if data.ssrc not in self.user_timestamps: # First packet from user
data.user_id = self.ws.ssrc_map.get(data.ssrc, {}).get("user_id")

if data.user_id is None:
_log.debug(
f"DEBUG: received packet with SSRC {data.ssrc} not linked to a user_id."
f"Queueing for later processing."
)
self.temp_queued_data.setdefault(data.ssrc, []).append(data)
return
elif data.ssrc in self.temp_queued_data:
_log.debug(
"DEBUG: We got %d packet(s) in queue for SSRC %d",
len(self.temp_queued_data[data.ssrc]),
data.ssrc,
)
queued_packets = self.temp_queued_data.pop(data.ssrc)
for q_packet in queued_packets:
q_packet.user_id = data.user_id
self._process_audio_packet(q_packet)

self._process_audio_packet(data)

def _process_audio_packet(self, data: RawData):
if data.user_id not in self.user_timestamps: # First packet from user
if (
not self.user_timestamps or not self.sync_start
): # First packet from anyone
Expand All @@ -886,19 +910,33 @@ def recv_decoded_audio(self, data: RawData):
) - 960

else: # Previously received a packet from user
dRT = (
data.receive_time - self.user_timestamps[data.ssrc][1]
) * 48000 # delta receive time
dT = data.timestamp - self.user_timestamps[data.ssrc][0] # delta timestamp
diff = abs(100 - dT * 100 / dRT)
if (
diff > 60 and dT != 960
): # If the difference in change is more than 60% threshold
silence = dRT - 960
prev_ssrc = self.user_timestamps[data.user_id][0]
prev_timestamp = self.user_timestamps[data.user_id][1]
prev_receive_time = self.user_timestamps[data.user_id][2]

if data.ssrc != prev_ssrc:
_log.info(
f"Received audio data from USER_ID {data.user_id} with a previous SSRC {prev_ssrc} and new "
f"SSRC {data.ssrc}."
)
dRT = (data.receive_time - prev_receive_time) * 1000
silence = max(0, int(dRT / (1000 / 48000))) - 960
else:
silence = dT - 960
dRT = (
data.receive_time - prev_receive_time
) * 48000 # delta receive time
dT = data.timestamp - prev_timestamp # delta timestamp
diff = abs(100 - dT * 100 / dRT)
if (
diff > 60 and dT != 960
): # If the difference in change is more than 60% threshold
silence = dRT - 960
else:
silence = dT - 960

self.user_timestamps.update({data.ssrc: (data.timestamp, data.receive_time)})
self.user_timestamps.update(
{data.user_id: (data.ssrc, data.timestamp, data.receive_time)}
)

data.decoded_data = (
struct.pack("<h", 0) * max(0, int(silence)) * opus._OpusStruct.CHANNELS
Expand Down