]> git.sesse.net Git - nageru/blobdiff - player.cpp
Add functionality for scrubbing in/out points in the clip list.
[nageru] / player.cpp
index 6e45f39cd3fc5cc2aaa6ac5b4770ef09b5ac5085..1f2d9e50cadbaa2d7938d4dc5ea0cb26c953d133 100644 (file)
@@ -21,56 +21,59 @@ void Player::thread_func()
        for ( ;; ) {
                // Wait until we're supposed to play something.
                {
-                       unique_lock<mutex> lock(cue_state_mu);
-                       cue_is_playing.wait(lock, [this]{
-                               return cue_state == PLAYING;
-                               //return current_cue_status.origin != steady_clock::time_point::max();
+                       unique_lock<mutex> lock(queue_state_mu);
+                       new_clip_changed.wait(lock, [this]{
+                               return new_clip_ready && current_clip.pts_in != -1;
                        });
+                       new_clip_ready = false;
                }
 
                Clip clip;
                unsigned stream_idx;
                {
-                       lock_guard<mutex> lock2(mu);
+                       lock_guard<mutex> lock(mu);
                        clip = current_clip;
                        stream_idx = current_stream_idx;
                }
                steady_clock::time_point origin = steady_clock::now();
                int64_t pts_origin = clip.pts_in;
 
-               int64_t next_pts = pts_origin;
-
-               bool eof = false;
-               while (!eof) {  // TODO: check for abort
-                       // FIXME: assumes a given timebase.
-                       double speed = 0.5;
-                       steady_clock::time_point next_frame_start =
-                               origin + microseconds((next_pts - pts_origin) * int(1000000 / speed) / 12800);
-                       this_thread::sleep_until(next_frame_start);
-                       destination->setFrame(stream_idx, next_pts);
+               int64_t next_pts = pts_origin - 1;  // Make sure we play the frame at clip.pts_in if it exists.
 
+               bool aborted = false;
+               for ( ;; ) {
                        // Find the next frame.
                        {
-                               lock_guard<mutex> lock2(frame_mu);
+                               lock_guard<mutex> lock(frame_mu);
                                auto it = upper_bound(frames[stream_idx].begin(),
                                        frames[stream_idx].end(),
                                        next_pts);
-                               if (it == frames[stream_idx].end()) {
-                                       eof = true;
-                               } else {
-                                       next_pts = *it;
-                                       if (next_pts >= clip.pts_out) {
-                                               eof = true;
-                                       }
+                               if (it == frames[stream_idx].end() || *it >= clip.pts_out) {
+                                       break;
                                }
+                               next_pts = *it;
+                       }
+
+                       // FIXME: assumes a given timebase.
+                       double speed = 0.5;
+                       steady_clock::time_point next_frame_start =
+                               origin + microseconds((next_pts - pts_origin) * int(1000000 / speed) / 12800);
+
+                       // Sleep until the next frame start, or until there's a new clip we're supposed to play.
+                       {
+                               unique_lock<mutex> lock(queue_state_mu);
+                               aborted = new_clip_changed.wait_until(lock, next_frame_start, [this]{
+                                       return new_clip_ready;
+                               });
+                               if (aborted) break;
                        }
-                       if (eof) break;
+
+                       destination->setFrame(stream_idx, next_pts);
+
                }
 
-               // TODO: callback so that the next playlist item can be cued.
-               {
-                       unique_lock<mutex> lock(cue_state_mu);
-                       cue_state = PAUSED;
+               if (done_callback != nullptr && !aborted) {
+                       done_callback();
                }
        }
 }
@@ -90,8 +93,8 @@ void Player::play_clip(const Clip &clip, unsigned stream_idx)
        }
 
        {
-               lock_guard<mutex> lock(cue_state_mu);
-               cue_state = PLAYING;
-               cue_is_playing.notify_all();
+               lock_guard<mutex> lock(queue_state_mu);
+               new_clip_ready = true;
+               new_clip_changed.notify_all();
        }
 }