From 121cfd342dbc95f0ca1e49ba3152721004d92adc Mon Sep 17 00:00:00 2001 From: Jonathan Thomas Date: Fri, 4 Dec 2015 01:10:40 -0600 Subject: [PATCH] Fixed a crash when stopping the QtPlayer class (the cache thread was not being correctly ended, and the audio timeslice thread was not being ended). Also fixed a crash when closing the openshot-player executable. --- include/Qt/PlayerDemo.h | 1 + include/Qt/VideoCacheThread.h | 2 +- src/Qt/AudioPlaybackThread.cpp | 9 +++++---- src/Qt/PlayerDemo.cpp | 9 +++++++++ src/Qt/PlayerPrivate.cpp | 2 +- src/Qt/VideoCacheThread.cpp | 2 +- src/QtPlayer.cpp | 11 +++++++++-- 7 files changed, 27 insertions(+), 9 deletions(-) diff --git a/include/Qt/PlayerDemo.h b/include/Qt/PlayerDemo.h index a3e18bd8..c02f863c 100644 --- a/include/Qt/PlayerDemo.h +++ b/include/Qt/PlayerDemo.h @@ -55,6 +55,7 @@ public: protected: void keyPressEvent(QKeyEvent *event); + void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE; private slots: void open(bool checked); diff --git a/include/Qt/VideoCacheThread.h b/include/Qt/VideoCacheThread.h index be3a71ca..7a3f9c60 100644 --- a/include/Qt/VideoCacheThread.h +++ b/include/Qt/VideoCacheThread.h @@ -80,7 +80,7 @@ namespace openshot void run(); /// Set the current thread's reader - void Reader(ReaderBase *new_reader) { reader=new_reader; }; + void Reader(ReaderBase *new_reader) { reader=new_reader; Play(); }; /// Parent class of VideoCacheThread friend class PlayerPrivate; diff --git a/src/Qt/AudioPlaybackThread.cpp b/src/Qt/AudioPlaybackThread.cpp index fae5c57c..dfca232f 100644 --- a/src/Qt/AudioPlaybackThread.cpp +++ b/src/Qt/AudioPlaybackThread.cpp @@ -100,7 +100,7 @@ namespace openshot source = new AudioReaderSource(reader, 1, buffer_size); source->setLooping(true); // prevent this source from terminating when it reaches the end - // Play the video + // Mark as 'playing' Play(); } @@ -176,12 +176,13 @@ namespace openshot transport.start(); while (!threadShouldExit() && transport.isPlaying() && is_playing) - sleep(100); + sleep(100); is_playing = false; - } - sleep(250); + // Stop time slice thread + thread.stopThread(-1); + } } } diff --git a/src/Qt/PlayerDemo.cpp b/src/Qt/PlayerDemo.cpp index 19a40b0a..8c5d267e 100644 --- a/src/Qt/PlayerDemo.cpp +++ b/src/Qt/PlayerDemo.cpp @@ -62,6 +62,15 @@ PlayerDemo::~PlayerDemo() { } +void PlayerDemo::closeEvent(QCloseEvent *event) +{ + // Close window, stop player, and quit + QWidget *pWin = QApplication::activeWindow(); + pWin->hide(); + player->Stop(); + QApplication::quit(); +} + void PlayerDemo::keyPressEvent(QKeyEvent *event) { if (event->key() == Qt::Key_Space || event->key() == Qt::Key_K) { diff --git a/src/Qt/PlayerPrivate.cpp b/src/Qt/PlayerPrivate.cpp index 647fb5b8..db788ed3 100644 --- a/src/Qt/PlayerPrivate.cpp +++ b/src/Qt/PlayerPrivate.cpp @@ -130,7 +130,7 @@ namespace openshot if (sleep_time > 0) sleep(sleep_time); // Debug output - std::cout << "video frame diff: " << video_frame_diff << std::endl; + std::cout << "frame: " << video_position << ", video frame diff: " << video_frame_diff << std::endl; } } diff --git a/src/Qt/VideoCacheThread.cpp b/src/Qt/VideoCacheThread.cpp index 1c7dafa8..5073a4b9 100644 --- a/src/Qt/VideoCacheThread.cpp +++ b/src/Qt/VideoCacheThread.cpp @@ -77,7 +77,7 @@ namespace openshot // Start the thread void VideoCacheThread::run() { - while (!threadShouldExit()) { + while (!threadShouldExit() && is_playing) { // Calculate sleep time for frame rate double frame_time = (1000.0 / reader->info.fps.ToDouble()); diff --git a/src/QtPlayer.cpp b/src/QtPlayer.cpp index b40eabc6..6b887262 100644 --- a/src/QtPlayer.cpp +++ b/src/QtPlayer.cpp @@ -137,9 +137,16 @@ void QtPlayer::Seek(int new_frame) void QtPlayer::Stop() { - mode = PLAYBACK_STOPPED; - p->stopPlayback(); + // Change mode to stopped + mode = PLAYBACK_STOPPED; + + // Notify threads of stopping p->videoCache->Stop(); + p->audioPlayback->Stop(); + + // Kill all threads + p->stopPlayback(); + p->video_position = 0; threads_started = false; }