summaryrefslogtreecommitdiff
path: root/tests/examples/overlay/qtgv-xoverlay.cpp
blob: 69252864747bd55d286ee8975f2cf9f60f4e6823 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* GStreamer
 * Copyright (C) <2010> Alexander Bokovoy <ab@samba.org>
 *
 * qtgv-xoverlay: demonstrate overlay handling using qt graphics view
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "qtgv-xoverlay.h"

#include <QApplication>
#include <QTimer>

#include <gst/interfaces/xoverlay.h>

SinkPipeline::SinkPipeline(QGraphicsView *parent) : QObject(parent)
{
  GstStateChangeReturn sret;
  
  pipeline = gst_pipeline_new ("xvoverlay");
  src = gst_element_factory_make ("videotestsrc", NULL);

  if ((sink = gst_element_factory_make ("xvimagesink", NULL))) {
    sret = gst_element_set_state (sink, GST_STATE_READY);
    if (sret != GST_STATE_CHANGE_SUCCESS) {
      gst_element_set_state (sink, GST_STATE_NULL);
      gst_object_unref (sink);
  
      if ((sink = gst_element_factory_make ("ximagesink", NULL))) {
        sret = gst_element_set_state (sink, GST_STATE_READY);
        if (sret != GST_STATE_CHANGE_SUCCESS) {
          gst_element_set_state (sink, GST_STATE_NULL);
          gst_object_unref (sink);
    
          if (strcmp (DEFAULT_VIDEOSINK, "xvimagesink") != 0 &&
              strcmp (DEFAULT_VIDEOSINK, "ximagesink") != 0) {
    
            if ((sink = gst_element_factory_make (DEFAULT_VIDEOSINK, NULL))) {
              if (!GST_IS_BIN (sink)) {
                sret = gst_element_set_state (sink, GST_STATE_READY);
                if (sret != GST_STATE_CHANGE_SUCCESS) {
                  gst_element_set_state (sink, GST_STATE_NULL);
                  gst_object_unref (sink);
                  sink = NULL;
                }
              } else {
                gst_object_unref (sink);
                sink = NULL;
              }
            }
          }
        }
      }
    }
  }

  if (sink == NULL)
    g_error ("Couldn't find a working video sink.");

  gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
  gst_element_link (src, sink);
  xwinid = parent->winId();
}

SinkPipeline::~SinkPipeline()
{
  gst_element_set_state (pipeline, GST_STATE_NULL);
  gst_object_unref (pipeline);
}

void SinkPipeline::startPipeline()
{
  GstStateChangeReturn sret;

  /* we know what the video sink is in this case (xvimagesink), so we can
   * just set it directly here now (instead of waiting for a prepare-xwindow-id
   * element message in a sync bus handler and setting it there) */

  gst_x_overlay_set_xwindow_id (GST_X_OVERLAY (sink), xwinid);

  sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
  if (sret == GST_STATE_CHANGE_FAILURE) {
    gst_element_set_state (pipeline, GST_STATE_NULL);
    gst_object_unref (pipeline);
    /* Exit application */
    QTimer::singleShot(0, QApplication::activeWindow(), SLOT(quit()));
  }
}

int main( int argc, char **argv )
{
    QApplication app(argc, argv);

    QGraphicsScene scene;
    scene.setSceneRect( -100.0, -100.0, 200.0, 200.0 );

    QGraphicsView view( &scene );
    view.resize(320, 240);
    view.setWindowTitle("GstXOverlay Qt GraphicsView demo");
    view.show();

    gst_init (&argc, &argv);
    SinkPipeline pipeline(&view);
    pipeline.startPipeline();

    int ret = app.exec();

    view.hide();
    
    return ret;
}