Browse Source

[voice] reuse buffer for minor optimization

pull/50/head
Andrei 8 years ago
parent
commit
f37a2b7d6d
  1. 21
      disco/voice/client.py
  2. 1
      disco/voice/opus.py

21
disco/voice/client.py

@ -51,23 +51,25 @@ class UDPVoiceClient(LoggingClass):
self.run_task = None self.run_task = None
self.connected = False self.connected = False
# Buffer used for encoding/sending frames
self._buffer = bytearray(24)
self._buffer[0] = 0x80
self._buffer[1] = 0x78
def send_frame(self, frame, sequence=None, timestamp=None): def send_frame(self, frame, sequence=None, timestamp=None):
# Convert the frame to a bytearray # Convert the frame to a bytearray
frame = bytearray(frame) frame = bytearray(frame)
# First, pack the header (TODO: reuse bytearray?) # Pack the rtc header into our buffer
header = bytearray(24) struct.pack_into('>H', self._buffer, 2, sequence or self.vc.sequence)
header[0] = 0x80 struct.pack_into('>I', self._buffer, 4, timestamp or self.vc.timestamp)
header[1] = 0x78 struct.pack_into('>i', self._buffer, 8, self.vc.ssrc)
struct.pack_into('>H', header, 2, sequence or self.vc.sequence)
struct.pack_into('>I', header, 4, timestamp or self.vc.timestamp)
struct.pack_into('>i', header, 8, self.vc.ssrc)
# Now encrypt the payload with the nonce as a header # Now encrypt the payload with the nonce as a header
raw = self.vc.secret_box.encrypt(bytes(frame), bytes(header)).ciphertext raw = self.vc.secret_box.encrypt(bytes(frame), bytes(self._buffer)).ciphertext
# Send the header (sans nonce padding) plus the payload # Send the header (sans nonce padding) plus the payload
self.send(header[:12] + raw) self.send(self._buffer[:12] + raw)
# Increment our sequence counter # Increment our sequence counter
self.vc.sequence += 1 self.vc.sequence += 1
@ -247,7 +249,6 @@ class VoiceClient(LoggingClass):
self.log.exception('Failed to parse voice gateway message: ') self.log.exception('Failed to parse voice gateway message: ')
def on_error(self, err): def on_error(self, err):
# TODO: raise an exception here
self.log.error('[%s] Voice websocket error: %s', self, err) self.log.error('[%s] Voice websocket error: %s', self, err)
def on_open(self): def on_open(self):

1
disco/voice/opus.py

@ -142,7 +142,6 @@ class OpusEncoder(BaseOpus):
if ret < 0: if ret < 0:
raise Exception('Failed to encode: {}'.format(ret)) raise Exception('Failed to encode: {}'.format(ret))
# TODO: py3
if six.PY3: if six.PY3:
return array.array('b', data[:ret]).tobytes() return array.array('b', data[:ret]).tobytes()
else: else:

Loading…
Cancel
Save