]> git.sesse.net Git - nageru/blob - mainwindow.cpp
Show time remaining when playing clips live.
[nageru] / mainwindow.cpp
1 #include "mainwindow.h"
2
3 #include "clip_list.h"
4 #include "player.h"
5 #include "post_to_main_thread.h"
6 #include "timebase.h"
7 #include "ui_mainwindow.h"
8
9 #include <string>
10 #include <vector>
11
12 #include <QMouseEvent>
13 #include <QWheelEvent>
14 #include <QShortcut>
15
16 using namespace std;
17
18 MainWindow *global_mainwindow = nullptr;
19 ClipList *cliplist_clips;
20 PlayList *playlist_clips;
21
22 extern int64_t current_pts;
23 extern mutex frame_mu;
24 extern vector<int64_t> frames[MAX_STREAMS];
25
26 MainWindow::MainWindow()
27         : ui(new Ui::MainWindow)
28 {
29         global_mainwindow = this;
30         ui->setupUi(this);
31
32         cliplist_clips = new ClipList();
33         ui->clip_list->setModel(cliplist_clips);
34
35         playlist_clips = new PlayList();
36         ui->playlist->setModel(playlist_clips);
37
38         // For scrubbing in the pts columns.
39         ui->clip_list->viewport()->installEventFilter(this);
40         ui->playlist->viewport()->installEventFilter(this);
41
42         QShortcut *cue_in = new QShortcut(QKeySequence(Qt::Key_A), this);
43         connect(cue_in, &QShortcut::activated, ui->cue_in_btn, &QPushButton::click);
44         connect(ui->cue_in_btn, &QPushButton::clicked, this, &MainWindow::cue_in_clicked);
45
46         QShortcut *cue_out = new QShortcut(QKeySequence(Qt::Key_S), this);
47         connect(cue_out, &QShortcut::activated, ui->cue_out_btn, &QPushButton::click);
48         connect(ui->cue_out_btn, &QPushButton::clicked, this, &MainWindow::cue_out_clicked);
49
50         QShortcut *queue = new QShortcut(QKeySequence(Qt::Key_Q), this);
51         connect(queue, &QShortcut::activated, ui->queue_btn, &QPushButton::click);
52         connect(ui->queue_btn, &QPushButton::clicked, this, &MainWindow::queue_clicked);
53
54         QShortcut *preview = new QShortcut(QKeySequence(Qt::Key_W), this);
55         connect(preview, &QShortcut::activated, ui->preview_btn, &QPushButton::click);
56         connect(ui->preview_btn, &QPushButton::clicked, this, &MainWindow::preview_clicked);
57
58         QShortcut *play = new QShortcut(QKeySequence(Qt::Key_Space), this);
59         connect(play, &QShortcut::activated, ui->play_btn, &QPushButton::click);
60         connect(ui->play_btn, &QPushButton::clicked, this, &MainWindow::play_clicked);
61
62         QShortcut *preview_1 = new QShortcut(QKeySequence(Qt::Key_1), this);
63         connect(preview_1, &QShortcut::activated, ui->preview_1_btn, &QPushButton::click);
64         connect(ui->input1_display, &JPEGFrameView::clicked, ui->preview_1_btn, &QPushButton::click);
65         connect(ui->preview_1_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(0); });
66         ui->input1_display->set_overlay("1");
67
68         QShortcut *preview_2 = new QShortcut(QKeySequence(Qt::Key_2), this);
69         connect(preview_2, &QShortcut::activated, ui->preview_2_btn, &QPushButton::click);
70         connect(ui->input2_display, &JPEGFrameView::clicked, ui->preview_2_btn, &QPushButton::click);
71         connect(ui->preview_2_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(1); });
72         ui->input2_display->set_overlay("2");
73
74         QShortcut *preview_3 = new QShortcut(QKeySequence(Qt::Key_3), this);
75         connect(preview_3, &QShortcut::activated, ui->preview_3_btn, &QPushButton::click);
76         connect(ui->input3_display, &JPEGFrameView::clicked, ui->preview_3_btn, &QPushButton::click);
77         connect(ui->preview_3_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(2); });
78         ui->input3_display->set_overlay("3");
79
80         QShortcut *preview_4 = new QShortcut(QKeySequence(Qt::Key_4), this);
81         connect(preview_4, &QShortcut::activated, ui->preview_4_btn, &QPushButton::click);
82         connect(ui->input4_display, &JPEGFrameView::clicked, ui->preview_4_btn, &QPushButton::click);
83         connect(ui->preview_4_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(3); });
84         ui->input4_display->set_overlay("4");
85
86         connect(ui->playlist_duplicate_btn, &QPushButton::clicked, this, &MainWindow::playlist_duplicate);
87
88         connect(ui->playlist_remove_btn, &QPushButton::clicked, this, &MainWindow::playlist_remove);
89         QShortcut *delete_key = new QShortcut(QKeySequence(Qt::Key_Delete), ui->playlist);
90         connect(delete_key, &QShortcut::activated, [this] {
91                 if (ui->playlist->hasFocus()) {
92                         playlist_remove();
93                 }
94         });
95
96         // TODO: support drag-and-drop.
97         connect(ui->playlist_move_up_btn, &QPushButton::clicked, [this]{ playlist_move(-1); });
98         connect(ui->playlist_move_down_btn, &QPushButton::clicked, [this]{ playlist_move(1); });
99
100         connect(ui->playlist->selectionModel(), &QItemSelectionModel::selectionChanged,
101                 this, &MainWindow::playlist_selection_changed);
102         playlist_selection_changed();  // First time set-up.
103
104         preview_player = new Player(ui->preview_display, /*also_output_to_stream=*/false);
105         live_player = new Player(ui->live_display, /*also_output_to_stream=*/true);
106         live_player->set_done_callback([this]{
107                 post_to_main_thread([this]{
108                         live_player_clip_done();
109                 });
110         });
111         live_player->set_progress_callback([this](double played_this_clip, double total_length) {
112                 post_to_main_thread([this, played_this_clip, total_length] {
113                         live_player_clip_progress(played_this_clip, total_length);
114                 });
115         });
116 }
117
118 void MainWindow::cue_in_clicked()
119 {
120         if (!cliplist_clips->empty() && cliplist_clips->back()->pts_out < 0) {
121                 cliplist_clips->back()->pts_in = current_pts;
122                 return;
123         }
124         Clip clip;
125         clip.pts_in = current_pts;
126         cliplist_clips->add_clip(clip);
127         playlist_selection_changed();
128 }
129
130 void MainWindow::cue_out_clicked()
131 {
132         if (!cliplist_clips->empty()) {
133                 cliplist_clips->back()->pts_out = current_pts;
134                 // TODO: select the row in the clip list?
135         }
136 }
137
138 void MainWindow::queue_clicked()
139 {
140         if (cliplist_clips->empty()) {
141                 return;
142         }
143
144         QItemSelectionModel *selected = ui->clip_list->selectionModel();
145         if (!selected->hasSelection()) {
146                 Clip clip = *cliplist_clips->back();
147                 clip.stream_idx = 0;
148                 playlist_clips->add_clip(clip);
149                 return;
150         }
151
152         QModelIndex index = selected->currentIndex();
153         if (index.column() >= int(ClipList::Column::CAMERA_1) &&
154             index.column() <= int(ClipList::Column::CAMERA_4)) {
155                 Clip clip = *cliplist_clips->clip(index.row());
156                 clip.stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
157                 playlist_clips->add_clip(clip);
158                 playlist_selection_changed();
159         }
160 }
161
162 void MainWindow::preview_clicked()
163 {
164         if (cliplist_clips->empty()) return;
165
166         QItemSelectionModel *selected = ui->clip_list->selectionModel();
167         if (!selected->hasSelection()) {
168                 preview_player->play_clip(*cliplist_clips->back(), 0);
169                 return;
170         }
171
172         QModelIndex index = selected->currentIndex();
173         if (index.column() >= int(ClipList::Column::CAMERA_1) &&
174             index.column() <= int(ClipList::Column::CAMERA_4)) {
175                 unsigned stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
176                 preview_player->play_clip(*cliplist_clips->clip(index.row()), stream_idx);
177         }
178 }
179
180 void MainWindow::preview_angle_clicked(unsigned stream_idx)
181 {
182         preview_player->override_angle(stream_idx);
183
184         // Change the selection if we were previewing a clip from the clip list.
185         // (The only other thing we could be showing is a pts scrub, and if so,
186         // that would be selected.)
187         QItemSelectionModel *selected = ui->clip_list->selectionModel();
188         if (selected->hasSelection()) {
189                 QModelIndex cell = selected->selectedIndexes()[0];
190                 int column = int(ClipList::Column::CAMERA_1) + stream_idx;
191                 selected->setCurrentIndex(cell.sibling(cell.row(), column), QItemSelectionModel::ClearAndSelect);
192         }
193 }
194
195 void MainWindow::playlist_duplicate()
196 {
197         QItemSelectionModel *selected = ui->playlist->selectionModel();
198         if (!selected->hasSelection()) {
199                 // Should have been grayed out, but OK.
200                 return;
201         }
202         QModelIndexList rows = selected->selectedRows();
203         int first = rows.front().row(), last = rows.back().row();
204         playlist_clips->duplicate_clips(first, last);
205         playlist_selection_changed();
206 }
207
208 void MainWindow::playlist_remove()
209 {
210         QItemSelectionModel *selected = ui->playlist->selectionModel();
211         if (!selected->hasSelection()) {
212                 // Should have been grayed out, but OK.
213                 return;
214         }
215         QModelIndexList rows = selected->selectedRows();
216         int first = rows.front().row(), last = rows.back().row();
217         playlist_clips->erase_clips(first, last);
218
219         // TODO: select the next one in the list?
220
221         playlist_selection_changed();
222 }
223
224 void MainWindow::playlist_move(int delta)
225 {
226         QItemSelectionModel *selected = ui->playlist->selectionModel();
227         if (!selected->hasSelection()) {
228                 // Should have been grayed out, but OK.
229                 return;
230         }
231
232         QModelIndexList rows = selected->selectedRows();
233         int first = rows.front().row(), last = rows.back().row();
234         if ((delta == -1 && first == 0) ||
235             (delta == 1 && size_t(last) == playlist_clips->size() - 1)) {
236                 // Should have been grayed out, but OK.
237                 return;
238         }
239
240         playlist_clips->move_clips(first, last, delta);
241         playlist_selection_changed();
242 }
243
244 void MainWindow::play_clicked()
245 {
246         if (playlist_clips->empty()) return;
247
248         QItemSelectionModel *selected = ui->playlist->selectionModel();
249         int row;
250         if (!selected->hasSelection()) {
251                 row = 0;
252         } else {
253                 row = selected->selectedRows(0)[0].row();
254         }
255
256         const Clip &clip = *playlist_clips->clip(row);
257         live_player->play_clip(clip, clip.stream_idx);
258         playlist_clips->set_currently_playing(row);
259         playlist_selection_changed();
260 }
261
262 void MainWindow::live_player_clip_done()
263 {
264         int row = playlist_clips->get_currently_playing();
265         if (row != -1 && row < int(playlist_clips->size()) - 1) {
266                 ++row;
267                 const Clip &clip = *playlist_clips->clip(row);
268                 live_player->play_clip(clip, clip.stream_idx);
269                 playlist_clips->set_currently_playing(row);
270         } else {
271                 playlist_clips->set_currently_playing(-1);
272                 ui->live_label->setText("Current output (paused)");
273         }
274 }
275
276 void MainWindow::live_player_clip_progress(double played_this_clip, double total_length)
277 {
278         double remaining = total_length - played_this_clip;
279         for (int row = playlist_clips->get_currently_playing() + 1; row < int(playlist_clips->size()); ++row) {
280                 const Clip clip = *playlist_clips->clip(row);
281                 remaining += double(clip.pts_out - clip.pts_in) / TIMEBASE / 0.5;   // FIXME: stop hardcoding speed.
282         }
283         int remaining_ms = lrint(remaining * 1e3);
284
285         int ms = remaining_ms % 1000;
286         remaining_ms /= 1000;
287         int s = remaining_ms % 60;
288         remaining_ms /= 60;
289         int m = remaining_ms;
290
291         char buf[256];
292         snprintf(buf, sizeof(buf), "Current output (%d:%02d.%03d left)", m, s, ms);
293         ui->live_label->setText(buf);
294 }
295
296 void MainWindow::resizeEvent(QResizeEvent *event)
297 {
298         QMainWindow::resizeEvent(event);
299
300         // Ask for a relayout, but only after the event loop is done doing relayout
301         // on everything else.
302         QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
303 }
304
305 void MainWindow::relayout()
306 {
307         ui->live_display->setMinimumHeight(ui->live_display->width() * 9 / 16);
308 }
309
310 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
311 {
312         constexpr int dead_zone_pixels = 3;  // To avoid that simple clicks get misinterpreted.
313         constexpr int scrub_sensitivity = 100;  // pts units per pixel.
314         constexpr int wheel_sensitivity = 100;  // pts units per degree.
315         constexpr int camera_degrees_per_pixel = 15;  // One click of most mice.
316
317         unsigned stream_idx = ui->preview_display->get_stream_idx();
318
319         if (event->type() != QEvent::Wheel) {
320                 last_mousewheel_camera_row = -1;
321         }
322
323         if (event->type() == QEvent::MouseButtonPress) {
324                 QMouseEvent *mouse = (QMouseEvent *)event;
325
326                 QTableView *destination;
327                 ScrubType type;
328
329                 if (watched == ui->clip_list->viewport()) {
330                         destination = ui->clip_list;
331                         type = SCRUBBING_CLIP_LIST;
332                 } else if (watched == ui->playlist->viewport()) {
333                         destination = ui->playlist;
334                         type = SCRUBBING_PLAYLIST;
335                 } else {
336                         return false;
337                 }
338                 int column = destination->columnAt(mouse->x());
339                 int row = destination->rowAt(mouse->y());
340                 if (column == -1 || row == -1) return false;
341
342                 if (type == SCRUBBING_CLIP_LIST) {
343                         if (ClipList::Column(column) == ClipList::Column::IN) {
344                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_in;
345                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
346                         } else if (ClipList::Column(column) == ClipList::Column::OUT) {
347                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_out;
348                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
349                         } else {
350                                 return false;
351                         }
352                 } else {
353                         if (PlayList::Column(column) == PlayList::Column::IN) {
354                                 scrub_pts_origin = playlist_clips->clip(row)->pts_in;
355                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
356                         } else if (PlayList::Column(column) == PlayList::Column::OUT) {
357                                 scrub_pts_origin = playlist_clips->clip(row)->pts_out;
358                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
359                         } else {
360                                 return false;
361                         }
362                 }
363
364                 scrubbing = true;
365                 scrub_row = row;
366                 scrub_column = column;
367                 scrub_x_origin = mouse->x();
368                 scrub_type = type;
369         } else if (event->type() == QEvent::MouseMove) {
370                 if (scrubbing) {
371                         QMouseEvent *mouse = (QMouseEvent *)event;
372                         int offset = mouse->x() - scrub_x_origin;
373                         int adjusted_offset;
374                         if (offset >= dead_zone_pixels) {
375                                 adjusted_offset = offset - dead_zone_pixels;
376                         } else if (offset < -dead_zone_pixels) {
377                                 adjusted_offset = offset + dead_zone_pixels;
378                         } else {
379                                 adjusted_offset = 0;
380                         }
381
382                         int64_t pts = scrub_pts_origin + adjusted_offset * scrub_sensitivity;
383
384                         if (scrub_type == SCRUBBING_CLIP_LIST) {
385                                 ClipProxy clip = cliplist_clips->clip(scrub_row);
386                                 if (scrub_column == int(ClipList::Column::IN)) {
387                                         pts = std::max<int64_t>(pts, 0);
388                                         pts = std::min(pts, clip->pts_out);
389                                         clip->pts_in = pts;
390                                         preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
391                                 } else {
392                                         pts = std::max(pts, clip->pts_in);
393                                         pts = std::min(pts, current_pts);
394                                         clip->pts_out = pts;
395                                         preview_single_frame(pts, stream_idx, LAST_BEFORE);
396                                 }
397                         } else {
398                                 ClipProxy clip = playlist_clips->clip(scrub_row);
399                                 if (scrub_column == int(PlayList::Column::IN)) {
400                                         pts = std::max<int64_t>(pts, 0);
401                                         pts = std::min(pts, clip->pts_out);
402                                         clip->pts_in = pts;
403                                         preview_single_frame(pts, clip->stream_idx, FIRST_AT_OR_AFTER);
404                                 } else {
405                                         pts = std::max(pts, clip->pts_in);
406                                         pts = std::min(pts, current_pts);
407                                         clip->pts_out = pts;
408                                         preview_single_frame(pts, clip->stream_idx, LAST_BEFORE);
409                                 }
410                         }
411
412                         return true;  // Don't use this mouse movement for selecting things.
413                 }
414         } else if (event->type() == QEvent::Wheel) {
415                 QWheelEvent *wheel = (QWheelEvent *)event;
416
417                 QTableView *destination;
418                 int in_column, out_column, camera_column;
419                 if (watched == ui->clip_list->viewport()) {
420                         destination = ui->clip_list;
421                         in_column = int(ClipList::Column::IN);
422                         out_column = int(ClipList::Column::OUT);
423                         camera_column = -1;
424                         last_mousewheel_camera_row = -1;
425                 } else if (watched == ui->playlist->viewport()) {
426                         destination = ui->playlist;
427                         in_column = int(PlayList::Column::IN);
428                         out_column = int(PlayList::Column::OUT);
429                         camera_column = int(PlayList::Column::CAMERA);
430                 } else {
431                         last_mousewheel_camera_row = -1;
432                         return false;
433                 }
434                 int column = destination->columnAt(wheel->x());
435                 int row = destination->rowAt(wheel->y());
436                 if (column == -1 || row == -1) return false;
437
438                 ClipProxy clip = (watched == ui->clip_list->viewport()) ?
439                         cliplist_clips->clip(row) : playlist_clips->clip(row);
440                 if (watched == ui->playlist->viewport()) {
441                         stream_idx = clip->stream_idx;
442                 }
443
444                 if (column != camera_column) {
445                         last_mousewheel_camera_row = -1;
446                 }
447                 if (column == in_column) {
448                         int64_t pts = clip->pts_in + wheel->angleDelta().y() * wheel_sensitivity;
449                         pts = std::max<int64_t>(pts, 0);
450                         pts = std::min(pts, clip->pts_out);
451                         clip->pts_in = pts;
452                         preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
453                 } else if (column == out_column) {
454                         int64_t pts = clip->pts_out + wheel->angleDelta().y() * wheel_sensitivity;
455                         pts = std::max(pts, clip->pts_in);
456                         pts = std::min(pts, current_pts);
457                         clip->pts_out = pts;
458                         preview_single_frame(pts, stream_idx, LAST_BEFORE);
459                 } else if (column == camera_column) {
460                         int angle_degrees = wheel->angleDelta().y();
461                         if (last_mousewheel_camera_row == row) {
462                                 angle_degrees += leftover_angle_degrees;
463                         }
464
465                         int stream_idx = clip->stream_idx + angle_degrees / camera_degrees_per_pixel;
466                         stream_idx = std::max(stream_idx, 0);
467                         stream_idx = std::min(stream_idx, NUM_CAMERAS - 1);
468                         clip->stream_idx = stream_idx;
469
470                         last_mousewheel_camera_row = row;
471                         leftover_angle_degrees = angle_degrees % camera_degrees_per_pixel;
472
473                         // Don't update the live view, that's rarely what the operator wants.
474                 }
475         } else if (event->type() == QEvent::MouseButtonRelease) {
476                 scrubbing = false;
477         }
478         return false;
479 }
480
481 void MainWindow::preview_single_frame(int64_t pts, unsigned stream_idx, MainWindow::Rounding rounding)
482 {
483         if (rounding == LAST_BEFORE) {
484                 lock_guard<mutex> lock(frame_mu);
485                 if (frames[stream_idx].empty()) return;
486                 auto it = lower_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts);
487                 if (it != frames[stream_idx].end()) {
488                         pts = *it;
489                 }
490         } else {
491                 assert(rounding == FIRST_AT_OR_AFTER);
492                 lock_guard<mutex> lock(frame_mu);
493                 if (frames[stream_idx].empty()) return;
494                 auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts - 1);
495                 if (it != frames[stream_idx].end()) {
496                         pts = *it;
497                 }
498         }
499
500         Clip fake_clip;
501         fake_clip.pts_in = pts;
502         fake_clip.pts_out = pts + 1;
503         preview_player->play_clip(fake_clip, stream_idx);
504 }
505
506 void MainWindow::playlist_selection_changed()
507 {
508         QItemSelectionModel *selected = ui->playlist->selectionModel();
509         bool any_selected = selected->hasSelection();
510         ui->playlist_duplicate_btn->setEnabled(any_selected);
511         ui->playlist_remove_btn->setEnabled(any_selected);
512         ui->playlist_move_up_btn->setEnabled(
513                 any_selected && selected->selectedRows().front().row() > 0);
514         ui->playlist_move_down_btn->setEnabled(
515                 any_selected && selected->selectedRows().back().row() < int(playlist_clips->size()) - 1);
516         ui->play_btn->setEnabled(!playlist_clips->empty());
517 }