]> git.sesse.net Git - kdenlive/blob - src/v4l/v4lcapture.cpp
webcam capture: Try to get webcam name instead of displaying /dev/video0
[kdenlive] / src / v4l / v4lcapture.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 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 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <pthread.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26
27 #include <QDebug>
28 #include <QImage>
29 #include <QTimer>
30 #include <QPainter>
31
32 #include <KDebug>
33 #include <KLocale>
34
35 #include "v4lcapture.h"
36 #include "kdenlivesettings.h"
37
38
39 static src_t v4lsrc;
40
41 class MyDisplay : public QLabel
42 {
43 public:
44     MyDisplay(QWidget *parent = 0);
45     void setImage(QImage img);
46     virtual void paintEvent(QPaintEvent *);
47     virtual void resizeEvent(QResizeEvent *);
48
49 private:
50     QImage m_img;
51     bool m_clear;
52 };
53
54 MyDisplay::MyDisplay(QWidget *parent):
55     QLabel(parent)
56     , m_clear(false)
57 {
58     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
59     setAttribute(Qt::WA_PaintOnScreen);
60     setAttribute(Qt::WA_OpaquePaintEvent);
61 }
62
63 void MyDisplay::resizeEvent(QResizeEvent *)
64 {
65     m_clear = true;
66 }
67
68 void MyDisplay::paintEvent(QPaintEvent *)
69 {
70     QPainter p(this);
71     if (m_clear) {
72         // widget resized, cleanup
73         p.fillRect(0, 0, width(), height(), palette().background());
74         m_clear = false;
75     }
76     if (m_img.isNull()) return;
77     QImage img = m_img.scaled(width(), height(), Qt::KeepAspectRatio);
78     p.drawImage((width() - img.width()) / 2, (height() - img.height()) / 2, img);
79     p.end();
80 }
81
82 void MyDisplay::setImage(QImage img)
83 {
84     m_img = img;
85     update();
86 }
87
88
89
90 V4lCaptureHandler::V4lCaptureHandler(QVBoxLayout *lay, QWidget *parent):
91     CaptureHandler(lay, parent)
92     , m_update(false)
93 {
94     if (lay == NULL) return;
95     m_display = new MyDisplay;
96     lay->addWidget(m_display);
97 }
98
99 QString V4lCaptureHandler::getDeviceName(QString input)
100 {
101     fswebcam_config_t *config;
102     /* Prepare the configuration structure. */
103     config = (fswebcam_config_t *) calloc(sizeof(fswebcam_config_t), 1);
104     if (!config) {
105         /*WARN("Out of memory.");*/
106         fprintf(stderr, "Out of MEM....");
107         return input;
108     }
109
110     /* Set the defaults. */
111     config->loop = 0;
112     config->offset = 0;
113     config->background = 0;
114     config->pidfile = NULL;
115     config->logfile = NULL;
116     config->gmt = 0;
117     config->start = 0;
118     config->device = strdup(input.toUtf8().constData());
119     config->input = NULL;
120     config->tuner = 0;
121     config->frequency = 0;
122     config->delay = 0;
123     config->use_read = 0;
124     config->list = 0;
125     config->width = 384;
126     config->height = 288;
127     config->fps = 0;
128     config->frames = 1;
129     config->skipframes = 0;
130     config->palette = SRC_PAL_ANY;
131     config->option = NULL;
132     config->dumpframe = NULL;
133     config->jobs = 0;
134     config->job = NULL;
135
136     /* Set defaults and parse the command line. */
137     /*if(fswc_getopts(config, argc, argv)) return(-1);*/
138
139
140     /* Record the start time. */
141     config->start = time(NULL);
142     /* Set source options... */
143     memset(&v4lsrc, 0, sizeof(v4lsrc));
144     v4lsrc.input      = config->input;
145     v4lsrc.tuner      = config->tuner;
146     v4lsrc.frequency  = config->frequency;
147     v4lsrc.delay      = config->delay;
148     v4lsrc.timeout    = 10; /* seconds */
149     v4lsrc.use_read   = config->use_read;
150     v4lsrc.list       = config->list;
151     v4lsrc.palette    = config->palette;
152     v4lsrc.width      = config->width;
153     v4lsrc.height     = config->height;
154     v4lsrc.fps        = config->fps;
155     v4lsrc.option     = config->option;
156     char *source = config->device;
157
158     QString deviceName = src_query(&v4lsrc, source);
159     kDebug() << "DEVIE NAME: " << deviceName << ".";
160     return deviceName.isEmpty() ? input : deviceName;
161 }
162
163 void V4lCaptureHandler::startPreview(int /*deviceId*/, int /*captureMode*/)
164 {
165     m_display->setHidden(false);
166     fswebcam_config_t *config;
167     /* Prepare the configuration structure. */
168     config = (fswebcam_config_t *) calloc(sizeof(fswebcam_config_t), 1);
169     if (!config) {
170         /*WARN("Out of memory.");*/
171         fprintf(stderr, "Out of MEM....");
172         return;
173     }
174
175     /* Set the defaults. */
176     config->loop = 0;
177     config->offset = 0;
178     config->background = 0;
179     config->pidfile = NULL;
180     config->logfile = NULL;
181     config->gmt = 0;
182     config->start = 0;
183     config->device = strdup(KdenliveSettings::video4vdevice().toUtf8().constData());
184     config->input = NULL;
185     config->tuner = 0;
186     config->frequency = 0;
187     config->delay = 0;
188     config->use_read = 0;
189     config->list = 0;
190     config->width = KdenliveSettings::video4size().section("x", 0, 0).toInt();/*384;*/
191     config->height = KdenliveSettings::video4size().section("x", -1).toInt();/*288;*/
192     config->fps = 0;
193     config->frames = 1;
194     config->skipframes = 0;
195     config->palette = SRC_PAL_ANY;
196     config->option = NULL;
197     config->dumpframe = NULL;
198     config->jobs = 0;
199     config->job = NULL;
200
201     /* Set defaults and parse the command line. */
202     /*if(fswc_getopts(config, argc, argv)) return(-1);*/
203
204
205     /* Record the start time. */
206     config->start = time(NULL);
207     /* Set source options... */
208     memset(&v4lsrc, 0, sizeof(v4lsrc));
209     v4lsrc.input      = config->input;
210     v4lsrc.tuner      = config->tuner;
211     v4lsrc.frequency  = config->frequency;
212     v4lsrc.delay      = config->delay;
213     v4lsrc.timeout    = 10; /* seconds */
214     v4lsrc.use_read   = config->use_read;
215     v4lsrc.list       = config->list;
216     v4lsrc.palette    = config->palette;
217     v4lsrc.width      = config->width;
218     v4lsrc.height     = config->height;
219     v4lsrc.fps        = config->fps;
220     v4lsrc.option     = config->option;
221     char *source = config->device;
222
223     if (src_open(&v4lsrc, source) != 0) return;
224     m_update = true;
225     QTimer::singleShot(200, this, SLOT(slotUpdate()));
226 }
227
228 V4lCaptureHandler::~V4lCaptureHandler()
229 {
230     stopCapture();
231 }
232
233 void V4lCaptureHandler::slotUpdate()
234 {
235     if (!m_update) return;
236     src_grab(&v4lsrc);
237     uint8_t *img = (uint8_t *) v4lsrc.img;
238     uint32_t i = v4lsrc.width * v4lsrc.height;
239
240     if (v4lsrc.length << 2 < i) return;
241
242     QImage qimg(v4lsrc.width, v4lsrc.height, QImage::Format_RGB32);
243     //Format_ARGB32_Premultiplied
244     //convert from uyvy422 to rgba
245     CaptureHandler::yuv2rgb((uchar *)img, (uchar *)qimg.bits(), v4lsrc.width, v4lsrc.height);
246     if (!m_captureFramePath.isEmpty()) {
247         qimg.save(m_captureFramePath);
248         emit frameSaved(m_captureFramePath);
249         m_captureFramePath.clear();
250     }
251     if (!m_overlayImage.isNull()) {
252         // overlay image
253         QPainter p(&qimg);
254         p.setOpacity(0.5);
255         p.drawImage(0, 0, m_overlayImage);
256         p.end();
257     }
258     m_display->setImage(qimg);
259     if (m_update) QTimer::singleShot(200, this, SLOT(slotUpdate()));
260 }
261
262 void V4lCaptureHandler::startCapture(const QString &/*path*/)
263 {
264 }
265
266 void V4lCaptureHandler::stopCapture()
267 {
268 }
269
270 void V4lCaptureHandler::captureFrame(const QString &fname)
271 {
272     m_captureFramePath = fname;
273 }
274
275 void V4lCaptureHandler::showOverlay(QImage img, bool /*transparent*/)
276 {
277     m_overlayImage = img;
278 }
279
280 void V4lCaptureHandler::hideOverlay()
281 {
282     m_overlayImage = QImage();
283 }
284
285 void V4lCaptureHandler::hidePreview(bool hide)
286 {
287     m_display->setHidden(hide);
288 }
289
290 void V4lCaptureHandler::stopPreview()
291 {
292     if (!m_update) return;
293     m_update = false;
294     src_close(&v4lsrc);
295 }