9894de8f12a7367231116fd43bfe2419bedd6d8d
[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 <QFileInfo>
28 #include <QDir>
29 #include "qtcamdevice_p.h"
30 #include "qtcamanalysisbin.h"
31 #include <gst/pbutils/encoding-profile.h>
32 #include <gst/pbutils/encoding-target.h>
33
34 #ifndef GST_USE_UNSTABLE_API
35 #define GST_USE_UNSTABLE_API
36 #endif /* GST_USE_UNSTABLE_API */
37 #include <gst/interfaces/photography.h>
38
39 #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"
40
41 class QtCamDevicePrivate;
42 class PreviewImageHandler;
43 class DoneHandler;
44
45 class QtCamModePrivate {
46 public:
47   QtCamModePrivate(QtCamDevicePrivate *d) : id(-1), dev(d) {}
48   virtual ~QtCamModePrivate() {}
49
50   int modeId(const char *mode) {
51     if (!dev->cameraBin) {
52       return -1;
53     }
54
55     GParamSpec *pspec = g_object_class_find_property(G_OBJECT_GET_CLASS(dev->cameraBin),
56                                                      "mode");
57     if (!pspec) {
58       return -1;
59     }
60
61     if (!G_IS_PARAM_SPEC_ENUM(pspec)) {
62       return -1;
63     }
64
65     GParamSpecEnum *e = G_PARAM_SPEC_ENUM(pspec);
66     GEnumClass *klass = e->enum_class;
67
68     for (unsigned x = 0; x < klass->n_values; x++) {
69       if (QLatin1String(mode) == QLatin1String(klass->values[x].value_nick)) {
70         return klass->values[x].value;
71       }
72     }
73
74     return -1;
75   }
76
77   GstEncodingProfile *loadProfile(const QString& path, const QString& name) {
78     GError *error = NULL;
79     QString targetPath;
80     QFileInfo info(path);
81     if (!info.isAbsolute()) {
82       targetPath = QDir(DATA_DIR).absoluteFilePath(path);
83     }
84     else {
85       targetPath = info.filePath();
86     }
87
88     GstEncodingTarget *target = gst_encoding_target_load_from_file(targetPath.toUtf8().constData(),
89                                                                    &error);
90     if (!target) {
91       qCritical() << "Failed to load encoding target from" << path << error->message;
92       g_error_free(error);
93       return 0;
94     }
95
96     GstEncodingProfile *profile = gst_encoding_target_get_profile(target, name.toUtf8().data());
97     if (!profile) {
98       qCritical() << "Failed to load encoding profile from" << path;
99       gst_encoding_target_unref(target);
100       return 0;
101     }
102
103     gst_encoding_target_unref(target);
104
105     return profile;
106   }
107
108   void resetCaps(const char *property) {
109     if (!dev->cameraBin) {
110       return;
111     }
112
113     g_object_set(dev->cameraBin, property, NULL, NULL);
114   }
115
116   bool inNightMode() {
117     if (!dev->cameraBin) {
118       return false;
119     }
120
121     int val = 0;
122
123     g_object_get(dev->videoSource, "scene-mode", &val, NULL);
124
125     return val == GST_PHOTOGRAPHY_SCENE_MODE_NIGHT;
126   }
127
128   void setCaps(const char *property, const QSize& resolution, int fps) {
129     if (!dev->cameraBin) {
130       return;
131     }
132
133     if (resolution.width() <= 0 || resolution.height() <= 0) {
134       return;
135     }
136
137     GstCaps *caps = 0;
138
139     if (fps <= 0) {
140       caps = gst_caps_new_simple("video/x-raw-yuv",
141                                  "width", G_TYPE_INT, resolution.width(),
142                                  "height", G_TYPE_INT, resolution.height(),
143                                  NULL);
144     }
145     else {
146       caps = gst_caps_new_simple("video/x-raw-yuv",
147                                  "width", G_TYPE_INT, resolution.width(),
148                                  "height", G_TYPE_INT, resolution.height(),
149                                  "framerate",
150                                  GST_TYPE_FRACTION_RANGE, fps - 1, 1, fps + 1, 1,
151                                  NULL);
152     }
153
154     GstCaps *old = 0;
155
156     g_object_get(dev->cameraBin, property, &old, NULL);
157
158     if (gst_caps_is_equal(caps, old)) {
159       gst_caps_unref(old);
160       gst_caps_unref(caps);
161
162       return;
163     }
164
165     g_object_set(dev->cameraBin, property, caps, NULL);
166
167     if (old) {
168       gst_caps_unref(old);
169     }
170   }
171
172   void setPreviewSize(const QSize& size) {
173     if (!dev->cameraBin) {
174       return;
175     }
176
177     if (size.width() <= 0 && size.height() <= 0) {
178       g_object_set(dev->cameraBin, "preview-caps", NULL, "post-previews", FALSE, NULL);
179     }
180     else {
181       QString preview = QString(PREVIEW_CAPS).arg(size.width()).arg(size.height());
182
183       GstCaps *caps = gst_caps_from_string(preview.toAscii());
184
185       g_object_set(dev->cameraBin, "preview-caps", caps, "post-previews", TRUE, NULL);
186
187       gst_caps_unref(caps);
188     }
189   }
190
191   void setFileName(const QString& file) {
192     fileName = file;
193   }
194
195   void setTempFileName(const QString& file) {
196     tempFileName = file;
197   }
198
199   void enableViewfinderFilters() {
200     if (dev->viewfinderFilters) {
201       dev->viewfinderFilters->setBlocked(false);
202     }
203   }
204
205   void disableViewfinderFilters() {
206     if (dev->viewfinderFilters) {
207       dev->viewfinderFilters->setBlocked(true);
208     }
209   }
210
211   int id;
212   QtCamMode *q_ptr;
213   QtCamDevicePrivate *dev;
214   PreviewImageHandler *previewImageHandler;
215   DoneHandler *doneHandler;
216   QString fileName;
217   QString tempFileName;
218 };
219
220 #endif /* QT_CAM_MODE_P_H */