]> git.sesse.net Git - kdenlive/blob - src/recmonitor.cpp
Captured files should go to project folder if wanted:
[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(const QString currentProjectFolder)
164 {
165     if (KdenliveSettings::capturetoprojectfolder()) m_capturePath = currentProjectFolder;
166     else m_capturePath = KdenliveSettings::capturefolder();
167     if (m_captureProcess) m_captureProcess->setWorkingDirectory(m_capturePath);
168     if (m_captureProcess->state() != QProcess::NotRunning) {
169         if (device_selector->currentIndex() == FIREWIRE)
170             KMessageBox::information(this, i18n("You need to disconnect and reconnect in the capture monitor to apply your changes"), i18n("Capturing"));
171         else KMessageBox::information(this, i18n("You need to stop capture before your changes can be applied"), i18n("Capturing"));
172     } else slotVideoDeviceChanged(device_selector->currentIndex());
173
174 #if KDE_IS_VERSION(4,2,0)
175     // update free space info
176     slotUpdateFreeSpace();
177 #endif
178 }
179
180 void RecMonitor::slotVideoDeviceChanged(int ix)
181 {
182     switch (ix) {
183     case SCREENGRAB:
184         m_discAction->setEnabled(false);
185         m_rewAction->setEnabled(false);
186         m_fwdAction->setEnabled(false);
187         m_recAction->setEnabled(true);
188         m_stopAction->setEnabled(false);
189         m_playAction->setEnabled(false);
190         if (KdenliveSettings::rmd_path().isEmpty()) {
191             QString rmdpath = KStandardDirs::findExe("recordmydesktop");
192             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")));
193             else KdenliveSettings::setRmd_path(rmdpath);
194         }
195         if (!KdenliveSettings::rmd_path().isEmpty()) video_frame->setPixmap(mergeSideBySide(KIcon("video-display").pixmap(QSize(50, 50)), i18n("Press record button\nto start screen capture\nFiles will be saved in:\n%1", m_capturePath)));
196         //video_frame->setText(i18n("Press record button\nto start screen capture"));
197         break;
198     case VIDEO4LINUX:
199         m_discAction->setEnabled(false);
200         m_rewAction->setEnabled(false);
201         m_fwdAction->setEnabled(false);
202         m_recAction->setEnabled(true);
203         m_stopAction->setEnabled(false);
204         m_playAction->setEnabled(true);
205         checkDeviceAvailability();
206         break;
207     default: // FIREWIRE
208         m_discAction->setEnabled(true);
209         m_recAction->setEnabled(false);
210         m_stopAction->setEnabled(false);
211         m_playAction->setEnabled(false);
212         m_rewAction->setEnabled(false);
213         m_fwdAction->setEnabled(false);
214
215         // Check that dvgab is available
216         if (KdenliveSettings::dvgrab_path().isEmpty()) {
217             QString dvgrabpath = KStandardDirs::findExe("dvgrab");
218             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")));
219             else KdenliveSettings::setDvgrab_path(dvgrabpath);
220         } else {
221             // Show capture info
222             QString capturefile = m_capturePath;
223             if (!capturefile.endsWith("/")) capturefile.append("/");
224             QString capturename = KdenliveSettings::dvgrabfilename();
225             if (capturename.isEmpty()) capturename = "capture";
226             QString extension;
227             switch (KdenliveSettings::firewireformat()) {
228             case 0:
229                 extension = ".dv";
230                 break;
231             case 1:
232             case 2:
233                 extension = ".avi";
234                 break;
235             case 3:
236                 extension = ".m2t";
237                 break;
238             }
239             capturename.append("xxx" + extension);
240             capturefile.append(capturename);
241             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)));
242         }
243         break;
244     }
245 }
246
247 QPixmap RecMonitor::mergeSideBySide(const QPixmap& pix, const QString txt)
248 {
249     QPainter p;
250     QRect r = QApplication::fontMetrics().boundingRect(QRect(0, 0, video_frame->width(), video_frame->height()), Qt::AlignLeft, txt);
251     int strWidth = r.width();
252     int strHeight = r.height();
253     int pixWidth = pix.width();
254     int pixHeight = pix.height();
255     QPixmap res(strWidth + 8 + pixWidth, qMax(strHeight, pixHeight));
256     res.fill(Qt::transparent);
257     p.begin(&res);
258     p.drawPixmap(0, 0, pix);
259     p.setPen(kapp->palette().text().color());
260     p.drawText(QRect(pixWidth + 8, 0, strWidth, strHeight), 0, txt);
261     p.end();
262     return res;
263 }
264
265
266 void RecMonitor::checkDeviceAvailability()
267 {
268     if (!KIO::NetAccess::exists(KUrl(KdenliveSettings::video4vdevice()), KIO::NetAccess::SourceSide , this)) {
269         m_playAction->setEnabled(false);
270         m_recAction->setEnabled(false);
271         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())));
272         //video_frame->setText(i18n("Cannot read from device %1\nPlease check drivers and access rights.", KdenliveSettings::video4vdevice()));
273     } else //video_frame->setText(i18n("Press play or record button\nto start video capture"));
274         video_frame->setPixmap(mergeSideBySide(KIcon("camera-web").pixmap(QSize(50, 50)), i18n("Press play or record button\nto start video capture\nFiles will be saved in:\n%1", m_capturePath)));
275 }
276
277 void RecMonitor::slotDisconnect()
278 {
279     if (m_captureProcess->state() == QProcess::NotRunning) {
280         m_captureTime = KDateTime::currentLocalDateTime();
281         kDebug() << "CURRENT TIME: " << m_captureTime.toString();
282         m_didCapture = false;
283         slotStartCapture(false);
284         m_discAction->setIcon(KIcon("network-disconnect"));
285         m_discAction->setText(i18n("Disconnect"));
286         m_recAction->setEnabled(true);
287         m_stopAction->setEnabled(true);
288         m_playAction->setEnabled(true);
289         m_rewAction->setEnabled(true);
290         m_fwdAction->setEnabled(true);
291     } else {
292         m_captureProcess->write("q", 1);
293         QTimer::singleShot(1000, m_captureProcess, SLOT(kill()));
294         if (m_didCapture) manageCapturedFiles();
295         m_didCapture = false;
296     }
297 }
298
299 void RecMonitor::slotRewind()
300 {
301     m_captureProcess->write("a", 1);
302 }
303
304 void RecMonitor::slotForward()
305 {
306     m_captureProcess->write("z", 1);
307 }
308
309 void RecMonitor::slotStopCapture()
310 {
311     // stop capture
312     switch (device_selector->currentIndex()) {
313     case FIREWIRE:
314         m_captureProcess->write("\e", 2);
315         m_playAction->setIcon(m_playIcon);
316         m_isPlaying = false;
317         break;
318     case VIDEO4LINUX:
319     case SCREENGRAB:
320         m_captureProcess->write("q\n", 3);
321         QTimer::singleShot(1000, m_captureProcess, SLOT(kill()));
322         break;
323     default:
324         break;
325     }
326 }
327
328 void RecMonitor::slotStartCapture(bool play)
329 {
330     if (m_captureProcess->state() != QProcess::NotRunning) {
331         if (device_selector->currentIndex() == FIREWIRE) {
332             if (m_isPlaying) {
333                 m_captureProcess->write("k", 1);
334                 //captureProcess->write("\e", 2);
335                 m_playAction->setIcon(m_playIcon);
336                 m_isPlaying = false;
337             } else {
338                 m_captureProcess->write("p", 1);
339                 m_playAction->setIcon(m_pauseIcon);
340                 m_isPlaying = true;
341             }
342         }
343         return;
344     }
345     m_captureArgs.clear();
346     m_displayArgs.clear();
347     m_isPlaying = false;
348     QString capturename = KdenliveSettings::dvgrabfilename();
349     QStringList dvargs = KdenliveSettings::dvgrabextra().simplified().split(" ", QString::SkipEmptyParts);
350
351     switch (device_selector->currentIndex()) {
352     case FIREWIRE:
353         switch (KdenliveSettings::firewireformat()) {
354         case 0:
355             // RAW DV CAPTURE
356             m_captureArgs << "--format" << "raw";
357             m_displayArgs << "-f" << "dv";
358             break;
359         case 1:
360             // DV type 1
361             m_captureArgs << "--format" << "dv1";
362             m_displayArgs << "-f" << "dv";
363             break;
364         case 2:
365             // DV type 2
366             m_captureArgs << "--format" << "dv2";
367             m_displayArgs << "-f" << "dv";
368             break;
369         case 3:
370             // HDV CAPTURE
371             m_captureArgs << "--format" << "hdv";
372             m_displayArgs << "-f" << KdenliveSettings::video4container();
373             break;
374         }
375         if (KdenliveSettings::firewireautosplit()) m_captureArgs << "--autosplit";
376         if (KdenliveSettings::firewiretimestamp()) m_captureArgs << "--timestamp";
377         if (!dvargs.isEmpty()) {
378             m_captureArgs << dvargs;
379         }
380         m_captureArgs << "-i";
381         if (capturename.isEmpty()) capturename = "capture";
382         m_captureArgs << capturename << "-";
383
384         m_displayArgs << "-x" << QString::number(video_frame->width()) << "-y" << QString::number(video_frame->height()) << "-";
385
386         m_captureProcess->setStandardOutputProcess(m_displayProcess);
387         m_captureProcess->setWorkingDirectory(m_capturePath);
388         kDebug() << "Capture: Running dvgrab " << m_captureArgs.join(" ");
389
390         m_captureProcess->start(KdenliveSettings::dvgrab_path(), m_captureArgs);
391         if (play) m_captureProcess->write(" ", 1);
392         m_discAction->setEnabled(true);
393         break;
394     case VIDEO4LINUX:
395         m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << KdenliveSettings::video4encoding().simplified().split(' ') << "-f" << KdenliveSettings::video4container() << "-";
396         m_displayArgs << "-f" << KdenliveSettings::video4container() << "-x" << QString::number(video_frame->width()) << "-y" << QString::number(video_frame->height()) << "-";
397         m_captureProcess->setStandardOutputProcess(m_displayProcess);
398         kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
399         m_captureProcess->start("ffmpeg", m_captureArgs);
400         break;
401     default:
402         break;
403     }
404
405     if (device_selector->currentIndex() != SCREENGRAB) {
406         kDebug() << "Capture: Running ffplay " << m_displayArgs.join(" ");
407         m_displayProcess->start("ffplay", m_displayArgs);
408         video_frame->setText(i18n("Initialising..."));
409     } else {
410         // do something when starting screen grab
411     }
412 }
413
414 void RecMonitor::slotRecord()
415 {
416     if (m_captureProcess->state() == QProcess::NotRunning && device_selector->currentIndex() == FIREWIRE) {
417         slotStartCapture();
418     }
419     if (m_isCapturing) {
420         switch (device_selector->currentIndex()) {
421         case FIREWIRE:
422             m_captureProcess->write("\e", 2);
423             m_playAction->setIcon(m_playIcon);
424             m_isCapturing = false;
425             m_isPlaying = false;
426             m_recAction->setChecked(false);
427             break;
428         case VIDEO4LINUX:
429             m_captureProcess->terminate();
430             slotStopCapture();
431             //m_isCapturing = false;
432             QTimer::singleShot(1000, this, SLOT(slotStartCapture()));
433             break;
434         case SCREENGRAB:
435             //captureProcess->write("q\n", 3);
436             m_captureProcess->terminate();
437             video_frame->setText(i18n("Encoding captured video..."));
438             // in case ffmpeg doesn't exit with the 'q' command, kill it one second later
439             //QTimer::singleShot(1000, captureProcess, SLOT(kill()));
440             break;
441         }
442         return;
443     } else if (device_selector->currentIndex() == FIREWIRE) {
444         m_isCapturing = true;
445         m_didCapture = true;
446         m_captureProcess->write("c\n", 3);
447 #if KDE_IS_VERSION(4,2,0)
448         m_spaceTimer.start();
449 #endif
450         return;
451     }
452     if (m_captureProcess->state() == QProcess::NotRunning) {
453         m_recAction->setChecked(true);
454         QString extension = "mp4";
455         if (device_selector->currentIndex() == SCREENGRAB) extension = "ogv"; //KdenliveSettings::screengrabextension();
456         else if (device_selector->currentIndex() == VIDEO4LINUX) extension = KdenliveSettings::video4extension();
457         QString path = m_capturePath + "/capture0000." + extension;
458         int i = 1;
459         while (QFile::exists(path)) {
460             QString num = QString::number(i).rightJustified(4, '0', false);
461             path = m_capturePath + "/capture" + num + '.' + extension;
462             i++;
463         }
464         m_captureFile = KUrl(path);
465
466         m_captureArgs.clear();
467         m_displayArgs.clear();
468         QString args;
469         QString capturename = KdenliveSettings::dvgrabfilename();
470         if (capturename.isEmpty()) capturename = "capture";
471
472         switch (device_selector->currentIndex()) {
473         case VIDEO4LINUX:
474             m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << KdenliveSettings::video4encoding().simplified().split(' ') << "-y" << m_captureFile.path() << "-f" << KdenliveSettings::video4container() << "-acodec" << KdenliveSettings::video4acodec() << "-vcodec" << KdenliveSettings::video4vcodec() << "-";
475             m_displayArgs << "-f" << KdenliveSettings::video4container() << "-x" << QString::number(video_frame->width()) << "-y" << QString::number(video_frame->height()) << "-";
476             m_captureProcess->setStandardOutputProcess(m_displayProcess);
477             kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
478             m_captureProcess->start("ffmpeg", m_captureArgs);
479             break;
480         case SCREENGRAB:
481             switch (KdenliveSettings::rmd_capture_type()) {
482             case 0:
483                 // Full screen capture, nothing special to do
484                 break;
485             default:
486                 // Region capture
487                 m_captureArgs << "--width" << QString::number(KdenliveSettings::rmd_width()) << "--height" << QString::number(KdenliveSettings::rmd_height());
488                 if (!KdenliveSettings::rmd_follow_mouse()) {
489                     m_captureArgs << "-x" << QString::number(KdenliveSettings::rmd_offsetx()) << "-y" << QString::number(KdenliveSettings::rmd_offsety());
490                 } else {
491                     m_captureArgs << "--follow-mouse";
492                     if (KdenliveSettings::rmd_hide_frame()) m_captureArgs << "--no-frame";
493                 }
494                 break;
495             }
496             if (KdenliveSettings::rmd_hide_mouse()) m_captureArgs << "--no-cursor";
497             m_isCapturing = true;
498             if (KdenliveSettings::rmd_capture_audio()) {
499                 m_captureArgs << "--freq" << KdenliveSettings::rmd_freq();
500                 m_captureArgs << "--channels" << QString::number(KdenliveSettings::rmd_audio_channels());
501                 if (KdenliveSettings::rmd_use_jack()) {
502                     m_captureArgs << "--use-jack";
503                     QStringList ports = KdenliveSettings::rmd_jackports().split(" ", QString::SkipEmptyParts);
504                     for (int i = 0; i < ports.count(); ++i) {
505                         m_captureArgs << ports.at(i);
506                     }
507                     if (KdenliveSettings::rmd_jack_buffer() > 0.0)
508                         m_captureArgs << "--ring-buffer-size" << QString::number(KdenliveSettings::rmd_jack_buffer());
509                 } else {
510                     if (!KdenliveSettings::rmd_alsadevicename().isEmpty())
511                         m_captureArgs << "--device" << KdenliveSettings::rmd_alsadevicename();
512                     if (KdenliveSettings::rmd_alsa_buffer() > 0)
513                         m_captureArgs << "--buffer-size" << QString::number(KdenliveSettings::rmd_alsa_buffer());
514                 }
515             } else m_captureArgs << "--no-sound";
516
517             if (KdenliveSettings::rmd_fullshots()) m_captureArgs << "--full-shots";
518             m_captureArgs << "--v_bitrate" << QString::number(KdenliveSettings::rmd_bitrate());
519             m_captureArgs << "--v_quality" << QString::number(KdenliveSettings::rmd_quality());
520             m_captureArgs << "--workdir" << KdenliveSettings::currenttmpfolder();
521             m_captureArgs << "--fps" << QString::number(KdenliveSettings::rmd_fps()) << "-o" << m_captureFile.path();
522             m_captureProcess->start(KdenliveSettings::rmd_path(), m_captureArgs);
523             kDebug() << "// RecordMyDesktop params: " << m_captureArgs;
524             break;
525         default:
526             break;
527         }
528
529
530         if (device_selector->currentIndex() != SCREENGRAB) {
531             m_isCapturing = true;
532             kDebug() << "Capture: Running ffplay " << m_displayArgs.join(" ");
533             m_displayProcess->start("ffplay", m_displayArgs);
534             video_frame->setText(i18n("Initialising..."));
535         }
536     } else {
537         // stop capture
538         m_displayProcess->kill();
539         //captureProcess->kill();
540         QTimer::singleShot(1000, this, SLOT(slotRecord()));
541     }
542 }
543
544 /*
545 void RecMonitor::slotStartGrab(const QRect &rect) {
546     rgnGrab->deleteLater();
547     QApplication::restoreOverrideCursor();
548     show();
549     if (rect.isNull()) return;
550     int width = rect.width();
551     int height = rect.height();
552     if (width % 2 != 0) width--;
553     if (height % 2 != 0) height--;
554     QString args = KdenliveSettings::screengrabcapture().replace("%size", QString::number(width) + "x" + QString::number(height)).replace("%offset", "+" + QString::number(rect.x()) + "," + QString::number(rect.y()));
555     if (KdenliveSettings::screengrabenableaudio()) {
556         // also capture audio
557         if (KdenliveSettings::useosscapture()) m_captureArgs << KdenliveSettings::screengrabosscapture().simplified().split(' ');
558         else m_captureArgs << KdenliveSettings::screengrabalsacapture2().simplified().split(' ');
559     }
560     m_captureArgs << args.simplified().split(' ') << KdenliveSettings::screengrabencoding().simplified().split(' ') << m_captureFile.path();
561     m_isCapturing = true;
562     video_frame->setText(i18n("Capturing..."));
563     if (KdenliveSettings::screengrabenableaudio() && !KdenliveSettings::useosscapture()) {
564         QStringList alsaArgs = KdenliveSettings::screengrabalsacapture().simplified().split(' ');
565         alsaProcess->setStandardOutputProcess(captureProcess);
566         kDebug() << "Capture: Running arecord " << alsaArgs.join(" ");
567         alsaProcess->start("arecord", alsaArgs);
568     }
569     kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
570     captureProcess->start("ffmpeg", m_captureArgs);
571 }*/
572
573 void RecMonitor::slotProcessStatus(QProcess::ProcessState status)
574 {
575     if (status == QProcess::NotRunning) {
576         m_displayProcess->kill();
577         if (m_isCapturing && device_selector->currentIndex() != FIREWIRE)
578             if (autoaddbox->isChecked() && QFile::exists(m_captureFile.path())) emit addProjectClip(m_captureFile);
579         if (device_selector->currentIndex() == FIREWIRE) {
580             m_discAction->setIcon(KIcon("network-connect"));
581             m_discAction->setText(i18n("Connect"));
582             m_playAction->setEnabled(false);
583             m_rewAction->setEnabled(false);
584             m_fwdAction->setEnabled(false);
585             m_recAction->setEnabled(false);
586         }
587         m_isPlaying = false;
588         m_playAction->setIcon(m_playIcon);
589         m_recAction->setChecked(false);
590         m_stopAction->setEnabled(false);
591         device_selector->setEnabled(true);
592         if (m_captureProcess && m_captureProcess->exitStatus() == QProcess::CrashExit) {
593             video_frame->setText(i18n("Capture crashed, please check your parameters"));
594         } else {
595             if (device_selector->currentIndex() != SCREENGRAB) {
596                 video_frame->setText(i18n("Not connected"));
597             } else {
598                 if (m_captureProcess->exitCode() != 0) {
599                     video_frame->setText(i18n("Capture crashed, please check your parameters\nRecordMyDesktop exit code: %1", QString::number(m_captureProcess->exitCode())));
600                 } else {
601                     video_frame->setPixmap(mergeSideBySide(KIcon("video-display").pixmap(QSize(50, 50)), i18n("Press record button\nto start screen capture\nFiles will be saved in:\n%1", m_capturePath)));
602                 }
603             }
604         }
605         m_isCapturing = false;
606
607 #if KDE_IS_VERSION(4,2,0)
608         m_spaceTimer.stop();
609         // update free space info
610         slotUpdateFreeSpace();
611 #endif
612
613     } else {
614         if (device_selector->currentIndex() != SCREENGRAB) m_stopAction->setEnabled(true);
615         device_selector->setEnabled(false);
616     }
617 }
618
619 void RecMonitor::manageCapturedFiles()
620 {
621     QString extension;
622     switch (KdenliveSettings::firewireformat()) {
623     case 0:
624         extension = ".dv";
625         break;
626     case 1:
627     case 2:
628         extension = ".avi";
629         break;
630     case 3:
631         extension = ".m2t";
632         break;
633     }
634     QDir dir(m_capturePath);
635     QStringList filters;
636     QString capturename = KdenliveSettings::dvgrabfilename();
637     if (capturename.isEmpty()) capturename = "capture";
638     filters << capturename + "*" + extension;
639     const QStringList result = dir.entryList(filters, QDir::Files, QDir::Time);
640     KUrl::List capturedFiles;
641     foreach(const QString & name, result) {
642         KUrl url = KUrl(dir.filePath(name));
643         if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, this)) {
644             KFileItem file(KFileItem::Unknown, KFileItem::Unknown, url, true);
645             if (file.time(KFileItem::ModificationTime) > m_captureTime) capturedFiles.append(url);
646         }
647     }
648     kDebug() << "Found : " << capturedFiles.count() << " new capture files";
649     kDebug() << capturedFiles;
650
651     if (capturedFiles.count() > 0) {
652         ManageCapturesDialog *d = new ManageCapturesDialog(capturedFiles, this);
653         if (d->exec() == QDialog::Accepted) {
654             capturedFiles = d->importFiles();
655             foreach(const KUrl & url, capturedFiles) {
656                 emit addProjectClip(url);
657             }
658         }
659         delete d;
660     }
661 }
662
663 // virtual
664 void RecMonitor::mousePressEvent(QMouseEvent * /*event*/)
665 {
666 #if KDE_IS_VERSION(4,2,0)
667     if (m_freeSpace->underMouse()) slotUpdateFreeSpace();
668 #endif
669 }
670
671 void RecMonitor::slotUpdateFreeSpace()
672 {
673 #if KDE_IS_VERSION(4,2,0)
674     KDiskFreeSpaceInfo info = KDiskFreeSpaceInfo::freeSpaceInfo(m_capturePath);
675     if (info.isValid()) {
676         m_freeSpace->setValue(100 * info.used() / info.size());
677         m_freeSpace->setText(i18n("Free space: %1", KIO::convertSize(info.available())));
678         m_freeSpace->update();
679     }
680 #endif
681 }
682
683 void RecMonitor::activateRecMonitor()
684 {
685     //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
686 }
687
688 void RecMonitor::stop()
689 {
690     m_isActive = false;
691
692 }
693
694 void RecMonitor::start()
695 {
696     m_isActive = true;
697
698 }
699
700 void RecMonitor::refreshRecMonitor(bool visible)
701 {
702     if (visible) {
703         //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
704
705     }
706 }
707
708 void RecMonitor::slotPlay()
709 {
710
711     //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
712
713 }
714
715 void RecMonitor::slotReadDvgrabInfo()
716 {
717     QString data = m_captureProcess->readAllStandardError().simplified();
718     data = data.section('"', 2, 2).simplified();
719     m_dvinfo.setText(data.left(11));
720     m_dvinfo.updateGeometry();
721 }
722
723 #include "recmonitor.moc"
724