main.cpp 8.88 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
/*
 * 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.
 */
20 21 22
#if defined(_MSC_VER)
# include <windows.h>
#endif
Matthew Waters's avatar
Matthew Waters committed
23 24

#include <GL/gl.h>
25
#if defined(__WIN32__) || defined(_WIN32)
Matthew Waters's avatar
Matthew Waters committed
26 27 28 29 30 31 32 33 34 35 36 37
# include <GL/glext.h>
#endif
#include <gst/gst.h>
#include <gst/video/video.h>

#include <iostream>
#include <string>

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

38
    switch (GST_MESSAGE_TYPE (msg))
Matthew Waters's avatar
Matthew Waters committed
39 40 41 42 43
    {
        case GST_MESSAGE_EOS:
              g_print ("End-of-stream\n");
              g_main_loop_quit (loop);
              break;
44
        case GST_MESSAGE_ERROR:
Matthew Waters's avatar
Matthew Waters committed
45 46 47 48 49 50 51 52 53
          {
              gchar *debug = NULL;
              GError *err = NULL;

              gst_message_parse_error (msg, &err, &debug);

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

54
              if (debug)
Matthew Waters's avatar
Matthew Waters committed
55 56 57 58 59 60 61
              {
                  g_print ("Debug details: %s\n", debug);
                  g_free (debug);
              }

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

    return TRUE;
}

//client draw callback
71
static gboolean drawCallback (void *filter, void *context, GLuint texture, GLuint width, GLuint height, gpointer data)
Matthew Waters's avatar
Matthew Waters committed
72 73
{
    static GLfloat	xrot = 0;
74
    static GLfloat	yrot = 0;
Matthew Waters's avatar
Matthew Waters committed
75 76 77
    static GLfloat	zrot = 0;
    static GTimeVal current_time;
    static glong last_sec = current_time.tv_sec;
78
    static gint nbFrames = 0;
Matthew Waters's avatar
Matthew Waters committed
79 80 81

    g_get_current_time (&current_time);
    nbFrames++ ;
82

Matthew Waters's avatar
Matthew Waters committed
83 84 85 86 87 88
    if ((current_time.tv_sec - last_sec) >= 1)
    {
        std::cout << "GRPHIC FPS = " << nbFrames << std::endl;
        nbFrames = 0;
        last_sec = current_time.tv_sec;
    }
89

Matthew Waters's avatar
Matthew Waters committed
90 91 92 93 94 95 96 97 98 99 100 101 102
    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();
103

Matthew Waters's avatar
Matthew Waters committed
104 105 106 107 108 109 110 111 112
    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);

    //cube
    glBegin(GL_QUADS);
	      // Front Face
113
	      glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
Matthew Waters's avatar
Matthew Waters committed
114
	      glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
115 116
	      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
117 118
	      // Back Face
	      glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
119 120 121
	      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
122
	      // Top Face
123 124
	      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
125
	      glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
126
	      glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
Matthew Waters's avatar
Matthew Waters committed
127
	      // Bottom Face
128
	      glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
Matthew Waters's avatar
Matthew Waters committed
129
	      glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
130 131
	      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
132 133
	      // Right face
	      glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
134 135 136
	      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
137
	      // Left Face
138
	      glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
Matthew Waters's avatar
Matthew Waters committed
139
	      glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
140 141
	      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
142 143 144 145 146 147
    glEnd();

    xrot+=0.3f;
    yrot+=0.2f;
    zrot+=0.4f;

148 149
    //return TRUE because we dealt with the texture
    return TRUE;
Matthew Waters's avatar
Matthew Waters committed
150 151 152
}


