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