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