153 154
//equivalent command line:
//gst-launch-1.0 videotestsrc num_buffers=400 ! gleffects effect=0 !
Matthew Waters's avatar
Matthew Waters committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
//avenc_mpeg4 ! avimux ! filesink location="record.avi"
// or
//gst-launch-1.0 videotestsrc num_buffers=400 ! gleffects effect=0 ! "video/x-raw, width=320, height=240" ! glfiltercube ! "video/x-raw, width=720, height=576" ! 
//avenc_mpeg4 ! avimux ! filesink location="record.avi"
gint main (gint argc, gchar *argv[])
{
    GstStateChangeReturn ret;
    GstElement *pipeline, *videosrc, *glfilterapp, *avenc_mpeg4, *avimux, *filesink;
    GMainLoop *loop;
    GstBus *bus;

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

    /* create elements */
    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) */
    bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
    gst_bus_add_watch (bus, bus_call, loop);
    gst_object_unref (bus);

    /* create elements */
    videosrc = gst_element_factory_make ("videotestsrc", "videotestsrc0");
    glfilterapp = gst_element_factory_make ("glfilterapp", "glfilterapp0");
    avenc_mpeg4  = gst_element_factory_make ("avenc_mpeg4", "avenc_mpeg40");
    avimux  = gst_element_factory_make ("avimux", "avimux0");
    filesink  = gst_element_factory_make ("filesink", "filesink0");


187
    if (!videosrc || !glfilterapp || !avenc_mpeg4 || !avimux || !filesink)
Matthew Waters's avatar
Matthew Waters committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    {
        g_print ("one element could not be found \n");
        return -1;
    }

    /* change video source caps */
    GstCaps *caps = gst_caps_new_simple("video/x-raw",
                                        "format", G_TYPE_STRING, "UYVY",
                                        "width", G_TYPE_INT, 320,
                                        "height", G_TYPE_INT, 240,
                                        "framerate", GST_TYPE_FRACTION, 25, 1,
                                        NULL);

    /* change video source caps */
    GstCaps *outcaps = gst_caps_new_simple("video/x-raw",
                                           "width", G_TYPE_INT, 640,
                                           "height", G_TYPE_INT, 480,
                                           NULL);

    /* configure elements */
    g_object_set(G_OBJECT(videosrc), "num-buffers", 400, NULL);
209
    g_signal_connect(G_OBJECT(glfilterapp), "client-draw", G_CALLBACK (drawCallback), NULL);
Matthew Waters's avatar
Matthew Waters committed
210
    g_object_set(G_OBJECT(filesink), "location", "record.avi", NULL);
211

Matthew Waters's avatar
Matthew Waters committed
212
    /* add elements */
213
    gst_bin_add_many (GST_BIN (pipeline), videosrc, glfilterapp,
Matthew Waters's avatar
Matthew Waters committed
214
        avenc_mpeg4, avimux, filesink, NULL);
215

Matthew Waters's avatar
Matthew Waters committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    /* link elements */
    gboolean link_ok = gst_element_link_filtered(videosrc, glfilterapp, caps) ;
    gst_caps_unref(caps) ;
    if(!link_ok)
    {
        g_warning("Failed to link videosrc to glfilterapp!\n") ;
        return -1 ;
    }

    link_ok = gst_element_link_filtered(glfilterapp, avenc_mpeg4, outcaps) ;
    gst_caps_unref(outcaps) ;
    if(!link_ok)
    {
        g_warning("Failed to link glfilterapp to avenc_mpeg4!\n") ;
        return -1 ;
    }
232
    if (!gst_element_link_many(avenc_mpeg4, avimux, filesink, NULL))
Matthew Waters's avatar
Matthew Waters committed
233 234 235 236 237
    {
        g_print ("Failed to link one or more elements!\n");
        return -1;
    }

238

Matthew Waters's avatar
Matthew Waters committed
239 240
    /* run */
    ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
241
    if (ret == GST_STATE_CHANGE_FAILURE)
Matthew Waters's avatar
Matthew Waters committed
242 243 244 245 246
    {
        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);
247
        if (msg)
Matthew Waters's avatar
Matthew Waters committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
        {
          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;
}