]> git.sesse.net Git - nageru/blob - player.cpp
Name some threads.
[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 <stdio.h>
9
10 #include <movit/util.h>
11
12 #include "clip_list.h"
13 #include "context.h"
14 #include "defs.h"
15 #include "ffmpeg_raii.h"
16 #include "httpd.h"
17 #include "jpeg_frame_view.h"
18 #include "mux.h"
19 #include "player.h"
20 #include "timebase.h"
21 #include "video_stream.h"
22
23 using namespace std;
24 using namespace std::chrono;
25
26 extern mutex frame_mu;
27 extern vector<int64_t> frames[MAX_STREAMS];
28 extern HTTPD *global_httpd;
29
30 void Player::thread_func(bool also_output_to_stream)
31 {
32         pthread_setname_np(pthread_self(), "Player");
33
34         QSurface *surface = create_surface();
35         QOpenGLContext *context = create_context(surface);
36         if (!make_current(context, surface)) {
37                 printf("oops\n");
38                 exit(1);
39         }
40
41         check_error();
42
43         // Create the VideoStream object, now that we have an OpenGL context.
44         if (also_output_to_stream) {
45                 video_stream.reset(new VideoStream);
46                 video_stream->start();
47         }
48         
49         check_error();
50
51         constexpr double output_framerate = 60000.0 / 1001.0;  // FIXME: make configurable
52         int64_t pts = 0;
53
54         for ( ;; ) {
55                 // Wait until we're supposed to play something.
56                 {
57                         unique_lock<mutex> lock(queue_state_mu);
58                         new_clip_changed.wait(lock, [this]{
59                                 return new_clip_ready && current_clip.pts_in != -1;
60                         });
61                         new_clip_ready = false;
62                         playing = true;
63                 }
64
65                 Clip clip;
66                 unsigned stream_idx;
67                 {
68                         lock_guard<mutex> lock(mu);
69                         clip = current_clip;
70                         stream_idx = current_stream_idx;
71                 }
72                 steady_clock::time_point origin = steady_clock::now();
73                 int64_t in_pts_origin = clip.pts_in;
74                 int64_t out_pts_origin = pts;
75
76                 // Start playing exactly at a frame.
77                 {
78                         lock_guard<mutex> lock(frame_mu);
79
80                         // Find the first frame such that frame.pts <= in_pts.
81                         auto it = lower_bound(frames[stream_idx].begin(),
82                                 frames[stream_idx].end(),
83                                 in_pts_origin);
84                         if (it != frames[stream_idx].end()) {
85                                 in_pts_origin = *it;
86                         }
87                 }
88
89                 // TODO: Lock to a rational multiple of the frame rate if possible.
90                 double speed = 0.5;
91
92                 bool aborted = false;
93                 for (int frameno = 0; ; ++frameno) {  // Ends when the clip ends.
94                         double out_pts = out_pts_origin + TIMEBASE * frameno / output_framerate;
95                         steady_clock::time_point next_frame_start =
96                                 origin + microseconds(lrint((out_pts - out_pts_origin) * 1e6 / TIMEBASE));
97                         int64_t in_pts = lrint(in_pts_origin + TIMEBASE * frameno * speed / output_framerate);
98                         pts = lrint(out_pts);
99
100                         int64_t in_pts_lower, in_pts_upper;
101
102                         // Find the frame immediately before and after this point.
103                         {
104                                 lock_guard<mutex> lock(frame_mu);
105
106                                 // Find the first frame such that in_pts >= frame.pts.
107                                 auto it = lower_bound(frames[stream_idx].begin(),
108                                         frames[stream_idx].end(),
109                                         in_pts);
110                                 if (it == frames[stream_idx].end() || *it >= clip.pts_out) {
111                                         break;
112                                 }
113                                 in_pts_upper = *it;
114
115                                 // Find the last frame such that in_pts <= frame.pts (if any).
116                                 if (it == frames[stream_idx].begin()) {
117                                         in_pts_lower = *it;
118                                 } else {
119                                         in_pts_lower = *(it - 1);
120                                 }
121                         }
122                         assert(in_pts >= in_pts_lower);
123                         assert(in_pts <= in_pts_upper);
124
125                         // Sleep until the next frame start, or until there's a new clip we're supposed to play.
126                         {
127                                 unique_lock<mutex> lock(queue_state_mu);
128                                 new_clip_changed.wait_until(lock, next_frame_start, [this]{
129                                         return new_clip_ready || override_stream_idx != -1;
130                                 });
131                                 if (new_clip_ready) break;
132                                 if (override_stream_idx != -1) {
133                                         stream_idx = override_stream_idx;
134                                         override_stream_idx = -1;
135                                         continue;
136                                 }
137                         }
138
139                         if (in_pts_lower == in_pts_upper) {
140                                 destination->setFrame(stream_idx, in_pts_lower);
141                                 if (video_stream != nullptr) {
142                                         video_stream->schedule_original_frame(lrint(out_pts), stream_idx, in_pts_lower);
143                                 }
144                                 continue;
145                         }
146
147                         // Snap to input frame: If we can do so with less than 1% jitter
148                         // (ie., move less than 1% of an _output_ frame), do so.
149                         double in_pts_lower_as_frameno = (in_pts_lower - in_pts_origin) * output_framerate / TIMEBASE / speed;
150                         double in_pts_upper_as_frameno = (in_pts_upper - in_pts_origin) * output_framerate / TIMEBASE / speed;
151                         if (fabs(in_pts_lower_as_frameno - frameno) < 0.01) {
152                                 destination->setFrame(stream_idx, in_pts_lower);
153                                 if (video_stream != nullptr) {
154                                         video_stream->schedule_original_frame(lrint(out_pts), stream_idx, in_pts_lower);
155                                 }
156                                 in_pts_origin += in_pts_lower - in_pts;
157                                 continue;
158                         } else if (fabs(in_pts_upper_as_frameno - frameno) < 0.01) {
159                                 destination->setFrame(stream_idx, in_pts_upper);
160                                 if (video_stream != nullptr) {
161                                         video_stream->schedule_original_frame(lrint(out_pts), stream_idx, in_pts_upper);
162                                 }
163                                 in_pts_origin += in_pts_upper - in_pts;
164                                 continue;
165                         }
166
167                         double alpha = double(in_pts - in_pts_lower) / (in_pts_upper - in_pts_lower);
168                         destination->setFrame(stream_idx, in_pts_lower);  // FIXME
169
170                         if (video_stream != nullptr) {
171                                 // Send the frame to the stream.
172                                 video_stream->schedule_interpolated_frame(lrint(out_pts), stream_idx, in_pts_lower, in_pts_upper, alpha);
173                         }
174                 }
175
176                 {
177                         unique_lock<mutex> lock(queue_state_mu);
178                         playing = false;
179                 }
180                 if (done_callback != nullptr && !aborted) {
181                         done_callback();
182                 }
183         }
184 }
185
186 Player::Player(JPEGFrameView *destination, bool also_output_to_stream)
187         : destination(destination)
188 {
189         thread(&Player::thread_func, this, also_output_to_stream).detach();
190 }
191
192 void Player::play_clip(const Clip &clip, unsigned stream_idx)
193 {
194         {
195                 lock_guard<mutex> lock(mu);
196                 current_clip = clip;
197                 current_stream_idx = stream_idx;
198         }
199
200         {
201                 lock_guard<mutex> lock(queue_state_mu);
202                 new_clip_ready = true;
203                 override_stream_idx = -1;
204                 new_clip_changed.notify_all();
205         }
206 }
207
208 void Player::override_angle(unsigned stream_idx)
209 {
210         // Corner case: If a new clip is waiting to be played, change its stream and then we're done. 
211         {
212                 unique_lock<mutex> lock(queue_state_mu);
213                 if (new_clip_ready) {
214                         lock_guard<mutex> lock2(mu);
215                         current_stream_idx = stream_idx;
216                         return;
217                 }
218         }
219
220         // If we are playing a clip, set override_stream_idx, and the player thread will
221         // pick it up and change its internal index.
222         {
223                 unique_lock<mutex> lock(queue_state_mu);
224                 if (playing) {
225                         override_stream_idx = stream_idx;
226                         new_clip_changed.notify_all();
227                 }
228         }
229
230         // OK, so we're standing still, presumably at the end of a clip.
231         // Look at the current pts_out (if it exists), and show the closest
232         // thing we've got.
233         int64_t pts_out;
234         {
235                 lock_guard<mutex> lock(mu);
236                 if (current_clip.pts_out < 0) {
237                         return;
238                 }
239                 pts_out = current_clip.pts_out;
240         }
241                         
242         lock_guard<mutex> lock(frame_mu);
243         auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts_out);
244         if (it == frames[stream_idx].end()) {
245                 return;
246         }
247         destination->setFrame(stream_idx, *it);
248 }