Skip to content

Commit bdb5015

Browse files
committed
Experimental change in audio thread sleep/waiting when paused
1 parent fad9ae9 commit bdb5015

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/Qt/AudioPlaybackThread.cpp

+18-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include <thread> // for std::this_thread::sleep_for
2626
#include <chrono> // for std::chrono::milliseconds
2727
#include <sstream>
28+
#include <condition_variable>
29+
#include <mutex>
2830

2931
using namespace juce;
3032

@@ -241,16 +243,21 @@ namespace openshot
241243
}
242244
}
243245

244-
// Play the audio
246+
// Override Play and Stop to notify of state changes
245247
void AudioPlaybackThread::Play() {
246-
// Start playing
247248
is_playing = true;
249+
NotifyTransportStateChanged();
248250
}
249251

250-
// Stop the audio
251252
void AudioPlaybackThread::Stop() {
252-
// Stop playing
253253
is_playing = false;
254+
NotifyTransportStateChanged();
255+
}
256+
257+
void AudioPlaybackThread::NotifyTransportStateChanged()
258+
{
259+
std::lock_guard<std::mutex> lock(transportMutex);
260+
transportCondition.notify_all();
254261
}
255262

256263
// Start audio thread
@@ -286,8 +293,13 @@ namespace openshot
286293
// Start the transport
287294
transport.start();
288295

289-
while (!threadShouldExit() && transport.isPlaying() && is_playing)
290-
std::this_thread::sleep_for(std::chrono::milliseconds(2));
296+
while (!threadShouldExit() && transport.isPlaying() && is_playing) {
297+
// Wait until transport state changes or thread should exit
298+
std::unique_lock<std::mutex> lock(transportMutex);
299+
transportCondition.wait_for(lock, std::chrono::milliseconds(10), [this]() {
300+
return threadShouldExit() || !transport.isPlaying() || !is_playing;
301+
});
302+
}
291303

292304
// Stop audio and shutdown transport
293305
Stop();

src/Qt/AudioPlaybackThread.h

+5
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,17 @@ class AudioDeviceManagerSingleton {
8686
bool is_playing;
8787
juce::TimeSliceThread time_thread;
8888
openshot::VideoCacheThread *videoCache; /// The cache thread (for pre-roll checking)
89+
std::mutex transportMutex;
90+
std::condition_variable transportCondition;
8991

9092
/// Constructor
9193
AudioPlaybackThread(openshot::VideoCacheThread* cache);
9294
/// Destructor
9395
~AudioPlaybackThread();
9496

97+
/// Notify all
98+
void NotifyTransportStateChanged();
99+
95100
/// Set the current thread's reader
96101
void Reader(openshot::ReaderBase *reader);
97102

0 commit comments

Comments
 (0)