org.red5.server.net.rtmp

## Class RTMPConnection

- java.lang.Object
- - org.red5.server.AttributeStore
  - - org.red5.server.BaseConnection
    - - org.red5.server.net.rtmp.RTMPConnection

All Implemented Interfaces:
  IEventDispatcher, IEventHandler, IEventListener, IAttributeStore, ICastingAttributeStore, IConnection, ICoreObject, IServiceCapableConnection, IStreamCapableConnection, AttributeStoreMXBean

Direct Known Subclasses:
  BaseRTMPTConnection, RTMPMinaConnection

    public abstract class RTMPConnection
    extends BaseConnection
    implements IStreamCapableConnection, IServiceCapableConnection

RTMP connection. Stores information about client streams, data transfer channels, pending RPC calls, bandwidth configuration, used encoding (AMF0/AMF3), connection state (is alive, last ping time and ping result) and session.

### Nested Class Summary

### Nested classes/interfaces inherited from interface org.red5.server.api.IConnection

`IConnection.Encoding`

### Field Summary

Fields
`protected int` `bytesReadInterval` Data read interval
`protected int` `clientId`
`protected int` `limitType` Bandwidth limit type / enforcement.
`protected int` `nextBytesRead` Number of bytes to read next.
`static String` `RTMP_CONNECTION_KEY`
`static byte` `RTMP_ENCRYPTED` Marker byte for encrypted RTMP data.
`static String` `RTMP_HANDSHAKE`
`static byte` `RTMP_NON_ENCRYPTED` Marker byte for standard or non-encrypted RTMP data.
`static String` `RTMPE_CIPHER_IN` Cipher for RTMPE input
`static String` `RTMPE_CIPHER_OUT` Cipher for RTMPE output
`protected RTMP` `state` protocol state

### Fields inherited from class org.red5.server.BaseConnection

`basicScopes, client, closed, droppedMessages, host, params, path, readMessages, remoteAddress, remoteAddresses, remotePort, scope, sessionId, type, writtenMessages`

### Fields inherited from class org.red5.server.AttributeStore

`attributes`

### Fields inherited from interface org.red5.server.api.IConnection

`PERSISTENT, POLLING, TRANSIENT`

### Constructor Summary

Constructors
Constructor and Description
`RTMPConnection(String type)` Creates anonymous RTMP connection without scope.

### Method Summary

