1
# Copyright (c) 2007-2009 The RTMPy Project.
2
# See LICENSE for details.
5
Interface documentation for RTMP primitives.
10
from zope.interface import Interface, Attribute
13
class IHeader(Interface):
18
channelId = Attribute("An C{int} representing the linked channel.")
20
"A C{bool} which is C{True} if this header is relative to the "
21
"previous. If C{False} then the header completely replaces the "
22
"previous. If this value is C{False} then all other attributes on "
23
"the header are guaranteed to be populated.")
24
timestamp = Attribute(
25
"An C{int} time value - not sure what this represents atm.")
27
"The datatype for the corresponding channel. See "
28
"U{RTMP datatypes on OSFlash<http://osflash.org/documentation/rtmp#rtmp_datatypes>} "
29
"for a list of possibles.")
30
bodyLength = Attribute(
31
"An C{int} which represents the length of the channel body.")
33
"An C{int} representing the linked stream.")
36
class IChannel(Interface):
38
An RTMP channel. A channel acts as an intermediary between two endpoints,
39
as well as providing context information for protocol en/decoding.
43
"The total number of bytes that have been routed through this "
46
"The total number of frames that have been routed through this "
48
bodyRemaining = Attribute(
49
"The number of bytes that remain until this channel's body is "
50
"considered complete.")
52
"An L{IChannelObserver} object that listens for events on this "
55
def registerManager(manager):
57
Registers a channel manager.
59
@param manager: L{IChannelManager}
60
@raise TypeError: If manager does not provide L{IChannelManager}
63
def registerObserver(observer):
65
Registers an observer for this channel. If the channel is buffering
66
data then the observer must be notified immediately.
68
@param observer: L{IChannelObserver}
69
@raise TypeError: If observer does not provide L{IChannelObserver}
74
Returns the header for this channel. Returns C{None} if no header was
75
applied to this channel.
77
@rtype: L{IHeader} or C{None}.
80
def setHeader(header):
82
Sets the header for this channel. If the header is relative, then it
83
is 'merged' with the last absolute header. If no header has been
84
applied to this channel then L{IChannelManager.initialiseChannel} must
87
@param header: The header to apply to this channel.
88
@type header: L{IHeader}
89
@raise TypeError: If C{header} does not provide L{IHeader}
92
def dataReceived(data):
94
Called when data has been received for this channel. The channel must
95
buffer the data until an observer has been registered.
102
Called to reset the context information. Called when after a channel
103
completes its body. This function should reset all contextual values
108
class IChannelManager(Interface):
110
The channel manager handles the interactions between its registered
111
channels and the outside world.
114
frameSize = Attribute(
115
"A read-only C{int} that defines the size (in bytes) of each frame "
116
"body. Set the frameSize via L{setFrameSize}")
118
def getChannel(channelId):
120
Returns a L{IChannel} object based on the channeId.
122
@raise IndexError: C{channelId} is not in range.
126
def getNextAvailableChannelId():
128
Returns the next available channel id.
131
@raise OverflowError: There are no free channels.
134
def channelComplete(channel):
136
Called when enough data has been written to the channel to satisfy
137
the body. The manager's job is to act appropriately on the event.
140
def initialiseChannel(channel):
142
Called when a channel needs to be initialised to begin accepting data.
143
This method must call L{IChannel.reset}.
145
@param channel: The channel to initialise.
146
@type channel: L{IChannel}
149
def setFrameSize(size):
151
Called to set the frame size, informing all registered channels of the
154
@param size: The new frame size.
159
class IChannelObserver(Interface):
161
Observes L{IChannel} events.
164
def dataReceived(data):
166
Called when the channel receives some data.
168
@param data: The data received by the channel.
174
Called when the amount of data received by the channel matches that
178
def headerChanged(header):
180
A new header was set on the channel.
182
@param header: The new relative header.
183
@type header: L{IHeader}
187
class IChannelScheduler(Interface):
189
A channel scheduler is meant to iteratively supply 'active' channels via
190
the L{getNextChannel} method. Used for RTMP encoding.
193
def activateChannel(channel):
195
Activates a channel for scheduling.
197
@param channel: The channel to activate.
198
@type channel: L{IChannel}
201
def deactivateChannel(channel):
203
Deactivates a channel for scheduling.
205
@param channel: The channel to deactivate.
206
@type channel: L{IChannel}
209
def getNextChannel():
211
Returns the next active channel. The definition of 'next' is up to the
212
implementing class. If there are no more active channels then C{None}
217
class ICodec(Interface):
221
deferred = Attribute("")
235
def registerObserver(observer):
238
@type observer: L{ICodecObserver}
242
class ICodecObserver(Interface):
244
Observes RTMP codec events.
249
Called when the codec has re/started.
254
Called when encoding has paused.
258
class IHandshakeObserver(Interface):
260
Observes handshake events.
263
def handshakeSuccess():
265
Handshaking was successful.
268
def handshakeFailure(reason):
272
@param reason: Why the handshake failed.
273
@type reason: Exception wrapped L{Failure}
278
Called when the handshake negotiator writes some data.
282
class IHandshakeNegotiator(Interface):
284
Negotiates handshakes.
287
observer = Attribute(
288
"An L{IHandshakeObserver} that listens for events from this "
291
"The server handshake token. Can be L{ServerToken} or C{None}")
293
"The client handshake token. Can be L{ServerToken} or C{None}")
295
def start(uptime=None, version=None):
297
Called to start the handshaking process. You can supply the uptime and
298
version, otherwise they will be worked out automatically. The version
299
specifically will be set to enable H.264 streaming.
302
def dataReceived(self, data):
304
Called when handshaking data has been received.
308
class IEvent(Interface):
312
@see: U{RTMP datatypes on OSFlash (external)
313
<http://osflash.org/documentation/rtmp#rtmp_datatypes>}
318
Encodes the event instance to C{bbs}. Can return a L{defer.Deferred}.
320
@param bbs: A stream object to write to.
321
@type bbs: L{rtmpy.util.BufferedByteStream}
326
Decodes the event instance from C{bbs}. Can return a L{defer.Deferred}.
328
@param bbs: A stream object to read from.
329
@type bbs: L{rtmpy.util.BufferedByteStream}
332
def dispatch(listener):
334
Dispatch the event to the listener. Calls the correct method with the
335
correct args according to L{IEventListener}.
337
@param listener: Receives the event dispatch request.
338
@type listener: L{IEventListener}
339
@return: Whatever is returned by the call to C{listener}.
344
class IEventListener(Interface):
346
Receives dispatched events.
349
def onInvoke(invoke):
351
Called when an invoke event have been received.
353
@param invoke: The object representing the call request. See
354
L{rtmpy.rtmp.event.Invoke} for an example implementation.
355
@return: The response, or a L{defer.Deferred} that will return the
359
def onNotify(notify):
361
Similar to L{onInvoke} but no response is expected and will be
364
@param notify: The object representing the notify request.
368
def onFrameSize(size):
370
Called when the RTMP frame size has changed.
372
@param size: The new size of the frames.
376
def onBytesRead(bytes):
378
Called when the connected endpoint reports the number of raw bytes
379
read from the stream.
381
@param bytes: The number of bytes read.
385
def onControlMessage(message):
387
Called when a control message is received by the connected endpoint.
389
@param message: The received message.
390
@type message: L{rtmpy.rtmp.event.ControlMessage}
393
def onDownstreamBandwidth(bandwidth):
395
Called when the connected endpoint reports its downstream bandwidth
398
@param bandwidth: The amount of bandwidth available (it appears to be
400
@type bandwidth: C{int}
403
def onUpstreamBandwidth(bandwidth, extra):
405
Called when the connected endpoint reports its upstream bandwidth
408
@param bandwidth: The amount of bandwidth available (it appears to be
410
@type bandwidth: C{int}
411
@param extra: Not quite sure what this represents atm.
415
def onAudioData(data):
417
Called when audio data is received.
419
@param data: The raw data received on the audio channel.
423
def onVideoData(data):
425
Called when video data is received.
427
@param data: The raw data received on the video channel.
432
class IConsumingStream(Interface):
434
Deals with part of a stream that linked with decoding RTMP events.
437
def channelRegistered(channel):
439
Called when a channel has registered itself to this stream.
441
@type channel: L{IChannel}
444
def channelUnregistered(channel):
446
Called when a channel has unregistered itself from this channel
448
@type channel: L{IChannel}
451
def dispatchEvent(event, channel):
453
Called to dispatch an event to the stream.
455
@type event: L{IEvent}
456
@param channel: The channel that the event was generated from.
457
@type channel: L{IChannel}
461
class IProducingStream(Interface):
463
Deals with part of a stream that linked with encoding RTMP events.
466
def registerChannel(channel):
468
Called to register a channel to this stream.
470
@type channel: L{IChannel}
473
def unregisterChannel(channel):
475
Called to unregister a channel from this stream.
477
@type channel: L{IChannel}
480
def writeEvent(event, channel=None):
482
Write an event to the stream. If channel is C{None} then one will be
485
@type event: L{IEvent}
486
@param channel: The channel that the event was generated from.
487
@type channel: L{IChannel}
491
class IStreamable(Interface):
493
Flags the implementing class as streaming. Used for marking audio/video
494
data events so that any observers are immediately notified.
498
class IStreamManager(Interface):
500
A manager that handles RTMP streams.
503
def registerStream(streamId, stream):
505
Registers a L{IStream} instance to the manager, based on the C{streamId}.
507
@param streamId: The id used to identify the stream to the manager.
508
@type streamId: C{int}
509
@param stream: The stream instance.
510
@type stream: L{interfaces.IStream}
511
@raise ValueError: C{streamId} is not in the correct range.
512
@raise TypeError: C{stream} does not implement L{interfaces.IStream}.
513
@raise IndexError: C{streamId} is already registered to another stream.
516
def removeStream(streamId):
518
Removes the stream from this manager.
520
@param streamId: The id used to identify the stream to the manager.
521
@type streamId: C{int}
522
@return: The stream object that has been removed.
524
@raise ValueError: C{streamId} is not in the correct range.
525
@raise IndexError: C{streamId} does not have a stream registered to it.
528
def getStream(streamId):