]> git.sesse.net Git - nageru/blob - player.cpp
Start hacking in support for interpolated frames in the main application.
[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 "video_stream.h"
21
22 using namespace std;
23 using namespace std::chrono;
24
25 extern mutex frame_mu;
26 extern vector<int64_t> frames[MAX_STREAMS];
27 extern HTTPD *global_httpd;
28
29 void Player::thread_func(bool also_output_to_stream)
30 {
31         QSurface *surface = create_surface();
32         QOpenGLContext *context = create_context(surface);
33         if (!make_current(context, surface)) {
34                 printf("oops\n");
35                 exit(1);
36         }
37
38         check_error();
39
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();
44         }
45         
46         check_error();
47
48         for ( ;; ) {
49                 // Wait until we're supposed to play something.
50                 {
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;
54                         });
55                         new_clip_ready = false;
56                         playing = true;
57                 }
58
59                 Clip clip;
60                 unsigned stream_idx;
61                 {
62                         lock_guard<mutex> lock(mu);
63                         clip = current_clip;
64                         stream_idx = current_stream_idx;
65                 }
66                 steady_clock::time_point origin = steady_clock::now();
67                 int64_t pts_origin = clip.pts_in;
68
69                 int64_t next_pts = pts_origin - 1;  // Make sure we play the frame at clip.pts_in if it exists.
70
71                 bool aborted = false;
72                 for ( ;; ) {
73                         // Find the next frame.
74                         {
75                                 lock_guard<mutex> lock(frame_mu);
76                                 auto it = upper_bound(frames[stream_idx].begin(),
77                                         frames[stream_idx].end(),
78                                         next_pts);
79                                 if (it == frames[stream_idx].end() || *it >= clip.pts_out) {
80                                         break;
81                                 }
82                                 next_pts = *it;
83                         }
84
85                         // FIXME: assumes a given timebase.
86                         double speed = 0.5;
87                         steady_clock::time_point next_frame_start =
88                                 origin + microseconds((next_pts - pts_origin) * int(1000000 / speed) / 12800);
89
90                         // Sleep until the next frame start, or until there's a new clip we're supposed to play.
91                         {
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;
95                                 });
96                                 if (new_clip_ready) break;
97                                 if (override_stream_idx != -1) {
98                                         stream_idx = override_stream_idx;
99                                         override_stream_idx = -1;
100                                         continue;
101                                 }
102                         }
103
104                         destination->setFrame(stream_idx, next_pts);
105
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);
112
113                                 // HACK: test interpolation by frame-doubling.
114                                 int64_t next_next_pts = -1;
115                                 {
116                                         lock_guard<mutex> lock(frame_mu);
117                                         auto it = upper_bound(frames[stream_idx].begin(),
118                                                 frames[stream_idx].end(),
119                                                 next_pts);
120                                         if (it == frames[stream_idx].end() || *it >= clip.pts_out) {
121                                                 break;
122                                         }
123                                         next_next_pts = *it;
124                                 }
125                                 if (next_next_pts != -1) {
126                                         int64_t interpolated_pts = (next_pts + next_next_pts) / 2;
127                                         video_stream->schedule_interpolated_frame(interpolated_pts, stream_idx, next_pts, next_next_pts, 0.5f);
128                                 }
129                         }
130                 }
131
132                 {
133                         unique_lock<mutex> lock(queue_state_mu);
134                         playing = false;
135                 }
136                 if (done_callback != nullptr && !aborted) {
137                         done_callback();
138                 }
139         }
140 }
141
142 Player::Player(JPEGFrameView *destination, bool also_output_to_stream)
143         : destination(destination)
144 {
145         thread(&Player::thread_func, this, also_output_to_stream).detach();
146 }
147
148 void Player::play_clip(const Clip &clip, unsigned stream_idx)
149 {
150         {
151                 lock_guard<mutex> lock(mu);
152                 current_clip = clip;
153                 current_stream_idx = stream_idx;
154         }
155
156         {
157                 lock_guard<mutex> lock(queue_state_mu);
158                 new_clip_ready = true;
159                 override_stream_idx = -1;
160                 new_clip_changed.notify_all();
161         }
162 }
163
164 void Player::override_angle(unsigned stream_idx)
165 {
166         // Corner case: If a new clip is waiting to be played, change its stream and then we're done. 
167         {
168                 unique_lock<mutex> lock(queue_state_mu);
169                 if (new_clip_ready) {
170                         lock_guard<mutex> lock2(mu);
171                         current_stream_idx = stream_idx;
172                         return;
173                 }
174         }
175
176         // If we are playing a clip, set override_stream_idx, and the player thread will
177         // pick it up and change its internal index.
178         {
179                 unique_lock<mutex> lock(queue_state_mu);
180                 if (playing) {
181                         override_stream_idx = stream_idx;
182                         new_clip_changed.notify_all();
183                 }
184         }
185
186         // OK, so we're standing still, presumably at the end of a clip.
187         // Look at the current pts_out (if it exists), and show the closest
188         // thing we've got.
189         int64_t pts_out;
190         {
191                 lock_guard<mutex> lock(mu);
192                 if (current_clip.pts_out < 0) {
193                         return;
194                 }
195                 pts_out = current_clip.pts_out;
196         }
197                         
198         lock_guard<mutex> lock(frame_mu);
199         auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts_out);
200         if (it == frames[stream_idx].end()) {
201                 return;
202         }
203         destination->setFrame(stream_idx, *it);
204 }