Methods
`void` `addClientStream(IClientStream stream)`
`void` `close()` Closes connection
`void` `closeChannel(int channelId)` Closes channel.
`boolean` `connect(IScope newScope, Object[] params)` Connect to another scope on server with given parameters
`OutputStream` `createOutputStream(int streamId)` Creates output stream object from stream id.
`protected String` `createStreamName()` Generates new stream name.
`void` `deleteStreamById(int streamId)` Deletes the stream with the given id.
`boolean` `equals(Object obj)`
`Channel` `getChannel(int channelId)` Return channel by id.
`long` `getClientBytesRead()` Get number of bytes the client reported to have received.
`IConnection.Encoding` `getEncoding()` Return AMF protocol encoding used by this connection.
`int` `getId()`
`int` `getInvokeId()` Generate next invoke id.
`int` `getLastPingTime()` Return roundtrip time of last ping command.
`int` `getNextAvailableChannelId()` Getter for next available channel id.
`IPendingServiceCall` `getPendingCall(int invokeId)` Get pending call service by id.
`long` `getPendingVideoMessages(int streamId)` Total number of video messages that are pending to be sent to a stream.
`long` `getReadBytes()` Total number of bytes read from the connection.
`RTMP` `getState()`
`byte` `getStateCode()`
`IClientStream` `getStreamByChannelId(int channelId)` Return stream by given channel id.
`IClientStream` `getStreamById(int id)` Get a stream by its id.
`int` `getStreamIdForChannel(int channelId)` Return stream id for given channel id.
`protected Collection<IClientStream>` `getStreams()` Getter for client streams.
`protected int` `getUsedStreamCount()` Getter for used stream count.
`long` `getWrittenBytes()` Total number of bytes written to the connection.
`int` `hashCode()`
`void` `invoke(IServiceCall call)` Invokes service using remoting call object
`void` `invoke(IServiceCall call, int channel)` Invoke service using call and channel
`void` `invoke(String method)` Invoke method by name
`void` `invoke(String method, IPendingServiceCallback callback)` Invoke method by name with callback
`void` `invoke(String method, Object[] params)` Invoke method with parameters
`void` `invoke(String method, Object[] params, IPendingServiceCallback callback)`
`boolean` `isChannelUsed(int channelId)` Checks whether channel is used.
`boolean` `isValidStreamId(int streamId)` Returns whether or not a given stream id is valid.
`protected void` `messageDropped()` Increases number of dropped messages.
`void` `messageReceived()` Increases number of read messages by one.
`void` `messageSent(Packet message)` Mark message as sent.
`IClientBroadcastStream` `newBroadcastStream(int streamId)` Create a broadcast stream.
`IPlaylistSubscriberStream` `newPlaylistSubscriberStream(int streamId)` Create a stream that can play a list.
`ISingleItemSubscriberStream` `newSingleItemSubscriberStream(int streamId)` Create a stream that can play only one item.
`void` `notify(IServiceCall call)`
`void` `notify(IServiceCall call, int channel)`
`void` `notify(String method)`
`void` `notify(String method, Object[] params)`
`protected abstract void` `onInactive()` Inactive state event handler.
`void` `ping()` Start measuring the roundtrip time for a packet on the connection.
`void` `ping(Ping ping)` Handler for ping event.
`void` `pingReceived(Ping pong)` Marks that ping back was received.
`void` `receivedBytesRead(int bytes)` Read number of received bytes.
`void` `registerDeferredResult(DeferredResult result)` Registers deferred result.
`void` `registerPendingCall(int invokeId, IPendingServiceCall call)` Register pending call (remote function call that is yet to finish).
`void` `rememberStreamBufferDuration(int streamId, int bufferDuration)`
`void` `removeClientStream(int streamId)`
`int` `reserveStreamId()` Return a reserved stream id for use.
`int` `reserveStreamId(int id)`
`IPendingServiceCall` `retrievePendingCall(int invokeId)` Retrieves and removes the pending call service by id.
`void` `sendPendingServiceCallsCloseError()` When the connection has been closed, notify any remaining pending service calls that they have failed because the connection is broken.
`void` `setBandwidth(int mbits)` Sets the bandwidth using a mbit/s value.
`void` `setId(int clientId)`
`void` `setMaxHandshakeTimeout(int maxHandshakeTimeout)` Set maximum time to wait for valid handshake in milliseconds.
`void` `setMaxInactivity(int maxInactivity)` Setter for maximum inactivity.
`void` `setPingInterval(int pingInterval)` Setter for ping interval.
`void` `setSchedulingService(ISchedulingService schedulingService)` Sets the scheduling service.
`void` `setState(RTMP state)`
`void` `setStateCode(byte code)`
`void` `setup(String host, String path, String sessionId, Map<String,Object> params)` Initialize connection.
`void` `startRoundTripMeasurement()` Starts measurement.
`protected void` `startWaitForHandshake(ISchedulingService service)` Start waiting for a valid handshake.
`String` `toString()`
`void` `unregisterDeferredResult(DeferredResult result)` Unregister deferred result
`void` `unreserveStreamId(int streamId)` Unreserve this id for future use.
`protected void` `updateBytesRead()` Update number of bytes to read next value.
`abstract void` `write(Packet out)` Write packet.
`abstract void` `writeRaw(IoBuffer out)` Write raw byte buffer.
`protected void` `writingMessage(Packet message)` Mark message as being written.

### Methods inherited from class org.red5.server.BaseConnection

`connect, dispatchEvent, getBasicScopes, getClient, getConnectParams, getDroppedMessages, getHost, getLock, getNextClientId, getPath, getPendingMessages, getReadMessages, getRemoteAddress, getRemoteAddresses, getRemotePort, getScope, getSessionId, getType, getWrittenMessages, handleEvent, initialize, isConnected, notifyEvent, registerBasicScope, unregisterBasicScope`

