#include "flags.h"
#include "frame_on_disk.h"
#include "player.h"
+#include "futatabi_midi_mapping.pb.h"
#include "shared/aboutdialog.h"
#include "shared/disk_space_estimator.h"
#include "shared/post_to_main_thread.h"
extern int64_t current_pts;
+namespace {
+
+void set_pts_in(int64_t pts, int64_t current_pts, ClipProxy &clip)
+{
+ pts = std::max<int64_t>(pts, 0);
+ if (clip->pts_out == -1) {
+ pts = std::min(pts, current_pts);
+ } else {
+ pts = std::min(pts, clip->pts_out);
+ }
+ clip->pts_in = pts;
+}
+
+} // namespace
+
MainWindow::MainWindow()
: ui(new Ui::MainWindow),
- db(global_flags.working_directory + "/futatabi.db")
+ db(global_flags.working_directory + "/futatabi.db"),
+ midi_mapper(this)
{
global_mainwindow = this;
ui->setupUi(this);
connect(ui->stop_btn, &QPushButton::clicked, this, &MainWindow::stop_clicked);
ui->stop_btn->setEnabled(false);
+ connect(ui->speed_slider, &QAbstractSlider::valueChanged, this, &MainWindow::speed_slider_changed);
+ connect(ui->speed_lock_btn, &QPushButton::clicked, this, &MainWindow::speed_lock_clicked);
+
connect(ui->playlist_duplicate_btn, &QPushButton::clicked, this, &MainWindow::playlist_duplicate);
connect(ui->playlist_remove_btn, &QPushButton::clicked, this, &MainWindow::playlist_remove);
playlist_selection_changed(); // First time set-up.
preview_player.reset(new Player(ui->preview_display, Player::NO_STREAM_OUTPUT));
+ preview_player->set_done_callback([this] {
+ post_to_main_thread([this] {
+ preview_player_done();
+ });
+ });
+
live_player.reset(new Player(ui->live_display, Player::HTTPD_STREAM_OUTPUT));
live_player->set_done_callback([this] {
post_to_main_thread([this] {
});
});
set_output_status("paused");
+ enable_or_disable_queue_button();
defer_timeout = new QTimer(this);
defer_timeout->setSingleShot(true);
connect(defer_timeout, &QTimer::timeout, this, &MainWindow::defer_timer_expired);
ui->undo_action->setEnabled(true);
+ lock_blink_timeout = new QTimer(this);
+ lock_blink_timeout->setSingleShot(true);
+ connect(lock_blink_timeout, &QTimer::timeout, this, &MainWindow::lock_blink_timer_expired);
+
connect(ui->clip_list->selectionModel(), &QItemSelectionModel::currentChanged,
this, &MainWindow::clip_list_selection_changed);
enable_or_disable_queue_button();
if (!global_flags.tally_url.empty()) {
start_tally();
}
+
+ if (!global_flags.midi_mapping_filename.empty()) {
+ MIDIMappingProto midi_mapping;
+ if (!load_midi_mapping_from_file(global_flags.midi_mapping_filename, &midi_mapping)) {
+ fprintf(stderr, "Couldn't load MIDI mapping '%s'; exiting.\n",
+ global_flags.midi_mapping_filename.c_str());
+ exit(1);
+ }
+ midi_mapper.set_midi_mapping(midi_mapping);
+ }
+ midi_mapper.refresh_lights();
+ midi_mapper.start_thread();
}
void MainWindow::change_num_cameras()
QModelIndex index = selected->currentIndex();
const Clip &clip = *playlist_clips->clip(index.row());
preview_player->play(clip);
+ preview_playing = true;
+ enable_or_disable_preview_button();
return;
}
}
QItemSelectionModel *selected = ui->clip_list->selectionModel();
if (!selected->hasSelection()) {
preview_player->play(*cliplist_clips->back());
+ preview_playing = true;
+ enable_or_disable_preview_button();
return;
}
clip.stream_idx = ui->preview_display->get_stream_idx();
}
preview_player->play(clip);
+ preview_playing = true;
+ enable_or_disable_preview_button();
}
void MainWindow::preview_angle_clicked(unsigned stream_idx)
playlist_selection_changed();
}
+void MainWindow::jog_internal(JogDestination jog_destination, int row, int column, int stream_idx, int pts_delta)
+{
+ constexpr int camera_pts_per_pixel = 1500; // One click of most mice (15 degrees), multiplied by the default wheel_sensitivity.
+
+ int in_column, out_column, camera_column;
+ if (jog_destination == JOG_CLIP_LIST) {
+ in_column = int(ClipList::Column::IN);
+ out_column = int(ClipList::Column::OUT);
+ camera_column = -1;
+ } else if (jog_destination == JOG_PLAYLIST) {
+ in_column = int(PlayList::Column::IN);
+ out_column = int(PlayList::Column::OUT);
+ camera_column = int(PlayList::Column::CAMERA);
+ } else {
+ assert(false);
+ }
+
+ currently_deferring_model_changes = true;
+ {
+ current_change_id = (jog_destination == JOG_CLIP_LIST) ? "cliplist:" : "playlist:";
+ ClipProxy clip = (jog_destination == JOG_CLIP_LIST) ? cliplist_clips->mutable_clip(row) : playlist_clips->mutable_clip(row);
+ if (jog_destination == JOG_PLAYLIST) {
+ stream_idx = clip->stream_idx;
+ }
+
+ if (column == in_column) {
+ current_change_id += "in:" + to_string(row);
+ int64_t pts = clip->pts_in + pts_delta;
+ set_pts_in(pts, current_pts, clip);
+ preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
+ } else if (column == out_column) {
+ current_change_id += "out:" + to_string(row);
+ int64_t pts = clip->pts_out + pts_delta;
+ pts = std::max(pts, clip->pts_in);
+ pts = std::min(pts, current_pts);
+ clip->pts_out = pts;
+ preview_single_frame(pts, stream_idx, LAST_BEFORE);
+ } else if (column == camera_column) {
+ current_change_id += "camera:" + to_string(row);
+ int angle_degrees = pts_delta;
+ if (last_mousewheel_camera_row == row) {
+ angle_degrees += leftover_angle_degrees;
+ }
+
+ int stream_idx = clip->stream_idx + angle_degrees / camera_pts_per_pixel;
+ stream_idx = std::max(stream_idx, 0);
+ stream_idx = std::min<int>(stream_idx, num_cameras - 1);
+ clip->stream_idx = stream_idx;
+
+ last_mousewheel_camera_row = row;
+ leftover_angle_degrees = angle_degrees % camera_pts_per_pixel;
+
+ // Don't update the live view, that's rarely what the operator wants.
+ }
+ }
+ currently_deferring_model_changes = false;
+}
+
void MainWindow::defer_timer_expired()
{
state_changed(deferred_state);
db.store_settings(settings);
}
-void MainWindow::play_clicked()
+void MainWindow::lock_blink_timer_expired()
{
- if (playlist_clips->empty())
- return;
+ midi_mapper.set_locked(MIDIMapper::LightState(ui->speed_lock_btn->isChecked())); // Presumably On, or the timer should have been canceled.
+}
+void MainWindow::play_clicked()
+{
QItemSelectionModel *selected = ui->playlist->selectionModel();
- unsigned start_row;
if (!selected->hasSelection()) {
- start_row = 0;
- } else {
- start_row = selected->selectedRows(0)[0].row();
+ return;
}
+ unsigned start_row = selected->selectedRows(0)[0].row();
vector<ClipWithID> clips;
for (unsigned row = start_row; row < playlist_clips->size(); ++row) {
live_player->play(clips);
playlist_clips->set_progress({ { start_row, 0.0f } });
ui->playlist->selectionModel()->clear();
- playlist_selection_changed();
-
ui->stop_btn->setEnabled(true);
+ playlist_selection_changed();
}
void MainWindow::stop_clicked()
playlist_clips->set_progress({});
live_player->play(fake_clip);
ui->stop_btn->setEnabled(false);
+ playlist_selection_changed();
+}
+
+void MainWindow::speed_slider_changed(int percent)
+{
+ float speed = percent / 100.0f;
+ ui->speed_lock_btn->setText(QString::fromStdString(" " + to_string(percent) + "%"));
+ live_player->set_master_speed(speed);
+}
+
+void MainWindow::speed_lock_clicked()
+{
+ // TODO: Make for a less abrupt transition if we're not already at 100%.
+ ui->speed_slider->setValue(100); // Also actually sets the master speed and updates the label.
+ ui->speed_slider->setEnabled(!ui->speed_lock_btn->isChecked());
+ midi_mapper.set_locked(MIDIMapper::LightState(ui->speed_lock_btn->isChecked()));
+ lock_blink_timeout->stop();
+}
+
+void MainWindow::preview_player_done()
+{
+ preview_playing = false;
+ enable_or_disable_preview_button();
}
void MainWindow::live_player_done()
{
- playlist_selection_changed();
playlist_clips->set_progress({});
ui->stop_btn->setEnabled(false);
+ playlist_selection_changed();
}
void MainWindow::live_player_clip_progress(const map<uint64_t, double> &progress, double time_remaining)
ui->preview_display->setMinimumWidth(ui->preview_display->height() * 16 / 9);
}
-void set_pts_in(int64_t pts, int64_t current_pts, ClipProxy &clip)
-{
- pts = std::max<int64_t>(pts, 0);
- if (clip->pts_out == -1) {
- pts = std::min(pts, current_pts);
- } else {
- pts = std::min(pts, clip->pts_out);
- }
- clip->pts_in = pts;
-}
-
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
constexpr int dead_zone_pixels = 3; // To avoid that simple clicks get misinterpreted.
- constexpr int camera_degrees_per_pixel = 15; // One click of most mice.
int scrub_sensitivity = 100; // pts units per pixel.
int wheel_sensitivity = 100; // pts units per degree.
if (event->type() == QEvent::FocusIn || event->type() == QEvent::FocusOut) {
enable_or_disable_preview_button();
+ hidden_jog_column = -1;
}
unsigned stream_idx = ui->preview_display->get_stream_idx();
}
QTableView *destination;
- int in_column, out_column, camera_column;
+ JogDestination jog_destination;
if (watched == ui->clip_list->viewport()) {
destination = ui->clip_list;
- in_column = int(ClipList::Column::IN);
- out_column = int(ClipList::Column::OUT);
- camera_column = -1;
+ jog_destination = JOG_CLIP_LIST;
last_mousewheel_camera_row = -1;
} else if (watched == ui->playlist->viewport()) {
destination = ui->playlist;
- in_column = int(PlayList::Column::IN);
- out_column = int(PlayList::Column::OUT);
- camera_column = int(PlayList::Column::CAMERA);
+ jog_destination = JOG_PLAYLIST;
+ if (destination->columnAt(wheel->x()) != int(PlayList::Column::CAMERA)) {
+ last_mousewheel_camera_row = -1;
+ }
} else {
last_mousewheel_camera_row = -1;
return false;
}
+
int column = destination->columnAt(wheel->x());
int row = destination->rowAt(wheel->y());
if (column == -1 || row == -1)
return false;
}
- currently_deferring_model_changes = true;
- {
- current_change_id = (watched == ui->clip_list->viewport()) ? "cliplist:" : "playlist:";
- ClipProxy clip = (watched == ui->clip_list->viewport()) ? cliplist_clips->mutable_clip(row) : playlist_clips->mutable_clip(row);
- if (watched == ui->playlist->viewport()) {
- stream_idx = clip->stream_idx;
- }
-
- if (column != camera_column) {
- last_mousewheel_camera_row = -1;
- }
- if (column == in_column) {
- current_change_id += "in:" + to_string(row);
- int64_t pts = clip->pts_in + angle_delta * wheel_sensitivity;
- set_pts_in(pts, current_pts, clip);
- preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
- } else if (column == out_column) {
- current_change_id += "out:" + to_string(row);
- int64_t pts = clip->pts_out + angle_delta * wheel_sensitivity;
- pts = std::max(pts, clip->pts_in);
- pts = std::min(pts, current_pts);
- clip->pts_out = pts;
- preview_single_frame(pts, stream_idx, LAST_BEFORE);
- } else if (column == camera_column) {
- current_change_id += "camera:" + to_string(row);
- int angle_degrees = angle_delta;
- if (last_mousewheel_camera_row == row) {
- angle_degrees += leftover_angle_degrees;
- }
-
- int stream_idx = clip->stream_idx + angle_degrees / camera_degrees_per_pixel;
- stream_idx = std::max(stream_idx, 0);
- stream_idx = std::min<int>(stream_idx, num_cameras - 1);
- clip->stream_idx = stream_idx;
-
- last_mousewheel_camera_row = row;
- leftover_angle_degrees = angle_degrees % camera_degrees_per_pixel;
-
- // Don't update the live view, that's rarely what the operator wants.
- }
- }
- currently_deferring_model_changes = false;
+ jog_internal(jog_destination, row, column, stream_idx, angle_delta * wheel_sensitivity);
return true; // Don't scroll.
} else if (event->type() == QEvent::MouseButtonRelease) {
scrubbing = false;
any_selected && selected->selectedRows().front().row() > 0);
ui->playlist_move_down_btn->setEnabled(
any_selected && selected->selectedRows().back().row() < int(playlist_clips->size()) - 1);
- ui->play_btn->setEnabled(!playlist_clips->empty());
+
+ ui->play_btn->setEnabled(any_selected);
+ if (ui->stop_btn->isEnabled()) { // Playing.
+ midi_mapper.set_play_enabled(MIDIMapper::On);
+ } else if (any_selected) {
+ midi_mapper.set_play_enabled(MIDIMapper::Blinking);
+ } else {
+ midi_mapper.set_play_enabled(MIDIMapper::Off);
+ }
if (!any_selected) {
set_output_status("paused");
}
}
-void MainWindow::clip_list_selection_changed(const QModelIndex ¤t, const QModelIndex &)
+void MainWindow::clip_list_selection_changed(const QModelIndex ¤t, const QModelIndex &previous)
{
int camera_selected = -1;
if (cliplist_clips->is_camera_column(current.column())) {
camera_selected = current.column() - int(ClipList::Column::CAMERA_1);
+
+ // See the comment on hidden_jog_column.
+ if (current.row() != previous.row()) {
+ hidden_jog_column = -1;
+ } else if (hidden_jog_column == -1) {
+ hidden_jog_column = previous.column();
+ }
+ } else {
+ hidden_jog_column = -1;
}
highlight_camera_input(camera_selected);
enable_or_disable_queue_button();
displays[i].frame->setStyleSheet("");
}
}
+ midi_mapper.highlight_camera_input(stream_idx);
}
void MainWindow::enable_or_disable_preview_button()
QItemSelectionModel *selected = ui->playlist->selectionModel();
if (selected->hasSelection()) {
ui->preview_btn->setEnabled(true);
+ midi_mapper.set_preview_enabled(preview_playing ? MIDIMapper::On : MIDIMapper::Blinking);
return;
}
}
// TODO: Perhaps only enable this if something is actually selected.
ui->preview_btn->setEnabled(!cliplist_clips->empty());
+ if (preview_playing) {
+ midi_mapper.set_preview_enabled(MIDIMapper::On);
+ } else {
+ midi_mapper.set_preview_enabled(cliplist_clips->empty() ? MIDIMapper::Off : MIDIMapper::Blinking);
+ }
}
void MainWindow::enable_or_disable_queue_button()
}
ui->queue_btn->setEnabled(enabled);
+ midi_mapper.set_queue_enabled(enabled);
}
void MainWindow::set_output_status(const string &status)
displays[stream_idx].display->setFrame(stream_idx, frame);
}
+void MainWindow::preview()
+{
+ post_to_main_thread([this] {
+ preview_clicked();
+ });
+}
+
+void MainWindow::queue()
+{
+ post_to_main_thread([this] {
+ queue_clicked();
+ });
+}
+
+void MainWindow::play()
+{
+ post_to_main_thread([this] {
+ play_clicked();
+ });
+}
+
+void MainWindow::toggle_lock()
+{
+ post_to_main_thread([this] {
+ ui->speed_lock_btn->setChecked(!ui->speed_lock_btn->isChecked());
+ speed_lock_clicked();
+ });
+}
+
+void MainWindow::jog(int delta)
+{
+ post_to_main_thread([this, delta] {
+ int64_t pts_delta = delta * (TIMEBASE / 60); // One click = frame at 60 fps.
+ if (ui->playlist->hasFocus()) {
+ QModelIndex selected = ui->playlist->selectionModel()->currentIndex();
+ if (selected.column() != -1 && selected.row() != -1) {
+ jog_internal(JOG_PLAYLIST, selected.row(), selected.column(), /*stream_idx=*/-1, pts_delta);
+ }
+ } else if (ui->clip_list->hasFocus()) {
+ QModelIndex selected = ui->clip_list->selectionModel()->currentIndex();
+ if (cliplist_clips->is_camera_column(selected.column()) &&
+ hidden_jog_column != -1) {
+ // See the definition on hidden_jog_column.
+ selected = selected.sibling(selected.row(), hidden_jog_column);
+ ui->clip_list->selectionModel()->setCurrentIndex(selected, QItemSelectionModel::ClearAndSelect);
+ hidden_jog_column = -1;
+ }
+ if (selected.column() != -1 && selected.row() != -1) {
+ jog_internal(JOG_CLIP_LIST, selected.row(), selected.column(), ui->preview_display->get_stream_idx(), pts_delta);
+ }
+ }
+ });
+}
+
+void MainWindow::switch_camera(unsigned camera_idx)
+{
+ post_to_main_thread([this, camera_idx] {
+ if (camera_idx < num_cameras) { // TODO: Also make this change a highlighted clip?
+ preview_angle_clicked(camera_idx);
+ }
+ });
+}
+
+void MainWindow::set_master_speed(float speed)
+{
+ speed = min(max(speed, 0.1f), 2.0f);
+
+ post_to_main_thread([this, speed] {
+ if (ui->speed_lock_btn->isChecked()) {
+ midi_mapper.set_locked(MIDIMapper::Blinking);
+ lock_blink_timeout->start(1000);
+ return;
+ }
+
+ int percent = lrintf(speed * 100.0f);
+ ui->speed_slider->blockSignals(true);
+ ui->speed_slider->setValue(percent);
+ ui->speed_slider->blockSignals(false);
+ ui->speed_lock_btn->setText(QString::fromStdString(" " + to_string(percent) + "%"));
+
+ live_player->set_master_speed(speed);
+ });
+}
+
+void MainWindow::cue_in()
+{
+ post_to_main_thread([this] { cue_in_clicked(); });
+}
+
+void MainWindow::cue_out()
+{
+ post_to_main_thread([this] { cue_out_clicked(); });
+}
+
template<class Model>
void MainWindow::replace_model(QTableView *view, Model **model, Model *new_model)
{