]> git.sesse.net Git - nageru/blob - player.cpp
Add some very basic playback.
[nageru] / player.cpp
1 #include <algorithm>
2 #include <chrono>
3 #include <condition_variable>
4 #include <mutex>
5 #include <thread>
6 #include <vector>
7
8 #include "clip_list.h"
9 #include "defs.h"
10 #include "mainwindow.h"
11 #include "ffmpeg_raii.h"
12 #include "post_to_main_thread.h"
13 #include "ui_mainwindow.h"
14
15 using namespace std;
16 using namespace std::chrono;
17
18 extern mutex frame_mu;
19 extern vector<int64_t> frames[MAX_STREAMS];
20
21 struct PlaylistClip {
22         Clip clip;
23         unsigned stream_idx;
24 };
25 vector<PlaylistClip> current_cue_playlist;
26 mutex playlist_mu;
27
28 enum { PAUSED, PLAYING } cue_state = PAUSED;
29 mutex cue_state_mu;
30 condition_variable cue_is_playing;
31 //int cue_playlist_index = -1;
32 //int64_t cue_playlist_pos = 0;
33
34 int preview_thread_func()
35 {
36         for ( ;; ) {
37                 // Wait until we're supposed to play something.
38                 {
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();
43                         });
44                 }
45
46                 PlaylistClip clip;
47                 {
48                         lock_guard<mutex> lock2(playlist_mu);
49                         clip = current_cue_playlist[0];
50                 }
51                 steady_clock::time_point origin = steady_clock::now();
52                 int64_t pts_origin = clip.clip.pts_in;
53
54                 int64_t next_pts = pts_origin;
55
56                 bool eof = false;
57                 while (!eof) {  // TODO: check for abort
58                         // FIXME: assumes a given timebase.
59                         double speed = 0.5;
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);
64
65                         // Find the next frame.
66                         {
67                                 lock_guard<mutex> lock2(frame_mu);
68                                 auto it = upper_bound(frames[clip.stream_idx].begin(),
69                                         frames[clip.stream_idx].end(),
70                                         next_pts);
71                                 if (it == frames[clip.stream_idx].end()) {
72                                         eof = true;
73                                 } else {
74                                         next_pts = *it;
75                                         if (next_pts >= clip.clip.pts_out) {
76                                                 eof = true;
77                                         }
78                                 }
79                         }
80                         if (eof) break;
81                 }
82
83                 // TODO: advance the playlist and look for the next element.
84                 {
85                         unique_lock<mutex> lock(cue_state_mu);
86                         cue_state = PAUSED;
87                 }
88         }
89 }
90
91 void start_player_thread()
92 {
93         thread(preview_thread_func).detach();
94 }
95
96 void play_clip(const Clip &clip, unsigned stream_idx)
97 {
98         {
99                 lock_guard<mutex> lock(playlist_mu);
100                 current_cue_playlist.clear();
101                 current_cue_playlist.push_back(PlaylistClip{ clip, stream_idx });
102         }
103
104         {
105                 lock_guard<mutex> lock(cue_state_mu);
106                 cue_state = PLAYING;
107                 cue_is_playing.notify_all();
108         }
109 }