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
|
|
|
|
2019-03-23 17:18:52 +00:00
|
|
|
mainLogger = logging.getLogger("hugvey")
|
|
|
|
logger = mainLogger.getChild("streamer")
|
2019-01-17 16:39:52 +00:00
|
|
|
|
|
|
|
class AudioStreamer(object):
|
2019-04-10 09:13:42 +00:00
|
|
|
def __init__(self, chunk, address: str, port: int, hv_id: int):
|
|
|
|
self.logger = mainLogger.getChild(f"{hv_id}").getChild('streamer')
|
|
|
|
|
2019-01-17 16:39:52 +00:00
|
|
|
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)
|
2019-05-01 16:27:10 +00:00
|
|
|
self.socket.setsockopt(zmq.RCVTIMEO, 8000) # timeout: 8 sec
|
2019-01-17 20:23:05 +00:00
|
|
|
self.socket.subscribe('')
|
|
|
|
# self.socket.setsockopt(zmq.CONFLATE, 1)
|
|
|
|
self.socket.connect(address)
|
|
|
|
|
|
|
|
# s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2019-05-01 16:27:10 +00:00
|
|
|
self.logger.info("0mq SUBSCRIBE for audio stream on {}:{}".format(self.address, self.port))
|
2019-01-17 20:23:05 +00:00
|
|
|
# s.connect((self.address, self.port))
|
|
|
|
#
|
2019-05-01 16:27:10 +00:00
|
|
|
while self.isRunning:
|
|
|
|
try:
|
2019-01-25 09:43:55 +00:00
|
|
|
data = await self.socket.recv()
|
2019-05-01 16:27:10 +00:00
|
|
|
# self.logger.debug('chunk received')
|
2019-01-25 09:43:55 +00:00
|
|
|
self.process(data)
|
2019-05-01 16:27:10 +00:00
|
|
|
except zmq.error.Again as timeout_e:
|
|
|
|
self.logger.warn("Timeout of audiostream. Hugvey shutdown?")
|
|
|
|
|
|
|
|
self.logger.info("Close socket on {}:{}".format(self.address, self.port))
|
|
|
|
self.socket.close()
|
2019-01-17 16:39:52 +00:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.isRunning = False
|
|
|
|
|
|
|
|
for consumer in self.consumers:
|
|
|
|
consumer.shutdown()
|
2019-04-25 15:39:44 +00:00
|
|
|
|
|
|
|
self.consumers = []
|
2019-01-17 16:39:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def process(self, chunk):
|
2019-04-10 09:13:42 +00:00
|
|
|
# self.logger.debug("Received chunk")
|
2019-01-17 16:39:52 +00:00
|
|
|
for consumer in self.consumers:
|
|
|
|
consumer.receive(chunk)
|
|
|
|
|
2019-04-10 08:13:35 +00:00
|
|
|
def triggerStart(self):
|
|
|
|
# start a (new) run on the hugvey. Send it to the consumers that need it
|
|
|
|
for consumer in self.consumers:
|
2019-04-25 15:39:44 +00:00
|
|
|
consumer.triggerStart()
|
|
|
|
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
self.logger.warn("Destroyed streamer object")
|