Added QtCamAutoFocus::setRegionOfInterest()
authorMohammed Sameer <msameer@foolab.org>
Wed, 19 Dec 2012 19:45:39 +0000 (21:45 +0200)
committerMohammed Sameer <msameer@foolab.org>
Wed, 19 Dec 2012 19:45:39 +0000 (21:45 +0200)
13 files changed:
lib/qtcamautofocus.cpp
lib/qtcamautofocus.h
lib/qtcamautofocus_p.h
lib/qtcamdevice.cpp
lib/qtcamdevice.h
lib/qtcamgraphicsviewfinder.cpp
lib/qtcamgraphicsviewfinder.h
lib/qtcamviewfinder.h
lib/qtcamviewfinderrenderer.h
lib/qtcamviewfinderrenderergeneric.cpp
lib/qtcamviewfinderrenderergeneric.h
lib/qtcamviewfinderrenderermeego.cpp
lib/qtcamviewfinderrenderermeego.h

index 5dd6354..96a4073 100644 (file)
 
 #include "qtcamautofocus.h"
 #include "qtcamautofocus_p.h"
+#include <QRectF>
+
+#ifndef G_VALUE_INIT
+#define G_VALUE_INIT  { 0, { { 0 } } }
+#endif /* G_VALUE_INIT */
 
 QtCamAutoFocus::QtCamAutoFocus(QtCamDevice *dev, QObject *parent) :
   QObject(parent),
@@ -54,3 +59,57 @@ bool QtCamAutoFocus::canFocus(const QtCamScene::SceneMode& mode) {
 
   return true;
 }
+
+void QtCamAutoFocus::setRegionOfInterest(const QRectF& roi) {
+  if (!d_ptr->dev || !d_ptr->dev->viewfinder()) {
+    return;
+  }
+
+  QSizeF vf = d_ptr->dev->viewfinder()->videoResolution();
+  if (vf.isEmpty()) {
+    return;
+  }
+
+  int frameWidth = vf.width();
+  int frameHeight = vf.height();
+  int x = roi.x() * frameWidth;
+  int y = roi.y() * frameHeight;
+  int width = roi.width() * frameWidth;
+  int height = roi.height() * frameHeight;
+
+  // if we have an empty roi then we reset:
+  int priority = roi.isEmpty() ? 0 : 1;
+
+  GstStructure *region = gst_structure_new("region0",
+                                          "region-x", G_TYPE_UINT, x,
+                                          "region-y", G_TYPE_UINT, y,
+                                          "region-w", G_TYPE_UINT, width,
+                                          "region-h", G_TYPE_UINT, height,
+                                          "region-priority", G_TYPE_UINT, priority,
+                                          "region-id", G_TYPE_UINT, 0,
+                                          NULL);
+
+  GValue regionValue = G_VALUE_INIT;
+  GValue regionList = G_VALUE_INIT;
+
+  g_value_init(&regionValue, GST_TYPE_STRUCTURE);
+  g_value_init(&regionList, GST_TYPE_LIST);
+
+  gst_value_set_structure(&regionValue, region);
+  gst_value_list_append_value(&regionList, &regionValue);
+
+  GstStructure *s = gst_structure_new("regions-of-interest",
+                                     "frame-width", G_TYPE_UINT, frameWidth,
+                                     "frame-height", G_TYPE_UINT, frameHeight,
+                                     NULL);
+  gst_structure_set_value(s, "regions", &regionList);
+
+  GstEvent *event = gst_event_new_custom(GST_EVENT_CUSTOM_UPSTREAM, s);
+  gst_structure_free(region);
+  g_value_unset(&regionValue);
+  g_value_unset(&regionList);
+
+  if (!d_ptr->sendEventToSource(event)) {
+    qWarning() << "Failed to send ROI event";
+  }
+}
index 11c5759..7459508 100644 (file)
@@ -28,6 +28,7 @@
 
 class QtCamDevice;
 class QtCamAutoFocusPrivate;
+class QRectF;
 
 class QtCamAutoFocus : public QObject {
   Q_OBJECT
@@ -56,6 +57,8 @@ public:
 
   bool canFocus(const QtCamScene::SceneMode& mode);
 
+  void setRegionOfInterest(const QRectF& roi);
+
 signals:
   void statusChanged();
   void cafStatusChanged();
index 64aabb5..a6b2482 100644 (file)
@@ -113,6 +113,16 @@ public:
     return false;
   }
 
