1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
21 #include <QMouseEvent>
22 #include <QStylePainter>
24 #include <QToolButton>
30 #include <KStandardDirs>
34 #include "kdenlivesettings.h"
35 #include "recmonitor.h"
37 RecMonitor::RecMonitor(QString name, QWidget *parent)
38 : QWidget(parent), m_name(name), m_isActive(false), m_isCapturing(false), m_isPlaying(false) {
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)));
47 QToolBar *toolbar = new QToolBar(name, this);
48 QHBoxLayout *layout = new QHBoxLayout;
50 m_playIcon = KIcon("media-playback-start");
51 m_pauseIcon = KIcon("media-playback-pause");
53 m_discAction = toolbar->addAction(KIcon("network-connect"), i18n("Connect"));
54 connect(m_discAction, SIGNAL(triggered()), this, SLOT(slotDisconnect()));
56 m_rewAction = toolbar->addAction(KIcon("media-seek-backward"), i18n("Rewind"));
57 connect(m_rewAction, SIGNAL(triggered()), this, SLOT(slotRewind()));
59 m_playAction = toolbar->addAction(m_playIcon, i18n("Play"));
60 connect(m_playAction, SIGNAL(triggered()), this, SLOT(slotStartCapture()));
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()));
68 m_recAction = toolbar->addAction(KIcon("media-record"), i18n("Record"));
69 connect(m_recAction, SIGNAL(triggered()), this, SLOT(slotRecord()));
70 m_recAction->setCheckable(true);
72 layout->addWidget(toolbar);
73 ui.control_frame_firewire->setLayout(layout);
75 slotVideoDeviceChanged(ui.device_selector->currentIndex());
76 displayProcess = new QProcess;
77 captureProcess = new QProcess;
79 connect(captureProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(slotProcessStatus(QProcess::ProcessState)));
81 QStringList env = QProcess::systemEnvironment();
82 env << "SDL_WINDOWID=" + QString::number(ui.video_frame->winId());
83 displayProcess->setEnvironment(env);
85 if (KdenliveSettings::video4capture().isEmpty()) {
86 QString captureCommand;
87 if (!KdenliveSettings::video4adevice().isEmpty()) captureCommand = "-f " + KdenliveSettings::video4aformat() + " -i " + KdenliveSettings::video4adevice();
89 captureCommand += " -f " + KdenliveSettings::video4vformat() + " -s " + KdenliveSettings::video4size() + " -r " + QString::number(KdenliveSettings::video4rate()) + " -i " + KdenliveSettings::video4vdevice() + " -f " + KdenliveSettings::video4vencoding();
90 KdenliveSettings::setVideo4capture(captureCommand);
93 if (KdenliveSettings::video4playback().isEmpty()) {
94 QString playbackCommand;
95 playbackCommand = "-f " + KdenliveSettings::video4vencoding();
96 KdenliveSettings::setVideo4playback(playbackCommand);
98 kDebug() << "/////// BUILDING MONITOR, ID: " << ui.video_frame->winId();
101 QString RecMonitor::name() const {
105 void RecMonitor::slotVideoDeviceChanged(int ix) {
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);
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);
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);
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);
146 void RecMonitor::slotRewind() {
147 captureProcess->write("a", 1);
150 void RecMonitor::slotForward() {
151 captureProcess->write("z", 1);
154 void RecMonitor::slotStopCapture() {
156 if (ui.device_selector->currentIndex() == 0) {
157 captureProcess->write("\e", 2);
158 m_playAction->setIcon(m_playIcon);
161 if (m_isCapturing && ui.autoaddbox->isChecked()) emit addProjectClip(m_captureFile);
162 captureProcess->kill();
163 displayProcess->kill();
167 void RecMonitor::slotStartCapture(bool play) {
170 *captureProcess<<"dvgrab";
174 switch (m_recPanel->capture_format->currentItem()){
176 *captureProcess<<"--format"<<"dv1";
179 *captureProcess<<"--format"<<"dv2";
182 *captureProcess<<"--format"<<"hdv";
186 *captureProcess<<"--format"<<"raw";
190 if (KdenliveSettings::autosplit()) *captureProcess<<"--autosplit";
191 if (KdenliveSettings::timestamp()) *captureProcess<<"--timestamp";
192 *captureProcess<<"-i"<<"capture"<<"-";*/
195 QStringList captureArgs;
196 captureArgs<<"--format"<<"hdv"<<"-i"<<"capture"<<"-";
197 QStringList displayArgs;
199 displayArgs<<"-f"<<"mpegts"<<"-x"<<QString::number(ui.video_frame->width())<<"-y"<<QString::number(ui.video_frame->height())<<"-";
201 captureProcess->setStandardOutputProcess(displayProcess);
202 ui.video_frame->setScaledContents(false);
203 captureProcess->start("dvgrab",captureArgs);
204 displayProcess->start("ffplay", displayArgs);*/
207 if (captureProcess->state() != QProcess::NotRunning) {
208 if (ui.device_selector->currentIndex() == 0) {
210 captureProcess->write("k", 1);
211 //captureProcess->write("\e", 2);
212 m_playAction->setIcon(m_playIcon);
215 captureProcess->write("p", 1);
216 m_playAction->setIcon(m_pauseIcon);
222 m_captureArgs.clear();
223 m_displayArgs.clear();
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()) << "-";
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()) << "-";
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..."));
246 void RecMonitor::slotRecord() {
247 if (captureProcess->state() == QProcess::NotRunning && ui.device_selector->currentIndex() == 0) {
251 if (ui.device_selector->currentIndex() == 0) {
252 captureProcess->write("\e", 2);
253 m_playAction->setIcon(m_playIcon);
254 m_isCapturing = false;
256 m_recAction->setChecked(false);
259 QTimer::singleShot(1000, this, SLOT(slotStartCapture()));
262 } else if (ui.device_selector->currentIndex() == 0) {
263 m_isCapturing = true;
264 captureProcess->write("c\n", 3);
267 if (captureProcess->state() == QProcess::NotRunning) {
268 m_recAction->setChecked(true);
270 QString path = KdenliveSettings::capturefolder() + "/capture0000.mpg";
272 while (QFile::exists(path)) {
273 QString num = QString::number(i).rightJustified(4, '0', false);
274 path = KdenliveSettings::capturefolder() + "/capture" + num + ".mpg";
278 m_captureFile = KUrl(path);
280 m_isCapturing = true;
281 m_captureArgs.clear();
282 m_displayArgs.clear();
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()) << "-";
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()) << "-";
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..."));
300 displayProcess->kill();
301 captureProcess->kill();
302 QTimer::singleShot(1000, this, SLOT(slotRecord()));
306 void RecMonitor::slotProcessStatus(QProcess::ProcessState status) {
307 if (status == QProcess::NotRunning) {
308 m_isCapturing = 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"));
316 m_stopAction->setEnabled(true);
317 ui.device_selector->setEnabled(false);
322 void RecMonitor::mousePressEvent(QMouseEvent * event) {
326 void RecMonitor::activateRecMonitor() {
327 //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
330 void RecMonitor::stop() {
335 void RecMonitor::start() {
340 void RecMonitor::refreshRecMonitor(bool visible) {
342 //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
347 void RecMonitor::slotPlay() {
349 //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
354 #include "recmonitor.moc"