]> git.sesse.net Git - kdenlive/blob - src/recmonitor.cpp
small cleanup
[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
28 #include <KDebug>
29 #include <KLocale>
30 #include <KStandardDirs>
31 #include <KComboBox>
32
33 #include "gentime.h"
34 #include "kdenlivesettings.h"
35 #include "recmonitor.h"
36
37 RecMonitor::RecMonitor(QString name, QWidget *parent)
38         : QWidget(parent), m_name(name), m_isActive(false), m_isCapturing(false), m_isPlaying(false) {
39     ui.setupUi(this);
40
41     ui.video_frame->setAttribute(Qt::WA_PaintOnScreen);
42     ui.device_selector->setCurrentIndex(KdenliveSettings::defaultcapture());
43     connect(ui.device_selector, SIGNAL(currentIndexChanged(int)), this, SLOT(slotVideoDeviceChanged(int)));
44
45
46
47     QToolBar *toolbar = new QToolBar(name, this);
48     QHBoxLayout *layout = new QHBoxLayout;
49
50     m_playIcon = KIcon("media-playback-start");
51     m_pauseIcon = KIcon("media-playback-pause");
52
53     m_discAction = toolbar->addAction(KIcon("network-connect"), i18n("Connect"));
54     connect(m_discAction, SIGNAL(triggered()), this, SLOT(slotDisconnect()));
55
56     m_rewAction = toolbar->addAction(KIcon("media-seek-backward"), i18n("Rewind"));
57     connect(m_rewAction, SIGNAL(triggered()), this, SLOT(slotRewind()));
58
59     m_playAction = toolbar->addAction(m_playIcon, i18n("Play"));
60     connect(m_playAction, SIGNAL(triggered()), this, SLOT(slotStartCapture()));
61
62     m_stopAction = toolbar->addAction(KIcon("media-playback-stop"), i18n("Stop"));
63     connect(m_stopAction, SIGNAL(triggered()), this, SLOT(slotStopCapture()));
64     m_stopAction->setEnabled(false);
65     m_fwdAction = toolbar->addAction(KIcon("media-seek-forward"), i18n("Forward"));
66     connect(m_fwdAction, SIGNAL(triggered()), this, SLOT(slotForward()));
67
68     m_recAction = toolbar->addAction(KIcon("media-record"), i18n("Record"));
69     connect(m_recAction, SIGNAL(triggered()), this, SLOT(slotRecord()));
70     m_recAction->setCheckable(true);
71
72     layout->addWidget(toolbar);
73     ui.control_frame_firewire->setLayout(layout);
74
75     slotVideoDeviceChanged(ui.device_selector->currentIndex());
76     displayProcess = new QProcess;
77     captureProcess = new QProcess;
78
79     connect(captureProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(slotProcessStatus(QProcess::ProcessState)));
80
81     QStringList env = QProcess::systemEnvironment();
82     env << "SDL_WINDOWID=" + QString::number(ui.video_frame->winId());
83     displayProcess->setEnvironment(env);
84
85     if (KdenliveSettings::video4capture().isEmpty()) {
86         QString captureCommand;
87         if (!KdenliveSettings::video4adevice().isEmpty()) captureCommand = "-f " + KdenliveSettings::video4aformat() + " -i " + KdenliveSettings::video4adevice();
88
89         captureCommand +=  " -f " + KdenliveSettings::video4vformat() + " -s " + KdenliveSettings::video4size() + " -r " + QString::number(KdenliveSettings::video4rate()) + " -i " + KdenliveSettings::video4vdevice() + " -f " + KdenliveSettings::video4vencoding();
90         KdenliveSettings::setVideo4capture(captureCommand);
91     }
92
93     if (KdenliveSettings::video4playback().isEmpty()) {
94         QString playbackCommand;
95         playbackCommand =  "-f " + KdenliveSettings::video4vencoding();
96         KdenliveSettings::setVideo4playback(playbackCommand);
97     }
98     kDebug() << "/////// BUILDING MONITOR, ID: " << ui.video_frame->winId();
99 }
100
101 QString RecMonitor::name() const {
102     return m_name;
103 }
104
105 void RecMonitor::slotVideoDeviceChanged(int ix) {
106
107     if (ix == 1) {
108         m_discAction->setEnabled(false);
109         m_rewAction->setEnabled(false);
110         m_fwdAction->setEnabled(false);
111         m_recAction->setEnabled(true);
112         m_stopAction->setEnabled(false);
113         m_playAction->setEnabled(true);
114     } else {
115         m_discAction->setEnabled(true);
116         m_recAction->setEnabled(false);
117         m_stopAction->setEnabled(false);
118         m_playAction->setEnabled(false);
119         m_rewAction->setEnabled(false);
120         m_fwdAction->setEnabled(false);
121     }
122 }
123
124 void RecMonitor::slotDisconnect() {
125     if (captureProcess->state() == QProcess::NotRunning) {
126         slotStartCapture(false);
127         m_discAction->setIcon(KIcon("network-disconnect"));
128         m_discAction->setText(i18n("Disonnect"));
129         m_recAction->setEnabled(true);
130         m_stopAction->setEnabled(true);
131         m_playAction->setEnabled(true);
132         m_rewAction->setEnabled(true);
133         m_fwdAction->setEnabled(true);
134     } else {
135         captureProcess->write("q", 1);
136         m_discAction->setIcon(KIcon("network-connect"));
137         m_discAction->setText(i18n("Connect"));
138         m_recAction->setEnabled(false);
139         m_stopAction->setEnabled(false);
140         m_playAction->setEnabled(false);
141         m_rewAction->setEnabled(false);
142         m_fwdAction->setEnabled(false);
143     }
144 }
145
146 void RecMonitor::slotRewind() {
147     captureProcess->write("a", 1);
148 }
149
150 void RecMonitor::slotForward() {
151     captureProcess->write("z", 1);
152 }
153
154 void RecMonitor::slotStopCapture() {
155     // stop capture
156     if (ui.device_selector->currentIndex() == 0) {
157         captureProcess->write("\e", 2);
158         m_playAction->setIcon(m_playIcon);
159         m_isPlaying = false;
160     } else {
161         if (m_isCapturing && ui.autoaddbox->isChecked()) emit addProjectClip(m_captureFile);
162         captureProcess->kill();
163         displayProcess->kill();
164     }
165 }
166
167 void RecMonitor::slotStartCapture(bool play) {
168
169     /*
170     *captureProcess<<"dvgrab";
171
172     bool isHdv = false;
173
174     switch (m_recPanel->capture_format->currentItem()){
175         case 0:
176       *captureProcess<<"--format"<<"dv1";
177      break;
178         case 1:
179       *captureProcess<<"--format"<<"dv2";
180      break;
181         case 3:
182       *captureProcess<<"--format"<<"hdv";
183      isHdv = true;
184      break;
185         default:
186             *captureProcess<<"--format"<<"raw";
187      break;
188     }
189
190     if (KdenliveSettings::autosplit()) *captureProcess<<"--autosplit";
191     if (KdenliveSettings::timestamp()) *captureProcess<<"--timestamp";
192     *captureProcess<<"-i"<<"capture"<<"-";*/
193
194     /*
195         QStringList captureArgs;
196         captureArgs<<"--format"<<"hdv"<<"-i"<<"capture"<<"-";
197         QStringList displayArgs;
198
199         displayArgs<<"-f"<<"mpegts"<<"-x"<<QString::number(ui.video_frame->width())<<"-y"<<QString::number(ui.video_frame->height())<<"-";
200
201         captureProcess->setStandardOutputProcess(displayProcess);
202         ui.video_frame->setScaledContents(false);
203         captureProcess->start("dvgrab",captureArgs);
204         displayProcess->start("ffplay",  displayArgs);*/
205
206
207     if (captureProcess->state() != QProcess::NotRunning) {
208         if (ui.device_selector->currentIndex() == 0) {
209             if (m_isPlaying) {
210                 captureProcess->write("k", 1);
211                 //captureProcess->write("\e", 2);
212                 m_playAction->setIcon(m_playIcon);
213                 m_isPlaying = false;
214             } else {
215                 captureProcess->write("p", 1);
216                 m_playAction->setIcon(m_pauseIcon);
217                 m_isPlaying = true;
218             }
219         }
220         return;
221     }
222     m_captureArgs.clear();
223     m_displayArgs.clear();
224     m_isPlaying = false;
225
226     if (ui.device_selector->currentIndex() == 0) {
227         m_captureArgs << "--format" << "hdv" << "-i" << "capture" << "-";
228         m_displayArgs << "-f" << "mpegts" << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
229     } else {
230         m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << "-";
231         m_displayArgs << KdenliveSettings::video4playback().simplified().split(' ') << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
232     }
233
234     captureProcess->setStandardOutputProcess(displayProcess);
235     if (ui.device_selector->currentIndex() == 0) {
236         captureProcess->setWorkingDirectory(KdenliveSettings::capturefolder());
237         captureProcess->start("dvgrab", m_captureArgs);
238         if (play) captureProcess->write(" ", 1);
239         m_discAction->setEnabled(true);
240     } else captureProcess->start("ffmpeg", m_captureArgs);
241     displayProcess->start("ffplay", m_displayArgs);
242     ui.video_frame->setText(i18n("Initialising..."));
243
244 }
245
246 void RecMonitor::slotRecord() {
247     if (captureProcess->state() == QProcess::NotRunning && ui.device_selector->currentIndex() == 0) {
248         slotStartCapture();
249     }
250     if (m_isCapturing) {
251         if (ui.device_selector->currentIndex() == 0) {
252             captureProcess->write("\e", 2);
253             m_playAction->setIcon(m_playIcon);
254             m_isCapturing = false;
255             m_isPlaying = false;
256             m_recAction->setChecked(false);
257         } else {
258             slotStopCapture();
259             QTimer::singleShot(1000, this, SLOT(slotStartCapture()));
260         }
261         return;
262     } else if (ui.device_selector->currentIndex() == 0) {
263         m_isCapturing = true;
264         captureProcess->write("c\n", 3);
265         return;
266     }
267     if (captureProcess->state() == QProcess::NotRunning) {
268         m_recAction->setChecked(true);
269
270         QString path = KdenliveSettings::capturefolder() + "/capture0000.mpg";
271         int i = 1;
272         while (QFile::exists(path)) {
273             QString num = QString::number(i).rightJustified(4, '0', false);
274             path = KdenliveSettings::capturefolder() + "/capture" + num + ".mpg";
275             i++;
276         }
277
278         m_captureFile = KUrl(path);
279
280         m_isCapturing = true;
281         m_captureArgs.clear();
282         m_displayArgs.clear();
283
284         if (ui.device_selector->currentIndex() == 0) {
285             m_captureArgs << "--format" << "hdv" << "-i" << "capture" << "-";
286             m_displayArgs << "-f" << "mpegts" << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
287         } else {
288             m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << "-y" << m_captureFile.path() << "-f" << KdenliveSettings::video4vencoding() << "-";
289             m_displayArgs << KdenliveSettings::video4playback().simplified().split(' ') << "-x" << QString::number(ui.video_frame->width()) << "-y" << QString::number(ui.video_frame->height()) << "-";
290         }
291
292         captureProcess->setStandardOutputProcess(displayProcess);
293         //ui.video_frame->setScaledContents(false);
294         if (ui.device_selector->currentIndex() == 0) captureProcess->start("dvgrab", m_captureArgs);
295         else captureProcess->start("ffmpeg", m_captureArgs);
296         displayProcess->start("ffplay", m_displayArgs);
297         ui.video_frame->setText(i18n("Initialising..."));
298     } else {
299         // stop capture
300         displayProcess->kill();
301         captureProcess->kill();
302         QTimer::singleShot(1000, this, SLOT(slotRecord()));
303     }
304 }
305
306 void RecMonitor::slotProcessStatus(QProcess::ProcessState status) {
307     if (status == QProcess::NotRunning) {
308         m_isCapturing = false;
309         m_isPlaying = false;
310         m_playAction->setIcon(m_playIcon);
311         m_recAction->setChecked(false);
312         m_stopAction->setEnabled(false);
313         ui.device_selector->setEnabled(true);
314         ui.video_frame->setText(i18n("Not connected"));
315     } else {
316         m_stopAction->setEnabled(true);
317         ui.device_selector->setEnabled(false);
318     }
319 }
320
321 // virtual
322 void RecMonitor::mousePressEvent(QMouseEvent * event) {
323     slotPlay();
324 }
325
326 void RecMonitor::activateRecMonitor() {
327     //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
328 }
329
330 void RecMonitor::stop() {
331     m_isActive = false;
332
333 }
334
335 void RecMonitor::start() {
336     m_isActive = true;
337
338 }
339
340 void RecMonitor::refreshRecMonitor(bool visible) {
341     if (visible) {
342         //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
343
344     }
345 }
346
347 void RecMonitor::slotPlay() {
348
349     //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
350
351 }
352
353
354 #include "recmonitor.moc"