org.jnetpcap.protocol.voip
Class Rtp

java.lang.Object
  extended by org.jnetpcap.nio.JMemory
      extended by org.jnetpcap.nio.JBuffer
          extended by org.jnetpcap.packet.JHeader
              extended by org.jnetpcap.protocol.voip.Rtp
All Implemented Interfaces:
JPayloadAccessor

@Header(spec="rfc3550",
        suite=VOIP,
        description="real-time transfer protocol")
public class Rtp
extends JHeader

The real-time transport protocol (RTP). RTP provides end-to-end network transport functions suitable for applications transmitting real-time data, such as audio, video or simulation data, over multicast or unicast network services. RTP does not address resource reservation and does not guarantee quality-of-service for real-time services. The data transport is augmented by a control protocol (RTCP) to allow monitoring of the data delivery in a manner scalable to large multicast networks, and to provide minimal control and identification functionality. RTP and RTCP are designed to be independent of the underlying transport and network layers. The protocol supports the use of RTP-level translators and mixers. *

Note that RTP itself does not provide any mechanism to ensure timely delivery or provide other quality-of-service guarantees, but relies on lower-layer services to do so. It does not guarantee delivery or prevent out-of-order delivery, nor does it assume that the underlying network is reliable and delivers packets in sequence. The sequence numbers included in RTP allow the receiver to reconstruct the sender's packet sequence, but sequence numbers might also be used to determine the proper location of a packet, for example in video decoding, without necessarily decoding packets in sequence.

The RTP header has the following format:

  0                   1                   2                   3
  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  |V=2|P|X|  CC   |M|     PT      |       sequence number         |
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  |                           timestamp                           |
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  |           synchronization source (SSRC) identifier            |
  +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
  |            contributing source (CSRC) identifiers             |
  |                             ....                              |
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 

While RTP is primarily designed to satisfy the needs of multi- participant multimedia conferences, it is not limited to that particular application. Storage of continuous data, interactive distributed simulation, active badge, and control and measurement applications may also find RTP applicable.

Author:
Mark Bednarczyk, Sly Technologies, Inc.

Nested Class Summary
static class Rtp.Extension
          An extension mechanism is provided to allow individual implementations to experiment with new payload-format-independent functions that require additional information to be carried in the RTP data packet header.
static class Rtp.PayloadType
          Constant payload types that have been defined for type field.
 
Nested classes/interfaces inherited from class org.jnetpcap.packet.JHeader
JHeader.State
 
Nested classes/interfaces inherited from class org.jnetpcap.nio.JMemory
JMemory.Type
 
Field Summary
static int CC_MASK
          Bitmask applied to byte 0 in the header which masks off the CSRC COUNT field.
static int CC_OFFSET
          Bit offset into byte 0 of the header for CSRC COUNT field.
static int CSRC_LENGTH
          Constant which defines the length of a CSRC entry in CSRC table in bytes.
static java.lang.String DESCRIPTION
          Constant containing a short description of this protocol header.
static int EXTENSION_MASK
          Bitmask applied to byte 0 in the header which masks off the extension bit option flag.
static int EXTENSION_OFFSET
          Bit offset into byte 0 of the header for EXTENSION field.
static int ID
          Registry assigned header ID.
static int MARKER_MASK
          Bitmask applied to byte 1 in the header which masks off the marker bit option flag.
static int MARKER_OFFSET
          Bit offset into byte 1 of the header for MARKER field.
static int PADDING_MASK
          Bitmask applied to byte 0 in the header which masks off the padding bit option flag.
static int PADDING_OFFSET
          Bit offset into byte 0 of the header for PADDING field.
static java.lang.String RFC
          Constant containing the name of the RFC that describes the specification of this header.
static int RTP_UDP_PORT
          Default RTP port number.
static int STATIC_HEADER_LENGTH
          Constant which defines the length of the static part of the header in bytes.
static ProtocolSuite SUITE
          Constant containing the name of the protocol suite this header belongs to.
static int TYPE_MASK
          Bitmask applied to byte 1 in the header which masks off the payload type field.