### Methods inherited from class org.red5.server.AttributeStore

`filterNull, from, getAttribute, getAttribute, getAttributeNames, getAttributes, getBoolAttribute, getByteAttribute, getDoubleAttribute, getIntAttribute, getListAttribute, getLongAttribute, getMapAttribute, getSetAttribute, getShortAttribute, getStringAttribute, hasAttribute, removeAttribute, removeAttributes, setAttribute, setAttributes, setAttributes`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface org.red5.server.api.IConnection

`connect, getBasicScopes, getClient, getConnectParams, getDroppedMessages, getHost, getPath, getPendingMessages, getReadMessages, getRemoteAddress, getRemoteAddresses, getRemotePort, getScope, getSessionId, getType, getWrittenMessages, initialize, isConnected`

### Methods inherited from interface org.red5.server.api.event.IEventDispatcher

`dispatchEvent`

### Methods inherited from interface org.red5.server.api.event.IEventHandler

`handleEvent`

### Methods inherited from interface org.red5.server.api.event.IEventListener

`notifyEvent`

### Methods inherited from interface org.red5.server.api.ICastingAttributeStore

`getBoolAttribute, getByteAttribute, getDoubleAttribute, getIntAttribute, getListAttribute, getLongAttribute, getMapAttribute, getSetAttribute, getShortAttribute, getStringAttribute`

### Methods inherited from interface org.red5.server.api.IAttributeStore

`getAttribute, getAttribute, getAttributeNames, getAttributes, hasAttribute, removeAttribute, removeAttributes, setAttribute, setAttributes, setAttributes`

### Field Detail

#### RTMP_CONNECTION_KEY

    public static final String RTMP_CONNECTION_KEY

See Also:
  Constant Field Values

#### RTMP_HANDSHAKE

    public static final String RTMP_HANDSHAKE

See Also:
  Constant Field Values

#### RTMP_NON_ENCRYPTED

    public static final byte RTMP_NON_ENCRYPTED

Marker byte for standard or non-encrypted RTMP data.

See Also:
  Constant Field Values

#### RTMP_ENCRYPTED

    public static final byte RTMP_ENCRYPTED

Marker byte for encrypted RTMP data.

See Also:
  Constant Field Values

#### RTMPE_CIPHER_IN

    public static final String RTMPE_CIPHER_IN

Cipher for RTMPE input

See Also:
  Constant Field Values

#### RTMPE_CIPHER_OUT

    public static final String RTMPE_CIPHER_OUT

Cipher for RTMPE output

See Also:
  Constant Field Values

#### bytesReadInterval

    protected int bytesReadInterval

Data read interval

#### nextBytesRead

    protected int nextBytesRead

Number of bytes to read next.

#### limitType

    protected int limitType

Bandwidth limit type / enforcement. (0=hard,1=soft,2=dynamic)

#### clientId

    protected volatile int clientId

#### state

    protected volatile RTMP state

protocol state

### Constructor Detail

#### RTMPConnection

    @ConstructorProperties(value="type")
    public RTMPConnection(String type)

Creates anonymous RTMP connection without scope.

Parameters:
  `type` - Connection type

### Method Detail

#### getId

    public int getId()

#### setId

    public void setId(int clientId)

#### getState

    public RTMP getState()

#### getStateCode

    public byte getStateCode()

#### setStateCode

    public void setStateCode(byte code)

#### setState

    public void setState(RTMP state)

#### setBandwidth

    public void setBandwidth(int mbits)

Sets the bandwidth using a mbit/s value.

Specified by:
  `setBandwidth` in interface `IConnection`

#### connect

    public boolean connect(IScope newScope,
                  Object[] params)

Description copied from class: `BaseConnection`
Connect to another scope on server with given parameters

Specified by:
  `connect` in interface `IConnection`
Overrides:
  `connect` in class `BaseConnection`
Parameters:
  `newScope` - New scope
  `params` - Parameters to connect with
Returns:
  true on success, false otherwise

#### setup

    public void setup(String host,
             String path,
             String sessionId,
             Map<String,Object> params)

