]> git.sesse.net Git - nageru/blob - mainwindow.cpp
Mark play progress with background color in the play list.
[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->mutable_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->mutable_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         Clip clip = *cliplist_clips->clip(index.row());
154         if (index.column() >= int(ClipList::Column::CAMERA_1) &&
155             index.column() <= int(ClipList::Column::CAMERA_4)) {
156                 clip.stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
157         } else {
158                 clip.stream_idx = ui->preview_display->get_stream_idx();
159         }
160
161         playlist_clips->add_clip(clip);
162         playlist_selection_changed();
163 }
164
165 void MainWindow::preview_clicked()
166 {
167         if (cliplist_clips->empty()) return;
168
169         QItemSelectionModel *selected = ui->clip_list->selectionModel();
170         if (!selected->hasSelection()) {
171                 preview_player->play_clip(*cliplist_clips->back(), 0);
172                 return;
173         }
174
175         QModelIndex index = selected->currentIndex();
176         unsigned stream_idx;
177         if (index.column() >= int(ClipList::Column::CAMERA_1) &&
178             index.column() <= int(ClipList::Column::CAMERA_4)) {
179                 stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
180         } else {
181                 stream_idx = ui->preview_display->get_stream_idx();
182         }
183         preview_player->play_clip(*cliplist_clips->clip(index.row()), stream_idx);
184 }
185
186 void MainWindow::preview_angle_clicked(unsigned stream_idx)
187 {
188         preview_player->override_angle(stream_idx);
189
190         // Change the selection if we were previewing a clip from the clip list.
191         // (The only other thing we could be showing is a pts scrub, and if so,
192         // that would be selected.)
193         QItemSelectionModel *selected = ui->clip_list->selectionModel();
194         if (selected->hasSelection()) {
195                 QModelIndex cell = selected->selectedIndexes()[0];
196                 int column = int(ClipList::Column::CAMERA_1) + stream_idx;
197                 selected->setCurrentIndex(cell.sibling(cell.row(), column), QItemSelectionModel::ClearAndSelect);
198         }
199 }
200
201 void MainWindow::playlist_duplicate()
202 {
203         QItemSelectionModel *selected = ui->playlist->selectionModel();
204         if (!selected->hasSelection()) {
205                 // Should have been grayed out, but OK.
206                 return;
207         }
208         QModelIndexList rows = selected->selectedRows();
209         int first = rows.front().row(), last = rows.back().row();
210         playlist_clips->duplicate_clips(first, last);
211         playlist_selection_changed();
212 }
213
214 void MainWindow::playlist_remove()
215 {
216         QItemSelectionModel *selected = ui->playlist->selectionModel();
217         if (!selected->hasSelection()) {
218                 // Should have been grayed out, but OK.
219                 return;
220         }
221         QModelIndexList rows = selected->selectedRows();
222         int first = rows.front().row(), last = rows.back().row();
223         playlist_clips->erase_clips(first, last);
224
225         // TODO: select the next one in the list?
226
227         playlist_selection_changed();
228 }
229
230 void MainWindow::playlist_move(int delta)
231 {
232         QItemSelectionModel *selected = ui->playlist->selectionModel();
233         if (!selected->hasSelection()) {
234                 // Should have been grayed out, but OK.
235                 return;
236         }
237
238         QModelIndexList rows = selected->selectedRows();
239         int first = rows.front().row(), last = rows.back().row();
240         if ((delta == -1 && first == 0) ||
241             (delta == 1 && size_t(last) == playlist_clips->size() - 1)) {
242                 // Should have been grayed out, but OK.
243                 return;
244         }
245
246         playlist_clips->move_clips(first, last, delta);
247         playlist_selection_changed();
248 }
249
250 void MainWindow::play_clicked()
251 {
252         if (playlist_clips->empty()) return;
253
254         QItemSelectionModel *selected = ui->playlist->selectionModel();
255         int row;
256         if (!selected->hasSelection()) {
257                 row = 0;
258         } else {
259                 row = selected->selectedRows(0)[0].row();
260         }
261
262         const Clip &clip = *playlist_clips->clip(row);
263         live_player->play_clip(clip, clip.stream_idx);
264         playlist_clips->set_currently_playing(row, 0.0f);
265         playlist_selection_changed();
266 }
267
268 void MainWindow::live_player_clip_done()
269 {
270         int row = playlist_clips->get_currently_playing();
271         if (row != -1 && row < int(playlist_clips->size()) - 1) {
272                 ++row;
273                 const Clip &clip = *playlist_clips->clip(row);
274                 live_player->play_clip(clip, clip.stream_idx);
275                 playlist_clips->set_currently_playing(row, 0.0f);
276         } else {
277                 playlist_clips->set_currently_playing(-1, 0.0f);
278                 ui->live_label->setText("Current output (paused)");
279         }
280 }
281
282 void MainWindow::live_player_clip_progress(double played_this_clip, double total_length)
283 {
284         playlist_clips->set_currently_playing(playlist_clips->get_currently_playing(), played_this_clip / total_length);
285
286         double remaining = total_length - played_this_clip;
287         for (int row = playlist_clips->get_currently_playing() + 1; row < int(playlist_clips->size()); ++row) {
288                 const Clip clip = *playlist_clips->clip(row);
289                 remaining += double(clip.pts_out - clip.pts_in) / TIMEBASE / 0.5;   // FIXME: stop hardcoding speed.
290         }
291         int remaining_ms = lrint(remaining * 1e3);
292
293         int ms = remaining_ms % 1000;
294         remaining_ms /= 1000;
295         int s = remaining_ms % 60;
296         remaining_ms /= 60;
297         int m = remaining_ms;
298
299         char buf[256];
300         snprintf(buf, sizeof(buf), "Current output (%d:%02d.%03d left)", m, s, ms);
301         ui->live_label->setText(buf);
302 }
303
304 void MainWindow::resizeEvent(QResizeEvent *event)
305 {
306         QMainWindow::resizeEvent(event);
307
308         // Ask for a relayout, but only after the event loop is done doing relayout
309         // on everything else.
310         QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
311 }
312
313 void MainWindow::relayout()
314 {
315         ui->live_display->setMinimumWidth(ui->live_display->height() * 16 / 9);
316         ui->preview_display->setMinimumWidth(ui->preview_display->height() * 16 / 9);
317 }
318
319 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
320 {
321         constexpr int dead_zone_pixels = 3;  // To avoid that simple clicks get misinterpreted.
322         constexpr int scrub_sensitivity = 100;  // pts units per pixel.
323         constexpr int wheel_sensitivity = 100;  // pts units per degree.
324         constexpr int camera_degrees_per_pixel = 15;  // One click of most mice.
325
326         unsigned stream_idx = ui->preview_display->get_stream_idx();
327
328         if (event->type() != QEvent::Wheel) {
329                 last_mousewheel_camera_row = -1;
330         }
331
332         if (event->type() == QEvent::MouseButtonPress) {
333                 QMouseEvent *mouse = (QMouseEvent *)event;
334
335                 QTableView *destination;
336                 ScrubType type;
337
338                 if (watched == ui->clip_list->viewport()) {
339                         destination = ui->clip_list;
340                         type = SCRUBBING_CLIP_LIST;
341                 } else if (watched == ui->playlist->viewport()) {
342                         destination = ui->playlist;
343                         type = SCRUBBING_PLAYLIST;
344                 } else {
345                         return false;
346                 }
347                 int column = destination->columnAt(mouse->x());
348                 int row = destination->rowAt(mouse->y());
349                 if (column == -1 || row == -1) return false;
350
351                 if (type == SCRUBBING_CLIP_LIST) {
352                         if (ClipList::Column(column) == ClipList::Column::IN) {
353                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_in;
354                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
355                         } else if (ClipList::Column(column) == ClipList::Column::OUT) {
356                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_out;
357                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
358                         } else {
359                                 return false;
360                         }
361                 } else {
362                         if (PlayList::Column(column) == PlayList::Column::IN) {
363                                 scrub_pts_origin = playlist_clips->clip(row)->pts_in;
364                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
365                         } else if (PlayList::Column(column) == PlayList::Column::OUT) {
366                                 scrub_pts_origin = playlist_clips->clip(row)->pts_out;
367                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
368                         } else {
369                                 return false;
370                         }
371                 }
372
373                 scrubbing = true;
374                 scrub_row = row;
375                 scrub_column = column;
376                 scrub_x_origin = mouse->x();
377                 scrub_type = type;
378         } else if (event->type() == QEvent::MouseMove) {
379                 if (scrubbing) {
380                         QMouseEvent *mouse = (QMouseEvent *)event;
381                         int offset = mouse->x() - scrub_x_origin;
382                         int adjusted_offset;
383                         if (offset >= dead_zone_pixels) {
384                                 adjusted_offset = offset - dead_zone_pixels;
385                         } else if (offset < -dead_zone_pixels) {
386                                 adjusted_offset = offset + dead_zone_pixels;
387                         } else {
388                                 adjusted_offset = 0;
389                         }
390
391                         int64_t pts = scrub_pts_origin + adjusted_offset * scrub_sensitivity;
392
393                         if (scrub_type == SCRUBBING_CLIP_LIST) {
394                                 ClipProxy clip = cliplist_clips->mutable_clip(scrub_row);
395                                 if (scrub_column == int(ClipList::Column::IN)) {
396                                         pts = std::max<int64_t>(pts, 0);
397                                         pts = std::min(pts, clip->pts_out);
398                                         clip->pts_in = pts;
399                                         preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
400                                 } else {
401                                         pts = std::max(pts, clip->pts_in);
402                                         pts = std::min(pts, current_pts);
403                                         clip->pts_out = pts;
404                                         preview_single_frame(pts, stream_idx, LAST_BEFORE);
405                                 }
406                         } else {
407                                 ClipProxy clip = playlist_clips->mutable_clip(scrub_row);
408                                 if (scrub_column == int(PlayList::Column::IN)) {
409                                         pts = std::max<int64_t>(pts, 0);
410                                         pts = std::min(pts, clip->pts_out);
411                                         clip->pts_in = pts;
412                                         preview_single_frame(pts, clip->stream_idx, FIRST_AT_OR_AFTER);
413                                 } else {
414                                         pts = std::max(pts, clip->pts_in);
415                                         pts = std::min(pts, current_pts);
416                                         clip->pts_out = pts;
417                                         preview_single_frame(pts, clip->stream_idx, LAST_BEFORE);
418                                 }
419                         }
420
421                         return true;  // Don't use this mouse movement for selecting things.
422                 }
423         } else if (event->type() == QEvent::Wheel) {
424                 QWheelEvent *wheel = (QWheelEvent *)event;
425
426                 QTableView *destination;
427                 int in_column, out_column, camera_column;
428                 if (watched == ui->clip_list->viewport()) {
429                         destination = ui->clip_list;
430                         in_column = int(ClipList::Column::IN);
431                         out_column = int(ClipList::Column::OUT);
432                         camera_column = -1;
433                         last_mousewheel_camera_row = -1;
434                 } else if (watched == ui->playlist->viewport()) {
435                         destination = ui->playlist;
436                         in_column = int(PlayList::Column::IN);
437                         out_column = int(PlayList::Column::OUT);
438                         camera_column = int(PlayList::Column::CAMERA);
439                 } else {
440                         last_mousewheel_camera_row = -1;
441                         return false;
442                 }
443                 int column = destination->columnAt(wheel->x());
444                 int row = destination->rowAt(wheel->y());
445                 if (column == -1 || row == -1) return false;
446
447                 ClipProxy clip = (watched == ui->clip_list->viewport()) ?
448                         cliplist_clips->mutable_clip(row) : playlist_clips->mutable_clip(row);
449                 if (watched == ui->playlist->viewport()) {
450                         stream_idx = clip->stream_idx;
451                 }
452
453                 if (column != camera_column) {
454                         last_mousewheel_camera_row = -1;
455                 }
456                 if (column == in_column) {
457                         int64_t pts = clip->pts_in + wheel->angleDelta().y() * wheel_sensitivity;
458                         pts = std::max<int64_t>(pts, 0);
459                         pts = std::min(pts, clip->pts_out);
460                         clip->pts_in = pts;
461                         preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
462                 } else if (column == out_column) {
463                         int64_t pts = clip->pts_out + wheel->angleDelta().y() * wheel_sensitivity;
464                         pts = std::max(pts, clip->pts_in);
465                         pts = std::min(pts, current_pts);
466                         clip->pts_out = pts;
467                         preview_single_frame(pts, stream_idx, LAST_BEFORE);
468                 } else if (column == camera_column) {
469                         int angle_degrees = wheel->angleDelta().y();
470                         if (last_mousewheel_camera_row == row) {
471                                 angle_degrees += leftover_angle_degrees;
472                         }
473
474                         int stream_idx = clip->stream_idx + angle_degrees / camera_degrees_per_pixel;
475                         stream_idx = std::max(stream_idx, 0);
476                         stream_idx = std::min(stream_idx, NUM_CAMERAS - 1);
477                         clip->stream_idx = stream_idx;
478
479                         last_mousewheel_camera_row = row;
480                         leftover_angle_degrees = angle_degrees % camera_degrees_per_pixel;
481
482                         // Don't update the live view, that's rarely what the operator wants.
483                 }
484         } else if (event->type() == QEvent::MouseButtonRelease) {
485                 scrubbing = false;
486         }
487         return false;
488 }
489
490 void MainWindow::preview_single_frame(int64_t pts, unsigned stream_idx, MainWindow::Rounding rounding)
491 {
492         if (rounding == LAST_BEFORE) {
493                 lock_guard<mutex> lock(frame_mu);
494                 if (frames[stream_idx].empty()) return;
495                 auto it = lower_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts);
496                 if (it != frames[stream_idx].end()) {
497                         pts = *it;
498                 }
499         } else {
500                 assert(rounding == FIRST_AT_OR_AFTER);
501                 lock_guard<mutex> lock(frame_mu);
502                 if (frames[stream_idx].empty()) return;
503                 auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts - 1);
504                 if (it != frames[stream_idx].end()) {
505                         pts = *it;
506                 }
507         }
508
509         Clip fake_clip;
510         fake_clip.pts_in = pts;
511         fake_clip.pts_out = pts + 1;
512         preview_player->play_clip(fake_clip, stream_idx);
513 }
514
515 void MainWindow::playlist_selection_changed()
516 {
517         QItemSelectionModel *selected = ui->playlist->selectionModel();
518         bool any_selected = selected->hasSelection();
519         ui->playlist_duplicate_btn->setEnabled(any_selected);
520         ui->playlist_remove_btn->setEnabled(any_selected);
521         ui->playlist_move_up_btn->setEnabled(
522                 any_selected && selected->selectedRows().front().row() > 0);
523         ui->playlist_move_down_btn->setEnabled(
524                 any_selected && selected->selectedRows().back().row() < int(playlist_clips->size()) - 1);
525         ui->play_btn->setEnabled(!playlist_clips->empty());
526 }