]> git.sesse.net Git - nageru/blob - mainwindow.cpp
Move transitioning to the next clip away from the done callback.
[nageru] / mainwindow.cpp
1 #include "mainwindow.h"
2
3 #include "clip_list.h"
4 #include "disk_space_estimator.h"
5 #include "player.h"
6 #include "post_to_main_thread.h"
7 #include "timebase.h"
8 #include "ui_mainwindow.h"
9
10 #include <string>
11 #include <vector>
12
13 #include <QMouseEvent>
14 #include <QWheelEvent>
15 #include <QShortcut>
16 #include <QTimer>
17
18 #include <sqlite3.h>
19
20 using namespace std;
21 using namespace std::placeholders;
22
23 MainWindow *global_mainwindow = nullptr;
24 ClipList *cliplist_clips;
25 PlayList *playlist_clips;
26
27 extern int64_t current_pts;
28 extern mutex frame_mu;
29 extern vector<int64_t> frames[MAX_STREAMS];
30
31 MainWindow::MainWindow()
32         : ui(new Ui::MainWindow),
33           db("futatabi.db")
34 {
35         global_mainwindow = this;
36         ui->setupUi(this);
37
38         // The menus.
39         connect(ui->exit_action, &QAction::triggered, this, &MainWindow::exit_triggered);
40
41         global_disk_space_estimator = new DiskSpaceEstimator(bind(&MainWindow::report_disk_space, this, _1, _2));
42         disk_free_label = new QLabel(this);
43         disk_free_label->setStyleSheet("QLabel {padding-right: 5px;}");
44         ui->menuBar->setCornerWidget(disk_free_label);
45
46         StateProto state = db.get_state();
47
48         cliplist_clips = new ClipList(state.clip_list());
49         ui->clip_list->setModel(cliplist_clips);
50         connect(cliplist_clips, &ClipList::any_content_changed, this, &MainWindow::content_changed);
51
52         playlist_clips = new PlayList(state.play_list());
53         ui->playlist->setModel(playlist_clips);
54         connect(playlist_clips, &PlayList::any_content_changed, this, &MainWindow::content_changed);
55
56         // For scrubbing in the pts columns.
57         ui->clip_list->viewport()->installEventFilter(this);
58         ui->playlist->viewport()->installEventFilter(this);
59
60         QShortcut *cue_in = new QShortcut(QKeySequence(Qt::Key_A), this);
61         connect(cue_in, &QShortcut::activated, ui->cue_in_btn, &QPushButton::click);
62         connect(ui->cue_in_btn, &QPushButton::clicked, this, &MainWindow::cue_in_clicked);
63
64         QShortcut *cue_out = new QShortcut(QKeySequence(Qt::Key_S), this);
65         connect(cue_out, &QShortcut::activated, ui->cue_out_btn, &QPushButton::click);
66         connect(ui->cue_out_btn, &QPushButton::clicked, this, &MainWindow::cue_out_clicked);
67
68         QShortcut *queue = new QShortcut(QKeySequence(Qt::Key_Q), this);
69         connect(queue, &QShortcut::activated, ui->queue_btn, &QPushButton::click);
70         connect(ui->queue_btn, &QPushButton::clicked, this, &MainWindow::queue_clicked);
71
72         QShortcut *preview = new QShortcut(QKeySequence(Qt::Key_W), this);
73         connect(preview, &QShortcut::activated, ui->preview_btn, &QPushButton::click);
74         connect(ui->preview_btn, &QPushButton::clicked, this, &MainWindow::preview_clicked);
75
76         QShortcut *play = new QShortcut(QKeySequence(Qt::Key_Space), this);
77         connect(play, &QShortcut::activated, ui->play_btn, &QPushButton::click);
78         connect(ui->play_btn, &QPushButton::clicked, this, &MainWindow::play_clicked);
79
80         QShortcut *preview_1 = new QShortcut(QKeySequence(Qt::Key_1), this);
81         connect(preview_1, &QShortcut::activated, ui->preview_1_btn, &QPushButton::click);
82         connect(ui->input1_display, &JPEGFrameView::clicked, ui->preview_1_btn, &QPushButton::click);
83         connect(ui->preview_1_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(0); });
84         ui->input1_display->set_overlay("1");
85
86         QShortcut *preview_2 = new QShortcut(QKeySequence(Qt::Key_2), this);
87         connect(preview_2, &QShortcut::activated, ui->preview_2_btn, &QPushButton::click);
88         connect(ui->input2_display, &JPEGFrameView::clicked, ui->preview_2_btn, &QPushButton::click);
89         connect(ui->preview_2_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(1); });
90         ui->input2_display->set_overlay("2");
91
92         QShortcut *preview_3 = new QShortcut(QKeySequence(Qt::Key_3), this);
93         connect(preview_3, &QShortcut::activated, ui->preview_3_btn, &QPushButton::click);
94         connect(ui->input3_display, &JPEGFrameView::clicked, ui->preview_3_btn, &QPushButton::click);
95         connect(ui->preview_3_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(2); });
96         ui->input3_display->set_overlay("3");
97
98         QShortcut *preview_4 = new QShortcut(QKeySequence(Qt::Key_4), this);
99         connect(preview_4, &QShortcut::activated, ui->preview_4_btn, &QPushButton::click);
100         connect(ui->input4_display, &JPEGFrameView::clicked, ui->preview_4_btn, &QPushButton::click);
101         connect(ui->preview_4_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(3); });
102         ui->input4_display->set_overlay("4");
103
104         connect(ui->playlist_duplicate_btn, &QPushButton::clicked, this, &MainWindow::playlist_duplicate);
105
106         connect(ui->playlist_remove_btn, &QPushButton::clicked, this, &MainWindow::playlist_remove);
107         QShortcut *delete_key = new QShortcut(QKeySequence(Qt::Key_Delete), ui->playlist);
108         connect(delete_key, &QShortcut::activated, [this] {
109                 if (ui->playlist->hasFocus()) {
110                         playlist_remove();
111                 }
112         });
113
114         // TODO: support drag-and-drop.
115         connect(ui->playlist_move_up_btn, &QPushButton::clicked, [this]{ playlist_move(-1); });
116         connect(ui->playlist_move_down_btn, &QPushButton::clicked, [this]{ playlist_move(1); });
117
118         connect(ui->playlist->selectionModel(), &QItemSelectionModel::selectionChanged,
119                 this, &MainWindow::playlist_selection_changed);
120         playlist_selection_changed();  // First time set-up.
121
122         preview_player = new Player(ui->preview_display, /*also_output_to_stream=*/false);
123         live_player = new Player(ui->live_display, /*also_output_to_stream=*/true);
124         live_player->set_done_callback([this]{
125                 post_to_main_thread([this]{
126                         live_player_clip_done();
127                 });
128         });
129         live_player->set_next_clip_callback(bind(&MainWindow::live_player_get_next_clip, this));
130         live_player->set_progress_callback([this](double played_this_clip, double total_length) {
131                 post_to_main_thread([this, played_this_clip, total_length] {
132                         live_player_clip_progress(played_this_clip, total_length);
133                 });
134         });
135
136         defer_timeout = new QTimer(this);
137         defer_timeout->setSingleShot(true);
138         connect(defer_timeout, &QTimer::timeout, this, &MainWindow::defer_timer_expired);
139 }
140
141 void MainWindow::cue_in_clicked()
142 {
143         if (!cliplist_clips->empty() && cliplist_clips->back()->pts_out < 0) {
144                 cliplist_clips->mutable_back()->pts_in = current_pts;
145                 return;
146         }
147         Clip clip;
148         clip.pts_in = current_pts;
149         cliplist_clips->add_clip(clip);
150         playlist_selection_changed();
151 }
152
153 void MainWindow::cue_out_clicked()
154 {
155         if (!cliplist_clips->empty()) {
156                 cliplist_clips->mutable_back()->pts_out = current_pts;
157                 // TODO: select the row in the clip list?
158         }
159 }
160
161 void MainWindow::queue_clicked()
162 {
163         if (cliplist_clips->empty()) {
164                 return;
165         }
166
167         QItemSelectionModel *selected = ui->clip_list->selectionModel();
168         if (!selected->hasSelection()) {
169                 Clip clip = *cliplist_clips->back();
170                 clip.stream_idx = 0;
171                 if (clip.pts_out != -1) {
172                         playlist_clips->add_clip(clip);
173                         playlist_selection_changed();
174                 }
175                 return;
176         }
177
178         QModelIndex index = selected->currentIndex();
179         Clip clip = *cliplist_clips->clip(index.row());
180         if (index.column() >= int(ClipList::Column::CAMERA_1) &&
181             index.column() <= int(ClipList::Column::CAMERA_4)) {
182                 clip.stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
183         } else {
184                 clip.stream_idx = ui->preview_display->get_stream_idx();
185         }
186
187         if (clip.pts_out != -1) {
188                 playlist_clips->add_clip(clip);
189                 playlist_selection_changed();
190         }
191 }
192
193 void MainWindow::preview_clicked()
194 {
195         if (cliplist_clips->empty()) return;
196
197         QItemSelectionModel *selected = ui->clip_list->selectionModel();
198         if (!selected->hasSelection()) {
199                 preview_player->play_clip(*cliplist_clips->back(), 0);
200                 return;
201         }
202
203         QModelIndex index = selected->currentIndex();
204         unsigned stream_idx;
205         if (index.column() >= int(ClipList::Column::CAMERA_1) &&
206             index.column() <= int(ClipList::Column::CAMERA_4)) {
207                 stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
208         } else {
209                 stream_idx = ui->preview_display->get_stream_idx();
210         }
211         preview_player->play_clip(*cliplist_clips->clip(index.row()), stream_idx);
212 }
213
214 void MainWindow::preview_angle_clicked(unsigned stream_idx)
215 {
216         preview_player->override_angle(stream_idx);
217
218         // Change the selection if we were previewing a clip from the clip list.
219         // (The only other thing we could be showing is a pts scrub, and if so,
220         // that would be selected.)
221         QItemSelectionModel *selected = ui->clip_list->selectionModel();
222         if (selected->hasSelection()) {
223                 QModelIndex cell = selected->selectedIndexes()[0];
224                 int column = int(ClipList::Column::CAMERA_1) + stream_idx;
225                 selected->setCurrentIndex(cell.sibling(cell.row(), column), QItemSelectionModel::ClearAndSelect);
226         }
227 }
228
229 void MainWindow::playlist_duplicate()
230 {
231         QItemSelectionModel *selected = ui->playlist->selectionModel();
232         if (!selected->hasSelection()) {
233                 // Should have been grayed out, but OK.
234                 return;
235         }
236         QModelIndexList rows = selected->selectedRows();
237         int first = rows.front().row(), last = rows.back().row();
238         playlist_clips->duplicate_clips(first, last);
239         playlist_selection_changed();
240 }
241
242 void MainWindow::playlist_remove()
243 {
244         QItemSelectionModel *selected = ui->playlist->selectionModel();
245         if (!selected->hasSelection()) {
246                 // Should have been grayed out, but OK.
247                 return;
248         }
249         QModelIndexList rows = selected->selectedRows();
250         int first = rows.front().row(), last = rows.back().row();
251         playlist_clips->erase_clips(first, last);
252
253         // TODO: select the next one in the list?
254
255         playlist_selection_changed();
256 }
257
258 void MainWindow::playlist_move(int delta)
259 {
260         QItemSelectionModel *selected = ui->playlist->selectionModel();
261         if (!selected->hasSelection()) {
262                 // Should have been grayed out, but OK.
263                 return;
264         }
265
266         QModelIndexList rows = selected->selectedRows();
267         int first = rows.front().row(), last = rows.back().row();
268         if ((delta == -1 && first == 0) ||
269             (delta == 1 && size_t(last) == playlist_clips->size() - 1)) {
270                 // Should have been grayed out, but OK.
271                 return;
272         }
273
274         playlist_clips->move_clips(first, last, delta);
275         playlist_selection_changed();
276 }
277
278 void MainWindow::defer_timer_expired()
279 {
280         state_changed(deferred_state);
281 }
282
283 void MainWindow::content_changed()
284 {
285         if (defer_timeout->isActive() &&
286             (!currently_deferring_model_changes || deferred_change_id != current_change_id)) {
287                 // There's some deferred event waiting, but this event is unrelated.
288                 // So it's time to short-circuit that timer and do the work it wanted to do.
289                 defer_timeout->stop();
290                 state_changed(deferred_state);
291         }
292         StateProto state;
293         *state.mutable_clip_list() = cliplist_clips->serialize();
294         *state.mutable_play_list() = playlist_clips->serialize();
295         if (currently_deferring_model_changes) {
296                 deferred_change_id = current_change_id;
297                 deferred_state = std::move(state);
298                 defer_timeout->start(200);
299                 return;
300         }
301         state_changed(state);
302 }
303
304 void MainWindow::state_changed(const StateProto &state)
305 {
306         db.store_state(state);
307 }
308
309 void MainWindow::play_clicked()
310 {
311         if (playlist_clips->empty()) return;
312
313         QItemSelectionModel *selected = ui->playlist->selectionModel();
314         int row;
315         if (!selected->hasSelection()) {
316                 row = 0;
317         } else {
318                 row = selected->selectedRows(0)[0].row();
319         }
320
321         const Clip &clip = *playlist_clips->clip(row);
322         live_player->play_clip(clip, clip.stream_idx);
323         playlist_clips->set_currently_playing(row, 0.0f);
324         playlist_selection_changed();
325 }
326
327 void MainWindow::live_player_clip_done()
328 {
329         int row = playlist_clips->get_currently_playing();
330         if (row == -1 || row == int(playlist_clips->size()) - 1) {
331                 ui->live_label->setText("Current output (paused)");
332                 playlist_clips->set_currently_playing(-1, 0.0f);
333         } else {
334                 playlist_clips->set_currently_playing(row + 1, 0.0f);
335         }
336 }
337
338 Clip MainWindow::live_player_get_next_clip()
339 {
340         // FIXME: threading
341         int row = playlist_clips->get_currently_playing();
342         if (row != -1 && row < int(playlist_clips->size()) - 1) {
343                 return *playlist_clips->clip(row + 1);
344         } else {
345                 return Clip();
346         }
347 }
348
349 void MainWindow::live_player_clip_progress(double played_this_clip, double total_length)
350 {
351         playlist_clips->set_currently_playing(playlist_clips->get_currently_playing(), played_this_clip / total_length);
352
353         double remaining = total_length - played_this_clip;
354         for (int row = playlist_clips->get_currently_playing() + 1; row < int(playlist_clips->size()); ++row) {
355                 const Clip clip = *playlist_clips->clip(row);
356                 remaining += double(clip.pts_out - clip.pts_in) / TIMEBASE / 0.5;   // FIXME: stop hardcoding speed.
357         }
358         int remaining_ms = lrint(remaining * 1e3);
359
360         int ms = remaining_ms % 1000;
361         remaining_ms /= 1000;
362         int s = remaining_ms % 60;
363         remaining_ms /= 60;
364         int m = remaining_ms;
365
366         char buf[256];
367         snprintf(buf, sizeof(buf), "Current output (%d:%02d.%03d left)", m, s, ms);
368         ui->live_label->setText(buf);
369 }
370
371 void MainWindow::resizeEvent(QResizeEvent *event)
372 {
373         QMainWindow::resizeEvent(event);
374
375         // Ask for a relayout, but only after the event loop is done doing relayout
376         // on everything else.
377         QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
378 }
379
380 void MainWindow::relayout()
381 {
382         ui->live_display->setMinimumWidth(ui->live_display->height() * 16 / 9);
383         ui->preview_display->setMinimumWidth(ui->preview_display->height() * 16 / 9);
384 }
385
386 void set_pts_in(int64_t pts, int64_t current_pts, ClipProxy &clip)
387 {
388         pts = std::max<int64_t>(pts, 0);
389         if (clip->pts_out == -1) {
390                 pts = std::min(pts, current_pts);
391         } else {
392                 pts = std::min(pts, clip->pts_out);
393         }
394         clip->pts_in = pts;
395 }
396
397 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
398 {
399         constexpr int dead_zone_pixels = 3;  // To avoid that simple clicks get misinterpreted.
400         constexpr int scrub_sensitivity = 100;  // pts units per pixel.
401         constexpr int wheel_sensitivity = 100;  // pts units per degree.
402         constexpr int camera_degrees_per_pixel = 15;  // One click of most mice.
403
404         unsigned stream_idx = ui->preview_display->get_stream_idx();
405
406         if (event->type() != QEvent::Wheel) {
407                 last_mousewheel_camera_row = -1;
408         }
409
410         if (event->type() == QEvent::MouseButtonPress) {
411                 QMouseEvent *mouse = (QMouseEvent *)event;
412
413                 QTableView *destination;
414                 ScrubType type;
415
416                 if (watched == ui->clip_list->viewport()) {
417                         destination = ui->clip_list;
418                         type = SCRUBBING_CLIP_LIST;
419                 } else if (watched == ui->playlist->viewport()) {
420                         destination = ui->playlist;
421                         type = SCRUBBING_PLAYLIST;
422                 } else {
423                         return false;
424                 }
425                 int column = destination->columnAt(mouse->x());
426                 int row = destination->rowAt(mouse->y());
427                 if (column == -1 || row == -1) return false;
428
429                 if (type == SCRUBBING_CLIP_LIST) {
430                         if (ClipList::Column(column) == ClipList::Column::IN) {
431                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_in;
432                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
433                         } else if (ClipList::Column(column) == ClipList::Column::OUT) {
434                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_out;
435                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
436                         } else {
437                                 return false;
438                         }
439                 } else {
440                         if (PlayList::Column(column) == PlayList::Column::IN) {
441                                 scrub_pts_origin = playlist_clips->clip(row)->pts_in;
442                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
443                         } else if (PlayList::Column(column) == PlayList::Column::OUT) {
444                                 scrub_pts_origin = playlist_clips->clip(row)->pts_out;
445                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
446                         } else {
447                                 return false;
448                         }
449                 }
450
451                 scrubbing = true;
452                 scrub_row = row;
453                 scrub_column = column;
454                 scrub_x_origin = mouse->x();
455                 scrub_type = type;
456         } else if (event->type() == QEvent::MouseMove) {
457                 if (scrubbing) {
458                         QMouseEvent *mouse = (QMouseEvent *)event;
459                         int offset = mouse->x() - scrub_x_origin;
460                         int adjusted_offset;
461                         if (offset >= dead_zone_pixels) {
462                                 adjusted_offset = offset - dead_zone_pixels;
463                         } else if (offset < -dead_zone_pixels) {
464                                 adjusted_offset = offset + dead_zone_pixels;
465                         } else {
466                                 adjusted_offset = 0;
467                         }
468
469                         int64_t pts = scrub_pts_origin + adjusted_offset * scrub_sensitivity;
470                         currently_deferring_model_changes = true;
471                         if (scrub_type == SCRUBBING_CLIP_LIST) {
472                                 ClipProxy clip = cliplist_clips->mutable_clip(scrub_row);
473                                 if (scrub_column == int(ClipList::Column::IN)) {
474                                         current_change_id = "cliplist:in:" + to_string(scrub_row);
475                                         set_pts_in(pts, current_pts, clip);
476                                         preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
477                                 } else {
478                                         current_change_id = "cliplist:out" + to_string(scrub_row);
479                                         pts = std::max(pts, clip->pts_in);
480                                         pts = std::min(pts, current_pts);
481                                         clip->pts_out = pts;
482                                         preview_single_frame(pts, stream_idx, LAST_BEFORE);
483                                 }
484                         } else {
485                                 ClipProxy clip = playlist_clips->mutable_clip(scrub_row);
486                                 if (scrub_column == int(PlayList::Column::IN)) {
487                                         current_change_id = "playlist:in:" + to_string(scrub_row);
488                                         set_pts_in(pts, current_pts, clip);
489                                         preview_single_frame(pts, clip->stream_idx, FIRST_AT_OR_AFTER);
490                                 } else {
491                                         current_change_id = "playlist:out:" + to_string(scrub_row);
492                                         pts = std::max(pts, clip->pts_in);
493                                         pts = std::min(pts, current_pts);
494                                         clip->pts_out = pts;
495                                         preview_single_frame(pts, clip->stream_idx, LAST_BEFORE);
496                                 }
497                         }
498                         currently_deferring_model_changes = false;
499
500                         return true;  // Don't use this mouse movement for selecting things.
501                 }
502         } else if (event->type() == QEvent::Wheel) {
503                 QWheelEvent *wheel = (QWheelEvent *)event;
504
505                 QTableView *destination;
506                 int in_column, out_column, camera_column;
507                 if (watched == ui->clip_list->viewport()) {
508                         destination = ui->clip_list;
509                         in_column = int(ClipList::Column::IN);
510                         out_column = int(ClipList::Column::OUT);
511                         camera_column = -1;
512                         last_mousewheel_camera_row = -1;
513                 } else if (watched == ui->playlist->viewport()) {
514                         destination = ui->playlist;
515                         in_column = int(PlayList::Column::IN);
516                         out_column = int(PlayList::Column::OUT);
517                         camera_column = int(PlayList::Column::CAMERA);
518                 } else {
519                         last_mousewheel_camera_row = -1;
520                         return false;
521                 }
522                 int column = destination->columnAt(wheel->x());
523                 int row = destination->rowAt(wheel->y());
524                 if (column == -1 || row == -1) return false;
525
526                 currently_deferring_model_changes = true;
527                 {
528                         current_change_id = (watched == ui->clip_list->viewport()) ? "cliplist:" : "playlist:";
529                         ClipProxy clip = (watched == ui->clip_list->viewport()) ?
530                                 cliplist_clips->mutable_clip(row) : playlist_clips->mutable_clip(row);
531                         if (watched == ui->playlist->viewport()) {
532                                 stream_idx = clip->stream_idx;
533                         }
534
535                         if (column != camera_column) {
536                                 last_mousewheel_camera_row = -1;
537                         }
538                         if (column == in_column) {
539                                 current_change_id += "in:" + to_string(row);
540                                 int64_t pts = clip->pts_in + wheel->angleDelta().y() * wheel_sensitivity;
541                                 set_pts_in(pts, current_pts, clip);
542                                 preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
543                         } else if (column == out_column) {
544                                 current_change_id += "out:" + to_string(row);
545                                 int64_t pts = clip->pts_out + wheel->angleDelta().y() * wheel_sensitivity;
546                                 pts = std::max(pts, clip->pts_in);
547                                 pts = std::min(pts, current_pts);
548                                 clip->pts_out = pts;
549                                 preview_single_frame(pts, stream_idx, LAST_BEFORE);
550                         } else if (column == camera_column) {
551                                 current_change_id += "camera:" + to_string(row);
552                                 int angle_degrees = wheel->angleDelta().y();
553                                 if (last_mousewheel_camera_row == row) {
554                                         angle_degrees += leftover_angle_degrees;
555                                 }
556
557                                 int stream_idx = clip->stream_idx + angle_degrees / camera_degrees_per_pixel;
558                                 stream_idx = std::max(stream_idx, 0);
559                                 stream_idx = std::min(stream_idx, NUM_CAMERAS - 1);
560                                 clip->stream_idx = stream_idx;
561
562                                 last_mousewheel_camera_row = row;
563                                 leftover_angle_degrees = angle_degrees % camera_degrees_per_pixel;
564
565                                 // Don't update the live view, that's rarely what the operator wants.
566                         }
567                 }
568                 currently_deferring_model_changes = false;
569         } else if (event->type() == QEvent::MouseButtonRelease) {
570                 scrubbing = false;
571         }
572         return false;
573 }
574
575 void MainWindow::preview_single_frame(int64_t pts, unsigned stream_idx, MainWindow::Rounding rounding)
576 {
577         if (rounding == LAST_BEFORE) {
578                 lock_guard<mutex> lock(frame_mu);
579                 if (frames[stream_idx].empty()) return;
580                 auto it = lower_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts);
581                 if (it != frames[stream_idx].end()) {
582                         pts = *it;
583                 }
584         } else {
585                 assert(rounding == FIRST_AT_OR_AFTER);
586                 lock_guard<mutex> lock(frame_mu);
587                 if (frames[stream_idx].empty()) return;
588                 auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts - 1);
589                 if (it != frames[stream_idx].end()) {
590                         pts = *it;
591                 }
592         }
593
594         Clip fake_clip;
595         fake_clip.pts_in = pts;
596         fake_clip.pts_out = pts + 1;
597         preview_player->play_clip(fake_clip, stream_idx);
598 }
599
600 void MainWindow::playlist_selection_changed()
601 {
602         QItemSelectionModel *selected = ui->playlist->selectionModel();
603         bool any_selected = selected->hasSelection();
604         ui->playlist_duplicate_btn->setEnabled(any_selected);
605         ui->playlist_remove_btn->setEnabled(any_selected);
606         ui->playlist_move_up_btn->setEnabled(
607                 any_selected && selected->selectedRows().front().row() > 0);
608         ui->playlist_move_down_btn->setEnabled(
609                 any_selected && selected->selectedRows().back().row() < int(playlist_clips->size()) - 1);
610         ui->play_btn->setEnabled(!playlist_clips->empty());
611 }
612
613 void MainWindow::report_disk_space(off_t free_bytes, double estimated_seconds_left)
614 {
615         char time_str[256];
616         if (estimated_seconds_left < 60.0) {
617                 strcpy(time_str, "<font color=\"red\">Less than a minute</font>");
618         } else if (estimated_seconds_left < 1800.0) {  // Less than half an hour: Xm Ys (red).
619                 int s = lrintf(estimated_seconds_left);
620                 int m = s / 60;
621                 s %= 60;
622                 snprintf(time_str, sizeof(time_str), "<font color=\"red\">%dm %ds</font>", m, s);
623         } else if (estimated_seconds_left < 3600.0) {  // Less than an hour: Xm.
624                 int m = lrintf(estimated_seconds_left / 60.0);
625                 snprintf(time_str, sizeof(time_str), "%dm", m);
626         } else if (estimated_seconds_left < 36000.0) {  // Less than ten hours: Xh Ym.
627                 int m = lrintf(estimated_seconds_left / 60.0);
628                 int h = m / 60;
629                 m %= 60;
630                 snprintf(time_str, sizeof(time_str), "%dh %dm", h, m);
631         } else {  // More than ten hours: Xh.
632                 int h = lrintf(estimated_seconds_left / 3600.0);
633                 snprintf(time_str, sizeof(time_str), "%dh", h);
634         }
635         char buf[256];
636         snprintf(buf, sizeof(buf), "Disk free: %'.0f MB (approx. %s)", free_bytes / 1048576.0, time_str);
637
638         std::string label = buf;
639
640         post_to_main_thread([this, label]{
641                         disk_free_label->setText(QString::fromStdString(label));
642                         ui->menuBar->setCornerWidget(disk_free_label);  // Need to set this again for the sizing to get right.
643                         });
644 }
645
646 void MainWindow::exit_triggered()
647 {
648         close();
649 }
650