forked from avTranscoder/avTranscoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTranscoder.hpp
231 lines (193 loc) · 9.28 KB
/
Transcoder.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#ifndef _AV_TRANSCODER_TRANSCODER_HPP_
#define _AV_TRANSCODER_TRANSCODER_HPP_
#include <AvTranscoder/common.hpp>
#include <AvTranscoder/file/InputFile.hpp>
#include <AvTranscoder/file/IOutputFile.hpp>
#include <AvTranscoder/stream/IInputStream.hpp>
#include <AvTranscoder/profile/ProfileLoader.hpp>
#include <AvTranscoder/stat/ProcessStat.hpp>
#include <AvTranscoder/transcoder/InputStreamDesc.hpp>
#include <AvTranscoder/transcoder/StreamTranscoder.hpp>
#include <string>
#include <vector>
namespace avtranscoder
{
/**
* @brief Enum to set a policy of how we manage the process in case of several streams.
* eProcessMethodShortest: stop the process at the end of the shortest stream.
* eProcessMethodLongest: stop the process at the end of the longest stream.
* eProcessMethodBasedOnStream: stop the process at the end of an indicated stream (@see _indexBasedStream attribute of
* Transcoder).
* eProcessMethodBasedOnDuration: stop the process at the end of an indicated duration, in seconds (@see _outputDuration
* attribute of Transcoder).
* eProcessMethodInfinity: stop the process by outside of avTranscoder (streaming mode)
*/
enum EProcessMethod
{
eProcessMethodShortest = 0,
eProcessMethodLongest,
eProcessMethodBasedOnStream,
eProcessMethodBasedOnDuration,
eProcessMethodInfinity,
};
/**
* @brief A Transcoder manages a list of streams,
* and process a transcode to create an output media file.
*/
class AvExport Transcoder
{
private:
Transcoder(const Transcoder& transcoder);
Transcoder& operator=(const Transcoder& transcoder);
public:
/**
* @note Set FFmpeg log level to quite.
*/
Transcoder(IOutputFile& outputFile);
~Transcoder();
//@{
// @brief Add a new stream to the output file, created from the given input description to process.
// @param profileName: the encoding profile (rewrap if empty)
// @param offset: in seconds
// If offset is positive, the transcoder will generate black images or silence (depending on the type of stream) before
// the stream to process.
// If offset is negative, the transcoder will seek in the stream and start process at this specific time.
void addStream(const InputStreamDesc& inputStreamDesc, const std::string& profileName = "", const float offset = 0);
void addStream(const InputStreamDesc& inputStreamDesc, const ProfileLoader::Profile& profile, const float offset = 0);
void addStream(const InputStreamDesc& inputStreamDesc, IEncoder* encoder);
//@}
//@{
// @brief Add a new stream to the output file, created from the given input description to process.
// @param inputStreamDescArray: the type of the described streams should be of the same type.
// @param profile: if empty, get the profile from the inputs.
void addStream(const std::vector<InputStreamDesc>& inputStreamDescArray, const std::string& profileName = "", float offset = 0);
void addStream(const std::vector<InputStreamDesc>& inputStreamDescArray, const ProfileLoader::Profile& profile, const float offset = 0);
void addStream(const std::vector<InputStreamDesc>& inputStreamDescArray, IEncoder* encoder);
//@}
//@{
// @brief Add a new generated stream to the output file, created from the given encoding profile.
void addGenerateStream(const std::string& encodingProfileName);
void addGenerateStream(const ProfileLoader::Profile& encodingProfile);
//@}
/**
* @brief Add the stream
*/
void addStream(StreamTranscoder& streamTranscoder);
/**
* @brief Initialize all added streams, processing codec latency.
* @note This can be called several times with no side effects.
* @note Can take a little bit of time.
*/
void preProcessCodecLatency();
/**
* @brief Process the next frame of all streams.
* @param progress: choose a progress, or create your own in C++ or in bindings by inherit IProgress class.
* @return if a frame was processed or not.
*/
bool processFrame(IProgress& progress);
bool processFrame(); ///< Call processFrame with no display of progression
/**
* @brief Process all the streams, and ended the process depending on the transcode politic.
* @note The function manages all process: init(), beginWrap(), processFrame()s, and endWrap().
* @param progress: choose a progress, or create your own in C++ or in bindings by inherit IProgress class.
* @return ProcessStat: object with statistics of the process for each stream.
* @see IProgress
*/
ProcessStat process(IProgress& progress);
ProcessStat process(); ///< Call process with no display of progression
/**
* @brief Return the list of streams added to the transcoder.
*/
std::vector<StreamTranscoder*>& getStreamTranscoders() { return _streamTranscoders; }
/**
* @param streamIndex: careful about the order of stream insertion of the Transcoder.
* @return a reference to a stream manage by the Transcoder.
*/
StreamTranscoder& getStreamTranscoder(size_t streamIndex) const { return *_streamTranscoders.at(streamIndex); }
/**
* @brief Get current processMethod
* @see EProcessMethod
*/
EProcessMethod getProcessMethod() const { return _eProcessMethod; }
/**
* @brief Set the transcoding policy.
* @note By default eProcessMethodBasedOnStream at index 0.
* @param indexBasedStream: in case of process method eProcessMethodBasedOnStream, stop transcode at the end of the
* indicated stream.
* @param outputDuration: in case of process method eProcessMethodBasedOnDuration, stop transcode at the end of the
* indicated duration.
*/
void setProcessMethod(const EProcessMethod eProcessMethod, const size_t indexBasedStream = 0,
const double outputDuration = 0);
private:
void addRewrapStream(const InputStreamDesc& inputStreamDesc, const float offset);
void addTranscodeStream(const std::vector<InputStreamDesc>& inputStreamDescArray, const ProfileLoader::Profile& profile,
const float offset = 0);
void addTranscodeStream(const std::vector<InputStreamDesc>& inputStreamDescArray, IEncoder* encoder, const float offset = 0);
/**
* @note If streamIndex is negative, activate all streams of the file.
*/
InputFile* addInputFile(const std::string& filename, const int streamIndex, const float offset);
/**
* @return The profile from the given inputs.
*/
ProfileLoader::Profile getProfileFromInput(const InputStreamDesc& inputStreamDesc);
ProfileLoader::Profile getProfileFromInputs(const std::vector<InputStreamDesc>& inputStreamDescArray);
/**
* @brief Get the duration of the stream, in seconds
* @note If the stream is a generator, return limit of double.
*/
float getStreamDuration(size_t indexStream) const;
/**
* @brief Get the duration of the shortest stream, in seconds
* @note Set the index of the main stream to stop the process at the end of the shortest stream.
*/
float getMinTotalDuration();
/**
* @brief Get the duration of the longest stream, in seconds
* @note Set the index of the main stream to stop the process at the end of the longest stream.
*/
float getMaxTotalDuration();
/**
* @brief Get the expected duration of the output program
* @note Depends on the streams, the process method, and the main stream index.
*/
float getExpectedOutputDuration();
/**
* @brief Get the current duration of the output program
* @note Returns the duration of the smallest stream.
* @return -1 if there is no output stream.
*/
float getCurrentOutputDuration() const;
/**
* @brief Set for each StreamTranscoder if it can switch to generator at the end.
*/
void manageSwitchToGenerator();
/**
* @brief Process the next frame of the specified stream.
* @return whether a frame was processed or not.
*/
bool processFrame(IProgress& progress, const size_t& streamIndex);
/**
* @brief Check whether the process is canceled or not, and whether the process reached the ending condition.
* @note The progress is updated in this function.
* @return whether the process must continue or stop.
*/
bool continueProcess(IProgress& progress);
/**
* @brief Fill the given ProcessStat to summarize the process.
*/
void fillProcessStat(ProcessStat& processStat);
private:
IOutputFile& _outputFile; ///< The output media file after process (has link)
std::vector<InputFile*> _inputFiles; ///< The list of input files which contain added streams (has ownership)
std::vector<StreamTranscoder*> _streamTranscoders; ///< All streams of the output media file after process.
std::vector<StreamTranscoder*> _streamTranscodersAllocated; ///< Streams allocated inside the Transcoder (has ownership)
ProfileLoader _profileLoader; ///< Objet to get existing profiles, and add new ones for the Transcoder.
EProcessMethod _eProcessMethod; ///< Processing policy
size_t _mainStreamIndex; ///< Index of stream used to stop the process.
size_t _processedFrames; ///< Counter for the number of processed frames.
float _outputDuration; ///< Duration of output media used to stop the process of transcode in case of eProcessMethodBasedOnDuration.
};
}
#endif