Blame tests/examples/gl/generic/doublecube/main.cpp

Packit 971217
/*
Packit 971217
 * GStreamer
Packit 971217
 * Copyright (C) 2008-2009 Julien Isorce <julien.isorce@gmail.com>
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
#include <gst/gst.h>
Packit 971217
#include <gst/gl/gl.h>
Packit 971217
#include <gst/gl/gstglfuncs.h>
Packit 971217
Packit 971217
#include <iostream>
Packit 971217
#include <sstream>
Packit 971217
#include <string>
Packit 971217
Packit 971217
static gboolean bus_call (GstBus *bus, GstMessage *msg, gpointer data)
Packit 971217
{
Packit 971217
    GMainLoop *loop = (GMainLoop*)data;
Packit 971217
Packit 971217
    switch (GST_MESSAGE_TYPE (msg))
Packit 971217
    {
Packit 971217
        case GST_MESSAGE_EOS:
Packit 971217
              g_print ("End-of-stream\n");
Packit 971217
              g_main_loop_quit (loop);
Packit 971217
              break;
Packit 971217
        case GST_MESSAGE_ERROR:
Packit 971217
          {
Packit 971217
              gchar *debug = NULL;
Packit 971217
              GError *err = NULL;
Packit 971217
Packit 971217
              gst_message_parse_error (msg, &err, &debug);
Packit 971217
Packit 971217
              g_print ("Error: %s\n", err->message);
Packit 971217
              g_error_free (err);
Packit 971217
Packit 971217
              if (debug)
Packit 971217
              {
Packit 971217
                  g_print ("Debug details: %s\n", debug);
Packit 971217
                  g_free (debug);
Packit 971217
              }
Packit 971217
Packit 971217
              g_main_loop_quit (loop);
Packit 971217
              break;
Packit 971217
          }
Packit 971217
        default:
Packit 971217
          break;
Packit 971217
    }
Packit 971217
Packit 971217
    return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
Packit 971217
//display video framerate
Packit 971217
static GstPadProbeReturn textoverlay_sink_pad_probe_cb (GstPad *pad, GstPadProbeInfo *info, GstElement* textoverlay)
Packit 971217
{
Packit 971217
  static GstClockTime last_timestamp = 0;
Packit 971217
  static gint nbFrames = 0 ;
Packit 971217
Packit 971217
  //display estimated video FPS
Packit 971217
  nbFrames++ ;
Packit 971217
  if (GST_BUFFER_TIMESTAMP(info->data) - last_timestamp >= 1000000000)
Packit 971217
  {
Packit 971217
    gchar *s = g_strdup_printf ("video framerate = %d", nbFrames);
Packit 971217
    g_object_set(G_OBJECT(textoverlay), "text", s, NULL);
Packit 971217
    g_free (s);
Packit 971217
    last_timestamp = GST_BUFFER_TIMESTAMP(info->data) ;
Packit 971217
    nbFrames = 0;
Packit 971217
  }
Packit 971217
Packit 971217
  return GST_PAD_PROBE_OK;
Packit 971217
}
Packit 971217
Packit 971217
Packit 971217
//client reshape callback
Packit 971217
static gboolean reshapeCallback (void *gl_sink, void *context, GLuint width, GLuint height)
Packit 971217
{
Packit 971217
    glViewport(0, 0, width, height);
Packit 971217
    glMatrixMode(GL_PROJECTION);
Packit 971217
    glLoadIdentity();
Packit 971217
    glMatrixMode(GL_MODELVIEW);
Packit 971217
Packit 971217
    return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
Packit 971217
//client draw callback
Packit 971217
static gboolean drawCallback (GstElement * gl_sink, GstGLContext *context, GstSample * sample, gpointer data)
Packit 971217
{
Packit 971217
    static GLfloat	xrot = 0;
Packit 971217
    static GLfloat	yrot = 0;
Packit 971217
    static GLfloat	zrot = 0;
Packit 971217
    static GTimeVal current_time;
Packit 971217
    static glong last_sec = current_time.tv_sec;
Packit 971217
    static gint nbFrames = 0;
Packit 971217
Packit 971217
    GstVideoFrame v_frame;
Packit 971217
    GstVideoInfo v_info;
Packit 971217
    guint texture = 0;
Packit 971217
    GstBuffer *buf = gst_sample_get_buffer (sample);
Packit 971217
    GstCaps *caps = gst_sample_get_caps (sample);
Packit 971217
Packit 971217
    gst_video_info_from_caps (&v_info, caps);
Packit 971217
Packit 971217
    if (!gst_video_frame_map (&v_frame, &v_info, buf, (GstMapFlags) (GST_MAP_READ | GST_MAP_GL))) {
Packit 971217
      g_warning ("Failed to map the video buffer");
Packit 971217
      return TRUE;
Packit 971217
    }
Packit 971217
Packit 971217
    texture = *(guint *) v_frame.data[0];
Packit 971217
Packit 971217
    g_get_current_time (&current_time);
Packit 971217
    nbFrames++ ;
Packit 971217
Packit 971217
    if ((current_time.tv_sec - last_sec) >= 1)
Packit 971217
    {
Packit 971217
        std::cout << "GRAPHIC FPS of the scene which contains the custom cube) = " << nbFrames << std::endl;
Packit 971217
        nbFrames = 0;
Packit 971217
        last_sec = current_time.tv_sec;
Packit 971217
    }
Packit 971217
Packit 971217
    glEnable(GL_DEPTH_TEST);
Packit 971217
Packit 971217
    glEnable (GL_TEXTURE_2D);
Packit 971217
    glBindTexture (GL_TEXTURE_2D, texture);
Packit 971217
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Packit 971217
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Packit 971217
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
Packit 971217
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Packit 971217
    glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Packit 971217
Packit 971217
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Packit 971217
    glMatrixMode(GL_MODELVIEW);
Packit 971217
    glLoadIdentity();
Packit 971217
Packit 971217
    glTranslatef(0.0f,0.0f,-5.0f);
Packit 971217
Packit 971217
    glRotatef(xrot,1.0f,0.0f,0.0f);
Packit 971217
    glRotatef(yrot,0.0f,1.0f,0.0f);
Packit 971217
    glRotatef(zrot,0.0f,0.0f,1.0f);
Packit 971217
Packit 971217
    glBegin(GL_QUADS);
Packit 971217
	      // Front Face
Packit 971217
	      glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
Packit 971217
	      glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
Packit 971217
	      glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
Packit 971217
	      glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
Packit 971217
	      // Back Face
Packit 971217
	      glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
Packit 971217
	      glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
Packit 971217
	      glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
Packit 971217
	      glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
Packit 971217
	      // Top Face
Packit 971217
	      glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
Packit 971217
	      glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
Packit 971217
	      glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
Packit 971217
	      glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
Packit 971217
	      // Bottom Face
Packit 971217
	      glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
Packit 971217
	      glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
Packit 971217
	      glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
Packit 971217
	      glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
Packit 971217
	      // Right face
Packit 971217
	      glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
Packit 971217
	      glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
Packit 971217
	      glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
Packit 971217
	      glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
Packit 971217
	      // Left Face
Packit 971217
	      glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
Packit 971217
	      glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
Packit 971217
	      glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
Packit 971217
	      glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
Packit 971217
    glEnd();
Packit 971217
Packit 971217
    gst_video_frame_unmap (&v_frame);
Packit 971217
Packit 971217
    xrot+=0.03f;
Packit 971217
    yrot+=0.02f;
Packit 971217
    zrot+=0.04f;
Packit 971217
Packit 971217
    return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
Packit 971217
static void cb_new_pad (GstElement* decodebin, GstPad* pad, GstElement* element)
Packit 971217
{
Packit 971217
    GstPad* element_pad = gst_element_get_static_pad (element, "sink");
Packit 971217
Packit 971217
    //only link once
Packit 971217
    if (!element_pad || GST_PAD_IS_LINKED (element_pad))
Packit 971217
    {
Packit 971217
        gst_object_unref (element_pad);
Packit 971217
        return;
Packit 971217
    }
Packit 971217
Packit 971217
    GstCaps* caps = gst_pad_get_current_caps (pad);
Packit 971217
    GstStructure* str = gst_caps_get_structure (caps, 0);
Packit 971217
Packit 971217
    GstCaps* caps2 = gst_pad_query_caps (element_pad, NULL);
Packit 971217
    gst_caps_unref (caps2);
Packit 971217
Packit 971217
    if (!g_strrstr (gst_structure_get_name (str), "video"))
Packit 971217
    {
Packit 971217
        gst_caps_unref (caps);
Packit 971217
        gst_object_unref (element_pad);
Packit 971217
        return;
Packit 971217
    }
Packit 971217
    gst_caps_unref (caps);
Packit 971217
Packit 971217
    GstPadLinkReturn ret = gst_pad_link (pad, element_pad);
Packit 971217
    if (ret != GST_PAD_LINK_OK)
Packit 971217
        g_warning ("Failed to link with decodebin %d!\n", ret);
Packit 971217
    gst_object_unref (element_pad);
Packit 971217
}
Packit 971217
Packit 971217
Packit 971217
gint main (gint argc, gchar *argv[])
Packit 971217
{
Packit 971217
    if (argc != 2)
Packit 971217
    {
Packit 971217
        g_warning ("usage: doublecube.exe videolocation\n");
Packit 971217
        return -1;
Packit 971217
    }
Packit 971217
Packit 971217
    std::string video_location(argv[1]);
Packit 971217
Packit 971217
    /* initialization */
