]> git.sesse.net Git - nageru/blob - player.cpp
Encode JPEGs from the interpolated frames.
[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                                         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);
129                                 }
130                         }
131                 }
132
133                 {
134                         unique_lock<mutex> lock(queue_state_mu);
135                         playing = false;
136                 }
137                 if (done_callback != nullptr && !aborted) {
138                         done_callback();
139                 }
140         }
141 }
142
143 Player::Player(JPEGFrameView *destination, bool also_output_to_stream)
144         : destination(destination)
145 {
146         thread(&Player::thread_func, this, also_output_to_stream).detach();
147 }
148
149 void Player::play_clip(const Clip &clip, unsigned stream_idx)
150 {
151         {
152                 lock_guard<mutex> lock(mu);
153                 current_clip = clip;
154                 current_stream_idx = stream_idx;
155         }
156
157         {
158                 lock_guard<mutex> lock(queue_state_mu);
159                 new_clip_ready = true;
160                 override_stream_idx = -1;
161                 new_clip_changed.notify_all();
162         }
163 }
164
165 void Player::override_angle(unsigned stream_idx)
166 {
167         // Corner case: If a new clip is waiting to be played, change its stream and then we're done. 
168         {
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;
173                         return;
174                 }
175         }
176
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.
179         {
180                 unique_lock<mutex> lock(queue_state_mu);
181                 if (playing) {
182                         override_stream_idx = stream_idx;
183                         new_clip_changed.notify_all();
184                 }
185         }
186
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
189         // thing we've got.
190         int64_t pts_out;
191         {
192                 lock_guard<mutex> lock(mu);
193                 if (current_clip.pts_out < 0) {
194                         return;
195                 }
196                 pts_out = current_clip.pts_out;
197         }
198                         
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()) {
202                 return;
203         }
204         destination->setFrame(stream_idx, *it);
205 }