]> git.sesse.net Git - nageru/blob - player.cpp
Some refactoring of the player code, and begin working on the playlist.
[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 "jpeg_frame_view.h"
11 #include "player.h"
12
13 using namespace std;
14 using namespace std::chrono;
15
16 extern mutex frame_mu;
17 extern vector<int64_t> frames[MAX_STREAMS];
18
19 void Player::thread_func()
20 {
21         for ( ;; ) {
22                 // Wait until we're supposed to play something.
23                 {
24                         unique_lock<mutex> lock(cue_state_mu);
25                         cue_is_playing.wait(lock, [this]{
26                                 return cue_state == PLAYING;
27                                 //return current_cue_status.origin != steady_clock::time_point::max();
28                         });
29                 }
30
31                 Clip clip;
32                 unsigned stream_idx;
33                 {
34                         lock_guard<mutex> lock2(mu);
35                         clip = current_clip;
36                         stream_idx = current_stream_idx;
37                 }
38                 steady_clock::time_point origin = steady_clock::now();
39                 int64_t pts_origin = clip.pts_in;
40
41                 int64_t next_pts = pts_origin;
42
43                 bool eof = false;
44                 while (!eof) {  // TODO: check for abort
45                         // FIXME: assumes a given timebase.
46                         double speed = 0.5;
47                         steady_clock::time_point next_frame_start =
48                                 origin + microseconds((next_pts - pts_origin) * int(1000000 / speed) / 12800);
49                         this_thread::sleep_until(next_frame_start);
50                         destination->setFrame(stream_idx, next_pts);
51
52                         // Find the next frame.
53                         {
54                                 lock_guard<mutex> lock2(frame_mu);
55                                 auto it = upper_bound(frames[stream_idx].begin(),
56                                         frames[stream_idx].end(),
57                                         next_pts);
58                                 if (it == frames[stream_idx].end()) {
59                                         eof = true;
60                                 } else {
61                                         next_pts = *it;
62                                         if (next_pts >= clip.pts_out) {
63                                                 eof = true;
64                                         }
65                                 }
66                         }
67                         if (eof) break;
68                 }
69
70                 // TODO: callback so that the next playlist item can be cued.
71                 {
72                         unique_lock<mutex> lock(cue_state_mu);
73                         cue_state = PAUSED;
74                 }
75         }
76 }
77
78 Player::Player(JPEGFrameView *destination)
79         : destination(destination)
80 {
81         thread(&Player::thread_func, this).detach();
82 }
83
84 void Player::play_clip(const Clip &clip, unsigned stream_idx)
85 {
86         {
87                 lock_guard<mutex> lock(mu);
88                 current_clip = clip;
89                 current_stream_idx = stream_idx;
90         }
91
92         {
93                 lock_guard<mutex> lock(cue_state_mu);
94                 cue_state = PLAYING;
95                 cue_is_playing.notify_all();
96         }
97 }