Packit 971217
    gst_init (&argc, &argv);
Packit 971217
    GMainLoop* loop = g_main_loop_new (NULL, FALSE);
Packit 971217
Packit 971217
    /* create elements */
Packit 971217
    GstElement* pipeline = gst_pipeline_new ("pipeline");
Packit 971217
Packit 971217
    /* watch for messages on the pipeline's bus (note that this will only
Packit 971217
     * work like this when a GLib main loop is running) */
Packit 971217
    GstBus* bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
Packit 971217
    gst_bus_add_watch (bus, bus_call, loop);
Packit 971217
    gst_object_unref (bus);
Packit 971217
Packit 971217
    /* create elements */
Packit 971217
    GstElement* videosrc = gst_element_factory_make ("filesrc", "filesrc0");
Packit 971217
    GstElement* decodebin = gst_element_factory_make ("decodebin", "decodebin0");
Packit 971217
    GstElement* videoconvert = gst_element_factory_make ("videoscale", "videoconvert0");
Packit 971217
    GstElement* textoverlay = gst_element_factory_make ("textoverlay", "textoverlay0"); //textoverlay required I420
Packit 971217
    GstElement* tee = gst_element_factory_make ("tee", "tee0");
Packit 971217
Packit 971217
    GstElement* queue0 = gst_element_factory_make ("queue", "queue0");
