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);
119 kDebug() << "/////// BUILDING MONITOR, ID: " << ui.video_frame->winId();
122 RecMonitor::~RecMonitor() {
123 delete captureProcess;
124 delete displayProcess;
128 QString RecMonitor::name() const {
132 void RecMonitor::slotConfigure() {
133 emit showConfigDialog(4, ui.device_selector->currentIndex());
136 void RecMonitor::slotVideoDeviceChanged(int ix) {
139 m_discAction->setEnabled(false);
140 m_rewAction->setEnabled(false);
141 m_fwdAction->setEnabled(false);
142 m_recAction->setEnabled(true);
143 m_stopAction->setEnabled(false);
144 m_playAction->setEnabled(false);
145 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())));
146 //ui.video_frame->setText(i18n("Press record button\nto start screen capture"));
149 m_discAction->setEnabled(false);
150 m_rewAction->setEnabled(false);
151 m_fwdAction->setEnabled(false);
152 m_recAction->setEnabled(true);
153 m_stopAction->setEnabled(false);
154 m_playAction->setEnabled(true);
155 checkDeviceAvailability();
158 m_discAction->setEnabled(true);
159 m_recAction->setEnabled(false);
160 m_stopAction->setEnabled(false);
161 m_playAction->setEnabled(false);
162 m_rewAction->setEnabled(false);
163 m_fwdAction->setEnabled(false);
164 //ui.video_frame->setText(i18n("Plug your camcorder and\npress connect button\nto initialize connection"));
165 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())));
170 QPixmap RecMonitor::mergeSideBySide(const QPixmap& pix, const QString txt) {
172 QRect r = p.fontMetrics().boundingRect(QRect(0, 0, ui.video_frame->width(), ui.video_frame->height()), Qt::AlignLeft, txt);
173 int strWidth = r.width();
174 int strHeight = r.height();
175 int pixWidth = pix.width();
176 int pixHeight = pix.height();
177 QPixmap res(strWidth + 8 + pixWidth, qMax(strHeight, pixHeight));
178 res.fill(Qt::transparent);
180 p.drawPixmap(0, 0, pix);
181 p.drawText(QRect(pixWidth + 8, 0, strWidth, strHeight), 0, txt);
187 void RecMonitor::checkDeviceAvailability() {
188 if (!KIO::NetAccess::exists(KUrl(KdenliveSettings::video4vdevice()), KIO::NetAccess::SourceSide , this)) {
189 m_playAction->setEnabled(false);
190 m_recAction->setEnabled(false);
191 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())));
192 //ui.video_frame->setText(i18n("Cannot read from device %1\nPlease check drivers and access rights.", KdenliveSettings::video4vdevice()));
193 } else //ui.video_frame->setText(i18n("Press play or record button\nto start video capture"));
194 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())));
197 void RecMonitor::slotDisconnect() {
198 if (captureProcess->state() == QProcess::NotRunning) {
199 m_captureTime = KDateTime::currentLocalDateTime();
200 kDebug() << "CURRENT TIME: " << m_captureTime.toString();
201 m_didCapture = false;
202 slotStartCapture(false);
203 m_discAction->setIcon(KIcon("network-disconnect"));
204 m_discAction->setText(i18n("Disonnect"));
205 m_recAction->setEnabled(true);
206 m_stopAction->setEnabled(true);
207 m_playAction->setEnabled(true);
208 m_rewAction->setEnabled(true);
209 m_fwdAction->setEnabled(true);
211 captureProcess->write("q", 1);
212 QTimer::singleShot(1000, captureProcess, SLOT(kill()));
213 if (m_didCapture) manageCapturedFiles();
214 m_didCapture = false;
218 void RecMonitor::slotRewind() {
219 captureProcess->write("a", 1);
222 void RecMonitor::slotForward() {
223 captureProcess->write("z", 1);
226 void RecMonitor::slotStopCapture() {
228 switch (ui.device_selector->currentIndex()) {
230 captureProcess->write("\e", 2);
231 m_playAction->setIcon(m_playIcon);
235 captureProcess->write("q\n", 3);
236 QTimer::singleShot(1000, captureProcess, SLOT(kill()));
240 captureProcess->write("q\n", 3);
241 QTimer::singleShot(1000, captureProcess, SLOT(kill()));
248 void RecMonitor::slotStartCapture(bool play) {
251 *captureProcess<<"dvgrab";
255 switch (m_recPanel->capture_format->currentItem()){
257 *captureProcess<<"--format"<<"dv1";
260 *captureProcess<<"--format"<<"dv2";
263 *captureProcess<<"--format"<<"hdv";
267 *captureProcess<<"--format"<<"raw";
271 if (KdenliveSettings::autosplit()) *captureProcess<<"--autosplit";
272 if (KdenliveSettings::timestamp()) *captureProcess<<"--timestamp";
273 *captureProcess<<"-i"<<"capture"<<"-";*/
276 QStringList captureArgs;
277 captureArgs<<"--format"<<"hdv"<<"-i"<<"capture"<<"-";
278 QStringList displayArgs;
280 displayArgs<<"-f"<<"mpegts"<<"-x"<<QString::number(ui.video_frame->width())<<"-y"<<QString::number(ui.video_frame->height())<<"-";
282 captureProcess->setStandardOutputProcess(displayProcess);
283 ui.video_frame->setScaledContents(false);
284 captureProcess->start("dvgrab",captureArgs);
285 displayProcess->start("ffplay", displayArgs);*/
288 if (captureProcess->state() != QProcess::NotRunning) {
289 if (ui.device_selector->currentIndex() == FIREWIRE) {
291 captureProcess->write("k", 1);
292 //captureProcess->write("\e", 2);
293 m_playAction->setIcon(m_playIcon);
296 captureProcess->write("p", 1);
297 m_playAction->setIcon(m_pauseIcon);
303 m_captureArgs.clear();
304 m_displayArgs.clear();
307 switch (ui.device_selector->currentIndex()) {
309 switch (KdenliveSettings::firewireformat()) {
312 m_captureArgs << "--format" << "raw";
313 m_displayArgs << "-f" << "dv";
317 m_captureArgs << "--format" << "dv1";
318 m_displayArgs << "-f" << "dv";
322 m_captureArgs << "--format" << "dv2";
323 m_displayArgs << "-f" << "dv";
327 m_captureArgs << "--format" << "hdv";
328 m_displayArgs << "-f" << "mpegts";
331 if (KdenliveSettings::firewireautosplit()) m_captureArgs << "--autosplit";
332 if (KdenliveSettings::firewiretimestamp()) m_captureArgs << "--timestamp";
333 m_captureArgs << "-i" << "capture" << "-";
334 m_displayArgs << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
336 captureProcess->setStandardOutputProcess(displayProcess);
337 captureProcess->setWorkingDirectory(KdenliveSettings::capturefolder());
338 kDebug() << "Capture: Running dvgrab " << m_captureArgs.join(" ");
339 captureProcess->start("dvgrab", m_captureArgs);
340 if (play) captureProcess->write(" ", 1);
341 m_discAction->setEnabled(true);
344 m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << "-";
345 m_displayArgs << KdenliveSettings::video4playback().simplified().split(' ') << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
346 captureProcess->setStandardOutputProcess(displayProcess);
347 kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
348 captureProcess->start("ffmpeg", m_captureArgs);
354 if (ui.device_selector->currentIndex() != SCREENGRAB) {
355 kDebug() << "Capture: Running ffplay " << m_captureArgs.join(" ");
356 displayProcess->start("ffplay", m_displayArgs);
357 ui.video_frame->setText(i18n("Initialising..."));
359 // do something when starting screen grab
363 void RecMonitor::slotRecord() {
364 if (captureProcess->state() == QProcess::NotRunning && ui.device_selector->currentIndex() == FIREWIRE) {
368 switch (ui.device_selector->currentIndex()) {
370 captureProcess->write("\e", 2);
371 m_playAction->setIcon(m_playIcon);
372 m_isCapturing = false;
374 m_recAction->setChecked(false);
377 captureProcess->terminate();
379 //m_isCapturing = false;
380 QTimer::singleShot(1000, this, SLOT(slotStartCapture()));
383 captureProcess->write("q\n", 3);
384 captureProcess->terminate();
385 alsaProcess->terminate();
387 // in case ffmpeg doesn't exit with the 'q' command, kill it one second later
388 QTimer::singleShot(1000, captureProcess, SLOT(kill()));
392 } else if (ui.device_selector->currentIndex() == FIREWIRE) {
393 m_isCapturing = true;
395 captureProcess->write("c\n", 3);
398 if (captureProcess->state() == QProcess::NotRunning) {
399 m_recAction->setChecked(true);
400 QString extension = "mpg";
401 if (ui.device_selector->currentIndex() == SCREENGRAB) extension = KdenliveSettings::screengrabextension();
402 QString path = KdenliveSettings::capturefolder() + "/capture0000." + extension;
404 while (QFile::exists(path)) {
405 QString num = QString::number(i).rightJustified(4, '0', false);
406 path = KdenliveSettings::capturefolder() + "/capture" + num + "." + extension;
410 m_captureFile = KUrl(path);
412 m_captureArgs.clear();
413 m_displayArgs.clear();
416 switch (ui.device_selector->currentIndex()) {
418 m_captureArgs << "--format" << "hdv" << "-i" << "capture" << "-";
419 m_displayArgs << "-f" << "mpegts" << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
420 captureProcess->setStandardOutputProcess(displayProcess);
421 kDebug() << "Capture: Running dvgrab " << m_captureArgs.join(" ");
422 captureProcess->start("dvgrab", m_captureArgs);
425 m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << "-y" << m_captureFile.path() << "-f" << KdenliveSettings::video4vencoding() << "-";
426 m_displayArgs << KdenliveSettings::video4playback().simplified().split(' ') << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
427 captureProcess->setStandardOutputProcess(displayProcess);
428 kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
429 captureProcess->start("ffmpeg", m_captureArgs);
432 if (KdenliveSettings::fullscreengrab()) {
433 const QRect rect = QApplication::desktop()->screenGeometry();
434 args = KdenliveSettings::screengrabcapture().replace("%size", QString::number(rect.width()) + "x" + QString::number(rect.height())).replace("%offset", QString());
435 if (KdenliveSettings::screengrabenableaudio()) {
436 // also capture audio
437 if (KdenliveSettings::useosscapture()) m_captureArgs << KdenliveSettings::screengrabosscapture().simplified().split(' ');
438 else m_captureArgs << KdenliveSettings::screengrabalsacapture2().simplified().split(' ');
440 m_captureArgs << args.simplified().split(' ') << KdenliveSettings::screengrabencoding().simplified().split(' ') << m_captureFile.path();
441 ui.video_frame->setText(i18n("Capturing..."));
442 m_isCapturing = true;
443 if (KdenliveSettings::screengrabenableaudio() && !KdenliveSettings::useosscapture()) {
444 QStringList alsaArgs = KdenliveSettings::screengrabalsacapture().simplified().split(' ');
445 alsaProcess->setStandardOutputProcess(captureProcess);
446 kDebug() << "Capture: Running arecord " << alsaArgs.join(" ");
447 alsaProcess->start("arecord", alsaArgs);
449 kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
450 captureProcess->start("ffmpeg", m_captureArgs);
452 ui.video_frame->setText(i18n("Select region..."));
453 rgnGrab = new RegionGrabber();
454 connect(rgnGrab, SIGNAL(regionGrabbed(const QRect&)), SLOT(slotStartGrab(const QRect &)));
461 //ui.video_frame->setScaledContents(false);
462 if (ui.device_selector->currentIndex() != SCREENGRAB) {
463 m_isCapturing = true;
464 kDebug() << "Capture: Running ffplay " << m_displayArgs.join(" ");
465 displayProcess->start("ffplay", m_displayArgs);
466 ui.video_frame->setText(i18n("Initialising..."));
470 displayProcess->kill();
471 captureProcess->kill();
473 QTimer::singleShot(1000, this, SLOT(slotRecord()));
477 void RecMonitor::slotStartGrab(const QRect &rect) {
478 rgnGrab->deleteLater();
479 QApplication::restoreOverrideCursor();
481 if (rect.isNull()) return;
482 int width = rect.width();
483 int height = rect.height();
484 if (width % 2 != 0) width--;
485 if (height % 2 != 0) height--;
486 QString args = KdenliveSettings::screengrabcapture().replace("%size", QString::number(width) + "x" + QString::number(height)).replace("%offset", "+" + QString::number(rect.x()) + "," + QString::number(rect.y()));
487 if (KdenliveSettings::screengrabenableaudio()) {
488 // also capture audio
489 if (KdenliveSettings::useosscapture()) m_captureArgs << KdenliveSettings::screengrabosscapture().simplified().split(' ');
490 else m_captureArgs << KdenliveSettings::screengrabalsacapture2().simplified().split(' ');
492 m_captureArgs << args.simplified().split(' ') << KdenliveSettings::screengrabencoding().simplified().split(' ') << m_captureFile.path();
493 m_isCapturing = true;
494 ui.video_frame->setText(i18n("Capturing..."));
495 if (KdenliveSettings::screengrabenableaudio() && !KdenliveSettings::useosscapture()) {
496 QStringList alsaArgs = KdenliveSettings::screengrabalsacapture().simplified().split(' ');
497 alsaProcess->setStandardOutputProcess(captureProcess);
498 kDebug() << "Capture: Running arecord " << alsaArgs.join(" ");
499 alsaProcess->start("arecord", alsaArgs);
501 kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
502 captureProcess->start("ffmpeg", m_captureArgs);
505 void RecMonitor::slotProcessStatus(QProcess::ProcessState status) {
506 if (status == QProcess::NotRunning) {
507 displayProcess->kill();
508 if (m_isCapturing && ui.device_selector->currentIndex() != FIREWIRE)
509 if (ui.autoaddbox->isChecked() && QFile::exists(m_captureFile.path())) emit addProjectClip(m_captureFile);
510 if (ui.device_selector->currentIndex() == FIREWIRE) {
511 m_discAction->setIcon(KIcon("network-connect"));
512 m_discAction->setText(i18n("Connect"));
513 m_playAction->setEnabled(false);
514 m_rewAction->setEnabled(false);
515 m_fwdAction->setEnabled(false);
516 m_recAction->setEnabled(false);
519 m_playAction->setIcon(m_playIcon);
520 m_recAction->setChecked(false);
521 m_stopAction->setEnabled(false);
522 ui.device_selector->setEnabled(true);
523 if (captureProcess && captureProcess->exitStatus() == QProcess::CrashExit) {
524 ui.video_frame->setText(i18n("Capture crashed, please check your parameters"));
526 ui.video_frame->setText(i18n("Not connected"));
528 m_isCapturing = false;
530 if (ui.device_selector->currentIndex() != SCREENGRAB) m_stopAction->setEnabled(true);
531 ui.device_selector->setEnabled(false);
535 void RecMonitor::manageCapturedFiles() {
537 switch (KdenliveSettings::firewireformat()) {
549 QDir dir(KdenliveSettings::capturefolder());
551 filters << "capture*" + extension;
552 const QStringList result = dir.entryList(filters, QDir::Files, QDir::Time);
553 KUrl::List capturedFiles;
554 foreach(QString name, result) {
555 KUrl url = KUrl(dir.filePath(name));
556 if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, this)) {
557 KFileItem file(KFileItem::Unknown, KFileItem::Unknown, url, true);
558 if (file.time(KFileItem::ModificationTime) > m_captureTime) capturedFiles.append(url);
561 kDebug() << "Found : " << capturedFiles.count() << " new capture files";
562 kDebug() << capturedFiles;
564 if (capturedFiles.count() > 0) {
565 ManageCapturesDialog *d = new ManageCapturesDialog(capturedFiles, this);
566 if (d->exec() == QDialog::Accepted) {
567 capturedFiles = d->importFiles();
568 foreach(KUrl url, capturedFiles) {
569 emit addProjectClip(url);
577 void RecMonitor::mousePressEvent(QMouseEvent * event) {
581 void RecMonitor::activateRecMonitor() {
582 //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
585 void RecMonitor::stop() {
590 void RecMonitor::start() {
595 void RecMonitor::refreshRecMonitor(bool visible) {
597 //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
602 void RecMonitor::slotPlay() {
604 //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
609 #include "recmonitor.moc"