Added enablePreview() and disablePreview() to QtCamMode
[harmattan/cameraplus] / lib / qtcamvideomode.cpp
index 87177cf..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
 #include "qtcamdevice_p.h"
 #include "qtcamdevice.h"
 #include "qtcamvideosettings.h"
+#include "qtcamnotifications.h"
+#include <QMutex>
+#include <QWaitCondition>
 
 class QtCamVideoModePrivate : public QtCamModePrivate {
 public:
   QtCamVideoModePrivate(QtCamDevicePrivate *dev) :
   QtCamModePrivate(dev),
-  settings(dev->conf->defaultVideoSettings()) {
+  settings(dev->conf->videoSettings(dev->id)),
+  resolution(settings->defaultResolution()) {
 
   }
 
-  ~QtCamVideoModePrivate() {}
+  ~QtCamVideoModePrivate() {
+    delete settings;
+  }
 
   void _d_idleStateChanged(bool isIdle) {
     if (isIdle && dev->active == dev->video) {
@@ -42,11 +48,57 @@ public:
     }
   }
 
-  QtCamVideoSettings settings;
+  QtCamVideoSettings *settings;
+  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;
 
@@ -75,25 +127,25 @@ bool QtCamVideoMode::canCapture() {
 void QtCamVideoMode::applySettings() {
   bool night = d_ptr->inNightMode();
 
-  int fps = night ? d->settings.nightFrameRate() : d->settings.frameRate();
+  int fps = night ? d->resolution.nightFrameRate() : d->resolution.frameRate();
 
-  d_ptr->setCaps("viewfinder-caps", d->settings.captureResolution(), fps);
+  d_ptr->setCaps("viewfinder-caps", d->resolution.captureResolution(), fps);
 
-  d_ptr->setCaps("video-capture-caps", d->settings.captureResolution(), fps);
+  d_ptr->setCaps("video-capture-caps", d->resolution.captureResolution(), fps);
 
-  setPreviewSize(d->settings.previewResolution());
+  enablePreview();
 
-  // TODO:
-  //  d_ptr->resetCaps("image-capture-caps");
+  // Not sure this is needed but just in case.
+  d_ptr->resetCaps("image-capture-caps");
 }
 
 void QtCamVideoMode::start() {
-  // Nothing
+  d_ptr->disableViewfinderFilters();
 }
 
 void QtCamVideoMode::stop() {
   if (isRecording()) {
-    stopRecording();
+    stopRecording(true);
   }
 }
 
@@ -101,7 +153,7 @@ bool QtCamVideoMode::isRecording() {
   return !d_ptr->dev->q_ptr->isIdle();
 }
 
-bool QtCamVideoMode::startRecording(const QString& fileName) {
+bool QtCamVideoMode::startRecording(const QString& fileName, const QString& tmpFileName) {
   if (!canCapture() || isRecording()) {
     return false;
   }
@@ -110,11 +162,19 @@ bool QtCamVideoMode::startRecording(const QString& fileName) {
     return false;
   }
 
-  setFileName(fileName);
+  d_ptr->setFileName(fileName);
+  d_ptr->setTempFileName(tmpFileName);
+
+  QString file = tmpFileName.isEmpty() ? fileName : tmpFileName;
 
-  g_object_set(d_ptr->dev->cameraBin, "location", fileName.toUtf8().data(), NULL);
+  QMetaObject::invokeMethod(d_ptr->dev->notifications, "videoRecordingStarted");
+
+  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();
@@ -122,16 +182,38 @@ bool QtCamVideoMode::startRecording(const QString& fileName) {
   return true;
 }
 
-void QtCamVideoMode::stopRecording() {
+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) {
+      handler->wait();
+      handler->unlock();
+    }
   }
 }
 
-bool QtCamVideoMode::setSettings(const QtCamVideoSettings& settings) {
-  d->settings = settings;
+bool QtCamVideoMode::setResolution(const QtCamVideoResolution& resolution) {
+  d->resolution = resolution;
 
-  if (!d_ptr->dev->q_ptr->isRunning() || isRecording()) {
+  if (!d_ptr->dev->q_ptr->isRunning()) {
+    // We will return true here because setting the resolution on a non-running pipeline
+    // doesn't make much sense (Probably the only use case is as a kind of optimization only).
+    // We will set it anyway when the pipeline gets started.
+    return true;
+  }
+
+  if (isRecording()) {
     return false;
   }
 
@@ -149,4 +231,16 @@ void QtCamVideoMode::setProfile(GstEncodingProfile *profile) {
   g_object_set(d_ptr->dev->cameraBin, "video-profile", profile, NULL);
 }
 
+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"