]> git.sesse.net Git - kdenlive/blob - src/recmonitor.cpp
add config button to rec monitor
[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 <QMouseEvent>
22 #include <QStylePainter>
23 #include <QMenu>
24 #include <QToolButton>
25 #include <QFile>
26 #include <QDir>
27 #include <QDesktopWidget>
28
29 #include <KDebug>
30 #include <KLocale>
31 #include <KStandardDirs>
32 #include <KComboBox>
33 #include <KIO/NetAccess>
34
35 #include "gentime.h"
36 #include "kdenlivesettings.h"
37 #include "recmonitor.h"
38
39 RecMonitor::RecMonitor(QString name, QWidget *parent)
40         : QWidget(parent), m_name(name), m_isActive(false), m_isCapturing(false), m_isPlaying(false) {
41     ui.setupUi(this);
42
43     ui.video_frame->setAttribute(Qt::WA_PaintOnScreen);
44     ui.device_selector->setCurrentIndex(KdenliveSettings::defaultcapture());
45     connect(ui.device_selector, SIGNAL(currentIndexChanged(int)), this, SLOT(slotVideoDeviceChanged(int)));
46
47
48
49     QToolBar *toolbar = new QToolBar(name, this);
50     QHBoxLayout *layout = new QHBoxLayout;
51     layout->setContentsMargins(0, 0, 0, 0);
52     m_playIcon = KIcon("media-playback-start");
53     m_pauseIcon = KIcon("media-playback-pause");
54
55     m_discAction = toolbar->addAction(KIcon("network-connect"), i18n("Connect"));
56     connect(m_discAction, SIGNAL(triggered()), this, SLOT(slotDisconnect()));
57
58     m_rewAction = toolbar->addAction(KIcon("media-seek-backward"), i18n("Rewind"));
59     connect(m_rewAction, SIGNAL(triggered()), this, SLOT(slotRewind()));
60
61     m_playAction = toolbar->addAction(m_playIcon, i18n("Play"));
62     connect(m_playAction, SIGNAL(triggered()), this, SLOT(slotStartCapture()));
63
64     m_stopAction = toolbar->addAction(KIcon("media-playback-stop"), i18n("Stop"));
65     connect(m_stopAction, SIGNAL(triggered()), this, SLOT(slotStopCapture()));
66     m_stopAction->setEnabled(false);
67     m_fwdAction = toolbar->addAction(KIcon("media-seek-forward"), i18n("Forward"));
68     connect(m_fwdAction, SIGNAL(triggered()), this, SLOT(slotForward()));
69
70     m_recAction = toolbar->addAction(KIcon("media-record"), i18n("Record"));
71     connect(m_recAction, SIGNAL(triggered()), this, SLOT(slotRecord()));
72     m_recAction->setCheckable(true);
73
74     toolbar->addSeparator();
75
76     QAction *configAction = toolbar->addAction(KIcon("configure"), i18n("Configure"));
77     connect(configAction, SIGNAL(triggered()), this, SLOT(slotConfigure()));
78     configAction->setCheckable(false);
79
80     layout->addWidget(toolbar);
81     ui.control_frame_firewire->setLayout(layout);
82
83     slotVideoDeviceChanged(ui.device_selector->currentIndex());
84     displayProcess = new QProcess;
85     captureProcess = new QProcess;
86
87     connect(captureProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(slotProcessStatus(QProcess::ProcessState)));
88
89     QStringList env = QProcess::systemEnvironment();
90     env << "SDL_WINDOWID=" + QString::number(ui.video_frame->winId());
91     displayProcess->setEnvironment(env);
92
93     if (KdenliveSettings::video4capture().isEmpty()) {
94         QString captureCommand;
95         if (!KdenliveSettings::video4adevice().isEmpty()) captureCommand = "-f " + KdenliveSettings::video4aformat() + " -i " + KdenliveSettings::video4adevice();
96
97         captureCommand +=  " -f " + KdenliveSettings::video4vformat() + " -s " + KdenliveSettings::video4size() + " -r " + QString::number(KdenliveSettings::video4rate()) + " -i " + KdenliveSettings::video4vdevice() + " -f " + KdenliveSettings::video4vencoding();
98         KdenliveSettings::setVideo4capture(captureCommand);
99     }
100
101     if (KdenliveSettings::video4playback().isEmpty()) {
102         QString playbackCommand;
103         playbackCommand =  "-f " + KdenliveSettings::video4vencoding();
104         KdenliveSettings::setVideo4playback(playbackCommand);
105     }
106     kDebug() << "/////// BUILDING MONITOR, ID: " << ui.video_frame->winId();
107 }
108
109 QString RecMonitor::name() const {
110     return m_name;
111 }
112
113 void RecMonitor::slotConfigure() {
114     emit showConfigDialog(4, ui.device_selector->currentIndex());
115 }
116
117 void RecMonitor::slotVideoDeviceChanged(int ix) {
118     switch (ix) {
119     case SCREENGRAB:
120         m_discAction->setEnabled(false);
121         m_rewAction->setEnabled(false);
122         m_fwdAction->setEnabled(false);
123         m_recAction->setEnabled(true);
124         m_stopAction->setEnabled(false);
125         m_playAction->setEnabled(false);
126         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())));
127         //ui.video_frame->setText(i18n("Press record button\nto start screen capture"));
128         break;
129     case VIDEO4LINUX:
130         m_discAction->setEnabled(false);
131         m_rewAction->setEnabled(false);
132         m_fwdAction->setEnabled(false);
133         m_recAction->setEnabled(true);
134         m_stopAction->setEnabled(false);
135         m_playAction->setEnabled(true);
136         checkDeviceAvailability();
137         break;
138     default: // FIREWIRE
139         m_discAction->setEnabled(true);
140         m_recAction->setEnabled(false);
141         m_stopAction->setEnabled(false);
142         m_playAction->setEnabled(false);
143         m_rewAction->setEnabled(false);
144         m_fwdAction->setEnabled(false);
145         //ui.video_frame->setText(i18n("Plug your camcorder and\npress connect button\nto initialize connection"));
146         ui.video_frame->setPixmap(mergeSideBySide(KIcon("network-connect").pixmap(QSize(50, 50)), i18n("Plug your camcorder and\npress connect button\nto initialize connection\nFiles will be saved in:\n%1", KdenliveSettings::capturefolder())));
147         break;
148     }
149 }
150
151 QPixmap RecMonitor::mergeSideBySide(const QPixmap& pix, const QString txt) {
152     QPainter p;
153     QRect r = p.fontMetrics().boundingRect(QRect(0, 0, ui.video_frame->width(), ui.video_frame->height()), Qt::AlignLeft, txt);
154     int strWidth = r.width();
155     int strHeight = r.height();
156     int pixWidth = pix.width();
157     int pixHeight = pix.height();
158     QPixmap res(strWidth + 8 + pixWidth, qMax(strHeight, pixHeight));
159     res.fill(Qt::transparent);
160     p.begin(&res);
161     p.drawPixmap(0, 0, pix);
162     p.drawText(QRect(pixWidth + 8, 0, strWidth, strHeight), 0, txt);
163     p.end();
164     return res;
165 }
166
167
168 void RecMonitor::checkDeviceAvailability() {
169     if (!KIO::NetAccess::exists(KUrl(KdenliveSettings::video4vdevice()), KIO::NetAccess::SourceSide , this)) {
170         m_playAction->setEnabled(false);
171         m_recAction->setEnabled(false);
172         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())));
173         //ui.video_frame->setText(i18n("Cannot read from device %1\nPlease check drivers and access rights.", KdenliveSettings::video4vdevice()));
174     } else //ui.video_frame->setText(i18n("Press play or record button\nto start video capture"));
175         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())));
176 }
177
178 void RecMonitor::slotDisconnect() {
179     if (captureProcess->state() == QProcess::NotRunning) {
180         slotStartCapture(false);
181         m_discAction->setIcon(KIcon("network-disconnect"));
182         m_discAction->setText(i18n("Disonnect"));
183         m_recAction->setEnabled(true);
184         m_stopAction->setEnabled(true);
185         m_playAction->setEnabled(true);
186         m_rewAction->setEnabled(true);
187         m_fwdAction->setEnabled(true);
188     } else {
189         captureProcess->write("q", 1);
190     }
191 }
192
193 void RecMonitor::slotRewind() {
194     captureProcess->write("a", 1);
195 }
196
197 void RecMonitor::slotForward() {
198     captureProcess->write("z", 1);
199 }
200
201 void RecMonitor::slotStopCapture() {
202     // stop capture
203     switch (ui.device_selector->currentIndex()) {
204     case FIREWIRE:
205         captureProcess->write("\e", 2);
206         m_playAction->setIcon(m_playIcon);
207         m_isPlaying = false;
208         break;
209     case VIDEO4LINUX:
210         captureProcess->write("q\n", 3);
211         QTimer::singleShot(1000, captureProcess, SLOT(kill()));
212
213         break;
214     case SCREENGRAB:
215         captureProcess->write("q\n", 3);
216         QTimer::singleShot(1000, captureProcess, SLOT(kill()));
217         break;
218     default:
219         break;
220     }
221 }
222
223 void RecMonitor::slotStartCapture(bool play) {
224
225     /*
226     *captureProcess<<"dvgrab";
227
228     bool isHdv = false;
229
230     switch (m_recPanel->capture_format->currentItem()){
231         case 0:
232       *captureProcess<<"--format"<<"dv1";
233      break;
234         case 1:
235       *captureProcess<<"--format"<<"dv2";
236      break;
237         case 3:
238       *captureProcess<<"--format"<<"hdv";
239      isHdv = true;
240      break;
241         default:
242             *captureProcess<<"--format"<<"raw";
243      break;
244     }
245
246     if (KdenliveSettings::autosplit()) *captureProcess<<"--autosplit";
247     if (KdenliveSettings::timestamp()) *captureProcess<<"--timestamp";
248     *captureProcess<<"-i"<<"capture"<<"-";*/
249
250     /*
251         QStringList captureArgs;
252         captureArgs<<"--format"<<"hdv"<<"-i"<<"capture"<<"-";
253         QStringList displayArgs;
254
255         displayArgs<<"-f"<<"mpegts"<<"-x"<<QString::number(ui.video_frame->width())<<"-y"<<QString::number(ui.video_frame->height())<<"-";
256
257         captureProcess->setStandardOutputProcess(displayProcess);
258         ui.video_frame->setScaledContents(false);
259         captureProcess->start("dvgrab",captureArgs);
260         displayProcess->start("ffplay",  displayArgs);*/
261
262
263     if (captureProcess->state() != QProcess::NotRunning) {
264         if (ui.device_selector->currentIndex() == FIREWIRE) {
265             if (m_isPlaying) {
266                 captureProcess->write("k", 1);
267                 //captureProcess->write("\e", 2);
268                 m_playAction->setIcon(m_playIcon);
269                 m_isPlaying = false;
270             } else {
271                 captureProcess->write("p", 1);
272                 m_playAction->setIcon(m_pauseIcon);
273                 m_isPlaying = true;
274             }
275         }
276         return;
277     }
278     m_captureArgs.clear();
279     m_displayArgs.clear();
280     m_isPlaying = false;
281
282     switch (ui.device_selector->currentIndex()) {
283     case FIREWIRE:
284         m_captureArgs << "--format" << "hdv" << "-i" << "capture" << "-";
285         m_displayArgs << "-f" << "mpegts" << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
286         captureProcess->setStandardOutputProcess(displayProcess);
287         captureProcess->setWorkingDirectory(KdenliveSettings::capturefolder());
288         captureProcess->start("dvgrab", m_captureArgs);
289         if (play) captureProcess->write(" ", 1);
290         m_discAction->setEnabled(true);
291         break;
292     case VIDEO4LINUX:
293         m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << "-";
294         m_displayArgs << KdenliveSettings::video4playback().simplified().split(' ') << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
295         captureProcess->setStandardOutputProcess(displayProcess);
296         captureProcess->start("ffmpeg", m_captureArgs);
297         break;
298     default:
299         break;
300     }
301
302     if (ui.device_selector->currentIndex() != SCREENGRAB) {
303         displayProcess->start("ffplay", m_displayArgs);
304         ui.video_frame->setText(i18n("Initialising..."));
305     } else {
306         // do something when starting screen grab
307     }
308 }
309
310 void RecMonitor::slotRecord() {
311     if (captureProcess->state() == QProcess::NotRunning && ui.device_selector->currentIndex() == FIREWIRE) {
312         slotStartCapture();
313     }
314     if (m_isCapturing) {
315         switch (ui.device_selector->currentIndex()) {
316         case FIREWIRE:
317             captureProcess->write("\e", 2);
318             m_playAction->setIcon(m_playIcon);
319             m_isCapturing = false;
320             m_isPlaying = false;
321             m_recAction->setChecked(false);
322             break;
323         case VIDEO4LINUX:
324             slotStopCapture();
325             //m_isCapturing = false;
326             QTimer::singleShot(1000, this, SLOT(slotStartCapture()));
327             break;
328         case SCREENGRAB:
329             captureProcess->write("q\n", 3);
330             // in case ffmpeg doesn't exit with the 'q' command, kill it one second later
331             QTimer::singleShot(1000, captureProcess, SLOT(kill()));
332             break;
333         }
334         return;
335     } else if (ui.device_selector->currentIndex() == FIREWIRE) {
336         m_isCapturing = true;
337         captureProcess->write("c\n", 3);
338         return;
339     }
340     if (captureProcess->state() == QProcess::NotRunning) {
341         m_recAction->setChecked(true);
342         QString extension = "mpg";
343         if (ui.device_selector->currentIndex() == SCREENGRAB) extension = KdenliveSettings::screengrabextension();
344         QString path = KdenliveSettings::capturefolder() + "/capture0000." + extension;
345         int i = 1;
346         while (QFile::exists(path)) {
347             QString num = QString::number(i).rightJustified(4, '0', false);
348             path = KdenliveSettings::capturefolder() + "/capture" + num + "." + extension;
349             i++;
350         }
351
352         m_captureFile = KUrl(path);
353
354         m_captureArgs.clear();
355         m_displayArgs.clear();
356         QString args;
357
358         switch (ui.device_selector->currentIndex()) {
359         case FIREWIRE:
360             m_captureArgs << "--format" << "hdv" << "-i" << "capture" << "-";
361             m_displayArgs << "-f" << "mpegts" << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
362             captureProcess->setStandardOutputProcess(displayProcess);
363             captureProcess->start("dvgrab", m_captureArgs);
364             break;
365         case VIDEO4LINUX:
366             m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << "-y" << m_captureFile.path() << "-f" << KdenliveSettings::video4vencoding() << "-";
367             m_displayArgs << KdenliveSettings::video4playback().simplified().split(' ') << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
368             captureProcess->setStandardOutputProcess(displayProcess);
369             captureProcess->start("ffmpeg", m_captureArgs);
370             break;
371         case SCREENGRAB:
372             if (KdenliveSettings::fullscreengrab()) {
373                 const QRect rect = QApplication::desktop()->screenGeometry();
374                 args = KdenliveSettings::screengrabcapture().replace("%size", QString::number(rect.width()) + "x" + QString::number(rect.height())).replace("%offset", QString());
375                 kDebug() << "// capture params: " << args;
376                 if (KdenliveSettings::screengrabenableaudio()) {
377                     // also capture audio
378                     m_captureArgs << KdenliveSettings::screengrabaudiocapture().simplified().split(' ');
379                 }
380                 m_captureArgs << args.simplified().split(' ') << KdenliveSettings::screengrabencoding().simplified().split(' ') << m_captureFile.path();
381                 ui.video_frame->setText(i18n("Capturing..."));
382                 m_isCapturing = true;
383                 captureProcess->start("ffmpeg", m_captureArgs);
384             } else {
385                 ui.video_frame->setText(i18n("Select region..."));
386                 rgnGrab = new RegionGrabber();
387                 connect(rgnGrab, SIGNAL(regionGrabbed(const QRect&)), SLOT(slotStartGrab(const QRect &)));
388             }
389             break;
390         default:
391             break;
392         }
393
394         //ui.video_frame->setScaledContents(false);
395         if (ui.device_selector->currentIndex() != SCREENGRAB) {
396             m_isCapturing = true;
397             displayProcess->start("ffplay", m_displayArgs);
398             ui.video_frame->setText(i18n("Initialising..."));
399         }
400     } else {
401         // stop capture
402         displayProcess->kill();
403         captureProcess->kill();
404         QTimer::singleShot(1000, this, SLOT(slotRecord()));
405     }
406 }
407
408 void RecMonitor::slotStartGrab(const QRect &rect) {
409     rgnGrab->deleteLater();
410     QApplication::restoreOverrideCursor();
411     show();
412     if (rect.isNull()) return;
413     int width = rect.width();
414     int height = rect.height();
415     if (width % 2 != 0) width--;
416     if (height % 2 != 0) height--;
417     QString args = KdenliveSettings::screengrabcapture().replace("%size", QString::number(width) + "x" + QString::number(height)).replace("%offset", "+" + QString::number(rect.x()) + "," + QString::number(rect.y()));
418     kDebug() << "// capture params: " << args;
419     if (KdenliveSettings::screengrabenableaudio()) {
420         // also capture audio
421         m_captureArgs << KdenliveSettings::screengrabaudiocapture().simplified().split(' ');
422     }
423     m_captureArgs << args.simplified().split(' ') << KdenliveSettings::screengrabencoding().simplified().split(' ') << m_captureFile.path();
424     m_isCapturing = true;
425     ui.video_frame->setText(i18n("Capturing..."));
426     captureProcess->start("ffmpeg", m_captureArgs);
427 }
428
429 void RecMonitor::slotProcessStatus(QProcess::ProcessState status) {
430     if (status == QProcess::NotRunning) {
431         displayProcess->kill();
432         if (m_isCapturing && ui.device_selector->currentIndex() != FIREWIRE)
433             if (ui.autoaddbox->isChecked() && QFile::exists(m_captureFile.path())) emit addProjectClip(m_captureFile);
434         if (ui.device_selector->currentIndex() == FIREWIRE) {
435             m_discAction->setIcon(KIcon("network-connect"));
436             m_discAction->setText(i18n("Connect"));
437             m_playAction->setEnabled(false);
438             m_rewAction->setEnabled(false);
439             m_fwdAction->setEnabled(false);
440             m_recAction->setEnabled(false);
441         }
442         m_isPlaying = false;
443         m_playAction->setIcon(m_playIcon);
444         m_recAction->setChecked(false);
445         m_stopAction->setEnabled(false);
446         ui.device_selector->setEnabled(true);
447         if (captureProcess && captureProcess->exitStatus() == QProcess::CrashExit) {
448             ui.video_frame->setText(i18n("Capture crashed, please check your parameters"));
449         } else {
450             ui.video_frame->setText(i18n("Not connected"));
451             if (m_isCapturing && ui.device_selector->currentIndex() == FIREWIRE) {
452                 //TODO: show dialog asking user confirmation for captured files
453             }
454         }
455         m_isCapturing = false;
456     } else {
457         if (ui.device_selector->currentIndex() != SCREENGRAB) m_stopAction->setEnabled(true);
458         ui.device_selector->setEnabled(false);
459     }
460 }
461
462 // virtual
463 void RecMonitor::mousePressEvent(QMouseEvent * event) {
464     slotPlay();
465 }
466
467 void RecMonitor::activateRecMonitor() {
468     //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
469 }
470
471 void RecMonitor::stop() {
472     m_isActive = false;
473
474 }
475
476 void RecMonitor::start() {
477     m_isActive = true;
478
479 }
480
481 void RecMonitor::refreshRecMonitor(bool visible) {
482     if (visible) {
483         //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
484
485     }
486 }
487
488 void RecMonitor::slotPlay() {
489
490     //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
491
492 }
493
494
495 #include "recmonitor.moc"