AVPKit
com::avpkit::core::Packet Class Reference
Inheritance diagram for com::avpkit::core::Packet:
Collaboration diagram for com::avpkit::core::Packet:

Public Member Functions

 VS_JNIUTILS_REFCOUNTED_OBJECT (Packet)
 
virtual int64_t getTimeStamp ()
 Get the time stamp of this object in getTimeBase() units. More...
 
virtual void setTimeStamp (int64_t aTimeStamp)
 Set the time stamp for this object in getTimeBase() units. More...
 
virtual bool isKey ()
 Is this object a key object? i.e. More...
 
virtual IRationalgetTimeBase ()
 Get the time base that time stamps of this object are represented in. More...
 
virtual void setTimeBase (IRational *aBase)
 Set the time base that time stamps of this object are represented in. More...
 
virtual void reset ()
 Clear out any data in this packet, but leaves the buffer available for reuse.
 
virtual int64_t getPts ()
 Get the Presentation Time Stamp (PTS) for this packet. More...
 
virtual int64_t getDts ()
 Get the Decompression Time Stamp (DTS) for this packet. More...
 
virtual int32_t getSize ()
 Get the size in bytes of the payload currently in this packet. More...
 
virtual int32_t getMaxSize ()
 Get the maximum size (in bytes) of payload this packet can hold. More...
 
virtual int32_t getStreamIndex ()
 Get the container-specific index for the stream this packet is part of. More...
 
virtual int32_t getFlags ()
 Get any flags set on this packet, as a 4-byte binary-ORed bit-mask. More...
 
virtual bool isKeyPacket ()
 Does this packet contain Key data? i.e. More...
 
virtual int64_t getDuration ()
 Return the duration of this packet, in units of getTimeBase(). More...
 
virtual int64_t getPosition ()
 Return the position (in bytes) of this packet in the stream. More...
 
virtual com::avpkit::ferry::IBuffergetData ()
 Get any underlying raw data available for this object. More...
 
virtual int32_t allocateNewPayload (int32_t payloadSize)
 Discard the current payload and allocate a new payload. More...
 
virtual bool isComplete ()
 Is this packet complete. More...
 
virtual void setKeyPacket (bool keyPacket)
 Set if this is a key packet. More...
 
virtual void setFlags (int32_t flags)
 Set any internal flags. More...
 
virtual void setPts (int64_t pts)
 Set a new Presentation Time Stamp (PTS) for this packet. More...
 
virtual void setDts (int64_t dts)
 Set a new Decompression Time Stamp (DTS) for this packet. More...
 
virtual void setComplete (bool complete, int32_t size)
 Set if this packet is complete, and what the total size of the data should be assumed to be. More...
 
virtual void setStreamIndex (int32_t streamIndex)
 Set the stream index for this packet. More...
 
virtual void setDuration (int64_t duration)
 Set the duration. More...
 
virtual void setPosition (int64_t position)
 Set the position. More...
 
virtual void setData (com::avpkit::ferry::IBuffer *buffer)
 Sets the underlying buffer used by this object. More...
 
AVPacket * getAVPacket ()
 
void wrapAVPacket (AVPacket *pkt)
 
void wrapBuffer (com::avpkit::ferry::IBuffer *buffer)
 
- Public Member Functions inherited from com::avpkit::ferry::RefCounted
virtual int32_t acquire ()
 Internal Only. More...
 
virtual int32_t release ()
 Internal Only. More...
 
virtual RefCountedcopyReference ()
 Create a new Java object that refers to the same native object. More...
 
virtual int32_t getCurrentRefCount ()
 Return the current reference count on this object. More...
 
void setJavaAllocator (void *allocator)
 This method is public but not part of the standard API. More...
 
void * getJavaAllocator ()
 This method is public but not part of the standard API. More...
 

Static Public Member Functions

static Packetmake (int32_t payloadSize)
 
static Packetmake (com::avpkit::ferry::IBuffer *buffer)
 
static Packetmake (Packet *packet, bool)
 
static void freeAVBuffer (void *buf, void *closure)
 
- Static Public Member Functions inherited from com::avpkit::core::IPacket
static IPacketmake ()
 Allocate a new packet. More...
 
