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);
119 kDebug() << "/////// BUILDING MONITOR, ID: " << ui.video_frame->winId();
122 RecMonitor::~RecMonitor() {
123 delete captureProcess;
124 delete displayProcess;
127 QString RecMonitor::name() const {
131 void RecMonitor::slotConfigure() {
132 emit showConfigDialog(4, ui.device_selector->currentIndex());
135 void RecMonitor::slotVideoDeviceChanged(int ix) {
138 m_discAction->setEnabled(false);
139 m_rewAction->setEnabled(false);
140 m_fwdAction->setEnabled(false);
141 m_recAction->setEnabled(true);
142 m_stopAction->setEnabled(false);
143 m_playAction->setEnabled(false);
144 if (KdenliveSettings::rmd_path().isEmpty()) {
145 QString rmdpath = KStandardDirs::findExe("recordmydesktop");
146 if (rmdpath.isEmpty()) ui.video_frame->setPixmap(mergeSideBySide(KIcon("dialog-warning").pixmap(QSize(50, 50)), i18n("Recordmydesktop utility not found, please install it for screen grabs")));
147 else KdenliveSettings::setRmd_path(rmdpath);
149 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())));
150 //ui.video_frame->setText(i18n("Press record button\nto start screen capture"));
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(true);
159 checkDeviceAvailability();
162 m_discAction->setEnabled(true);
163 m_recAction->setEnabled(false);
164 m_stopAction->setEnabled(false);
165 m_playAction->setEnabled(false);
166 m_rewAction->setEnabled(false);
167 m_fwdAction->setEnabled(false);
168 //ui.video_frame->setText(i18n("Plug your camcorder and\npress connect button\nto initialize connection"));
169 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())));
174 QPixmap RecMonitor::mergeSideBySide(const QPixmap& pix, const QString txt) {
176 QRect r = p.fontMetrics().boundingRect(QRect(0, 0, ui.video_frame->width(), ui.video_frame->height()), Qt::AlignLeft, txt);
177 int strWidth = r.width();
178 int strHeight = r.height();
179 int pixWidth = pix.width();
180 int pixHeight = pix.height();
181 QPixmap res(strWidth + 8 + pixWidth, qMax(strHeight, pixHeight));
182 res.fill(Qt::transparent);
184 p.drawPixmap(0, 0, pix);
185 p.drawText(QRect(pixWidth + 8, 0, strWidth, strHeight), 0, txt);
191 void RecMonitor::checkDeviceAvailability() {
192 if (!KIO::NetAccess::exists(KUrl(KdenliveSettings::video4vdevice()), KIO::NetAccess::SourceSide , this)) {
193 m_playAction->setEnabled(false);
194 m_recAction->setEnabled(false);
195 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())));
196 //ui.video_frame->setText(i18n("Cannot read from device %1\nPlease check drivers and access rights.", KdenliveSettings::video4vdevice()));
197 } else //ui.video_frame->setText(i18n("Press play or record button\nto start video capture"));
198 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())));
201 void RecMonitor::slotDisconnect() {
202 if (captureProcess->state() == QProcess::NotRunning) {
203 m_captureTime = KDateTime::currentLocalDateTime();
204 kDebug() << "CURRENT TIME: " << m_captureTime.toString();
205 m_didCapture = false;
206 slotStartCapture(false);
207 m_discAction->setIcon(KIcon("network-disconnect"));
208 m_discAction->setText(i18n("Disonnect"));
209 m_recAction->setEnabled(true);
210 m_stopAction->setEnabled(true);
211 m_playAction->setEnabled(true);
212 m_rewAction->setEnabled(true);
213 m_fwdAction->setEnabled(true);
215 captureProcess->write("q", 1);
216 QTimer::singleShot(1000, captureProcess, SLOT(kill()));
217 if (m_didCapture) manageCapturedFiles();
218 m_didCapture = false;
222 void RecMonitor::slotRewind() {
223 captureProcess->write("a", 1);
226 void RecMonitor::slotForward() {
227 captureProcess->write("z", 1);
230 void RecMonitor::slotStopCapture() {
232 switch (ui.device_selector->currentIndex()) {
234 captureProcess->write("\e", 2);
235 m_playAction->setIcon(m_playIcon);
239 captureProcess->write("q\n", 3);
240 QTimer::singleShot(1000, captureProcess, SLOT(kill()));
244 captureProcess->write("q\n", 3);
245 QTimer::singleShot(1000, captureProcess, SLOT(kill()));
252 void RecMonitor::slotStartCapture(bool play) {
255 *captureProcess<<"dvgrab";
259 switch (m_recPanel->capture_format->currentItem()){
261 *captureProcess<<"--format"<<"dv1";
264 *captureProcess<<"--format"<<"dv2";
267 *captureProcess<<"--format"<<"hdv";
271 *captureProcess<<"--format"<<"raw";
275 if (KdenliveSettings::autosplit()) *captureProcess<<"--autosplit";
276 if (KdenliveSettings::timestamp()) *captureProcess<<"--timestamp";
277 *captureProcess<<"-i"<<"capture"<<"-";*/
280 QStringList captureArgs;
281 captureArgs<<"--format"<<"hdv"<<"-i"<<"capture"<<"-";
282 QStringList displayArgs;
284 displayArgs<<"-f"<<"mpegts"<<"-x"<<QString::number(ui.video_frame->width())<<"-y"<<QString::number(ui.video_frame->height())<<"-";
286 captureProcess->setStandardOutputProcess(displayProcess);
287 ui.video_frame->setScaledContents(false);
288 captureProcess->start("dvgrab",captureArgs);
289 displayProcess->start("ffplay", displayArgs);*/
292 if (captureProcess->state() != QProcess::NotRunning) {
293 if (ui.device_selector->currentIndex() == FIREWIRE) {
295 captureProcess->write("k", 1);
296 //captureProcess->write("\e", 2);
297 m_playAction->setIcon(m_playIcon);
300 captureProcess->write("p", 1);
301 m_playAction->setIcon(m_pauseIcon);
307 m_captureArgs.clear();
308 m_displayArgs.clear();
311 switch (ui.device_selector->currentIndex()) {
313 switch (KdenliveSettings::firewireformat()) {
316 m_captureArgs << "--format" << "raw";
317 m_displayArgs << "-f" << "dv";
321 m_captureArgs << "--format" << "dv1";
322 m_displayArgs << "-f" << "dv";
326 m_captureArgs << "--format" << "dv2";
327 m_displayArgs << "-f" << "dv";
331 m_captureArgs << "--format" << "hdv";
332 m_displayArgs << "-f" << "mpegts";
335 if (KdenliveSettings::firewireautosplit()) m_captureArgs << "--autosplit";
336 if (KdenliveSettings::firewiretimestamp()) m_captureArgs << "--timestamp";
337 m_captureArgs << "-i" << "capture" << "-";
338 m_displayArgs << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
340 captureProcess->setStandardOutputProcess(displayProcess);
341 captureProcess->setWorkingDirectory(KdenliveSettings::capturefolder());
342 kDebug() << "Capture: Running dvgrab " << m_captureArgs.join(" ");
343 captureProcess->start("dvgrab", m_captureArgs);
344 if (play) captureProcess->write(" ", 1);
345 m_discAction->setEnabled(true);
348 m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << "-";
349 m_displayArgs << KdenliveSettings::video4playback().simplified().split(' ') << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
350 captureProcess->setStandardOutputProcess(displayProcess);
351 kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
352 captureProcess->start("ffmpeg", m_captureArgs);
358 if (ui.device_selector->currentIndex() != SCREENGRAB) {
359 kDebug() << "Capture: Running ffplay " << m_captureArgs.join(" ");
360 displayProcess->start("ffplay", m_displayArgs);
361 ui.video_frame->setText(i18n("Initialising..."));
363 // do something when starting screen grab
367 void RecMonitor::slotRecord() {
368 if (captureProcess->state() == QProcess::NotRunning && ui.device_selector->currentIndex() == FIREWIRE) {
372 switch (ui.device_selector->currentIndex()) {
374 captureProcess->write("\e", 2);
375 m_playAction->setIcon(m_playIcon);
376 m_isCapturing = false;
378 m_recAction->setChecked(false);
381 captureProcess->terminate();
383 //m_isCapturing = false;
384 QTimer::singleShot(1000, this, SLOT(slotStartCapture()));
387 //captureProcess->write("q\n", 3);
388 captureProcess->terminate();
389 ui.video_frame->setText(i18n("Encoding captured video..."));
390 // in case ffmpeg doesn't exit with the 'q' command, kill it one second later
391 //QTimer::singleShot(1000, captureProcess, SLOT(kill()));
395 } else if (ui.device_selector->currentIndex() == FIREWIRE) {
396 m_isCapturing = true;
398 captureProcess->write("c\n", 3);
401 if (captureProcess->state() == QProcess::NotRunning) {
402 m_recAction->setChecked(true);
403 QString extension = "mpg";
404 if (ui.device_selector->currentIndex() == SCREENGRAB) extension = "ogv"; //KdenliveSettings::screengrabextension();
405 QString path = KdenliveSettings::capturefolder() + "/capture0000." + extension;
407 while (QFile::exists(path)) {
408 QString num = QString::number(i).rightJustified(4, '0', false);
409 path = KdenliveSettings::capturefolder() + "/capture" + num + "." + extension;
413 m_captureFile = KUrl(path);
415 m_captureArgs.clear();
416 m_displayArgs.clear();
419 switch (ui.device_selector->currentIndex()) {
421 m_captureArgs << "--format" << "hdv" << "-i" << "capture" << "-";
422 m_displayArgs << "-f" << "mpegts" << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
423 captureProcess->setStandardOutputProcess(displayProcess);
424 kDebug() << "Capture: Running dvgrab " << m_captureArgs.join(" ");
425 captureProcess->start("dvgrab", m_captureArgs);
428 m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << "-y" << m_captureFile.path() << "-f" << KdenliveSettings::video4vencoding() << "-";
429 m_displayArgs << KdenliveSettings::video4playback().simplified().split(' ') << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
430 captureProcess->setStandardOutputProcess(displayProcess);
431 kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
432 captureProcess->start("ffmpeg", m_captureArgs);
435 switch (KdenliveSettings::rmd_capture_type()) {
437 // Full screen capture, nothing special to do
441 m_captureArgs << "-width" << QString::number(KdenliveSettings::rmd_width()) << "-height" << QString::number(KdenliveSettings::rmd_height());
442 if (!KdenliveSettings::rmd_follow_mouse()) {
443 m_captureArgs << "-x" << QString::number(KdenliveSettings::rmd_offsetx()) << "-y" << QString::number(KdenliveSettings::rmd_offsety());
444 } else m_captureArgs << "--follow-mouse";
447 m_isCapturing = true;
448 if (KdenliveSettings::rmd_capture_audio()) {
449 if (KdenliveSettings::rmd_use_jack()) m_captureArgs << "-use-jack" << KdenliveSettings::rmd_jackports();
450 else if (!KdenliveSettings::rmd_alsadevicename().isEmpty())
451 m_captureArgs << "-device" << KdenliveSettings::rmd_alsadevicename();
452 } else m_captureArgs << "--no-sound";
454 if (KdenliveSettings::rmd_fullshots()) m_captureArgs << "--full-shots";
455 m_captureArgs << "-fps" << QString::number(KdenliveSettings::rmd_fps()) << "-o" << m_captureFile.path();
456 captureProcess->start(KdenliveSettings::rmd_path(), m_captureArgs);
457 kDebug() << "// RecordMyDesktop params: " << m_captureArgs;
464 if (ui.device_selector->currentIndex() != SCREENGRAB) {
465 m_isCapturing = true;
466 kDebug() << "Capture: Running ffplay " << m_displayArgs.join(" ");
467 displayProcess->start("ffplay", m_displayArgs);
468 ui.video_frame->setText(i18n("Initialising..."));
472 displayProcess->kill();
473 //captureProcess->kill();
474 QTimer::singleShot(1000, this, SLOT(slotRecord()));
479 void RecMonitor::slotStartGrab(const QRect &rect) {
480 rgnGrab->deleteLater();
481 QApplication::restoreOverrideCursor();
483 if (rect.isNull()) return;
484 int width = rect.width();
485 int height = rect.height();
486 if (width % 2 != 0) width--;
487 if (height % 2 != 0) height--;
488 QString args = KdenliveSettings::screengrabcapture().replace("%size", QString::number(width) + "x" + QString::number(height)).replace("%offset", "+" + QString::number(rect.x()) + "," + QString::number(rect.y()));
489 if (KdenliveSettings::screengrabenableaudio()) {
490 // also capture audio
491 if (KdenliveSettings::useosscapture()) m_captureArgs << KdenliveSettings::screengrabosscapture().simplified().split(' ');
492 else m_captureArgs << KdenliveSettings::screengrabalsacapture2().simplified().split(' ');
494 m_captureArgs << args.simplified().split(' ') << KdenliveSettings::screengrabencoding().simplified().split(' ') << m_captureFile.path();
495 m_isCapturing = true;
496 ui.video_frame->setText(i18n("Capturing..."));
497 if (KdenliveSettings::screengrabenableaudio() && !KdenliveSettings::useosscapture()) {
498 QStringList alsaArgs = KdenliveSettings::screengrabalsacapture().simplified().split(' ');
499 alsaProcess->setStandardOutputProcess(captureProcess);
500 kDebug() << "Capture: Running arecord " << alsaArgs.join(" ");
501 alsaProcess->start("arecord", alsaArgs);
503 kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
504 captureProcess->start("ffmpeg", m_captureArgs);
507 void RecMonitor::slotProcessStatus(QProcess::ProcessState status) {
508 if (status == QProcess::NotRunning) {
509 displayProcess->kill();
510 if (m_isCapturing && ui.device_selector->currentIndex() != FIREWIRE)
511 if (ui.autoaddbox->isChecked() && QFile::exists(m_captureFile.path())) emit addProjectClip(m_captureFile);
512 if (ui.device_selector->currentIndex() == FIREWIRE) {
513 m_discAction->setIcon(KIcon("network-connect"));
514 m_discAction->setText(i18n("Connect"));
515 m_playAction->setEnabled(false);
516 m_rewAction->setEnabled(false);
517 m_fwdAction->setEnabled(false);
518 m_recAction->setEnabled(false);
521 m_playAction->setIcon(m_playIcon);
522 m_recAction->setChecked(false);
523 m_stopAction->setEnabled(false);
524 ui.device_selector->setEnabled(true);
525 if (captureProcess && captureProcess->exitStatus() == QProcess::CrashExit) {
526 ui.video_frame->setText(i18n("Capture crashed, please check your parameters"));
528 if (ui.device_selector->currentIndex() != SCREENGRAB) ui.video_frame->setText(i18n("Not connected"));
529 else 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())));
531 m_isCapturing = false;
533 if (ui.device_selector->currentIndex() != SCREENGRAB) m_stopAction->setEnabled(true);
534 ui.device_selector->setEnabled(false);
538 void RecMonitor::manageCapturedFiles() {
540 switch (KdenliveSettings::firewireformat()) {
552 QDir dir(KdenliveSettings::capturefolder());
554 filters << "capture*" + extension;
555 const QStringList result = dir.entryList(filters, QDir::Files, QDir::Time);
556 KUrl::List capturedFiles;
557 foreach(QString name, result) {
558 KUrl url = KUrl(dir.filePath(name));
559 if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, this)) {
560 KFileItem file(KFileItem::Unknown, KFileItem::Unknown, url, true);
561 if (file.time(KFileItem::ModificationTime) > m_captureTime) capturedFiles.append(url);
564 kDebug() << "Found : " << capturedFiles.count() << " new capture files";
565 kDebug() << capturedFiles;
567 if (capturedFiles.count() > 0) {
568 ManageCapturesDialog *d = new ManageCapturesDialog(capturedFiles, this);
569 if (d->exec() == QDialog::Accepted) {
570 capturedFiles = d->importFiles();
571 foreach(KUrl url, capturedFiles) {
572 emit addProjectClip(url);
580 void RecMonitor::mousePressEvent(QMouseEvent * event) {
584 void RecMonitor::activateRecMonitor() {
585 //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
588 void RecMonitor::stop() {
593 void RecMonitor::start() {
598 void RecMonitor::refreshRecMonitor(bool visible) {
600 //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
605 void RecMonitor::slotPlay() {
607 //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
612 #include "recmonitor.moc"