Packit 971217
    GstElement* glimagesink0  = gst_element_factory_make ("glimagesink", "glimagesink0");
Packit 971217
Packit 971217
    GstElement* queue1 = gst_element_factory_make ("queue", "queue1");
Packit 971217
    GstElement* glfiltercube  = gst_element_factory_make ("glfiltercube", "glfiltercube");
Packit 971217
    GstElement* glimagesink1  = gst_element_factory_make ("glimagesink", "glimagesink1");
Packit 971217
Packit 971217
    GstElement* queue2 = gst_element_factory_make ("queue", "queue2");
Packit 971217
    GstElement* glimagesink2  = gst_element_factory_make ("glimagesink", "glimagesink2");
Packit 971217
Packit 971217
Packit 971217
    if (!videosrc || !decodebin || !videoconvert || !textoverlay || !tee ||
Packit 971217
        !queue0 || !glimagesink0 ||
Packit 971217
        !queue1 || !glfiltercube || !glimagesink1 ||
Packit 971217
        !queue2 || !glimagesink2)
Packit 971217
    {
Packit 971217
        g_warning ("one element could not be found \n");
Packit 971217
        return -1;
Packit 971217
    }
Packit 971217
Packit 971217
    GstCaps* cubecaps = gst_caps_new_simple("video/x-raw",
Packit 971217
                                            "width", G_TYPE_INT, 600,
Packit 971217
                                            "height", G_TYPE_INT, 400,
Packit 971217
                                            NULL);
Packit 971217
Packit 971217
    /* configure elements */
Packit 971217
    g_object_set(G_OBJECT(videosrc), "num-buffers", 1000, NULL);
Packit 971217
    g_object_set(G_OBJECT(videosrc), "location", video_location.c_str(), NULL);
Packit 971217
    g_object_set(G_OBJECT(textoverlay), "font_desc", "Ahafoni CLM Bold 30", NULL);
