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), m_dvgrabVersion(0) {
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;
88 alsaProcess = new QProcess;
90 connect(captureProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(slotProcessStatus(QProcess::ProcessState)));
92 QStringList env = QProcess::systemEnvironment();
93 env << "SDL_WINDOWID=" + QString::number(ui.video_frame->winId());
95 QString videoDriver = KdenliveSettings::videodrivername();
96 if (!videoDriver.isEmpty()) {
97 if (videoDriver == "x11_noaccel") {
98 env << "SDL_VIDEO_YUV_HWACCEL=0";
99 env << "SDL_VIDEODRIVER=x11";
100 } else env << "SDL_VIDEODRIVER=" + videoDriver;
102 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 1);
104 displayProcess->setEnvironment(env);
106 if (KdenliveSettings::video4capture().isEmpty()) {
107 QString captureCommand;
108 if (!KdenliveSettings::video4adevice().isEmpty()) captureCommand = "-f " + KdenliveSettings::video4aformat() + " -i " + KdenliveSettings::video4adevice();
110 captureCommand += " -f " + KdenliveSettings::video4vformat() + " -s " + KdenliveSettings::video4size() + " -r " + QString::number(KdenliveSettings::video4rate()) + " -i " + KdenliveSettings::video4vdevice() + " -f " + KdenliveSettings::video4vencoding();
111 KdenliveSettings::setVideo4capture(captureCommand);
114 if (KdenliveSettings::video4playback().isEmpty()) {
115 QString playbackCommand;
116 playbackCommand = "-f " + KdenliveSettings::video4vencoding();
117 KdenliveSettings::setVideo4playback(playbackCommand);
120 //HACK: check dvgrab version, because only dvgrab >= 3.3 supports
121 // --timestamp option without bug
122 QProcess *versionCheck = new QProcess;
123 versionCheck->setProcessChannelMode(QProcess::MergedChannels);
124 versionCheck->start("dvgrab", QStringList() << "--version");
125 if (versionCheck->waitForFinished()) {
126 QString version = QString(versionCheck->readAll()).simplified();
127 if (version.contains(' ')) version = version.section(' ', -1);
128 m_dvgrabVersion = version.toDouble();
129 kDebug() << "// FOUND DVGRAB VERSION: " << m_dvgrabVersion;
131 if (versionCheck) delete versionCheck;
133 kDebug() << "/////// BUILDING MONITOR, ID: " << ui.video_frame->winId();
136 RecMonitor::~RecMonitor() {
137 delete captureProcess;
138 delete displayProcess;
142 QString RecMonitor::name() const {
146 void RecMonitor::slotConfigure() {
147 emit showConfigDialog(4, ui.device_selector->currentIndex());
150 void RecMonitor::slotVideoDeviceChanged(int ix) {
153 m_discAction->setEnabled(false);
154 m_rewAction->setEnabled(false);
155 m_fwdAction->setEnabled(false);
156 m_recAction->setEnabled(true);
157 m_stopAction->setEnabled(false);
158 m_playAction->setEnabled(false);
159 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())));
160 //ui.video_frame->setText(i18n("Press record button\nto start screen capture"));
163 m_discAction->setEnabled(false);
164 m_rewAction->setEnabled(false);
165 m_fwdAction->setEnabled(false);
166 m_recAction->setEnabled(true);
167 m_stopAction->setEnabled(false);
168 m_playAction->setEnabled(true);
169 checkDeviceAvailability();
172 m_discAction->setEnabled(true);
173 m_recAction->setEnabled(false);
174 m_stopAction->setEnabled(false);
175 m_playAction->setEnabled(false);
176 m_rewAction->setEnabled(false);
177 m_fwdAction->setEnabled(false);
178 //ui.video_frame->setText(i18n("Plug your camcorder and\npress connect button\nto initialize connection"));
179 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())));
184 QPixmap RecMonitor::mergeSideBySide(const QPixmap& pix, const QString txt) {
186 QRect r = p.fontMetrics().boundingRect(QRect(0, 0, ui.video_frame->width(), ui.video_frame->height()), Qt::AlignLeft, txt);
187 int strWidth = r.width();
188 int strHeight = r.height();
189 int pixWidth = pix.width();
190 int pixHeight = pix.height();
191 QPixmap res(strWidth + 8 + pixWidth, qMax(strHeight, pixHeight));
192 res.fill(Qt::transparent);
194 p.drawPixmap(0, 0, pix);
195 p.drawText(QRect(pixWidth + 8, 0, strWidth, strHeight), 0, txt);
201 void RecMonitor::checkDeviceAvailability() {
202 if (!KIO::NetAccess::exists(KUrl(KdenliveSettings::video4vdevice()), KIO::NetAccess::SourceSide , this)) {
203 m_playAction->setEnabled(false);
204 m_recAction->setEnabled(false);
205 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())));
206 //ui.video_frame->setText(i18n("Cannot read from device %1\nPlease check drivers and access rights.", KdenliveSettings::video4vdevice()));
207 } else //ui.video_frame->setText(i18n("Press play or record button\nto start video capture"));
208 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())));
211 void RecMonitor::slotDisconnect() {
212 if (captureProcess->state() == QProcess::NotRunning) {
213 m_captureTime = KDateTime::currentLocalDateTime();
214 kDebug() << "CURRENT TIME: " << m_captureTime.toString();
215 m_didCapture = false;
216 slotStartCapture(false);
217 m_discAction->setIcon(KIcon("network-disconnect"));
218 m_discAction->setText(i18n("Disonnect"));
219 m_recAction->setEnabled(true);
220 m_stopAction->setEnabled(true);
221 m_playAction->setEnabled(true);
222 m_rewAction->setEnabled(true);
223 m_fwdAction->setEnabled(true);
225 captureProcess->write("q", 1);
226 QTimer::singleShot(1000, captureProcess, SLOT(kill()));
227 if (m_didCapture) manageCapturedFiles();
228 m_didCapture = false;
232 void RecMonitor::slotRewind() {
233 captureProcess->write("a", 1);
236 void RecMonitor::slotForward() {
237 captureProcess->write("z", 1);
240 void RecMonitor::slotStopCapture() {
242 switch (ui.device_selector->currentIndex()) {
244 captureProcess->write("\e", 2);
245 m_playAction->setIcon(m_playIcon);
249 captureProcess->write("q\n", 3);
250 QTimer::singleShot(1000, captureProcess, SLOT(kill()));
254 captureProcess->write("q\n", 3);
255 QTimer::singleShot(1000, captureProcess, SLOT(kill()));
262 void RecMonitor::slotStartCapture(bool play) {
265 *captureProcess<<"dvgrab";
269 switch (m_recPanel->capture_format->currentItem()){
271 *captureProcess<<"--format"<<"dv1";
274 *captureProcess<<"--format"<<"dv2";
277 *captureProcess<<"--format"<<"hdv";
281 *captureProcess<<"--format"<<"raw";
285 if (KdenliveSettings::autosplit()) *captureProcess<<"--autosplit";
286 if (KdenliveSettings::timestamp()) *captureProcess<<"--timestamp";
287 *captureProcess<<"-i"<<"capture"<<"-";*/
290 QStringList captureArgs;
291 captureArgs<<"--format"<<"hdv"<<"-i"<<"capture"<<"-";
292 QStringList displayArgs;
294 displayArgs<<"-f"<<"mpegts"<<"-x"<<QString::number(ui.video_frame->width())<<"-y"<<QString::number(ui.video_frame->height())<<"-";
296 captureProcess->setStandardOutputProcess(displayProcess);
297 ui.video_frame->setScaledContents(false);
298 captureProcess->start("dvgrab",captureArgs);
299 displayProcess->start("ffplay", displayArgs);*/
302 if (captureProcess->state() != QProcess::NotRunning) {
303 if (ui.device_selector->currentIndex() == FIREWIRE) {
305 captureProcess->write("k", 1);
306 //captureProcess->write("\e", 2);
307 m_playAction->setIcon(m_playIcon);
310 captureProcess->write("p", 1);
311 m_playAction->setIcon(m_pauseIcon);
317 m_captureArgs.clear();
318 m_displayArgs.clear();
321 switch (ui.device_selector->currentIndex()) {
323 switch (KdenliveSettings::firewireformat()) {
326 m_captureArgs << "--format" << "raw";
327 m_displayArgs << "-f" << "dv";
331 m_captureArgs << "--format" << "dv1";
332 m_displayArgs << "-f" << "dv";
336 m_captureArgs << "--format" << "dv2";
337 m_displayArgs << "-f" << "dv";
341 m_captureArgs << "--format" << "hdv";
342 m_displayArgs << "-f" << "mpegts";
345 if (KdenliveSettings::firewireautosplit()) m_captureArgs << "--autosplit";
346 if (KdenliveSettings::firewiretimestamp() && m_dvgrabVersion >= 3.3) m_captureArgs << "--timestamp";
347 m_captureArgs << "-i" << "capture" << "-";
348 m_displayArgs << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
350 captureProcess->setStandardOutputProcess(displayProcess);
351 captureProcess->setWorkingDirectory(KdenliveSettings::capturefolder());
352 kDebug() << "Capture: Running dvgrab " << m_captureArgs.join(" ");
353 captureProcess->start("dvgrab", m_captureArgs);
354 if (play) captureProcess->write(" ", 1);
355 m_discAction->setEnabled(true);
358 m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << "-";
359 m_displayArgs << KdenliveSettings::video4playback().simplified().split(' ') << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
360 captureProcess->setStandardOutputProcess(displayProcess);
361 kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
362 captureProcess->start("ffmpeg", m_captureArgs);
368 if (ui.device_selector->currentIndex() != SCREENGRAB) {
369 kDebug() << "Capture: Running ffplay " << m_captureArgs.join(" ");
370 displayProcess->start("ffplay", m_displayArgs);
371 ui.video_frame->setText(i18n("Initialising..."));
373 // do something when starting screen grab
377 void RecMonitor::slotRecord() {
378 if (captureProcess->state() == QProcess::NotRunning && ui.device_selector->currentIndex() == FIREWIRE) {
382 switch (ui.device_selector->currentIndex()) {
384 captureProcess->write("\e", 2);
385 m_playAction->setIcon(m_playIcon);
386 m_isCapturing = false;
388 m_recAction->setChecked(false);
391 captureProcess->terminate();
393 //m_isCapturing = false;
394 QTimer::singleShot(1000, this, SLOT(slotStartCapture()));
397 captureProcess->write("q\n", 3);
398 captureProcess->terminate();
399 alsaProcess->terminate();
401 // in case ffmpeg doesn't exit with the 'q' command, kill it one second later
402 QTimer::singleShot(1000, captureProcess, SLOT(kill()));
406 } else if (ui.device_selector->currentIndex() == FIREWIRE) {
407 m_isCapturing = true;
409 captureProcess->write("c\n", 3);
412 if (captureProcess->state() == QProcess::NotRunning) {
413 m_recAction->setChecked(true);
414 QString extension = "mpg";
415 if (ui.device_selector->currentIndex() == SCREENGRAB) extension = KdenliveSettings::screengrabextension();
416 QString path = KdenliveSettings::capturefolder() + "/capture0000." + extension;
418 while (QFile::exists(path)) {
419 QString num = QString::number(i).rightJustified(4, '0', false);
420 path = KdenliveSettings::capturefolder() + "/capture" + num + "." + extension;
424 m_captureFile = KUrl(path);
426 m_captureArgs.clear();
427 m_displayArgs.clear();
430 switch (ui.device_selector->currentIndex()) {
432 m_captureArgs << "--format" << "hdv" << "-i" << "capture" << "-";
433 m_displayArgs << "-f" << "mpegts" << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
434 captureProcess->setStandardOutputProcess(displayProcess);
435 kDebug() << "Capture: Running dvgrab " << m_captureArgs.join(" ");
436 captureProcess->start("dvgrab", m_captureArgs);
439 m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << "-y" << m_captureFile.path() << "-f" << KdenliveSettings::video4vencoding() << "-";
440 m_displayArgs << KdenliveSettings::video4playback().simplified().split(' ') << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
441 captureProcess->setStandardOutputProcess(displayProcess);
442 kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
443 captureProcess->start("ffmpeg", m_captureArgs);
446 if (KdenliveSettings::fullscreengrab()) {
447 const QRect rect = QApplication::desktop()->screenGeometry();
448 args = KdenliveSettings::screengrabcapture().replace("%size", QString::number(rect.width()) + "x" + QString::number(rect.height())).replace("%offset", QString());
449 if (KdenliveSettings::screengrabenableaudio()) {
450 // also capture audio
451 if (KdenliveSettings::useosscapture()) m_captureArgs << KdenliveSettings::screengrabosscapture().simplified().split(' ');
452 else m_captureArgs << KdenliveSettings::screengrabalsacapture2().simplified().split(' ');
454 m_captureArgs << args.simplified().split(' ') << KdenliveSettings::screengrabencoding().simplified().split(' ') << m_captureFile.path();
455 ui.video_frame->setText(i18n("Capturing..."));
456 m_isCapturing = true;
457 if (KdenliveSettings::screengrabenableaudio() && !KdenliveSettings::useosscapture()) {
458 QStringList alsaArgs = KdenliveSettings::screengrabalsacapture().simplified().split(' ');
459 alsaProcess->setStandardOutputProcess(captureProcess);
460 kDebug() << "Capture: Running arecord " << alsaArgs.join(" ");
461 alsaProcess->start("arecord", alsaArgs);
463 kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
464 captureProcess->start("ffmpeg", m_captureArgs);
466 ui.video_frame->setText(i18n("Select region..."));
467 rgnGrab = new RegionGrabber();
468 connect(rgnGrab, SIGNAL(regionGrabbed(const QRect&)), SLOT(slotStartGrab(const QRect &)));
475 //ui.video_frame->setScaledContents(false);
476 if (ui.device_selector->currentIndex() != SCREENGRAB) {
477 m_isCapturing = true;
478 kDebug() << "Capture: Running ffplay " << m_displayArgs.join(" ");
479 displayProcess->start("ffplay", m_displayArgs);
480 ui.video_frame->setText(i18n("Initialising..."));
484 displayProcess->kill();
485 captureProcess->kill();
487 QTimer::singleShot(1000, this, SLOT(slotRecord()));
491 void RecMonitor::slotStartGrab(const QRect &rect) {
492 rgnGrab->deleteLater();
493 QApplication::restoreOverrideCursor();
495 if (rect.isNull()) return;
496 int width = rect.width();
497 int height = rect.height();
498 if (width % 2 != 0) width--;
499 if (height % 2 != 0) height--;
500 QString args = KdenliveSettings::screengrabcapture().replace("%size", QString::number(width) + "x" + QString::number(height)).replace("%offset", "+" + QString::number(rect.x()) + "," + QString::number(rect.y()));
501 if (KdenliveSettings::screengrabenableaudio()) {
502 // also capture audio
503 if (KdenliveSettings::useosscapture()) m_captureArgs << KdenliveSettings::screengrabosscapture().simplified().split(' ');
504 else m_captureArgs << KdenliveSettings::screengrabalsacapture2().simplified().split(' ');
506 m_captureArgs << args.simplified().split(' ') << KdenliveSettings::screengrabencoding().simplified().split(' ') << m_captureFile.path();
507 m_isCapturing = true;
508 ui.video_frame->setText(i18n("Capturing..."));
509 if (KdenliveSettings::screengrabenableaudio() && !KdenliveSettings::useosscapture()) {
510 QStringList alsaArgs = KdenliveSettings::screengrabalsacapture().simplified().split(' ');
511 alsaProcess->setStandardOutputProcess(captureProcess);
512 kDebug() << "Capture: Running arecord " << alsaArgs.join(" ");
513 alsaProcess->start("arecord", alsaArgs);
515 kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
516 captureProcess->start("ffmpeg", m_captureArgs);
519 void RecMonitor::slotProcessStatus(QProcess::ProcessState status) {
520 if (status == QProcess::NotRunning) {
521 displayProcess->kill();
522 if (m_isCapturing && ui.device_selector->currentIndex() != FIREWIRE)
523 if (ui.autoaddbox->isChecked() && QFile::exists(m_captureFile.path())) emit addProjectClip(m_captureFile);
524 if (ui.device_selector->currentIndex() == FIREWIRE) {
525 m_discAction->setIcon(KIcon("network-connect"));
526 m_discAction->setText(i18n("Connect"));
527 m_playAction->setEnabled(false);
528 m_rewAction->setEnabled(false);
529 m_fwdAction->setEnabled(false);
530 m_recAction->setEnabled(false);
533 m_playAction->setIcon(m_playIcon);
534 m_recAction->setChecked(false);
535 m_stopAction->setEnabled(false);
536 ui.device_selector->setEnabled(true);
537 if (captureProcess && captureProcess->exitStatus() == QProcess::CrashExit) {
538 ui.video_frame->setText(i18n("Capture crashed, please check your parameters"));
540 ui.video_frame->setText(i18n("Not connected"));
542 m_isCapturing = false;
544 if (ui.device_selector->currentIndex() != SCREENGRAB) m_stopAction->setEnabled(true);
545 ui.device_selector->setEnabled(false);
549 void RecMonitor::manageCapturedFiles() {
551 switch (KdenliveSettings::firewireformat()) {
563 QDir dir(KdenliveSettings::capturefolder());
565 filters << "capture*" + extension;
566 const QStringList result = dir.entryList(filters, QDir::Files, QDir::Time);
567 KUrl::List capturedFiles;
568 foreach(QString name, result) {
569 KUrl url = KUrl(dir.filePath(name));
570 if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, this)) {
571 KFileItem file(KFileItem::Unknown, KFileItem::Unknown, url, true);
572 if (file.time(KFileItem::ModificationTime) > m_captureTime) capturedFiles.append(url);
575 kDebug() << "Found : " << capturedFiles.count() << " new capture files";
576 kDebug() << capturedFiles;
578 if (capturedFiles.count() > 0) {
579 ManageCapturesDialog *d = new ManageCapturesDialog(capturedFiles, this);
580 if (d->exec() == QDialog::Accepted) {
581 capturedFiles = d->importFiles();
582 foreach(KUrl url, capturedFiles) {
583 emit addProjectClip(url);
591 void RecMonitor::mousePressEvent(QMouseEvent * event) {
595 void RecMonitor::activateRecMonitor() {
596 //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
599 void RecMonitor::stop() {
604 void RecMonitor::start() {
609 void RecMonitor::refreshRecMonitor(bool visible) {
611 //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
616 void RecMonitor::slotPlay() {
618 //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
623 #include "recmonitor.moc"