]> git.sesse.net Git - nageru/blob - player.cpp
Make it possible to start a new clip before the previous one has ended.
[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(queue_state_mu);
25                         new_clip_changed.wait(lock, [this]{
26                                 return new_clip_ready && current_clip.pts_in != -1;
27                         });
28                         new_clip_ready = false;
29                 }
30
31                 Clip clip;
32                 unsigned stream_idx;
33                 {
34                         lock_guard<mutex> lock(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, aborted = false;
44                 while (!eof) {
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
50                         // Sleep until the next frame start, or until there's a new clip we're supposed to play.
51                         {
52                                 unique_lock<mutex> lock(queue_state_mu);
53                                 aborted = new_clip_changed.wait_until(lock, next_frame_start, [this]{
54                                         return new_clip_ready;
55                                 });
56                                 eof |= aborted;
57                         }
58
59                         destination->setFrame(stream_idx, next_pts);
60
61                         // Find the next frame.
62                         {
63                                 lock_guard<mutex> lock(frame_mu);
64                                 auto it = upper_bound(frames[stream_idx].begin(),
65                                         frames[stream_idx].end(),
66                                         next_pts);
67                                 if (it == frames[stream_idx].end()) {
68                                         eof = true;
69                                 } else {
70                                         next_pts = *it;
71                                         if (next_pts >= clip.pts_out) {
72                                                 eof = true;
73                                         }
74                                 }
75                         }
76                 }
77
78                 if (done_callback != nullptr && !aborted) {
79                         done_callback();
80                 }
81         }
82 }
83
84 Player::Player(JPEGFrameView *destination)
85         : destination(destination)
86 {
87         thread(&Player::thread_func, this).detach();
88 }
89
90 void Player::play_clip(const Clip &clip, unsigned stream_idx)
91 {
92         {
93                 lock_guard<mutex> lock(mu);
94                 current_clip = clip;
95                 current_stream_idx = stream_idx;
96         }
97
98         {
99                 lock_guard<mutex> lock(queue_state_mu);
100                 new_clip_ready = true;
101                 new_clip_changed.notify_all();
102         }
103 }