+  bool sendEventToSource(GstEvent *event) {
+    if (!dev->d_ptr->videoSource) {
+      qWarning() << "No video source";
+      gst_event_unref(event);
+      return false;
+    }
+
+    return gst_element_send_event(dev->d_ptr->videoSource, event) == TRUE ? true : false;
+  }
+
 public slots:
   void handleMessage(GstMessage *message) {
     if (setStatus(&status, message)) {
index a492287..c9cce2b 100644 (file)
@@ -128,6 +128,10 @@ bool QtCamDevice::setViewfinder(QtCamViewfinder *viewfinder) {
   return true;
 }
 
+QtCamViewfinder *QtCamDevice::viewfinder() const {
+  return d_ptr->viewfinder;
+}
+
 bool QtCamDevice::start() {
   if (d_ptr->error) {
     qWarning() << "Pipeline must be stopped first because of an error.";
index 692c401..34f72e8 100644 (file)
@@ -45,6 +45,8 @@ public:
   ~QtCamDevice();
 
   bool setViewfinder(QtCamViewfinder *viewfinder);
+  QtCamViewfinder *viewfinder() const;
+
   bool start();
   bool stop(bool force);
   bool isRunning();
index 2c9b82f..0cd418e 100644 (file)
@@ -39,6 +39,7 @@ public:
       renderer->resize(q_ptr->size());
       QObject::connect(renderer, SIGNAL(updateRequested()), q_ptr, SLOT(updateRequested()));
       QObject::connect(renderer, SIGNAL(renderAreaChanged()), q_ptr, SIGNAL(renderAreaChanged()));
+      QObject::connect(renderer, SIGNAL(videoResolutionChanged()), q_ptr, SIGNAL(videoResolutionChanged()));
     }
   }
 
@@ -143,3 +144,11 @@ QRectF QtCamGraphicsViewfinder::renderArea() const {
 
   return d_ptr->renderer->renderArea();
 }
+
+QSizeF QtCamGraphicsViewfinder::videoResolution() const {
+  if (!d_ptr->renderer) {
+    return QSizeF();
+  }
+
+  return d_ptr->renderer->videoResolution();
+}
index 25edab1..e9db5b8 100644 (file)
@@ -44,9 +44,11 @@ public:
                     QWidget *widget = 0);
 
   QRectF renderArea() const;
+  QSizeF videoResolution() const;
 
 signals:
   void renderAreaChanged();
+  void videoResolutionChanged();
 
 protected:
   void resizeEvent(QGraphicsSceneResizeEvent *event);
index d534a56..642e6cb 100644 (file)
@@ -24,6 +24,7 @@
 #define QT_CAM_VIEWFINDER_H
 
 #include <gst/gst.h>
+#include <QRectF>
 
 class QtCamDevice;
 
@@ -35,6 +36,9 @@ public:
   virtual GstElement *sinkElement() = 0;
   virtual bool setDevice(QtCamDevice *device) = 0;
   virtual void stop() = 0;
+
+  virtual QRectF renderArea() const = 0;
+  virtual QSizeF videoResolution() const = 0;
 };
 
 #endif /* QT_CAM_VIEWFINDER_H */
index e7b7b6b..6937ede 100644 (file)
@@ -47,6 +47,7 @@ public:
   virtual GstElement *sinkElement() = 0;
 
   virtual QRectF renderArea() = 0;
+  virtual QSizeF videoResolution() = 0;
 
 protected:
   QtCamViewfinderRenderer(QtCamConfig *config, QObject *parent = 0);
@@ -54,6 +55,7 @@ protected:
 signals:
   void updateRequested();
   void renderAreaChanged();
+  void videoResolutionChanged();
 };
 
 #define QT_CAM_VIEWFINDER_RENDERER(key, klass) \
index 8b85000..7afc42d 100644 (file)
@@ -131,6 +131,12 @@ QRectF QtCamViewfinderRendererGeneric::renderArea() {
   return QRectF();
 }
 
+QSizeF QtCamViewfinderRendererGeneric::videoResolution() {
+  // TODO:
+
+  return QSizeF();
+}
+
 void QtCamViewfinderRendererGeneric::on_gst_buffer(GstElement *element,
                                                   GstBuffer *buf, GstPad *pad,
                                                   QtCamViewfinderRendererGeneric *q) {
index f16f07c..5d51fe5 100644 (file)
@@ -40,6 +40,7 @@ public:
   virtual void reset();
   virtual GstElement *sinkElement();
   QRectF renderArea();
+  QSizeF videoResolution();
 
 private:
   static void on_gst_buffer(GstElement *element, GstBuffer *buf, GstPad *pad,
index 87bcc9d..209b211 100644 (file)
@@ -200,7 +200,7 @@ void QtCamViewfinderRendererMeeGo::sink_caps_changed(GObject *obj, GParamSpec *p
   Q_UNUSED(pspec);
 
   int width, height;
-  if (obj && gst_video_get_size  (GST_PAD(obj), &width, &height)) {
+  if (obj && gst_video_get_size(GST_PAD(obj), &width, &height)) {
     QMetaObject::invokeMethod(q, "setVideoSize", Qt::QueuedConnection,
                              Q_ARG(QSizeF, QSizeF(width, height)));
   }
@@ -331,6 +331,10 @@ QRectF QtCamViewfinderRendererMeeGo::renderArea() {
   return m_renderArea;
 }
 
+QSizeF QtCamViewfinderRendererMeeGo::videoResolution() {
+  return m_videoSize;
+}
+
 void QtCamViewfinderRendererMeeGo::setVideoSize(const QSizeF& size) {
   if (size == m_videoSize) {
     return;
@@ -345,4 +349,5 @@ void QtCamViewfinderRendererMeeGo::setVideoSize(const QSizeF& size) {
   m_needsInit = true;
 
   emit renderAreaChanged();
+  emit videoResolution();
 }
index a48d850..ca0ab7e 100644 (file)
@@ -45,6 +45,7 @@ public:
   virtual GstElement *sinkElement();
 
   QRectF renderArea();
+  QSizeF videoResolution();
 
 private slots:
   void setVideoSize(const QSizeF& size);