static int TYPE_OFFSET
          Bit offset into byte 1 of the header for PAYLOAD TYPE field.
static int VERSION_MASK
          Bitmask applied to byte 0 in the header which masks off the version number of the Rtp header within the packet.
static int VERSION_OFFSET
          Bit offset into byte 0 of the header for VERSION field.
 
Fields inherited from class org.jnetpcap.packet.JHeader
annotatedHeader, BYTE, EMPTY_HEADER_ARRAY, isSubHeader, packet, state
 
Fields inherited from class org.jnetpcap.nio.JMemory
JNETPCAP_LIBRARY_NAME, MAX_DIRECT_MEMORY_DEFAULT, POINTER
 
Constructor Summary
Rtp()
           
 
Method Summary
 int count()
          The CSRC count contains the number of CSRC identifiers that follow the fixed header.
 int[] csrc()
           The CSRC list identifies the contributing sources for the payload contained in this packet.
 int csrcLength()
          Calculates the length of the csrc field in bits.
 boolean hasExtension()
          If the extension bit is set, the fixed header MUST be followed by exactly one header extension, with a format defined in Section 5.3.1 of RFC3550
 boolean hasMarker()
          The interpretation of the marker is defined by a profile.
 boolean hasPadding()
          If the padding bit is set, the packet contains one or more additional padding octets at the end which are not part of the payload.
static int headerLength(JBuffer buffer, int offset)
          Determines the length of the header in octets.
 int paddingLength()
          Returns the number of padding bytes that were appended at the end of this Rtp frame.
static int postfixLength(JBuffer buffer, int offset)
          Determines the length of Rtp padding if the header has been padded.
 int sequence()
          The sequence number increments by one for each RTP data packet sent, and may be used by the receiver to detect packet loss and to restore packet sequence.
 long ssrc()
          The SSRC field identifies the synchronization source.
 long timestamp()
           The timestamp reflects the sampling instant of the first octet in the RTP data packet.
 int type()
          This field identifies the format of the RTP payload and determines its interpretation by the application.
 Rtp.PayloadType typeEnum()
          This field identifies the format of the RTP payload and determines its interpretation by the application.
 int version()
          This field identifies the version of RTP.
 
Methods inherited from class org.jnetpcap.packet.JHeader
decode, decodeHeader, getAnnotatedHeader, getDescription, getFields, getGap, getGapLength, getGapOffset, getHeader, getHeaderLength, getHeaderOffset, getId, getIndex, getLength, getName, getNextHeaderId, getNextHeaderOffset, getNicname, getOffset, getPacket, getParent, getPayload, getPayloadLength, getPayloadOffset, getPostfix, getPostfixLength, getPostfixOffset, getPrefix, getPrefixLength, getPrefixOffset, getPreviousHeaderId, getPreviousHeaderOffset, getState, getSubHeaders, hasDescription, hasGap, hasNextHeader, hasPayload, hasPostfix, hasPrefix, hasPreviousHeader, hasSubHeaders, isFragmented, isGapTruncated, isHeaderTruncated, isPayloadTruncated, isPostfixTruncated, isPrefixTruncated, peer, peer, peerPayloadTo, setPacket, setSubHeaders, sizeof, toString, transferPayloadTo, transferPayloadTo, transferPayloadTo, validateHeader
 
Methods inherited from class org.jnetpcap.nio.JBuffer
findUTF8String, getByte, getByteArray, getByteArray, getByteArray, getDouble, getFloat, getInt, getLong, getShort, getUByte, getUInt, getUShort, getUTF8Char, getUTF8String, getUTF8String, getUTF8String, getUTF8String, isReadonly, order, order, peer, peer, peer, peer, setByte, setByteArray, setByteBuffer, setDouble, setFloat, setInt, setLong, setShort, setShort0, setUByte, setUInt, setUShort, transferFrom, transferFrom, transferFrom, transferTo, transferTo, transferTo
 
