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