Initialize connection.

Parameters:
  `host` - Connection host
  `path` - Connection path
  `sessionId` - Connection session id
  `params` - Params passed from client

#### getEncoding

    public IConnection.Encoding getEncoding()

Return AMF protocol encoding used by this connection.

Specified by:
  `getEncoding` in interface `IConnection`
Returns:
  AMF encoding used by connection

#### getNextAvailableChannelId

    public int getNextAvailableChannelId()

Getter for next available channel id.

Returns:
  Next available channel id

#### isChannelUsed

    public boolean isChannelUsed(int channelId)

Checks whether channel is used.

Parameters:
  `channelId` - Channel id
Returns:
  `true` if channel is in use, `false` otherwise

#### getChannel

    public Channel getChannel(int channelId)

Return channel by id.

Parameters:
  `channelId` - Channel id
Returns:
  Channel by id

#### closeChannel

    public void closeChannel(int channelId)

Closes channel.

Parameters:
  `channelId` - Channel id

#### getStreams

    protected Collection<IClientStream> getStreams()

Getter for client streams.

Returns:
  Client streams as array

#### reserveStreamId

    public int reserveStreamId()

Return a reserved stream id for use. According to FCS/FMS regulation, the base is 1.

Specified by:
  `reserveStreamId` in interface `IStreamCapableConnection`
Returns:
  Reserved stream id

#### reserveStreamId

    public int reserveStreamId(int id)

Specified by:
  `reserveStreamId` in interface `IStreamCapableConnection`

#### isValidStreamId

    public boolean isValidStreamId(int streamId)

Returns whether or not a given stream id is valid.

Parameters:
  `streamId` -
Returns:
  true if its valid, false if its invalid

#### createOutputStream

    public OutputStream createOutputStream(int streamId)

Creates output stream object from stream id. Output stream consists of audio, data and video channels.

Parameters:
  `streamId` - Stream id
Returns:
  Output stream object
See Also:
  `OutputStream`

#### newBroadcastStream

    public IClientBroadcastStream newBroadcastStream(int streamId)

Create a broadcast stream.

Specified by:
  `newBroadcastStream` in interface `IStreamCapableConnection`
Parameters:
  `streamId` - Stream id
Returns:
  New broadcast stream

#### newSingleItemSubscriberStream

    public ISingleItemSubscriberStream newSingleItemSubscriberStream(int streamId)

Create a stream that can play only one item.

Specified by:
  `newSingleItemSubscriberStream` in interface `IStreamCapableConnection`
Parameters:
  `streamId` - Stream id
Returns:
  New subscriber stream that can play only one item

#### newPlaylistSubscriberStream

    public IPlaylistSubscriberStream newPlaylistSubscriberStream(int streamId)

Create a stream that can play a list.

Specified by:
  `newPlaylistSubscriberStream` in interface `IStreamCapableConnection`
Parameters:
  `streamId` - Stream id
Returns:
  New stream that can play sequence of items

#### addClientStream

    public void addClientStream(IClientStream stream)

#### removeClientStream

    public void removeClientStream(int streamId)

#### getUsedStreamCount

    protected int getUsedStreamCount()

Getter for used stream count.

Returns:
  Value for property 'usedStreamCount'.

#### getStreamById

    public IClientStream getStreamById(int id)

Get a stream by its id.

Specified by:
  `getStreamById` in interface `IStreamCapableConnection`
Parameters:
  `id` - Stream id
Returns:
  Stream with given id

#### getStreamIdForChannel

    public int getStreamIdForChannel(int channelId)

Return stream id for given channel id.

Parameters:
  `channelId` - Channel id
Returns:
  ID of stream that channel belongs to

#### getStreamByChannelId

    public IClientStream getStreamByChannelId(int channelId)

Return stream by given channel id.

Parameters:
  `channelId` - Channel id
Returns:
  Stream that channel belongs to

#### close

    public void close()

Closes connection

Specified by:
  `close` in interface `IConnection`
Overrides:
  `close` in class `BaseConnection`

#### sendPendingServiceCallsCloseError

    public void sendPendingServiceCallsCloseError()

