main.cpp 12.2 KB
Newer Older
Matthew Waters's avatar
Matthew Waters committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * GStreamer
 * Copyright (C) 2008-2009 Julien Isorce <julien.isorce@gmail.com>
 *
 * 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., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <gst/gst.h>
22
#include <gst/gl/gl.h>
Matthew Waters's avatar
Matthew Waters committed
23 24 25 26 27 28 29 30 31

#include <iostream>
#include <sstream>
#include <string>

static gboolean bus_call (GstBus *bus, GstMessage *msg, gpointer data)
{
    GMainLoop *loop = (GMainLoop*)data;

32
    switch (GST_MESSAGE_TYPE (msg))
Matthew Waters's avatar
Matthew Waters committed
33 34 35 36 37
    {
        case GST_MESSAGE_EOS:
              g_print ("End-of-stream\n");
              g_main_loop_quit (loop);
              break;
38
        case GST_MESSAGE_ERROR:
Matthew Waters's avatar
Matthew Waters committed
39 40 41 42 43 44 45 46 47
          {
              gchar *debug = NULL;
              GError *err = NULL;

              gst_message_parse_error (msg, &err, &debug);

              g_print ("Error: %s\n", err->message);
              g_error_free (err);

48
              if (debug)
Matthew Waters's avatar
Matthew Waters committed
49 50 51 52 53 54 55
              {
                  g_print ("Debug details: %s\n", debug);
                  g_free (debug);
              }

              g_main_loop_quit (loop);
              break;
56
          }
Matthew Waters's avatar
Matthew Waters committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70
        default:
          break;
    }

    return TRUE;
}


//display video framerate
static GstPadProbeReturn textoverlay_sink_pad_probe_cb (GstPad *pad, GstPadProbeInfo *info, GstElement* textoverlay)
{
  static GstClockTime last_timestamp = 0;
  static gint nbFrames = 0 ;

71
  //display estimated video FPS
Matthew Waters's avatar
Matthew Waters committed
72 73 74
  nbFrames++ ;
  if (GST_BUFFER_TIMESTAMP(info->data) - last_timestamp >= 1000000000)
  {
75
    std::ostringstream oss;
Matthew Waters's avatar
Matthew Waters committed
76
    oss << "video framerate = " << nbFrames ;
77
    std::string s(oss.str());
Matthew Waters's avatar
Matthew Waters committed
78 79
    g_object_set(G_OBJECT(textoverlay), "text", s.c_str(), NULL);
    last_timestamp = GST_BUFFER_TIMESTAMP(info->data) ;
80
    nbFrames = 0;
Matthew Waters's avatar
Matthew Waters committed
81 82 83 84 85 86 87
  }

  return GST_PAD_PROBE_OK;
}


//client reshape callback
88
static gboolean reshapeCallback (void *gl_sink, void *context, GLuint width, GLuint height)
Matthew Waters's avatar
Matthew Waters committed
89 90 91 92
{
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
93
    glMatrixMode(GL_MODELVIEW);
94 95

    return TRUE;
Matthew Waters's avatar
Matthew Waters committed
96 97 98 99
}


//client draw callback
100
static gboolean drawCallback (GstElement * gl_sink, GstGLContext *context, GstSample * sample, gpointer data)
Matthew Waters's avatar
Matthew Waters committed
101 102
{
    static GLfloat	xrot = 0;
103
    static GLfloat	yrot = 0;
Matthew Waters's avatar
Matthew Waters committed
104 105 106
    static GLfloat	zrot = 0;
    static GTimeVal current_time;
    static glong last_sec = current_time.tv_sec;
107
    static gint nbFrames = 0;
Matthew Waters's avatar
Matthew Waters committed
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    GstVideoFrame v_frame;
    GstVideoInfo v_info;
    guint texture = 0;
    GstBuffer *buf = gst_sample_get_buffer (sample);
    GstCaps *caps = gst_sample_get_caps (sample);

    gst_video_info_from_caps (&v_info, caps);

    if (!gst_video_frame_map (&v_frame, &v_info, buf, (GstMapFlags) (GST_MAP_READ | GST_MAP_GL))) {
      g_warning ("Failed to map the video buffer");
      return TRUE;
    }

    texture = *(guint *) v_frame.data[0];

Matthew Waters's avatar
Matthew Waters committed
124 125
    g_get_current_time (&current_time);
    nbFrames++ ;
126

Matthew Waters's avatar
Matthew Waters committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    if ((current_time.tv_sec - last_sec) >= 1)
    {
        std::cout << "GRAPHIC FPS of the scene which contains the custom cube) = " << nbFrames << std::endl;
        nbFrames = 0;
        last_sec = current_time.tv_sec;
    }

    glEnable(GL_DEPTH_TEST);

    glEnable (GL_TEXTURE_2D);
    glBindTexture (GL_TEXTURE_2D, texture);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
147

Matthew Waters's avatar
Matthew Waters committed
148 149 150 151 152 153 154 155
    glTranslatef(0.0f,0.0f,-5.0f);

    glRotatef(xrot,1.0f,0.0f,0.0f);
    glRotatef(yrot,0.0f,1.0f,0.0f);
    glRotatef(zrot,0.0f,0.0f,1.0f);

    glBegin(GL_QUADS);
	      // Front Face
156
	      glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
Matthew Waters's avatar
Matthew Waters committed
157
	      glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
158 159
	      glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
	      glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
Matthew Waters's avatar
Matthew Waters committed
160 161
	      // Back Face
	      glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
162 163 164
	      glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
	      glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
	      glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
Matthew Waters's avatar
Matthew Waters committed
165
	      // Top Face
166 167
	      glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
	      glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
Matthew Waters's avatar
Matthew Waters committed
168
	      glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
169
	      glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
Matthew Waters's avatar
Matthew Waters committed
170
	      // Bottom Face
171
	      glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
Matthew Waters's avatar
Matthew Waters committed
172
	      glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
173 174
	      glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
	      glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
Matthew Waters's avatar
Matthew Waters committed
175 176
	      // Right face
	      glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
177 178 179
	      glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
	      glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
	      glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
Matthew Waters's avatar
Matthew Waters committed
180
	      // Left Face
181
	      glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
Matthew Waters's avatar
Matthew Waters committed
182
	      glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
183 184
	      glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
	      glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
Matthew Waters's avatar
Matthew Waters committed
185 186
    glEnd();

187 188
    gst_video_frame_unmap (&v_frame);

Matthew Waters's avatar
Matthew Waters committed
189 190 191 192 193 194 195 196 197 198 199
    xrot+=0.03f;
    yrot+=0.02f;
    zrot+=0.04f;

    return TRUE;
}


