AVPKit
Packet.h
1 /*******************************************************************************
2  * Copyright (c) 2024, 2026, Olivier Ayache. All rights reserved.
3  *
4  * This file is part of AVPKit.
5  *
6  * AVPKit is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * AVPKit is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with AVPKit. If not, see <http://www.gnu.org/licenses/>.
18  *******************************************************************************/
19 
20 #ifndef PACKET_H_
21 #define PACKET_H_
22 
23 #include <com/avpkit/ferry/RefPointer.h>
24 #include <com/avpkit/core/IPacket.h>
25 #include <com/avpkit/core/FfmpegIncludes.h>
26 #include <com/avpkit/ferry/IBuffer.h>
27 
28 namespace com { namespace avpkit { namespace core
29 {
30  class Packet : public IPacket
31  {
32  public:
33  /* The default make() method doesn't add a payload */
34  VS_JNIUTILS_REFCOUNTED_OBJECT(Packet);
35  public:
36  /* This make allocates a default payload of size payloadSize */
37  static Packet* make(int32_t payloadSize);
38  /* This make a packet that just wraps a given IBuffer */
39  static Packet* make(com::avpkit::ferry::IBuffer* buffer);
40  /* This makes a packet wrapping the buffer in another packet and copying
41  * it's settings
42  */
43  static Packet* make(Packet* packet, bool);
44  public:
45  // IMediaData
46  virtual int64_t getTimeStamp() { return getDts(); }
47  virtual void setTimeStamp(int64_t aTimeStamp) { setDts(aTimeStamp); }
48  virtual bool isKey() { return isKeyPacket(); }
49  virtual IRational* getTimeBase() { return mTimeBase.get(); }
50  virtual void setTimeBase(IRational *aBase) { mTimeBase.reset(aBase, true); }
51 
52  // IPacket
53  virtual void reset();
54 
55  virtual int64_t getPts();
56  virtual int64_t getDts();
57  virtual int32_t getSize();
58  virtual int32_t getMaxSize();
59 
60  virtual int32_t getStreamIndex();
61  virtual int32_t getFlags();
62  virtual bool isKeyPacket();
63  virtual int64_t getDuration();
64  virtual int64_t getPosition();
66  virtual int32_t allocateNewPayload(int32_t payloadSize);
67  virtual bool isComplete();
68 
69  virtual void setKeyPacket(bool keyPacket);
70  virtual void setFlags(int32_t flags);
71  virtual void setPts(int64_t pts);
72  virtual void setDts(int64_t dts);
73  virtual void setComplete(bool complete, int32_t size);
74  virtual void setStreamIndex(int32_t streamIndex);
75  virtual void setDuration(int64_t duration);
76  virtual void setPosition(int64_t position);
77  virtual void setData(com::avpkit::ferry::IBuffer* buffer);
78 
79  AVPacket *getAVPacket() { return mPacket; }
80  /*
81  * Unfortunately people can do a getAVPacket() and have
82  * FFMPEG update the buffers without us knowing. When
83  * that happens, we need them to tell us so we can update
84  * our own buffer state.
85  */
86  void wrapAVPacket(AVPacket* pkt);
87  void wrapBuffer(com::avpkit::ferry::IBuffer *buffer);
88  // Used by the IBuffer to free buffers.
89  static void freeAVBuffer(void *buf, void *closure);
90 
91  protected:
92  Packet();
93  virtual ~Packet();
94  private:
95  AVPacket* mPacket;
98  bool mIsComplete;
99  };
100 
101 }}}
102 
103 #endif /*PACKET_H_*/
Represents an encoded piece of data that can be placed in an IContainer for a given IStream of data.
Definition: IPacket.h:50
static IPacket * make()
Allocate a new packet.
Definition: IPacket.cpp:37
This class wraps represents a Rational number for the AVPKit.
Definition: IRational.h:43
virtual void setFlags(int32_t flags)
Set any internal flags.
Definition: Packet.cpp:125
virtual int32_t allocateNewPayload(int32_t payloadSize)
Discard the current payload and allocate a new payload.
Definition: Packet.cpp:309
virtual int64_t getPts()
Get the Presentation Time Stamp (PTS) for this packet.
Definition: Packet.cpp:63
virtual void setKeyPacket(bool keyPacket)
Set if this is a key packet.
Definition: Packet.cpp:113
virtual void setTimeStamp(int64_t aTimeStamp)
Set the time stamp for this object in getTimeBase() units.
Definition: Packet.h:47
virtual void setDuration(int64_t duration)
Set the duration.
Definition: Packet.cpp:155
virtual com::avpkit::ferry::IBuffer * getData()
Get any underlying raw data available for this object.
Definition: Packet.cpp:173
virtual IRational * getTimeBase()
Get the time base that time stamps of this object are represented in.
Definition: Packet.h:49
virtual bool isKey()
Is this object a key object? i.e.
Definition: Packet.h:48
virtual void setPosition(int64_t position)
Set the position.
Definition: Packet.cpp:167
virtual int32_t getMaxSize()
Get the maximum size (in bytes) of payload this packet can hold.
Definition: Packet.cpp:92
virtual bool isKeyPacket()
Does this packet contain Key data? i.e.
Definition: Packet.cpp:107
virtual void setStreamIndex(int32_t streamIndex)
Set the stream index for this packet.
Definition: Packet.cpp:143
virtual void reset()
Clear out any data in this packet, but leaves the buffer available for reuse.
Definition: Packet.cpp:211
virtual int32_t getSize()
Get the size in bytes of the payload currently in this packet.
Definition: Packet.cpp:87
virtual void setPts(int64_t pts)
Set a new Presentation Time Stamp (PTS) for this packet.
Definition: Packet.cpp:69
virtual void setData(com::avpkit::ferry::IBuffer *buffer)
Sets the underlying buffer used by this object.
Definition: Packet.cpp:356
virtual void setDts(int64_t dts)
Set a new Decompression Time Stamp (DTS) for this packet.
Definition: Packet.cpp:81
virtual int32_t getFlags()
Get any flags set on this packet, as a 4-byte binary-ORed bit-mask.
Definition: Packet.cpp:102
virtual bool isComplete()
Is this packet complete.
Definition: Packet.cpp:384
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
virtual int32_t getStreamIndex()
Get the container-specific index for the stream this packet is part of.
Definition: Packet.cpp:97
virtual int64_t getTimeStamp()
Get the time stamp of this object in getTimeBase() units.
Definition: Packet.h:46
virtual void setTimeBase(IRational *aBase)
Set the time base that time stamps of this object are represented in.
Definition: Packet.h:50
virtual int64_t getPosition()
Return the position (in bytes) of this packet in the stream.
Definition: Packet.cpp:161
virtual int64_t getDts()
Get the Decompression Time Stamp (DTS) for this packet.
Definition: Packet.cpp:75
virtual int64_t getDuration()
Return the duration of this packet, in units of getTimeBase().
Definition: Packet.cpp:149
Allows Java code to get data from a native buffers, and optionally modify native memory directly.
Definition: IBuffer.h:54
WARNING: Do not use logging in this class, and do not set any static file variables to values other t...