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