87177cfaf946597c9788be51aed0b5721f7ae13a
[harmattan/cameraplus] / lib / qtcamvideomode.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "qtcamvideomode.h"
22 #include "qtcammode_p.h"
23 #include <QDebug>
24 #include "qtcamdevice_p.h"
25 #include "qtcamdevice.h"
26 #include "qtcamvideosettings.h"
27
28 class QtCamVideoModePrivate : public QtCamModePrivate {
29 public:
30   QtCamVideoModePrivate(QtCamDevicePrivate *dev) :
31   QtCamModePrivate(dev),
32   settings(dev->conf->defaultVideoSettings()) {
33
34   }
35
36   ~QtCamVideoModePrivate() {}
37
38   void _d_idleStateChanged(bool isIdle) {
39     if (isIdle && dev->active == dev->video) {
40       QMetaObject::invokeMethod(dev->video, "recordingStateChanged");
41       QMetaObject::invokeMethod(dev->video, "canCaptureChanged");
42     }
43   }
44
45   QtCamVideoSettings settings;
46 };
47
48 QtCamVideoMode::QtCamVideoMode(QtCamDevicePrivate *dev, QObject *parent) :
49   QtCamMode(new QtCamVideoModePrivate(dev), "mode-video", "video-done", parent) {
50
51   d = (QtCamVideoModePrivate *)QtCamMode::d_ptr;
52
53   QString name = d_ptr->dev->conf->videoEncodingProfileName();
54   QString path = d_ptr->dev->conf->videoEncodingProfilePath();
55
56   if (!name.isEmpty() && !path.isEmpty()) {
57     GstEncodingProfile *profile = d_ptr->loadProfile(path, name);
58     if (profile) {
59       setProfile(profile);
60     }
61   }
62
63   QObject::connect(d_ptr->dev->q_ptr, SIGNAL(idleStateChanged(bool)),
64                    this, SLOT(_d_idleStateChanged(bool)));
65 }
66
67 QtCamVideoMode::~QtCamVideoMode() {
68   d = 0;
69 }
70
71 bool QtCamVideoMode::canCapture() {
72   return QtCamMode::canCapture() && d_ptr->dev->q_ptr->isIdle();
73 }
74
75 void QtCamVideoMode::applySettings() {
76   bool night = d_ptr->inNightMode();
77
78   int fps = night ? d->settings.nightFrameRate() : d->settings.frameRate();
79
80   d_ptr->setCaps("viewfinder-caps", d->settings.captureResolution(), fps);
81
82   d_ptr->setCaps("video-capture-caps", d->settings.captureResolution(), fps);
83
84   setPreviewSize(d->settings.previewResolution());
85
86   // TODO:
87   //  d_ptr->resetCaps("image-capture-caps");
88 }
89
90 void QtCamVideoMode::start() {
91   // Nothing
92 }
93
94 void QtCamVideoMode::stop() {
95   if (isRecording()) {
96     stopRecording();
97   }
98 }
99
100 bool QtCamVideoMode::isRecording() {
101   return !d_ptr->dev->q_ptr->isIdle();
102 }
103
104 bool QtCamVideoMode::startRecording(const QString& fileName) {
105   if (!canCapture() || isRecording()) {
106     return false;
107   }
108
109   if (fileName.isEmpty()) {
110     return false;
111   }
112
113   setFileName(fileName);
114
115   g_object_set(d_ptr->dev->cameraBin, "location", fileName.toUtf8().data(), NULL);
116   g_signal_emit_by_name(d_ptr->dev->cameraBin, "start-capture", NULL);
117
118   emit recordingStateChanged();
119
120   emit canCaptureChanged();
121
122   return true;
123 }
124
125 void QtCamVideoMode::stopRecording() {
126   if (isRecording()) {
127     g_signal_emit_by_name(d_ptr->dev->cameraBin, "stop-capture", NULL);
128   }
129 }
130
131 bool QtCamVideoMode::setSettings(const QtCamVideoSettings& settings) {
132   d->settings = settings;
133
134   if (!d_ptr->dev->q_ptr->isRunning() || isRecording()) {
135     return false;
136   }
137
138   applySettings();
139
140   return true;
141 }
142
143 void QtCamVideoMode::setProfile(GstEncodingProfile *profile) {
144   if (!d_ptr->dev->cameraBin) {
145     gst_encoding_profile_unref(profile);
146     return;
147   }
148
149   g_object_set(d_ptr->dev->cameraBin, "video-profile", profile, NULL);
150 }
151
152 #include "moc_qtcamvideomode.cpp"