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