Disable viewfinder in settings pages
[harmattan/cameraplus] / lib / qtcammode_p.h
1 // -*- c++ -*-
2
3 /*!
4  * This file is part of CameraPlus.
5  *
6  * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #ifndef QT_CAM_MODE_P_H
24 #define QT_CAM_MODE_P_H
25
26 #include <QSize>
27 #include "qtcamdevice_p.h"
28 #include "qtcamanalysisbin.h"
29 #include <gst/pbutils/encoding-profile.h>
30 #include <gst/pbutils/encoding-target.h>
31
32 #ifndef GST_USE_UNSTABLE_API
33 #define GST_USE_UNSTABLE_API
34 #endif /* GST_USE_UNSTABLE_API */
35 #include <gst/interfaces/photography.h>
36
37 #define PREVIEW_CAPS "video/x-raw-rgb, width = (int) %1, height = (int) %2, bpp = (int) 32, depth = (int) 24, red_mask = (int) 65280, green_mask = (int) 16711680, blue_mask = (int) -16777216"
38
39 class QtCamDevicePrivate;
40 class PreviewImageHandler;
41 class DoneHandler;
42
43 class QtCamModePrivate {
44 public:
45   QtCamModePrivate(QtCamDevicePrivate *d) : id(-1), dev(d) {}
46   virtual ~QtCamModePrivate() {}
47
48   int modeId(const char *mode) {
49     if (!dev->cameraBin) {
50       return -1;
51     }
52
53     GParamSpec *pspec = g_object_class_find_property(G_OBJECT_GET_CLASS(dev->cameraBin),
54                                                      "mode");
55     if (!pspec) {
56       return -1;
57     }
58
59     if (!G_IS_PARAM_SPEC_ENUM(pspec)) {
60       return -1;
61     }
62
63     GParamSpecEnum *e = G_PARAM_SPEC_ENUM(pspec);
64     GEnumClass *klass = e->enum_class;
65
66     for (unsigned x = 0; x < klass->n_values; x++) {
67       if (QLatin1String(mode) == QLatin1String(klass->values[x].value_nick)) {
68         return klass->values[x].value;
69       }
70     }
71
72     return -1;
73   }
74
75   GstEncodingProfile *loadProfile(const QString& path, const QString& name) {
76     GError *error = NULL;
77
78     GstEncodingTarget *target = gst_encoding_target_load_from_file(path.toUtf8().data(), &error);
79     if (!target) {
80       qCritical() << "Failed to load encoding target from" << path << error->message;
81       g_error_free(error);
82       return 0;
83     }
84
85     GstEncodingProfile *profile = gst_encoding_target_get_profile(target, name.toUtf8().data());
86     if (!profile) {
87       qCritical() << "Failed to load encoding profile from" << path;
88       gst_encoding_target_unref(target);
89       return 0;
90     }
91
92     gst_encoding_target_unref(target);
93
94     return profile;
95   }
96
97   void resetCaps(const char *property) {
98     if (!dev->cameraBin) {
99       return;
100     }
101
102     g_object_set(dev->cameraBin, property, NULL, NULL);
103   }
104
105   bool inNightMode() {
106     if (!dev->cameraBin) {
107       return false;
108     }
109
110     int val = 0;
111
112     g_object_get(dev->videoSource, "scene-mode", &val, NULL);
113
114     return val == GST_PHOTOGRAPHY_SCENE_MODE_NIGHT;
115   }
116
117   void setCaps(const char *property, const QSize& resolution, int fps) {
118     if (!dev->cameraBin) {
119       return;
120     }
121
122     if (resolution.width() <= 0 || resolution.height() <= 0) {
123       return;
124     }
125
126     GstCaps *caps = 0;
127
128     if (fps <= 0) {
129       caps = gst_caps_new_simple("video/x-raw-yuv",
130                                  "width", G_TYPE_INT, resolution.width(),
131                                  "height", G_TYPE_INT, resolution.height(),
132                                  NULL);
133     }
134     else {
135       caps = gst_caps_new_simple("video/x-raw-yuv",
136                                  "width", G_TYPE_INT, resolution.width(),
137                                  "height", G_TYPE_INT, resolution.height(),
138                                  "framerate",
139                                  GST_TYPE_FRACTION_RANGE, fps - 1, 1, fps + 1, 1,
140                                  NULL);
141     }
142
143     GstCaps *old = 0;
144
145     g_object_get(dev->cameraBin, property, &old, NULL);
146
147     if (gst_caps_is_equal(caps, old)) {
148       gst_caps_unref(old);
149       gst_caps_unref(caps);
150
151       return;
152     }
153
154     g_object_set(dev->cameraBin, property, caps, NULL);
155
156     if (old) {
157       gst_caps_unref(old);
158     }
159   }
160
161   void setPreviewSize(const QSize& size) {
162     if (!dev->cameraBin) {
163       return;
164     }
165
166     if (size.width() <= 0 && size.height() <= 0) {
167       g_object_set(dev->cameraBin, "preview-caps", NULL, "post-previews", FALSE, NULL);
168     }
169     else {
170       QString preview = QString(PREVIEW_CAPS).arg(size.width()).arg(size.height());
171
172       GstCaps *caps = gst_caps_from_string(preview.toAscii());
173
174       g_object_set(dev->cameraBin, "preview-caps", caps, "post-previews", TRUE, NULL);
175
176       gst_caps_unref(caps);
177     }
178   }
179
180   void setFileName(const QString& file) {
181     fileName = file;
182   }
183
184   void setTempFileName(const QString& file) {
185     tempFileName = file;
186   }
187
188   void enableViewfinderFilters() {
189     if (dev->viewfinderFilters) {
190       dev->viewfinderFilters->setBlocked(false);
191     }
192   }
193
194   void disableViewfinderFilters() {
195     if (dev->viewfinderFilters) {
196       dev->viewfinderFilters->setBlocked(true);
197     }
198   }
199
200   int id;
201   QtCamMode *q_ptr;
202   QtCamDevicePrivate *dev;
203   PreviewImageHandler *previewImageHandler;
204   DoneHandler *doneHandler;
205   QString fileName;
206   QString tempFileName;
207 };
208
209 #endif /* QT_CAM_MODE_P_H */