Packit 971217
    g_signal_connect(G_OBJECT(glimagesink0), "client-reshape", G_CALLBACK (reshapeCallback), NULL);
Packit 971217
    g_signal_connect(G_OBJECT(glimagesink0), "client-draw", G_CALLBACK (drawCallback), NULL);
Packit 971217
Packit 971217
    /* add elements */
Packit 971217
    gst_bin_add_many (GST_BIN (pipeline), videosrc, decodebin, videoconvert, textoverlay, tee, 
Packit 971217
                                          queue0, glimagesink0,
Packit 971217
                                          queue1, glfiltercube, glimagesink1,
Packit 971217
                                          queue2, glimagesink2, NULL);
Packit 971217
Packit 971217
    GstPad* textoverlay_sink_pad = gst_element_get_static_pad (textoverlay, "video_sink");
Packit 971217
    gst_pad_add_probe (textoverlay_sink_pad, GST_PAD_PROBE_TYPE_BUFFER,
Packit 971217
                       (GstPadProbeCallback) textoverlay_sink_pad_probe_cb, (gpointer)textoverlay, NULL);
Packit 971217
    gst_object_unref (textoverlay_sink_pad);
Packit 971217
Packit 971217
    if (!gst_element_link_many(videoconvert, textoverlay, tee, NULL))
Packit 971217
    {
Packit 971217
        g_print ("Failed to link videoconvert to tee!\n");
Packit 971217
        return -1;
Packit 971217
    }
Packit 971217
Packit 971217
    if (!gst_element_link(videosrc, decodebin))
Packit 971217
    {
Packit 971217
        g_print ("Failed to link videosrc to decodebin!\n");
Packit 971217
        return -1;
Packit 971217
    }
Packit 971217
Packit 971217
    g_signal_connect (decodebin, "pad-added", G_CALLBACK (cb_new_pad), videoconvert);
Packit 971217
Packit 971217
    if (!gst_element_link_many(tee, queue0, NULL))
Packit 971217
    {
Packit 971217
        g_warning ("Failed to link one or more elements bettween tee and queue0!\n");
Packit 971217
        return -1;
Packit 971217
    }
Packit 971217
Packit 971217
    gboolean link_ok = gst_element_link_filtered(queue0, glimagesink0, cubecaps) ;
Packit 971217
    gst_caps_unref(cubecaps) ;
Packit 971217
    if(!link_ok)
Packit 971217
    {
Packit 971217
        g_warning("Failed to link queue0 to glimagesink0!\n") ;
Packit 971217
        return -1 ;
Packit 971217
    }
Packit 971217
Packit 971217
    if (!gst_element_link_many(tee, queue1, glfiltercube, glimagesink1, NULL))
Packit 971217
    {
Packit 971217
        g_warning ("Failed to link one or more elements bettween tee and glimagesink1!\n");
Packit 971217
        return -1;
Packit 971217
    }
Packit 971217
Packit 971217
    if (!gst_element_link_many(tee, queue2, glimagesink2, NULL))
Packit 971217
    {
Packit 971217
        g_warning ("Failed to link one or more elements bettween tee and glimagesink2!\n");
Packit 971217
        return -1;
Packit 971217
    }
Packit 971217
Packit 971217
    /* run */
Packit 971217
    GstStateChangeReturn ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
Packit 971217
    if (ret == GST_STATE_CHANGE_FAILURE)
Packit 971217
    {
Packit 971217
        g_print ("Failed to start up pipeline!\n");
Packit 971217
Packit 971217
        /* check if there is an error message with details on the bus */
Packit 971217
        GstMessage* msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
Packit 971217
        if (msg)
Packit 971217
        {
Packit 971217
          GError *err = NULL;
Packit 971217
Packit 971217
          gst_message_parse_error (msg, &err, NULL);
Packit 971217
          g_print ("ERROR: %s\n", err->message);
Packit 971217
          g_error_free (err);
Packit 971217
          gst_message_unref (msg);
Packit 971217
        }
Packit 971217
        return -1;
Packit 971217
    }
Packit 971217
Packit 971217
    g_main_loop_run (loop);
Packit 971217
Packit 971217
    /* clean up */
Packit 971217
    gst_element_set_state (pipeline, GST_STATE_NULL);
Packit 971217
    gst_object_unref (pipeline);
Packit 971217
Packit 971217
    return 0;
Packit 971217
}