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