Methods inherited from class org.jnetpcap.nio.JMemory
availableDirectMemory, check, cleanup, createReference, isInitialized, isJMemoryBasedOwner, isOwner, maxDirectMemory, peer, reservedDirectMemory, setSize, size, softDirectMemory, toDebugString, toHexdump, toHexdump, totalActiveAllocated, totalAllocateCalls, totalAllocated, totalAllocatedSegments0To255Bytes, totalAllocatedSegments256OrAbove, totalDeAllocateCalls, totalDeAllocated, transferFrom, transferFrom, transferFromDirect, transferOwnership, transferTo, transferTo, transferTo, transferTo, transferTo, transferTo0
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CC_MASK

public static final int CC_MASK
Bitmask applied to byte 0 in the header which masks off the CSRC COUNT field.

See Also:
Constant Field Values

CC_OFFSET

public static final int CC_OFFSET
Bit offset into byte 0 of the header for CSRC COUNT field.

See Also:
Constant Field Values

CSRC_LENGTH

public static final int CSRC_LENGTH
Constant which defines the length of a CSRC entry in CSRC table in bytes.

See Also:
Constant Field Values

DESCRIPTION

public static final java.lang.String DESCRIPTION
Constant containing a short description of this protocol header.

See Also:
Constant Field Values

EXTENSION_MASK

public static final int EXTENSION_MASK
Bitmask applied to byte 0 in the header which masks off the extension bit option flag.

See Also:
Constant Field Values

EXTENSION_OFFSET

public static final int EXTENSION_OFFSET
Bit offset into byte 0 of the header for EXTENSION field.

See Also:
Constant Field Values

ID

public static int ID
Registry assigned header ID.


MARKER_MASK

public static final int MARKER_MASK
Bitmask applied to byte 1 in the header which masks off the marker bit option flag.

See Also:
Constant Field Values

MARKER_OFFSET

public static final int MARKER_OFFSET
Bit offset into byte 1 of the header for MARKER field.

See Also:
Constant Field Values

PADDING_MASK

public static final int PADDING_MASK
Bitmask applied to byte 0 in the header which masks off the padding bit option flag.

See Also:
Constant Field Values

PADDING_OFFSET

public static final int PADDING_OFFSET
Bit offset into byte 0 of the header for PADDING field.

See Also:
Constant Field Values

RFC

public static final java.lang.String RFC
Constant containing the name of the RFC that describes the specification of this header.

See Also:
Constant Field Values

RTP_UDP_PORT

public static final int RTP_UDP_PORT
Default RTP port number.

See Also:
Constant Field Values

STATIC_HEADER_LENGTH

public static final int STATIC_HEADER_LENGTH
Constant which defines the length of the static part of the header in bytes.

See Also:
Constant Field Values

SUITE

public static final ProtocolSuite SUITE
Constant containing the name of the protocol suite this header belongs to.


TYPE_MASK

public static final int TYPE_MASK
Bitmask applied to byte 1 in the header which masks off the payload type field.

See Also:
Constant Field Values

TYPE_OFFSET

public static final int TYPE_OFFSET
Bit offset into byte 1 of the header for PAYLOAD TYPE field.

See Also:
Constant Field Values

VERSION_MASK

public static final int VERSION_MASK
Bitmask applied to byte 0 in the header which masks off the version number of the Rtp header within the packet.

See Also:
Constant Field Values

VERSION_OFFSET

public static final int VERSION_OFFSET
Bit offset into byte 0 of the header for VERSION field.

See Also:
Constant Field Values
Constructor Detail

Rtp

public Rtp()
Method Detail

headerLength

@HeaderLength
public static int headerLength(JBuffer buffer,
                                            int offset)
Determines the length of the header in octets. The value is calculated by adding to the length of the static part of the header the length of the CSRC table. The CC field contains number of 32-bit entries within the table.

Parameters:
buffer - buffer containing the header data
offset - offset within the buffer of the start of the header
Returns:
length of the header in bytes

postfixLength

@HeaderLength(value=POSTFIX)
public static int postfixLength(JBuffer buffer,
                                                         int offset)
Determines the length of Rtp padding if the header has been padded. If the Rtp.P bit is set, that means that last byte within the frame contains the number of bytes that were used to pad after the payload following this header.

