]> git.sesse.net Git - nageru/blobdiff - player.cpp
Microoptimization in the SOR fragment shader.
[nageru] / player.cpp
index a2c1a9371edcc8a465ddea7e1884b370e1890ef3..9f524bb884cdc0b36a46bb3069f7092224a4e6be 100644 (file)
@@ -7,10 +7,8 @@
 
 #include "clip_list.h"
 #include "defs.h"
-#include "mainwindow.h"
-#include "ffmpeg_raii.h"
-#include "post_to_main_thread.h"
-#include "ui_mainwindow.h"
+#include "jpeg_frame_view.h"
+#include "player.h"
 
 using namespace std;
 using namespace std::chrono;
@@ -18,92 +16,138 @@ using namespace std::chrono;
 extern mutex frame_mu;
 extern vector<int64_t> frames[MAX_STREAMS];
 
-struct PlaylistClip {
-       Clip clip;
-       unsigned stream_idx;
-};
-vector<PlaylistClip> current_cue_playlist;
-mutex playlist_mu;
-
-enum { PAUSED, PLAYING } cue_state = PAUSED;
-mutex cue_state_mu;
-condition_variable cue_is_playing;
-//int cue_playlist_index = -1;
-//int64_t cue_playlist_pos = 0;
-
-int preview_thread_func()
+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, []{
-                               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;
+                       playing = true;
                }
 
-               PlaylistClip clip;
+               Clip clip;
+               unsigned stream_idx;
                {
-                       lock_guard<mutex> lock2(playlist_mu);
-                       clip = current_cue_playlist[0];
+                       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.clip.pts_in;
+               int64_t pts_origin = clip.pts_in;
 
-               int64_t next_pts = pts_origin;
+               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> lock(frame_mu);
+                               auto it = upper_bound(frames[stream_idx].begin(),
+                                       frames[stream_idx].end(),
+                                       next_pts);
+                               if (it == frames[stream_idx].end() || *it >= clip.pts_out) {
+                                       break;
+                               }
+                               next_pts = *it;
+                       }
 
-               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);
-                       global_mainwindow->ui->preview_display->setFrame(clip.stream_idx, next_pts);
 
-                       // Find the next frame.
+                       // Sleep until the next frame start, or until there's a new clip we're supposed to play.
                        {
-                               lock_guard<mutex> lock2(frame_mu);
-                               auto it = upper_bound(frames[clip.stream_idx].begin(),
-                                       frames[clip.stream_idx].end(),
-                                       next_pts);
-                               if (it == frames[clip.stream_idx].end()) {
-                                       eof = true;
-                               } else {
-                                       next_pts = *it;
-                                       if (next_pts >= clip.clip.pts_out) {
-                                               eof = true;
-                                       }
+                               unique_lock<mutex> lock(queue_state_mu);
+                               new_clip_changed.wait_until(lock, next_frame_start, [this]{
+                                       return new_clip_ready || override_stream_idx != -1;
+                               });
+                               if (new_clip_ready) break;
+                               if (override_stream_idx != -1) {
+                                       stream_idx = override_stream_idx;
+                                       override_stream_idx = -1;
+                                       continue;
                                }
                        }
-                       if (eof) break;
+
+                       destination->setFrame(stream_idx, next_pts);
+
                }
 
-               // TODO: advance the playlist and look for the next element.
                {
-                       unique_lock<mutex> lock(cue_state_mu);
-                       cue_state = PAUSED;
+                       unique_lock<mutex> lock(queue_state_mu);
+                       playing = false;
+               }
+               if (done_callback != nullptr && !aborted) {
+                       done_callback();
                }
        }
 }
 
-void start_player_thread()
+Player::Player(JPEGFrameView *destination)
+       : destination(destination)
+{
+       thread(&Player::thread_func, this).detach();
+}
+
+void Player::play_clip(const Clip &clip, unsigned stream_idx)
 {
-       thread(preview_thread_func).detach();
+       {
+               lock_guard<mutex> lock(mu);
+               current_clip = clip;
+               current_stream_idx = stream_idx;
+       }
+
+       {
+               lock_guard<mutex> lock(queue_state_mu);
+               new_clip_ready = true;
+               override_stream_idx = -1;
+               new_clip_changed.notify_all();
+       }
 }
 
-void play_clip(const Clip &clip, unsigned stream_idx)
+void Player::override_angle(unsigned stream_idx)
 {
+       // Corner case: If a new clip is waiting to be played, change its stream and then we're done. 
        {
-               lock_guard<mutex> lock(playlist_mu);
-               current_cue_playlist.clear();
-               current_cue_playlist.push_back(PlaylistClip{ clip, stream_idx });
+               unique_lock<mutex> lock(queue_state_mu);
+               if (new_clip_ready) {
+                       lock_guard<mutex> lock2(mu);
+                       current_stream_idx = stream_idx;
+                       return;
+               }
        }
 
+       // If we are playing a clip, set override_stream_idx, and the player thread will
+       // pick it up and change its internal index.
        {
-               lock_guard<mutex> lock(cue_state_mu);
-               cue_state = PLAYING;
-               cue_is_playing.notify_all();
+               unique_lock<mutex> lock(queue_state_mu);
+               if (playing) {
+                       override_stream_idx = stream_idx;
+                       new_clip_changed.notify_all();
+               }
+       }
+
+       // OK, so we're standing still, presumably at the end of a clip.
+       // Look at the current pts_out (if it exists), and show the closest
+       // thing we've got.
+       int64_t pts_out;
+       {
+               lock_guard<mutex> lock(mu);
+               if (current_clip.pts_out < 0) {
+                       return;
+               }
+               pts_out = current_clip.pts_out;
+       }
+                       
+       lock_guard<mutex> lock(frame_mu);
+       auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts_out);
+       if (it == frames[stream_idx].end()) {
+               return;
        }
+       destination->setFrame(stream_idx, *it);
 }