Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tags/Version Original/ARToolkit/lib/SRC/VideoGStreamer/video.c
blob: deef8f1819116fa19c9e311b2a00d4ad604d7a84 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
 * Video capture module utilising the GStreamer pipeline for AR Toolkit
 *
 * (c) Copyrights 2003-2007 Hartmut Seichter
 *
 * licensed under the terms of the GPL v2.0
 *
 */

/* include AR Toolkit*/
#include <AR/config.h>
#include <AR/ar.h>
#include <AR/video.h>

/* include GLib for GStreamer */
#include <glib.h>

/* include GStreamer itself */
#include <gst/gst.h>

/* using memcpy */
#include <string.h>


struct _AR2VideoParamT {

	/* GStreamer pipeline */
	GstElement *pipeline;

	/* GStreamer identity needed for probing */
	GstElement *probe;

	/* size of the image */
	int	width, height;

	/* the actual video buffer */
    ARUint8             *videoBuffer;

};


static AR2VideoParamT *gVid = 0;

static gboolean
cb_have_data (GstPad    *pad,
	      GstBuffer *buffer,
	      gpointer   u_data)
{
 	const GstCaps *caps;
	GstStructure *str;

	gint width,height;
	gdouble rate;

	AR2VideoParamT *vid = (AR2VideoParamT*)u_data;


	/* only do initialy for the buffer */
	if (vid->videoBuffer == 0)
	{

		/*
		 * Get the capabilities of the frame, we need that in order
		 * to extract information about the frame
		 */
		caps=gst_pad_get_negotiated_caps(pad);
		str=gst_caps_get_structure(caps,0);

		/* Get some data about the frame */
		gst_structure_get_int(str,"width",&width);
		gst_structure_get_int(str,"height",&height);
		gst_structure_get_double(str,"framerate",&rate);

		g_print("libARvideo: GStreamer negotiated %dx%d\n",width,height);

		vid->width = width;
		vid->height = height;

		/* allocate the buffer */
		vid->videoBuffer = malloc(buffer->size);

		return TRUE;

	}
	else
	{
		/* copy the video buffer */
		memcpy(vid->videoBuffer, buffer->data, buffer->size);
	}

	return TRUE;
}

void
testing_pad(GstPad *pad)
{
	const GstCaps *caps;
	GstStructure *str;

	gint width,height;
	gdouble rate;

	caps=gst_pad_get_negotiated_caps(pad);

	if (caps) {
		str=gst_caps_get_structure(caps,0);

		/* Get some data about the frame */
		gst_structure_get_int(str,"width",&width);
		gst_structure_get_int(str,"height",&height);
		gst_structure_get_double(str,"framerate",&rate);

		g_print("libARvideo: GStreamer negotiated %dx%d\n",width,height);
	} else {
		return;
#if 0
		g_print("Nothing yet!");
#endif

	}
}


int
arVideoPause() {
   if( gVid != NULL ) {
        return ar2VideoPause();
    }else{
        printf("Device has not been opened!!\n");
        return -1;
    }

}

int
arVideoOpen( char *config ) {
   if( gVid != NULL ) {
        printf("Device has been opened!!\n");
        return -1;
    }
    gVid = ar2VideoOpen( config );
    if( gVid == NULL ) return -1;
}

int
arVideoClose( void )
{
	return ar2VideoClose(gVid);
}

int
arVideoDispOption( void )
{
   return 0;
}

int
arVideoInqSize( int *x, int *y ) {

	ar2VideoInqSize(gVid,x,y);

	return 0;
}

ARUint8
*arVideoGetImage( void )
{
   return ar2VideoGetImage(gVid);  // address of your image data
}

int
arVideoCapStart( void ) {

	ar2VideoCapStart(gVid);
	return 0;
}

int
arVideoCapStop( void )
{
	ar2VideoCapStop(gVid);
	return 0;
}

int arVideoCapNext( void )
{
	ar2VideoCapNext(gVid);
	return 0;
}

/*---------------------------------------------------------------------------*/