When the connection has been closed, notify any remaining pending service calls that they have failed because the connection is broken. Implementors of IPendingServiceCallback may only deduce from this notification that it was not possible to read a result for this service call. It is possible that (1) the service call was never written to the service, or (2) the service call was written to the service and although the remote method was invoked, the connection failed before the result could be read, or (3) although the remote method was invoked on the service, the service implementor detected the failure of the connection and performed only partial processing. The caller only knows that it cannot be confirmed that the callee has invoked the service call and returned a result.

#### unreserveStreamId

    public void unreserveStreamId(int streamId)

Unreserve this id for future use.

Specified by:
  `unreserveStreamId` in interface `IStreamCapableConnection`
Parameters:
  `streamId` - ID of stream to unreserve

#### deleteStreamById

    public void deleteStreamById(int streamId)

Deletes the stream with the given id.

Specified by:
  `deleteStreamById` in interface `IStreamCapableConnection`
Parameters:
  `streamId` - ID of stream to delete

#### ping

    public void ping(Ping ping)

Handler for ping event.

Parameters:
  `ping` - Ping event context

#### write

    public abstract void write(Packet out)

Write packet.

Parameters:
  `out` - Packet

#### writeRaw

    public abstract void writeRaw(IoBuffer out)

Write raw byte buffer.

Parameters:
  `out` - IoBuffer

#### updateBytesRead

    protected void updateBytesRead()

Update number of bytes to read next value.

#### receivedBytesRead

    public void receivedBytesRead(int bytes)

Read number of received bytes.

Parameters:
  `bytes` - Number of bytes

#### getClientBytesRead

    public long getClientBytesRead()

Get number of bytes the client reported to have received.

Specified by:
  `getClientBytesRead` in interface `IConnection`
Overrides:
  `getClientBytesRead` in class `BaseConnection`
Returns:
  Number of bytes
See Also:
  `BytesRead`

#### invoke

    public void invoke(IServiceCall call)

Invokes service using remoting call object

Specified by:
  `invoke` in interface `IServiceCapableConnection`
Parameters:
  `call` - Service call object

#### getInvokeId

    public int getInvokeId()

Generate next invoke id.

Returns:
  Next invoke id for RPC

#### registerPendingCall

    public void registerPendingCall(int invokeId,
                           IPendingServiceCall call)

Register pending call (remote function call that is yet to finish).

Parameters:
  `invokeId` - Deferred operation id
  `call` - Call service

#### invoke

    public void invoke(IServiceCall call,
              int channel)

Invoke service using call and channel

Specified by:
  `invoke` in interface `IServiceCapableConnection`
Parameters:
  `call` - Service call
  `channel` - Channel used

#### invoke

    public void invoke(String method)

Invoke method by name

Specified by:
  `invoke` in interface `IServiceCapableConnection`
Parameters:
  `method` - Called method name

#### invoke

    public void invoke(String method,
              Object[] params)

Invoke method with parameters

Specified by:
  `invoke` in interface `IServiceCapableConnection`
Parameters:
  `method` - Method name
  `params` - Invocation parameters passed to method

#### invoke

    public void invoke(String method,
              IPendingServiceCallback callback)

Invoke method by name with callback

Specified by:
  `invoke` in interface `IServiceCapableConnection`
Parameters:
  `method` - Called method name
  `callback` - Callback

#### invoke

    public void invoke(String method,
              Object[] params,
              IPendingServiceCallback callback)

Specified by:
  `invoke` in interface `IServiceCapableConnection`

#### notify

    public void notify(IServiceCall call)

Specified by:
  `notify` in interface `IServiceCapableConnection`

#### notify

    public void notify(IServiceCall call,
              int channel)

Specified by:
  `notify` in interface `IServiceCapableConnection`

#### notify

    public void notify(String method)

Specified by:
  `notify` in interface `IServiceCapableConnection`

#### notify

    public void notify(String method,
              Object[] params)

Specified by:
  `notify` in interface `IServiceCapableConnection`

#### getReadBytes

    public long getReadBytes()

Total number of bytes read from the connection.