static IPacketmake (com::avpkit::ferry::IBuffer *buffer)
 Allocate a new packet that wraps an existing IBuffer. More...
 
static IPacketmake (IPacket *packet, bool copyData)
 Allocate a new packet wrapping the existing contents of a passed in packet. More...
 
static IPacketmake (int32_t size)
 Allocate a new packet. More...
 

Additional Inherited Members

- Protected Member Functions inherited from com::avpkit::ferry::RefCounted
virtual void destroy ()
 This method is called by RefCounted objects when their Ref Count reaches zero and they are about to be destroyed.
 
- Protected Attributes inherited from com::avpkit::ferry::RefCounted
AtomicIntegermRefCount
 This is the internal reference count, represented as an AtomicInteger to make sure it is thread safe.
 
void * mAllocator
 Not part of public API.
 

Detailed Description

Definition at line 30 of file Packet.h.

Member Function Documentation

◆ allocateNewPayload()

int32_t com::avpkit::core::Packet::allocateNewPayload ( int32_t  payloadSize)
virtual

Discard the current payload and allocate a new payload.

Note that if any people have access to the old payload using getData(), the memory will continue to be available to them until they release their hold of the IBuffer.

When requesting a packet size, the system may allocate a larger payloadSize.

Parameters
payloadSizeThe (minimum) payloadSize of this packet in bytes.
Returns
>= 0 if successful. < 0 if error.

Implements com::avpkit::core::IPacket.

Definition at line 309 of file Packet.cpp.

310  {
311  int32_t retval = -1;
312  reset();
313  uint8_t* payload = 0;
314 
315  // Some FFMPEG encoders will read past the end of a
316  // buffer, so you need to allocate extra; yuck.
317  if (!mBuffer || mBuffer->getBufferSize() < payloadSize)
318  {
319  // buffer isn't big enough; we need to make a new one.
320  payload = (uint8_t*) av_malloc(payloadSize+AV_INPUT_BUFFER_PADDING_SIZE);
321  if (!payload)
322  throw std::bad_alloc();
323 
324  // we don't use the JVM for packets because Ffmpeg is REAL squirly about that
325  mBuffer = Buffer::make(0, payload,
326  payloadSize,
327  Packet::freeAVBuffer, 0);
328  if (!mBuffer) {
329  av_free(payload);
330  throw std::bad_alloc();
331  }
332  // and memset the padding area.
333  memset(payload + payloadSize,
334  0,
335  AV_INPUT_BUFFER_PADDING_SIZE);
336  } else {
337  payload = (uint8_t*)mBuffer->getBytes(0, payloadSize);
338  }
339  VS_ASSERT(mPacket, "Should already have a packet");
340  VS_ASSERT(mBuffer, "Should have allocated a buffer");
341  VS_ASSERT(payload, "Should have allocated a payload");
342  if (mBuffer && mPacket)
343  {
344  mPacket->data = payload;
345 
346  // And start out at zero.
347  mPacket->size = 0;
348  this->setComplete(false, 0);
349 
350  retval = 0;
351  }
352  return retval;
353  }
virtual void reset()
Clear out any data in this packet, but leaves the buffer available for reuse.
Definition: Packet.cpp:211
virtual void setComplete(bool complete, int32_t size)
Set if this packet is complete, and what the total size of the data should be assumed to be.
Definition: Packet.cpp:132
static VS_API_FERRY Buffer * make(RefCounted *requestor, int32_t bufferSize)
Allocate a new buffer of at least bufferSize.
Definition: Buffer.cpp:91
virtual void * getBytes(int32_t offset, int32_t length)=0
Returns up to length bytes, starting at offset in the underlying buffer we're managing.
virtual int32_t getBufferSize()=0
Get the current maximum number of bytes that can be safely placed in this buffer.

References com::avpkit::ferry::Buffer::make().

Referenced by com::avpkit::core::StreamCoder::encodeAudio(), and com::avpkit::core::StreamCoder::encodeVideo().

◆ getData()

com::avpkit::ferry::IBuffer * com::avpkit::core::Packet::getData ( )
virtual

