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>
39 #include <QMouseEvent>
41 #include <QToolButton>
46 RecMonitor::RecMonitor(Kdenlive::MONITORID name, MonitorManager *manager, QWidget *parent) :
47 AbstractMonitor(name, manager, parent),
51 m_captureDevice(NULL),
56 //video_frame->setAttribute(Qt::WA_PaintOnScreen);
57 device_selector->setCurrentIndex(KdenliveSettings::defaultcapture());
58 connect(device_selector, SIGNAL(currentIndexChanged(int)), this, SLOT(slotVideoDeviceChanged(int)));
60 // Video widget holder
61 QVBoxLayout *l = new QVBoxLayout;
62 l->setContentsMargins(0, 0, 0, 0);
64 l->addWidget(videoBox, 10);
65 video_frame->setLayout(l);
68 QToolBar *toolbar = new QToolBar(this);
69 QHBoxLayout *layout = new QHBoxLayout;
70 layout->setContentsMargins(0, 0, 0, 0);
71 m_playIcon = KIcon("media-playback-start");
72 m_pauseIcon = KIcon("media-playback-pause");
74 m_discAction = toolbar->addAction(KIcon("network-connect"), i18n("Connect"));
75 connect(m_discAction, SIGNAL(triggered()), this, SLOT(slotDisconnect()));
77 m_rewAction = toolbar->addAction(KIcon("media-seek-backward"), i18n("Rewind"));
78 connect(m_rewAction, SIGNAL(triggered()), this, SLOT(slotRewind()));
80 m_playAction = toolbar->addAction(m_playIcon, i18n("Play"));
81 connect(m_playAction, SIGNAL(triggered()), this, SLOT(slotStartPreview()));
83 m_stopAction = toolbar->addAction(KIcon("media-playback-stop"), i18n("Stop"));
84 connect(m_stopAction, SIGNAL(triggered()), this, SLOT(slotStopCapture()));
85 m_stopAction->setEnabled(false);
86 m_fwdAction = toolbar->addAction(KIcon("media-seek-forward"), i18n("Forward"));
87 connect(m_fwdAction, SIGNAL(triggered()), this, SLOT(slotForward()));
89 m_recAction = toolbar->addAction(KIcon("media-record"), i18n("Record"));
90 connect(m_recAction, SIGNAL(triggered()), this, SLOT(slotRecord()));
91 m_recAction->setCheckable(true);
93 rec_options->setIcon(KIcon("system-run"));
94 QMenu *menu = new QMenu(this);
95 m_addCapturedClip = new QAction(i18n("Add Captured File to Project"), this);
96 m_addCapturedClip->setCheckable(true);
97 m_addCapturedClip->setChecked(true);
98 menu->addAction(m_addCapturedClip);
100 rec_audio->setChecked(KdenliveSettings::v4l_captureaudio());
101 rec_video->setChecked(KdenliveSettings::v4l_capturevideo());
103 m_previewSettings = new QAction(i18n("Recording Preview"), this);
104 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 layout->addWidget(toolbar);
117 layout->addWidget(&m_logger);
118 layout->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 layout->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(layout);
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(slotReadDvgrabInfo()));
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 slotVideoDeviceChanged(device_selector->currentIndex());
168 m_previewSettings->setChecked(KdenliveSettings::enable_recording_preview());
169 connect(m_previewSettings, SIGNAL(triggered(bool)), this, SLOT(slotChangeRecordingPreview(bool)));
172 RecMonitor::~RecMonitor()
175 delete m_captureProcess;
176 delete m_displayProcess;
177 if (m_captureDevice) delete m_captureDevice;
180 void RecMonitor::mouseDoubleClickEvent(QMouseEvent * event)
182 if (!KdenliveSettings::openglmonitors() && videoBox && videoBox->isVisible()) {
183 videoBox->switchFullScreen();
188 void RecMonitor::slotSwitchFullScreen()
190 videoBox->switchFullScreen();
193 void RecMonitor::stop()
198 void RecMonitor::start()
200 //slotStartPreview(true);
203 void RecMonitor::slotConfigure()
205 emit showConfigDialog(4, device_selector->currentIndex());
208 void RecMonitor::slotUpdateCaptureFolder(const QString ¤tProjectFolder)
210 if (KdenliveSettings::capturetoprojectfolder()) m_capturePath = currentProjectFolder;
211 else m_capturePath = KdenliveSettings::capturefolder();
212 if (m_captureProcess) m_captureProcess->setWorkingDirectory(m_capturePath);
213 if (m_captureProcess->state() != QProcess::NotRunning) {
214 if (device_selector->currentIndex() == FIREWIRE)
215 KMessageBox::information(this, i18n("You need to disconnect and reconnect in the capture monitor to apply your changes"), i18n("Capturing"));
216 else KMessageBox::information(this, i18n("You need to stop capture before your changes can be applied"), i18n("Capturing"));
217 } else slotVideoDeviceChanged(device_selector->currentIndex());
219 // update free space info
220 slotUpdateFreeSpace();
223 void RecMonitor::slotVideoDeviceChanged(int ix)
227 m_previewSettings->setEnabled(ix == VIDEO4LINUX || ix == BLACKMAGIC);
228 rec_audio->setVisible(ix == VIDEO4LINUX);
229 rec_video->setVisible(ix == VIDEO4LINUX);
230 m_fwdAction->setVisible(ix == FIREWIRE);
231 m_discAction->setVisible(ix == FIREWIRE);
232 m_rewAction->setVisible(ix == FIREWIRE);
233 m_recAction->setEnabled(ix != FIREWIRE);
234 m_logger.setVisible(ix == BLACKMAGIC);
235 if (m_captureDevice) {
236 // MLT capture still running, abort
237 m_monitorManager->clearScopeSource();
238 m_captureDevice->stop();
239 delete m_captureDevice;
240 m_captureDevice = NULL;
243 // The m_videoBox container has to be shown once before the MLT consumer is build, or preview will fail
244 videoBox->setHidden(ix != VIDEO4LINUX && ix != BLACKMAGIC);
245 videoBox->setHidden(true);
248 m_discAction->setEnabled(false);
249 m_rewAction->setEnabled(false);
250 m_fwdAction->setEnabled(false);
251 m_stopAction->setEnabled(false);
252 m_playAction->setEnabled(false);
253 if (KdenliveSettings::rmd_path().isEmpty()) {
254 QString rmdpath = KStandardDirs::findExe("recordmydesktop");
255 if (rmdpath.isEmpty()) video_frame->setPixmap(mergeSideBySide(KIcon("dialog-warning").pixmap(QSize(50, 50)), i18n("Recordmydesktop utility not found,\n please install it for screen grabs")));
256 else KdenliveSettings::setRmd_path(rmdpath);
258 if (!KdenliveSettings::rmd_path().isEmpty()) 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)));
259 //video_frame->setText(i18n("Press record button\nto start screen capture"));
262 m_stopAction->setEnabled(false);
263 m_playAction->setEnabled(true);
264 checkDeviceAvailability();
267 m_stopAction->setEnabled(false);
268 m_playAction->setEnabled(true);
269 capturefile = m_capturePath;
270 if (!capturefile.endsWith('/')) capturefile.append('/');
271 capturename = KdenliveSettings::decklink_filename();
272 capturename.append("xxx.");
273 capturename.append(KdenliveSettings::decklink_extension());
274 capturefile.append(capturename);
275 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)));
278 m_discAction->setEnabled(true);
279 m_stopAction->setEnabled(false);
280 m_playAction->setEnabled(false);
281 m_rewAction->setEnabled(false);
282 m_fwdAction->setEnabled(false);
284 // Check that dvgab is available
285 if (KdenliveSettings::dvgrab_path().isEmpty()) {
286 QString dvgrabpath = KStandardDirs::findExe("dvgrab");
287 if (dvgrabpath.isEmpty()) video_frame->setPixmap(mergeSideBySide(KIcon("dialog-warning").pixmap(QSize(50, 50)), i18n("dvgrab utility not found,\n please install it for firewire capture")));
288 else KdenliveSettings::setDvgrab_path(dvgrabpath);
291 capturefile = m_capturePath;
292 if (!capturefile.endsWith('/')) capturefile.append('/');
293 capturename = KdenliveSettings::dvgrabfilename();
294 if (capturename.isEmpty()) capturename = "capture";
296 switch (KdenliveSettings::firewireformat()) {
308 capturename.append("xxx" + extension);
309 capturefile.append(capturename);
310 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)));
316 void RecMonitor::slotSetInfoMessage(const QString &message)
318 m_logger.insertItem(0, message);
321 QPixmap RecMonitor::mergeSideBySide(const QPixmap& pix, const QString &txt)
324 QRect r = QApplication::fontMetrics().boundingRect(QRect(0, 0, video_frame->width(), video_frame->height()), Qt::AlignLeft, txt);
325 int strWidth = r.width();
326 int strHeight = r.height();
327 int pixWidth = pix.width();
328 int pixHeight = pix.height();
329 QPixmap res(strWidth + 8 + pixWidth, qMax(strHeight, pixHeight));
330 res.fill(Qt::transparent);
332 p.drawPixmap(0, 0, pix);
333 p.setPen(kapp->palette().text().color());
334 p.drawText(QRect(pixWidth + 8, 0, strWidth, strHeight), 0, txt);
340 void RecMonitor::checkDeviceAvailability()
342 if (!KIO::NetAccess::exists(KUrl(KdenliveSettings::video4vdevice()), KIO::NetAccess::SourceSide , this)) {
343 m_playAction->setEnabled(false);
344 m_recAction->setEnabled(false);
345 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())));
347 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)));
351 void RecMonitor::slotDisconnect()
353 if (m_captureProcess->state() == QProcess::NotRunning) {
354 m_captureTime = KDateTime::currentLocalDateTime();
355 kDebug() << "CURRENT TIME: " << m_captureTime.toString();
356 m_didCapture = false;
357 slotStartPreview(false);
358 m_discAction->setIcon(KIcon("network-disconnect"));
359 m_discAction->setText(i18n("Disconnect"));
360 m_recAction->setEnabled(true);
361 m_stopAction->setEnabled(true);
362 m_playAction->setEnabled(true);
363 m_rewAction->setEnabled(true);
364 m_fwdAction->setEnabled(true);
366 m_captureProcess->write("q", 1);
367 QTimer::singleShot(1000, m_captureProcess, SLOT(kill()));
368 if (m_didCapture) manageCapturedFiles();
369 m_didCapture = false;
373 void RecMonitor::slotRewind()
375 m_captureProcess->write("a", 1);
378 void RecMonitor::slotForward()
380 m_captureProcess->write("z", 1);
383 void RecMonitor::slotStopCapture()
386 if (!m_isCapturing && !m_isPlaying) return;
387 videoBox->setHidden(true);
388 rec_audio->setEnabled(true);
389 rec_video->setEnabled(true);
390 switch (device_selector->currentIndex()) {
392 m_captureProcess->write("\e", 2);
393 m_playAction->setIcon(m_playIcon);
397 m_captureProcess->write("q\n", 3);
398 m_captureProcess->terminate();
399 video_frame->setText(i18n("Encoding captured video..."));
400 QTimer::singleShot(1000, m_captureProcess, SLOT(kill()));
404 if (m_captureDevice) {
405 m_captureDevice->stop();
407 m_previewSettings->setEnabled(true);
408 m_isCapturing = false;
410 m_playAction->setEnabled(true);
411 m_stopAction->setEnabled(false);
412 m_recAction->setEnabled(true);
413 slotSetInfoMessage(i18n("Capture stopped"));
414 m_isCapturing = false;
415 m_recAction->setChecked(false);
416 if (m_addCapturedClip->isChecked() && !m_captureFile.isEmpty() && QFile::exists(m_captureFile.path())) {
417 emit addProjectClip(m_captureFile);
418 m_captureFile.clear();
426 void RecMonitor::slotStartPreview(bool play)
428 if (m_captureProcess->state() != QProcess::NotRunning) {
429 if (device_selector->currentIndex() == FIREWIRE) {
430 videoBox->setHidden(false);
432 m_captureProcess->write("k", 1);
433 //captureProcess->write("\e", 2);
434 m_playAction->setIcon(m_playIcon);
437 m_captureProcess->write("p", 1);
438 m_playAction->setIcon(m_pauseIcon);
444 slotActivateMonitor();
445 if (m_isPlaying) return;
446 m_captureArgs.clear();
447 m_displayArgs.clear();
449 QString capturename = KdenliveSettings::dvgrabfilename();
451 MltVideoProfile profile;
453 QStringList dvargs = KdenliveSettings::dvgrabextra().simplified().split(' ', QString::SkipEmptyParts);
454 int ix = device_selector->currentIndex();
455 videoBox->setHidden(ix != VIDEO4LINUX && ix != BLACKMAGIC && ix != FIREWIRE);
458 switch (KdenliveSettings::firewireformat()) {
461 m_captureArgs << "--format" << "raw";
462 m_displayArgs << "-f" << "dv";
466 m_captureArgs << "--format" << "dv1";
467 m_displayArgs << "-f" << "dv";
471 m_captureArgs << "--format" << "dv2";
472 m_displayArgs << "-f" << "dv";
476 m_captureArgs << "--format" << "hdv";
477 m_displayArgs << "-f" << "mpegts";
480 if (KdenliveSettings::firewireautosplit()) m_captureArgs << "--autosplit";
481 if (KdenliveSettings::firewiretimestamp()) m_captureArgs << "--timestamp";
482 if (!dvargs.isEmpty()) {
483 m_captureArgs << dvargs;
485 m_captureArgs << "-i";
486 if (capturename.isEmpty()) capturename = "capture";
487 m_captureArgs << capturename << "-";
489 m_displayArgs << "-x" << QString::number(video_frame->width()) << "-y" << QString::number(video_frame->height()) << "-noframedrop" << "-";
491 m_captureProcess->setStandardOutputProcess(m_displayProcess);
492 m_captureProcess->setWorkingDirectory(m_capturePath);
493 kDebug() << "Capture: Running dvgrab " << m_captureArgs.join(" ");
495 m_captureProcess->start(KdenliveSettings::dvgrab_path(), m_captureArgs);
496 if (play) m_captureProcess->write(" ", 1);
497 m_discAction->setEnabled(true);
500 path = KStandardDirs::locateLocal("appdata", "profiles/video4linux");
501 buildMltDevice(path);
502 profile = ProfilesDialog::getVideoProfile(path);
503 producer = getV4lXmlPlaylist(profile);
505 //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);
506 if (!m_captureDevice->slotStartPreview(producer, true)) {
507 // v4l capture failed to start
508 video_frame->setText(i18n("Failed to start Video4Linux,\ncheck your parameters..."));
509 videoBox->setHidden(true);
512 m_playAction->setEnabled(false);
513 m_stopAction->setEnabled(true);
519 path = KdenliveSettings::current_profile();
520 slotActivateMonitor();
521 buildMltDevice(path);
522 producer = QString("decklink:%1").arg(KdenliveSettings::decklink_capturedevice());
523 if (!m_captureDevice->slotStartPreview(producer)) {
524 // v4l capture failed to start
525 video_frame->setText(i18n("Failed to start Decklink,\ncheck your parameters..."));
526 videoBox->setHidden(true);
529 m_playAction->setEnabled(false);
530 m_stopAction->setEnabled(true);
538 rec_audio->setEnabled(false);
539 rec_video->setEnabled(false);
541 if (device_selector->currentIndex() == FIREWIRE) {
542 kDebug() << "Capture: Running ffplay " << m_displayArgs.join(" ");
543 m_displayProcess->start(KdenliveSettings::ffplaypath(), m_displayArgs);
544 //video_frame->setText(i18n("Initialising..."));
546 // do something when starting screen grab
550 void RecMonitor::slotRecord()
552 rec_audio->setEnabled(false);
553 rec_video->setEnabled(false);
555 if (m_captureProcess->state() == QProcess::NotRunning && device_selector->currentIndex() == FIREWIRE) {
559 // User stopped capture
562 } else if (device_selector->currentIndex() == FIREWIRE) {
563 m_isCapturing = true;
565 m_captureProcess->write("c\n", 3);
566 m_spaceTimer.start();
569 if (m_captureProcess->state() == QProcess::NotRunning) {
571 m_recAction->setChecked(true);
572 QString extension = "mpg";
573 if (device_selector->currentIndex() == SCREENGRAB) extension = "ogv"; //KdenliveSettings::screengrabextension();
574 else if (device_selector->currentIndex() == VIDEO4LINUX) {
575 // TODO: when recording audio only, allow configuration?
576 if (!rec_video->isChecked()) extension = "wav";
577 else extension = KdenliveSettings::v4l_extension();
579 else if (device_selector->currentIndex() == BLACKMAGIC) extension = KdenliveSettings::decklink_extension();
580 QString path = KUrl(m_capturePath).path(KUrl::AddTrailingSlash) + "capture0000." + extension;
582 while (QFile::exists(path)) {
583 QString num = QString::number(i).rightJustified(4, '0', false);
584 path = KUrl(m_capturePath).path(KUrl::AddTrailingSlash) + "capture" + num + '.' + extension;
587 m_captureFile = KUrl(path);
589 m_captureArgs.clear();
590 m_displayArgs.clear();
593 QString v4lparameters;
594 MltVideoProfile profile;
596 QString capturename = KdenliveSettings::dvgrabfilename();
597 if (capturename.isEmpty()) capturename = "capture";
599 switch (device_selector->currentIndex()) {
601 slotActivateMonitor();
602 path = KStandardDirs::locateLocal("appdata", "profiles/video4linux");
603 profile = ProfilesDialog::getVideoProfile(path);
604 //m_videoBox->setRatio((double) profile.display_aspect_num / profile.display_aspect_den);
605 buildMltDevice(path);
606 playlist = getV4lXmlPlaylist(profile);
608 v4lparameters = KdenliveSettings::v4l_parameters();
610 // TODO: when recording audio only, allow param configuration?
611 if (!rec_video->isChecked()) v4lparameters.clear();
613 // Add alsa audio capture
614 if (!rec_audio->isChecked()) {
615 // if we do not want audio, make sure that we don't have audio encoding parameters
616 // this is required otherwise the MLT avformat consumer will not close properly
617 if (v4lparameters.contains("acodec")) {
618 QString endParam = v4lparameters.section("acodec", 1);
619 int vcodec = endParam.indexOf(" vcodec");
620 int format = endParam.indexOf(" f=");
621 int cutPosition = -1;
624 cutPosition = qMin(vcodec, format);
626 else cutPosition = vcodec;
628 else if (format > -1) {
629 cutPosition = format;
632 // nothing interesting in end params
635 if (cutPosition > -1) {
636 endParam.remove(0, cutPosition);
638 v4lparameters = QString(v4lparameters.section("acodec", 0, 0) + "an=1 " + endParam).simplified();
642 showPreview = m_previewSettings->isChecked();
643 if (!rec_video->isChecked()) showPreview = false;
645 if (m_captureDevice->slotStartCapture(v4lparameters, m_captureFile.path(), playlist, showPreview)) {
646 videoBox->setHidden(false);
647 m_isCapturing = true;
648 m_recAction->setEnabled(false);
649 m_stopAction->setEnabled(true);
650 m_previewSettings->setEnabled(false);
653 video_frame->setText(i18n("Failed to start Video4Linux,\ncheck your parameters..."));
654 videoBox->setHidden(true);
655 m_isCapturing = false;
660 slotActivateMonitor();
661 path = KdenliveSettings::current_profile();
662 profile = ProfilesDialog::getVideoProfile(path);
663 //m_videoBox->setRatio((double) profile.display_aspect_num / profile.display_aspect_den);
664 buildMltDevice(path);
666 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());
668 if (m_captureDevice->slotStartCapture(KdenliveSettings::decklink_parameters(), m_captureFile.path(), QString("decklink:%1").arg(KdenliveSettings::decklink_capturedevice()), m_previewSettings->isChecked(), false)) {
669 videoBox->setHidden(false);
670 m_isCapturing = true;
671 slotSetInfoMessage(i18n("Capturing to %1", m_captureFile.fileName()));
672 m_recAction->setEnabled(false);
673 m_stopAction->setEnabled(true);
674 m_previewSettings->setEnabled(false);
677 video_frame->setText(i18n("Failed to start Decklink,\ncheck your parameters..."));
678 slotSetInfoMessage(i18n("Failed to start capture"));
679 videoBox->setHidden(true);
680 m_isCapturing = false;
685 switch (KdenliveSettings::rmd_capture_type()) {
687 // Full screen capture, nothing special to do
691 m_captureArgs << "--width" << QString::number(KdenliveSettings::rmd_width()) << "--height" << QString::number(KdenliveSettings::rmd_height());
692 if (!KdenliveSettings::rmd_follow_mouse()) {
693 m_captureArgs << "-x" << QString::number(KdenliveSettings::rmd_offsetx()) << "-y" << QString::number(KdenliveSettings::rmd_offsety());
695 m_captureArgs << "--follow-mouse";
696 if (KdenliveSettings::rmd_hide_frame()) m_captureArgs << "--no-frame";
700 if (KdenliveSettings::rmd_hide_mouse()) m_captureArgs << "--no-cursor";
701 m_isCapturing = true;
702 if (KdenliveSettings::rmd_capture_audio()) {
703 m_captureArgs << "--freq" << KdenliveSettings::rmd_freq();
704 m_captureArgs << "--channels" << QString::number(KdenliveSettings::rmd_audio_channels());
705 if (KdenliveSettings::rmd_use_jack()) {
706 m_captureArgs << "--use-jack";
707 QStringList ports = KdenliveSettings::rmd_jackports().split(' ', QString::SkipEmptyParts);
708 for (int i = 0; i < ports.count(); ++i) {
709 m_captureArgs << ports.at(i);
711 if (KdenliveSettings::rmd_jack_buffer() > 0.0)
712 m_captureArgs << "--ring-buffer-size" << QString::number(KdenliveSettings::rmd_jack_buffer());
714 if (!KdenliveSettings::rmd_alsadevicename().isEmpty())
715 m_captureArgs << "--device" << KdenliveSettings::rmd_alsadevicename();
716 if (KdenliveSettings::rmd_alsa_buffer() > 0)
717 m_captureArgs << "--buffer-size" << QString::number(KdenliveSettings::rmd_alsa_buffer());
719 } else m_captureArgs << "--no-sound";
721 if (KdenliveSettings::rmd_fullshots()) m_captureArgs << "--full-shots";
722 m_captureArgs << "--v_bitrate" << QString::number(KdenliveSettings::rmd_bitrate());
723 m_captureArgs << "--v_quality" << QString::number(KdenliveSettings::rmd_quality());
724 m_captureArgs << "--workdir" << KdenliveSettings::currenttmpfolder();
725 m_captureArgs << "--fps" << QString::number(KdenliveSettings::rmd_fps()) << "-o" << m_captureFile.path();
726 m_captureProcess->start(KdenliveSettings::rmd_path(), m_captureArgs);
727 kDebug() << "// RecordMyDesktop params: " << m_captureArgs;
734 if (device_selector->currentIndex() == FIREWIRE) {
735 m_isCapturing = true;
736 kDebug() << "Capture: Running ffplay " << m_displayArgs.join(" ");
737 m_displayProcess->start(KdenliveSettings::ffplaypath(), m_displayArgs);
738 video_frame->setText(i18n("Initialising..."));
742 m_displayProcess->kill();
743 //captureProcess->kill();
744 QTimer::singleShot(1000, this, SLOT(slotRecord()));
748 const QString RecMonitor::getV4lXmlPlaylist(MltVideoProfile profile) {
750 QString 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);
752 if (rec_video->isChecked()) {
753 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));
756 if (rec_audio->isChecked()) {
757 playlist.append(QString("<producer id=\"producer1\" in=\"0\" out=\"999999\"><property name=\"mlt_type\">producer</property><property name=\"length\">1000000</property><property name=\"eof\">loop</property><property name=\"resource\">alsa:%5</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()));
759 playlist.append("<tractor id=\"tractor0\" title=\"video0\" global_feed=\"1\" in=\"0\" out=\"999999\">");
760 if (rec_video->isChecked()) playlist.append("<track producer=\"playlist0\"/>");
761 if (rec_audio->isChecked()) playlist.append("<track producer=\"playlist1\"/>");
762 playlist.append("</tractor></mlt>");
768 void RecMonitor::slotStartGrab(const QRect &rect) {
769 rgnGrab->deleteLater();
770 QApplication::restoreOverrideCursor();
772 if (rect.isNull()) return;
773 int width = rect.width();
774 int height = rect.height();
775 if (width % 2 != 0) width--;
776 if (height % 2 != 0) height--;
777 QString args = KdenliveSettings::screengrabcapture().replace("%size", QString::number(width) + "x" + QString::number(height)).replace("%offset", "+" + QString::number(rect.x()) + "," + QString::number(rect.y()));
778 if (KdenliveSettings::screengrabenableaudio()) {
779 // also capture audio
780 if (KdenliveSettings::useosscapture()) m_captureArgs << KdenliveSettings::screengrabosscapture().simplified().split(' ');
781 else m_captureArgs << KdenliveSettings::screengrabalsacapture2().simplified().split(' ');
783 m_captureArgs << args.simplified().split(' ') << KdenliveSettings::screengrabencoding().simplified().split(' ') << m_captureFile.path();
784 m_isCapturing = true;
785 video_frame->setText(i18n("Capturing..."));
786 if (KdenliveSettings::screengrabenableaudio() && !KdenliveSettings::useosscapture()) {
787 QStringList alsaArgs = KdenliveSettings::screengrabalsacapture().simplified().split(' ');
788 alsaProcess->setStandardOutputProcess(captureProcess);
789 kDebug() << "Capture: Running arecord " << alsaArgs.join(" ");
790 alsaProcess->start("arecord", alsaArgs);
792 kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
793 captureProcess->start(KdenliveSettings::ffmpegpath(), m_captureArgs);
796 void RecMonitor::slotProcessStatus(QProcess::ProcessState status)
798 if (status == QProcess::NotRunning) {
799 m_displayProcess->kill();
800 if (m_isCapturing && device_selector->currentIndex() != FIREWIRE)
801 if (m_addCapturedClip->isChecked() && !m_captureFile.isEmpty() && QFile::exists(m_captureFile.path())) {
802 emit addProjectClip(m_captureFile);
803 m_captureFile.clear();
805 if (device_selector->currentIndex() == FIREWIRE) {
806 m_discAction->setIcon(KIcon("network-connect"));
807 m_discAction->setText(i18n("Connect"));
808 m_playAction->setEnabled(false);
809 m_rewAction->setEnabled(false);
810 m_fwdAction->setEnabled(false);
811 m_recAction->setEnabled(false);
814 m_playAction->setIcon(m_playIcon);
815 m_recAction->setChecked(false);
816 m_stopAction->setEnabled(false);
817 device_selector->setEnabled(true);
818 if (m_captureProcess && m_captureProcess->exitStatus() == QProcess::CrashExit) {
819 video_frame->setText(i18n("Capture crashed, please check your parameters"));
821 if (device_selector->currentIndex() != SCREENGRAB) {
822 video_frame->setText(i18n("Not connected"));
824 if (m_captureProcess->exitCode() != 0) {
825 video_frame->setText(i18n("Capture crashed, please check your parameters\nRecordMyDesktop exit code: %1", QString::number(m_captureProcess->exitCode())));
827 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)));
831 m_isCapturing = false;
834 // update free space info
835 slotUpdateFreeSpace();
837 if (device_selector->currentIndex() != SCREENGRAB) m_stopAction->setEnabled(true);
838 device_selector->setEnabled(false);
842 void RecMonitor::manageCapturedFiles()
845 switch (KdenliveSettings::firewireformat()) {
857 QDir dir(m_capturePath);
859 QString capturename = KdenliveSettings::dvgrabfilename();
860 if (capturename.isEmpty()) capturename = "capture";
861 filters << capturename + '*' + extension;
862 const QStringList result = dir.entryList(filters, QDir::Files, QDir::Time);
863 KUrl::List capturedFiles;
864 foreach(const QString & name, result) {
865 KUrl url = KUrl(dir.filePath(name));
866 if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, this)) {
867 KFileItem file(KFileItem::Unknown, KFileItem::Unknown, url, true);
868 if (file.time(KFileItem::ModificationTime) > m_captureTime) {
869 // The file was captured in the last batch
870 if (url.fileName().contains(':')) {
871 // Several dvgrab options (--timecode,...) use : in the file name, which is
872 // not supported by MLT, so rename them
873 QString newUrl = url.directory(KUrl::AppendTrailingSlash) + url.fileName().replace(':', '_');
874 if (QFile::rename(url.path(), newUrl)) {
879 capturedFiles.append(url);
883 kDebug() << "Found : " << capturedFiles.count() << " new capture files";
884 kDebug() << capturedFiles;
886 if (capturedFiles.count() > 0) {
887 QPointer<ManageCapturesDialog> d = new ManageCapturesDialog(capturedFiles, this);
888 if (d->exec() == QDialog::Accepted) {
889 emit addProjectClipList(d->importFiles());
896 void RecMonitor::mousePressEvent(QMouseEvent *event)
898 if (m_freeSpace->underMouse()) slotUpdateFreeSpace();
899 else QWidget::mousePressEvent(event);//m_videoBox->mousePressEvent(event);
902 void RecMonitor::slotUpdateFreeSpace()
904 KDiskFreeSpaceInfo info = KDiskFreeSpaceInfo::freeSpaceInfo(m_capturePath);
905 if (info.isValid() && info.size() > 0) {
906 m_freeSpace->setValue(100 * info.used() / info.size());
907 m_freeSpace->setText(i18n("Free space: %1", KIO::convertSize(info.available())));
908 m_freeSpace->update();
912 void RecMonitor::refreshRecMonitor(bool visible)
915 //if (!m_isActive) activateMonitor();
920 void RecMonitor::slotPlay()
922 /*if (m_isPlaying) slotStopCapture();
923 else slotStartPreview(true);*/
926 void RecMonitor::slotReadDvgrabInfo()
928 QString data = m_captureProcess->readAllStandardError().simplified();
929 data = data.section('"', 2, 2).simplified();
930 m_dvinfo.setText(data.left(11));
931 m_dvinfo.updateGeometry();
934 AbstractRender *RecMonitor::abstractRender()
936 return m_captureDevice;
940 void RecMonitor::analyseFrames(bool analyse)
943 if (m_captureDevice) m_captureDevice->sendFrameForAnalysis = analyse;
946 void RecMonitor::slotDroppedFrames(int dropped)
948 slotSetInfoMessage(i18n("%1 dropped frames", dropped));
951 void RecMonitor::buildMltDevice(const QString &path)
953 if (m_captureDevice == NULL) {
954 m_monitorManager->updateScopeSource();
955 m_captureDevice = new MltDeviceCapture(path, videoSurface, this);
956 connect(m_captureDevice, SIGNAL(droppedFrames(int)), this, SLOT(slotDroppedFrames(int)));
957 m_captureDevice->sendFrameForAnalysis = m_analyse;
958 m_monitorManager->updateScopeSource();
962 void RecMonitor::slotChangeRecordingPreview(bool enable)
964 KdenliveSettings::setEnable_recording_preview(enable);
968 void RecMonitor::slotMouseSeek(int /*eventDelta*/, bool /*fast*/)
973 #include "recmonitor.moc"