Record video in a hidden directory then copy it to target directory.
authorMohammed Sameer <msameer@foolab.org>
Tue, 9 Oct 2012 21:31:56 +0000 (00:31 +0300)
committerMohammed Sameer <msameer@foolab.org>
Tue, 9 Oct 2012 21:31:56 +0000 (00:31 +0300)
This is to avoid tracker indexing the video being recorded and waste CPU time.
The problem with tracker is it keeps getting update events because the file is
being written to. Tracker will ignore them but that still wastes CPU.

imports/videomode.cpp
imports/videomode.h
lib/qtcammode.cpp
lib/qtcammode_p.h
lib/qtcamvideomode.cpp
lib/qtcamvideomode.h
qml/VideoPage.qml
qml/main.qml
src/filenaming.cpp
src/filenaming.h

index bedba7f..7be1d77 100644 (file)
@@ -34,8 +34,8 @@ VideoMode::~VideoMode() {
 }
 
 
-bool VideoMode::startRecording(const QString& fileName) {
-  return m_video ? m_video->startRecording(fileName) : false;
+bool VideoMode::startRecording(const QString& fileName, const QString& tmpFileName) {
+  return m_video ? m_video->startRecording(fileName, tmpFileName) : false;
 }
 
 void VideoMode::stopRecording() {
index a229930..b8e31ea 100644 (file)
@@ -35,7 +35,7 @@ public:
   VideoMode(QObject *parent = 0);
   ~VideoMode();
 
-  Q_INVOKABLE bool startRecording(const QString& fileName);
+  Q_INVOKABLE bool startRecording(const QString& fileName, const QString& tmpFileName);
 
   bool isRecording();
 
index 47c3978..1b9fc75 100644 (file)
@@ -27,6 +27,7 @@
 #include "qtcamgstreamermessagelistener.h"
 #include <gst/video/video.h>
 #include <QImage>
+#include <QFile>
 
 class PreviewImageHandler : public QtCamGStreamerMessageHandler {
 public:
@@ -94,7 +95,18 @@ public:
     if (gst_structure_has_field(s, "filename")) {
       const char *str = gst_structure_get_string(s, "filename");
       if (str) {
-       mode->fileName = QString::fromUtf8(str);
+       mode->tempFileName = QString::fromUtf8(str);
+      }
+    }
+
+    if (mode->fileName.isEmpty()) {
+      mode->fileName = mode->tempFileName;
+    }
+
+    if (!mode->tempFileName.isEmpty() && !mode->fileName.isEmpty() &&
+       mode->tempFileName != mode->fileName) {
+      if (!QFile::rename(mode->tempFileName, mode->fileName)) {
+       qCritical() << "Failed to rename" << mode->tempFileName << "to" << mode->fileName;
       }
     }
 
index e63390b..201d0ac 100644 (file)
@@ -179,6 +179,9 @@ public:
   void setFileName(const QString& file) {
     fileName = file;
   }
+  void setTempFileName(const QString& file) {
+    tempFileName = file;
+  }
 
   int id;
   QtCamMode *q_ptr;
@@ -186,6 +189,7 @@ public:
   PreviewImageHandler *previewImageHandler;
   DoneHandler *doneHandler;
   QString fileName;
+  QString tempFileName;
 };
 
 #endif /* QT_CAM_MODE_P_H */
index 1f7121f..9573226 100644 (file)
@@ -106,7 +106,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;
   }
@@ -116,10 +116,13 @@ bool QtCamVideoMode::startRecording(const QString& fileName) {
   }
 
   d_ptr->setFileName(fileName);
+  d_ptr->setTempFileName(tmpFileName);
+
+  QString file = tmpFileName.isEmpty() ? fileName : tmpFileName;
 
   QMetaObject::invokeMethod(d_ptr->dev->notifications, "videoRecordingStarted");
 
-  g_object_set(d_ptr->dev->cameraBin, "location", fileName.toUtf8().data(), NULL);
+  g_object_set(d_ptr->dev->cameraBin, "location", file.toUtf8().data(), NULL);
   g_signal_emit_by_name(d_ptr->dev->cameraBin, "start-capture", NULL);
 
   emit recordingStateChanged();
index 909403d..eba10a9 100644 (file)
@@ -44,7 +44,8 @@ public:
   virtual void applySettings();
 
   bool isRecording();
-  Q_INVOKABLE bool startRecording(const QString& fileName);
+
+  bool startRecording(const QString& fileName, const QString& tmpFileName = QString());
 
   bool setResolution(const QtCamVideoResolution& resolution);
 
index 3d79b0a..f88dcaa 100644 (file)
@@ -113,7 +113,8 @@ CameraPage {
                                         }
 
 
-                                        if (!videoMode.startRecording(fileNaming.videoFileName())) {
+                                        if (!videoMode.startRecording(fileNaming.videoFileName(),
+                                             fileNaming.temporaryVideoFileName())) {
                                                 showError(qsTr("Failed to record video. Please restart the camera."));
                                                 policyMode = CameraResources.Video
 }
index 8883028..19fa76d 100644 (file)
@@ -32,7 +32,6 @@ import QtMobility.location 1.2
 // TODO: flash not ready
 // TODO: focus, caf, ...
 // TODO: portrait/landscape
-// TODO: record video in a hidden directory and then copy the video to avoid tracker indexing it.
 // TODO: stop viewfinder in settings pages ?
 // TODO: grid lines, face tracking, ambr
 // TODO: complete settings pages
index 65331e6..6b5e03a 100644 (file)
@@ -25,6 +25,7 @@
 #include <QFile>
 
 #define PATH QString("%1%2MyDocs%2cameraplus%2").arg(QDir::homePath()).arg(QDir::separator())
+#define TEMP_PATH QString("%1%2MyDocs%2.cameraplus%2").arg(QDir::homePath()).arg(QDir::separator())
 
 FileNaming::FileNaming(QObject *parent) :
   QObject(parent) {
@@ -100,3 +101,12 @@ QString FileNaming::fileName(const QString& suffix) {
 QString FileNaming::path() const {
   return PATH;
 }
+
+QString FileNaming::temporaryVideoFileName() {
+  if (!QDir::root().mkpath(TEMP_PATH)) {
+    qWarning() << "Failed to create temporary path" << TEMP_PATH;
+    return QString();
+  }
+
+  return QString("%1.cameraplus_video.tmp").arg(TEMP_PATH);
+}
index 383efdc..06af84d 100644 (file)
@@ -44,6 +44,7 @@ public:
 
   Q_INVOKABLE QString imageFileName();
   Q_INVOKABLE QString videoFileName();
+  Q_INVOKABLE QString temporaryVideoFileName();
 
   QString path() const;