AVPKit
Container.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 CONTAINER_H_
21 #define CONTAINER_H_
22 
23 #include <com/avpkit/ferry/RefCounted.h>
24 #include <com/avpkit/ferry/RefPointer.h>
25 #include <com/avpkit/core/IContainer.h>
26 #include <com/avpkit/core/FfmpegIncludes.h>
27 #include <com/avpkit/core/Stream.h>
28 #include <com/avpkit/core/StreamCoder.h>
29 #include <com/avpkit/core/ContainerFormat.h>
30 #include <com/avpkit/core/MetaData.h>
31 
32 #include <com/avpkit/core/io/URLProtocolHandler.h>
33 #include <vector>
34 #include <list>
35 
36 #include <com/avpkit/core/io/URLProtocolHandlerFactory.h>
37 
38 namespace com { namespace avpkit { namespace core
39 {
40  class Container : public IContainer
41  {
42  VS_JNIUTILS_REFCOUNTED_OBJECT(Container)
43  public:
44  virtual int32_t setInputBufferLength(int32_t size);
45  virtual int32_t getInputBufferLength();
46  virtual bool isOpened();
47  virtual bool isHeaderWritten();
48 
49  virtual int32_t open(const char *url, Type type,
50  IContainerFormat* pContainerFormat);
51  virtual int32_t open(const char *url, Type type,
52  IContainerFormat* pContainerFormat, bool, bool);
56 
57  virtual Type getType();
58  virtual int32_t close();
59  virtual int32_t close(bool);
60  virtual int32_t getNumStreams();
61 
62  virtual Stream* getStream(int32_t position);
63 
64  virtual Stream* addNewStream(int32_t id);
65 
66  virtual int32_t readNextPacket(IPacket *packet);
67  virtual int32_t writePacket(IPacket *packet, bool forceInterleave);
68  virtual int32_t writePacket(IPacket *packet);
69 
70  virtual int32_t writeHeader();
71  virtual int32_t writeTrailer();
72  AVFormatContext *getFormatContext();
73 
74  /*
75  * Added as of 1.17
76  */
77  virtual int32_t queryStreamMetaData();
78  virtual int32_t seekKeyFrame(int streamIndex, int64_t timestamp, int32_t flags);
79  virtual int64_t getDuration();
80  virtual int64_t getStartTime();
81  virtual int64_t getFileSize();
82  virtual int32_t getBitRate();
83  virtual int32_t setPreload(int32_t preload);
84  virtual int32_t getPreload();
85  virtual int32_t setMaxDelay(int32_t maxdelay);
86  virtual int32_t getMaxDelay();
87 
88  /*
89  * Added for 1.19
90  */
91  virtual int32_t getNumProperties();
92  virtual IProperty* getPropertyMetaData(int32_t propertyNo);
93  virtual IProperty* getPropertyMetaData(const char *name);
94 
95  virtual int32_t setProperty(const char* name, const char* value);
96  virtual int32_t setProperty(const char* name, double value);
97  virtual int32_t setProperty(const char* name, int64_t value);
98  virtual int32_t setProperty(const char* name, bool value);
99  virtual int32_t setProperty(const char* name, IRational *value);
100 
101  virtual char * getPropertyAsString(const char* name);
102  virtual double getPropertyAsDouble(const char* name);
103  virtual int64_t getPropertyAsLong(const char* name);
104  virtual IRational *getPropertyAsRational(const char* name);
105  virtual bool getPropertyAsBoolean(const char* name);
106 
107  virtual int32_t getFlags();
108  virtual void setFlags(int32_t newFlags);
109  virtual bool getFlag(Flags flag);
110  virtual void setFlag(Flags flag, bool value);
111 
112  virtual const char * getURL();
113  virtual int32_t flushPackets();
114 
115  virtual int32_t getReadRetryCount();
116  virtual void setReadRetryCount(int32_t count);
117 
118  virtual bool canStreamsBeAddedDynamically();
119 
120  virtual IMetaData* getMetaData();
121  virtual void setMetaData(IMetaData* metaData);
122  virtual int32_t createSDPData(com::avpkit::ferry::IBuffer* buffer);
123 
124  virtual int32_t setForcedAudioCodec(ICodec::ID id);
125  virtual int32_t setForcedVideoCodec(ICodec::ID id);
126  virtual int32_t setForcedSubtitleCodec(ICodec::ID id);
127 
128  /*
129  * Added for 3.4
130  */
131  virtual int32_t seekKeyFrame(int32_t streamIndex, int64_t minTimeStamp,
132  int64_t targetTimeStamp, int64_t maxTimeStamp, int32_t flags);
133 
134  /*
135  * Added for 5.0
136  */
137  virtual Stream* addNewStream(ICodec::ID id);
138  virtual Stream* addNewStream(ICodec* codec);
139  virtual Stream* addNewStream(IStreamCoder* coder);
140 
141  virtual ContainerFormat *getFormat() { return mFormat.get(); };
142  virtual int32_t setFormat(IContainerFormat* format);
143  static Container* make(IContainerFormat* format);
144 
145  virtual int32_t setProperty(IMetaData* valuesToSet, IMetaData* valuesNotFound);
146  virtual int32_t open(const char *url, Type type,
147  IContainerFormat* pContainerFormat, bool, bool,
148  IMetaData*, IMetaData*);
149 
150  int32_t setCustomIOProtocol(io::URLProtocolHandlerFactory* factory);
151 
152  protected:
153  virtual ~Container();
154  Container();
155 
156  private:
157  // This is the object we wrap
158  int32_t openInputURL(const char*url, bool, bool, AVDictionary** options);
159  int32_t openOutputURL(const char*url, bool, AVDictionary **options);
160  int32_t setupAllInputStreams();
161  AVFormatContext *mFormatContext;
162  AVPacket *mPacket;
163  AVPacket *mBsfPacket;
164  void reset();
165  void resetContext();
166  // We do pointer to RefPointers to avoid too many
167  // acquire() / release() cycles as the vector manages
168  // itself.
169  std::vector<
171  > mStreams;
172  uint32_t mNumStreams;
173  bool mIsOpened;
174  bool mNeedTrailerWrite;
175  std::list<
177  > mOpenCoders;
178 
179  bool mIsMetaDataQueried;
180  size_t mInputBufferLength;
181 
182  int32_t mReadRetryCount;
185 
186  io::URLProtocolHandler *mCustomIOHandler;
187  };
188 }}}
189 
190 #endif /*CONTAINER_H_*/
virtual void setMetaData(IMetaData *metaData)
Set the IMetaData on this object, overriding any previous meta data.
Definition: Container.cpp:1401
virtual int32_t setForcedVideoCodec(ICodec::ID id)
Forces the IContainer to assume all video streams are encoded with the given video codec when demuxin...
Definition: Container.cpp:1459
virtual double getPropertyAsDouble(const char *name)
Gets the value of this property, and returns as a double;.
Definition: Container.cpp:1224
virtual int32_t getFlags()
Get the flags associated with this object.
Definition: Container.cpp:1248
virtual int32_t setProperty(const char *name, const char *value)
Sets a property on this Object.
Definition: Container.cpp:1186
virtual int32_t writeHeader()
Adds a header, if needed, for this container.
Definition: Container.cpp:883
virtual int32_t getMaxDelay()
Gets the AVFormatContext.max_delay property if possible.
Definition: Container.cpp:1153
virtual IStreamCoder::CodecStandardsCompliance getStandardsCompliance()
Gets the current level of standards compliance.
Definition: Container.cpp:387
virtual int32_t getNumStreams()
The number of streams in this container.
Definition: Container.cpp:629
virtual void setFlag(Flags flag, bool value)
Set the flag.
Definition: Container.cpp:1276
virtual bool getFlag(Flags flag)
Get the setting for the specified flag.
Definition: Container.cpp:1267
virtual int32_t open(const char *url, Type type, IContainerFormat *pContainerFormat)
Open this container and make it ready for reading or writing.
Definition: Container.cpp:270
virtual ContainerFormat * getFormat()
Get the IContainerFormat that is used by this IContainer.
Definition: Container.h:141
virtual int32_t setForcedAudioCodec(ICodec::ID id)
Forces the IContainer to assume all audio streams are encoded with the given audio codec when demuxin...
Definition: Container.cpp:1446
virtual int32_t setMaxDelay(int32_t maxdelay)
Sets the max delay for the AVFormatContext.max_delay property.
Definition: Container.cpp:1137
virtual int64_t getDuration()
Gets the duration, if known, of this container.
Definition: Container.cpp:1077
virtual int32_t setFormat(IContainerFormat *format)
Set the IContainerFormat to use with this IContainer.
Definition: Container.cpp:1338
virtual Type getType()
Find out the type of this container.
Definition: Container.cpp:622
virtual int32_t setInputBufferLength(int32_t size)
Set the buffer length AVPKit will suggest to FFMPEG for reading inputs.
Definition: Container.cpp:236
virtual int32_t writeTrailer()
Adds a trailer, if needed, for this container.
Definition: Container.cpp:952
virtual int32_t setPreload(int32_t preload)
@Deprecated use setProperty instead.
Definition: Container.cpp:1123
virtual Stream * getStream(int32_t position)
Get the stream at the given position.
Definition: Container.cpp:714
virtual void setFlags(int32_t newFlags)
Set the flags to use with this object.
Definition: Container.cpp:1257
virtual int32_t readNextPacket(IPacket *packet)
Reads the next packet into the IPacket.
Definition: Container.cpp:732
virtual int32_t writePacket(IPacket *packet, bool forceInterleave)
Writes the contents of the packet to the container.
Definition: Container.cpp:809
virtual int32_t getInputBufferLength()
Return the input buffer length.
Definition: Container.cpp:252
virtual int32_t getReadRetryCount()
Get the number of times IContainer#readNextPacket(IPacket) will retry a read if it gets a IError....
Definition: Container.cpp:1326
virtual int64_t getPropertyAsLong(const char *name)
Gets the value of this property, and returns as an long;.
Definition: Container.cpp:1230
virtual const char * getURL()
Get the URL the IContainer was opened with.
Definition: Container.cpp:1293
virtual IMetaData * getMetaData()
Get the IMetaData for this object, or null if none.
Definition: Container.cpp:1387
virtual int32_t getNumProperties()
Returns the total number of settable properties on this object.
Definition: Container.cpp:1162
virtual bool getPropertyAsBoolean(const char *name)
Gets the value of this property, and returns as a boolean.
Definition: Container.cpp:1242
virtual int32_t setStandardsCompliance(IStreamCoder::CodecStandardsCompliance compliance)
Set the level of standards compliance.
Definition: Container.cpp:393
virtual Stream * addNewStream(int32_t id)
Definition: Container.cpp:1587
virtual int32_t getPreload()
@Deprecated use getPropertyAsLong instead.
Definition: Container.cpp:1130
virtual bool isHeaderWritten()
Has a header been successfully written?
Definition: Container.cpp:264
virtual IRational * getPropertyAsRational(const char *name)
Gets the value of this property, and returns as an IRational;.
Definition: Container.cpp:1236
virtual int32_t setForcedSubtitleCodec(ICodec::ID id)
Forces the IContainer to assume all subtitle streams are encoded with the given subtitle codec when d...
Definition: Container.cpp:1472
virtual IProperty * getPropertyMetaData(int32_t propertyNo)
Returns the name of the numbered property.
Definition: Container.cpp:1168
virtual void setReadRetryCount(int32_t count)
Sets the read retry count.
Definition: Container.cpp:1332
virtual bool isOpened()
Is this container opened?
Definition: Container.cpp:258
virtual char * getPropertyAsString(const char *name)
Gets a property on this Object.
Definition: Container.cpp:1218
virtual int64_t getFileSize()
Get the file size in bytes of this container.
Definition: Container.cpp:1097
virtual int32_t queryStreamMetaData()
Attempts to read all the meta data in this stream, potentially by reading ahead and decoding packets.
Definition: Container.cpp:999
virtual int32_t createSDPData(com::avpkit::ferry::IBuffer *buffer)
Fills the given buffer with a null-terminated ASCII set of bytes representing SDP data that is suitab...
Definition: Container.cpp:1413
virtual int64_t getStartTime()
Get the starting timestamp in microseconds of the first packet of the earliest stream in this contain...
Definition: Container.cpp:1087
virtual int32_t getBitRate()
Get the calculated overall bit rate of this file.
Definition: Container.cpp:1113
virtual IContainerFormat * getContainerFormat()
Returns the IContainerFormat object being used for this IContainer, or null if the IContainer doesn't...
Definition: Container.cpp:405
virtual int32_t flushPackets()
Flush all packets to output.
Definition: Container.cpp:1299
virtual bool canStreamsBeAddedDynamically()
Can streams be added dynamically to this container?
Definition: Container.cpp:1379
A "key" to an IStreamCoder that tells it how to encode or decode data.
Definition: ICodec.h:53
ID
These are the codecs this library currently supports.
Definition: ICodec.h:61
Specifies format information than can be used to configure an IContainer for input or output.
A file (or network data source) that contains one or more IStream objects of audio and video data.
Definition: IContainer.h:100
static IContainer * make()
Create a new container object.
Definition: IContainer.cpp:31
Type
The different types of Containers AVPKit supports.
Definition: IContainer.h:106
Get MetaData about a IContainer or IStream.
Definition: IMetaData.h:51
Represents an encoded piece of data that can be placed in an IContainer for a given IStream of data.
Definition: IPacket.h:50
Represents settable properties that effect how AVPKit objects operate.
Definition: IProperty.h:37
This class wraps represents a Rational number for the AVPKit.
Definition: IRational.h:43
The work horse of the AVPKit: Takes IPacket data from an IContainer (representing an IStream) and an ...
Definition: IStreamCoder.h:45
CodecStandardsCompliance
An enumeration of how strictly Codecs may follow the spec.
Definition: IStreamCoder.h:959
A class for managing custom io protocols.
Allows Java code to get data from a native buffers, and optionally modify native memory directly.
Definition: IBuffer.h:54
This class is only useful from C++.
Definition: RefPointer.h:47
WARNING: Do not use logging in this class, and do not set any static file variables to values other t...