static void cb_new_pad (GstElement* decodebin, GstPad* pad, GstElement* element)
{
    GstPad* element_pad = gst_element_get_static_pad (element, "sink");
200 201 202

    //only link once
    if (!element_pad || GST_PAD_IS_LINKED (element_pad))
Matthew Waters's avatar
Matthew Waters committed
203 204 205 206 207 208 209
    {
        gst_object_unref (element_pad);
        return;
    }

    GstCaps* caps = gst_pad_get_current_caps (pad);
    GstStructure* str = gst_caps_get_structure (caps, 0);
210

Matthew Waters's avatar
Matthew Waters committed
211 212
    GstCaps* caps2 = gst_pad_query_caps (element_pad, NULL);
    gst_caps_unref (caps2);
213 214

    if (!g_strrstr (gst_structure_get_name (str), "video"))
Matthew Waters's avatar
Matthew Waters committed
215 216 217 218 219 220 221 222
    {
        gst_caps_unref (caps);
        gst_object_unref (element_pad);
        return;
    }
    gst_caps_unref (caps);

    GstPadLinkReturn ret = gst_pad_link (pad, element_pad);
223
    if (ret != GST_PAD_LINK_OK)
Matthew Waters's avatar
Matthew Waters committed
224 225 226 227 228 229 230 231 232 233 234 235
        g_warning ("Failed to link with decodebin %d!\n", ret);
    gst_object_unref (element_pad);
}


