Added enablePreview() and disablePreview() to QtCamMode
[harmattan/cameraplus] / lib / qtcamvideomode.cpp
index bf5d0d6..d4b8035 100644 (file)
@@ -1,7 +1,7 @@
 /*!
  * This file is part of CameraPlus.
  *
- * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
+ * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -25,7 +25,8 @@
 #include "qtcamdevice.h"
 #include "qtcamvideosettings.h"
 #include "qtcamnotifications.h"
-#include "qtcamgstreamermessagelistener.h"
+#include <QMutex>
+#include <QWaitCondition>
 
 class QtCamVideoModePrivate : public QtCamModePrivate {
 public:
@@ -51,8 +52,53 @@ public:
   QtCamVideoResolution resolution;
 };
 
+class VideoDoneHandler : public DoneHandler {
+public:
+  VideoDoneHandler(QtCamModePrivate *d, QObject *parent = 0) :
+    DoneHandler(d, "video-done", parent), m_done(false) {}
+
+  virtual void handleMessage(GstMessage *message) {
+    DoneHandler::handleMessage(message);
+    wake();
+  }
+
+  void lock() {
+    m_mutex.lock();
+  }
+
+  void unlock() {
+    m_mutex.unlock();
+  }
+
+  void wait() {
+    m_cond.wait(&m_mutex);
+  }
+
+  void reset() {
+    m_done = false;
+  }
+
+  bool isDone() {
+    return m_done;
+  }
+
+private:
+  void wake() {
+    lock();
+    m_done = true;
+    m_cond.wakeOne();
+    unlock();
+  }
+
+  bool m_done;
+  QMutex m_mutex;
+  QWaitCondition m_cond;
+};
+
 QtCamVideoMode::QtCamVideoMode(QtCamDevicePrivate *dev, QObject *parent) :
-  QtCamMode(new QtCamVideoModePrivate(dev), "mode-video", "video-done", parent) {
+  QtCamMode(new QtCamVideoModePrivate(dev), "mode-video", parent) {
+
+  d_ptr->init(new VideoDoneHandler(d_ptr, this));
 
   d = (QtCamVideoModePrivate *)QtCamMode::d_ptr;
 
@@ -87,7 +133,7 @@ void QtCamVideoMode::applySettings() {
 
   d_ptr->setCaps("video-capture-caps", d->resolution.captureResolution(), fps);
 
-  d_ptr->setPreviewSize(d->resolution.previewResolution());
+  enablePreview();
 
   // Not sure this is needed but just in case.
   d_ptr->resetCaps("image-capture-caps");
@@ -126,6 +172,9 @@ bool QtCamVideoMode::startRecording(const QString& fileName, const QString& tmpF
   g_object_set(d_ptr->dev->cameraBin, "location", file.toUtf8().data(), NULL);
   g_signal_emit_by_name(d_ptr->dev->cameraBin, "start-capture", NULL);
 
+  VideoDoneHandler *handler = dynamic_cast<VideoDoneHandler *>(d_ptr->doneHandler);
+  handler->reset();
+
   emit recordingStateChanged();
 
   emit canCaptureChanged();
@@ -135,13 +184,21 @@ bool QtCamVideoMode::startRecording(const QString& fileName, const QString& tmpF
 
 void QtCamVideoMode::stopRecording(bool sync) {
   if (isRecording()) {
+    VideoDoneHandler *handler = dynamic_cast<VideoDoneHandler *>(d_ptr->doneHandler);
+    if (sync) {
+      handler->lock();
+
+      if (handler->isDone()) {
+       handler->unlock();
+       return;
+      }
+    }
+
     g_signal_emit_by_name(d_ptr->dev->cameraBin, "stop-capture", NULL);
 
     if (sync) {
-      GstMessage *msg = d_ptr->dev->listener->waitForMessage(QLatin1String("video-done"));
-      if (msg) {
-       gst_message_unref(msg);
-      }
+      handler->wait();
+      handler->unlock();
     }
   }
 }
@@ -178,4 +235,12 @@ QtCamVideoSettings *QtCamVideoMode::settings() {
   return d->settings;
 }
 
+QtCamVideoResolution QtCamVideoMode::currentResolution() {
+  return d->resolution;
+}
+
+void QtCamVideoMode::enablePreview() {
+  d_ptr->setPreviewSize(d->resolution.previewResolution());
+}
+
 #include "moc_qtcamvideomode.cpp"