Browse Source

Cleanup

feature/voice
andrei 8 years ago
parent
commit
879011f6f0
  1. 20
      disco/voice/client.py
  2. 7
      disco/voice/player.py

20
disco/voice/client.py

@ -33,20 +33,28 @@ class UDPVoiceClient(LoggingClass):
def __init__(self, vc):
super(UDPVoiceClient, self).__init__()
self.vc = vc
# The underlying UDP socket
self.conn = None
# Connection information
self.ip = None
self.port = None
self.sequence = 0
self.timestamp = 0
self.run_task = None
self.connected = False
def send_frame(self, frame, sequence, timestamp):
def send_frame(self, frame, sequence=None, timestamp=None):
data = bytearray(12)
data[0] = 0x80
data[1] = 0x78
struct.pack_into('>H', data, 2, sequence)
struct.pack_into('>I', data, 4, timestamp)
struct.pack_into('>H', data, 2, sequence or self.sequence)
struct.pack_into('>I', data, 4, timestamp or self.timestamp)
struct.pack_into('>i', data, 8, self.vc.ssrc)
self.send(data + ''.join(frame))
self.sequence += 1
def run(self):
while True:
@ -58,12 +66,15 @@ class UDPVoiceClient(LoggingClass):
def disconnect(self):
self.run_task.kill()
def connect(self, host, port, timeout=10):
def connect(self, host, port, timeout=10, addrinfo=None):
self.ip = socket.gethostbyname(host)
self.port = port
self.conn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
if addrinfo:
ip, port = addrinfo
else:
# Send discovery packet
packet = bytearray(70)
struct.pack_into('>I', packet, 0, self.vc.ssrc)
@ -205,7 +216,6 @@ class VoiceClient(LoggingClass):
})
def on_close(self, code, error):
# TODO
self.log.warning('Voice websocket disconnected (%s, %s)', code, error)
if self.state == VoiceState.CONNECTED:

7
disco/voice/player.py

@ -180,10 +180,10 @@ class Player(object):
def play(self, item):
start = time.time()
loops = 0
timestamp = 0
while True:
loops += 1
if self.paused:
self.client.set_speaking(False)
self.paused.wait()
@ -199,8 +199,9 @@ class Player(object):
if not item.have_frame():
return
self.client.send_frame(item.next_frame(), loops, timestamp)
timestamp += item.samples_per_frame
self.client.send_frame(item.next_frame())
self.client.timestamp += item.samples_per_frame
next_time = start + 0.02 * loops
delay = max(0, 0.02 + (next_time - time.time()))
gevent.sleep(delay)

Loading…
Cancel
Save