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"
20 #include "video_stream.h"
23 using namespace std::chrono;
25 extern mutex frame_mu;
26 extern vector<int64_t> frames[MAX_STREAMS];
27 extern HTTPD *global_httpd;
29 void Player::thread_func(bool also_output_to_stream)
31 QSurface *surface = create_surface();
32 QOpenGLContext *context = create_context(surface);
33 if (!make_current(context, surface)) {
40 // Create the VideoStream object, now that we have an OpenGL context.
41 if (also_output_to_stream) {
42 video_stream.reset(new VideoStream);
43 video_stream->start();
49 // Wait until we're supposed to play something.
51 unique_lock<mutex> lock(queue_state_mu);
52 new_clip_changed.wait(lock, [this]{
53 return new_clip_ready && current_clip.pts_in != -1;
55 new_clip_ready = false;
62 lock_guard<mutex> lock(mu);
64 stream_idx = current_stream_idx;
66 steady_clock::time_point origin = steady_clock::now();
67 int64_t pts_origin = clip.pts_in;
69 int64_t next_pts = pts_origin - 1; // Make sure we play the frame at clip.pts_in if it exists.
73 // Find the next frame.
75 lock_guard<mutex> lock(frame_mu);
76 auto it = upper_bound(frames[stream_idx].begin(),
77 frames[stream_idx].end(),
79 if (it == frames[stream_idx].end() || *it >= clip.pts_out) {
85 // FIXME: assumes a given timebase.
87 steady_clock::time_point next_frame_start =
88 origin + microseconds((next_pts - pts_origin) * int(1000000 / speed) / 12800);
90 // Sleep until the next frame start, or until there's a new clip we're supposed to play.
92 unique_lock<mutex> lock(queue_state_mu);
93 new_clip_changed.wait_until(lock, next_frame_start, [this]{
94 return new_clip_ready || override_stream_idx != -1;
96 if (new_clip_ready) break;
97 if (override_stream_idx != -1) {
98 stream_idx = override_stream_idx;
99 override_stream_idx = -1;
104 destination->setFrame(stream_idx, next_pts);
106 if (video_stream != nullptr) {
107 // Send the frame to the stream.
108 // FIXME: Vaguely less crazy pts, perhaps.
109 double pts_float = fmod(duration<double>(next_frame_start.time_since_epoch()).count(), 86400.0f);
110 int64_t pts = lrint(pts_float * TIMEBASE);
111 video_stream->schedule_original_frame(pts, stream_idx, next_pts);
113 // HACK: test interpolation by frame-doubling.
114 int64_t next_next_pts = -1;
116 lock_guard<mutex> lock(frame_mu);
117 auto it = upper_bound(frames[stream_idx].begin(),
118 frames[stream_idx].end(),
120 if (it == frames[stream_idx].end() || *it >= clip.pts_out) {
125 if (next_next_pts != -1) {
126 auto frame_len = microseconds((next_next_pts - next_pts) * int(1000000 / speed) / 12800) / 2;
127 int64_t interpolated_pts = pts + lrint(duration<double>(frame_len).count() * TIMEBASE);
128 video_stream->schedule_interpolated_frame(interpolated_pts, stream_idx, next_pts, next_next_pts, 0.5f);
134 unique_lock<mutex> lock(queue_state_mu);
137 if (done_callback != nullptr && !aborted) {
143 Player::Player(JPEGFrameView *destination, bool also_output_to_stream)
144 : destination(destination)
146 thread(&Player::thread_func, this, also_output_to_stream).detach();
149 void Player::play_clip(const Clip &clip, unsigned stream_idx)
152 lock_guard<mutex> lock(mu);
154 current_stream_idx = stream_idx;
158 lock_guard<mutex> lock(queue_state_mu);
159 new_clip_ready = true;
160 override_stream_idx = -1;
161 new_clip_changed.notify_all();
165 void Player::override_angle(unsigned stream_idx)
167 // Corner case: If a new clip is waiting to be played, change its stream and then we're done.
169 unique_lock<mutex> lock(queue_state_mu);
170 if (new_clip_ready) {
171 lock_guard<mutex> lock2(mu);
172 current_stream_idx = stream_idx;
177 // If we are playing a clip, set override_stream_idx, and the player thread will
178 // pick it up and change its internal index.
180 unique_lock<mutex> lock(queue_state_mu);
182 override_stream_idx = stream_idx;
183 new_clip_changed.notify_all();
187 // OK, so we're standing still, presumably at the end of a clip.
188 // Look at the current pts_out (if it exists), and show the closest
192 lock_guard<mutex> lock(mu);
193 if (current_clip.pts_out < 0) {
196 pts_out = current_clip.pts_out;
199 lock_guard<mutex> lock(frame_mu);
200 auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts_out);
201 if (it == frames[stream_idx].end()) {
204 destination->setFrame(stream_idx, *it);