def connect( self, url, timeout=None ): # Generator to connect to the given url, and return True or False. if url[:7].lower() != 'rtmp://': raise ValueError('Invalid URL scheme. Must be rtmp://') path, ignore, ignore = url[7:].partition('?') hostport, ignore, path = path.partition('/') host, port = (hostport.split(':', 1) + ['1935'])[:2] self.data.tcUrl, self.data.app = url, path sock = socket.socket(type=socket.SOCK_STREAM) if _debug: print 'NetConnection.connect url=', url, 'host=', host, 'port=', port try: sock.connect((host, int(port))) except: raise StopIteration, False sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) # make it non-block self.client = yield Client(sock).handshake() result, fault = yield self.client.send(Command(name='connect', cmdData=self.data), timeout=timeout) if _debug: print 'NetConnection.connect result=', result, 'fault=', fault raise StopIteration, (result is not None)
def send(self, msg): def sendInternal(self, msg): yield self.queue.put(msg) if msg.type in (Message.RPC, Message.RPC3): cmd = Command.fromMessage(msg) if cmd.name == 'onStatus' and len(cmd.args) > 0 and hasattr(cmd.args[0], 'code') and cmd.args[ 0].code == 'NetStream.Play.Stop': msg = False # indicates end of file multitask.add(sendInternal(self, msg))
def messageReceived(self, msg): # invoked by base class framework to handle a receive message. if (msg.type == Message.RPC or msg.type == Message.RPC3) and msg.streamId == 0: cmd = Command.fromMessage(msg) yield self.queue.put(cmd, timeout=5) # RPC call, must be processed by application within 5 seconds, or will be discarded. elif msg.streamId in self.streams: # this has to be a message on the stream stream = self.streams[msg.streamId] if not stream.client: stream.client = self yield stream.queue.put(msg) # give it to stream elif _debug: print 'ignoring stream message for streamId=', msg.streamId
def create(self, nc, timeout=None): self.nc = nc result, fault = yield self.nc.client.send(Command(name='createStream'), timeout=timeout) if _debug: print 'createStream result=', result, 'fault=', fault if result: stream = self.stream = Stream(self.nc.client) stream.queue, stream.id = multitask.SmartQueue(), int(result.args[0]) # replace with SmartQueue self.nc.client.streams[stream.id] = stream raise StopIteration, self else: raise StopIteration, None
def send(self, msg): def sendInternal(self, msg): yield self.queue.put(msg) if msg.type in (Message.RPC, Message.RPC3): cmd = Command.fromMessage(msg) if cmd.name == 'onStatus' and len(cmd.args) > 0 and hasattr( cmd.args[0], 'code') and cmd.args[0].code == 'NetStream.Play.Stop': msg = False # indicates end of file multitask.add(sendInternal(self, msg))
def close(self): self.stream.send(Command(name='closeStream')) msg = yield self.stream.recv() if _debug: print 'closeStream response=', msg
def play(self, name, timeout=None): self.stream.send(Command(name='play', args=[name])) msg = yield self.stream.recv() if _debug: print 'play response=', msg raise StopIteration, True
def publish(self, name, timeout=None): self.stream.send(Command(name='publish', args=[name])) msg = yield self.stream.recv() if _debug: print 'publish result=', msg raise StopIteration, True