Commit bdb5015 1 parent fad9ae9 commit bdb5015 Copy full SHA for bdb5015
File tree 2 files changed +23
-6
lines changed
2 files changed +23
-6
lines changed Original file line number Diff line number Diff line change 25
25
#include < thread> // for std::this_thread::sleep_for
26
26
#include < chrono> // for std::chrono::milliseconds
27
27
#include < sstream>
28
+ #include < condition_variable>
29
+ #include < mutex>
28
30
29
31
using namespace juce ;
30
32
@@ -241,16 +243,21 @@ namespace openshot
241
243
}
242
244
}
243
245
244
- // Play the audio
246
+ // Override Play and Stop to notify of state changes
245
247
void AudioPlaybackThread::Play () {
246
- // Start playing
247
248
is_playing = true ;
249
+ NotifyTransportStateChanged ();
248
250
}
249
251
250
- // Stop the audio
251
252
void AudioPlaybackThread::Stop () {
252
- // Stop playing
253
253
is_playing = false ;
254
+ NotifyTransportStateChanged ();
255
+ }
256
+
257
+ void AudioPlaybackThread::NotifyTransportStateChanged ()
258
+ {
259
+ std::lock_guard<std::mutex> lock (transportMutex);
260
+ transportCondition.notify_all ();
254
261
}
255
262
256
263
// Start audio thread
@@ -286,8 +293,13 @@ namespace openshot
286
293
// Start the transport
287
294
transport.start ();
288
295
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
+ }
291
303
292
304
// Stop audio and shutdown transport
293
305
Stop ();
Original file line number Diff line number Diff line change @@ -86,12 +86,17 @@ class AudioDeviceManagerSingleton {
86
86
bool is_playing;
87
87
juce::TimeSliceThread time_thread;
88
88
openshot::VideoCacheThread *videoCache; // / The cache thread (for pre-roll checking)
89
+ std::mutex transportMutex;
90
+ std::condition_variable transportCondition;
89
91
90
92
// / Constructor
91
93
AudioPlaybackThread (openshot::VideoCacheThread* cache);
92
94
// / Destructor
93
95
~AudioPlaybackThread ();
94
96
97
+ // / Notify all
98
+ void NotifyTransportStateChanged ();
99
+
95
100
// / Set the current thread's reader
96
101
void Reader (openshot::ReaderBase *reader);
97
102
You can’t perform that action at this time.
0 commit comments