]> git.sesse.net Git - kdenlive/blob - src/recmonitor.cpp
Fix v4l capture on the first attempt
[kdenlive] / src / recmonitor.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20
21 #include "recmonitor.h"
22 #include "gentime.h"
23 #include "mltdevicecapture.h"
24 #include "kdenlivesettings.h"
25 #include "managecapturesdialog.h"
26 #include "monitormanager.h"
27 #include "monitor.h"
28 #include "profilesdialog.h"
29
30 #include <KDebug>
31 #include <KLocale>
32 #include <KStandardDirs>
33 #include <KComboBox>
34 #include <KIO/NetAccess>
35 #include <KFileItem>
36 #include <KMessageBox>
37 #include <KApplication>
38
39 #if KDE_IS_VERSION(4,2,0)
40 #include <KDiskFreeSpaceInfo>
41 #endif
42
43 #include <QMouseEvent>
44 #include <QMenu>
45 #include <QToolButton>
46 #include <QFile>
47 #include <QDir>
48
49
50 RecMonitor::RecMonitor(QString name, MonitorManager *manager, QWidget *parent) :
51     AbstractMonitor(parent),
52     m_name(name),
53     m_isCapturing(false),
54     m_didCapture(false),
55     m_isPlaying(false),
56     m_blackmagicCapturing(false),
57     m_manager(manager),
58     m_captureDevice(NULL),
59     m_analyse(false)
60 {
61     setupUi(this);
62
63     video_frame->setAttribute(Qt::WA_PaintOnScreen);
64     device_selector->setCurrentIndex(KdenliveSettings::defaultcapture());
65     connect(device_selector, SIGNAL(currentIndexChanged(int)), this, SLOT(slotVideoDeviceChanged(int)));
66
67     // Video widget holder
68     QVBoxLayout *l = new QVBoxLayout;
69     l->setContentsMargins(0, 0, 0, 0);
70     l->setSpacing(0);
71     m_videoBox = new VideoPreviewContainer();
72     m_videoBox->setContentsMargins(0, 0, 0, 0);
73     m_videoBox->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
74     l->addWidget(m_videoBox);
75     video_frame->setLayout(l);
76
77     QToolBar *toolbar = new QToolBar(this);
78     QHBoxLayout *layout = new QHBoxLayout;
79     layout->setContentsMargins(0, 0, 0, 0);
80     m_playIcon = KIcon("media-playback-start");
81     m_pauseIcon = KIcon("media-playback-pause");
82
83     m_discAction = toolbar->addAction(KIcon("network-connect"), i18n("Connect"));
84     connect(m_discAction, SIGNAL(triggered()), this, SLOT(slotDisconnect()));
85
86     m_rewAction = toolbar->addAction(KIcon("media-seek-backward"), i18n("Rewind"));
87     connect(m_rewAction, SIGNAL(triggered()), this, SLOT(slotRewind()));
88
89     m_playAction = toolbar->addAction(m_playIcon, i18n("Play"));
90     connect(m_playAction, SIGNAL(triggered()), this, SLOT(slotStartPreview()));
91
92     m_stopAction = toolbar->addAction(KIcon("media-playback-stop"), i18n("Stop"));
93     connect(m_stopAction, SIGNAL(triggered()), this, SLOT(slotStopCapture()));
94     m_stopAction->setEnabled(false);
95     m_fwdAction = toolbar->addAction(KIcon("media-seek-forward"), i18n("Forward"));
96     connect(m_fwdAction, SIGNAL(triggered()), this, SLOT(slotForward()));
97
98     m_recAction = toolbar->addAction(KIcon("media-record"), i18n("Record"));
99     connect(m_recAction, SIGNAL(triggered()), this, SLOT(slotRecord()));
100     m_recAction->setCheckable(true);
101
102     toolbar->addSeparator();
103
104     QAction *configAction = toolbar->addAction(KIcon("configure"), i18n("Configure"));
105     connect(configAction, SIGNAL(triggered()), this, SLOT(slotConfigure()));
106     configAction->setCheckable(false);
107
108     layout->addWidget(toolbar);
109     layout->addWidget(&m_logger);
110     layout->addWidget(&m_dvinfo);
111     m_logger.setMaxCount(10);
112     m_logger.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
113     m_logger.setFrame(false);
114     //m_logger.setInsertPolicy(QComboBox::InsertAtTop);
115
116 #if KDE_IS_VERSION(4,2,0)
117     m_freeSpace = new KCapacityBar(KCapacityBar::DrawTextInline, this);
118     m_freeSpace->setMaximumWidth(150);
119     QFontMetricsF fontMetrics(font());
120     m_freeSpace->setMaximumHeight(fontMetrics.height() * 1.2);
121     slotUpdateFreeSpace();
122     layout->addWidget(m_freeSpace);
123     connect(&m_spaceTimer, SIGNAL(timeout()), this, SLOT(slotUpdateFreeSpace()));
124     m_spaceTimer.setInterval(30000);
125     m_spaceTimer.setSingleShot(false);
126 #endif
127
128     control_frame_firewire->setLayout(layout);
129
130     slotVideoDeviceChanged(device_selector->currentIndex());
131     m_displayProcess = new QProcess;
132     m_captureProcess = new QProcess;
133
134     connect(m_captureProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(slotProcessStatus(QProcess::ProcessState)));
135     connect(m_captureProcess, SIGNAL(readyReadStandardError()), this, SLOT(slotReadDvgrabInfo()));
136
137     
138     QString videoDriver = KdenliveSettings::videodrivername();
139 #if QT_VERSION >= 0x040600
140     QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
141     env.insert("SDL_WINDOWID", QString::number(video_frame->winId()));
142     if (!videoDriver.isEmpty()) {
143         if (videoDriver == "x11_noaccel") {
144             env.insert("SDL_VIDEO_YUV_HWACCEL", "0");
145             env.insert("SDL_VIDEODRIVER", "x11");
146         } else env.insert("SDL_VIDEODRIVER", videoDriver);
147     }
148     m_displayProcess->setProcessEnvironment(env);
149 #else
150     QStringList env = QProcess::systemEnvironment();
151     env << "SDL_WINDOWID=" + QString::number(video_frame->winId());
152     if (!videoDriver.isEmpty()) {
153         if (videoDriver == "x11_noaccel") {
154             env << "SDL_VIDEO_YUV_HWACCEL=0";
155             env << "SDL_VIDEODRIVER=x11";
156         } else env << "SDL_VIDEODRIVER=" + videoDriver;
157     }
158     m_displayProcess->setEnvironment(env);
159 #endif
160     setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 1);
161
162     kDebug() << "/////// BUILDING MONITOR, ID: " << video_frame->winId();
163 }
164
165 RecMonitor::~RecMonitor()
166 {
167 #if KDE_IS_VERSION(4,2,0)
168     m_spaceTimer.stop();
169 #endif
170     delete m_captureProcess;
171     delete m_displayProcess;
172     if (m_captureDevice) delete m_captureDevice;
173 }
174
175 const QString RecMonitor::name() const
176 {
177     return m_name;
178 }
179
180 void RecMonitor::stop()
181 {
182     slotStopCapture();
183 }
184
185 void RecMonitor::start()
186 {
187 }
188
189 void RecMonitor::slotConfigure()
190 {
191     emit showConfigDialog(4, device_selector->currentIndex());
192 }
193
194 void RecMonitor::slotUpdateCaptureFolder(const QString currentProjectFolder)
195 {
196     if (KdenliveSettings::capturetoprojectfolder()) m_capturePath = currentProjectFolder;
197     else m_capturePath = KdenliveSettings::capturefolder();
198     if (m_captureProcess) m_captureProcess->setWorkingDirectory(m_capturePath);
199     if (m_captureProcess->state() != QProcess::NotRunning) {
200         if (device_selector->currentIndex() == FIREWIRE)
201             KMessageBox::information(this, i18n("You need to disconnect and reconnect in the capture monitor to apply your changes"), i18n("Capturing"));
202         else KMessageBox::information(this, i18n("You need to stop capture before your changes can be applied"), i18n("Capturing"));
203     } else slotVideoDeviceChanged(device_selector->currentIndex());
204
205 #if KDE_IS_VERSION(4,2,0)
206     // update free space info
207     slotUpdateFreeSpace();
208 #endif
209 }
210
211 void RecMonitor::slotVideoDeviceChanged(int ix)
212 {
213     QString capturefile;
214     QString capturename;
215     QString path;
216     m_videoBox->setHidden(true);
217     enable_preview->setHidden(ix != VIDEO4LINUX && ix != BLACKMAGIC);
218     m_fwdAction->setVisible(ix == FIREWIRE);
219     m_discAction->setVisible(ix == FIREWIRE);
220     m_rewAction->setVisible(ix == FIREWIRE);
221     m_recAction->setEnabled(ix != FIREWIRE);
222     m_logger.setVisible(ix == BLACKMAGIC);
223     if (m_captureDevice) {
224         // MLT capture still running, abort
225         m_captureDevice->stop();
226         delete m_captureDevice;
227         m_captureDevice = NULL;
228         m_manager->clearScopeSource();
229     }
230     switch (ix) {
231     case SCREENGRAB:
232         m_discAction->setEnabled(false);
233         m_rewAction->setEnabled(false);
234         m_fwdAction->setEnabled(false);
235         m_stopAction->setEnabled(false);
236         m_playAction->setEnabled(false);
237         if (KdenliveSettings::rmd_path().isEmpty()) {
238             QString rmdpath = KStandardDirs::findExe("recordmydesktop");
239             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")));
240             else KdenliveSettings::setRmd_path(rmdpath);
241         }
242         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)));
243         //video_frame->setText(i18n("Press record button\nto start screen capture"));
244         break;
245     case VIDEO4LINUX:
246         m_stopAction->setEnabled(false);
247         m_playAction->setEnabled(true);
248         path = KStandardDirs::locateLocal("appdata", "profiles/video4linux");
249         if (checkDeviceAvailability()) buildMltDevice(path);
250         break;
251     case BLACKMAGIC:
252         m_stopAction->setEnabled(false);
253         m_playAction->setEnabled(true);
254         capturefile = m_capturePath;
255         if (!capturefile.endsWith("/")) capturefile.append("/");
256         capturename = KdenliveSettings::decklink_filename();
257         capturename.append("xxx.");
258         capturename.append(KdenliveSettings::decklink_extension());
259         capturefile.append(capturename);
260         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)));
261         path = KdenliveSettings::current_profile();
262         buildMltDevice(path);
263         break;
264     default: // FIREWIRE
265         m_discAction->setEnabled(true);
266         m_stopAction->setEnabled(false);
267         m_playAction->setEnabled(false);
268         m_rewAction->setEnabled(false);
269         m_fwdAction->setEnabled(false);
270
271         // Check that dvgab is available
272         if (KdenliveSettings::dvgrab_path().isEmpty()) {
273             QString dvgrabpath = KStandardDirs::findExe("dvgrab");
274             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")));
275             else KdenliveSettings::setDvgrab_path(dvgrabpath);
276         } else {
277             // Show capture info
278             capturefile = m_capturePath;
279             if (!capturefile.endsWith("/")) capturefile.append("/");
280             capturename = KdenliveSettings::dvgrabfilename();
281             if (capturename.isEmpty()) capturename = "capture";
282             QString extension;
283             switch (KdenliveSettings::firewireformat()) {
284             case 0:
285                 extension = ".dv";
286                 break;
287             case 1:
288             case 2:
289                 extension = ".avi";
290                 break;
291             case 3:
292                 extension = ".m2t";
293                 break;
294             }
295             capturename.append("xxx" + extension);
296             capturefile.append(capturename);
297             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)));
298         }
299         break;
300     }
301 }
302
303 void RecMonitor::slotGotBlackmagicFrameNumber(ulong ix)
304 {
305     m_dvinfo.setText(QString::number(ix));
306 }
307
308 void RecMonitor::slotSetInfoMessage(const QString &message)
309 {
310     m_logger.insertItem(0, message);
311 }
312
313 QPixmap RecMonitor::mergeSideBySide(const QPixmap& pix, const QString txt)
314 {
315     QPainter p;
316     QRect r = QApplication::fontMetrics().boundingRect(QRect(0, 0, video_frame->width(), video_frame->height()), Qt::AlignLeft, txt);
317     int strWidth = r.width();
318     int strHeight = r.height();
319     int pixWidth = pix.width();
320     int pixHeight = pix.height();
321     QPixmap res(strWidth + 8 + pixWidth, qMax(strHeight, pixHeight));
322     res.fill(Qt::transparent);
323     p.begin(&res);
324     p.drawPixmap(0, 0, pix);
325     p.setPen(kapp->palette().text().color());
326     p.drawText(QRect(pixWidth + 8, 0, strWidth, strHeight), 0, txt);
327     p.end();
328     return res;
329 }
330
331
332 bool RecMonitor::checkDeviceAvailability()
333 {
334     if (!KIO::NetAccess::exists(KUrl(KdenliveSettings::video4vdevice()), KIO::NetAccess::SourceSide , this)) {
335         m_playAction->setEnabled(false);
336         m_recAction->setEnabled(false);
337         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())));
338         return false;
339     } else {
340         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)));
341         return true;
342     }
343 }
344
345 void RecMonitor::slotDisconnect()
346 {
347     if (m_captureProcess->state() == QProcess::NotRunning) {
348         m_captureTime = KDateTime::currentLocalDateTime();
349         kDebug() << "CURRENT TIME: " << m_captureTime.toString();
350         m_didCapture = false;
351         slotStartPreview(false);
352         m_discAction->setIcon(KIcon("network-disconnect"));
353         m_discAction->setText(i18n("Disconnect"));
354         m_recAction->setEnabled(true);
355         m_stopAction->setEnabled(true);
356         m_playAction->setEnabled(true);
357         m_rewAction->setEnabled(true);
358         m_fwdAction->setEnabled(true);
359     } else {
360         m_captureProcess->write("q", 1);
361         QTimer::singleShot(1000, m_captureProcess, SLOT(kill()));
362         if (m_didCapture) manageCapturedFiles();
363         m_didCapture = false;
364     }
365 }
366
367 void RecMonitor::slotRewind()
368 {
369     m_captureProcess->write("a", 1);
370 }
371
372 void RecMonitor::slotForward()
373 {
374     m_captureProcess->write("z", 1);
375 }
376
377 void RecMonitor::slotStopCapture()
378 {
379     // stop capture
380     if (!m_isCapturing && !m_isPlaying) return;
381     m_videoBox->setHidden(true);
382     switch (device_selector->currentIndex()) {
383     case FIREWIRE:
384         m_captureProcess->write("\e", 2);
385         m_playAction->setIcon(m_playIcon);
386         m_isPlaying = false;
387         break;
388     case SCREENGRAB:
389         m_captureProcess->write("q\n", 3);
390         m_captureProcess->terminate();
391         video_frame->setText(i18n("Encoding captured video..."));
392         QTimer::singleShot(1000, m_captureProcess, SLOT(kill()));
393         break;
394     case VIDEO4LINUX:
395     case BLACKMAGIC:
396         if (m_captureDevice) {
397             m_captureDevice->stop();
398         }
399         m_isCapturing = false;
400         m_isPlaying = false;
401         m_playAction->setEnabled(true);
402         m_stopAction->setEnabled(false);
403         m_recAction->setEnabled(true);
404         slotSetInfoMessage(i18n("Capture stopped"));
405         m_isCapturing = false;
406         m_recAction->setChecked(false);
407         if (autoaddbox->isChecked() && !m_captureFile.isEmpty() && QFile::exists(m_captureFile.path())) {
408             emit addProjectClip(m_captureFile);
409             m_captureFile.clear();
410         }
411         break;
412     default:
413         break;
414     }
415 }
416
417 void RecMonitor::slotStartPreview(bool play)
418 {
419     if (m_captureProcess->state() != QProcess::NotRunning) {
420         if (device_selector->currentIndex() == FIREWIRE) {
421             if (m_isPlaying) {
422                 m_captureProcess->write("k", 1);
423                 //captureProcess->write("\e", 2);
424                 m_playAction->setIcon(m_playIcon);
425                 m_isPlaying = false;
426             } else {
427                 m_captureProcess->write("p", 1);
428                 m_playAction->setIcon(m_pauseIcon);
429                 m_isPlaying = true;
430             }
431         }
432         return;
433     }
434     m_captureArgs.clear();
435     m_displayArgs.clear();
436     m_isPlaying = false;
437     QString capturename = KdenliveSettings::dvgrabfilename();
438     QString path;
439     MltVideoProfile profile;
440     QString producer;
441     QStringList dvargs = KdenliveSettings::dvgrabextra().simplified().split(" ", QString::SkipEmptyParts);
442     int ix = device_selector->currentIndex();
443     m_videoBox->setHidden(ix != VIDEO4LINUX && ix != BLACKMAGIC);
444     switch (ix) {
445     case FIREWIRE:
446         switch (KdenliveSettings::firewireformat()) {
447         case 0:
448             // RAW DV CAPTURE
449             m_captureArgs << "--format" << "raw";
450             m_displayArgs << "-f" << "dv";
451             break;
452         case 1:
453             // DV type 1
454             m_captureArgs << "--format" << "dv1";
455             m_displayArgs << "-f" << "dv";
456             break;
457         case 2:
458             // DV type 2
459             m_captureArgs << "--format" << "dv2";
460             m_displayArgs << "-f" << "dv";
461             break;
462         case 3:
463             // HDV CAPTURE
464             m_captureArgs << "--format" << "hdv";
465             m_displayArgs << "-f" << "mpegts";
466             break;
467         }
468         if (KdenliveSettings::firewireautosplit()) m_captureArgs << "--autosplit";
469         if (KdenliveSettings::firewiretimestamp()) m_captureArgs << "--timestamp";
470         if (!dvargs.isEmpty()) {
471             m_captureArgs << dvargs;
472         }
473         m_captureArgs << "-i";
474         if (capturename.isEmpty()) capturename = "capture";
475         m_captureArgs << capturename << "-";
476
477         m_displayArgs << "-x" << QString::number(video_frame->width()) << "-y" << QString::number(video_frame->height()) << "-";
478
479         m_captureProcess->setStandardOutputProcess(m_displayProcess);
480         m_captureProcess->setWorkingDirectory(m_capturePath);
481         kDebug() << "Capture: Running dvgrab " << m_captureArgs.join(" ");
482
483         m_captureProcess->start(KdenliveSettings::dvgrab_path(), m_captureArgs);
484         if (play) m_captureProcess->write(" ", 1);
485         m_discAction->setEnabled(true);
486         break;
487     case VIDEO4LINUX:
488         path = KStandardDirs::locateLocal("appdata", "profiles/video4linux");
489         m_manager->activateMonitor("record");
490         buildMltDevice(path);
491         profile = ProfilesDialog::getVideoProfile(path);
492         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);
493         kDebug()<< "PROD: "<<producer;
494         if (!m_captureDevice->slotStartPreview(producer)) {
495             // v4l capture failed to start
496             video_frame->setText(i18n("Failed to start Video4Linux,\ncheck your parameters..."));
497             m_videoBox->setHidden(true);
498             
499         } else {
500             m_playAction->setEnabled(false);
501             m_stopAction->setEnabled(true);
502             m_isPlaying = true;
503         }
504         
505         break;
506     case BLACKMAGIC:
507         path = KdenliveSettings::current_profile();
508         m_manager->activateMonitor("record");
509         buildMltDevice(path);
510         profile = ProfilesDialog::getVideoProfile(path);
511         producer = QString("decklink:%1").arg(KdenliveSettings::decklink_capturedevice());
512         if (!m_captureDevice->slotStartPreview(producer)) {
513             // v4l capture failed to start
514             video_frame->setText(i18n("Failed to start Decklink,\ncheck your parameters..."));
515             m_videoBox->setHidden(true);
516             
517         } else {
518             m_playAction->setEnabled(false);
519             m_stopAction->setEnabled(true);
520             m_isPlaying = true;
521         }
522         break;
523     default:
524         break;
525     }
526
527     if (device_selector->currentIndex() == FIREWIRE) {
528         kDebug() << "Capture: Running ffplay " << m_displayArgs.join(" ");
529         m_displayProcess->start("ffplay", m_displayArgs);
530         video_frame->setText(i18n("Initialising..."));
531     } else {
532         // do something when starting screen grab
533     }
534 }
535
536 void RecMonitor::slotRecord()
537 {
538     if (m_captureProcess->state() == QProcess::NotRunning && device_selector->currentIndex() == FIREWIRE) {
539         slotStartPreview();
540     }
541     if (m_isCapturing) {
542         // User stopped capture
543         slotStopCapture();
544         return;
545     } else if (device_selector->currentIndex() == FIREWIRE) {
546         m_isCapturing = true;
547         m_didCapture = true;
548         m_captureProcess->write("c\n", 3);
549 #if KDE_IS_VERSION(4,2,0)
550         m_spaceTimer.start();
551 #endif
552         return;
553     }
554     if (m_captureProcess->state() == QProcess::NotRunning) {
555         m_recAction->setChecked(true);
556         QString extension = "mpg";
557         if (device_selector->currentIndex() == SCREENGRAB) extension = "ogv"; //KdenliveSettings::screengrabextension();
558         else if (device_selector->currentIndex() == VIDEO4LINUX) extension = KdenliveSettings::v4l_extension();
559         else if (device_selector->currentIndex() == BLACKMAGIC) extension = KdenliveSettings::decklink_extension();
560         QString path = KUrl(m_capturePath).path(KUrl::AddTrailingSlash) + "capture0000." + extension;
561         int i = 1;
562         while (QFile::exists(path)) {
563             QString num = QString::number(i).rightJustified(4, '0', false);
564             path = KUrl(m_capturePath).path(KUrl::AddTrailingSlash) + "capture" + num + '.' + extension;
565             i++;
566         }
567         m_captureFile = KUrl(path);
568
569         m_captureArgs.clear();
570         m_displayArgs.clear();
571         QString args;
572         QString playlist;
573         MltVideoProfile profile;
574         QString capturename = KdenliveSettings::dvgrabfilename();
575         if (capturename.isEmpty()) capturename = "capture";
576
577         switch (device_selector->currentIndex()) {
578         case VIDEO4LINUX:
579             path = KStandardDirs::locateLocal("appdata", "profiles/video4linux");
580             profile = ProfilesDialog::getVideoProfile(path);
581             buildMltDevice(path);
582             playlist = QString("<mlt title=\"capture\"><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\">video4linux2:%1?width:%2&amp;height:%3&amp;frame_rate:%4</property><property name=\"mlt_service\">avformat-novalidate</property></producer><playlist id=\"playlist0\"><entry producer=\"producer0\" in=\"0\" out=\"99999\"/></playlist>").arg(KdenliveSettings::video4vdevice()).arg(profile.width).arg(profile.height).arg((double) profile.frame_rate_num / profile.frame_rate_den);
583
584             // Add alsa audio capture
585             if (KdenliveSettings::v4l_captureaudio()) {
586                 playlist.append(QString("<producer id=\"producer1\" in=\"0\" out=\"99999\"><property name=\"mlt_type\">producer</property><property name=\"length\">100000</property><property name=\"eof\">pause</property><property name=\"resource\">alsa:%5</property><property name=\"audio_index\">0</property><property name=\"video_index\">-1</property><property name=\"mlt_service\">avformat</property></producer><playlist id=\"playlist1\"><entry producer=\"producer1\" in=\"0\" out=\"99999\"/></playlist>").arg(KdenliveSettings::v4l_alsadevicename()));
587             }
588             
589
590             playlist.append("<tractor id=\"tractor0\" title=\"video0\" global_feed=\"1\" in=\"0\" out=\"99999\">");
591
592             playlist.append("<track producer=\"playlist0\"/>");            
593
594             // Audio mix
595             if (KdenliveSettings::v4l_captureaudio()) {
596                 playlist.append("<track producer=\"playlist1\"/>");
597                 playlist.append("<transition id=\"transition0\" in=\"0\" out=\"0\"><property name=\"a_track\">0</property><property name=\"b_track\">1</property><property name=\"mlt_type\">transition</property><property name=\"mlt_service\">mix</property></transition>");
598             }
599
600             playlist.append("</tractor></mlt>");
601
602             if (m_captureDevice->slotStartCapture(KdenliveSettings::v4l_parameters(), m_captureFile.path(), playlist, enable_preview->isChecked())) {
603                 m_videoBox->setHidden(false);
604                 m_isCapturing = true;
605                 m_recAction->setEnabled(false);
606                 m_stopAction->setEnabled(true);
607             }
608             else {
609                 video_frame->setText(i18n("Failed to start Video4Linux,\ncheck your parameters..."));                
610                 m_videoBox->setHidden(true);
611                 m_isCapturing = false;
612             }
613             break;
614             
615         case BLACKMAGIC:
616             path = KdenliveSettings::current_profile();
617             profile = ProfilesDialog::getVideoProfile(path);
618             buildMltDevice(path);
619                
620             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());
621
622             if (m_captureDevice->slotStartCapture(KdenliveSettings::decklink_parameters(), m_captureFile.path(), QString("decklink:%1").arg(KdenliveSettings::decklink_capturedevice()), enable_preview->isChecked(), false)) {
623                 m_videoBox->setHidden(false);
624                 m_isCapturing = true;
625                 slotSetInfoMessage(i18n("Capturing to %1", m_captureFile.fileName()));
626                 m_recAction->setEnabled(false);
627                 m_stopAction->setEnabled(true);
628             }
629             else {
630                 video_frame->setText(i18n("Failed to start Decklink,\ncheck your parameters..."));
631                 slotSetInfoMessage(i18n("Failed to start capture"));
632                 m_videoBox->setHidden(true);
633                 m_isCapturing = false;
634             }
635             break;
636             
637         case SCREENGRAB:
638             switch (KdenliveSettings::rmd_capture_type()) {
639             case 0:
640                 // Full screen capture, nothing special to do
641                 break;
642             default:
643                 // Region capture
644                 m_captureArgs << "--width" << QString::number(KdenliveSettings::rmd_width()) << "--height" << QString::number(KdenliveSettings::rmd_height());
645                 if (!KdenliveSettings::rmd_follow_mouse()) {
646                     m_captureArgs << "-x" << QString::number(KdenliveSettings::rmd_offsetx()) << "-y" << QString::number(KdenliveSettings::rmd_offsety());
647                 } else {
648                     m_captureArgs << "--follow-mouse";
649                     if (KdenliveSettings::rmd_hide_frame()) m_captureArgs << "--no-frame";
650                 }
651                 break;
652             }
653             if (KdenliveSettings::rmd_hide_mouse()) m_captureArgs << "--no-cursor";
654             m_isCapturing = true;
655             if (KdenliveSettings::rmd_capture_audio()) {
656                 m_captureArgs << "--freq" << KdenliveSettings::rmd_freq();
657                 m_captureArgs << "--channels" << QString::number(KdenliveSettings::rmd_audio_channels());
658                 if (KdenliveSettings::rmd_use_jack()) {
659                     m_captureArgs << "--use-jack";
660                     QStringList ports = KdenliveSettings::rmd_jackports().split(" ", QString::SkipEmptyParts);
661                     for (int i = 0; i < ports.count(); ++i) {
662                         m_captureArgs << ports.at(i);
663                     }
664                     if (KdenliveSettings::rmd_jack_buffer() > 0.0)
665                         m_captureArgs << "--ring-buffer-size" << QString::number(KdenliveSettings::rmd_jack_buffer());
666                 } else {
667                     if (!KdenliveSettings::rmd_alsadevicename().isEmpty())
668                         m_captureArgs << "--device" << KdenliveSettings::rmd_alsadevicename();
669                     if (KdenliveSettings::rmd_alsa_buffer() > 0)
670                         m_captureArgs << "--buffer-size" << QString::number(KdenliveSettings::rmd_alsa_buffer());
671                 }
672             } else m_captureArgs << "--no-sound";
673
674             if (KdenliveSettings::rmd_fullshots()) m_captureArgs << "--full-shots";
675             m_captureArgs << "--v_bitrate" << QString::number(KdenliveSettings::rmd_bitrate());
676             m_captureArgs << "--v_quality" << QString::number(KdenliveSettings::rmd_quality());
677             m_captureArgs << "--workdir" << KdenliveSettings::currenttmpfolder();
678             m_captureArgs << "--fps" << QString::number(KdenliveSettings::rmd_fps()) << "-o" << m_captureFile.path();
679             m_captureProcess->start(KdenliveSettings::rmd_path(), m_captureArgs);
680             kDebug() << "// RecordMyDesktop params: " << m_captureArgs;
681             break;
682         default:
683             break;
684         }
685
686
687         if (device_selector->currentIndex() == FIREWIRE) {
688             m_isCapturing = true;
689             kDebug() << "Capture: Running ffplay " << m_displayArgs.join(" ");
690             m_displayProcess->start("ffplay", m_displayArgs);
691             video_frame->setText(i18n("Initialising..."));
692         }
693     } else {
694         // stop capture
695         m_displayProcess->kill();
696         //captureProcess->kill();
697         QTimer::singleShot(1000, this, SLOT(slotRecord()));
698     }
699 }
700
701 /*
702 void RecMonitor::slotStartGrab(const QRect &rect) {
703     rgnGrab->deleteLater();
704     QApplication::restoreOverrideCursor();
705     show();
706     if (rect.isNull()) return;
707     int width = rect.width();
708     int height = rect.height();
709     if (width % 2 != 0) width--;
710     if (height % 2 != 0) height--;
711     QString args = KdenliveSettings::screengrabcapture().replace("%size", QString::number(width) + "x" + QString::number(height)).replace("%offset", "+" + QString::number(rect.x()) + "," + QString::number(rect.y()));
712     if (KdenliveSettings::screengrabenableaudio()) {
713         // also capture audio
714         if (KdenliveSettings::useosscapture()) m_captureArgs << KdenliveSettings::screengrabosscapture().simplified().split(' ');
715         else m_captureArgs << KdenliveSettings::screengrabalsacapture2().simplified().split(' ');
716     }
717     m_captureArgs << args.simplified().split(' ') << KdenliveSettings::screengrabencoding().simplified().split(' ') << m_captureFile.path();
718     m_isCapturing = true;
719     video_frame->setText(i18n("Capturing..."));
720     if (KdenliveSettings::screengrabenableaudio() && !KdenliveSettings::useosscapture()) {
721         QStringList alsaArgs = KdenliveSettings::screengrabalsacapture().simplified().split(' ');
722         alsaProcess->setStandardOutputProcess(captureProcess);
723         kDebug() << "Capture: Running arecord " << alsaArgs.join(" ");
724         alsaProcess->start("arecord", alsaArgs);
725     }
726     kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
727     captureProcess->start("ffmpeg", m_captureArgs);
728 }*/
729
730 void RecMonitor::slotProcessStatus(QProcess::ProcessState status)
731 {
732     if (status == QProcess::NotRunning) {
733         m_displayProcess->kill();
734         if (m_isCapturing && device_selector->currentIndex() != FIREWIRE)
735             if (autoaddbox->isChecked() && !m_captureFile.isEmpty() && QFile::exists(m_captureFile.path())) {
736                 emit addProjectClip(m_captureFile);
737                 m_captureFile.clear();
738             }
739         if (device_selector->currentIndex() == FIREWIRE) {
740             m_discAction->setIcon(KIcon("network-connect"));
741             m_discAction->setText(i18n("Connect"));
742             m_playAction->setEnabled(false);
743             m_rewAction->setEnabled(false);
744             m_fwdAction->setEnabled(false);
745             m_recAction->setEnabled(false);
746         }
747         m_isPlaying = false;
748         m_playAction->setIcon(m_playIcon);
749         m_recAction->setChecked(false);
750         m_stopAction->setEnabled(false);
751         device_selector->setEnabled(true);
752         if (m_captureProcess && m_captureProcess->exitStatus() == QProcess::CrashExit) {
753             video_frame->setText(i18n("Capture crashed, please check your parameters"));
754         } else {
755             if (device_selector->currentIndex() != SCREENGRAB) {
756                 video_frame->setText(i18n("Not connected"));
757             } else {
758                 if (m_captureProcess->exitCode() != 0) {
759                     video_frame->setText(i18n("Capture crashed, please check your parameters\nRecordMyDesktop exit code: %1", QString::number(m_captureProcess->exitCode())));
760                 } else {
761                     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)));
762                 }
763             }
764         }
765         m_isCapturing = false;
766
767 #if KDE_IS_VERSION(4,2,0)
768         m_spaceTimer.stop();
769         // update free space info
770         slotUpdateFreeSpace();
771 #endif
772
773     } else {
774         if (device_selector->currentIndex() != SCREENGRAB) m_stopAction->setEnabled(true);
775         device_selector->setEnabled(false);
776     }
777 }
778
779 void RecMonitor::manageCapturedFiles()
780 {
781     QString extension;
782     switch (KdenliveSettings::firewireformat()) {
783     case 0:
784         extension = ".dv";
785         break;
786     case 1:
787     case 2:
788         extension = ".avi";
789         break;
790     case 3:
791         extension = ".m2t";
792         break;
793     }
794     QDir dir(m_capturePath);
795     QStringList filters;
796     QString capturename = KdenliveSettings::dvgrabfilename();
797     if (capturename.isEmpty()) capturename = "capture";
798     filters << capturename + "*" + extension;
799     const QStringList result = dir.entryList(filters, QDir::Files, QDir::Time);
800     KUrl::List capturedFiles;
801     foreach(const QString & name, result) {
802         KUrl url = KUrl(dir.filePath(name));
803         if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, this)) {
804             KFileItem file(KFileItem::Unknown, KFileItem::Unknown, url, true);
805             if (file.time(KFileItem::ModificationTime) > m_captureTime) capturedFiles.append(url);
806         }
807     }
808     kDebug() << "Found : " << capturedFiles.count() << " new capture files";
809     kDebug() << capturedFiles;
810
811     if (capturedFiles.count() > 0) {
812         ManageCapturesDialog *d = new ManageCapturesDialog(capturedFiles, this);
813         if (d->exec() == QDialog::Accepted) {
814             capturedFiles = d->importFiles();
815             foreach(const KUrl & url, capturedFiles) {
816                 emit addProjectClip(url);
817             }
818         }
819         delete d;
820     }
821 }
822
823 // virtual
824 void RecMonitor::mousePressEvent(QMouseEvent * /*event*/)
825 {
826 #if KDE_IS_VERSION(4,2,0)
827     if (m_freeSpace->underMouse()) slotUpdateFreeSpace();
828 #endif
829 }
830
831 void RecMonitor::slotUpdateFreeSpace()
832 {
833 #if KDE_IS_VERSION(4,2,0)
834     KDiskFreeSpaceInfo info = KDiskFreeSpaceInfo::freeSpaceInfo(m_capturePath);
835     if (info.isValid()) {
836         m_freeSpace->setValue(100 * info.used() / info.size());
837         m_freeSpace->setText(i18n("Free space: %1", KIO::convertSize(info.available())));
838         m_freeSpace->update();
839     }
840 #endif
841 }
842
843 void RecMonitor::refreshRecMonitor(bool visible)
844 {
845     if (visible) {
846         //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
847
848     }
849 }
850
851 void RecMonitor::slotPlay()
852 {
853
854     //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
855
856 }
857
858 void RecMonitor::slotReadDvgrabInfo()
859 {
860     QString data = m_captureProcess->readAllStandardError().simplified();
861     data = data.section('"', 2, 2).simplified();
862     m_dvinfo.setText(data.left(11));
863     m_dvinfo.updateGeometry();
864 }
865
866 AbstractRender *RecMonitor::abstractRender()
867 {
868     return m_captureDevice;
869 }
870
871
872 void RecMonitor::analyseFrames(bool analyse)
873 {
874     m_analyse = analyse;
875     if (m_captureDevice) m_captureDevice->sendFrameForAnalysis = analyse;
876 }
877
878 void RecMonitor::slotDroppedFrames(int dropped)
879 {
880     slotSetInfoMessage(i18n("%1 dropped frames", dropped));
881 }
882
883 void RecMonitor::buildMltDevice(const QString &path)
884 {
885     if (m_captureDevice == NULL) {
886         m_captureDevice = new MltDeviceCapture(path, m_videoBox, this);
887         connect(m_captureDevice, SIGNAL(droppedFrames(int)), this, SLOT(slotDroppedFrames(int)));
888         m_captureDevice->sendFrameForAnalysis = m_analyse;
889         m_manager->updateScopeSource();
890     }
891 }
892
893
894 #include "recmonitor.moc"
895