Get any underlying raw data available for this object.

Returns
The raw data, or null if not accessible.

Implements com::avpkit::core::IMediaData.

Definition at line 173 of file Packet.cpp.

174  {
175  return mBuffer.get();
176  }
T * get()
Call RefCounted::acquire() on the managed pointer and return it.
Definition: RefPointer.h:206

Referenced by com::avpkit::core::StreamCoder::decodeAudio(), com::avpkit::core::StreamCoder::decodeVideo(), com::avpkit::core::StreamCoder::encodeAudio(), and com::avpkit::core::StreamCoder::encodeVideo().

◆ getDts()

int64_t com::avpkit::core::Packet::getDts ( )
virtual

Get the Decompression Time Stamp (DTS) for this packet.

This is the time at which the payload for this packet should be decompressed, in units of getTimeBase(), relative to the start of stream.

Some media codecs can require packets from the "future" to be decompressed before earliest packets as an additional way to compress data. In general you don't need to worry about this, but if you're curious start reading about the difference between I-Frames, P-Frames and B-Frames (or Bi-Directional Frames). B-Frames can use information from future frames when compressed.

Returns
Get the Decompression Timestamp (i.e. when this was read relative to the start of reading packets).

Implements com::avpkit::core::IPacket.

Definition at line 75 of file Packet.cpp.

76  {
77  return (mPacket ? mPacket->dts : (int64_t)-1);
78  }

Referenced by getTimeStamp(), and com::avpkit::core::Container::readNextPacket().

◆ getDuration()

int64_t com::avpkit::core::Packet::getDuration ( )
virtual

Return the duration of this packet, in units of getTimeBase().

Returns
Duration of this packet, in same time-base as the PTS.

Implements com::avpkit::core::IPacket.

Definition at line 149 of file Packet.cpp.

150  {
151  return (mPacket ? mPacket->duration: (int64_t)-1);
152  }

Referenced by com::avpkit::core::Container::readNextPacket().

◆ getFlags()

int32_t com::avpkit::core::Packet::getFlags ( )
virtual

Get any flags set on this packet, as a 4-byte binary-ORed bit-mask.

This is access to raw FFMPEG flags, but it is easier to use the is* methods below.

Returns
Any flags on the packet.

Implements com::avpkit::core::IPacket.

Definition at line 102 of file Packet.cpp.

103  {
104  return (mPacket ? mPacket->flags: (int32_t)-1);
105  }

Referenced by com::avpkit::core::Container::readNextPacket().

◆ getMaxSize()

int32_t com::avpkit::core::Packet::getMaxSize ( )
virtual

Get the maximum size (in bytes) of payload this packet can hold.

Returns
Get maximum size (in bytes) of payload this packet can hold.

Implements com::avpkit::core::IPacket.

Definition at line 92 of file Packet.cpp.

93  {
94  return (mBuffer ? mBuffer->getBufferSize() : -1);
95  }

◆ getPosition()

int64_t com::avpkit::core::Packet::getPosition ( )
virtual

Return the position (in bytes) of this packet in the stream.

Returns
The position of this packet in the stream, or -1 if unknown.

Implements com::avpkit::core::IPacket.

Definition at line 161 of file Packet.cpp.

162  {
163  return (mPacket ? mPacket->pos: (int64_t)-1);
164  }

Referenced by com::avpkit::core::Container::readNextPacket().

◆ getPts()

int64_t com::avpkit::core::Packet::getPts ( )
virtual

Get the Presentation Time Stamp (PTS) for this packet.

This is the time at which the payload for this packet should be presented to the user, in units of getTimeBase(), relative to the start of stream.

Returns
Get the Presentation Timestamp for this packet.

Implements com::avpkit::core::IPacket.

Definition at line 63 of file Packet.cpp.

64  {
65  return (mPacket ? mPacket->pts : (int64_t)-1);
66  }

Referenced by com::avpkit::core::StreamCoder::decodeAudio(), and com::avpkit::core::Container::readNextPacket().

◆ getSize()

int32_t com::avpkit::core::Packet::getSize ( )
virtual

