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 QSurface *surface = create_surface();
33 QOpenGLContext *context = create_context(surface);
34 if (!make_current(context, surface)) {
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();
49 constexpr double output_framerate = 60000.0 / 1001.0; // FIXME: make configurable
53 // Wait until we're supposed to play something.
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;
59 new_clip_ready = false;
66 lock_guard<mutex> lock(mu);
68 stream_idx = current_stream_idx;
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;
74 // Start playing exactly at a frame.
76 lock_guard<mutex> lock(frame_mu);
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(),
82 if (it != frames[stream_idx].end()) {
87 // TODO: Lock to a rational multiple of the frame rate if possible.
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);
98 int64_t in_pts_lower, in_pts_upper;
100 // Find the frame immediately before and after this point.
102 lock_guard<mutex> lock(frame_mu);
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(),
108 if (it == frames[stream_idx].end() || *it >= clip.pts_out) {
113 // Find the last frame such that in_pts <= frame.pts (if any).
114 if (it == frames[stream_idx].begin()) {
117 in_pts_lower = *(it - 1);
120 assert(in_pts >= in_pts_lower);
121 assert(in_pts <= in_pts_upper);
123 // Sleep until the next frame start, or until there's a new clip we're supposed to play.
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;
129 if (new_clip_ready) break;
130 if (override_stream_idx != -1) {
131 stream_idx = override_stream_idx;
132 override_stream_idx = -1;
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);
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);
154 in_pts_origin += in_pts_lower - in_pts;
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);
161 in_pts_origin += in_pts_upper - in_pts;
165 double alpha = double(in_pts - in_pts_lower) / (in_pts_upper - in_pts_lower);
166 destination->setFrame(stream_idx, in_pts_lower); // FIXME
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);
175 unique_lock<mutex> lock(queue_state_mu);
178 if (done_callback != nullptr && !aborted) {
184 Player::Player(JPEGFrameView *destination, bool also_output_to_stream)
185 : destination(destination)
187 thread(&Player::thread_func, this, also_output_to_stream).detach();
190 void Player::play_clip(const Clip &clip, unsigned stream_idx)
193 lock_guard<mutex> lock(mu);
195 current_stream_idx = stream_idx;
199 lock_guard<mutex> lock(queue_state_mu);
200 new_clip_ready = true;
201 override_stream_idx = -1;
202 new_clip_changed.notify_all();
206 void Player::override_angle(unsigned stream_idx)
208 // Corner case: If a new clip is waiting to be played, change its stream and then we're done.
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;
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.
221 unique_lock<mutex> lock(queue_state_mu);
223 override_stream_idx = stream_idx;
224 new_clip_changed.notify_all();
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
233 lock_guard<mutex> lock(mu);
234 if (current_clip.pts_out < 0) {
237 pts_out = current_clip.pts_out;
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()) {
245 destination->setFrame(stream_idx, *it);