2 * This file is part of CameraPlus.
4 * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "qtcamvideomode.h"
22 #include "qtcammode_p.h"
24 #include "qtcamdevice_p.h"
25 #include "qtcamdevice.h"
26 #include "qtcamvideosettings.h"
27 #include "qtcamnotifications.h"
29 #include <QWaitCondition>
31 class QtCamVideoModePrivate : public QtCamModePrivate {
33 QtCamVideoModePrivate(QtCamDevicePrivate *dev) :
34 QtCamModePrivate(dev),
35 settings(dev->conf->videoSettings(dev->id)),
36 resolution(settings->defaultResolution()) {
40 ~QtCamVideoModePrivate() {
44 void _d_idleStateChanged(bool isIdle) {
45 if (isIdle && dev->active == dev->video) {
46 QMetaObject::invokeMethod(dev->video, "recordingStateChanged");
47 QMetaObject::invokeMethod(dev->video, "canCaptureChanged");
51 QtCamVideoSettings *settings;
52 QtCamVideoResolution resolution;
55 class VideoDoneHandler : public DoneHandler {
57 VideoDoneHandler(QtCamModePrivate *d, QObject *parent = 0) :
58 DoneHandler(d, "video-done", parent) {}
60 virtual void handleMessage(GstMessage *message) {
61 DoneHandler::handleMessage(message);
81 m_cond.wait(&m_mutex);
86 QWaitCondition m_cond;
89 QtCamVideoMode::QtCamVideoMode(QtCamDevicePrivate *dev, QObject *parent) :
90 QtCamMode(new QtCamVideoModePrivate(dev), "mode-video", parent) {
92 d_ptr->init(new VideoDoneHandler(d_ptr, this));
94 d = (QtCamVideoModePrivate *)QtCamMode::d_ptr;
96 QString name = d_ptr->dev->conf->videoEncodingProfileName();
97 QString path = d_ptr->dev->conf->videoEncodingProfilePath();
99 if (!name.isEmpty() && !path.isEmpty()) {
100 GstEncodingProfile *profile = d_ptr->loadProfile(path, name);
106 QObject::connect(d_ptr->dev->q_ptr, SIGNAL(idleStateChanged(bool)),
107 this, SLOT(_d_idleStateChanged(bool)));
110 QtCamVideoMode::~QtCamVideoMode() {
114 bool QtCamVideoMode::canCapture() {
115 return QtCamMode::canCapture() && d_ptr->dev->q_ptr->isIdle();
118 void QtCamVideoMode::applySettings() {
119 bool night = d_ptr->inNightMode();
121 int fps = night ? d->resolution.nightFrameRate() : d->resolution.frameRate();
123 d_ptr->setCaps("viewfinder-caps", d->resolution.captureResolution(), fps);
125 d_ptr->setCaps("video-capture-caps", d->resolution.captureResolution(), fps);
127 d_ptr->setPreviewSize(d->resolution.previewResolution());
129 // Not sure this is needed but just in case.
130 d_ptr->resetCaps("image-capture-caps");
133 void QtCamVideoMode::start() {
134 d_ptr->disableViewfinderFilters();
137 void QtCamVideoMode::stop() {
143 bool QtCamVideoMode::isRecording() {
144 return !d_ptr->dev->q_ptr->isIdle();
147 bool QtCamVideoMode::startRecording(const QString& fileName, const QString& tmpFileName) {
148 if (!canCapture() || isRecording()) {
152 if (fileName.isEmpty()) {
156 d_ptr->setFileName(fileName);
157 d_ptr->setTempFileName(tmpFileName);
159 QString file = tmpFileName.isEmpty() ? fileName : tmpFileName;
161 QMetaObject::invokeMethod(d_ptr->dev->notifications, "videoRecordingStarted");
163 g_object_set(d_ptr->dev->cameraBin, "location", file.toUtf8().data(), NULL);
164 g_signal_emit_by_name(d_ptr->dev->cameraBin, "start-capture", NULL);
166 emit recordingStateChanged();
168 emit canCaptureChanged();
173 void QtCamVideoMode::stopRecording(bool sync) {
175 VideoDoneHandler *handler = dynamic_cast<VideoDoneHandler *>(d_ptr->doneHandler);
180 g_signal_emit_by_name(d_ptr->dev->cameraBin, "stop-capture", NULL);
189 bool QtCamVideoMode::setResolution(const QtCamVideoResolution& resolution) {
190 d->resolution = resolution;
192 if (!d_ptr->dev->q_ptr->isRunning()) {
193 // We will return true here because setting the resolution on a non-running pipeline
194 // doesn't make much sense (Probably the only use case is as a kind of optimization only).
195 // We will set it anyway when the pipeline gets started.
208 void QtCamVideoMode::setProfile(GstEncodingProfile *profile) {
209 if (!d_ptr->dev->cameraBin) {
210 gst_encoding_profile_unref(profile);
214 g_object_set(d_ptr->dev->cameraBin, "video-profile", profile, NULL);
217 QtCamVideoSettings *QtCamVideoMode::settings() {
221 #include "moc_qtcamvideomode.cpp"