]> git.sesse.net Git - nageru/blob - player.cpp
Remove an unused variable.
[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                 for (int frameno = 0; ; ++frameno) {  // Ends when the clip ends.
93                         double out_pts = out_pts_origin + TIMEBASE * frameno / output_framerate;
94                         steady_clock::time_point next_frame_start =
95                                 origin + microseconds(lrint((out_pts - out_pts_origin) * 1e6 / TIMEBASE));
96                         int64_t in_pts = lrint(in_pts_origin + TIMEBASE * frameno * speed / output_framerate);
97                         pts = lrint(out_pts);
98
99                         steady_clock::duration time_behind = steady_clock::now() - next_frame_start;
100                         if (time_behind >= milliseconds(200)) {
101                                 fprintf(stderr, "WARNING: %ld ms behind, dropping a frame (no matter the type).\n",
102                                         lrint(1e3 * duration<double>(time_behind).count()));
103                                 continue;
104                         }
105
106                         int64_t in_pts_lower, in_pts_upper;
107                         bool ok = find_surrounding_frames(in_pts, stream_idx, &in_pts_lower, &in_pts_upper);
108                         if (!ok || in_pts_upper >= clip.pts_out) {
109                                 break;
110                         }
111
112                         // Sleep until the next frame start, or until there's a new clip we're supposed to play.
113                         {
114                                 unique_lock<mutex> lock(queue_state_mu);
115                                 new_clip_changed.wait_until(lock, next_frame_start, [this]{
116                                         return new_clip_ready || override_stream_idx != -1;
117                                 });
118                                 if (new_clip_ready) break;
119                                 if (override_stream_idx != -1) {
120                                         stream_idx = override_stream_idx;
121                                         override_stream_idx = -1;
122                                         continue;
123                                 }
124                         }
125
126                         if (progress_callback != nullptr) {
127                                 // NOTE: None of this will take into account any snapping done below.
128                                 double played_this_clip = double(in_pts - clip.pts_in) / TIMEBASE / speed;
129                                 double total_length = double(clip.pts_out - clip.pts_in) / TIMEBASE / speed;
130                                 progress_callback(played_this_clip, total_length);
131                         }
132
133                         if (in_pts_lower == in_pts_upper) {
134                                 destination->setFrame(stream_idx, in_pts_lower, /*interpolated=*/false);
135                                 if (video_stream != nullptr) {
136                                         video_stream->schedule_original_frame(pts, stream_idx, in_pts_lower);
137                                 }
138                                 continue;
139                         }
140
141                         // Snap to input frame: If we can do so with less than 1% jitter
142                         // (ie., move less than 1% of an _output_ frame), do so.
143                         bool snapped = false;
144                         for (int64_t snap_pts : { in_pts_lower, in_pts_upper }) {
145                                 double snap_pts_as_frameno = (snap_pts - in_pts_origin) * output_framerate / TIMEBASE / speed;
146                                 if (fabs(snap_pts_as_frameno - frameno) < 0.01) {
147                                         destination->setFrame(stream_idx, snap_pts, /*interpolated=*/false);
148                                         if (video_stream != nullptr) {
149                                                 video_stream->schedule_original_frame(pts, stream_idx, snap_pts);
150                                         }
151                                         in_pts_origin += snap_pts - in_pts;
152                                         snapped = true;
153                                         break;
154                                 }
155                         }
156                         if (snapped) {
157                                 continue;
158                         }
159
160                         if (time_behind >= milliseconds(100)) {
161                                 fprintf(stderr, "WARNING: %ld ms behind, dropping an interpolated frame.\n",
162                                         lrint(1e3 * duration<double>(time_behind).count()));
163                                 continue;
164                         }
165
166                         double alpha = double(in_pts - in_pts_lower) / (in_pts_upper - in_pts_lower);
167
168                         if (video_stream == nullptr) {
169                                 // Previews don't do any interpolation.
170                                 destination->setFrame(stream_idx, in_pts_lower, /*interpolated=*/false);
171                         } else {
172                                 // Calculate the interpolated frame. When it's done, the destination
173                                 // will be unblocked.
174                                 destination->setFrame(stream_idx, pts, /*interpolated=*/true);
175                                 video_stream->schedule_interpolated_frame(pts, stream_idx, in_pts_lower, in_pts_upper, alpha);
176                         }
177                 }
178
179                 {
180                         unique_lock<mutex> lock(queue_state_mu);
181                         playing = false;
182                 }
183                 if (done_callback != nullptr) {
184                         done_callback();
185                 }
186         }
187 }
188
189 // Find the frame immediately before and after this point.
190 bool Player::find_surrounding_frames(int64_t pts, int stream_idx, int64_t *pts_lower, int64_t *pts_upper)
191 {
192         lock_guard<mutex> lock(frame_mu);
193
194         // Find the first frame such that frame.pts >= pts.
195         auto it = lower_bound(frames[stream_idx].begin(),
196                 frames[stream_idx].end(),
197                 pts);
198         if (it == frames[stream_idx].end()) {
199                 return false;
200         }
201         *pts_upper = *it;
202
203         // Find the last frame such that in_pts <= frame.pts (if any).
204         if (it == frames[stream_idx].begin()) {
205                 *pts_lower = *it;
206         } else {
207                 *pts_lower = *(it - 1);
208         }
209         assert(pts >= *pts_lower);
210         assert(pts <= *pts_upper);
211         return true;
212 }
213
214 Player::Player(JPEGFrameView *destination, bool also_output_to_stream)
215         : destination(destination)
216 {
217         thread(&Player::thread_func, this, also_output_to_stream).detach();
218 }
219
220 void Player::play_clip(const Clip &clip, unsigned stream_idx)
221 {
222         {
223                 lock_guard<mutex> lock(mu);
224                 current_clip = clip;
225                 current_stream_idx = stream_idx;
226         }
227
228         {
229                 lock_guard<mutex> lock(queue_state_mu);
230                 new_clip_ready = true;
231                 override_stream_idx = -1;
232                 new_clip_changed.notify_all();
233         }
234 }
235
236 void Player::override_angle(unsigned stream_idx)
237 {
238         // Corner case: If a new clip is waiting to be played, change its stream and then we're done. 
239         {
240                 unique_lock<mutex> lock(queue_state_mu);
241                 if (new_clip_ready) {
242                         lock_guard<mutex> lock2(mu);
243                         current_stream_idx = stream_idx;
244                         return;
245                 }
246         }
247
248         // If we are playing a clip, set override_stream_idx, and the player thread will
249         // pick it up and change its internal index.
250         {
251                 unique_lock<mutex> lock(queue_state_mu);
252                 if (playing) {
253                         override_stream_idx = stream_idx;
254                         new_clip_changed.notify_all();
255                 }
256         }
257
258         // OK, so we're standing still, presumably at the end of a clip.
259         // Look at the current pts_out (if it exists), and show the closest
260         // thing we've got.
261         int64_t pts_out;
262         {
263                 lock_guard<mutex> lock(mu);
264                 if (current_clip.pts_out < 0) {
265                         return;
266                 }
267                 pts_out = current_clip.pts_out;
268         }
269                         
270         lock_guard<mutex> lock(frame_mu);
271         auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts_out);
272         if (it == frames[stream_idx].end()) {
273                 return;
274         }
275         destination->setFrame(stream_idx, *it, /*interpolated=*/false);
276 }