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), m_done(false) {}
60 virtual void handleMessage(GstMessage *message) {
61 DoneHandler::handleMessage(message);
74 m_cond.wait(&m_mutex);
95 QWaitCondition m_cond;
98 QtCamVideoMode::QtCamVideoMode(QtCamDevicePrivate *dev, QObject *parent) :
99 QtCamMode(new QtCamVideoModePrivate(dev), "mode-video", parent) {
101 d_ptr->init(new VideoDoneHandler(d_ptr, this));
103 d = (QtCamVideoModePrivate *)QtCamMode::d_ptr;
105 QString name = d_ptr->dev->conf->videoEncodingProfileName();
106 QString path = d_ptr->dev->conf->videoEncodingProfilePath();
108 if (!name.isEmpty() && !path.isEmpty()) {
109 GstEncodingProfile *profile = d_ptr->loadProfile(path, name);
115 QObject::connect(d_ptr->dev->q_ptr, SIGNAL(idleStateChanged(bool)),
116 this, SLOT(_d_idleStateChanged(bool)));
119 QtCamVideoMode::~QtCamVideoMode() {
123 bool QtCamVideoMode::canCapture() {
124 return QtCamMode::canCapture() && d_ptr->dev->q_ptr->isIdle();
127 void QtCamVideoMode::applySettings() {
128 bool night = d_ptr->inNightMode();
130 int fps = night ? d->resolution.nightFrameRate() : d->resolution.frameRate();
132 d_ptr->setCaps("viewfinder-caps", d->resolution.captureResolution(), fps);
134 d_ptr->setCaps("video-capture-caps", d->resolution.captureResolution(), fps);
136 d_ptr->setPreviewSize(d->resolution.previewResolution());
138 // Not sure this is needed but just in case.
139 d_ptr->resetCaps("image-capture-caps");
142 void QtCamVideoMode::start() {
143 d_ptr->disableViewfinderFilters();
146 void QtCamVideoMode::stop() {
152 bool QtCamVideoMode::isRecording() {
153 return !d_ptr->dev->q_ptr->isIdle();
156 bool QtCamVideoMode::startRecording(const QString& fileName, const QString& tmpFileName) {
157 if (!canCapture() || isRecording()) {
161 if (fileName.isEmpty()) {
165 d_ptr->setFileName(fileName);
166 d_ptr->setTempFileName(tmpFileName);
168 QString file = tmpFileName.isEmpty() ? fileName : tmpFileName;
170 QMetaObject::invokeMethod(d_ptr->dev->notifications, "videoRecordingStarted");
172 g_object_set(d_ptr->dev->cameraBin, "location", file.toUtf8().data(), NULL);
173 g_signal_emit_by_name(d_ptr->dev->cameraBin, "start-capture", NULL);
175 VideoDoneHandler *handler = dynamic_cast<VideoDoneHandler *>(d_ptr->doneHandler);
178 emit recordingStateChanged();
180 emit canCaptureChanged();
185 void QtCamVideoMode::stopRecording(bool sync) {
187 VideoDoneHandler *handler = dynamic_cast<VideoDoneHandler *>(d_ptr->doneHandler);
191 if (handler->isDone()) {
197 g_signal_emit_by_name(d_ptr->dev->cameraBin, "stop-capture", NULL);
206 bool QtCamVideoMode::setResolution(const QtCamVideoResolution& resolution) {
207 d->resolution = resolution;
209 if (!d_ptr->dev->q_ptr->isRunning()) {
210 // We will return true here because setting the resolution on a non-running pipeline
211 // doesn't make much sense (Probably the only use case is as a kind of optimization only).
212 // We will set it anyway when the pipeline gets started.
225 void QtCamVideoMode::setProfile(GstEncodingProfile *profile) {
226 if (!d_ptr->dev->cameraBin) {
227 gst_encoding_profile_unref(profile);
231 g_object_set(d_ptr->dev->cameraBin, "video-profile", profile, NULL);
234 QtCamVideoSettings *QtCamVideoMode::settings() {
238 #include "moc_qtcamvideomode.cpp"