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