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