summaryrefslogtreecommitdiff
path: root/attic/gcr/gcr.c
blob: 68888e3bd9dd191edb0fdd59730a76b00e9bf1e8 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* Minimal screencast recorder for clutter (GL) using GEGL.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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.
 *
 * Copyright (C) 2008 Øyvind Kolås
 * Copyright (C) 2008 OpenedHand
 */

#include <gegl.h>
#include <clutter/clutter.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <string.h>

/* some basic configuration */
#define FPS 25
#define KBITRATE (7000*8)  

/* TODO: Move away from pixbufs, the original code used a pixbuf but
 * with newer GEGL it should be better to use a linear buffer directly
 */

static GThread      *encode_thread   = NULL;
static GStaticMutex  mutex           = G_STATIC_MUTEX_INIT;
static gint          prev_width      = 0;
static gint          prev_height     = 0;
static gint          frames          = 0;
static guchar       *pixels          = NULL;
static guchar       *pixels_inverted = NULL;
static gboolean      got_data        = FALSE;
static GdkPixbuf    *pixbuf          = NULL;
static GeglNode     *gegl            = NULL;
static GeglNode     *load_pixbuf     = NULL;
static GeglNode     *ff_save;
static long          prev_stored     = 0;

static gpointer encoder (gpointer data);
static void save_frame  (gpointer data);

long babl_ticks (void);

void gcr_prepare (const gchar *path)
{
  if (!g_thread_supported ()) g_thread_init (NULL);

  gegl_init (NULL, NULL);
  gegl = gegl_node_new ();

  ff_save = gegl_node_new_child (gegl,
     "operation", "ff-save",
     "bitrate",   KBITRATE *1000.0,
     "fps",       (FPS * 1.0),
     "path",      path,
     NULL
    );
  load_pixbuf = gegl_node_create_child (gegl, "pixbuf");
  gegl_node_link (load_pixbuf, ff_save);
}

void gcr_start (void)
{
  ClutterActor *stage = clutter_stage_get_default ();

  g_signal_connect_after (stage, "paint", G_CALLBACK (save_frame), NULL);

  encode_thread = g_thread_create (encoder, NULL, FALSE, NULL);

  prev_stored = babl_ticks ();
}

void gcr_stop (void)
{
  /* FIXME: NYI */
}

/* this is called by clutter each time a stage has been rendered */
static void save_frame (gpointer data)
{

  GLint      viewport[4];
  gint       x, y, width, height;
  glong      delta;

  /* issue commands that might make sure we're fully rendered ... */
  glFinish ();
  glFlush ();

  glGetIntegerv(GL_VIEWPORT, viewport);
 
  x         = viewport[0]; 
  y         = viewport[1]; 
  width     = viewport[2] - x;
  height    = viewport[3] - y;

  /* by locking here we wait until the encoder thread is finished encoding */
  g_static_mutex_lock (&mutex);

  /* we only create the pixbuf on the first frame (hopefully) */

  if (prev_width  != width ||
      prev_height != height)
    {
      if (pixels)
        g_free (pixels);
      if (pixels_inverted)
        g_free (pixels_inverted);
      if (pixbuf)
        g_object_unref (pixbuf);
      pixels_inverted = g_malloc (width * height * 4);
      pixels = g_malloc (width * height * 4);

      pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
                                         width, height, width * 4, NULL, NULL);

      prev_width = width;
      prev_height = height;
    }

  /* figure out the time elapsed since the previously stored encoded frame */
  delta = babl_ticks () - prev_stored;

  if (got_data == FALSE && delta >= 1000000.0/FPS)
    {
      prev_stored += delta;
      glReadPixels (x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels_inverted);
      /* we only do the read pixels here, the encoder thread does the
       * inversion
       */
      got_data = TRUE;

      frames = delta / (1000000.0/FPS);
      /* FIXME: * precision loss, we need to keep a remainder around to produce
       * the correct framerate */
    }
  g_static_mutex_unlock (&mutex);
}

static gpointer encoder (gpointer data)
{
  while (TRUE)
    {
      gint rowstride;
      gint width, height;
      gint repeats = 0;

      gboolean action = FALSE;

      /* critical section */
      g_static_mutex_lock (&mutex);
      if (got_data)
        {
          gint y;
          height = prev_height;
          width = prev_width;
          rowstride = prev_width * 4;

          /* flip the image right side up when copying it */
          for (y=0; y<height; y++)
            {
              memcpy (pixels + ((height-y-1) * rowstride),
                      pixels_inverted + y * rowstride, rowstride);
            }
          got_data = FALSE;
          action = TRUE;  /* store that we've got work to do */
          repeats = frames;
        }
      g_static_mutex_unlock (&mutex);

      if (action)
        {
          static     gint dump_no = -1;

          if (dump_no<=0)
            {
              dump_no++;
            }
          else
            {
              /* encode the current frame for the number of frames
               * that is needed
               */
              while (repeats--)
                {
                  gegl_node_set (load_pixbuf, "pixbuf", pixbuf, NULL);
                  gegl_node_process (ff_save);
                }
            }
        }
      else
        {
          g_usleep (100);
        }
    }
}