Skip to content

Commit

Permalink
Experimental change in audio thread sleep/waiting when paused
Browse files Browse the repository at this point in the history
  • Loading branch information
jonoomph committed Dec 9, 2024
1 parent fad9ae9 commit bdb5015
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/Qt/AudioPlaybackThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <thread> // for std::this_thread::sleep_for
#include <chrono> // for std::chrono::milliseconds
#include <sstream>
#include <condition_variable>
#include <mutex>

using namespace juce;

Expand Down Expand Up @@ -241,16 +243,21 @@ namespace openshot
}
}

// Play the audio
// Override Play and Stop to notify of state changes
void AudioPlaybackThread::Play() {
// Start playing
is_playing = true;
NotifyTransportStateChanged();
}

// Stop the audio
void AudioPlaybackThread::Stop() {
// Stop playing
is_playing = false;
NotifyTransportStateChanged();
}

void AudioPlaybackThread::NotifyTransportStateChanged()
{
std::lock_guard<std::mutex> lock(transportMutex);
transportCondition.notify_all();
}

// Start audio thread
Expand Down Expand Up @@ -286,8 +293,13 @@ namespace openshot
// Start the transport
transport.start();

while (!threadShouldExit() && transport.isPlaying() && is_playing)
std::this_thread::sleep_for(std::chrono::milliseconds(2));
while (!threadShouldExit() && transport.isPlaying() && is_playing) {
// Wait until transport state changes or thread should exit
std::unique_lock<std::mutex> lock(transportMutex);
transportCondition.wait_for(lock, std::chrono::milliseconds(10), [this]() {
return threadShouldExit() || !transport.isPlaying() || !is_playing;
});
}

// Stop audio and shutdown transport
Stop();
Expand Down
5 changes: 5 additions & 0 deletions src/Qt/AudioPlaybackThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,17 @@ class AudioDeviceManagerSingleton {
bool is_playing;
juce::TimeSliceThread time_thread;
openshot::VideoCacheThread *videoCache; /// The cache thread (for pre-roll checking)
std::mutex transportMutex;
std::condition_variable transportCondition;

/// Constructor
AudioPlaybackThread(openshot::VideoCacheThread* cache);
/// Destructor
~AudioPlaybackThread();

/// Notify all
void NotifyTransportStateChanged();

/// Set the current thread's reader
void Reader(openshot::ReaderBase *reader);

Expand Down

0 comments on commit bdb5015

Please sign in to comment.