gint main (gint argc, gchar *argv[])
{
    if (argc != 2)
    {
        g_warning ("usage: doublecube.exe videolocation\n");
        return -1;
    }
236

Matthew Waters's avatar
Matthew Waters committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    std::string video_location(argv[1]);

    /* initialization */
    gst_init (&argc, &argv);
    GMainLoop* loop = g_main_loop_new (NULL, FALSE);

    /* create elements */
    GstElement* pipeline = gst_pipeline_new ("pipeline");

    /* watch for messages on the pipeline's bus (note that this will only
     * work like this when a GLib main loop is running) */
    GstBus* bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
    gst_bus_add_watch (bus, bus_call, loop);
    gst_object_unref (bus);

    /* create elements */
    GstElement* videosrc = gst_element_factory_make ("filesrc", "filesrc0");
    GstElement* decodebin = gst_element_factory_make ("decodebin", "decodebin0");
    GstElement* videoconvert = gst_element_factory_make ("videoscale", "videoconvert0");
    GstElement* textoverlay = gst_element_factory_make ("textoverlay", "textoverlay0"); //textoverlay required I420
    GstElement* tee = gst_element_factory_make ("tee", "tee0");

    GstElement* queue0 = gst_element_factory_make ("queue", "queue0");
    GstElement* glimagesink0  = gst_element_factory_make ("glimagesink", "glimagesink0");

    GstElement* queue1 = gst_element_factory_make ("queue", "queue1");
    GstElement* glfiltercube  = gst_element_factory_make ("glfiltercube", "glfiltercube");
    GstElement* glimagesink1  = gst_element_factory_make ("glimagesink", "glimagesink1");

    GstElement* queue2 = gst_element_factory_make ("queue", "queue2");
    GstElement* glimagesink2  = gst_element_factory_make ("glimagesink", "glimagesink2");


    if (!videosrc || !decodebin || !videoconvert || !textoverlay || !tee ||
        !queue0 || !glimagesink0 ||
        !queue1 || !glfiltercube || !glimagesink1 ||
273
        !queue2 || !glimagesink2)
Matthew Waters's avatar
Matthew Waters committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287
    {
        g_warning ("one element could not be found \n");
        return -1;
    }

    GstCaps* cubecaps = gst_caps_new_simple("video/x-raw",
                                            "width", G_TYPE_INT, 600,
                                            "height", G_TYPE_INT, 400,
                                            NULL);

    /* configure elements */
    g_object_set(G_OBJECT(videosrc), "num-buffers", 1000, NULL);
    g_object_set(G_OBJECT(videosrc), "location", video_location.c_str(), NULL);
    g_object_set(G_OBJECT(textoverlay), "font_desc", "Ahafoni CLM Bold 30", NULL);
288 289
    g_signal_connect(G_OBJECT(glimagesink0), "client-reshape", G_CALLBACK (reshapeCallback), NULL);
    g_signal_connect(G_OBJECT(glimagesink0), "client-draw", G_CALLBACK (drawCallback), NULL);
290

Matthew Waters's avatar
Matthew Waters committed
291 292 293
    /* add elements */
    gst_bin_add_many (GST_BIN (pipeline), videosrc, decodebin, videoconvert, textoverlay, tee, 
                                          queue0, glimagesink0,
294
                                          queue1, glfiltercube, glimagesink1,
Matthew Waters's avatar
Matthew Waters committed
295
                                          queue2, glimagesink2, NULL);
296

Matthew Waters's avatar
Matthew Waters committed
297
    GstPad* textoverlay_sink_pad = gst_element_get_static_pad (textoverlay, "video_sink");
298
    gst_pad_add_probe (textoverlay_sink_pad, GST_PAD_PROBE_TYPE_BUFFER,
Matthew Waters's avatar
Matthew Waters committed
299 300 301
                       (GstPadProbeCallback) textoverlay_sink_pad_probe_cb, (gpointer)textoverlay, NULL);
    gst_object_unref (textoverlay_sink_pad);

302
    if (!gst_element_link_many(videoconvert, textoverlay, tee, NULL))
Matthew Waters's avatar
Matthew Waters committed
303 304 305 306 307
    {
        g_print ("Failed to link videoconvert to tee!\n");
        return -1;
    }

308
    if (!gst_element_link(videosrc, decodebin))
Matthew Waters's avatar
Matthew Waters committed
309 310 311 312 313 314
    {
        g_print ("Failed to link videosrc to decodebin!\n");
        return -1;
    }

    g_signal_connect (decodebin, "pad-added", G_CALLBACK (cb_new_pad), videoconvert);
315 316

    if (!gst_element_link_many(tee, queue0, NULL))
Matthew Waters's avatar
Matthew Waters committed
317 318 319 320 321 322 323 324 325 326 327 328 329
    {
        g_warning ("Failed to link one or more elements bettween tee and queue0!\n");
        return -1;
    }

    gboolean link_ok = gst_element_link_filtered(queue0, glimagesink0, cubecaps) ;
    gst_caps_unref(cubecaps) ;
    if(!link_ok)
    {
        g_warning("Failed to link queue0 to glimagesink0!\n") ;
        return -1 ;
    }

330
    if (!gst_element_link_many(tee, queue1, glfiltercube, glimagesink1, NULL))
Matthew Waters's avatar
Matthew Waters committed
331 332 333 334 335
    {
        g_warning ("Failed to link one or more elements bettween tee and glimagesink1!\n");
        return -1;
    }

336
    if (!gst_element_link_many(tee, queue2, glimagesink2, NULL))
Matthew Waters's avatar
Matthew Waters committed
337 338 339 340
    {
        g_warning ("Failed to link one or more elements bettween tee and glimagesink2!\n");
        return -1;
    }
341

Matthew Waters's avatar
Matthew Waters committed
342 343
    /* run */
    GstStateChangeReturn ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
344
    if (ret == GST_STATE_CHANGE_FAILURE)
Matthew Waters's avatar
Matthew Waters committed
345 346 347 348 349
    {
        g_print ("Failed to start up pipeline!\n");

        /* check if there is an error message with details on the bus */
        GstMessage* msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
350
        if (msg)
Matthew Waters's avatar
Matthew Waters committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
        {
          GError *err = NULL;

          gst_message_parse_error (msg, &err, NULL);
          g_print ("ERROR: %s\n", err->message);
          g_error_free (err);
          gst_message_unref (msg);
        }
        return -1;
    }

    g_main_loop_run (loop);

    /* clean up */
    gst_element_set_state (pipeline, GST_STATE_NULL);
    gst_object_unref (pipeline);

    return 0;
}