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 "recmonitor.h"
23 #include "mltdevicecapture.h"
24 #include "kdenlivesettings.h"
25 #include "managecapturesdialog.h"
26 #include "monitormanager.h"
28 #include "profilesdialog.h"
32 #include <KStandardDirs>
34 #include <KIO/NetAccess>
36 #include <KMessageBox>
37 #include <KApplication>
38 #include <KDiskFreeSpaceInfo>
40 #include <QMouseEvent>
42 #include <QToolButton>
45 #include <QDesktopWidget>
48 RecMonitor::RecMonitor(Kdenlive::MONITORID name, MonitorManager *manager, QWidget *parent) :
49 AbstractMonitor(name, manager, parent),
53 m_captureDevice(NULL),
58 //video_frame->setAttribute(Qt::WA_PaintOnScreen);
59 device_selector->setCurrentIndex(KdenliveSettings::defaultcapture());
60 connect(device_selector, SIGNAL(currentIndexChanged(int)), this, SLOT(slotVideoDeviceChanged(int)));
61 // Video widget holder
62 QVBoxLayout *l = new QVBoxLayout;
63 l->setContentsMargins(0, 0, 0, 0);
65 l->addWidget(videoBox, 10);
66 video_frame->setLayout(l);
69 QToolBar *toolbar = new QToolBar(this);
70 QHBoxLayout *hlayout = new QHBoxLayout;
71 hlayout->setContentsMargins(0, 0, 0, 0);
72 m_playIcon = KIcon("media-playback-start");
73 m_pauseIcon = KIcon("media-playback-pause");
75 m_discAction = toolbar->addAction(KIcon("network-connect"), i18n("Connect"));
76 connect(m_discAction, SIGNAL(triggered()), this, SLOT(slotDisconnect()));
78 m_rewAction = toolbar->addAction(KIcon("media-seek-backward"), i18n("Rewind"));
79 connect(m_rewAction, SIGNAL(triggered()), this, SLOT(slotRewind()));
81 m_playAction = toolbar->addAction(m_playIcon, i18n("Play"));
82 connect(m_playAction, SIGNAL(triggered()), this, SLOT(slotStartPreview()));
84 m_stopAction = toolbar->addAction(KIcon("media-playback-stop"), i18n("Stop"));
85 connect(m_stopAction, SIGNAL(triggered()), this, SLOT(slotStopCapture()));
86 m_stopAction->setEnabled(false);
87 m_fwdAction = toolbar->addAction(KIcon("media-seek-forward"), i18n("Forward"));
88 connect(m_fwdAction, SIGNAL(triggered()), this, SLOT(slotForward()));
90 m_recAction = toolbar->addAction(KIcon("media-record"), i18n("Record"));
91 connect(m_recAction, SIGNAL(triggered()), this, SLOT(slotRecord()));
92 m_recAction->setCheckable(true);
94 rec_options->setIcon(KIcon("system-run"));
95 QMenu *menu = new QMenu(this);
96 m_addCapturedClip = new QAction(i18n("Add Captured File to Project"), this);
97 m_addCapturedClip->setCheckable(true);
98 m_addCapturedClip->setChecked(true);
99 menu->addAction(m_addCapturedClip);
101 rec_audio->setChecked(KdenliveSettings::v4l_captureaudio());
102 rec_video->setChecked(KdenliveSettings::v4l_capturevideo());
104 m_previewSettings = new QAction(i18n("Recording Preview"), this);
105 m_previewSettings->setCheckable(true);
107 rec_options->setMenu(menu);
108 menu->addAction(m_previewSettings);
110 toolbar->addSeparator();
112 QAction *configAction = toolbar->addAction(KIcon("configure"), i18n("Configure"));
113 connect(configAction, SIGNAL(triggered()), this, SLOT(slotConfigure()));
114 configAction->setCheckable(false);
116 hlayout->addWidget(toolbar);
117 hlayout->addWidget(&m_logger);
118 hlayout->addWidget(&m_dvinfo);
119 m_logger.setMaxCount(10);
120 m_logger.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
121 m_logger.setFrame(false);
122 //m_logger.setInsertPolicy(QComboBox::InsertAtTop);
124 m_freeSpace = new KCapacityBar(KCapacityBar::DrawTextInline, this);
125 m_freeSpace->setMaximumWidth(150);
126 QFontMetricsF fontMetrics(font());
127 m_freeSpace->setMaximumHeight(fontMetrics.height() * 1.2);
128 slotUpdateFreeSpace();
129 hlayout->addWidget(m_freeSpace);
130 connect(&m_spaceTimer, SIGNAL(timeout()), this, SLOT(slotUpdateFreeSpace()));
131 m_spaceTimer.setInterval(30000);
132 m_spaceTimer.setSingleShot(false);
134 control_frame_firewire->setLayout(hlayout);
135 m_displayProcess = new QProcess;
136 m_captureProcess = new QProcess;
138 connect(m_captureProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(slotProcessStatus(QProcess::ProcessState)));
139 connect(m_captureProcess, SIGNAL(readyReadStandardError()), this, SLOT(slotReadProcessInfo()));
141 QString videoDriver = KdenliveSettings::videodrivername();
142 #if QT_VERSION >= 0x040600
143 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
144 env.insert("SDL_WINDOWID", QString::number(videoSurface->winId()));
145 if (!videoDriver.isEmpty()) {
146 if (videoDriver == "x11_noaccel") {
147 env.insert("SDL_VIDEO_YUV_HWACCEL", "0");
148 env.insert("SDL_VIDEODRIVER", "x11");
149 } else env.insert("SDL_VIDEODRIVER", videoDriver);
151 m_displayProcess->setProcessEnvironment(env);
153 QStringList env = QProcess::systemEnvironment();
154 env << "SDL_WINDOWID=" + QString::number(videoSurface->winId());
155 if (!videoDriver.isEmpty()) {
156 if (videoDriver == "x11_noaccel") {
157 env << "SDL_VIDEO_YUV_HWACCEL=0";
158 env << "SDL_VIDEODRIVER=x11";
159 } else env << "SDL_VIDEODRIVER=" + videoDriver;
161 m_displayProcess->setEnvironment(env);
164 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 1);
166 kDebug() << "/////// BUILDING MONITOR, ID: " << videoSurface->winId();
167 #if KDE_IS_VERSION(4,7,0)
168 m_infoMessage = new KMessageWidget;
169 QVBoxLayout *s = static_cast <QVBoxLayout *> (layout());
170 s->insertWidget(1, m_infoMessage);
171 m_infoMessage->hide();
174 slotVideoDeviceChanged(device_selector->currentIndex());
175 m_previewSettings->setChecked(KdenliveSettings::enable_recording_preview());
176 connect(m_previewSettings, SIGNAL(triggered(bool)), this, SLOT(slotChangeRecordingPreview(bool)));
179 RecMonitor::~RecMonitor()
182 delete m_captureProcess;
183 delete m_displayProcess;
184 if (m_captureDevice) delete m_captureDevice;
187 void RecMonitor::mouseDoubleClickEvent(QMouseEvent * event)
189 if (!KdenliveSettings::openglmonitors() && videoBox && videoBox->isVisible()) {
190 videoBox->switchFullScreen();
195 void RecMonitor::slotSwitchFullScreen()
197 videoBox->switchFullScreen();
200 void RecMonitor::stop()
202 // Special case: when recording audio only, do not stop so that we can do voiceover.
203 if (device_selector->currentIndex() == SCREENGRAB || (device_selector->currentIndex() == VIDEO4LINUX && !rec_video->isChecked())) return;
207 void RecMonitor::start()
209 //slotStartPreview(true);
212 void RecMonitor::slotConfigure()
214 emit showConfigDialog(4, device_selector->currentIndex());
217 void RecMonitor::slotUpdateCaptureFolder(const QString ¤tProjectFolder)
219 if (KdenliveSettings::capturetoprojectfolder()) m_capturePath = currentProjectFolder;
220 else m_capturePath = KdenliveSettings::capturefolder();
221 if (m_captureProcess) m_captureProcess->setWorkingDirectory(m_capturePath);
222 if (m_captureProcess->state() != QProcess::NotRunning) {
223 if (device_selector->currentIndex() == FIREWIRE)
224 KMessageBox::information(this, i18n("You need to disconnect and reconnect in the capture monitor to apply your changes"), i18n("Capturing"));
225 else KMessageBox::information(this, i18n("You need to stop capture before your changes can be applied"), i18n("Capturing"));
226 } else slotVideoDeviceChanged(device_selector->currentIndex());
228 // update free space info
229 slotUpdateFreeSpace();
232 void RecMonitor::slotVideoDeviceChanged(int ix)
236 m_infoMessage->animatedHide();
237 m_previewSettings->setEnabled(ix == VIDEO4LINUX || ix == BLACKMAGIC);
238 control_frame->setVisible(ix == VIDEO4LINUX);
239 m_playAction->setVisible(ix != SCREENGRAB);
240 m_fwdAction->setVisible(ix == FIREWIRE);
241 m_discAction->setVisible(ix == FIREWIRE);
242 m_rewAction->setVisible(ix == FIREWIRE);
243 m_recAction->setEnabled(ix != FIREWIRE);
244 m_logger.setVisible(ix == BLACKMAGIC);
245 if (m_captureDevice) {
246 // MLT capture still running, abort
247 m_monitorManager->clearScopeSource();
248 m_captureDevice->stop();
249 delete m_captureDevice;
250 m_captureDevice = NULL;
253 // The m_videoBox container has to be shown once before the MLT consumer is build, or preview will fail
254 videoBox->setHidden(ix != VIDEO4LINUX && ix != BLACKMAGIC);
255 videoBox->setHidden(true);
258 m_discAction->setEnabled(false);
259 m_rewAction->setEnabled(false);
260 m_fwdAction->setEnabled(false);
261 m_stopAction->setEnabled(false);
262 m_playAction->setEnabled(false);
263 if (KdenliveSettings::ffmpegpath().isEmpty()) {
264 QString exepath = KStandardDirs::findExe("ffmpeg");
265 if (exepath.isEmpty()) {
266 // Check for libav version
267 exepath = KStandardDirs::findExe("avconv");
269 if (exepath.isEmpty()) showMessage(i18n("ffmpeg or avconv not found,\n please install it for screen grabs"), "dialog-warning");
270 else KdenliveSettings::setFfmpegpath(exepath);
272 if (!KdenliveSettings::ffmpegpath().isEmpty()) {
273 if (!Render::checkX11Grab()) {
274 // FFmpeg does not support screen grab
275 showMessage("dialog-warning", i18n("Your FFmpeg / Libav installation\n does not support screen grab"));
276 m_recAction->setEnabled(false);
278 else 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", m_capturePath)));
280 //video_frame->setText(i18n("Press record button\nto start screen capture"));
283 m_stopAction->setEnabled(false);
284 m_playAction->setEnabled(true);
285 checkDeviceAvailability();
288 m_stopAction->setEnabled(false);
289 m_playAction->setEnabled(true);
290 capturefile = m_capturePath;
291 if (!capturefile.endsWith('/')) capturefile.append('/');
292 capturename = KdenliveSettings::decklink_filename();
293 capturename.append("xxx.");
294 capturename.append(KdenliveSettings::decklink_extension());
295 capturefile.append(capturename);
296 video_frame->setPixmap(mergeSideBySide(KIcon("camera-photo").pixmap(QSize(50, 50)), i18n("Plug your camcorder and\npress play button\nto start preview.\nFiles will be saved in:\n%1", capturefile)));
299 m_discAction->setEnabled(true);
300 m_stopAction->setEnabled(false);
301 m_playAction->setEnabled(false);
302 m_rewAction->setEnabled(false);
303 m_fwdAction->setEnabled(false);
305 // Check that dvgab is available
306 if (KdenliveSettings::dvgrab_path().isEmpty()) {
307 QString dvgrabpath = KStandardDirs::findExe("dvgrab");
308 if (dvgrabpath.isEmpty()) {
309 showMessage(i18n("dvgrab utility not found,\n please install it for firewire capture"), "dialog-warning");
311 else KdenliveSettings::setDvgrab_path(dvgrabpath);
314 capturefile = m_capturePath;
315 if (!capturefile.endsWith('/')) capturefile.append('/');
316 capturename = KdenliveSettings::dvgrabfilename();
317 if (capturename.isEmpty()) capturename = "capture";
319 switch (KdenliveSettings::firewireformat()) {
331 capturename.append("xxx" + extension);
332 capturefile.append(capturename);
333 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", capturefile)));
339 void RecMonitor::slotSetInfoMessage(const QString &message)
341 m_logger.insertItem(0, message);
344 QPixmap RecMonitor::mergeSideBySide(const QPixmap& pix, const QString &txt)
347 QRect r = QApplication::fontMetrics().boundingRect(QRect(0, 0, video_frame->width(), video_frame->height()), Qt::AlignLeft, txt);
348 int strWidth = r.width();
349 int strHeight = r.height();
350 int pixWidth = pix.width();
351 int pixHeight = pix.height();
352 QPixmap res(strWidth + 8 + pixWidth, qMax(strHeight, pixHeight));
353 res.fill(Qt::transparent);
355 p.drawPixmap(0, 0, pix);
356 p.setPen(kapp->palette().text().color());
357 p.drawText(QRect(pixWidth + 8, 0, strWidth, strHeight), 0, txt);
363 void RecMonitor::checkDeviceAvailability()
365 if (!KIO::NetAccess::exists(KUrl(KdenliveSettings::video4vdevice()), KIO::NetAccess::SourceSide , this)) {
366 rec_video->setChecked(false);
367 rec_video->setEnabled(false);
368 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())));
370 rec_video->setEnabled(true);
371 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", m_capturePath)));
375 void RecMonitor::slotDisconnect()
377 if (m_captureProcess->state() == QProcess::NotRunning) {
378 m_captureTime = KDateTime::currentLocalDateTime();
379 kDebug() << "CURRENT TIME: " << m_captureTime.toString();
380 m_didCapture = false;
381 slotStartPreview(false);
382 m_discAction->setIcon(KIcon("network-disconnect"));
383 m_discAction->setText(i18n("Disconnect"));
384 m_recAction->setEnabled(true);
385 m_stopAction->setEnabled(true);
386 m_playAction->setEnabled(true);
387 m_rewAction->setEnabled(true);
388 m_fwdAction->setEnabled(true);
390 m_captureProcess->write("q", 1);
391 QTimer::singleShot(1000, m_captureProcess, SLOT(kill()));
392 if (m_didCapture) manageCapturedFiles();
393 m_didCapture = false;
397 void RecMonitor::slotRewind()
399 m_captureProcess->write("a", 1);
402 void RecMonitor::slotForward()
404 m_captureProcess->write("z", 1);
407 void RecMonitor::slotStopCapture()
410 if (!m_isCapturing && !m_isPlaying) return;
411 videoBox->setHidden(true);
412 control_frame->setEnabled(true);
413 slotActivateMonitor();
414 switch (device_selector->currentIndex()) {
416 m_captureProcess->write("\e", 2);
417 m_playAction->setIcon(m_playIcon);
421 m_captureProcess->write("q\n", 3);
422 m_captureProcess->terminate();
423 //video_frame->setText(i18n("Encoding captured video..."));
424 QTimer::singleShot(1000, m_captureProcess, SLOT(kill()));
428 if (m_captureDevice) {
429 m_captureDevice->stop();
431 m_previewSettings->setEnabled(true);
432 m_isCapturing = false;
434 m_playAction->setEnabled(true);
435 m_stopAction->setEnabled(false);
436 m_recAction->setEnabled(true);
437 slotSetInfoMessage(i18n("Capture stopped"));
438 m_isCapturing = false;
439 m_recAction->setChecked(false);
440 if (m_addCapturedClip->isChecked() && !m_captureFile.isEmpty() && QFile::exists(m_captureFile.path())) {
441 emit addProjectClip(m_captureFile);
442 m_captureFile.clear();
450 void RecMonitor::slotStartPreview(bool play)
452 if (m_captureProcess->state() != QProcess::NotRunning) {
453 if (device_selector->currentIndex() == FIREWIRE) {
454 videoBox->setHidden(false);
456 m_captureProcess->write("k", 1);
457 //captureProcess->write("\e", 2);
458 m_playAction->setIcon(m_playIcon);
461 m_captureProcess->write("p", 1);
462 m_playAction->setIcon(m_pauseIcon);
468 slotActivateMonitor();
469 if (m_isPlaying) return;
470 m_captureArgs.clear();
471 m_displayArgs.clear();
473 QString capturename = KdenliveSettings::dvgrabfilename();
475 MltVideoProfile profile;
477 QStringList dvargs = KdenliveSettings::dvgrabextra().simplified().split(' ', QString::SkipEmptyParts);
478 int ix = device_selector->currentIndex();
480 videoBox->setHidden(ix != VIDEO4LINUX && ix != BLACKMAGIC && ix != FIREWIRE);
483 switch (KdenliveSettings::firewireformat()) {
486 m_captureArgs << "--format" << "raw";
487 m_displayArgs << "-f" << "dv";
491 m_captureArgs << "--format" << "dv1";
492 m_displayArgs << "-f" << "dv";
496 m_captureArgs << "--format" << "dv2";
497 m_displayArgs << "-f" << "dv";
501 m_captureArgs << "--format" << "hdv";
502 m_displayArgs << "-f" << "mpegts";
505 if (KdenliveSettings::firewireautosplit()) m_captureArgs << "--autosplit";
506 if (KdenliveSettings::firewiretimestamp()) m_captureArgs << "--timestamp";
507 if (!dvargs.isEmpty()) {
508 m_captureArgs << dvargs;
510 m_captureArgs << "-i";
511 if (capturename.isEmpty()) capturename = "capture";
512 m_captureArgs << capturename << "-";
514 m_displayArgs << "-x" << QString::number(video_frame->width()) << "-y" << QString::number(video_frame->height()) << "-noframedrop" << "-";
516 m_captureProcess->setStandardOutputProcess(m_displayProcess);
517 m_captureProcess->setWorkingDirectory(m_capturePath);
518 kDebug() << "Capture: Running dvgrab " << m_captureArgs.join(" ");
520 m_captureProcess->start(KdenliveSettings::dvgrab_path(), m_captureArgs);
521 if (play) m_captureProcess->write(" ", 1);
522 m_discAction->setEnabled(true);
525 path = KStandardDirs::locateLocal("appdata", "profiles/video4linux");
526 buildMltDevice(path);
527 profile = ProfilesDialog::getVideoProfile(path);
528 producer = getV4lXmlPlaylist(profile, &isXml);
530 //producer = QString("avformat-novalidate:video4linux2:%1?width:%2&height:%3&frame_rate:%4").arg(KdenliveSettings::video4vdevice()).arg(profile.width).arg(profile.height).arg((double) profile.frame_rate_num / profile.frame_rate_den);
531 if (!m_captureDevice->slotStartPreview(producer, isXml)) {
532 // v4l capture failed to start
533 video_frame->setText(i18n("Failed to start Video4Linux,\ncheck your parameters..."));
534 videoBox->setHidden(true);
537 m_playAction->setEnabled(false);
538 m_stopAction->setEnabled(true);
544 path = KdenliveSettings::current_profile();
545 slotActivateMonitor();
546 buildMltDevice(path);
547 producer = QString("decklink:%1").arg(KdenliveSettings::decklink_capturedevice());
548 if (!m_captureDevice->slotStartPreview(producer)) {
549 // v4l capture failed to start
550 video_frame->setText(i18n("Failed to start Decklink,\ncheck your parameters..."));
551 videoBox->setHidden(true);
554 m_playAction->setEnabled(false);
555 m_stopAction->setEnabled(true);
563 control_frame->setEnabled(false);
565 if (device_selector->currentIndex() == FIREWIRE) {
566 kDebug() << "Capture: Running ffplay " << m_displayArgs.join(" ");
567 m_displayProcess->start(KdenliveSettings::ffplaypath(), m_displayArgs);
568 //video_frame->setText(i18n("Initialising..."));
570 // do something when starting screen grab
574 void RecMonitor::slotRecord()
577 if (m_captureProcess->state() == QProcess::NotRunning && device_selector->currentIndex() == FIREWIRE) {
581 // User stopped capture
584 } else if (device_selector->currentIndex() == FIREWIRE) {
585 m_isCapturing = true;
587 m_captureProcess->write("c\n", 3);
588 m_spaceTimer.start();
591 if (m_captureProcess->state() == QProcess::NotRunning) {
593 m_recAction->setChecked(true);
594 QString extension = "mpg";
595 if (device_selector->currentIndex() == SCREENGRAB) {
596 extension = KdenliveSettings::grab_extension();
598 else if (device_selector->currentIndex() == VIDEO4LINUX) {
599 // TODO: when recording audio only, allow configuration?
600 if (!rec_video->isChecked()) extension = "wav";
601 else extension = KdenliveSettings::v4l_extension();
603 else if (device_selector->currentIndex() == BLACKMAGIC) extension = KdenliveSettings::decklink_extension();
604 QString path = KUrl(m_capturePath).path(KUrl::AddTrailingSlash) + "capture0000." + extension;
606 while (QFile::exists(path)) {
607 QString num = QString::number(i).rightJustified(4, '0', false);
608 path = KUrl(m_capturePath).path(KUrl::AddTrailingSlash) + "capture" + num + '.' + extension;
611 m_captureFile = KUrl(path);
613 m_captureArgs.clear();
614 m_displayArgs.clear();
617 QString v4lparameters;
618 QStringList grabParameters;
619 MltVideoProfile profile;
623 QRect screenSize = QApplication::desktop()->screenGeometry();
624 QString capturename = KdenliveSettings::dvgrabfilename();
625 if (capturename.isEmpty()) capturename = "capture";
627 switch (device_selector->currentIndex()) {
629 if (rec_video->isChecked()) slotActivateMonitor();
630 path = KStandardDirs::locateLocal("appdata", "profiles/video4linux");
631 profile = ProfilesDialog::getVideoProfile(path);
632 //m_videoBox->setRatio((double) profile.display_aspect_num / profile.display_aspect_den);
633 buildMltDevice(path);
634 playlist = getV4lXmlPlaylist(profile, &isXml);
636 v4lparameters = KdenliveSettings::v4l_parameters();
638 // TODO: when recording audio only, allow param configuration?
639 if (!rec_video->isChecked()) v4lparameters.clear();
641 // Add alsa audio capture
642 if (!rec_audio->isChecked()) {
643 // if we do not want audio, make sure that we don't have audio encoding parameters
644 // this is required otherwise the MLT avformat consumer will not close properly
645 if (v4lparameters.contains("acodec")) {
646 QString endParam = v4lparameters.section("acodec", 1);
647 int vcodec = endParam.indexOf(" vcodec");
648 int format = endParam.indexOf(" f=");
649 int cutPosition = -1;
652 cutPosition = qMin(vcodec, format);
654 else cutPosition = vcodec;
656 else if (format > -1) {
657 cutPosition = format;
660 // nothing interesting in end params
663 if (cutPosition > -1) {
664 endParam.remove(0, cutPosition);
666 v4lparameters = QString(v4lparameters.section("acodec", 0, 0) + "an=1 " + endParam).simplified();
670 showPreview = m_previewSettings->isChecked();
671 if (!rec_video->isChecked()) showPreview = false;
673 if (m_captureDevice->slotStartCapture(v4lparameters, m_captureFile.path(), playlist, showPreview, isXml)) {
674 videoBox->setHidden(false);
675 m_isCapturing = true;
676 m_recAction->setEnabled(false);
677 m_stopAction->setEnabled(true);
678 m_previewSettings->setEnabled(false);
679 control_frame->setEnabled(false);
682 video_frame->setText(i18n("Failed to start Video4Linux,\ncheck your parameters..."));
683 videoBox->setHidden(true);
684 m_recAction->blockSignals(true);
685 m_recAction->setChecked(false);
686 m_recAction->blockSignals(false);
687 m_isCapturing = false;
692 slotActivateMonitor();
693 path = KdenliveSettings::current_profile();
694 profile = ProfilesDialog::getVideoProfile(path);
695 //m_videoBox->setRatio((double) profile.display_aspect_num / profile.display_aspect_den);
696 buildMltDevice(path);
698 playlist = QString("<producer id=\"producer0\" in=\"0\" out=\"99999\"><property name=\"mlt_type\">producer</property><property name=\"length\">100000</property><property name=\"eof\">pause</property><property name=\"resource\">%1</property><property name=\"mlt_service\">decklink</property></producer>").arg(KdenliveSettings::decklink_capturedevice());
700 if (m_captureDevice->slotStartCapture(KdenliveSettings::decklink_parameters(), m_captureFile.path(), QString("decklink:%1").arg(KdenliveSettings::decklink_capturedevice()), m_previewSettings->isChecked(), false)) {
701 videoBox->setHidden(false);
702 m_isCapturing = true;
703 slotSetInfoMessage(i18n("Capturing to %1", m_captureFile.fileName()));
704 m_recAction->setEnabled(false);
705 m_stopAction->setEnabled(true);
706 m_previewSettings->setEnabled(false);
709 video_frame->setText(i18n("Failed to start Decklink,\ncheck your parameters..."));
710 slotSetInfoMessage(i18n("Failed to start capture"));
711 videoBox->setHidden(true);
712 m_isCapturing = false;
717 m_captureArgs << "-f" << "x11grab";
718 if (KdenliveSettings::grab_follow_mouse()) m_captureArgs << "-follow_mouse" << "centered";
719 if (!KdenliveSettings::grab_hide_frame()) m_captureArgs << "-show_region" << "1";
720 if (KdenliveSettings::grab_capture_type() == 0) {
721 // Full screen capture
722 captureSize = ":0.0";
723 m_captureArgs << "-s" << QString::number(screenSize.width()) + "x" + QString::number(screenSize.height());
726 m_captureArgs << "-s" << QString::number(KdenliveSettings::grab_width()) + "x" + QString::number(KdenliveSettings::grab_height());
727 captureSize = ":" + QString::number(KdenliveSettings::grab_offsetx()) + "." + QString::number(KdenliveSettings::grab_offsetx());
730 m_captureArgs << "-r" << QString::number(KdenliveSettings::grab_fps());
731 if (KdenliveSettings::grab_hide_mouse()) captureSize.append("+nomouse");
732 m_captureArgs << "-i" << captureSize;
733 grabParameters = KdenliveSettings::grab_parameters().simplified().split(" ");
734 m_captureArgs << grabParameters;
735 m_captureArgs << path;
737 m_isCapturing = true;
738 m_recAction->setEnabled(false);
739 /*if (KdenliveSettings::rmd_capture_audio()) {
740 m_captureArgs << "--freq" << KdenliveSettings::rmd_freq();
741 m_captureArgs << "--channels" << QString::number(KdenliveSettings::rmd_audio_channels());
742 if (KdenliveSettings::rmd_use_jack()) {
743 m_captureArgs << "--use-jack";
744 QStringList ports = KdenliveSettings::rmd_jackports().split(' ', QString::SkipEmptyParts);
745 for (int i = 0; i < ports.count(); ++i) {
746 m_captureArgs << ports.at(i);
748 if (KdenliveSettings::rmd_jack_buffer() > 0.0)
749 m_captureArgs << "--ring-buffer-size" << QString::number(KdenliveSettings::rmd_jack_buffer());
751 if (!KdenliveSettings::rmd_alsadevicename().isEmpty())
752 m_captureArgs << "--device" << KdenliveSettings::rmd_alsadevicename();
753 if (KdenliveSettings::rmd_alsa_buffer() > 0)
754 m_captureArgs << "--buffer-size" << QString::number(KdenliveSettings::rmd_alsa_buffer());
756 } else m_captureArgs << "--no-sound";*/
758 m_captureProcess->start(KdenliveSettings::ffmpegpath(), m_captureArgs);
759 if (!m_captureProcess->waitForStarted()) {
760 // Problem launching capture app
761 showMessage(i18n("Failed to start the capture application:\n%1", KdenliveSettings::ffmpegpath()), "dialog-warning");
763 //kDebug() << "// Screen grab params: " << m_captureArgs;
770 if (device_selector->currentIndex() == FIREWIRE) {
771 m_isCapturing = true;
772 kDebug() << "Capture: Running ffplay " << m_displayArgs.join(" ");
773 m_displayProcess->start(KdenliveSettings::ffplaypath(), m_displayArgs);
774 video_frame->setText(i18n("Initialising..."));
778 m_displayProcess->kill();
779 //captureProcess->kill();
780 QTimer::singleShot(1000, this, SLOT(slotRecord()));
785 void RecMonitor::showMessage(const QString &text, const QString &icon, bool logAction)
787 #if KDE_IS_VERSION(4,7,0)
788 m_infoMessage->setText(text);
789 m_infoMessage->setMessageType(KMessageWidget::Warning);
791 QAction *manualAction = new QAction(i18n("Show log"), this);
792 connect(manualAction, SIGNAL(triggered()), this, SLOT(slotShowLog()));
793 m_infoMessage->addAction(manualAction);
795 m_infoMessage->animatedShow();
798 video_frame->setPixmap(mergeSideBySide(KIcon(icon).pixmap(QSize(50, 50)), text));
802 video_frame->setText(QString("<qt>" + text + "<br><a href=\"http://kde.org\">" + i18n("Show log") + "</a>"));
803 connect(video_frame, SIGNAL(linkActivated (const QString &)), this, SLOT(slotShowLog()));
808 const QString RecMonitor::getV4lXmlPlaylist(MltVideoProfile profile, bool *isXml)
811 if (rec_video->isChecked() && rec_audio->isChecked()) {
812 // We want to capture audio and video, use xml playlist
814 playlist = QString("<mlt title=\"capture\" LC_NUMERIC=\"C\"><profile description=\"v4l\" width=\"%1\" height=\"%2\" progressive=\"%3\" sample_aspect_num=\"%4\" sample_aspect_den=\"%5\" display_aspect_num=\"%6\" display_aspect_den=\"%7\" frame_rate_num=\"%8\" frame_rate_den=\"%9\" colorspace=\"%10\"/>").arg(profile.width).arg(profile.height).arg(profile.progressive).arg(profile.sample_aspect_num).arg(profile.sample_aspect_den).arg(profile.display_aspect_num).arg(profile.display_aspect_den).arg(profile.frame_rate_num).arg(profile.frame_rate_den).arg(profile.colorspace);
816 playlist.append(QString("<producer id=\"producer0\" in=\"0\" out=\"999999\"><property name=\"mlt_type\">producer</property><property name=\"length\">1000000</property><property name=\"eof\">loop</property><property name=\"resource\">video4linux2:%1?width:%2&height:%3&frame_rate:%4</property><property name=\"mlt_service\">avformat-novalidate</property></producer><playlist id=\"playlist0\"><entry producer=\"producer0\" in=\"0\" out=\"999999\"/></playlist>").arg(KdenliveSettings::video4vdevice()).arg(profile.width).arg(profile.height).arg((double) profile.frame_rate_num / profile.frame_rate_den));
818 playlist.append(QString("<producer id=\"producer1\" in=\"0\" out=\"999999\"><property name=\"mlt_type\">producer</property><property name=\"length\">1000000</property><property name=\"resource\">alsa:%1?channels=%2</property><property name=\"audio_index\">0</property><property name=\"video_index\">-1</property><property name=\"mlt_service\">avformat-novalidate</property></producer><playlist id=\"playlist1\"><entry producer=\"producer1\" in=\"0\" out=\"999999\"/></playlist>").arg(KdenliveSettings::v4l_alsadevicename()).arg(KdenliveSettings::alsachannels()));
820 playlist.append("<tractor id=\"tractor0\" title=\"video0\" global_feed=\"1\" in=\"0\" out=\"999999\">");
821 playlist.append("<track producer=\"playlist0\"/><track producer=\"playlist1\"/>");
822 playlist.append("</tractor></mlt>");
824 else if (rec_audio->isChecked()) {
825 // Audio only recording
827 playlist =QString("alsa:%1?channels=%2").arg(KdenliveSettings::v4l_alsadevicename()).arg(KdenliveSettings::alsachannels());
830 // Video only recording
832 playlist =QString("video4linux2:%1?width:%2&height:%3&frame_rate:%4").arg(KdenliveSettings::video4vdevice()).arg(profile.width).arg(profile.height).arg((double) profile.frame_rate_num / profile.frame_rate_den);
840 void RecMonitor::slotStartGrab(const QRect &rect) {
841 rgnGrab->deleteLater();
842 QApplication::restoreOverrideCursor();
844 if (rect.isNull()) return;
845 int width = rect.width();
846 int height = rect.height();
847 if (width % 2 != 0) width--;
848 if (height % 2 != 0) height--;
849 QString args = KdenliveSettings::screengrabcapture().replace("%size", QString::number(width) + "x" + QString::number(height)).replace("%offset", "+" + QString::number(rect.x()) + "," + QString::number(rect.y()));
850 if (KdenliveSettings::screengrabenableaudio()) {
851 // also capture audio
852 if (KdenliveSettings::useosscapture()) m_captureArgs << KdenliveSettings::screengrabosscapture().simplified().split(' ');
853 else m_captureArgs << KdenliveSettings::screengrabalsacapture2().simplified().split(' ');
855 m_captureArgs << args.simplified().split(' ') << KdenliveSettings::screengrabencoding().simplified().split(' ') << m_captureFile.path();
856 m_isCapturing = true;
857 video_frame->setText(i18n("Capturing..."));
858 if (KdenliveSettings::screengrabenableaudio() && !KdenliveSettings::useosscapture()) {
859 QStringList alsaArgs = KdenliveSettings::screengrabalsacapture().simplified().split(' ');
860 alsaProcess->setStandardOutputProcess(captureProcess);
861 kDebug() << "Capture: Running arecord " << alsaArgs.join(" ");
862 alsaProcess->start("arecord", alsaArgs);
864 kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
865 captureProcess->start(KdenliveSettings::ffmpegpath(), m_captureArgs);
868 void RecMonitor::slotProcessStatus(QProcess::ProcessState status)
870 kDebug()<<"// STATUS CHANGED: "<<status;
871 if (status == QProcess::NotRunning) {
872 m_displayProcess->kill();
873 if (m_isCapturing && device_selector->currentIndex() != FIREWIRE)
874 if (m_addCapturedClip->isChecked() && !m_captureFile.isEmpty() && QFile::exists(m_captureFile.path())) {
875 emit addProjectClip(m_captureFile);
876 m_captureFile.clear();
878 if (device_selector->currentIndex() == FIREWIRE) {
879 m_discAction->setIcon(KIcon("network-connect"));
880 m_discAction->setText(i18n("Connect"));
881 m_playAction->setEnabled(false);
882 m_rewAction->setEnabled(false);
883 m_fwdAction->setEnabled(false);
884 m_recAction->setEnabled(false);
887 m_recAction->setEnabled(true);
890 m_playAction->setIcon(m_playIcon);
891 m_recAction->setChecked(false);
892 m_stopAction->setEnabled(false);
893 device_selector->setEnabled(true);
894 if (m_captureProcess && m_captureProcess->exitStatus() == QProcess::CrashExit) {
895 showMessage(i18n("Capture crashed, please check your parameters"), "dialog-warning", true);
897 if (device_selector->currentIndex() != SCREENGRAB) {
898 video_frame->setText(i18n("Not connected"));
900 int code = m_captureProcess->exitCode();
901 if (code != 0 && code != 255) {
902 showMessage(i18n("Capture crashed, please check your parameters"), "dialog-warning", true);
904 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", m_capturePath)));
908 m_isCapturing = false;
911 // update free space info
912 slotUpdateFreeSpace();
914 if (device_selector->currentIndex()) m_stopAction->setEnabled(true);
915 device_selector->setEnabled(false);
919 void RecMonitor::manageCapturedFiles()
922 switch (KdenliveSettings::firewireformat()) {
934 QDir dir(m_capturePath);
936 QString capturename = KdenliveSettings::dvgrabfilename();
937 if (capturename.isEmpty()) capturename = "capture";
938 filters << capturename + '*' + extension;
939 const QStringList result = dir.entryList(filters, QDir::Files, QDir::Time);
940 KUrl::List capturedFiles;
941 foreach(const QString & name, result) {
942 KUrl url = KUrl(dir.filePath(name));
943 if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, this)) {
944 KFileItem file(KFileItem::Unknown, KFileItem::Unknown, url, true);
945 if (file.time(KFileItem::ModificationTime) > m_captureTime) {
946 // The file was captured in the last batch
947 if (url.fileName().contains(':')) {
948 // Several dvgrab options (--timecode,...) use : in the file name, which is
949 // not supported by MLT, so rename them
950 QString newUrl = url.directory(KUrl::AppendTrailingSlash) + url.fileName().replace(':', '_');
951 if (QFile::rename(url.path(), newUrl)) {
956 capturedFiles.append(url);
960 kDebug() << "Found : " << capturedFiles.count() << " new capture files";
961 kDebug() << capturedFiles;
963 if (capturedFiles.count() > 0) {
964 QPointer<ManageCapturesDialog> d = new ManageCapturesDialog(capturedFiles, this);
965 if (d->exec() == QDialog::Accepted) {
966 emit addProjectClipList(d->importFiles());
973 void RecMonitor::mousePressEvent(QMouseEvent *event)
975 if (m_freeSpace->underMouse()) slotUpdateFreeSpace();
976 else QWidget::mousePressEvent(event);//m_videoBox->mousePressEvent(event);
979 void RecMonitor::slotUpdateFreeSpace()
981 KDiskFreeSpaceInfo info = KDiskFreeSpaceInfo::freeSpaceInfo(m_capturePath);
982 if (info.isValid() && info.size() > 0) {
983 m_freeSpace->setValue(100 * info.used() / info.size());
984 m_freeSpace->setText(i18n("Free space: %1", KIO::convertSize(info.available())));
985 m_freeSpace->update();
989 void RecMonitor::refreshRecMonitor(bool visible)
992 //if (!m_isActive) activateMonitor();
997 void RecMonitor::slotPlay()
999 /*if (m_isPlaying) slotStopCapture();
1000 else slotStartPreview(true);*/
1003 void RecMonitor::slotReadProcessInfo()
1005 QString data = m_captureProcess->readAllStandardError().simplified();
1006 if (device_selector->currentIndex() == SCREENGRAB) {
1007 m_error.append(data + "\n");
1009 else if (device_selector->currentIndex() == FIREWIRE) {
1010 data = data.section('"', 2, 2).simplified();
1011 m_dvinfo.setText(data.left(11));
1012 m_dvinfo.updateGeometry();
1016 void RecMonitor::slotShowLog()
1018 KMessageBox::information(this, m_error);
1021 AbstractRender *RecMonitor::abstractRender()
1023 return m_captureDevice;
1027 void RecMonitor::analyseFrames(bool analyse)
1029 m_analyse = analyse;
1030 if (m_captureDevice) m_captureDevice->sendFrameForAnalysis = analyse;
1033 void RecMonitor::slotDroppedFrames(int dropped)
1035 slotSetInfoMessage(i18n("%1 dropped frames", dropped));
1038 void RecMonitor::buildMltDevice(const QString &path)
1040 if (m_captureDevice == NULL) {
1041 m_monitorManager->updateScopeSource();
1042 m_captureDevice = new MltDeviceCapture(path, videoSurface, this);
1043 connect(m_captureDevice, SIGNAL(droppedFrames(int)), this, SLOT(slotDroppedFrames(int)));
1044 m_captureDevice->sendFrameForAnalysis = m_analyse;
1045 m_monitorManager->updateScopeSource();
1049 void RecMonitor::slotChangeRecordingPreview(bool enable)
1051 KdenliveSettings::setEnable_recording_preview(enable);
1055 void RecMonitor::slotMouseSeek(int /*eventDelta*/, bool /*fast*/)
1060 #include "recmonitor.moc"