fi.helsinki.cs.ohtu.mpeg2
Class PESPacket

java.lang.Object
  extended by fi.helsinki.cs.ohtu.mpeg2.PESPacket
Direct Known Subclasses:
AudioVideoPESPacket, PaddingPESPacket

public class PESPacket
extends java.lang.Object

Represents a MPEG PES packet. Data from elementary MPEG streams is encapsulated to PES packets for multiplexing in a transport or program stream. Each PES packet carries data from a single stream. PES packets have a short header common to all stream types, and may have additional type-specific header fields. This class represents the common part - subclasses exist for the different stream types with additional fields and semantics.


Nested Class Summary
private  class PESPacket.ByteAppenderOutputStream
           
 
Field Summary
private  java.util.ArrayList<java.lang.Integer> bytes
           
private  java.util.ArrayList<PESPacketAboutToResetListener> resetListeners
           
private  BitOutputStream stream
           
private  StreamID streamID
           
 
Constructor Summary
PESPacket(StreamID streamID)
          Class constructor.
 
Method Summary
 void addAboutToResetListener(PESPacketAboutToResetListener listener)
          Adds an about-to-reset listener.
 PESPacketAboutToResetListener[] getAboutToResetListeners()
          Returns an array of all the about-to-reset listeners registered on this packet.
 int getPacketLength()
          Gets the packet length (not counting the common header part).
 BitOutputStream getStream()
          Gets a bit-by-bit output stream for writing the payload to the packet.
 StreamID getStreamID()
          Gets the ID of the stream the data in the packet belongs to.
 int getTotalLength()
          Returns the packet total length, including the common header.
protected  void insertTemplateBytes()
          Inserts any stream-type specific header fields to the byte stream.
 void removeAboutToResetListener(PESPacketAboutToResetListener listener)
          Removes the specified about-to-reset listener so that it no longer receives events from this packet.
 void reset()
          Resets the packet to its initial state.
 void writeTo(BitOutputStream stream)
          Writes the packet to a stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

streamID

private final StreamID streamID

bytes

private final java.util.ArrayList<java.lang.Integer> bytes

stream

private BitOutputStream stream

resetListeners

private java.util.ArrayList<PESPacketAboutToResetListener> resetListeners
Constructor Detail

PESPacket

public PESPacket(StreamID streamID)
Class constructor.

Parameters:
streamID - ID of the stream the data in the packet belongs to.
Method Detail

getStreamID

public final StreamID getStreamID()
Gets the ID of the stream the data in the packet belongs to.

Returns:
The ID.

getPacketLength

public final int getPacketLength()
Gets the packet length (not counting the common header part). PES packets have a maximum payload length of 65535. Therefore, it shouldn't be attempted to write more payload data than that to the packet.

Returns:
The length, in bytes. Will be in range [0..65535].

getTotalLength

public final int getTotalLength()
Returns the packet total length, including the common header.

Returns:
The length, in bytes
See Also:
getPacketLength()

getStream

public final BitOutputStream getStream()
Gets a bit-by-bit output stream for writing the payload to the packet. As the stream is shared, it should not be attempted to be closed.

Returns:
The stream.

reset

public final void reset()
Resets the packet to its initial state. Before anything else is done, all about-to-reset listeners added using #addAboutToResetListener() are invoked with this packet as the argument to the PESPacketAboutToResetListener#aboutToReset() method. Afterwards, the currently written payload, along with any bits lingering in the payload writing stream buffer (as returned by getStream()) will be discarded. This also includes any template bytes, however, #insertTemplateBytes() is called after everything has been cleared to re-add them (in their possibly updated value). Therefore, most subclass header field setters should probably do a reset to make their change take effect.


writeTo

public final void writeTo(BitOutputStream stream)
                   throws java.io.IOException
Writes the packet to a stream. Packets with a zero-length payload are illegal. Therefore, this method MUST not be called for packets into which no payload data has been inserted (either by a subclass writing additional headers or manually writing packet data).

Parameters:
stream - The stream to write to.
Throws:
java.io.IOException - Propagated from the stream write.

addAboutToResetListener

public final void addAboutToResetListener(PESPacketAboutToResetListener listener)
Adds an about-to-reset listener.

Parameters:
listener - A class implementing the #PESPacketAboutToResetListener interface to register as the listener.

getAboutToResetListeners

public final PESPacketAboutToResetListener[] getAboutToResetListeners()
Returns an array of all the about-to-reset listeners registered on this packet.

Returns:
All of this packet's about-to-reset listeners or an empty array if there are none.

removeAboutToResetListener

public final void removeAboutToResetListener(PESPacketAboutToResetListener listener)
Removes the specified about-to-reset listener so that it no longer receives events from this packet. If listener is null, nothing happens.

Parameters:
listener - The listener to remove.

insertTemplateBytes

protected void insertTemplateBytes()
Inserts any stream-type specific header fields to the byte stream. This should be overridden by any subclasses wishing to include additional header fields in the start of the payload. It will be called automatically by reset() after clearing the previous payload. However, it's not automatically called when constructing an instance to avoid calling the overridden method before the subclass is fully constructed.