Blame tests/examples/overlay/qtgv-videooverlay.cpp

Packit 971217
/* GStreamer
Packit 971217
 * Copyright (C) <2010> Alexander Bokovoy <ab@samba.org>
Packit 971217
 *
Packit 971217
 * qtgv-xoverlay: demonstrate overlay handling using qt graphics view
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
Packit 971217
#ifdef HAVE_CONFIG_H
Packit 971217
#include "config.h"
Packit 971217
#endif
Packit 971217
Packit 971217
#include "qtgv-videooverlay.h"
Packit 971217
Packit 971217
#include <QApplication>
Packit 971217
#include <QTimer>
Packit 971217
Packit 971217
#include <gst/video/videooverlay.h>
Packit 971217
Packit 971217
SinkPipeline::SinkPipeline(QGraphicsView *parent) : QObject(parent)
Packit 971217
{
Packit 971217
  GstStateChangeReturn sret;
Packit 971217
  
Packit 971217
  pipeline = gst_pipeline_new ("xvoverlay");
Packit 971217
  src = gst_element_factory_make ("videotestsrc", NULL);
Packit 971217
Packit 971217
  if ((sink = gst_element_factory_make ("xvimagesink", NULL))) {
Packit 971217
    sret = gst_element_set_state (sink, GST_STATE_READY);
Packit 971217
    if (sret != GST_STATE_CHANGE_SUCCESS) {
Packit 971217
      gst_element_set_state (sink, GST_STATE_NULL);
Packit 971217
      gst_object_unref (sink);
Packit 971217
    }
Packit 971217
  } else if ((sink = gst_element_factory_make ("ximagesink", NULL))) {
Packit 971217
    sret = gst_element_set_state (sink, GST_STATE_READY);
Packit 971217
    if (sret != GST_STATE_CHANGE_SUCCESS) {
Packit 971217
      gst_element_set_state (sink, GST_STATE_NULL);
Packit 971217
      gst_object_unref (sink);
Packit 971217
    }
Packit 971217
  } else if (strcmp (DEFAULT_VIDEOSINK, "xvimagesink") != 0 &&
Packit 971217
             strcmp (DEFAULT_VIDEOSINK, "ximagesink") != 0) {
Packit 971217
    if ((sink = gst_element_factory_make (DEFAULT_VIDEOSINK, NULL))) {
Packit 971217
      if (!GST_IS_BIN (sink)) {
Packit 971217
        sret = gst_element_set_state (sink, GST_STATE_READY);
Packit 971217
        if (sret != GST_STATE_CHANGE_SUCCESS) {
Packit 971217
          gst_element_set_state (sink, GST_STATE_NULL);
Packit 971217
          gst_object_unref (sink);
Packit 971217
          sink = NULL;
Packit 971217
        }
Packit 971217
      } else {
Packit 971217
        gst_object_unref (sink);
Packit 971217
        sink = NULL;
Packit 971217
      }
Packit 971217
    }
Packit 971217
  }
Packit 971217
Packit 971217
  if (sink == NULL)
Packit 971217
    g_error ("Couldn't find a working video sink.");
Packit 971217
Packit 971217
  gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
Packit 971217
  gst_element_link (src, sink);
Packit 971217
  xwinid = parent->winId();
Packit 971217
}
Packit 971217
Packit 971217
SinkPipeline::~SinkPipeline()
Packit 971217
{
Packit 971217
  gst_element_set_state (pipeline, GST_STATE_NULL);
Packit 971217
  gst_object_unref (pipeline);
Packit 971217
}
Packit 971217
Packit 971217
void SinkPipeline::startPipeline()
Packit 971217
{
Packit 971217
  GstStateChangeReturn sret;
Packit 971217
Packit 971217
  /* we know what the video sink is in this case (xvimagesink), so we can
Packit 971217
   * just set it directly here now (instead of waiting for a
Packit 971217
   * prepare-window-handle element message in a sync bus handler and setting
Packit 971217
   * it there) */
Packit 971217
  gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (sink), xwinid);
Packit 971217
Packit 971217
  sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
Packit 971217
  if (sret == GST_STATE_CHANGE_FAILURE) {
Packit 971217
    gst_element_set_state (pipeline, GST_STATE_NULL);
Packit 971217
    gst_object_unref (pipeline);
Packit 971217
    /* Exit application */
Packit 971217
    QTimer::singleShot(0, QApplication::activeWindow(), SLOT(quit()));
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
int main( int argc, char **argv )
Packit 971217
{
Packit 971217
    QApplication app(argc, argv);
Packit 971217
Packit 971217
    QGraphicsScene scene;
Packit 971217
    scene.setSceneRect( -100.0, -100.0, 200.0, 200.0 );
Packit 971217
Packit 971217
    QGraphicsView view( &scene );
Packit 971217
    view.resize(320, 240);
Packit 971217
    view.setWindowTitle("GstVideoOverlay Qt GraphicsView demo");
Packit 971217
    view.show();
Packit 971217
Packit 971217
    gst_init (&argc, &argv);
Packit 971217
    SinkPipeline pipeline(&view);
Packit 971217
    pipeline.startPipeline();
Packit 971217
Packit 971217
    int ret = app.exec();
Packit 971217
Packit 971217
    view.hide();
Packit 971217
    
Packit 971217
    return ret;
Packit 971217
}