1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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 2 of the License, or *
7 * (at your option) any later version. *
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. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
21 #include <QMouseEvent>
22 #include <QStylePainter>
24 #include <QToolButton>
27 #include <QDesktopWidget>
31 #include <KStandardDirs>
33 #include <KIO/NetAccess>
37 #include "kdenlivesettings.h"
38 #include "managecapturesdialog.h"
39 #include "recmonitor.h"
41 RecMonitor::RecMonitor(QString name, QWidget *parent)
42 : QWidget(parent), m_name(name), m_isActive(false), m_isCapturing(false), m_isPlaying(false), m_didCapture(false) {
45 ui.video_frame->setAttribute(Qt::WA_PaintOnScreen);
46 ui.device_selector->setCurrentIndex(KdenliveSettings::defaultcapture());
47 connect(ui.device_selector, SIGNAL(currentIndexChanged(int)), this, SLOT(slotVideoDeviceChanged(int)));
51 QToolBar *toolbar = new QToolBar(name, this);
52 QHBoxLayout *layout = new QHBoxLayout;
53 layout->setContentsMargins(0, 0, 0, 0);
54 m_playIcon = KIcon("media-playback-start");
55 m_pauseIcon = KIcon("media-playback-pause");
57 m_discAction = toolbar->addAction(KIcon("network-connect"), i18n("Connect"));
58 connect(m_discAction, SIGNAL(triggered()), this, SLOT(slotDisconnect()));
60 m_rewAction = toolbar->addAction(KIcon("media-seek-backward"), i18n("Rewind"));
61 connect(m_rewAction, SIGNAL(triggered()), this, SLOT(slotRewind()));
63 m_playAction = toolbar->addAction(m_playIcon, i18n("Play"));
64 connect(m_playAction, SIGNAL(triggered()), this, SLOT(slotStartCapture()));
66 m_stopAction = toolbar->addAction(KIcon("media-playback-stop"), i18n("Stop"));
67 connect(m_stopAction, SIGNAL(triggered()), this, SLOT(slotStopCapture()));
68 m_stopAction->setEnabled(false);
69 m_fwdAction = toolbar->addAction(KIcon("media-seek-forward"), i18n("Forward"));
70 connect(m_fwdAction, SIGNAL(triggered()), this, SLOT(slotForward()));
72 m_recAction = toolbar->addAction(KIcon("media-record"), i18n("Record"));
73 connect(m_recAction, SIGNAL(triggered()), this, SLOT(slotRecord()));
74 m_recAction->setCheckable(true);
76 toolbar->addSeparator();
78 QAction *configAction = toolbar->addAction(KIcon("configure"), i18n("Configure"));
79 connect(configAction, SIGNAL(triggered()), this, SLOT(slotConfigure()));
80 configAction->setCheckable(false);
82 layout->addWidget(toolbar);
83 ui.control_frame_firewire->setLayout(layout);
85 slotVideoDeviceChanged(ui.device_selector->currentIndex());
86 displayProcess = new QProcess;
87 captureProcess = new QProcess;
89 connect(captureProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(slotProcessStatus(QProcess::ProcessState)));
91 QStringList env = QProcess::systemEnvironment();
92 env << "SDL_WINDOWID=" + QString::number(ui.video_frame->winId());
93 displayProcess->setEnvironment(env);
95 if (KdenliveSettings::video4capture().isEmpty()) {
96 QString captureCommand;
97 if (!KdenliveSettings::video4adevice().isEmpty()) captureCommand = "-f " + KdenliveSettings::video4aformat() + " -i " + KdenliveSettings::video4adevice();
99 captureCommand += " -f " + KdenliveSettings::video4vformat() + " -s " + KdenliveSettings::video4size() + " -r " + QString::number(KdenliveSettings::video4rate()) + " -i " + KdenliveSettings::video4vdevice() + " -f " + KdenliveSettings::video4vencoding();
100 KdenliveSettings::setVideo4capture(captureCommand);
103 if (KdenliveSettings::video4playback().isEmpty()) {
104 QString playbackCommand;
105 playbackCommand = "-f " + KdenliveSettings::video4vencoding();
106 KdenliveSettings::setVideo4playback(playbackCommand);
108 kDebug() << "/////// BUILDING MONITOR, ID: " << ui.video_frame->winId();
111 QString RecMonitor::name() const {
115 void RecMonitor::slotConfigure() {
116 emit showConfigDialog(4, ui.device_selector->currentIndex());
119 void RecMonitor::slotVideoDeviceChanged(int ix) {
122 m_discAction->setEnabled(false);
123 m_rewAction->setEnabled(false);
124 m_fwdAction->setEnabled(false);
125 m_recAction->setEnabled(true);
126 m_stopAction->setEnabled(false);
127 m_playAction->setEnabled(false);
128 ui.video_frame->setPixmap(mergeSideBySide(KIcon("video-display").pixmap(QSize(50, 50)), i18n("Press record button\nto start screen capture\nFiles will be saved in:\n%1", KdenliveSettings::capturefolder())));
129 //ui.video_frame->setText(i18n("Press record button\nto start screen capture"));
132 m_discAction->setEnabled(false);
133 m_rewAction->setEnabled(false);
134 m_fwdAction->setEnabled(false);
135 m_recAction->setEnabled(true);
136 m_stopAction->setEnabled(false);
137 m_playAction->setEnabled(true);
138 checkDeviceAvailability();
141 m_discAction->setEnabled(true);
142 m_recAction->setEnabled(false);
143 m_stopAction->setEnabled(false);
144 m_playAction->setEnabled(false);
145 m_rewAction->setEnabled(false);
146 m_fwdAction->setEnabled(false);
147 //ui.video_frame->setText(i18n("Plug your camcorder and\npress connect button\nto initialize connection"));
148 ui.video_frame->setPixmap(mergeSideBySide(KIcon("network-connect").pixmap(QSize(50, 50)), i18n("Plug your camcorder and\npress connect button\nto initialize connection\nFiles will be saved in:\n%1", KdenliveSettings::capturefolder())));
153 QPixmap RecMonitor::mergeSideBySide(const QPixmap& pix, const QString txt) {
155 QRect r = p.fontMetrics().boundingRect(QRect(0, 0, ui.video_frame->width(), ui.video_frame->height()), Qt::AlignLeft, txt);
156 int strWidth = r.width();
157 int strHeight = r.height();
158 int pixWidth = pix.width();
159 int pixHeight = pix.height();
160 QPixmap res(strWidth + 8 + pixWidth, qMax(strHeight, pixHeight));
161 res.fill(Qt::transparent);
163 p.drawPixmap(0, 0, pix);
164 p.drawText(QRect(pixWidth + 8, 0, strWidth, strHeight), 0, txt);
170 void RecMonitor::checkDeviceAvailability() {
171 if (!KIO::NetAccess::exists(KUrl(KdenliveSettings::video4vdevice()), KIO::NetAccess::SourceSide , this)) {
172 m_playAction->setEnabled(false);
173 m_recAction->setEnabled(false);
174 ui.video_frame->setPixmap(mergeSideBySide(KIcon("camera-web").pixmap(QSize(50, 50)), i18n("Cannot read from device %1\nPlease check drivers and access rights.", KdenliveSettings::video4vdevice())));
175 //ui.video_frame->setText(i18n("Cannot read from device %1\nPlease check drivers and access rights.", KdenliveSettings::video4vdevice()));
176 } else //ui.video_frame->setText(i18n("Press play or record button\nto start video capture"));
177 ui.video_frame->setPixmap(mergeSideBySide(KIcon("camera-web").pixmap(QSize(50, 50)), i18n("Press play or record button\nto start video capture\nFiles will be saved in:\n%1", KdenliveSettings::capturefolder())));
180 void RecMonitor::slotDisconnect() {
181 if (captureProcess->state() == QProcess::NotRunning) {
182 m_captureTime = KDateTime::currentLocalDateTime();
183 kDebug() << "CURRENT TIME: " << m_captureTime.toString();
184 m_didCapture = false;
185 slotStartCapture(false);
186 m_discAction->setIcon(KIcon("network-disconnect"));
187 m_discAction->setText(i18n("Disonnect"));
188 m_recAction->setEnabled(true);
189 m_stopAction->setEnabled(true);
190 m_playAction->setEnabled(true);
191 m_rewAction->setEnabled(true);
192 m_fwdAction->setEnabled(true);
194 captureProcess->write("q", 1);
195 QTimer::singleShot(1000, captureProcess, SLOT(kill()));
196 if (m_didCapture) manageCapturedFiles();
197 m_didCapture = false;
201 void RecMonitor::slotRewind() {
202 captureProcess->write("a", 1);
205 void RecMonitor::slotForward() {
206 captureProcess->write("z", 1);
209 void RecMonitor::slotStopCapture() {
211 switch (ui.device_selector->currentIndex()) {
213 captureProcess->write("\e", 2);
214 m_playAction->setIcon(m_playIcon);
218 captureProcess->write("q\n", 3);
219 QTimer::singleShot(1000, captureProcess, SLOT(kill()));
223 captureProcess->write("q\n", 3);
224 QTimer::singleShot(1000, captureProcess, SLOT(kill()));
231 void RecMonitor::slotStartCapture(bool play) {
234 *captureProcess<<"dvgrab";
238 switch (m_recPanel->capture_format->currentItem()){
240 *captureProcess<<"--format"<<"dv1";
243 *captureProcess<<"--format"<<"dv2";
246 *captureProcess<<"--format"<<"hdv";
250 *captureProcess<<"--format"<<"raw";
254 if (KdenliveSettings::autosplit()) *captureProcess<<"--autosplit";
255 if (KdenliveSettings::timestamp()) *captureProcess<<"--timestamp";
256 *captureProcess<<"-i"<<"capture"<<"-";*/
259 QStringList captureArgs;
260 captureArgs<<"--format"<<"hdv"<<"-i"<<"capture"<<"-";
261 QStringList displayArgs;
263 displayArgs<<"-f"<<"mpegts"<<"-x"<<QString::number(ui.video_frame->width())<<"-y"<<QString::number(ui.video_frame->height())<<"-";
265 captureProcess->setStandardOutputProcess(displayProcess);
266 ui.video_frame->setScaledContents(false);
267 captureProcess->start("dvgrab",captureArgs);
268 displayProcess->start("ffplay", displayArgs);*/
271 if (captureProcess->state() != QProcess::NotRunning) {
272 if (ui.device_selector->currentIndex() == FIREWIRE) {
274 captureProcess->write("k", 1);
275 //captureProcess->write("\e", 2);
276 m_playAction->setIcon(m_playIcon);
279 captureProcess->write("p", 1);
280 m_playAction->setIcon(m_pauseIcon);
286 m_captureArgs.clear();
287 m_displayArgs.clear();
290 switch (ui.device_selector->currentIndex()) {
292 switch (KdenliveSettings::firewireformat()) {
295 m_captureArgs << "--format" << "raw";
296 m_displayArgs << "-f" << "dv";
300 m_captureArgs << "--format" << "dv1";
301 m_displayArgs << "-f" << "dv";
305 m_captureArgs << "--format" << "dv2";
306 m_displayArgs << "-f" << "dv";
310 m_captureArgs << "--format" << "hdv";
311 m_displayArgs << "-f" << "mpegts";
314 if (KdenliveSettings::firewireautosplit()) m_captureArgs << "--autosplit";
315 if (KdenliveSettings::firewiretimestamp()) m_captureArgs << "--timestamp";
316 m_captureArgs << "-i" << "capture" << "-";
317 m_displayArgs << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
319 captureProcess->setStandardOutputProcess(displayProcess);
320 captureProcess->setWorkingDirectory(KdenliveSettings::capturefolder());
321 captureProcess->start("dvgrab", m_captureArgs);
322 if (play) captureProcess->write(" ", 1);
323 m_discAction->setEnabled(true);
326 m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << "-";
327 m_displayArgs << KdenliveSettings::video4playback().simplified().split(' ') << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
328 captureProcess->setStandardOutputProcess(displayProcess);
329 captureProcess->start("ffmpeg", m_captureArgs);
335 if (ui.device_selector->currentIndex() != SCREENGRAB) {
336 displayProcess->start("ffplay", m_displayArgs);
337 ui.video_frame->setText(i18n("Initialising..."));
339 // do something when starting screen grab
343 void RecMonitor::slotRecord() {
344 if (captureProcess->state() == QProcess::NotRunning && ui.device_selector->currentIndex() == FIREWIRE) {
348 switch (ui.device_selector->currentIndex()) {
350 captureProcess->write("\e", 2);
351 m_playAction->setIcon(m_playIcon);
352 m_isCapturing = false;
354 m_recAction->setChecked(false);
358 //m_isCapturing = false;
359 QTimer::singleShot(1000, this, SLOT(slotStartCapture()));
362 captureProcess->write("q\n", 3);
363 // in case ffmpeg doesn't exit with the 'q' command, kill it one second later
364 QTimer::singleShot(1000, captureProcess, SLOT(kill()));
368 } else if (ui.device_selector->currentIndex() == FIREWIRE) {
369 m_isCapturing = true;
371 captureProcess->write("c\n", 3);
374 if (captureProcess->state() == QProcess::NotRunning) {
375 m_recAction->setChecked(true);
376 QString extension = "mpg";
377 if (ui.device_selector->currentIndex() == SCREENGRAB) extension = KdenliveSettings::screengrabextension();
378 QString path = KdenliveSettings::capturefolder() + "/capture0000." + extension;
380 while (QFile::exists(path)) {
381 QString num = QString::number(i).rightJustified(4, '0', false);
382 path = KdenliveSettings::capturefolder() + "/capture" + num + "." + extension;
386 m_captureFile = KUrl(path);
388 m_captureArgs.clear();
389 m_displayArgs.clear();
392 switch (ui.device_selector->currentIndex()) {
394 m_captureArgs << "--format" << "hdv" << "-i" << "capture" << "-";
395 m_displayArgs << "-f" << "mpegts" << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
396 captureProcess->setStandardOutputProcess(displayProcess);
397 captureProcess->start("dvgrab", m_captureArgs);
400 m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << "-y" << m_captureFile.path() << "-f" << KdenliveSettings::video4vencoding() << "-";
401 m_displayArgs << KdenliveSettings::video4playback().simplified().split(' ') << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
402 captureProcess->setStandardOutputProcess(displayProcess);
403 captureProcess->start("ffmpeg", m_captureArgs);
406 if (KdenliveSettings::fullscreengrab()) {
407 const QRect rect = QApplication::desktop()->screenGeometry();
408 args = KdenliveSettings::screengrabcapture().replace("%size", QString::number(rect.width()) + "x" + QString::number(rect.height())).replace("%offset", QString());
409 kDebug() << "// capture params: " << args;
410 if (KdenliveSettings::screengrabenableaudio()) {
411 // also capture audio
412 m_captureArgs << KdenliveSettings::screengrabaudiocapture().simplified().split(' ');
414 m_captureArgs << args.simplified().split(' ') << KdenliveSettings::screengrabencoding().simplified().split(' ') << m_captureFile.path();
415 ui.video_frame->setText(i18n("Capturing..."));
416 m_isCapturing = true;
417 captureProcess->start("ffmpeg", m_captureArgs);
419 ui.video_frame->setText(i18n("Select region..."));
420 rgnGrab = new RegionGrabber();
421 connect(rgnGrab, SIGNAL(regionGrabbed(const QRect&)), SLOT(slotStartGrab(const QRect &)));
428 //ui.video_frame->setScaledContents(false);
429 if (ui.device_selector->currentIndex() != SCREENGRAB) {
430 m_isCapturing = true;
431 displayProcess->start("ffplay", m_displayArgs);
432 ui.video_frame->setText(i18n("Initialising..."));
436 displayProcess->kill();
437 captureProcess->kill();
438 QTimer::singleShot(1000, this, SLOT(slotRecord()));
442 void RecMonitor::slotStartGrab(const QRect &rect) {
443 rgnGrab->deleteLater();
444 QApplication::restoreOverrideCursor();
446 if (rect.isNull()) return;
447 int width = rect.width();
448 int height = rect.height();
449 if (width % 2 != 0) width--;
450 if (height % 2 != 0) height--;
451 QString args = KdenliveSettings::screengrabcapture().replace("%size", QString::number(width) + "x" + QString::number(height)).replace("%offset", "+" + QString::number(rect.x()) + "," + QString::number(rect.y()));
452 kDebug() << "// capture params: " << args;
453 if (KdenliveSettings::screengrabenableaudio()) {
454 // also capture audio
455 m_captureArgs << KdenliveSettings::screengrabaudiocapture().simplified().split(' ');
457 m_captureArgs << args.simplified().split(' ') << KdenliveSettings::screengrabencoding().simplified().split(' ') << m_captureFile.path();
458 m_isCapturing = true;
459 ui.video_frame->setText(i18n("Capturing..."));
460 captureProcess->start("ffmpeg", m_captureArgs);
463 void RecMonitor::slotProcessStatus(QProcess::ProcessState status) {
464 if (status == QProcess::NotRunning) {
465 displayProcess->kill();
466 if (m_isCapturing && ui.device_selector->currentIndex() != FIREWIRE)
467 if (ui.autoaddbox->isChecked() && QFile::exists(m_captureFile.path())) emit addProjectClip(m_captureFile);
468 if (ui.device_selector->currentIndex() == FIREWIRE) {
469 m_discAction->setIcon(KIcon("network-connect"));
470 m_discAction->setText(i18n("Connect"));
471 m_playAction->setEnabled(false);
472 m_rewAction->setEnabled(false);
473 m_fwdAction->setEnabled(false);
474 m_recAction->setEnabled(false);
477 m_playAction->setIcon(m_playIcon);
478 m_recAction->setChecked(false);
479 m_stopAction->setEnabled(false);
480 ui.device_selector->setEnabled(true);
481 if (captureProcess && captureProcess->exitStatus() == QProcess::CrashExit) {
482 ui.video_frame->setText(i18n("Capture crashed, please check your parameters"));
484 ui.video_frame->setText(i18n("Not connected"));
486 m_isCapturing = false;
488 if (ui.device_selector->currentIndex() != SCREENGRAB) m_stopAction->setEnabled(true);
489 ui.device_selector->setEnabled(false);
493 void RecMonitor::manageCapturedFiles() {
495 switch (KdenliveSettings::firewireformat()) {
507 QDir dir(KdenliveSettings::capturefolder());
509 filters << "capture*" + extension;
510 QStringList result = dir.entryList(filters, QDir::Files, QDir::Time);
511 KUrl::List capturedFiles;
512 foreach(QString name, result) {
513 KUrl url = KUrl(dir.filePath(name));
514 if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, this)) {
515 KFileItem file(KFileItem::Unknown, KFileItem::Unknown, url, true);
516 if (file.time(KFileItem::ModificationTime) > m_captureTime) capturedFiles.append(url);
519 kDebug() << "Found : " << capturedFiles.count() << " new capture files";
520 kDebug() << capturedFiles;
522 if (capturedFiles.count() > 0) {
523 ManageCapturesDialog *d = new ManageCapturesDialog(capturedFiles, this);
524 if (d->exec() == QDialog::Accepted) {
525 capturedFiles = d->importFiles();
526 foreach(KUrl url, capturedFiles) {
527 emit addProjectClip(url);
535 void RecMonitor::mousePressEvent(QMouseEvent * event) {
539 void RecMonitor::activateRecMonitor() {
540 //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
543 void RecMonitor::stop() {
548 void RecMonitor::start() {
553 void RecMonitor::refreshRecMonitor(bool visible) {
555 //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
560 void RecMonitor::slotPlay() {
562 //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
567 #include "recmonitor.moc"