'''
This is a simple implementation of a Flash RTMP client to perform remote copy similar to secure copy (scp) application.
It shares some classes with the rtmp.py module, and is based on the server implementation of that module.
The client takes two arguments, src and dest, for source and destination resources. It copies the stream from src to dest resources.
A resource can be identified using an URL with "rtmp", "http" or "file" scheme. If no scheme is given, it is assumed to be a local file
with a "file://" prefix. An "rtmp" resource URL is of the form "rtmp://server/app?id=streamname" which represents an RTMP connection
to server using NetConnection URL "rtmp://server/app" followed by a new NetStream (either published or played) with name "streamname".
An "http" resource URL is of the form "http://server/something/file1.flv" and represents a web accessible FLV file. When reading a
local or web resource, the reading stops at the end of the file. When reading a RTMP resource, the reading stops either on connection
termination by the server or <ctrl-C> on command line or when no stream data is received for 10 seconds. You can change this timeout
using the "timeout" header in the "rtmp" URL, e.g., "rtmp://server/app?id=streamname&timeout=20".
Most common use of rtmpclient.py is to record a real-time stream or stream out a file to the server.
Case 1: record a real-time stream to a local file.
$ python rtmpclient.py "rtmp://server/app?id=user1" file1.flv
Case 2: stream out a local file to a real-time server stream.
$ python rtmpclient.py file2.flv "rtmp://server/app?id=user2"
The other use cases of the software such as downloading an http resource to local file or storing real-time stream to an http resource,
are straight forward to implement, either not dependent on RTMP or use one of the above cases.
This module can be tested using the same testClient you used for testing the rtmp.py server module. In particular, when the server is
running, you can run an instance of testClient to publish a stream named user1, and run rtmpclient.py to record that stream into file1.flv.
Then, for second case, you can stream out file1.flv using rtmpclient.py and have testClient play that stream.
To understand the code, please see the high level method copy() and open(). Usually you can use the copy method to invoke the copier. If you
want to work on individual resource objects, use the open method and the returned resource object.
'''
import os, sys, traceback, time, urlparse, socket, logging
from ... import multitask
from .rtmp import Protocol, Message, Command, ConnectionClosed, Stream, FLV
from .amf import Object
logger = logging.getLogger('rtmpclient')
class Client(Protocol):
'''Internal class to interface with the RTMP parser from the rtmp.py module. The other classes such as NetConnection and NetStream use this
class to do handshake() and send() RPC commands to the server. The send method itself receives the RPC response.'''
def __init__(self, sock):
Protocol.__init__(self, sock)
self.streams, self.objectEncoding, self._nextCallId, self.queue, self.close_queue = {}, 0.0, 1, multitask.SmartQueue(), multitask.Queue()
def handshake(self):
yield self.stream.write('\x03' + '\x00'*(Protocol.PING_SIZE))
data = (yield self.stream.read(Protocol.PING_SIZE + 1))
yield self.stream.write(data[1:])
data = (yield self.stream.read(Protocol.PING_SIZE))
multitask.add(self.parse()); multitask.add(self.write())
raise StopIteration, self
def parse(self):
try: yield self.parseMessages()
except ConnectionClosed: yield self.connectionClosed()
def connectionClosed(self):
logger.debug('Client.connectionClosed')
yield self.writeMessage(None)
for stream in self.streams.values(): yield stream.queue.put(None)
yield self.queue.put(None)
yield self.close_queue.put(None)
self.streams.clear()
def send(self, cmd, timeout=None):
cmd.id, cmd.type = float(self._nextCallId), (self.objectEncoding == 0.0 and Message.RPC or Message.RPC3)
callId = self._nextCallId; self._nextCallId += 1
logger.debug('Client.send cmd=%r name=%r args=%r msg=%r', cmd, cmd.name, cmd.args, cmd.toMessage())
yield self.writeMessage(cmd.toMessage())
try:
res = yield self.queue.get(timeout=timeout, criteria=lambda x: x is None or x.id == callId)
result = res if res is not None and res.name == '_result' else None
fault = res if res is None or res.name == '_error' else None
raise StopIteration, (result, fault)
except multitask.Timeout:
logger.debug('Client.send timed out')
raise StopIteration, (None, None)
def messageReceived(self, msg):
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)
elif msg.streamId in self.streams:
stream = self.streams[msg.streamId]
if not stream.client: stream.client = self
yield stream.queue.put(msg)
else: logger.debug('ignoring stream message for streamId=%r', msg.streamId)
@property
def rpc(self):
return Message.RPC if self.objectEncoding == 0.0 else Message.RPC3
def call(self, method, *args):
'''Call a (callback) method on the client.'''
cmd = Command()
cmd.id, cmd.time, cmd.name, cmd.type = self._nextCallId, self.relativeTime, method, self.rpc
cmd.args, cmd.cmdData = args, None
self._nextCallId += 1
logger.debug('Client.call method=%r args=%r msg=%r', method, args, cmd.toMessage())
yield self.writeMessage(cmd.toMessage())
class NetConnection(object):
'''This is similar to the NetConnection object of ActionScript 3.0, and represents a client-server connection. The application usually
invokes the connect() method to initiate the connection, create one or more NetStream, and finally close() method to disconnect.'''
def __init__(self):
self.client = self.path = None
self.data = Object(videoCodecs=252.0, audioCodecs=3191.0, flashVer='WIN 10,0,32,18', swfUrl=None, videoFunction=1.0, capabilities=15.0, fpad=False, objectEncoding=0.0)
def connect(self, url, timeout=None, *args):
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)
logger.debug('NetConnection.connect url=%r host=%r port=%r', url, host, port)
try: sock.connect((host, int(port)))
except: raise StopIteration, False
try:
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
self.client = yield Client(sock).handshake()
result, fault = yield self.client.send(Command(name='connect', cmdData=self.data, args=args), timeout=timeout)
logger.debug('NetConnection.connect result=%r fault=%r', result, fault)
except:
logger.exception('NetConnection.connect failed to do handshake')
try: sock.close()
except: pass
raise StopIteration, False
raise StopIteration, (result is not None)
def close(self):
if self.client is not None:
yield self.client.connectionClosed()
try: os.close(self.client.stream.sock.fileno())
except: pass
self.client = None
class NetStream(object):
'''This is similar to the NetStream class of ActionScript 3.0, and represents a client-server media stream for play or publish. The application
creates a NetStream, first invokes create(), then either publish() or play() but not both, and finally close() to terminate.'''
def __init__(self):
self.nc = self.stream = None
def create(self, nc, timeout=None):
self.nc = nc
result, fault = yield self.nc.client.send(Command(name='createStream'), timeout=timeout)
logger.debug('createStream result=%r fault=%r', result, fault)
if result:
stream = self.stream = Stream(self.nc.client)
stream.queue, stream.id = multitask.SmartQueue(), int(result.args[0])
self.nc.client.streams[stream.id] = stream
raise StopIteration, self
else: raise StopIteration, None
def publish(self, name, mode='live', timeout=None):
yield self.send(Command(name='publish', args=[name, mode]))
msg = yield self.stream.recv()
logger.debug('publish result=%r', msg)
raise StopIteration, True
def play(self, name, timeout=None):
yield self.send(Command(name='play', args=[name]))
msg = yield self.stream.recv()
logger.debug('play response=%r', msg)
raise StopIteration, True
def close(self):
yield self.send(Command(name='closeStream'))
msg = yield self.stream.recv()
logger.debug('closeStream response=%r', msg)
def send(self, cmd):
cmd.id, cmd.type = float(self.nc.client._nextCallId), (self.nc.client.objectEncoding == 0.0 and Message.RPC or Message.RPC3)
self.nc.client._nextCallId += 1
msg = cmd.toMessage()
msg.streamId = self.stream.id
logger.debug('Stream.send cmd=%r name=%r args=%r msg=%r', cmd, cmd.name, cmd.args, msg)
yield self.nc.client.writeMessage(msg)
class Result(Exception): pass
class Resource(object):
__slots__ = ['url', 'type', 'mode']
def __init__(self): self.url = self.type = self.mode = None; self.queue = multitask.SmartQueue()
def get(self, timeout=None, criteria=None): result = yield self.queue.get(timeout=timeout, criteria=criteria); raise StopIteration, result
def put(self, item, timeout=None): result = yield self.queue.put(item, timeout=timeout); raise StopIteration, result
class RTMPReader(Resource):
def __init__(self):
Resource.__init__(self)
self.type, self.mode, self._gen, self.timeout, self.stream = 'rtmp', 'r', None, None, ''
def open(self, url):
self.url, options = url, dict(map(lambda x: tuple(x.split('=', 1)+[''])[:2], url[7:].partition('?')[2].split('&')))
self.timeout, self.stream = int(options['timeout']) if 'timeout' in options else 10, options['id'] if 'id' in options else None
if not self.stream:
logger.error('missing id parameter in rtmp url')
raise StopIteration, None
logger.debug('RTMPReader.open timeout=%r stream=%r url=%r', self.timeout, self.stream, self.url)
self.nc = NetConnection(); result = yield self.nc.connect(self.url, timeout=self.timeout)
if not result: raise StopIteration, None
if self.stream:
self.ns = yield NetStream().create(self.nc, timeout=self.timeout)
result = yield self.ns.play(self.stream, timeout=self.timeout)
if not result: raise StopIteration, None
self._gen = self.run(); multitask.add(self._gen)
raise StopIteration, self
def close(self):
if self.nc is not None: yield self.nc.close(); self.nc = None
if self._gen is not None: self._gen.close()
def run(self):
try:
while True:
msg = yield self.ns.stream.queue.get(timeout=self.timeout, criteria=lambda x: x is None or x.type in (Message.AUDIO, Message.VIDEO))
yield self.queue.put(msg)
except multitask.Timeout:
logger.debug('RTMPReader.run() timedout')
yield self.queue.put(False)
class RTMPWriter(Resource):
def __init__(self):
Resource.__init__(self)
self.type, self.mode, self.timeout, self.stream = 'rtmp', 'w', None, ''
def open(self, url):
self.url, options = url, dict(map(lambda x: tuple(x.split('=', 1)+[''])[:2], url[7:].partition('?')[2].split('&')))
self.timeout, self.stream = int(options['timeout']) if 'timeout' in options else 10, options['id'] if 'id' in options else None
if not self.stream: raise StopIteration, None
logger.debug('RTMPWriter.open timeout=%r stream=%r url=%r', self.timeout, self.stream, self.url)
self.nc = NetConnection(); result = yield self.nc.connect(self.url, timeout=self.timeout)
if not result: raise StopIteration, None
if self.stream:
self.ns = yield NetStream().create(self.nc, timeout=self.timeout)
result = yield self.ns.publish(self.stream, timeout=self.timeout)
if not result: raise StopIteration, None
raise StopIteration, self
def close(self):
if self.nc is not None: yield self.nc.close(); self.nc = None
def put(self, item):
if self.ns is not None: yield self.ns.stream.send(item)
yield
class HTTPReader(Resource):
def open(self, url): raise StopIteration, False
class HTTPWriter(Resource):
def open(self, url): raise StopIteration, False
class FLVReader(Resource):
def __init__(self):
Resource.__init__(self)
self.type, self.mode, self._gen, self.id, self.client = 'file', 'r', None, 1, True
def open(self, url):
logger.debug('FLVReader.open %r', url)
yield
self.url, u = url, urlparse.urlparse(url, 'file')
self.fp = FLV().open(u.path)
if self.fp:
self._gen = self.fp.reader(self); multitask.add(self._gen)
raise StopIteration, self
else: raise StopIteration, None
def close(self):
if self.fp: self.fp.close(); self.fp = None
if self._gen is not None: self._gen.close()
yield
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
multitask.add(sendInternal(self, msg))
class FLVWriter(Resource):
def __init__(self):
Resource.__init__(self)
self.type, self.mode = 'file', 'w'
def open(self, url):
logger.debug('FLVWrite.open %r', url)
self.url, u = url, urlparse.urlparse(url, 'file')
self.fp = FLV().open(u.path, 'record'); yield
raise StopIteration, self if self.fp else None
def close(self):
if self.fp is not None: self.fp.close(); self.fp = None
yield
def put(self, item):
if self.fp is not None: self.fp.write(item)
yield
def open(url, mode='r'):
'''Open the given resource for read "r" or write "w" mode. Returns an object that has generator methods such as put(), get() and close().'''
type = 'rtmp' if str(url).startswith('rtmp://') else 'http' if str(url).startswith('http://') else 'file'
types = {'rtmp-r': RTMPReader, 'rtmp-w': RTMPWriter, 'http-r': HTTPReader, 'http-w': HTTPWriter, 'file-r': FLVReader, 'file-w': FLVWriter }
r = yield types[type + '-' + mode]().open(url=url)
raise StopIteration, r
def copy(src, dest):
'''Copy from given src url (str) to dest url (str).'''
s = yield open(src, 'r')
if not s: raise Result, (False, 'Cannot open source %r'%(src))
d = yield open(dest, 'w')
if not d: yield s.close(); raise Result, (False, 'Cannot open destination %r'%(dest))
result = (True, 'Completed')
try:
while True:
msg = yield s.get()
if not msg: break;
yield d.put(msg)
except Result, e: result = e
except KeyboardInterrupt: result = (True, 'Keyboard Interrupt')
yield s.close()
yield d.close()
raise Result, result
def _copy_loop(filename, ns, enableAudio, enableVideo):
'''Local function used by connect() to stream from file to NetStream in a loop.'''
reader = yield FLVReader().open(filename)
if not reader: raise StopIteration('Failed to open file %r'%(filename,))
try:
while True:
msg = yield reader.get()
if not msg:
logger.debug('Reached end, re-opening the file %r', filename)
reader.close()
reader = yield FLVReader().open(filename)
elif enableAudio and msg.type == Message.AUDIO or enableVideo and msg.type == Message.VIDEO:
yield ns.stream.send(msg)
except Result, e: logger.exception('copy %r', filename)
except GeneratorExit: pass
except: logger.exception('exception')
reader.close()
def connect(url, params=None, timeout=10, duration=60, publishStream='publish', playStream='play', publishFile=None, enableAudio=True, enableVideo=True):
'''Connect to the RTMP url with supplied parameters in NetConnection.connect. Once connected it publishes and plays the supplied
streams, and if publish file is supplied uses that to publish to the stream. The connection is kept up for the duration seconds.
Any attempt to connect, open streams or file is with supplied timeout. It returns an error string on failure or None on success.
Example to publish audio-only with three parameters from 'file1.flv':
result = yield connect("rtmp://server/app", ['str-param', None, 20], publish_file='file1.flv', enableVideo=False)
'''
logger.debug('connect url=%r params=%r timeout=%r duration=%r publishStream=%r playStream=%r publishFile=%r enableAudio=%r enableVideo=%r', url, params, timeout, duration, publishStream, playStream, publishFile, enableAudio, enableVideo)
nc = NetConnection()
result = yield nc.connect(url, timeout, *params)
if not result: raise StopIteration, 'Failed to connect %r'%(url,)
if publishStream:
ns1 = yield NetStream().create(nc, timeout=timeout)
if not ns1: raise StopIteration, 'Failed to create publish stream'
result = yield ns1.publish(publishStream, timeout=timeout)
if not result: yield nc.close(); raise StopIteration, 'Failed to create publish stream %r'%(publishStream,)
if playStream:
ns2 = yield NetStream().create(nc, timeout=timeout)
if not ns2: raise StopIteration, 'Failed to create play stream'
result = yield ns2.play(playStream, timeout=timeout)
if not result: yield nc.close(); raise StopIteration, 'Failed to create play stream %r'%(playStream,)
if publishFile and publishStream:
gen1 = _copy_loop(publishFile, ns1, enableAudio, enableVideo)
multitask.add(gen1)
try:
logger.debug('timeout=%r', duration)
yield nc.client.close_queue.get(timeout=duration)
logger.debug('received connection close')
if publishFile and publishStream: gen1.close()
if playFile and playStream: gen2.close()
except (multitask.Timeout, GeneratorExit):
logger.debug('duration completed, connect closing')
if publishFile and publishStream: gen1.close()
if playFile and playStream: gen2.close()
yield nc.close()
raise StopIteration(None)
_usage = '''usage: python rtmpclient.py [-d] src dest
-d: verbose mode prints trace statements
src and dest: either "rtmp" URL or a file name. Use "id" to specify stream name, e.g., rtmp://localhost/myapp?id=user1
This software depends on Python 2.7 (won't work with 2.4 or 3.0)'''
if __name__ == '__main__':
if sys.argv[-1] == '--test': sys.exit()
if len(sys.argv) < 3: print _usage; sys.exit(-1)
logging.basicConfig()
logger.setLevel(logging.DEBUG if sys.argv[1] == '-d' else logging.INFO)
try:
multitask.add(copy(sys.argv[-2], sys.argv[-1]))
multitask.run()
except Result, e:
logger.exception('result')
except KeyboardInterrupt:
logger.debug('keyboard interrupt')