Skip to content

Commit 737a3d1

Browse files
author
Clement Champetier
committed
StreamTranscoder: removed _currentInputStream attribute
Confusing when we will have several input streams.
1 parent 8dbe2d6 commit 737a3d1

File tree

3 files changed

+52
-49
lines changed

3 files changed

+52
-49
lines changed

src/AvTranscoder/transcoder/StreamTranscoder.cpp

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ namespace avtranscoder
2222
StreamTranscoder::StreamTranscoder(IInputStream& inputStream, IOutputFile& outputFile, const float offset)
2323
: _inputStreamDesc()
2424
, _inputStreams()
25-
, _currentInputStream(&inputStream)
2625
, _outputStream(NULL)
2726
, _decodedData()
2827
, _filteredData()
@@ -36,22 +35,22 @@ StreamTranscoder::StreamTranscoder(IInputStream& inputStream, IOutputFile& outpu
3635
, _offset(offset)
3736
, _needToSwitchToGenerator(false)
3837
{
39-
_inputStreams.push_back(_currentInputStream);
38+
_inputStreams.push_back(&inputStream);
4039

4140
// create a re-wrapping case
42-
switch(_currentInputStream->getProperties().getStreamType())
41+
switch(inputStream.getProperties().getStreamType())
4342
{
4443
case AVMEDIA_TYPE_VIDEO:
4544
{
4645
// output stream
47-
_outputStream = &outputFile.addVideoStream(_currentInputStream->getVideoCodec());
46+
_outputStream = &outputFile.addVideoStream(inputStream.getVideoCodec());
4847

4948
try
5049
{
5150
// filter
52-
_filterGraph = new FilterGraph(_currentInputStream->getVideoCodec());
51+
_filterGraph = new FilterGraph(inputStream.getVideoCodec());
5352

54-
VideoFrameDesc inputFrameDesc(_currentInputStream->getVideoCodec().getVideoFrameDesc());
53+
VideoFrameDesc inputFrameDesc(inputStream.getVideoCodec().getVideoFrameDesc());
5554

5655
// generator decoder
5756
_generators.push_back(new VideoGenerator(inputFrameDesc));
@@ -65,13 +64,13 @@ StreamTranscoder::StreamTranscoder(IInputStream& inputStream, IOutputFile& outpu
6564
_transform = new VideoTransform();
6665

6766
// output encoder
68-
VideoEncoder* outputVideo = new VideoEncoder(_currentInputStream->getVideoCodec().getCodecName());
67+
VideoEncoder* outputVideo = new VideoEncoder(inputStream.getVideoCodec().getCodecName());
6968
outputVideo->setupVideoEncoder(inputFrameDesc);
7069
_outputEncoder = outputVideo;
7170
}
7271
catch(std::runtime_error& e)
7372
{
74-
LOG_WARN("Cannot create the video encoder for stream " << _currentInputStream->getStreamIndex() << " if needed. "
73+
LOG_WARN("Cannot create the video encoder for stream " << inputStream.getStreamIndex() << " if needed. "
7574
<< e.what())
7675
}
7776

@@ -80,14 +79,14 @@ StreamTranscoder::StreamTranscoder(IInputStream& inputStream, IOutputFile& outpu
8079
case AVMEDIA_TYPE_AUDIO:
8180
{
8281
// output stream
83-
_outputStream = &outputFile.addAudioStream(_currentInputStream->getAudioCodec());
82+
_outputStream = &outputFile.addAudioStream(inputStream.getAudioCodec());
8483

8584
try
8685
{
8786
// filter
88-
_filterGraph = new FilterGraph(_currentInputStream->getAudioCodec());
87+
_filterGraph = new FilterGraph(inputStream.getAudioCodec());
8988

90-
AudioFrameDesc inputFrameDesc(_currentInputStream->getAudioCodec().getAudioFrameDesc());
89+
AudioFrameDesc inputFrameDesc(inputStream.getAudioCodec().getAudioFrameDesc());
9190

9291
// generator decoder
9392
_generators.push_back(new AudioGenerator(inputFrameDesc));
@@ -101,14 +100,14 @@ StreamTranscoder::StreamTranscoder(IInputStream& inputStream, IOutputFile& outpu
101100
_transform = new AudioTransform();
102101

103102
// output encoder
104-
AudioEncoder* outputAudio = new AudioEncoder(_currentInputStream->getAudioCodec().getCodecName());
103+
AudioEncoder* outputAudio = new AudioEncoder(inputStream.getAudioCodec().getCodecName());
105104
outputAudio->setupAudioEncoder(inputFrameDesc);
106105
_outputEncoder = outputAudio;
107106
}
108107

109108
catch(std::runtime_error& e)
110109
{
111-
LOG_WARN("Cannot create the audio encoder for stream " << _currentInputStream->getStreamIndex() << " if needed. "
110+
LOG_WARN("Cannot create the audio encoder for stream " << inputStream.getStreamIndex() << " if needed. "
112111
<< e.what())
113112
}
114113

@@ -117,7 +116,7 @@ StreamTranscoder::StreamTranscoder(IInputStream& inputStream, IOutputFile& outpu
117116
case AVMEDIA_TYPE_DATA:
118117
{
119118
// @warning: rewrap a data stream can't be lengthen by a generator (end of rewrapping will end the all process)
120-
_outputStream = &outputFile.addDataStream(_currentInputStream->getDataCodec());
119+
_outputStream = &outputFile.addDataStream(inputStream.getDataCodec());
121120
break;
122121
}
123122
default:
@@ -130,7 +129,6 @@ StreamTranscoder::StreamTranscoder(const InputStreamDesc& inputStreamDesc, IInpu
130129
const ProfileLoader::Profile& profile, const float offset)
131130
: _inputStreamDesc()
132131
, _inputStreams()
133-
, _currentInputStream(&inputStream)
134132
, _outputStream(NULL)
135133
, _decodedData()
136134
, _filteredData()
@@ -145,15 +143,15 @@ StreamTranscoder::StreamTranscoder(const InputStreamDesc& inputStreamDesc, IInpu
145143
, _needToSwitchToGenerator(false)
146144
{
147145
_inputStreamDesc.push_back(inputStreamDesc);
148-
_inputStreams.push_back(_currentInputStream);
146+
_inputStreams.push_back(&inputStream);
149147

150148
// create a transcode case
151-
switch(_currentInputStream->getProperties().getStreamType())
149+
switch(inputStream.getProperties().getStreamType())
152150
{
153151
case AVMEDIA_TYPE_VIDEO:
154152
{
155153
// filter
156-
_filterGraph = new FilterGraph(_currentInputStream->getVideoCodec());
154+
_filterGraph = new FilterGraph(inputStream.getVideoCodec());
157155

158156
// input decoder
159157
VideoDecoder* inputVideo = new VideoDecoder(*static_cast<InputStream*>(&inputStream));
@@ -165,16 +163,16 @@ StreamTranscoder::StreamTranscoder(const InputStreamDesc& inputStreamDesc, IInpu
165163
VideoEncoder* outputVideo = new VideoEncoder(profile.at(constants::avProfileCodec));
166164
_outputEncoder = outputVideo;
167165

168-
VideoFrameDesc outputFrameDesc = _currentInputStream->getVideoCodec().getVideoFrameDesc();
166+
VideoFrameDesc outputFrameDesc = inputStream.getVideoCodec().getVideoFrameDesc();
169167
outputFrameDesc.setParameters(profile);
170168
outputVideo->setupVideoEncoder(outputFrameDesc, profile);
171169

172170
// output stream
173171
_outputStream = &outputFile.addVideoStream(outputVideo->getVideoCodec());
174172

175173
// buffers to process
176-
_decodedData.push_back(VideoFrame(_currentInputStream->getVideoCodec().getVideoFrameDesc()));
177-
_filteredData = VideoFrame(_currentInputStream->getVideoCodec().getVideoFrameDesc());
174+
_decodedData.push_back(VideoFrame(inputStream.getVideoCodec().getVideoFrameDesc()));
175+
_filteredData = VideoFrame(inputStream.getVideoCodec().getVideoFrameDesc());
178176
_transformedData = VideoFrame(outputVideo->getVideoCodec().getVideoFrameDesc());
179177

180178
// transform
@@ -188,7 +186,7 @@ StreamTranscoder::StreamTranscoder(const InputStreamDesc& inputStreamDesc, IInpu
188186
case AVMEDIA_TYPE_AUDIO:
189187
{
190188
// filter
191-
_filterGraph = new FilterGraph(_currentInputStream->getAudioCodec());
189+
_filterGraph = new FilterGraph(inputStream.getAudioCodec());
192190

193191
// input decoder
194192
AudioDecoder* inputAudio = new AudioDecoder(*static_cast<InputStream*>(&inputStream));
@@ -200,7 +198,7 @@ StreamTranscoder::StreamTranscoder(const InputStreamDesc& inputStreamDesc, IInpu
200198
AudioEncoder* outputAudio = new AudioEncoder(profile.at(constants::avProfileCodec));
201199
_outputEncoder = outputAudio;
202200

203-
AudioFrameDesc outputFrameDesc(_currentInputStream->getAudioCodec().getAudioFrameDesc());
201+
AudioFrameDesc outputFrameDesc(inputStream.getAudioCodec().getAudioFrameDesc());
204202
outputFrameDesc.setParameters(profile);
205203
if(inputStreamDesc.demultiplexing())
206204
outputFrameDesc._nbChannels = inputStreamDesc._channelIndexArray.size();
@@ -210,7 +208,7 @@ StreamTranscoder::StreamTranscoder(const InputStreamDesc& inputStreamDesc, IInpu
210208
_outputStream = &outputFile.addAudioStream(outputAudio->getAudioCodec());
211209

212210
// buffers to process
213-
AudioFrameDesc inputFrameDesc(_currentInputStream->getAudioCodec().getAudioFrameDesc());
211+
AudioFrameDesc inputFrameDesc(inputStream.getAudioCodec().getAudioFrameDesc());
214212
if(inputStreamDesc.demultiplexing())
215213
inputFrameDesc._nbChannels = inputStreamDesc._channelIndexArray.size();
216214

@@ -238,7 +236,6 @@ StreamTranscoder::StreamTranscoder(const InputStreamDesc& inputStreamDesc, IInpu
238236
StreamTranscoder::StreamTranscoder(IOutputFile& outputFile, const ProfileLoader::Profile& profile)
239237
: _inputStreamDesc()
240238
, _inputStreams()
241-
, _currentInputStream(NULL)
242239
, _outputStream(NULL)
243240
, _decodedData()
244241
, _filteredData()
@@ -345,13 +342,8 @@ void StreamTranscoder::preProcessCodecLatency()
345342
if(!_outputEncoder)
346343
{
347344
std::stringstream msg;
348-
msg << "No encoder found for input stream ";
349-
if(getProcessCase() == eProcessCaseGenerator)
350-
msg << "generator";
351-
else
352-
msg << _currentInputStream->getStreamIndex();
353-
msg << ": will not preProcessCodecLatency.";
354-
LOG_WARN(msg.str())
345+
msg << "No encoder found: will not preProcessCodecLatency.";
346+
LOG_INFO(msg.str())
355347
return;
356348
}
357349

@@ -419,8 +411,11 @@ bool StreamTranscoder::processFrame()
419411
}
420412
else if(_offset < 0)
421413
{
422-
const bool endOfStream =
423-
_outputStream->getStreamDuration() >= (_currentInputStream->getProperties().getDuration() + _offset);
414+
bool endOfStream = false;
415+
for(size_t index = 0; index < _inputStreams.size(); ++index)
416+
{
417+
endOfStream = endOfStream && _outputStream->getStreamDuration() >= (_inputStreams.at(index)->getProperties().getDuration() + _offset);
418+
}
424419
if(endOfStream)
425420
{
426421
LOG_INFO("End of negative offset")
@@ -438,13 +433,13 @@ bool StreamTranscoder::processFrame()
438433

439434
bool StreamTranscoder::processRewrap()
440435
{
441-
assert(_currentInputStream != NULL);
436+
assert(_inputStreams.size() == 1);
442437
assert(_outputStream != NULL);
443438

444439
LOG_DEBUG("StreamTranscoder::processRewrap")
445440

446441
CodedData data;
447-
if(!_currentInputStream->readNextPacket(data))
442+
if(! _inputStreams.at(0)->readNextPacket(data))
448443
{
449444
if(_needToSwitchToGenerator)
450445
{
@@ -480,11 +475,14 @@ bool StreamTranscoder::processTranscode()
480475
LOG_DEBUG("StreamTranscoder::processTranscode")
481476

482477
LOG_DEBUG("Decode next frame")
483-
bool decodingStatus = false;
484-
if(! _inputStreamDesc.empty() && _inputStreamDesc.at(0).demultiplexing())
485-
decodingStatus = _currentDecoder->decodeNextFrame(_decodedData.at(0), _inputStreamDesc.at(0)._channelIndexArray);
486-
else
487-
decodingStatus = _currentDecoder->decodeNextFrame(_decodedData.at(0));
478+
bool decodingStatus = true;
479+
for(size_t index = 0; index < _inputDecoders.size(); ++index)
480+
{
481+
if(! _inputStreamDesc.empty() && _inputStreamDesc.at(index).demultiplexing())
482+
decodingStatus = decodingStatus && _currentDecoder->decodeNextFrame(_decodedData.at(index), _inputStreamDesc.at(index)._channelIndexArray);
483+
else
484+
decodingStatus = decodingStatus && _currentDecoder->decodeNextFrame(_decodedData.at(index));
485+
}
488486

489487
CodedData data;
490488
if(decodingStatus)
@@ -548,11 +546,17 @@ float StreamTranscoder::getDuration() const
548546
{
549547
if(! _inputStreams.empty())
550548
{
551-
const StreamProperties& streamProperties = _currentInputStream->getProperties();
552-
const float totalDuration = streamProperties.getDuration() + _offset;
549+
float minStreamDuration = -1;
550+
for(size_t index = 0; index < _inputStreams.size(); ++index)
551+
{
552+
const StreamProperties& streamProperties = _inputStreams.at(index)->getProperties();
553+
if(minStreamDuration == -1 || streamProperties.getDuration() < minStreamDuration)
554+
minStreamDuration = streamProperties.getDuration();
555+
}
556+
const float totalDuration = minStreamDuration + _offset;
553557
if(totalDuration < 0)
554558
{
555-
LOG_WARN("Offset of " << _offset << "s applied to a stream with a duration of " << streamProperties.getDuration()
559+
LOG_WARN("Offset of " << _offset << "s applied to a stream with a duration of " << minStreamDuration
556560
<< "s. Set its duration to 0s.")
557561
return 0.;
558562
}
@@ -575,7 +579,7 @@ void StreamTranscoder::needToSwitchToGenerator(const bool needToSwitch)
575579
if(needToSwitch && !canSwitchToGenerator())
576580
{
577581
std::stringstream os;
578-
os << "The stream at index " << _currentInputStream->getStreamIndex() << " has a duration of " << getDuration() << "s.";
582+
os << "The stream has a duration of " << getDuration() << "s.";
579583
os << " It needs to switch to a generator during the process, but it cannot. ";
580584
throw std::runtime_error(os.str());
581585
}

src/AvTranscoder/transcoder/StreamTranscoder.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class AvExport StreamTranscoder
6565
//@}
6666

6767
/**
68-
* @brief Get the total duration (in seconds), ie. duration of the stream and the offset applies
68+
* @brief Get the total duration (in seconds), ie. duration of the shortest input stream and the offset applies
6969
* @note if it's a generated stream, return limit of double.
7070
* @note if offset > duration of the stream, return 0
7171
*/
@@ -83,7 +83,7 @@ class AvExport StreamTranscoder
8383
FilterGraph* getFilterGraph() const { return _filterGraph; }
8484

8585
/// Returns a pointer to the stream which unwraps data
86-
IInputStream* getCurrentInputStream() const { return _currentInputStream; }
86+
std::vector<IInputStream*> getInputStreams() const { return _inputStreams; }
8787
/// Returns a reference to the stream which wraps data
8888
IOutputStream& getOutputStream() const { return *_outputStream; }
8989

@@ -124,7 +124,6 @@ class AvExport StreamTranscoder
124124
private:
125125
std::vector<InputStreamDesc> _inputStreamDesc; ///< Description of the data to extract from the input stream.
126126
std::vector<IInputStream*> _inputStreams; ///< List of input stream to read next packet (has link, no ownership)
127-
IInputStream* _currentInputStream; ///< Current input stream (has link, no ownership)
128127
IOutputStream* _outputStream; ///< Output stream to wrap next packet (has link, no ownership)
129128

130129
std::vector<Frame> _decodedData; ///< List of buffers of decoded data.

src/AvTranscoder/transcoder/Transcoder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ void Transcoder::fillProcessStat(ProcessStat& processStat)
463463
for(size_t streamIndex = 0; streamIndex < _streamTranscoders.size(); ++streamIndex)
464464
{
465465
IOutputStream& stream = _streamTranscoders.at(streamIndex)->getOutputStream();
466-
const IInputStream* inputStream = _streamTranscoders.at(streamIndex)->getCurrentInputStream();
466+
const IInputStream* inputStream = _streamTranscoders.at(streamIndex)->getInputStreams().at(0);
467467
if(inputStream == NULL)
468468
{
469469
LOG_WARN("Cannot process statistics of generated stream.")

0 commit comments

Comments
 (0)