3 #include <condition_variable>
10 #include "mainwindow.h"
11 #include "ffmpeg_raii.h"
12 #include "post_to_main_thread.h"
13 #include "ui_mainwindow.h"
16 using namespace std::chrono;
18 extern mutex frame_mu;
19 extern vector<int64_t> frames[MAX_STREAMS];
25 vector<PlaylistClip> current_cue_playlist;
28 enum { PAUSED, PLAYING } cue_state = PAUSED;
30 condition_variable cue_is_playing;
31 //int cue_playlist_index = -1;
32 //int64_t cue_playlist_pos = 0;
34 int preview_thread_func()
37 // Wait until we're supposed to play something.
39 unique_lock<mutex> lock(cue_state_mu);
40 cue_is_playing.wait(lock, []{
41 return cue_state == PLAYING;
42 //return current_cue_status.origin != steady_clock::time_point::max();
48 lock_guard<mutex> lock2(playlist_mu);
49 clip = current_cue_playlist[0];
51 steady_clock::time_point origin = steady_clock::now();
52 int64_t pts_origin = clip.clip.pts_in;
54 int64_t next_pts = pts_origin;
57 while (!eof) { // TODO: check for abort
58 // FIXME: assumes a given timebase.
60 steady_clock::time_point next_frame_start =
61 origin + microseconds((next_pts - pts_origin) * int(1000000 / speed) / 12800);
62 this_thread::sleep_until(next_frame_start);
63 global_mainwindow->ui->preview_display->setFrame(clip.stream_idx, next_pts);
65 // Find the next frame.
67 lock_guard<mutex> lock2(frame_mu);
68 auto it = upper_bound(frames[clip.stream_idx].begin(),
69 frames[clip.stream_idx].end(),
71 if (it == frames[clip.stream_idx].end()) {
75 if (next_pts >= clip.clip.pts_out) {
83 // TODO: advance the playlist and look for the next element.
85 unique_lock<mutex> lock(cue_state_mu);
91 void start_player_thread()
93 thread(preview_thread_func).detach();
96 void play_clip(const Clip &clip, unsigned stream_idx)
99 lock_guard<mutex> lock(playlist_mu);
100 current_cue_playlist.clear();
101 current_cue_playlist.push_back(PlaylistClip{ clip, stream_idx });
105 lock_guard<mutex> lock(cue_state_mu);
107 cue_is_playing.notify_all();