864383a4847da238e14323c3f9b741aa22b2d968
[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 *d, QObject *parent) :
29   QtCamMode(new QtCamVideoModePrivate(d), "mode-video", "video-done", parent) {
30
31   d_ptr = (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
49 }
50
51 bool QtCamVideoMode::canCapture() {
52   return d_ptr->dev->q_ptr->isIdle();
53 }
54
55 void QtCamVideoMode::applySettings() {
56   d_ptr->setCaps("viewfinder-caps", d_ptr->settings.captureResolution(),
57           d_ptr->settings.frameRate());
58
59   d_ptr->setCaps("video-capture-caps", d_ptr->settings.captureResolution(),
60           d_ptr->settings.frameRate());
61
62   setPreviewSize(d_ptr->settings.previewResolution());
63 }
64
65 void QtCamVideoMode::start() {
66   // Nothing
67 }
68
69 void QtCamVideoMode::stop() {
70   if (isRecording()) {
71     stopRecording();
72   }
73 }
74
75 bool QtCamVideoMode::isRecording() {
76   return !d_ptr->dev->q_ptr->isIdle();
77 }
78
79 bool QtCamVideoMode::startRecording(const QString& fileName) {
80   if (!canCapture() || isRecording()) {
81     return false;
82   }
83
84   setFileName(fileName);
85
86   g_object_set(d_ptr->dev->cameraBin, "location", fileName.toUtf8().data(), NULL);
87   g_signal_emit_by_name(d_ptr->dev->cameraBin, "start-capture", NULL);
88
89   emit recordingStateChanged();
90
91   emit canCaptureChanged();
92
93   return true;
94 }
95
96 void QtCamVideoMode::stopRecording() {
97   if (isRecording()) {
98     g_signal_emit_by_name(d_ptr->dev->cameraBin, "stop-capture", NULL);
99   }
100 }
101
102 bool QtCamVideoMode::setSettings(const QtCamVideoSettings& settings) {
103   d_ptr->settings = settings;
104
105   if (isRecording()) {
106     return false;
107   }
108
109   applySettings();
110
111   return true;
112 }
113
114 void QtCamVideoMode::setProfile(GstEncodingProfile *profile) {
115   if (!d_ptr->dev->cameraBin) {
116     gst_encoding_profile_unref(profile);
117     return;
118   }
119
120   g_object_set(d_ptr->dev->cameraBin, "video-profile", profile, NULL);
121 }
122
123 #include "moc_qtcamvideomode.cpp"