Parameters:
buffer - buffer to read options and padding information from
offset - offset to the start of the header
Returns:
number of bytes padding rtp payload or 0 if no padding bytes

count

@Field(offset=4,
       length=4)
public int count()
The CSRC count contains the number of CSRC identifiers that follow the fixed header.

Returns:
number of 4 octect CSRC identifiers that follow the fixed header part

csrc

@Field(offset=96)
public int[] csrc()

The CSRC list identifies the contributing sources for the payload contained in this packet. The number of identifiers is given by the CC field. If there are more than 15 contributing sources, only 15 can be identified. CSRC identifiers are inserted by mixers (see Section 7.1), using the SSRC identifiers of contributing sources. For example, for audio packets the SSRC identifiers of all sources that were mixed together to create a packet are listed, allowing correct talker indication at the receiver.

Returns:
array which contains values of the csrc list field

csrcLength

@Dynamic(value=LENGTH)
public int csrcLength()
Calculates the length of the csrc field in bits. The length is calculated by using the count (CC) field and multiplying by 32 bits which is the length of each CSRC entry.

Returns:
length of the csrc field in bits

hasExtension

@Field(offset=3,
       length=1)
public boolean hasExtension()
If the extension bit is set, the fixed header MUST be followed by exactly one header extension, with a format defined in Section 5.3.1 of RFC3550

Returns:
value of the extension field

hasMarker

@Field(offset=8,
       length=1)
public boolean hasMarker()
The interpretation of the marker is defined by a profile. It is intended to allow significant events such as frame boundaries to be marked in the packet stream. A profile MAY define additional marker bits or specify that there is no marker bit by changing the number of bits in the payload type field (see Section 5.3 of RFC3550).

Returns:
value of the marker field

hasPadding

@Field(offset=2,
       length=1)
public boolean hasPadding()
If the padding bit is set, the packet contains one or more additional padding octets at the end which are not part of the payload. The last octet of the padding contains a count of how many padding octets should be ignored, including itself. Padding may be needed by some encryption algorithms with fixed block sizes or for carrying several RTP packets in a lower-layer protocol data unit.

Returns:
value of the padding field

paddingLength

public int paddingLength()
Returns the number of padding bytes that were appended at the end of this Rtp frame.

Returns:
number of padding bytes

sequence

@Field(offset=16,
       length=16)
public int sequence()
The sequence number increments by one for each RTP data packet sent, and may be used by the receiver to detect packet loss and to restore packet sequence. The initial value of the sequence number SHOULD be random (unpredictable) to make known-plaintext attacks on encryption more difficult, even if the source itself does not encrypt according to the method in Section 9.1 of RFC3550, because the packets may flow through a translator that does.

Returns:
value of the sequence number field

ssrc

@Field(offset=64,
       length=32)
public long ssrc()
The SSRC field identifies the synchronization source. This identifier SHOULD be chosen randomly, with the intent that no two synchronization sources within the same RTP session will have the same SSRC identifier. An example algorithm for generating a random identifier is presented in Appendix A.6. Although the probability of multiple sources choosing the same identifier is low, all RTP implementations must be prepared to detect and resolve collisions. Section 8 describes the probability of collision along with a mechanism for resolving collisions and detecting RTP-level forwarding loops based on the uniqueness of the SSRC identifier. If a source changes its source transport address, it must also choose a new SSRC identifier to avoid being interpreted as a looped source (see Section 8.2).

Returns:
value of the unsigned 32-bit ssrc field

timestamp

@Field(offset=32,
       length=32)
public long timestamp()

The timestamp reflects the sampling instant of the first octet in the RTP data packet. The sampling instant MUST be derived from a clock that increments monotonically and linearly in time to allow synchronization and jitter calculations (see Section 6.4.1). The resolution of the clock MUST be sufficient for the desired synchronization accuracy and for measuring packet arrival jitter (one tick per video frame is typically not sufficient). The clock frequency is dependent on the format of data carried as payload and is specified statically in the profile or payload format specification that defines the format, or MAY be specified dynamically for payload formats defined through non-RTP means. If RTP packets are generated periodically, the nominal sampling instant as determined from the sampling clock is to be used, not a reading of the system clock. As an example, for fixed-rate audio the timestamp clock would likely increment by one for each sampling period. If an audio application reads blocks covering 160 sampling periods from the input device, the timestamp would be increased by 160 for each such block, regardless of whether the block is transmitted in a packet or dropped as silent.

