3 #include <condition_variable>
10 #include <movit/util.h>
12 #include "clip_list.h"
15 #include "ffmpeg_raii.h"
17 #include "jpeg_frame_view.h"
21 #include "video_stream.h"
24 using namespace std::chrono;
26 extern mutex frame_mu;
27 extern vector<int64_t> frames[MAX_STREAMS];
28 extern HTTPD *global_httpd;
30 void Player::thread_func(bool also_output_to_stream)
32 pthread_setname_np(pthread_self(), "Player");
34 QSurface *surface = create_surface();
35 QOpenGLContext *context = create_context(surface);
36 if (!make_current(context, surface)) {
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();
51 constexpr double output_framerate = 60000.0 / 1001.0; // FIXME: make configurable
55 // Wait until we're supposed to play something.
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;
61 new_clip_ready = false;
68 lock_guard<mutex> lock(mu);
70 stream_idx = current_stream_idx;
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;
76 // Start playing exactly at a frame.
78 lock_guard<mutex> lock(frame_mu);
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(),
84 if (it != frames[stream_idx].end()) {
89 // TODO: Lock to a rational multiple of the frame rate if possible.
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);
100 int64_t in_pts_lower, in_pts_upper;
102 // Find the frame immediately before and after this point.
104 lock_guard<mutex> lock(frame_mu);
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(),
110 if (it == frames[stream_idx].end() || *it >= clip.pts_out) {
115 // Find the last frame such that in_pts <= frame.pts (if any).
116 if (it == frames[stream_idx].begin()) {
119 in_pts_lower = *(it - 1);
122 assert(in_pts >= in_pts_lower);
123 assert(in_pts <= in_pts_upper);
125 // Sleep until the next frame start, or until there's a new clip we're supposed to play.
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;
131 if (new_clip_ready) break;
132 if (override_stream_idx != -1) {
133 stream_idx = override_stream_idx;
134 override_stream_idx = -1;
139 if (progress_callback != nullptr) {
140 // NOTE: None of this will take into account any snapping done below.
141 double played_this_clip = double(in_pts - clip.pts_in) / TIMEBASE / speed;
142 double total_length = double(clip.pts_out - clip.pts_in) / TIMEBASE / speed;
143 progress_callback(played_this_clip, total_length);
146 if (in_pts_lower == in_pts_upper) {
147 destination->setFrame(stream_idx, in_pts_lower, /*interpolated=*/false);
148 if (video_stream != nullptr) {
149 video_stream->schedule_original_frame(lrint(out_pts), stream_idx, in_pts_lower);
154 // Snap to input frame: If we can do so with less than 1% jitter
155 // (ie., move less than 1% of an _output_ frame), do so.
156 double in_pts_lower_as_frameno = (in_pts_lower - in_pts_origin) * output_framerate / TIMEBASE / speed;
157 double in_pts_upper_as_frameno = (in_pts_upper - in_pts_origin) * output_framerate / TIMEBASE / speed;
158 if (fabs(in_pts_lower_as_frameno - frameno) < 0.01) {
159 destination->setFrame(stream_idx, in_pts_lower, /*interpolated=*/false);
160 if (video_stream != nullptr) {
161 video_stream->schedule_original_frame(lrint(out_pts), stream_idx, in_pts_lower);
163 in_pts_origin += in_pts_lower - in_pts;
165 } else if (fabs(in_pts_upper_as_frameno - frameno) < 0.01) {
166 destination->setFrame(stream_idx, in_pts_upper, /*interpolated=*/false);
167 if (video_stream != nullptr) {
168 video_stream->schedule_original_frame(lrint(out_pts), stream_idx, in_pts_upper);
170 in_pts_origin += in_pts_upper - in_pts;
174 double alpha = double(in_pts - in_pts_lower) / (in_pts_upper - in_pts_lower);
176 if (video_stream == nullptr) {
177 // Previews don't do any interpolation.
178 destination->setFrame(stream_idx, in_pts_lower, /*interpolated=*/false);
180 // Calculate the interpolated frame. When it's done, the destination
181 // will be unblocked.
182 destination->setFrame(stream_idx, lrint(out_pts), /*interpolated=*/true);
183 video_stream->schedule_interpolated_frame(lrint(out_pts), stream_idx, in_pts_lower, in_pts_upper, alpha);
188 unique_lock<mutex> lock(queue_state_mu);
191 if (done_callback != nullptr && !aborted) {
197 Player::Player(JPEGFrameView *destination, bool also_output_to_stream)
198 : destination(destination)
200 thread(&Player::thread_func, this, also_output_to_stream).detach();
203 void Player::play_clip(const Clip &clip, unsigned stream_idx)
206 lock_guard<mutex> lock(mu);
208 current_stream_idx = stream_idx;
212 lock_guard<mutex> lock(queue_state_mu);
213 new_clip_ready = true;
214 override_stream_idx = -1;
215 new_clip_changed.notify_all();
219 void Player::override_angle(unsigned stream_idx)
221 // Corner case: If a new clip is waiting to be played, change its stream and then we're done.
223 unique_lock<mutex> lock(queue_state_mu);
224 if (new_clip_ready) {
225 lock_guard<mutex> lock2(mu);
226 current_stream_idx = stream_idx;
231 // If we are playing a clip, set override_stream_idx, and the player thread will
232 // pick it up and change its internal index.
234 unique_lock<mutex> lock(queue_state_mu);
236 override_stream_idx = stream_idx;
237 new_clip_changed.notify_all();
241 // OK, so we're standing still, presumably at the end of a clip.
242 // Look at the current pts_out (if it exists), and show the closest
246 lock_guard<mutex> lock(mu);
247 if (current_clip.pts_out < 0) {
250 pts_out = current_clip.pts_out;
253 lock_guard<mutex> lock(frame_mu);
254 auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts_out);
255 if (it == frames[stream_idx].end()) {
258 destination->setFrame(stream_idx, *it, /*interpolated=*/false);