]> git.sesse.net Git - kdenlive/blob - src/recmonitor.cpp
Improve webcam capture parameters
[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 {
55     setupUi(this);
56
57     video_frame->setAttribute(Qt::WA_PaintOnScreen);
58     device_selector->setCurrentIndex(KdenliveSettings::defaultcapture());
59     connect(device_selector, SIGNAL(currentIndexChanged(int)), this, SLOT(slotVideoDeviceChanged(int)));
60
61
62
63     QToolBar *toolbar = new QToolBar(name, 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
96     layout->addWidget(&m_dvinfo);
97
98 #if KDE_IS_VERSION(4,2,0)
99     m_freeSpace = new KCapacityBar(KCapacityBar::DrawTextInline, this);
100     m_freeSpace->setMaximumWidth(150);
101     QFontMetricsF fontMetrics(font());
102     m_freeSpace->setMaximumHeight(fontMetrics.height() * 1.2);
103     slotUpdateFreeSpace();
104     layout->addWidget(m_freeSpace);
105     connect(&m_spaceTimer, SIGNAL(timeout()), this, SLOT(slotUpdateFreeSpace()));
106     m_spaceTimer.setInterval(30000);
107     m_spaceTimer.setSingleShot(false);
108 #endif
109
110     control_frame_firewire->setLayout(layout);
111
112     slotVideoDeviceChanged(device_selector->currentIndex());
113     m_displayProcess = new QProcess;
114     m_captureProcess = new QProcess;
115
116     connect(m_captureProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(slotProcessStatus(QProcess::ProcessState)));
117     connect(m_captureProcess, SIGNAL(readyReadStandardError()), this, SLOT(slotReadDvgrabInfo()));
118
119     QStringList env = QProcess::systemEnvironment();
120     env << "SDL_WINDOWID=" + QString::number(video_frame->winId());
121
122     QString videoDriver = KdenliveSettings::videodrivername();
123     if (!videoDriver.isEmpty()) {
124         if (videoDriver == "x11_noaccel") {
125             env << "SDL_VIDEO_YUV_HWACCEL=0";
126             env << "SDL_VIDEODRIVER=x11";
127         } else env << "SDL_VIDEODRIVER=" + videoDriver;
128     }
129     setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 1);
130
131     m_displayProcess->setEnvironment(env);
132
133     if (KdenliveSettings::video4capture().isEmpty()) {
134         QString captureCommand;
135         if (!KdenliveSettings::video4adevice().isEmpty()) captureCommand = "-f " + KdenliveSettings::video4aformat() + " -i " + KdenliveSettings::video4adevice() + " -acodec " + KdenliveSettings::video4acodec();
136
137         captureCommand +=  " -f " + KdenliveSettings::video4vformat() + " -s " + KdenliveSettings::video4size() + " -r " + QString::number(KdenliveSettings::video4rate()) + " -i " + KdenliveSettings::video4vdevice() + " -vcodec " + KdenliveSettings::video4vcodec();;
138         KdenliveSettings::setVideo4capture(captureCommand);
139     }
140
141     kDebug() << "/////// BUILDING MONITOR, ID: " << video_frame->winId();
142 }
143
144 RecMonitor::~RecMonitor()
145 {
146 #if KDE_IS_VERSION(4,2,0)
147     m_spaceTimer.stop();
148 #endif
149     delete m_captureProcess;
150     delete m_displayProcess;
151 }
152
153 QString RecMonitor::name() const
154 {
155     return m_name;
156 }
157
158 void RecMonitor::slotConfigure()
159 {
160     emit showConfigDialog(4, device_selector->currentIndex());
161 }
162
163 void RecMonitor::slotUpdateCaptureFolder()
164 {
165     if (m_captureProcess) m_captureProcess->setWorkingDirectory(KdenliveSettings::capturefolder());
166     if (m_captureProcess->state() != QProcess::NotRunning) {
167         if (device_selector->currentIndex() == FIREWIRE)
168             KMessageBox::information(this, i18n("You need to disconnect and reconnect in the capture monitor to apply your changes"), i18n("Capturing"));
169         else KMessageBox::information(this, i18n("You need to stop capture before your changes can be applied"), i18n("Capturing"));
170     } else slotVideoDeviceChanged(device_selector->currentIndex());
171     kDebug() << "// UPDATE CAPT FOLD: " << KdenliveSettings::capturefolder();
172
173 #if KDE_IS_VERSION(4,2,0)
174     // update free space info
175     slotUpdateFreeSpace();
176 #endif
177 }
178
179 void RecMonitor::slotVideoDeviceChanged(int ix)
180 {
181     switch (ix) {
182     case SCREENGRAB:
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(false);
189         if (KdenliveSettings::rmd_path().isEmpty()) {
190             QString rmdpath = KStandardDirs::findExe("recordmydesktop");
191             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")));
192             else KdenliveSettings::setRmd_path(rmdpath);
193         }
194         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", KdenliveSettings::capturefolder())));
195         //video_frame->setText(i18n("Press record button\nto start screen capture"));
196         break;
197     case VIDEO4LINUX:
198         m_discAction->setEnabled(false);
199         m_rewAction->setEnabled(false);
200         m_fwdAction->setEnabled(false);
201         m_recAction->setEnabled(true);
202         m_stopAction->setEnabled(false);
203         m_playAction->setEnabled(true);
204         checkDeviceAvailability();
205         break;
206     default: // FIREWIRE
207         m_discAction->setEnabled(true);
208         m_recAction->setEnabled(false);
209         m_stopAction->setEnabled(false);
210         m_playAction->setEnabled(false);
211         m_rewAction->setEnabled(false);
212         m_fwdAction->setEnabled(false);
213
214         // Check that dvgab is available
215         if (KdenliveSettings::dvgrab_path().isEmpty()) {
216             QString dvgrabpath = KStandardDirs::findExe("dvgrab");
217             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")));
218             else KdenliveSettings::setDvgrab_path(dvgrabpath);
219         } else {
220             // Show capture info
221             QString capturefile = KdenliveSettings::capturefolder();
222             if (!capturefile.endsWith("/")) capturefile.append("/");
223             QString capturename = KdenliveSettings::dvgrabfilename();
224             if (capturename.isEmpty()) capturename = "capture";
225             QString extension;
226             switch (KdenliveSettings::firewireformat()) {
227             case 0:
228                 extension = ".dv";
229                 break;
230             case 1:
231             case 2:
232                 extension = ".avi";
233                 break;
234             case 3:
235                 extension = ".m2t";
236                 break;
237             }
238             capturename.append("xxx" + extension);
239             capturefile.append(capturename);
240             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)));
241         }
242         break;
243     }
244 }
245
246 QPixmap RecMonitor::mergeSideBySide(const QPixmap& pix, const QString txt)
247 {
248     QPainter p;
249     QRect r = QApplication::fontMetrics().boundingRect(QRect(0, 0, video_frame->width(), video_frame->height()), Qt::AlignLeft, txt);
250     int strWidth = r.width();
251     int strHeight = r.height();
252     int pixWidth = pix.width();
253     int pixHeight = pix.height();
254     QPixmap res(strWidth + 8 + pixWidth, qMax(strHeight, pixHeight));
255     res.fill(Qt::transparent);
256     p.begin(&res);
257     p.drawPixmap(0, 0, pix);
258     p.setPen(kapp->palette().text().color());
259     p.drawText(QRect(pixWidth + 8, 0, strWidth, strHeight), 0, txt);
260     p.end();
261     return res;
262 }
263
264
265 void RecMonitor::checkDeviceAvailability()
266 {
267     if (!KIO::NetAccess::exists(KUrl(KdenliveSettings::video4vdevice()), KIO::NetAccess::SourceSide , this)) {
268         m_playAction->setEnabled(false);
269         m_recAction->setEnabled(false);
270         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())));
271         //video_frame->setText(i18n("Cannot read from device %1\nPlease check drivers and access rights.", KdenliveSettings::video4vdevice()));
272     } else //video_frame->setText(i18n("Press play or record button\nto start video capture"));
273         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())));
274 }
275
276 void RecMonitor::slotDisconnect()
277 {
278     if (m_captureProcess->state() == QProcess::NotRunning) {
279         m_captureTime = KDateTime::currentLocalDateTime();
280         kDebug() << "CURRENT TIME: " << m_captureTime.toString();
281         m_didCapture = false;
282         slotStartCapture(false);
283         m_discAction->setIcon(KIcon("network-disconnect"));
284         m_discAction->setText(i18n("Disconnect"));
285         m_recAction->setEnabled(true);
286         m_stopAction->setEnabled(true);
287         m_playAction->setEnabled(true);
288         m_rewAction->setEnabled(true);
289         m_fwdAction->setEnabled(true);
290     } else {
291         m_captureProcess->write("q", 1);
292         QTimer::singleShot(1000, m_captureProcess, SLOT(kill()));
293         if (m_didCapture) manageCapturedFiles();
294         m_didCapture = false;
295     }
296 }
297
298 void RecMonitor::slotRewind()
299 {
300     m_captureProcess->write("a", 1);
301 }
302
303 void RecMonitor::slotForward()
304 {
305     m_captureProcess->write("z", 1);
306 }
307
308 void RecMonitor::slotStopCapture()
309 {
310     // stop capture
311     switch (device_selector->currentIndex()) {
312     case FIREWIRE:
313         m_captureProcess->write("\e", 2);
314         m_playAction->setIcon(m_playIcon);
315         m_isPlaying = false;
316         break;
317     case VIDEO4LINUX:
318     case SCREENGRAB:
319         m_captureProcess->write("q\n", 3);
320         QTimer::singleShot(1000, m_captureProcess, SLOT(kill()));
321         break;
322     default:
323         break;
324     }
325 }
326
327 void RecMonitor::slotStartCapture(bool play)
328 {
329     if (m_captureProcess->state() != QProcess::NotRunning) {
330         if (device_selector->currentIndex() == FIREWIRE) {
331             if (m_isPlaying) {
332                 m_captureProcess->write("k", 1);
333                 //captureProcess->write("\e", 2);
334                 m_playAction->setIcon(m_playIcon);
335                 m_isPlaying = false;
336             } else {
337                 m_captureProcess->write("p", 1);
338                 m_playAction->setIcon(m_pauseIcon);
339                 m_isPlaying = true;
340             }
341         }
342         return;
343     }
344     m_captureArgs.clear();
345     m_displayArgs.clear();
346     m_isPlaying = false;
347     QString capturename = KdenliveSettings::dvgrabfilename();
348     QStringList dvargs = KdenliveSettings::dvgrabextra().simplified().split(" ", QString::SkipEmptyParts);
349
350     switch (device_selector->currentIndex()) {
351     case FIREWIRE:
352         switch (KdenliveSettings::firewireformat()) {
353         case 0:
354             // RAW DV CAPTURE
355             m_captureArgs << "--format" << "raw";
356             m_displayArgs << "-f" << "dv";
357             break;
358         case 1:
359             // DV type 1
360             m_captureArgs << "--format" << "dv1";
361             m_displayArgs << "-f" << "dv";
362             break;
363         case 2:
364             // DV type 2
365             m_captureArgs << "--format" << "dv2";
366             m_displayArgs << "-f" << "dv";
367             break;
368         case 3:
369             // HDV CAPTURE
370             m_captureArgs << "--format" << "hdv";
371             m_displayArgs << "-f" << KdenliveSettings::video4container();
372             break;
373         }
374         if (KdenliveSettings::firewireautosplit()) m_captureArgs << "--autosplit";
375         if (KdenliveSettings::firewiretimestamp()) m_captureArgs << "--timestamp";
376         if (!dvargs.isEmpty()) {
377             m_captureArgs << dvargs;
378         }
379         m_captureArgs << "-i";
380         if (capturename.isEmpty()) capturename = "capture";
381         m_captureArgs << capturename << "-";
382
383         m_displayArgs << "-x" << QString::number(video_frame->width()) << "-y" << QString::number(video_frame->height()) << "-";
384
385         m_captureProcess->setStandardOutputProcess(m_displayProcess);
386         m_captureProcess->setWorkingDirectory(KdenliveSettings::capturefolder());
387         kDebug() << "Capture: Running dvgrab " << m_captureArgs.join(" ");
388
389         m_captureProcess->start(KdenliveSettings::dvgrab_path(), m_captureArgs);
390         if (play) m_captureProcess->write(" ", 1);
391         m_discAction->setEnabled(true);
392         break;
393     case VIDEO4LINUX:
394         m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << KdenliveSettings::video4encoding().simplified().split(' ') << "-f" << KdenliveSettings::video4container() << "-";
395         m_displayArgs << "-f" << KdenliveSettings::video4container() << "-x" << QString::number(video_frame->width()) << "-y" << QString::number(video_frame->height()) << "-";
396         m_captureProcess->setStandardOutputProcess(m_displayProcess);
397         kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
398         m_captureProcess->start("ffmpeg", m_captureArgs);
399         break;
400     default:
401         break;
402     }
403
404     if (device_selector->currentIndex() != SCREENGRAB) {
405         kDebug() << "Capture: Running ffplay " << m_displayArgs.join(" ");
406         m_displayProcess->start("ffplay", m_displayArgs);
407         video_frame->setText(i18n("Initialising..."));
408     } else {
409         // do something when starting screen grab
410     }
411 }
412
413 void RecMonitor::slotRecord()
414 {
415     if (m_captureProcess->state() == QProcess::NotRunning && device_selector->currentIndex() == FIREWIRE) {
416         slotStartCapture();
417     }
418     if (m_isCapturing) {
419         switch (device_selector->currentIndex()) {
420         case FIREWIRE:
421             m_captureProcess->write("\e", 2);
422             m_playAction->setIcon(m_playIcon);
423             m_isCapturing = false;
424             m_isPlaying = false;
425             m_recAction->setChecked(false);
426             break;
427         case VIDEO4LINUX:
428             m_captureProcess->terminate();
429             slotStopCapture();
430             //m_isCapturing = false;
431             QTimer::singleShot(1000, this, SLOT(slotStartCapture()));
432             break;
433         case SCREENGRAB:
434             //captureProcess->write("q\n", 3);
435             m_captureProcess->terminate();
436             video_frame->setText(i18n("Encoding captured video..."));
437             // in case ffmpeg doesn't exit with the 'q' command, kill it one second later
438             //QTimer::singleShot(1000, captureProcess, SLOT(kill()));
439             break;
440         }
441         return;
442     } else if (device_selector->currentIndex() == FIREWIRE) {
443         m_isCapturing = true;
444         m_didCapture = true;
445         m_captureProcess->write("c\n", 3);
446 #if KDE_IS_VERSION(4,2,0)
447         m_spaceTimer.start();
448 #endif
449         return;
450     }
451     if (m_captureProcess->state() == QProcess::NotRunning) {
452         m_recAction->setChecked(true);
453         QString extension = "mp4";
454         if (device_selector->currentIndex() == SCREENGRAB) extension = "ogv"; //KdenliveSettings::screengrabextension();
455         else if (device_selector->currentIndex() == VIDEO4LINUX) extension = KdenliveSettings::video4extension();
456         QString path = KdenliveSettings::capturefolder() + "/capture0000." + extension;
457         int i = 1;
458         while (QFile::exists(path)) {
459             QString num = QString::number(i).rightJustified(4, '0', false);
460             path = KdenliveSettings::capturefolder() + "/capture" + num + '.' + extension;
461             i++;
462         }
463         m_captureFile = KUrl(path);
464
465         m_captureArgs.clear();
466         m_displayArgs.clear();
467         QString args;
468         QString capturename = KdenliveSettings::dvgrabfilename();
469         if (capturename.isEmpty()) capturename = "capture";
470
471         switch (device_selector->currentIndex()) {
472         case VIDEO4LINUX:
473             m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << KdenliveSettings::video4encoding().simplified().split(' ') << "-y" << m_captureFile.path() << "-f" << KdenliveSettings::video4container() << "-acodec" << KdenliveSettings::video4acodec() << "-vcodec" << KdenliveSettings::video4vcodec() << "-";
474             m_displayArgs << "-f" << KdenliveSettings::video4container() << "-x" << QString::number(video_frame->width()) << "-y" << QString::number(video_frame->height()) << "-";
475             m_captureProcess->setStandardOutputProcess(m_displayProcess);
476             kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
477             m_captureProcess->start("ffmpeg", m_captureArgs);
478             break;
479         case SCREENGRAB:
480             switch (KdenliveSettings::rmd_capture_type()) {
481             case 0:
482                 // Full screen capture, nothing special to do
483                 break;
484             default:
485                 // Region capture
486                 m_captureArgs << "--width" << QString::number(KdenliveSettings::rmd_width()) << "--height" << QString::number(KdenliveSettings::rmd_height());
487                 if (!KdenliveSettings::rmd_follow_mouse()) {
488                     m_captureArgs << "-x" << QString::number(KdenliveSettings::rmd_offsetx()) << "-y" << QString::number(KdenliveSettings::rmd_offsety());
489                 } else {
490                     m_captureArgs << "--follow-mouse";
491                     if (KdenliveSettings::rmd_hide_frame()) m_captureArgs << "--no-frame";
492                 }
493                 break;
494             }
495             if (KdenliveSettings::rmd_hide_mouse()) m_captureArgs << "--no-cursor";
496             m_isCapturing = true;
497             if (KdenliveSettings::rmd_capture_audio()) {
498                 m_captureArgs << "--freq" << KdenliveSettings::rmd_freq();
499                 m_captureArgs << "--channels" << QString::number(KdenliveSettings::rmd_audio_channels());
500                 if (KdenliveSettings::rmd_use_jack()) {
501                     m_captureArgs << "--use-jack";
502                     QStringList ports = KdenliveSettings::rmd_jackports().split(" ", QString::SkipEmptyParts);
503                     for (int i = 0; i < ports.count(); ++i) {
504                         m_captureArgs << ports.at(i);
505                     }
506                     if (KdenliveSettings::rmd_jack_buffer() > 0.0)
507                         m_captureArgs << "--ring-buffer-size" << QString::number(KdenliveSettings::rmd_jack_buffer());
508                 } else {
509                     if (!KdenliveSettings::rmd_alsadevicename().isEmpty())
510                         m_captureArgs << "--device" << KdenliveSettings::rmd_alsadevicename();
511                     if (KdenliveSettings::rmd_alsa_buffer() > 0)
512                         m_captureArgs << "--buffer-size" << QString::number(KdenliveSettings::rmd_alsa_buffer());
513                 }
514             } else m_captureArgs << "--no-sound";
515
516             if (KdenliveSettings::rmd_fullshots()) m_captureArgs << "--full-shots";
517             m_captureArgs << "--v_bitrate" << QString::number(KdenliveSettings::rmd_bitrate());
518             m_captureArgs << "--v_quality" << QString::number(KdenliveSettings::rmd_quality());
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 (device_selector->currentIndex() != SCREENGRAB) {
530             m_isCapturing = true;
531             kDebug() << "Capture: Running ffplay " << m_displayArgs.join(" ");
532             m_displayProcess->start("ffplay", m_displayArgs);
533             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     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 && device_selector->currentIndex() != FIREWIRE)
577             if (autoaddbox->isChecked() && QFile::exists(m_captureFile.path())) emit addProjectClip(m_captureFile);
578         if (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         device_selector->setEnabled(true);
591         if (m_captureProcess && m_captureProcess->exitStatus() == QProcess::CrashExit) {
592             video_frame->setText(i18n("Capture crashed, please check your parameters"));
593         } else {
594             if (device_selector->currentIndex() != SCREENGRAB) {
595                 video_frame->setText(i18n("Not connected"));
596             } else {
597                 if (m_captureProcess->exitCode() != 0) {
598                     video_frame->setText(i18n("Capture crashed, please check your parameters\nRecordMyDesktop exit code: %1", QString::number(m_captureProcess->exitCode())));
599                 } else {
600                     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())));
601                 }
602             }
603         }
604         m_isCapturing = false;
605
606 #if KDE_IS_VERSION(4,2,0)
607         m_spaceTimer.stop();
608         // update free space info
609         slotUpdateFreeSpace();
610 #endif
611
612     } else {
613         if (device_selector->currentIndex() != SCREENGRAB) m_stopAction->setEnabled(true);
614         device_selector->setEnabled(false);
615     }
616 }
617
618 void RecMonitor::manageCapturedFiles()
619 {
620     QString extension;
621     switch (KdenliveSettings::firewireformat()) {
622     case 0:
623         extension = ".dv";
624         break;
625     case 1:
626     case 2:
627         extension = ".avi";
628         break;
629     case 3:
630         extension = ".m2t";
631         break;
632     }
633     QDir dir(KdenliveSettings::capturefolder());
634     QStringList filters;
635     QString capturename = KdenliveSettings::dvgrabfilename();
636     if (capturename.isEmpty()) capturename = "capture";
637     filters << capturename + "*" + extension;
638     const QStringList result = dir.entryList(filters, QDir::Files, QDir::Time);
639     KUrl::List capturedFiles;
640     foreach(const QString &name, result) {
641         KUrl url = KUrl(dir.filePath(name));
642         if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, this)) {
643             KFileItem file(KFileItem::Unknown, KFileItem::Unknown, url, true);
644             if (file.time(KFileItem::ModificationTime) > m_captureTime) capturedFiles.append(url);
645         }
646     }
647     kDebug() << "Found : " << capturedFiles.count() << " new capture files";
648     kDebug() << capturedFiles;
649
650     if (capturedFiles.count() > 0) {
651         ManageCapturesDialog *d = new ManageCapturesDialog(capturedFiles, this);
652         if (d->exec() == QDialog::Accepted) {
653             capturedFiles = d->importFiles();
654             foreach(const KUrl &url, capturedFiles) {
655                 emit addProjectClip(url);
656             }
657         }
658         delete d;
659     }
660 }
661
662 // virtual
663 void RecMonitor::mousePressEvent(QMouseEvent * /*event*/)
664 {
665 #if KDE_IS_VERSION(4,2,0)
666     if (m_freeSpace->underMouse()) slotUpdateFreeSpace();
667 #endif
668 }
669
670 void RecMonitor::slotUpdateFreeSpace()
671 {
672 #if KDE_IS_VERSION(4,2,0)
673     KDiskFreeSpaceInfo info = KDiskFreeSpaceInfo::freeSpaceInfo(KdenliveSettings::capturefolder());
674     if (info.isValid()) {
675         m_freeSpace->setValue(100 * info.used() / info.size());
676         m_freeSpace->setText(i18n("Free space: %1", KIO::convertSize(info.available())));
677         m_freeSpace->update();
678     }
679 #endif
680 }
681
682 void RecMonitor::activateRecMonitor()
683 {
684     //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
685 }
686
687 void RecMonitor::stop()
688 {
689     m_isActive = false;
690
691 }
692
693 void RecMonitor::start()
694 {
695     m_isActive = true;
696
697 }
698
699 void RecMonitor::refreshRecMonitor(bool visible)
700 {
701     if (visible) {
702         //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
703
704     }
705 }
706
707 void RecMonitor::slotPlay()
708 {
709
710     //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
711
712 }
713
714 void RecMonitor::slotReadDvgrabInfo()
715 {
716     QString data = m_captureProcess->readAllStandardError().simplified();
717     data = data.section('"', 2, 2).simplified();
718     m_dvinfo.setText(data.left(11));
719     m_dvinfo.updateGeometry();
720 }
721
722 #include "recmonitor.moc"
723