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