Skip to content

Video/AudioDecoder: fix the codec state when calling decoder destructor #296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/AvTranscoder/codec/ICodec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ ICodec::ICodec(const ECodecType type, AVCodecContext& avCodecContext)

ICodec::~ICodec()
{
avcodec_close(_avCodecContext);

if(!_isCodecContextAllocated)
return;

Expand Down Expand Up @@ -73,14 +71,21 @@ void ICodec::openCodec()
msg += ") ";
}

avcodec_close(_avCodecContext);
closeCodec();

msg += getDescriptionFromErrorCode(ret);

throw std::runtime_error(msg);
}
}

void ICodec::closeCodec()
{
if(!_avCodecContext)
throw std::runtime_error("Unable to close a codec without codec context");
avcodec_close(_avCodecContext);
}

std::string ICodec::getCodecName() const
{
assert(_avCodecContext != NULL);
Expand Down
2 changes: 2 additions & 0 deletions src/AvTranscoder/codec/ICodec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class AvExport ICodec

/// Initialize the codec context.
void openCodec();
/// Reset the codec context.
void closeCodec();

std::string getCodecName() const;
AVCodecID getCodecId() const;
Expand Down
4 changes: 3 additions & 1 deletion src/AvTranscoder/decoder/AudioDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ AudioDecoder::AudioDecoder(InputStream& inputStream)

AudioDecoder::~AudioDecoder()
{
if(_isSetup)
_inputStream->getAudioCodec().closeCodec();
}

void AudioDecoder::setupDecoder(const ProfileLoader::Profile& profile)
Expand Down Expand Up @@ -73,7 +75,7 @@ void AudioDecoder::setupDecoder(const ProfileLoader::Profile& profile)
}

// open decoder
_inputStream->getAudioCodec().openCodec();
codec.openCodec();
_isSetup = true;
}

Expand Down
4 changes: 3 additions & 1 deletion src/AvTranscoder/decoder/VideoDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ VideoDecoder::VideoDecoder(InputStream& inputStream)

VideoDecoder::~VideoDecoder()
{
if(_isSetup)
_inputStream->getVideoCodec().closeCodec();
}

void VideoDecoder::setupDecoder(const ProfileLoader::Profile& profile)
Expand Down Expand Up @@ -70,7 +72,7 @@ void VideoDecoder::setupDecoder(const ProfileLoader::Profile& profile)
}

// open decoder
_inputStream->getVideoCodec().openCodec();
codec.openCodec();
_isSetup = true;
}

Expand Down
6 changes: 3 additions & 3 deletions src/AvTranscoder/transcoder/Transcoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ Transcoder::Transcoder(IOutputFile& outputFile)

Transcoder::~Transcoder()
{
for(std::vector<InputFile*>::iterator it = _inputFiles.begin(); it != _inputFiles.end(); ++it)
for(std::vector<StreamTranscoder*>::iterator it = _streamTranscodersAllocated.begin();
it != _streamTranscodersAllocated.end(); ++it)
{
delete(*it);
}
for(std::vector<StreamTranscoder*>::iterator it = _streamTranscodersAllocated.begin();
it != _streamTranscodersAllocated.end(); ++it)
for(std::vector<InputFile*>::iterator it = _inputFiles.begin(); it != _inputFiles.end(); ++it)
{
delete(*it);
}
Expand Down
7 changes: 5 additions & 2 deletions test/pyTest/testAudioReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,18 @@ def testAudioReaderChannelsExtraction():

assert_equals( sizeOfFrameWithAllChannels / nbChannels, sizeOfFrameWithOneChannels )

# Force to call the readers destructor before the inputFile destructor (which cannot happen in C++)
readerOfAllChannels = None
readerOfOneChannel = None


def testAudioReaderWithGenerator():
"""
Read an audio stream with the AudioReader.
When there is no more data to decode, switch to a generator and process some frames.
"""
inputFileName = os.environ['AVTRANSCODER_TEST_AUDIO_WAVE_FILE']
inputFile = av.InputFile(inputFileName)
reader = av.AudioReader(inputFile)
reader = av.AudioReader(inputFileName)

# read all frames and check their size
while True:
Expand Down
4 changes: 3 additions & 1 deletion test/pyTest/testVideoReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def testVideoReaderCreateNewInputFile():
assert_equals( frame.getDataSize(), reader.getOutputWidth() * reader.getOutputHeight() * bytesPerPixel )

# check if there is no next frame
frame = reader.readNextFrame()
assert_equals( reader.readNextFrame(), None )


Expand All @@ -47,6 +46,9 @@ def testVideoReaderReferenceInputFile():
# check if there is no next frame
assert_equals( reader.readNextFrame(), None )

# Force to call the reader destructor before the inputFile destructor (which cannot happen in C++)
reader = None


def testVideoReaderWithGenerator():
"""
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy