Skip to content

Commit

Permalink
reset decoders after unload
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-bin committed Nov 15, 2014
1 parent 55ba8ab commit b2006a2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/AVDemuxer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ bool AVDemuxer::close()
if (auto_reset_stream) {
wanted_audio_stream = wanted_subtitle_stream = wanted_video_stream = -1;
}
a_codec_context = v_codec_context = s_codec_contex = 0;
audio_stream = video_stream = subtitle_stream = -2;
audio_streams.clear();
video_streams.clear();
Expand Down Expand Up @@ -1166,7 +1167,6 @@ void AVDemuxer::handleError(int averr, AVError::ErrorCode *errorCode, QString &m
if (interrupted) { // interrupted by callback, so can not determine whether the media is valid
if (mediaStatus() == LoadingMedia)
setMediaStatus(UnknownMediaStatus);
qDebug("interrupted!!!!!");
if (getInterruptStatus())
err_msg += " [" + tr("interrupted by user") + "]";
else
Expand Down
45 changes: 26 additions & 19 deletions src/AVPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,18 +472,19 @@ bool AVPlayer::load(bool reload)
// TODO: call unload if reload?
if (mediaStatus() == QtAV::LoadingMedia) //async loading
return true;
if (isLoaded()) {
// release codec ctx. if not loaded, they are released by avformat. TODO: always let avformat release them?
if (d->adec)
d->adec->setCodecContext(0);
if (d->vdec)
d->vdec->setCodecContext(0);
}
d->loaded = false;
if (!d->current_source.isValid()) {
qDebug("Invalid media source. No file or IODevice was set.");
return false;
}
// release codec ctx
if (d->adec) {
d->adec->setCodecContext(0);
}
if (d->vdec) {
d->vdec->setCodecContext(0);
}

qDebug() << "Loading " << d->current_source << " ...";
if (reload || !d->demuxer.isLoaded(d->current_source.toString())) {
if (isAsyncLoad()) {
Expand Down Expand Up @@ -513,18 +514,12 @@ bool AVPlayer::load(bool reload)
void AVPlayer::loadInternal()
{
// release codec ctx
if (d->adec) {
d->adec->setCodecContext(0);
}
if (d->vdec) {
d->vdec->setCodecContext(0);
}
//close decoders here to make sure open and close in the same thread
if (d->adec && d->adec->isOpen()) {
d->adec->close();
}
if (d->vdec && d->vdec->isOpen()) {
d->vdec->close();
//close decoders here to make sure open and close in the same thread if not async load
if (isLoaded()) {
if (d->adec)
d->adec->setCodecContext(0);
if (d->vdec)
d->vdec->setCodecContext(0);
}
if (d->current_source.type() == QVariant::String) {
d->loaded = d->demuxer.loadFile(d->current_source.toString());
Expand Down Expand Up @@ -557,6 +552,18 @@ void AVPlayer::unloadInternal()
disconnect(&d->demuxer, SIGNAL(loaded()), this, SLOT(unloadInternal()));
disconnect(&d->demuxer, SIGNAL(userInterrupted()), this, SLOT(unloadInternal()));

d->loaded = false;
if (d->adec) {
d->adec->setCodecContext(0);
delete d->adec;
d->adec = 0;
}
if (d->vdec) {
d->vdec->setCodecContext(0);
delete d->vdec;
d->vdec = 0;
}

d->demuxer.close();
}

Expand Down
2 changes: 1 addition & 1 deletion src/AVPlayerPrivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,6 @@ bool AVPlayer::Private::setupAudioThread(AVPlayer *player)
qDebug("new audio thread");
athread = new AudioThread(player);
athread->setClock(clock);
athread->setDecoder(adec);
athread->setStatistics(&statistics);
athread->setOutputSet(aos);
qDebug("demux thread setAudioThread");
Expand All @@ -324,6 +323,7 @@ bool AVPlayer::Private::setupAudioThread(AVPlayer *player)
}
}
}
athread->setDecoder(adec);
player->setAudioOutput(ao);
int queue_min = 0.61803*qMax<qreal>(24.0, statistics.video_only.fps_guess);
int queue_max = int(1.61803*(qreal)queue_min); //about 1 second
Expand Down

0 comments on commit b2006a2

Please sign in to comment.