The initial value of the timestamp SHOULD be random, as for the sequence number. Several consecutive RTP packets will have equal timestamps if they are (logically) generated at once, e.g., belong to the same video frame. Consecutive RTP packets MAY contain timestamps that are not monotonic if the data is not transmitted in the order it was sampled, as in the case of MPEG interpolated video frames. (The sequence numbers of the packets as transmitted will still be monotonic.)

RTP timestamps from different media streams may advance at different rates and usually have independent, random offsets. Therefore, although these timestamps are sufficient to reconstruct the timing of a single stream, directly comparing RTP timestamps from different media is not effective for synchronization. Instead, for each medium the RTP timestamp is related to the sampling instant by pairing it with a timestamp from a reference clock (wallclock) that represents the time when the data corresponding to the RTP timestamp was sampled. The reference clock is shared by all media to be synchronized. The timestamp pairs are not transmitted in every data packet, but at a lower rate in RTCP SR packets as described in Section 6.4.

The sampling instant is chosen as the point of reference for the RTP timestamp because it is known to the transmitting endpoint and has a common definition for all media, independent of encoding delays or other processing. The purpose is to allow synchronized presentation of all media sampled at the same time.

Applications transmitting stored data rather than data sampled in real time typically use a virtual presentation timeline derived from wallclock time to determine when the next frame or other unit of each medium in the stored data should be presented. In this case, the RTP timestamp would reflect the presentation time for each unit. That is, the RTP timestamp for each unit would be related to the wallclock time at which the unit becomes current on the virtual presentation timeline. Actual presentation occurs some time later as determined by the receiver.

An example describing live audio narration of prerecorded video illustrates the significance of choosing the sampling instant as the reference point. In this scenario, the video would be presented locally for the narrator to view and would be simultaneously transmitted using RTP. The "sampling instant" of a video frame transmitted in RTP would be established by referencing its timestamp to the wallclock time when that video frame was presented to the narrator. The sampling instant for the audio RTP packets containing the narrator's speech would be established by referencing the same wallclock time when the audio was sampled. The audio and video may even be transmitted by different hosts if the reference clocks on the two hosts are synchronized by some means such as NTP. A receiver can then synchronize presentation of the audio and video packets by relating their RTP timestamps using the timestamp pairs in RTCP SR packets.

Returns:
value of the unsigned 32-bit timestamp field

type

@Field(offset=9,
       length=7)
public int type()
This field identifies the format of the RTP payload and determines its interpretation by the application. A profile MAY specify a default static mapping of payload type codes to payload formats. Additional payload type codes MAY be defined dynamically through non-RTP means (see Section 3). A set of default mappings for audio and video is specified in the companion RFC 3551 [1]. An RTP source MAY change the payload type during a session, but this field SHOULD NOT be used for multiplexing separate media streams (see Section 5.2 of RFC3550). A receiver MUST ignore packets with payload types that it does not understand.

Returns:
value of the payload type field

typeEnum

public Rtp.PayloadType typeEnum()
This field identifies the format of the RTP payload and determines its interpretation by the application. A profile MAY specify a default static mapping of payload type codes to payload formats. Additional payload type codes MAY be defined dynamically through non-RTP means (see Section 3). A set of default mappings for audio and video is specified in the companion RFC 3551 [1]. An RTP source MAY change the payload type during a session, but this field SHOULD NOT be used for multiplexing separate media streams (see Section 5.2 of RFC3550). A receiver MUST ignore packets with payload types that it does not understand.

Returns:
value of the payload type field as a constant

version

@Field(offset=0,
       length=2)
public int version()
This field identifies the version of RTP. The version defined by this specification is two (2). (The value 1 is used by the first draft version of RTP and the value 0 is used by the protocol initially implemented in the "vat" audio tool.)

Returns:
version number of rtp header