Get the size in bytes of the payload currently in this packet.

Returns
Size (in bytes) of payload currently in packet.

Implements com::avpkit::core::IPacket.

Definition at line 87 of file Packet.cpp.

88  {
89  return (mPacket ? mPacket->size: (int32_t)-1);
90  }

Referenced by com::avpkit::core::StreamCoder::decodeAudio(), com::avpkit::core::StreamCoder::decodeVideo(), com::avpkit::core::StreamCoder::encodeAudio(), and com::avpkit::core::Container::readNextPacket().

◆ getStreamIndex()

int32_t com::avpkit::core::Packet::getStreamIndex ( )
virtual

Get the container-specific index for the stream this packet is part of.

Returns
Stream in container that this packet has data for.

Implements com::avpkit::core::IPacket.

Definition at line 97 of file Packet.cpp.

98  {
99  return (mPacket ? mPacket->stream_index: (int32_t)-1);
100  }

Referenced by com::avpkit::core::Container::readNextPacket(), and com::avpkit::core::Container::writePacket().

◆ getTimeBase()

virtual IRational* com::avpkit::core::Packet::getTimeBase ( )
inlinevirtual

Get the time base that time stamps of this object are represented in.

Caller must release the returned value.

Returns
the time base.

Implements com::avpkit::core::IMediaData.

Definition at line 49 of file Packet.h.

49 { return mTimeBase.get(); }

◆ getTimeStamp()

virtual int64_t com::avpkit::core::Packet::getTimeStamp ( )
inlinevirtual

Get the time stamp of this object in getTimeBase() units.

Returns
the time stamp

Implements com::avpkit::core::IMediaData.

Definition at line 46 of file Packet.h.

46 { return getDts(); }
virtual int64_t getDts()
Get the Decompression Time Stamp (DTS) for this packet.
Definition: Packet.cpp:75

References getDts().

◆ isComplete()

bool com::avpkit::core::Packet::isComplete ( )
virtual

Is this packet complete.

Returns
Is this packet full and therefore has valid information.

Implements com::avpkit::core::IPacket.

Definition at line 384 of file Packet.cpp.

385  {
386  return mIsComplete && mPacket->data;
387  }

Referenced by com::avpkit::core::Container::writePacket().

◆ isKey()

virtual bool com::avpkit::core::Packet::isKey ( )
inlinevirtual

Is this object a key object? i.e.

it can be interpreted without needing any other media objects

Returns
true if it's a key, false if not

Implements com::avpkit::core::IMediaData.

Definition at line 48 of file Packet.h.

48 { return isKeyPacket(); }
virtual bool isKeyPacket()
Does this packet contain Key data? i.e.
Definition: Packet.cpp:107

References isKeyPacket().

◆ isKeyPacket()

bool com::avpkit::core::Packet::isKeyPacket ( )
virtual

Does this packet contain Key data? i.e.

data that needs no other frames or samples to decode.

Returns
true if key; false otherwise.

Implements com::avpkit::core::IPacket.

Definition at line 107 of file Packet.cpp.

108  {
109  return (mPacket ? mPacket->flags & AV_PKT_FLAG_KEY : false);
110  }

Referenced by isKey().

◆ setComplete()

void com::avpkit::core::Packet::setComplete ( bool  complete,
int32_t  size 
)
virtual

Set if this packet is complete, and what the total size of the data should be assumed to be.

Parameters
completeTrue for complete, false for not.
sizeSize of data in packet.

Implements com::avpkit::core::IPacket.

Definition at line 132 of file Packet.cpp.

133  {
134  mIsComplete = complete;
135  if (mIsComplete)
136  {
137  if (mPacket)
138  mPacket->size = size;
139  }
140  }

◆ setData()

void com::avpkit::core::Packet::setData ( com::avpkit::ferry::IBuffer buffer)
virtual

Sets the underlying buffer used by this object.

This is an advanced method and is not recommended for use by those who don't fully understand how IBuffers work. Implementations of IMediaData may behave in undefined ways if the buffer you pass in is not big enough for what you ask them to do (e.g. they may discard your buffer and allocate a larger one if they need more space). It is up to the caller to ensure the buffer passed in is large enough, and is not simultaneously in use by another part of the system.

