diff --git a/src/Http/HlsPlayer.cpp b/src/Http/HlsPlayer.cpp index 60bda337..f22ca7c4 100644 --- a/src/Http/HlsPlayer.cpp +++ b/src/Http/HlsPlayer.cpp @@ -380,10 +380,14 @@ void HlsPlayerImp::onPlayResult(const SockException &ex) { void HlsPlayerImp::onShutdown(const SockException &ex) { while (_demuxer) { try { + //shared_from_this()可能抛异常 std::weak_ptr weak_self = static_pointer_cast(shared_from_this()); + if (_decoder) { + _decoder->flush(); + } + //等待所有frame flush输出后,再触发onShutdown事件 static_pointer_cast(_demuxer)->pushTask([weak_self, ex]() { - auto strong_self = weak_self.lock(); - if (strong_self) { + if (auto strong_self = weak_self.lock()) { strong_self->_demuxer = nullptr; strong_self->onShutdown(ex); } @@ -393,9 +397,6 @@ void HlsPlayerImp::onShutdown(const SockException &ex) { break; } } - if (_decoder) { - _decoder->flush(); - } PlayerImp::onShutdown(ex); } diff --git a/src/Http/TsplayerImp.cpp b/src/Http/TsplayerImp.cpp index d41fcb97..05db8a61 100644 --- a/src/Http/TsplayerImp.cpp +++ b/src/Http/TsplayerImp.cpp @@ -47,10 +47,14 @@ void TsPlayerImp::onPlayResult(const SockException &ex) { void TsPlayerImp::onShutdown(const SockException &ex) { while (_demuxer) { try { + //shared_from_this()可能抛异常 std::weak_ptr weak_self = static_pointer_cast(shared_from_this()); + if (_decoder) { + _decoder->flush(); + } + //等待所有frame flush输出后,再触发onShutdown事件 static_pointer_cast(_demuxer)->pushTask([weak_self, ex]() { - auto strong_self = weak_self.lock(); - if (strong_self) { + if (auto strong_self = weak_self.lock()) { strong_self->_demuxer = nullptr; strong_self->onShutdown(ex); } @@ -60,9 +64,6 @@ void TsPlayerImp::onShutdown(const SockException &ex) { break; } } - if (_decoder) { - _decoder->flush(); - } PlayerImp::onShutdown(ex); }