Changes to night mode handling:
[harmattan/cameraplus] / lib / qtcammode_p.h
1 // -*- c++ -*-
2
3 #ifndef QT_CAM_MODE_P_H
4 #define QT_CAM_MODE_P_H
5
6 #include <QSize>
7 #include "qtcamdevice_p.h"
8 #include <gst/pbutils/encoding-profile.h>
9 #include <gst/pbutils/encoding-target.h>
10
11 #ifndef GST_USE_UNSTABLE_API
12 #define GST_USE_UNSTABLE_API
13 #endif /* GST_USE_UNSTABLE_API */
14 #include <gst/interfaces/photography.h>
15
16 class QtCamDevicePrivate;
17 class PreviewImageHandler;
18 class DoneHandler;
19
20 class QtCamModePrivate {
21 public:
22   QtCamModePrivate(QtCamDevicePrivate *d) : id(-1), dev(d) {}
23   virtual ~QtCamModePrivate() {}
24
25   int modeId(const char *mode) {
26     if (!dev->cameraBin) {
27       return -1;
28     }
29
30     GParamSpec *pspec = g_object_class_find_property(G_OBJECT_GET_CLASS(dev->cameraBin),
31                                                      "mode");
32     if (!pspec) {
33       return -1;
34     }
35
36     if (!G_IS_PARAM_SPEC_ENUM(pspec)) {
37       return -1;
38     }
39
40     GParamSpecEnum *e = (GParamSpecEnum *)pspec;
41     GEnumClass *klass = e->enum_class;
42
43     for (unsigned x = 0; x < klass->n_values; x++) {
44       if (QLatin1String(mode) == QLatin1String(klass->values[x].value_nick)) {
45         return klass->values[x].value;
46       }
47     }
48
49     return -1;
50   }
51
52   GstEncodingProfile *loadProfile(const QString& path, const QString& name) {
53     GError *error = NULL;
54
55     GstEncodingTarget *target = gst_encoding_target_load_from_file(path.toUtf8().data(), &error);
56     if (!target) {
57       qCritical() << "Failed to load encoding target from" << path << error->message;
58       g_error_free(error);
59       return 0;
60     }
61
62     GstEncodingProfile *profile = gst_encoding_target_get_profile(target, name.toUtf8().data());
63     if (!profile) {
64       qCritical() << "Failed to load encoding profile from" << path;
65       gst_encoding_target_unref(target);
66       return 0;
67     }
68
69     gst_encoding_target_unref(target);
70
71     return profile;
72   }
73
74   void resetCaps(const char *property) {
75     if (!dev->cameraBin) {
76       return;
77     }
78
79     g_object_set(dev->cameraBin, property, NULL, NULL);
80   }
81
82   bool inNightMode() {
83     if (!dev->cameraBin) {
84       return false;
85     }
86
87     int val = 0;
88
89     g_object_get(dev->videoSource, "scene-mode", &val, NULL);
90
91     return val == GST_PHOTOGRAPHY_SCENE_MODE_NIGHT;
92   }
93
94   void setCaps(const char *property, const QSize& resolution, int fps) {
95     if (!dev->cameraBin) {
96       return;
97     }
98
99     if (resolution.width() <= 0 || resolution.height() <= 0) {
100       return;
101     }
102
103     GstCaps *caps = 0;
104
105     if (fps <= 0) {
106       caps = gst_caps_new_simple("video/x-raw-yuv",
107                                  "width", G_TYPE_INT, resolution.width(),
108                                  "height", G_TYPE_INT, resolution.height(),
109                                  NULL);
110     }
111     else {
112       caps = gst_caps_new_simple("video/x-raw-yuv",
113                                  "width", G_TYPE_INT, resolution.width(),
114                                  "height", G_TYPE_INT, resolution.height(),
115                                  "framerate",
116                                  GST_TYPE_FRACTION_RANGE, fps - 1, 1, fps + 1, 1,
117                                  NULL);
118     }
119
120     GstCaps *old = 0;
121
122     g_object_get(dev->cameraBin, property, &old, NULL);
123
124     if (gst_caps_is_equal(caps, old)) {
125       gst_caps_unref(old);
126       gst_caps_unref(caps);
127
128       return;
129     }
130
131     qDebug() << "Setting caps" << property << gst_caps_to_string(caps);
132
133     g_object_set(dev->cameraBin, property, caps, NULL);
134
135     if (old) {
136       gst_caps_unref(old);
137     }
138   }
139
140   int id;
141   QtCamDevicePrivate *dev;
142   PreviewImageHandler *previewImageHandler;
143   DoneHandler *doneHandler;
144 };
145
146 #endif /* QT_CAM_MODE_P_H */