]> git.sesse.net Git - kdenlive/blob - src/recmonitor.cpp
- Fix recordMyDesktop arguments: they mostly start with "--" instead of "-".
[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();
136
137         captureCommand +=  " -f " + KdenliveSettings::video4vformat() + " -s " + KdenliveSettings::video4size() + " -r " + QString::number(KdenliveSettings::video4rate()) + " -i " + KdenliveSettings::video4vdevice();
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         m_captureProcess->write("q\n", 3);
319         QTimer::singleShot(1000, m_captureProcess, SLOT(kill()));
320
321         break;
322     case SCREENGRAB:
323         m_captureProcess->write("q\n", 3);
324         QTimer::singleShot(1000, m_captureProcess, SLOT(kill()));
325         break;
326     default:
327         break;
328     }
329 }
330
331 void RecMonitor::slotStartCapture(bool play)
332 {
333     if (m_captureProcess->state() != QProcess::NotRunning) {
334         if (device_selector->currentIndex() == FIREWIRE) {
335             if (m_isPlaying) {
336                 m_captureProcess->write("k", 1);
337                 //captureProcess->write("\e", 2);
338                 m_playAction->setIcon(m_playIcon);
339                 m_isPlaying = false;
340             } else {
341                 m_captureProcess->write("p", 1);
342                 m_playAction->setIcon(m_pauseIcon);
343                 m_isPlaying = true;
344             }
345         }
346         return;
347     }
348     m_captureArgs.clear();
349     m_displayArgs.clear();
350     m_isPlaying = false;
351     QString capturename = KdenliveSettings::dvgrabfilename();
352     QStringList dvargs = KdenliveSettings::dvgrabextra().simplified().split(" ", QString::SkipEmptyParts);
353
354     switch (device_selector->currentIndex()) {
355     case FIREWIRE:
356         switch (KdenliveSettings::firewireformat()) {
357         case 0:
358             // RAW DV CAPTURE
359             m_captureArgs << "--format" << "raw";
360             m_displayArgs << "-f" << "dv";
361             break;
362         case 1:
363             // DV type 1
364             m_captureArgs << "--format" << "dv1";
365             m_displayArgs << "-f" << "dv";
366             break;
367         case 2:
368             // DV type 2
369             m_captureArgs << "--format" << "dv2";
370             m_displayArgs << "-f" << "dv";
371             break;
372         case 3:
373             // HDV CAPTURE
374             m_captureArgs << "--format" << "hdv";
375             m_displayArgs << "-f" << "mpegts";
376             break;
377         }
378         if (KdenliveSettings::firewireautosplit()) m_captureArgs << "--autosplit";
379         if (KdenliveSettings::firewiretimestamp()) m_captureArgs << "--timestamp";
380         if (!dvargs.isEmpty()) {
381             m_captureArgs << dvargs;
382         }
383         m_captureArgs << "-i";
384         if (capturename.isEmpty()) capturename = "capture";
385         m_captureArgs << capturename << "-";
386
387         m_displayArgs << "-x" << QString::number(video_frame->width()) << "-y" << QString::number(video_frame->height()) << "-";
388
389         m_captureProcess->setStandardOutputProcess(m_displayProcess);
390         m_captureProcess->setWorkingDirectory(KdenliveSettings::capturefolder());
391         kDebug() << "Capture: Running dvgrab " << m_captureArgs.join(" ");
392
393         m_captureProcess->start(KdenliveSettings::dvgrab_path(), m_captureArgs);
394         if (play) m_captureProcess->write(" ", 1);
395         m_discAction->setEnabled(true);
396         break;
397     case VIDEO4LINUX:
398         m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << KdenliveSettings::video4encoding().simplified().split(' ') << "-f" << "mpegts" << "-vcodec" << "mpeg4" << "-acodec" << "mp2" << "-";
399         m_displayArgs << "-f" << "mpegts" << "-x" << QString::number(video_frame->width()) << "-y" << QString::number(video_frame->height()) << "-";
400         m_captureProcess->setStandardOutputProcess(m_displayProcess);
401         kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
402         m_captureProcess->start("ffmpeg", m_captureArgs);
403         break;
404     default:
405         break;
406     }
407
408     if (device_selector->currentIndex() != SCREENGRAB) {
409         kDebug() << "Capture: Running ffplay " << m_displayArgs.join(" ");
410         m_displayProcess->start("ffplay", m_displayArgs);
411         video_frame->setText(i18n("Initialising..."));
412     } else {
413         // do something when starting screen grab
414     }
415 }
416
417 void RecMonitor::slotRecord()
418 {
419     if (m_captureProcess->state() == QProcess::NotRunning && device_selector->currentIndex() == FIREWIRE) {
420         slotStartCapture();
421     }
422     if (m_isCapturing) {
423         switch (device_selector->currentIndex()) {
424         case FIREWIRE:
425             m_captureProcess->write("\e", 2);
426             m_playAction->setIcon(m_playIcon);
427             m_isCapturing = false;
428             m_isPlaying = false;
429             m_recAction->setChecked(false);
430             break;
431         case VIDEO4LINUX:
432             m_captureProcess->terminate();
433             slotStopCapture();
434             //m_isCapturing = false;
435             QTimer::singleShot(1000, this, SLOT(slotStartCapture()));
436             break;
437         case SCREENGRAB:
438             //captureProcess->write("q\n", 3);
439             m_captureProcess->terminate();
440             video_frame->setText(i18n("Encoding captured video..."));
441             // in case ffmpeg doesn't exit with the 'q' command, kill it one second later
442             //QTimer::singleShot(1000, captureProcess, SLOT(kill()));
443             break;
444         }
445         return;
446     } else if (device_selector->currentIndex() == FIREWIRE) {
447         m_isCapturing = true;
448         m_didCapture = true;
449         m_captureProcess->write("c\n", 3);
450 #if KDE_IS_VERSION(4,2,0)
451         m_spaceTimer.start();
452 #endif
453         return;
454     }
455     if (m_captureProcess->state() == QProcess::NotRunning) {
456         m_recAction->setChecked(true);
457         QString extension = "mp4";
458         if (device_selector->currentIndex() == SCREENGRAB) extension = "ogv"; //KdenliveSettings::screengrabextension();
459         QString path = KdenliveSettings::capturefolder() + "/capture0000." + extension;
460         int i = 1;
461         while (QFile::exists(path)) {
462             QString num = QString::number(i).rightJustified(4, '0', false);
463             path = KdenliveSettings::capturefolder() + "/capture" + num + '.' + extension;
464             i++;
465         }
466         m_captureFile = KUrl(path);
467
468         m_captureArgs.clear();
469         m_displayArgs.clear();
470         QString args;
471         QString capturename = KdenliveSettings::dvgrabfilename();
472         if (capturename.isEmpty()) capturename = "capture";
473
474         switch (device_selector->currentIndex()) {
475         case VIDEO4LINUX:
476             m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << KdenliveSettings::video4encoding().simplified().split(' ') << "-vcodec" << "mpeg4" << "-acodec" << "mp2" << "-y" << m_captureFile.path() << "-f" << "mpegts" << "-vcodec" << "mpeg4" << "-acodec" << "mp2" << "-";
477             m_displayArgs << "-f" << "mpegts" << "-x" << QString::number(video_frame->width()) << "-y" << QString::number(video_frame->height()) << "-";
478             m_captureProcess->setStandardOutputProcess(m_displayProcess);
479             kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
480             m_captureProcess->start("ffmpeg", m_captureArgs);
481             break;
482         case SCREENGRAB:
483             switch (KdenliveSettings::rmd_capture_type()) {
484             case 0:
485                 // Full screen capture, nothing special to do
486                 break;
487             default:
488                 // Region capture
489                 m_captureArgs << "--width" << QString::number(KdenliveSettings::rmd_width()) << "--height" << QString::number(KdenliveSettings::rmd_height());
490                 if (!KdenliveSettings::rmd_follow_mouse()) {
491                     m_captureArgs << "-x" << QString::number(KdenliveSettings::rmd_offsetx()) << "-y" << QString::number(KdenliveSettings::rmd_offsety());
492                 } else {
493                     m_captureArgs << "--follow-mouse";
494                     if (KdenliveSettings::rmd_hide_frame()) m_captureArgs << "--no-frame";
495                 }
496                 break;
497             }
498             if (KdenliveSettings::rmd_hide_mouse()) m_captureArgs << "--no-cursor";
499             m_isCapturing = true;
500             if (KdenliveSettings::rmd_capture_audio()) {
501                 m_captureArgs << "--freq" << KdenliveSettings::rmd_freq();
502                 m_captureArgs << "--channels" << QString::number(KdenliveSettings::rmd_audio_channels());
503                 if (KdenliveSettings::rmd_use_jack()) {
504                     m_captureArgs << "--use-jack";
505                     QStringList ports = KdenliveSettings::rmd_jackports().split(" ", QString::SkipEmptyParts);
506                     for(int i = 0; i < ports.count(); ++i) {
507                         m_captureArgs << ports.at(i);
508                     }
509                     if (KdenliveSettings::rmd_jack_buffer() > 0.0)
510                         m_captureArgs << "--ring-buffer-size" << QString::number(KdenliveSettings::rmd_jack_buffer());
511                 } else {
512                     if (!KdenliveSettings::rmd_alsadevicename().isEmpty())
513                         m_captureArgs << "--device" << KdenliveSettings::rmd_alsadevicename();
514                     if (KdenliveSettings::rmd_alsa_buffer() > 0)
515                         m_captureArgs << "--buffer-size" << QString::number(KdenliveSettings::rmd_alsa_buffer());
516                 }
517             } else m_captureArgs << "--no-sound";
518
519             if (KdenliveSettings::rmd_fullshots()) m_captureArgs << "--full-shots";
520             m_captureArgs << "--v_bitrate" << QString::number(KdenliveSettings::rmd_bitrate());
521             m_captureArgs << "--v_quality" << QString::number(KdenliveSettings::rmd_quality());
522             m_captureArgs << "--workdir" << KdenliveSettings::currenttmpfolder();
523             m_captureArgs << "--fps" << QString::number(KdenliveSettings::rmd_fps()) << "-o" << m_captureFile.path();
524             m_captureProcess->start(KdenliveSettings::rmd_path(), m_captureArgs);
525             kDebug() << "// RecordMyDesktop params: " << m_captureArgs;
526             break;
527         default:
528             break;
529         }
530
531
532         if (device_selector->currentIndex() != SCREENGRAB) {
533             m_isCapturing = true;
534             kDebug() << "Capture: Running ffplay " << m_displayArgs.join(" ");
535             m_displayProcess->start("ffplay", m_displayArgs);
536             video_frame->setText(i18n("Initialising..."));
537         }
538     } else {
539         // stop capture
540         m_displayProcess->kill();
541         //captureProcess->kill();
542         QTimer::singleShot(1000, this, SLOT(slotRecord()));
543     }
544 }
545
546 /*
547 void RecMonitor::slotStartGrab(const QRect &rect) {
548     rgnGrab->deleteLater();
549     QApplication::restoreOverrideCursor();
550     show();
551     if (rect.isNull()) return;
552     int width = rect.width();
553     int height = rect.height();
554     if (width % 2 != 0) width--;
555     if (height % 2 != 0) height--;
556     QString args = KdenliveSettings::screengrabcapture().replace("%size", QString::number(width) + "x" + QString::number(height)).replace("%offset", "+" + QString::number(rect.x()) + "," + QString::number(rect.y()));
557     if (KdenliveSettings::screengrabenableaudio()) {
558         // also capture audio
559         if (KdenliveSettings::useosscapture()) m_captureArgs << KdenliveSettings::screengrabosscapture().simplified().split(' ');
560         else m_captureArgs << KdenliveSettings::screengrabalsacapture2().simplified().split(' ');
561     }
562     m_captureArgs << args.simplified().split(' ') << KdenliveSettings::screengrabencoding().simplified().split(' ') << m_captureFile.path();
563     m_isCapturing = true;
564     video_frame->setText(i18n("Capturing..."));
565     if (KdenliveSettings::screengrabenableaudio() && !KdenliveSettings::useosscapture()) {
566         QStringList alsaArgs = KdenliveSettings::screengrabalsacapture().simplified().split(' ');
567         alsaProcess->setStandardOutputProcess(captureProcess);
568         kDebug() << "Capture: Running arecord " << alsaArgs.join(" ");
569         alsaProcess->start("arecord", alsaArgs);
570     }
571     kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
572     captureProcess->start("ffmpeg", m_captureArgs);
573 }*/
574
575 void RecMonitor::slotProcessStatus(QProcess::ProcessState status)
576 {
577     if (status == QProcess::NotRunning) {
578         m_displayProcess->kill();
579         if (m_isCapturing && device_selector->currentIndex() != FIREWIRE)
580             if (autoaddbox->isChecked() && QFile::exists(m_captureFile.path())) emit addProjectClip(m_captureFile);
581         if (device_selector->currentIndex() == FIREWIRE) {
582             m_discAction->setIcon(KIcon("network-connect"));
583             m_discAction->setText(i18n("Connect"));
584             m_playAction->setEnabled(false);
585             m_rewAction->setEnabled(false);
586             m_fwdAction->setEnabled(false);
587             m_recAction->setEnabled(false);
588         }
589         m_isPlaying = false;
590         m_playAction->setIcon(m_playIcon);
591         m_recAction->setChecked(false);
592         m_stopAction->setEnabled(false);
593         device_selector->setEnabled(true);
594         if (m_captureProcess && m_captureProcess->exitStatus() == QProcess::CrashExit) {
595             video_frame->setText(i18n("Capture crashed, please check your parameters"));
596         } else {
597             if (device_selector->currentIndex() != SCREENGRAB) video_frame->setText(i18n("Not connected"));
598             else 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())));
599         }
600         m_isCapturing = false;
601
602 #if KDE_IS_VERSION(4,2,0)
603         m_spaceTimer.stop();
604         // update free space info
605         slotUpdateFreeSpace();
606 #endif
607
608     } else {
609         if (device_selector->currentIndex() != SCREENGRAB) m_stopAction->setEnabled(true);
610         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     QString capturename = KdenliveSettings::dvgrabfilename();
632     if (capturename.isEmpty()) capturename = "capture";
633     filters << capturename + "*" + extension;
634     const QStringList result = dir.entryList(filters, QDir::Files, QDir::Time);
635     KUrl::List capturedFiles;
636     foreach(const QString &name, result) {
637         KUrl url = KUrl(dir.filePath(name));
638         if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, this)) {
639             KFileItem file(KFileItem::Unknown, KFileItem::Unknown, url, true);
640             if (file.time(KFileItem::ModificationTime) > m_captureTime) capturedFiles.append(url);
641         }
642     }
643     kDebug() << "Found : " << capturedFiles.count() << " new capture files";
644     kDebug() << capturedFiles;
645
646     if (capturedFiles.count() > 0) {
647         ManageCapturesDialog *d = new ManageCapturesDialog(capturedFiles, this);
648         if (d->exec() == QDialog::Accepted) {
649             capturedFiles = d->importFiles();
650             foreach(const KUrl &url, capturedFiles) {
651                 emit addProjectClip(url);
652             }
653         }
654         delete d;
655     }
656 }
657
658 // virtual
659 void RecMonitor::mousePressEvent(QMouseEvent * /*event*/)
660 {
661 #if KDE_IS_VERSION(4,2,0)
662     if (m_freeSpace->underMouse()) slotUpdateFreeSpace();
663 #endif
664 }
665
666 void RecMonitor::slotUpdateFreeSpace()
667 {
668 #if KDE_IS_VERSION(4,2,0)
669     KDiskFreeSpaceInfo info = KDiskFreeSpaceInfo::freeSpaceInfo(KdenliveSettings::capturefolder());
670     if (info.isValid()) {
671         m_freeSpace->setValue(100 * info.used() / info.size());
672         m_freeSpace->setText(i18n("Free space: %1", KIO::convertSize(info.available())));
673         m_freeSpace->update();
674     }
675 #endif
676 }
677
678 void RecMonitor::activateRecMonitor()
679 {
680     //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
681 }
682
683 void RecMonitor::stop()
684 {
685     m_isActive = false;
686
687 }
688
689 void RecMonitor::start()
690 {
691     m_isActive = true;
692
693 }
694
695 void RecMonitor::refreshRecMonitor(bool visible)
696 {
697     if (visible) {
698         //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
699
700     }
701 }
702
703 void RecMonitor::slotPlay()
704 {
705
706     //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
707
708 }
709
710 void RecMonitor::slotReadDvgrabInfo()
711 {
712     QString data = m_captureProcess->readAllStandardError().simplified();
713     data = data.section('"', 2, 2).simplified();
714     m_dvinfo.setText(data.left(11));
715     m_dvinfo.updateGeometry();
716 }
717
718 #include "recmonitor.moc"
719