Specified by:
  `getReadBytes` in interface `IConnection`
Specified by:
  `getReadBytes` in class `BaseConnection`
Returns:
  bytes read

#### getWrittenBytes

    public long getWrittenBytes()

Total number of bytes written to the connection.

Specified by:
  `getWrittenBytes` in interface `IConnection`
Specified by:
  `getWrittenBytes` in class `BaseConnection`
Returns:
  bytes written

#### getPendingCall

    public IPendingServiceCall getPendingCall(int invokeId)

Get pending call service by id.

Parameters:
  `invokeId` - Pending call service id
Returns:
  Pending call service object

#### retrievePendingCall

    public IPendingServiceCall retrievePendingCall(int invokeId)

Retrieves and removes the pending call service by id.

Parameters:
  `invokeId` - Pending call service id
Returns:
  Pending call service object

#### createStreamName

    protected String createStreamName()

Generates new stream name.

Returns:
  New stream name

#### writingMessage

    protected void writingMessage(Packet message)

Mark message as being written.

Parameters:
  `message` - Message to mark

#### messageReceived

    public void messageReceived()

Increases number of read messages by one. Updates number of bytes read.

#### messageSent

    public void messageSent(Packet message)

Mark message as sent.

Parameters:
  `message` - Message to mark

#### messageDropped

    protected void messageDropped()

Increases number of dropped messages.

#### getPendingVideoMessages

    public long getPendingVideoMessages(int streamId)

Total number of video messages that are pending to be sent to a stream.

Specified by:
  `getPendingVideoMessages` in interface `IStreamCapableConnection`
Overrides:
  `getPendingVideoMessages` in class `BaseConnection`
Parameters:
  `streamId` - the id you want to know about
Returns:
  pending messages for this streamId

#### ping

    public void ping()

Start measuring the roundtrip time for a packet on the connection.

Specified by:
  `ping` in interface `IConnection`

#### pingReceived

    public void pingReceived(Ping pong)

Marks that ping back was received.

Parameters:
  `pong` - Ping object

#### getLastPingTime

    public int getLastPingTime()

Return roundtrip time of last ping command.

Specified by:
  `getLastPingTime` in interface `IConnection`
Returns:
  roundtrip time in milliseconds

#### setPingInterval

    public void setPingInterval(int pingInterval)

Setter for ping interval.

Parameters:
  `pingInterval` - Interval in ms to ping clients. Set to `0` to disable ghost detection code.

#### setMaxInactivity

    public void setMaxInactivity(int maxInactivity)

Setter for maximum inactivity.

Parameters:
  `maxInactivity` - Maximum time in ms after which a client is disconnected in case of inactivity.

#### startRoundTripMeasurement

    public void startRoundTripMeasurement()

Starts measurement.

#### setSchedulingService

    public void setSchedulingService(ISchedulingService schedulingService)

Sets the scheduling service.

Parameters:
  `schedulingService` - scheduling service

#### onInactive

    protected abstract void onInactive()

Inactive state event handler.

#### toString

    public String toString()

Overrides:
  `toString` in class `Object`

#### registerDeferredResult

    public void registerDeferredResult(DeferredResult result)

Registers deferred result.

Parameters:
  `result` - Result to register

#### unregisterDeferredResult

    public void unregisterDeferredResult(DeferredResult result)

Unregister deferred result

Parameters:
  `result` - Result to unregister

#### rememberStreamBufferDuration

    public void rememberStreamBufferDuration(int streamId,
                                    int bufferDuration)

#### setMaxHandshakeTimeout

    public void setMaxHandshakeTimeout(int maxHandshakeTimeout)

Set maximum time to wait for valid handshake in milliseconds.

Parameters:
  `maxHandshakeTimeout` - Maximum time in milliseconds

#### startWaitForHandshake

    protected void startWaitForHandshake(ISchedulingService service)

Start waiting for a valid handshake.

Parameters:
  `service` - The scheduling service to use

#### hashCode

    public int hashCode()

Overrides:
  `hashCode` in class `BaseConnection`

#### equals

    public boolean equals(Object obj)

Overrides:
  `equals` in class `BaseConnection`