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