AR2VideoParamT*
ar2VideoOpen(char *config_in ) {

	AR2VideoParamT *vid = 0;
	GError *error = 0;
	int i;
	GstPad *pad, *peerpad;
	GstXML *xml;
	GstStateChangeReturn _ret;
	char *config;

	/* If no config string is supplied, we should use the environment variable, otherwise set a sane default */
	if (!config_in || !(config_in[0])) {
		/* None suppplied, lets see if the user supplied one from the shell */
		char *envconf = getenv ("ARTOOLKIT_CONFIG");
		if (envconf && envconf[0]) {
			config = envconf;
			g_printf ("Using config string from environment [%s].\n", envconf);
		} else {
			config = NULL;
			g_printf ("No video config string supplied, using defaults.\n");
		}
	} else {
		config = config_in;
		g_printf ("Using supplied video config string [%s].\n", config_in);
	}

	/* initialise GStreamer */
	gst_init(0,0);

	/* init ART structure */
    arMalloc( vid, AR2VideoParamT, 1 );

	/* initialise buffer */
	vid->videoBuffer = 0;

	/* report the current version and features */
	g_print ("libARvideo: %s\n", gst_version_string());

#if 0
	xml = gst_xml_new();

	/* first check if config contains an xml file */
	if (gst_xml_parse_file(xml,config,NULL))
	{
		/* parse the pipe definition */

	} else
	{
		vid->pipeline = gst_xml_get_element(xml,"pipeline");
	}

#endif

	vid->pipeline = gst_parse_launch (config, &error);

	if (!vid->pipeline) {
		g_print ("Parse error: %s\n", error->message);
		return 0;
	};

	/* get the video sink */
	vid->probe = gst_bin_get_by_name(GST_BIN(vid->pipeline), "artoolkit");

	if (!vid->probe) {
		g_print("Pipeline has no element named 'artoolkit'!\n");
		return 0;
	};

	/* get the pad from the probe (the source pad seems to be more flexible) */
	pad = gst_element_get_pad (vid->probe, "src");


	/* install the probe callback for capturing */
	gst_pad_add_buffer_probe (pad, G_CALLBACK (cb_have_data), vid);



#if 0
	/* request ready state */
	gst_element_set_state (vid->pipeline, GST_STATE_READY);

	/* check if stream is ready */
	if (gst_element_get_state (vid->pipeline, NULL, NULL, -1) == GST_STATE_CHANGE_FAILURE) {
    	g_error ("libARvideo: failed to put GStreamer into READY state!\n");
    } else {
    	g_print ("libARvideo: GStreamer pipeline is READY!\n");
    }
#endif

	/* Needed to fill the information for ARVidInfo */
	gst_element_set_state (vid->pipeline, GST_STATE_PAUSED);

	peerpad = gst_pad_get_peer(pad);

	testing_pad(peerpad);

	/* dismiss the pad */
	gst_object_unref (pad);

	/* wait until it's up and running or failed */
	if (gst_element_get_state (vid->pipeline, NULL, NULL, -1) == GST_STATE_CHANGE_FAILURE) {
    	g_error ("libARvideo: failed to put GStreamer into PAUSE state!\n");
    } else {
    	g_print ("libARvideo: GStreamer pipeline is PAUSED!\n");
    }

	/* now preroll for V4L v2 interfaces */
	if ((strstr(config, "v4l2src") != 0) ||
		(strstr(config, "dv1394src") != 0))
	{
		/* set playing state of the pipeline */
		gst_element_set_state (vid->pipeline, GST_STATE_PLAYING);

		/* wait until it's up and running or failed */
		if (gst_element_get_state (vid->pipeline, NULL, NULL, -1) == GST_STATE_CHANGE_FAILURE) {
	    	g_error ("libARvideo: failed to put GStreamer into PLAYING state!\n");
	    } else {
	    	g_print ("libARvideo: GStreamer pipeline is PLAYING!\n");
	    }

		/* set playing state of the pipeline */
		gst_element_set_state (vid->pipeline, GST_STATE_PAUSED);

		/* wait until it's up and running or failed */
		if (gst_element_get_state (vid->pipeline, NULL, NULL, -1) == GST_STATE_CHANGE_FAILURE) {
	    	g_error ("libARvideo: failed to put GStreamer into PAUSED state!\n");
	    } else {
	    	g_print ("libARvideo: GStreamer pipeline is PAUSED!\n");
	    }
	}

#if 0
	/* write the bin to stdout */
	gst_xml_write_file (GST_ELEMENT (vid->pipeline), stdout);
#endif

	/* return the video handle */
	return vid;
};


int ar2VideoPause(AR2VideoParamT *vid){
    /* ready the pipeline */
    g_print("Paso a ready\n");
    gst_element_set_state (vid->pipeline, GST_STATE_READY);

	if (gst_element_get_state (vid->pipeline, NULL, NULL, -1) == GST_STATE_CHANGE_FAILURE) {
        g_error ("libARvideo: failed to put GStreamer into READY state!\n");
    } else {
        g_print ("libARvideo: GStreamer pipeline is READY!\n");
    }


    /*pause the pipeline*/
    g_print("Paso a paused\n");
	gst_element_set_state (vid->pipeline, GST_STATE_PAUSED);

	if (gst_element_get_state (vid->pipeline, NULL, NULL, -1) == GST_STATE_CHANGE_FAILURE) {
        g_error ("libARvideo: failed to put GStreamer into PAUSED state!\n");
    } else {
        g_print ("libARvideo: GStreamer pipeline is PAUSED!\n");
    }
}

int
ar2VideoClose(AR2VideoParamT *vid) {

	/* stop the pipeline */
	gst_element_set_state (vid->pipeline, GST_STATE_NULL);

	/* free the pipeline handle */
	gst_object_unref (GST_OBJECT (vid->pipeline));

	return 0;
}


ARUint8*
ar2VideoGetImage(AR2VideoParamT *vid) {
	/* just return the bare video buffer */
	return vid->videoBuffer;
}

int
ar2VideoCapStart(AR2VideoParamT *vid)
{
	GstStateChangeReturn _ret;

	/* set playing state of the pipeline */
	_ret = gst_element_set_state (vid->pipeline, GST_STATE_PLAYING);

	if (_ret == GST_STATE_CHANGE_ASYNC)
	{

		/* wait until it's up and running or failed */
		if (gst_element_get_state (vid->pipeline,
				NULL, NULL, GST_CLOCK_TIME_NONE) == GST_STATE_CHANGE_FAILURE)
		{
    		g_error ("libARvideo: failed to put GStreamer into PLAYING state!\n");
    		return 0;

        } else {
			g_print ("libARvideo: GStreamer pipeline is PLAYING!\n");
		}
	}
	return 1;
}

int
ar2VideoCapStop(AR2VideoParamT *vid) {
	/* stop pipeline */
	return gst_element_set_state (vid->pipeline, GST_STATE_NULL);
}

int
ar2VideoCapNext(AR2VideoParamT *vid)
{
	/* gstreamer should */
	return TRUE;
}

int
ar2VideoInqSize(AR2VideoParamT *vid, int *x, int *y )
{

   *x = vid->width; // width of your static image
   *y = vid->height; // height of your static image

}