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());
94 QString videoDriver = KdenliveSettings::videodrivername();
95 if (!videoDriver.isEmpty()) {
96 if (videoDriver == "x11_noaccel") {
97 env << "SDL_VIDEO_YUV_HWACCEL=0";
98 env << "SDL_VIDEODRIVER=x11";
99 } else env << "SDL_VIDEODRIVER=" + videoDriver;
101 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 1);
103 displayProcess->setEnvironment(env);
105 if (KdenliveSettings::video4capture().isEmpty()) {
106 QString captureCommand;
107 if (!KdenliveSettings::video4adevice().isEmpty()) captureCommand = "-f " + KdenliveSettings::video4aformat() + " -i " + KdenliveSettings::video4adevice();
109 captureCommand += " -f " + KdenliveSettings::video4vformat() + " -s " + KdenliveSettings::video4size() + " -r " + QString::number(KdenliveSettings::video4rate()) + " -i " + KdenliveSettings::video4vdevice() + " -f " + KdenliveSettings::video4vencoding();
110 KdenliveSettings::setVideo4capture(captureCommand);
113 if (KdenliveSettings::video4playback().isEmpty()) {
114 QString playbackCommand;
115 playbackCommand = "-f " + KdenliveSettings::video4vencoding();
116 KdenliveSettings::setVideo4playback(playbackCommand);
118 kDebug() << "/////// BUILDING MONITOR, ID: " << ui.video_frame->winId();
121 QString RecMonitor::name() const {
125 void RecMonitor::slotConfigure() {
126 emit showConfigDialog(4, ui.device_selector->currentIndex());
129 void RecMonitor::slotVideoDeviceChanged(int ix) {
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(false);
138 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())));
139 //ui.video_frame->setText(i18n("Press record button\nto start screen capture"));
142 m_discAction->setEnabled(false);
143 m_rewAction->setEnabled(false);
144 m_fwdAction->setEnabled(false);
145 m_recAction->setEnabled(true);
146 m_stopAction->setEnabled(false);
147 m_playAction->setEnabled(true);
148 checkDeviceAvailability();
151 m_discAction->setEnabled(true);
152 m_recAction->setEnabled(false);
153 m_stopAction->setEnabled(false);
154 m_playAction->setEnabled(false);
155 m_rewAction->setEnabled(false);
156 m_fwdAction->setEnabled(false);
157 //ui.video_frame->setText(i18n("Plug your camcorder and\npress connect button\nto initialize connection"));
158 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())));
163 QPixmap RecMonitor::mergeSideBySide(const QPixmap& pix, const QString txt) {
165 QRect r = p.fontMetrics().boundingRect(QRect(0, 0, ui.video_frame->width(), ui.video_frame->height()), Qt::AlignLeft, txt);
166 int strWidth = r.width();
167 int strHeight = r.height();
168 int pixWidth = pix.width();
169 int pixHeight = pix.height();
170 QPixmap res(strWidth + 8 + pixWidth, qMax(strHeight, pixHeight));
171 res.fill(Qt::transparent);
173 p.drawPixmap(0, 0, pix);
174 p.drawText(QRect(pixWidth + 8, 0, strWidth, strHeight), 0, txt);
180 void RecMonitor::checkDeviceAvailability() {
181 if (!KIO::NetAccess::exists(KUrl(KdenliveSettings::video4vdevice()), KIO::NetAccess::SourceSide , this)) {
182 m_playAction->setEnabled(false);
183 m_recAction->setEnabled(false);
184 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())));
185 //ui.video_frame->setText(i18n("Cannot read from device %1\nPlease check drivers and access rights.", KdenliveSettings::video4vdevice()));
186 } else //ui.video_frame->setText(i18n("Press play or record button\nto start video capture"));
187 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())));
190 void RecMonitor::slotDisconnect() {
191 if (captureProcess->state() == QProcess::NotRunning) {
192 m_captureTime = KDateTime::currentLocalDateTime();
193 kDebug() << "CURRENT TIME: " << m_captureTime.toString();
194 m_didCapture = false;
195 slotStartCapture(false);
196 m_discAction->setIcon(KIcon("network-disconnect"));
197 m_discAction->setText(i18n("Disonnect"));
198 m_recAction->setEnabled(true);
199 m_stopAction->setEnabled(true);
200 m_playAction->setEnabled(true);
201 m_rewAction->setEnabled(true);
202 m_fwdAction->setEnabled(true);
204 captureProcess->write("q", 1);
205 QTimer::singleShot(1000, captureProcess, SLOT(kill()));
206 if (m_didCapture) manageCapturedFiles();
207 m_didCapture = false;
211 void RecMonitor::slotRewind() {
212 captureProcess->write("a", 1);
215 void RecMonitor::slotForward() {
216 captureProcess->write("z", 1);
219 void RecMonitor::slotStopCapture() {
221 switch (ui.device_selector->currentIndex()) {
223 captureProcess->write("\e", 2);
224 m_playAction->setIcon(m_playIcon);
228 captureProcess->write("q\n", 3);
229 QTimer::singleShot(1000, captureProcess, SLOT(kill()));
233 captureProcess->write("q\n", 3);
234 QTimer::singleShot(1000, captureProcess, SLOT(kill()));
241 void RecMonitor::slotStartCapture(bool play) {
244 *captureProcess<<"dvgrab";
248 switch (m_recPanel->capture_format->currentItem()){
250 *captureProcess<<"--format"<<"dv1";
253 *captureProcess<<"--format"<<"dv2";
256 *captureProcess<<"--format"<<"hdv";
260 *captureProcess<<"--format"<<"raw";
264 if (KdenliveSettings::autosplit()) *captureProcess<<"--autosplit";
265 if (KdenliveSettings::timestamp()) *captureProcess<<"--timestamp";
266 *captureProcess<<"-i"<<"capture"<<"-";*/
269 QStringList captureArgs;
270 captureArgs<<"--format"<<"hdv"<<"-i"<<"capture"<<"-";
271 QStringList displayArgs;
273 displayArgs<<"-f"<<"mpegts"<<"-x"<<QString::number(ui.video_frame->width())<<"-y"<<QString::number(ui.video_frame->height())<<"-";
275 captureProcess->setStandardOutputProcess(displayProcess);
276 ui.video_frame->setScaledContents(false);
277 captureProcess->start("dvgrab",captureArgs);
278 displayProcess->start("ffplay", displayArgs);*/
281 if (captureProcess->state() != QProcess::NotRunning) {
282 if (ui.device_selector->currentIndex() == FIREWIRE) {
284 captureProcess->write("k", 1);
285 //captureProcess->write("\e", 2);
286 m_playAction->setIcon(m_playIcon);
289 captureProcess->write("p", 1);
290 m_playAction->setIcon(m_pauseIcon);
296 m_captureArgs.clear();
297 m_displayArgs.clear();
300 switch (ui.device_selector->currentIndex()) {
302 switch (KdenliveSettings::firewireformat()) {
305 m_captureArgs << "--format" << "raw";
306 m_displayArgs << "-f" << "dv";
310 m_captureArgs << "--format" << "dv1";
311 m_displayArgs << "-f" << "dv";
315 m_captureArgs << "--format" << "dv2";
316 m_displayArgs << "-f" << "dv";
320 m_captureArgs << "--format" << "hdv";
321 m_displayArgs << "-f" << "mpegts";
324 if (KdenliveSettings::firewireautosplit()) m_captureArgs << "--autosplit";
325 if (KdenliveSettings::firewiretimestamp()) m_captureArgs << "--timestamp";
326 m_captureArgs << "-i" << "capture" << "-";
327 m_displayArgs << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
329 captureProcess->setStandardOutputProcess(displayProcess);
330 captureProcess->setWorkingDirectory(KdenliveSettings::capturefolder());
331 captureProcess->start("dvgrab", m_captureArgs);
332 if (play) captureProcess->write(" ", 1);
333 m_discAction->setEnabled(true);
336 m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << "-";
337 m_displayArgs << KdenliveSettings::video4playback().simplified().split(' ') << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
338 captureProcess->setStandardOutputProcess(displayProcess);
339 captureProcess->start("ffmpeg", m_captureArgs);
345 if (ui.device_selector->currentIndex() != SCREENGRAB) {
346 displayProcess->start("ffplay", m_displayArgs);
347 ui.video_frame->setText(i18n("Initialising..."));
349 // do something when starting screen grab
353 void RecMonitor::slotRecord() {
354 if (captureProcess->state() == QProcess::NotRunning && ui.device_selector->currentIndex() == FIREWIRE) {
358 switch (ui.device_selector->currentIndex()) {
360 captureProcess->write("\e", 2);
361 m_playAction->setIcon(m_playIcon);
362 m_isCapturing = false;
364 m_recAction->setChecked(false);
368 //m_isCapturing = false;
369 QTimer::singleShot(1000, this, SLOT(slotStartCapture()));
372 captureProcess->write("q\n", 3);
373 // in case ffmpeg doesn't exit with the 'q' command, kill it one second later
374 QTimer::singleShot(1000, captureProcess, SLOT(kill()));
378 } else if (ui.device_selector->currentIndex() == FIREWIRE) {
379 m_isCapturing = true;
381 captureProcess->write("c\n", 3);
384 if (captureProcess->state() == QProcess::NotRunning) {
385 m_recAction->setChecked(true);
386 QString extension = "mpg";
387 if (ui.device_selector->currentIndex() == SCREENGRAB) extension = KdenliveSettings::screengrabextension();
388 QString path = KdenliveSettings::capturefolder() + "/capture0000." + extension;
390 while (QFile::exists(path)) {
391 QString num = QString::number(i).rightJustified(4, '0', false);
392 path = KdenliveSettings::capturefolder() + "/capture" + num + "." + extension;
396 m_captureFile = KUrl(path);
398 m_captureArgs.clear();
399 m_displayArgs.clear();
402 switch (ui.device_selector->currentIndex()) {
404 m_captureArgs << "--format" << "hdv" << "-i" << "capture" << "-";
405 m_displayArgs << "-f" << "mpegts" << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
406 captureProcess->setStandardOutputProcess(displayProcess);
407 captureProcess->start("dvgrab", m_captureArgs);
410 m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << "-y" << m_captureFile.path() << "-f" << KdenliveSettings::video4vencoding() << "-";
411 m_displayArgs << KdenliveSettings::video4playback().simplified().split(' ') << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
412 captureProcess->setStandardOutputProcess(displayProcess);
413 captureProcess->start("ffmpeg", m_captureArgs);
416 if (KdenliveSettings::fullscreengrab()) {
417 const QRect rect = QApplication::desktop()->screenGeometry();
418 args = KdenliveSettings::screengrabcapture().replace("%size", QString::number(rect.width()) + "x" + QString::number(rect.height())).replace("%offset", QString());
419 kDebug() << "// capture params: " << args;
420 if (KdenliveSettings::screengrabenableaudio()) {
421 // also capture audio
422 m_captureArgs << KdenliveSettings::screengrabaudiocapture().simplified().split(' ');
424 m_captureArgs << args.simplified().split(' ') << KdenliveSettings::screengrabencoding().simplified().split(' ') << m_captureFile.path();
425 ui.video_frame->setText(i18n("Capturing..."));
426 m_isCapturing = true;
427 captureProcess->start("ffmpeg", m_captureArgs);
429 ui.video_frame->setText(i18n("Select region..."));
430 rgnGrab = new RegionGrabber();
431 connect(rgnGrab, SIGNAL(regionGrabbed(const QRect&)), SLOT(slotStartGrab(const QRect &)));
438 //ui.video_frame->setScaledContents(false);
439 if (ui.device_selector->currentIndex() != SCREENGRAB) {
440 m_isCapturing = true;
441 displayProcess->start("ffplay", m_displayArgs);
442 ui.video_frame->setText(i18n("Initialising..."));
446 displayProcess->kill();
447 captureProcess->kill();
448 QTimer::singleShot(1000, this, SLOT(slotRecord()));
452 void RecMonitor::slotStartGrab(const QRect &rect) {
453 rgnGrab->deleteLater();
454 QApplication::restoreOverrideCursor();
456 if (rect.isNull()) return;
457 int width = rect.width();
458 int height = rect.height();
459 if (width % 2 != 0) width--;
460 if (height % 2 != 0) height--;
461 QString args = KdenliveSettings::screengrabcapture().replace("%size", QString::number(width) + "x" + QString::number(height)).replace("%offset", "+" + QString::number(rect.x()) + "," + QString::number(rect.y()));
462 kDebug() << "// capture params: " << args;
463 if (KdenliveSettings::screengrabenableaudio()) {
464 // also capture audio
465 m_captureArgs << KdenliveSettings::screengrabaudiocapture().simplified().split(' ');
467 m_captureArgs << args.simplified().split(' ') << KdenliveSettings::screengrabencoding().simplified().split(' ') << m_captureFile.path();
468 m_isCapturing = true;
469 ui.video_frame->setText(i18n("Capturing..."));
470 captureProcess->start("ffmpeg", m_captureArgs);
473 void RecMonitor::slotProcessStatus(QProcess::ProcessState status) {
474 if (status == QProcess::NotRunning) {
475 displayProcess->kill();
476 if (m_isCapturing && ui.device_selector->currentIndex() != FIREWIRE)
477 if (ui.autoaddbox->isChecked() && QFile::exists(m_captureFile.path())) emit addProjectClip(m_captureFile);
478 if (ui.device_selector->currentIndex() == FIREWIRE) {
479 m_discAction->setIcon(KIcon("network-connect"));
480 m_discAction->setText(i18n("Connect"));
481 m_playAction->setEnabled(false);
482 m_rewAction->setEnabled(false);
483 m_fwdAction->setEnabled(false);
484 m_recAction->setEnabled(false);
487 m_playAction->setIcon(m_playIcon);
488 m_recAction->setChecked(false);
489 m_stopAction->setEnabled(false);
490 ui.device_selector->setEnabled(true);
491 if (captureProcess && captureProcess->exitStatus() == QProcess::CrashExit) {
492 ui.video_frame->setText(i18n("Capture crashed, please check your parameters"));
494 ui.video_frame->setText(i18n("Not connected"));
496 m_isCapturing = false;
498 if (ui.device_selector->currentIndex() != SCREENGRAB) m_stopAction->setEnabled(true);
499 ui.device_selector->setEnabled(false);
503 void RecMonitor::manageCapturedFiles() {
505 switch (KdenliveSettings::firewireformat()) {
517 QDir dir(KdenliveSettings::capturefolder());
519 filters << "capture*" + extension;
520 const QStringList result = dir.entryList(filters, QDir::Files, QDir::Time);
521 KUrl::List capturedFiles;
522 foreach(QString name, result) {
523 KUrl url = KUrl(dir.filePath(name));
524 if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, this)) {
525 KFileItem file(KFileItem::Unknown, KFileItem::Unknown, url, true);
526 if (file.time(KFileItem::ModificationTime) > m_captureTime) capturedFiles.append(url);
529 kDebug() << "Found : " << capturedFiles.count() << " new capture files";
530 kDebug() << capturedFiles;
532 if (capturedFiles.count() > 0) {
533 ManageCapturesDialog *d = new ManageCapturesDialog(capturedFiles, this);
534 if (d->exec() == QDialog::Accepted) {
535 capturedFiles = d->importFiles();
536 foreach(KUrl url, capturedFiles) {
537 emit addProjectClip(url);
545 void RecMonitor::mousePressEvent(QMouseEvent * event) {
549 void RecMonitor::activateRecMonitor() {
550 //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
553 void RecMonitor::stop() {
558 void RecMonitor::start() {
563 void RecMonitor::refreshRecMonitor(bool visible) {
565 //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
570 void RecMonitor::slotPlay() {
572 //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
577 #include "recmonitor.moc"