2019-01-17 16:39:52 +00:00
|
|
|
"""
|
|
|
|
Consume a given Hugvey audio socket, and stream into the given services to emit events to the given server
|
|
|
|
"""
|
|
|
|
import socket
|
|
|
|
import logging
|
2019-01-17 20:23:05 +00:00
|
|
|
from zmq.asyncio import Context
|
|
|
|
import zmq
|
2019-01-17 16:39:52 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-01-17 20:23:05 +00:00
|
|
|
address = "tcp://{}:{}".format(self.address, self.port)
|
|
|
|
self.ctx = Context.instance()
|
|
|
|
self.socket = self.ctx.socket(zmq.SUB)
|
|
|
|
self.socket.subscribe('')
|
|
|
|
# self.socket.setsockopt(zmq.CONFLATE, 1)
|
|
|
|
self.socket.connect(address)
|
|
|
|
|
|
|
|
# s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2019-01-17 16:39:52 +00:00
|
|
|
logger.info("Attempt connection on {}:{}".format(self.address, self.port))
|
2019-01-17 20:23:05 +00:00
|
|
|
# s.connect((self.address, self.port))
|
|
|
|
#
|
2019-01-17 16:39:52 +00:00
|
|
|
while self.isRunning:
|
2019-01-17 20:23:05 +00:00
|
|
|
data = await self.socket.recv()
|
2019-01-17 16:39:52 +00:00
|
|
|
# logger.debug('chunk received')
|
|
|
|
self.process(data)
|
|
|
|
|
|
|
|
logger.info("Close socket on {}:{}".format(self.address, self.port))
|
2019-01-17 20:23:05 +00:00
|
|
|
self.socket.close()
|
2019-01-17 16:39:52 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|