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