hugvey/hugvey/voice/streamer.py

49 lines
1.3 KiB
Python

"""
Consume a given Hugvey audio socket, and stream into the given services to emit events to the given server
"""
import socket
import logging
logger = logging.getLogger("streamer")
class AudioStreamer(object):
def __init__(self, chunk, address: str, port: int):
self.consumers = []
self.chunk = chunk
self.address = address
self.port = port
self.isRunning = False
def addConsumer(self, consumer):
self.consumers.append(consumer)
async def run(self):
self.isRunning = True
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
logger.info("Attempt connection on {}:{}".format(self.address, self.port))
s.connect((self.address, self.port))
while self.isRunning:
data = s.recv(self.chunk)
# logger.debug('chunk received')
self.process(data)
logger.info("Close socket on {}:{}".format(self.address, self.port))
s.close()
def stop(self):
self.isRunning = False
for consumer in self.consumers:
consumer.shutdown()
def process(self, chunk):
# logger.debug("Received chunk")
for consumer in self.consumers:
consumer.receive(chunk)