Parameters
bufferThe buffer to set. If null, this method is ignored.

Implements com::avpkit::core::IMediaData.

Definition at line 356 of file Packet.cpp.

357  {
358  wrapBuffer(buffer);
359  }

◆ setDts()

void com::avpkit::core::Packet::setDts ( int64_t  aDts)
virtual

Set a new Decompression Time Stamp (DTS) for this packet.

Parameters
aDtsa new DTS for this packet.
See also
getDts()

Implements com::avpkit::core::IPacket.

Definition at line 81 of file Packet.cpp.

82  {
83  if (mPacket) mPacket->dts = aDts;
84  }

Referenced by setTimeStamp().

◆ setDuration()

void com::avpkit::core::Packet::setDuration ( int64_t  duration)
virtual

Set the duration.

Parameters
durationnew duration
See also
getDuration()

Implements com::avpkit::core::IPacket.

Definition at line 155 of file Packet.cpp.

156  {
157  if (mPacket) mPacket->duration = duration;
158  }

◆ setFlags()

void com::avpkit::core::Packet::setFlags ( int32_t  flags)
virtual

Set any internal flags.

Parameters
flagsFlags to set

Implements com::avpkit::core::IPacket.

Definition at line 125 of file Packet.cpp.

126  {
127  if (mPacket)
128  mPacket->flags = flags;
129  }

◆ setKeyPacket()

void com::avpkit::core::Packet::setKeyPacket ( bool  keyPacket)
virtual

Set if this is a key packet.

Parameters
keyPackettrue for yes, false for no.

Implements com::avpkit::core::IPacket.

Definition at line 113 of file Packet.cpp.

114  {
115  if (mPacket)
116  {
117  if (bKeyPacket)
118  mPacket->flags |= AV_PKT_FLAG_KEY;
119  else
120  mPacket->flags = 0;
121  }
122  }

◆ setPosition()

void com::avpkit::core::Packet::setPosition ( int64_t  position)
virtual

Set the position.

Parameters
positionnew position
See also
getPosition()

Implements com::avpkit::core::IPacket.

Definition at line 167 of file Packet.cpp.

168  {
169  if (mPacket) mPacket->pos = position;
170  }

◆ setPts()

void com::avpkit::core::Packet::setPts ( int64_t  aPts)
virtual

Set a new Presentation Time Stamp (PTS) for this packet.

Parameters
aPtsa new PTS for this packet.
See also
getPts()

Implements com::avpkit::core::IPacket.

Definition at line 69 of file Packet.cpp.

70  {
71  if (mPacket) mPacket->pts = aPts;
72  }

◆ setStreamIndex()

void com::avpkit::core::Packet::setStreamIndex ( int32_t  streamIndex)
virtual

Set the stream index for this packet.

Parameters
streamIndexThe stream index, as determined from the IContainer this packet will be written to.

Implements com::avpkit::core::IPacket.

Definition at line 143 of file Packet.cpp.

144  {
145  if (mPacket)
146  mPacket->stream_index = streamIndex;
147  }

◆ setTimeBase()

virtual void com::avpkit::core::Packet::setTimeBase ( IRational aBase)
inlinevirtual

Set the time base that time stamps of this object are represented in.

Parameters
aBasethe new time base. If null an exception is thrown.

Implements com::avpkit::core::IMediaData.

Definition at line 50 of file Packet.h.

50 { mTimeBase.reset(aBase, true); }

Referenced by com::avpkit::core::Container::readNextPacket().

◆ setTimeStamp()

virtual void com::avpkit::core::Packet::setTimeStamp ( int64_t  aTimeStamp)
inlinevirtual

Set the time stamp for this object in getTimeBase() units.

Parameters
aTimeStampThe time stamp

Implements com::avpkit::core::IMediaData.

Definition at line 47 of file Packet.h.

47 { setDts(aTimeStamp); }
virtual void setDts(int64_t dts)
Set a new Decompression Time Stamp (DTS) for this packet.
Definition: Packet.cpp:81

References setDts().


The documentation for this class was generated from the following files: