1 /*
2 * Copyright (c) 2015-2020 Meltytech, LLC
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "gdigrabwidget.h"
19 #include "ui_gdigrabwidget.h"
20 #include "mltcontroller.h"
21 #include "shotcut_mlt_properties.h"
22 #include "util.h"
23 #include "settings.h"
24 #include <QtWidgets>
25 #include <QAudioDeviceInfo>
26
GDIgrabWidget(QWidget * parent)27 GDIgrabWidget::GDIgrabWidget(QWidget *parent) :
28 QWidget(parent),
29 ui(new Ui::GDIgrabWidget)
30 {
31 ui->setupUi(this);
32 Util::setColorsToHighlight(ui->label_9);
33 ui->applyButton->hide();
34 const QRect& r = QApplication::desktop()->screenGeometry();
35 ui->widthSpinBox->setValue(r.size().width());
36 ui->heightSpinBox->setValue(r.size().height());
37 ui->preset->saveDefaultPreset(getPreset());
38 ui->preset->loadPresets();
39
40 if (QAudioDeviceInfo::availableDevices(QAudio::AudioInput).count() > 0) {
41 foreach (const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::availableDevices(QAudio::AudioInput))
42 ui->audioComboBox->addItem(deviceInfo.deviceName());
43 } else {
44 ui->audioLabel->hide();
45 ui->audioComboBox->hide();
46 }
47 }
48
~GDIgrabWidget()49 GDIgrabWidget::~GDIgrabWidget()
50 {
51 delete ui;
52 }
53
URL(Mlt::Profile & profile) const54 QString GDIgrabWidget::URL(Mlt::Profile& profile) const
55 {
56 if (!profile.is_explicit()) {
57 profile.set_width(ui->widthSpinBox->value());
58 profile.set_height(ui->heightSpinBox->value());
59 profile.set_sample_aspect(1, 1);
60 profile.set_progressive(1);
61 profile.set_colorspace(709);
62 profile.set_frame_rate(25, 1);
63 MLT.updatePreviewProfile();
64 MLT.setPreviewScale(Settings.playerPreviewScale());
65 }
66 QString s = QString("gdigrab:desktop?offset_x=%1&offset_y=%2&video_size=%3x%4&framerate=%5&show_region=%6&draw_mouse=%7")
67 .arg(ui->xSpinBox->value())
68 .arg(ui->ySpinBox->value())
69 .arg(ui->widthSpinBox->value())
70 .arg(ui->heightSpinBox->value())
71 .arg(profile.fps())
72 .arg(ui->showRegionCheckBox->isChecked()? 1: 0)
73 .arg(ui->drawMouseCheckBox->isChecked()? 1 : 0);
74 return s;
75 }
76
newProducer(Mlt::Profile & profile)77 Mlt::Producer* GDIgrabWidget::newProducer(Mlt::Profile& profile)
78 {
79 Mlt::Producer* p = new Mlt::Producer(profile, URL(profile).toLatin1().constData());
80 if (!p->is_valid()) {
81 delete p;
82 p = new Mlt::Producer(profile, "color:");
83 p->set("error", 1);
84 }
85 else if (ui->audioComboBox->currentIndex() > 0) {
86 Mlt::Producer* audio = new Mlt::Producer(profile,
87 QString("dshow:audio=%1").arg(ui->audioComboBox->currentText())
88 .toLatin1().constData());
89 Mlt::Tractor* tractor = new Mlt::Tractor;
90 tractor->set("_profile", profile.get_profile(), 0);
91 tractor->set_track(*p, 0);
92 delete p;
93 tractor->set_track(*audio, 1);
94 delete audio;
95 p = new Mlt::Producer(tractor->get_producer());
96 delete tractor;
97 }
98 p->set("xpos", ui->xSpinBox->value());
99 p->set("ypos", ui->ySpinBox->value());
100 p->set("width", ui->widthSpinBox->value());
101 p->set("height", ui->heightSpinBox->value());
102 p->set("show_region", ui->showRegionCheckBox->isChecked()? 1: 0);
103 p->set("draw_mouse", ui->drawMouseCheckBox->isChecked()? 1: 0);
104 p->set("audio_ix", ui->audioComboBox->currentIndex());
105 p->set(kBackgroundCaptureProperty, 1);
106 p->set("force_seekable", 0);
107 return p;
108 }
109
getPreset() const110 Mlt::Properties GDIgrabWidget::getPreset() const
111 {
112 Mlt::Properties p;
113 p.set("xpos", ui->xSpinBox->value());
114 p.set("ypos", ui->ySpinBox->value());
115 p.set("width", ui->widthSpinBox->value());
116 p.set("height", ui->heightSpinBox->value());
117 p.set("show_region", ui->showRegionCheckBox->isChecked()? 1: 0);
118 p.set("draw_mouse", ui->drawMouseCheckBox->isChecked()? 1: 0);
119 p.set("audio_ix", ui->audioComboBox->currentIndex());
120 p.set(kBackgroundCaptureProperty, 1);
121 return p;
122 }
123
loadPreset(Mlt::Properties & p)124 void GDIgrabWidget::loadPreset(Mlt::Properties& p)
125 {
126 ui->xSpinBox->setValue(p.get_int("xpos"));
127 ui->ySpinBox->setValue(p.get_int("ypos"));
128 ui->widthSpinBox->setValue(p.get_int("width"));
129 ui->heightSpinBox->setValue(p.get_int("height"));
130 ui->showRegionCheckBox->setChecked(p.get_int("show_region"));
131 ui->drawMouseCheckBox->setChecked(p.get_int("draw_mouse"));
132 ui->audioComboBox->setCurrentIndex(p.get_int("audio_ix"));
133 }
134
on_preset_selected(void * p)135 void GDIgrabWidget::on_preset_selected(void* p)
136 {
137 Mlt::Properties* properties = (Mlt::Properties*) p;
138 loadPreset(*properties);
139 delete properties;
140 }
141
on_preset_saveClicked()142 void GDIgrabWidget::on_preset_saveClicked()
143 {
144 ui->preset->savePreset(getPreset());
145 }
146
setProducer(Mlt::Producer * producer)147 void GDIgrabWidget::setProducer(Mlt::Producer* producer)
148 {
149 ui->applyButton->show();
150 if (producer)
151 loadPreset(*producer);
152 }
153
on_applyButton_clicked()154 void GDIgrabWidget::on_applyButton_clicked()
155 {
156 MLT.setProducer(newProducer(MLT.profile()));
157 MLT.play();
158 }
159