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;
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 kDebug() << "/////// BUILDING MONITOR, ID: " << ui.video_frame->winId();
123 RecMonitor::~RecMonitor() {
124 delete captureProcess;
125 delete displayProcess;
129 QString RecMonitor::name() const {
133 void RecMonitor::slotConfigure() {
134 emit showConfigDialog(4, ui.device_selector->currentIndex());
137 void RecMonitor::slotVideoDeviceChanged(int ix) {
140 m_discAction->setEnabled(false);
141 m_rewAction->setEnabled(false);
142 m_fwdAction->setEnabled(false);
143 m_recAction->setEnabled(true);
144 m_stopAction->setEnabled(false);
145 m_playAction->setEnabled(false);
146 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())));
147 //ui.video_frame->setText(i18n("Press record button\nto start screen capture"));
150 m_discAction->setEnabled(false);
151 m_rewAction->setEnabled(false);
152 m_fwdAction->setEnabled(false);
153 m_recAction->setEnabled(true);
154 m_stopAction->setEnabled(false);
155 m_playAction->setEnabled(true);
156 checkDeviceAvailability();
159 m_discAction->setEnabled(true);
160 m_recAction->setEnabled(false);
161 m_stopAction->setEnabled(false);
162 m_playAction->setEnabled(false);
163 m_rewAction->setEnabled(false);
164 m_fwdAction->setEnabled(false);
165 //ui.video_frame->setText(i18n("Plug your camcorder and\npress connect button\nto initialize connection"));
166 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())));
171 QPixmap RecMonitor::mergeSideBySide(const QPixmap& pix, const QString txt) {
173 QRect r = p.fontMetrics().boundingRect(QRect(0, 0, ui.video_frame->width(), ui.video_frame->height()), Qt::AlignLeft, txt);
174 int strWidth = r.width();
175 int strHeight = r.height();
176 int pixWidth = pix.width();
177 int pixHeight = pix.height();
178 QPixmap res(strWidth + 8 + pixWidth, qMax(strHeight, pixHeight));
179 res.fill(Qt::transparent);
181 p.drawPixmap(0, 0, pix);
182 p.drawText(QRect(pixWidth + 8, 0, strWidth, strHeight), 0, txt);
188 void RecMonitor::checkDeviceAvailability() {
189 if (!KIO::NetAccess::exists(KUrl(KdenliveSettings::video4vdevice()), KIO::NetAccess::SourceSide , this)) {
190 m_playAction->setEnabled(false);
191 m_recAction->setEnabled(false);
192 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())));
193 //ui.video_frame->setText(i18n("Cannot read from device %1\nPlease check drivers and access rights.", KdenliveSettings::video4vdevice()));
194 } else //ui.video_frame->setText(i18n("Press play or record button\nto start video capture"));
195 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())));
198 void RecMonitor::slotDisconnect() {
199 if (captureProcess->state() == QProcess::NotRunning) {
200 m_captureTime = KDateTime::currentLocalDateTime();
201 kDebug() << "CURRENT TIME: " << m_captureTime.toString();
202 m_didCapture = false;
203 slotStartCapture(false);
204 m_discAction->setIcon(KIcon("network-disconnect"));
205 m_discAction->setText(i18n("Disonnect"));
206 m_recAction->setEnabled(true);
207 m_stopAction->setEnabled(true);
208 m_playAction->setEnabled(true);
209 m_rewAction->setEnabled(true);
210 m_fwdAction->setEnabled(true);
212 captureProcess->write("q", 1);
213 QTimer::singleShot(1000, captureProcess, SLOT(kill()));
214 if (m_didCapture) manageCapturedFiles();
215 m_didCapture = false;
219 void RecMonitor::slotRewind() {
220 captureProcess->write("a", 1);
223 void RecMonitor::slotForward() {
224 captureProcess->write("z", 1);
227 void RecMonitor::slotStopCapture() {
229 switch (ui.device_selector->currentIndex()) {
231 captureProcess->write("\e", 2);
232 m_playAction->setIcon(m_playIcon);
236 captureProcess->write("q\n", 3);
237 QTimer::singleShot(1000, captureProcess, SLOT(kill()));
241 captureProcess->write("q\n", 3);
242 QTimer::singleShot(1000, captureProcess, SLOT(kill()));
249 void RecMonitor::slotStartCapture(bool play) {
252 *captureProcess<<"dvgrab";
256 switch (m_recPanel->capture_format->currentItem()){
258 *captureProcess<<"--format"<<"dv1";
261 *captureProcess<<"--format"<<"dv2";
264 *captureProcess<<"--format"<<"hdv";
268 *captureProcess<<"--format"<<"raw";
272 if (KdenliveSettings::autosplit()) *captureProcess<<"--autosplit";
273 if (KdenliveSettings::timestamp()) *captureProcess<<"--timestamp";
274 *captureProcess<<"-i"<<"capture"<<"-";*/
277 QStringList captureArgs;
278 captureArgs<<"--format"<<"hdv"<<"-i"<<"capture"<<"-";
279 QStringList displayArgs;
281 displayArgs<<"-f"<<"mpegts"<<"-x"<<QString::number(ui.video_frame->width())<<"-y"<<QString::number(ui.video_frame->height())<<"-";
283 captureProcess->setStandardOutputProcess(displayProcess);
284 ui.video_frame->setScaledContents(false);
285 captureProcess->start("dvgrab",captureArgs);
286 displayProcess->start("ffplay", displayArgs);*/
289 if (captureProcess->state() != QProcess::NotRunning) {
290 if (ui.device_selector->currentIndex() == FIREWIRE) {
292 captureProcess->write("k", 1);
293 //captureProcess->write("\e", 2);
294 m_playAction->setIcon(m_playIcon);
297 captureProcess->write("p", 1);
298 m_playAction->setIcon(m_pauseIcon);
304 m_captureArgs.clear();
305 m_displayArgs.clear();
308 switch (ui.device_selector->currentIndex()) {
310 switch (KdenliveSettings::firewireformat()) {
313 m_captureArgs << "--format" << "raw";
314 m_displayArgs << "-f" << "dv";
318 m_captureArgs << "--format" << "dv1";
319 m_displayArgs << "-f" << "dv";
323 m_captureArgs << "--format" << "dv2";
324 m_displayArgs << "-f" << "dv";
328 m_captureArgs << "--format" << "hdv";
329 m_displayArgs << "-f" << "mpegts";
332 if (KdenliveSettings::firewireautosplit()) m_captureArgs << "--autosplit";
333 if (KdenliveSettings::firewiretimestamp()) m_captureArgs << "--timestamp";
334 m_captureArgs << "-i" << "capture" << "-";
335 m_displayArgs << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
337 captureProcess->setStandardOutputProcess(displayProcess);
338 captureProcess->setWorkingDirectory(KdenliveSettings::capturefolder());
339 kDebug() << "Capture: Running dvgrab " << m_captureArgs.join(" ");
340 captureProcess->start("dvgrab", m_captureArgs);
341 if (play) captureProcess->write(" ", 1);
342 m_discAction->setEnabled(true);
345 m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << "-";
346 m_displayArgs << KdenliveSettings::video4playback().simplified().split(' ') << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
347 captureProcess->setStandardOutputProcess(displayProcess);
348 kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
349 captureProcess->start("ffmpeg", m_captureArgs);
355 if (ui.device_selector->currentIndex() != SCREENGRAB) {
356 kDebug() << "Capture: Running ffplay " << m_captureArgs.join(" ");
357 displayProcess->start("ffplay", m_displayArgs);
358 ui.video_frame->setText(i18n("Initialising..."));
360 // do something when starting screen grab
364 void RecMonitor::slotRecord() {
365 if (captureProcess->state() == QProcess::NotRunning && ui.device_selector->currentIndex() == FIREWIRE) {
369 switch (ui.device_selector->currentIndex()) {
371 captureProcess->write("\e", 2);
372 m_playAction->setIcon(m_playIcon);
373 m_isCapturing = false;
375 m_recAction->setChecked(false);
378 captureProcess->terminate();
380 //m_isCapturing = false;
381 QTimer::singleShot(1000, this, SLOT(slotStartCapture()));
384 captureProcess->write("q\n", 3);
385 captureProcess->terminate();
386 alsaProcess->terminate();
388 // in case ffmpeg doesn't exit with the 'q' command, kill it one second later
389 QTimer::singleShot(1000, captureProcess, SLOT(kill()));
393 } else if (ui.device_selector->currentIndex() == FIREWIRE) {
394 m_isCapturing = true;
396 captureProcess->write("c\n", 3);
399 if (captureProcess->state() == QProcess::NotRunning) {
400 m_recAction->setChecked(true);
401 QString extension = "mpg";
402 if (ui.device_selector->currentIndex() == SCREENGRAB) extension = KdenliveSettings::screengrabextension();
403 QString path = KdenliveSettings::capturefolder() + "/capture0000." + extension;
405 while (QFile::exists(path)) {
406 QString num = QString::number(i).rightJustified(4, '0', false);
407 path = KdenliveSettings::capturefolder() + "/capture" + num + "." + extension;
411 m_captureFile = KUrl(path);
413 m_captureArgs.clear();
414 m_displayArgs.clear();
417 switch (ui.device_selector->currentIndex()) {
419 m_captureArgs << "--format" << "hdv" << "-i" << "capture" << "-";
420 m_displayArgs << "-f" << "mpegts" << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
421 captureProcess->setStandardOutputProcess(displayProcess);
422 kDebug() << "Capture: Running dvgrab " << m_captureArgs.join(" ");
423 captureProcess->start("dvgrab", m_captureArgs);
426 m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << "-y" << m_captureFile.path() << "-f" << KdenliveSettings::video4vencoding() << "-";
427 m_displayArgs << KdenliveSettings::video4playback().simplified().split(' ') << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
428 captureProcess->setStandardOutputProcess(displayProcess);
429 kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
430 captureProcess->start("ffmpeg", m_captureArgs);
433 if (KdenliveSettings::fullscreengrab()) {
434 const QRect rect = QApplication::desktop()->screenGeometry();
435 args = KdenliveSettings::screengrabcapture().replace("%size", QString::number(rect.width()) + "x" + QString::number(rect.height())).replace("%offset", QString());
436 if (KdenliveSettings::screengrabenableaudio()) {
437 // also capture audio
438 if (KdenliveSettings::useosscapture()) m_captureArgs << KdenliveSettings::screengrabosscapture().simplified().split(' ');
439 else m_captureArgs << KdenliveSettings::screengrabalsacapture2().simplified().split(' ');
441 m_captureArgs << args.simplified().split(' ') << KdenliveSettings::screengrabencoding().simplified().split(' ') << m_captureFile.path();
442 ui.video_frame->setText(i18n("Capturing..."));
443 m_isCapturing = true;
444 if (KdenliveSettings::screengrabenableaudio() && !KdenliveSettings::useosscapture()) {
445 QStringList alsaArgs = KdenliveSettings::screengrabalsacapture().simplified().split(' ');
446 alsaProcess->setStandardOutputProcess(captureProcess);
447 kDebug() << "Capture: Running arecord " << alsaArgs.join(" ");
448 alsaProcess->start("arecord", alsaArgs);
450 kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
451 captureProcess->start("ffmpeg", m_captureArgs);
453 ui.video_frame->setText(i18n("Select region..."));
454 rgnGrab = new RegionGrabber();
455 connect(rgnGrab, SIGNAL(regionGrabbed(const QRect&)), SLOT(slotStartGrab(const QRect &)));
462 //ui.video_frame->setScaledContents(false);
463 if (ui.device_selector->currentIndex() != SCREENGRAB) {
464 m_isCapturing = true;
465 kDebug() << "Capture: Running ffplay " << m_displayArgs.join(" ");
466 displayProcess->start("ffplay", m_displayArgs);
467 ui.video_frame->setText(i18n("Initialising..."));
471 displayProcess->kill();
472 captureProcess->kill();
474 QTimer::singleShot(1000, this, SLOT(slotRecord()));
478 void RecMonitor::slotStartGrab(const QRect &rect) {
479 rgnGrab->deleteLater();
480 QApplication::restoreOverrideCursor();
482 if (rect.isNull()) return;
483 int width = rect.width();
484 int height = rect.height();
485 if (width % 2 != 0) width--;
486 if (height % 2 != 0) height--;
487 QString args = KdenliveSettings::screengrabcapture().replace("%size", QString::number(width) + "x" + QString::number(height)).replace("%offset", "+" + QString::number(rect.x()) + "," + QString::number(rect.y()));
488 if (KdenliveSettings::screengrabenableaudio()) {
489 // also capture audio
490 if (KdenliveSettings::useosscapture()) m_captureArgs << KdenliveSettings::screengrabosscapture().simplified().split(' ');
491 else m_captureArgs << KdenliveSettings::screengrabalsacapture2().simplified().split(' ');
493 m_captureArgs << args.simplified().split(' ') << KdenliveSettings::screengrabencoding().simplified().split(' ') << m_captureFile.path();
494 m_isCapturing = true;
495 ui.video_frame->setText(i18n("Capturing..."));
496 if (KdenliveSettings::screengrabenableaudio() && !KdenliveSettings::useosscapture()) {
497 QStringList alsaArgs = KdenliveSettings::screengrabalsacapture().simplified().split(' ');
498 alsaProcess->setStandardOutputProcess(captureProcess);
499 kDebug() << "Capture: Running arecord " << alsaArgs.join(" ");
500 alsaProcess->start("arecord", alsaArgs);
502 kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
503 captureProcess->start("ffmpeg", m_captureArgs);
506 void RecMonitor::slotProcessStatus(QProcess::ProcessState status) {
507 if (status == QProcess::NotRunning) {
508 displayProcess->kill();
509 if (m_isCapturing && ui.device_selector->currentIndex() != FIREWIRE)
510 if (ui.autoaddbox->isChecked() && QFile::exists(m_captureFile.path())) emit addProjectClip(m_captureFile);
511 if (ui.device_selector->currentIndex() == FIREWIRE) {
512 m_discAction->setIcon(KIcon("network-connect"));
513 m_discAction->setText(i18n("Connect"));
514 m_playAction->setEnabled(false);
515 m_rewAction->setEnabled(false);
516 m_fwdAction->setEnabled(false);
517 m_recAction->setEnabled(false);
520 m_playAction->setIcon(m_playIcon);
521 m_recAction->setChecked(false);
522 m_stopAction->setEnabled(false);
523 ui.device_selector->setEnabled(true);
524 if (captureProcess && captureProcess->exitStatus() == QProcess::CrashExit) {
525 ui.video_frame->setText(i18n("Capture crashed, please check your parameters"));
527 ui.video_frame->setText(i18n("Not connected"));
529 m_isCapturing = false;
531 if (ui.device_selector->currentIndex() != SCREENGRAB) m_stopAction->setEnabled(true);
532 ui.device_selector->setEnabled(false);
536 void RecMonitor::manageCapturedFiles() {
538 switch (KdenliveSettings::firewireformat()) {
550 QDir dir(KdenliveSettings::capturefolder());
552 filters << "capture*" + extension;
553 const QStringList result = dir.entryList(filters, QDir::Files, QDir::Time);
554 KUrl::List capturedFiles;
555 foreach(QString name, result) {
556 KUrl url = KUrl(dir.filePath(name));
557 if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, this)) {
558 KFileItem file(KFileItem::Unknown, KFileItem::Unknown, url, true);
559 if (file.time(KFileItem::ModificationTime) > m_captureTime) capturedFiles.append(url);
562 kDebug() << "Found : " << capturedFiles.count() << " new capture files";
563 kDebug() << capturedFiles;
565 if (capturedFiles.count() > 0) {
566 ManageCapturesDialog *d = new ManageCapturesDialog(capturedFiles, this);
567 if (d->exec() == QDialog::Accepted) {
568 capturedFiles = d->importFiles();
569 foreach(KUrl url, capturedFiles) {
570 emit addProjectClip(url);
578 void RecMonitor::mousePressEvent(QMouseEvent * event) {
582 void RecMonitor::activateRecMonitor() {
583 //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
586 void RecMonitor::stop() {
591 void RecMonitor::start() {
596 void RecMonitor::refreshRecMonitor(bool visible) {
598 //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
603 void RecMonitor::slotPlay() {
605 //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
610 #include "recmonitor.moc"