dbus: increase player timer period
[vlc.git] / test / dynamicoverlay / overlay-test.c
bloba572cd7a0abdc1d46edd0cfaf9b72d2c6e01225d
1 /*****************************************************************************
2 * overlay-test.c : test program for the dynamic overlay plugin
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
6 * Author: Søren Bøg <avacore@videolan.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <math.h>
33 #include <sys/ipc.h>
34 #include <sys/shm.h>
35 #include <unistd.h>
37 /*****************************************************************************
38 * Images
39 *****************************************************************************/
41 #define WIDTH 128
42 #define HEIGHT 128
44 #define TEXT "Hello world!"
45 #define TEXTSIZE sizeof( TEXT )
47 char *p_imageRGBA;
48 char *p_text;
50 void DataCreate( void ) {
51 char *p_data = p_imageRGBA;
52 for( size_t i = 0; i < HEIGHT; ++i ) {
53 for( size_t j = 0; j < HEIGHT; ++j ) {
54 *(p_data++) = i * 4 & 0xFF;
55 *(p_data++) = 0xFF;
56 *(p_data++) = 0x00;
57 *(p_data++) = j * 4 & 0xFF;
61 memcpy( p_text, TEXT, TEXTSIZE );
64 /*****************************************************************************
65 * I/O Helpers
66 *****************************************************************************/
68 int IsFailure( char *psz_text ) {
69 return strncmp( psz_text, "SUCCESS:", 8 );
72 void CheckResult( FILE *p_res ) {
73 char psz_resp[9];
75 fscanf( p_res, "%8s", &psz_resp );
76 if( IsFailure( psz_resp ) ) {
77 printf( " failed\n" );
78 exit( -1 );
82 void CheckedCommand( FILE *p_cmd, FILE *p_res, char *p_format, ... ) {
83 va_list ap;
84 va_start( ap, p_format );
86 vfprintf( p_cmd, p_format, ap );
87 fflush( p_cmd );
88 if( p_res != NULL ) {
89 CheckResult( p_res );
91 va_end( ap );
94 int GenImage( FILE *p_cmd, FILE *p_res ) {
95 int i_overlay;
97 printf( "Getting an overlay..." );
98 CheckedCommand( p_cmd, p_res, "GenImage\n" );
99 fscanf( p_res, "%d", &i_overlay );
100 printf( " done. Overlay is %d\n", i_overlay );
102 return i_overlay;
105 void DeleteImage( FILE *p_cmd, FILE *p_res, int i_overlay ) {
106 printf( "Removing image..." );
107 CheckedCommand( p_cmd, p_res, "DeleteImage %d\n", i_overlay );
108 printf( " done\n" );
111 void StartAtomic( FILE *p_cmd, FILE *p_res ) {
112 CheckedCommand( p_cmd, p_res, "StartAtomic\n" );
115 void EndAtomic( FILE *p_cmd, FILE *p_res ) {
116 CheckedCommand( p_cmd, p_res, "EndAtomic\n" );
119 void DataSharedMem( FILE *p_cmd, FILE *p_res, int i_overlay, int i_width,
120 int i_height, char *psz_format, int i_shmid ) {
122 printf( "Sending data via shared memory..." );
123 CheckedCommand( p_cmd, p_res, "DataSharedMem %d %d %d %s %d\n", i_overlay,
124 i_width, i_height, psz_format, i_shmid );
125 printf( " done\n" );
128 void SetAlpha( FILE *p_cmd, FILE *p_res, int i_overlay, int i_alpha ) {
129 CheckedCommand( p_cmd, p_res, "SetAlpha %d %d\n", i_overlay, i_alpha );
132 void SetPosition( FILE *p_cmd, FILE *p_res, int i_overlay, int i_x, int i_y ) {
133 CheckedCommand( p_cmd, p_res, "SetPosition %d %d %d\n", i_overlay, i_x,
134 i_y );
137 void SetVisibility( FILE *p_cmd, FILE *p_res, int i_overlay, int i_visible ) {
138 CheckedCommand( p_cmd, p_res, "SetVisibility %d %d\n", i_overlay,
139 i_visible );
142 void SetTextAlpha( FILE *p_cmd, FILE *p_res, int i_overlay, int i_alpha ) {
143 CheckedCommand( p_cmd, p_res, "SetTextAlpha %d %d\n", i_overlay, i_alpha );
146 void SetTextColor( FILE *p_cmd, FILE *p_res, int i_overlay, int i_red,
147 int i_green, int i_blue ) {
148 CheckedCommand( p_cmd, p_res, "SetTextColor %d %d %d %d\n", i_overlay,
149 i_red, i_green, i_blue );
152 void SetTextSize( FILE *p_cmd, FILE *p_res, int i_overlay, int i_size ) {
153 CheckedCommand( p_cmd, p_res, "SetTextSize %d %d\n", i_overlay, i_size );
156 int GetTextSize( FILE *p_cmd, FILE *p_res, int i_overlay ) {
157 int i_size;
159 CheckedCommand( p_cmd, p_res, "GetTextSize %d\n", i_overlay );
160 fscanf( p_res, "%d", &i_size );
162 return i_size;
165 /*****************************************************************************
166 * Test Routines
167 *****************************************************************************/
169 void BasicTest( FILE *p_cmd, FILE *p_res, int i_overlay ) {
170 printf( "Activating overlay..." );
171 SetVisibility( p_cmd, p_res, i_overlay, 1 );
172 printf( " done\n" );
174 printf( "Sweeping alpha..." );
175 for( int i_alpha = 0xFF; i_alpha >= -0xFF ; i_alpha -= 8 ) {
176 SetAlpha( p_cmd, p_res, i_overlay, abs( i_alpha ) );
177 usleep( 20000 );
179 SetAlpha( p_cmd, p_res, i_overlay, 255 );
180 printf( " done\n" );
182 printf( "Circle motion..." );
183 for( float f_theta = 0; f_theta <= 2 * M_PI ; f_theta += M_PI / 64.0 ) {
184 SetPosition( p_cmd, p_res, i_overlay,
185 (int)( - cos( f_theta ) * 100.0 + 100.0 ),
186 (int)( - sin( f_theta ) * 100.0 + 100.0 ) );
187 usleep( 20000 );
189 SetPosition( p_cmd, p_res, i_overlay, 0, 100 );
190 printf( " done\n" );
192 printf( "Atomic motion..." );
193 StartAtomic( p_cmd, p_res );
194 SetPosition( p_cmd, NULL, i_overlay, 200, 50 );
195 sleep( 1 );
196 SetPosition( p_cmd, NULL, i_overlay, 0, 0 );
197 EndAtomic( p_cmd, p_res );
198 CheckResult( p_res );
199 CheckResult( p_res );
200 printf( " done\n" );
202 sleep( 5 );
205 void TextTest( FILE *p_cmd, FILE *p_res, int i_overlay ) {
206 printf( "Sweeping (text) alpha..." );
207 for( int i_alpha = 0xFF; i_alpha >= -0xFF ; i_alpha -= 8 ) {
208 SetTextAlpha( p_cmd, p_res, i_overlay, abs( i_alpha ) );
209 usleep( 20000 );
211 SetTextAlpha( p_cmd, p_res, i_overlay, 255 );
212 printf( " done\n" );
214 printf( "Sweeping colors..." );
215 for( int i_red = 0xFF; i_red >= 0x00 ; i_red -= 8 ) {
216 SetTextColor( p_cmd, p_res, i_overlay, i_red, 0xFF, 0xFF );
217 usleep( 20000 );
219 for( int i_green = 0xFF; i_green >= 0x00 ; i_green -= 8 ) {
220 SetTextColor( p_cmd, p_res, i_overlay, 0x00, i_green, 0xFF );
221 usleep( 20000 );
223 for( int i_blue = 0xFF; i_blue >= 0x00 ; i_blue -= 8 ) {
224 SetTextColor( p_cmd, p_res, i_overlay, 0x00, 0x00, i_blue );
225 usleep( 20000 );
227 SetTextColor( p_cmd, p_res, i_overlay, 0x00, 0x00, 0x00 );
228 printf( " done\n" );
230 printf( "Getting size..." );
231 int i_basesize = GetTextSize( p_cmd, p_res, i_overlay );
232 printf( " done. Size is %d\n", i_basesize );
234 printf( "Sweeping size..." );
235 for( float f_theta = 0; f_theta <= M_PI ; f_theta += M_PI / 128.0 ) {
236 SetTextSize( p_cmd, p_res, i_overlay,
237 i_basesize * ( 1 + 3 * sin( f_theta ) ) );
238 usleep( 20000 );
240 SetTextSize( p_cmd, p_res, i_overlay, i_basesize );
241 printf( " done\n" );
243 sleep( 5 );
246 /*****************************************************************************
247 * main
248 *****************************************************************************/
250 int main( int i_argc, char *ppsz_argv[] ) {
251 if( i_argc != 3 ) {
252 printf( "Incorrect number of parameters.\n"
253 "Usage is: %s command-fifo response-fifo\n", ppsz_argv[0] );
254 exit( -2 );
257 printf( "Creating shared memory for RGBA..." );
258 int i_shmRGBA = shmget( IPC_PRIVATE, WIDTH * HEIGHT * 4, S_IRWXU );
259 if( i_shmRGBA == -1 ) {
260 printf( " failed\n" );
261 exit( -1 );
263 printf( " done, ID is %d. Text...", i_shmRGBA );
264 int i_shmText = shmget( IPC_PRIVATE, TEXTSIZE, S_IRWXU );
265 if( i_shmText == -1 ) {
266 printf( " failed\n" );
267 exit( -1 );
269 printf( " done, ID is %d\n", i_shmText );
271 printf( "Attaching shared memory for RGBA..." );
272 p_imageRGBA = shmat( i_shmRGBA, NULL, 0 );
273 if( p_imageRGBA == (void*)-1 ) {
274 printf( " failed\n" );
275 exit( -1 );
277 printf( " done. Text..." );
278 p_text = shmat( i_shmText, NULL, 0 );
279 if( p_text == (void*)-1 ) {
280 printf( " failed\n" );
281 exit( -1 );
283 printf( " done\n" );
285 printf( "Queueing shared memory for destruction, RGBA..." );
286 if( shmctl( i_shmRGBA, IPC_RMID, 0 ) == -1 ) {
287 printf( " failed\n" );
288 exit( -1 );
290 printf( " done. Text..." );
291 if( shmctl( i_shmText, IPC_RMID, 0 ) == -1 ) {
292 printf( " failed\n" );
293 exit( -1 );
295 printf( " done\n" );
297 printf( "Generating data..." );
298 DataCreate();
299 printf( " done\n" );
301 printf( "Making FIFOs..." );
302 if( mkfifo( ppsz_argv[1], S_IRWXU ) ) {
303 if( errno != EEXIST ) {
304 printf( " failed\n" );
305 exit( -1 );
307 printf( " input already exists..." );
309 if( mkfifo( ppsz_argv[2], S_IRWXU ) ) {
310 if( errno != EEXIST ) {
311 printf( " failed\n" );
312 exit( -1 );
314 printf( " output already exists..." );
316 printf( " done\n" );
318 printf( "Please make sure vlc is running.\n"
319 "You should append parameters similar to the following:\n"
320 "--sub-source overlay{input=%s,output=%s}\n",
321 ppsz_argv[1], ppsz_argv[2] );
323 printf( "Opening FIFOs..." );
324 FILE *p_cmd = fopen( ppsz_argv[1], "w" );
325 if( p_cmd == NULL ) {
326 printf( " failed\n" );
327 exit( -1 );
329 FILE *p_res = fopen( ppsz_argv[2], "r" );
330 if( p_res == NULL ) {
331 printf( " failed\n" );
332 exit( -1 );
334 printf( " done\n" );
336 int i_overlay_image = GenImage( p_cmd, p_res );
337 int i_overlay_text = GenImage( p_cmd, p_res );
338 DataSharedMem( p_cmd, p_res, i_overlay_image, WIDTH, HEIGHT, "RGBA",
339 i_shmRGBA );
340 DataSharedMem( p_cmd, p_res, i_overlay_text, TEXTSIZE, 1, "TEXT",
341 i_shmText );
343 BasicTest( p_cmd, p_res, i_overlay_image );
344 BasicTest( p_cmd, p_res, i_overlay_text );
345 TextTest( p_cmd, p_res, i_overlay_text );
347 DeleteImage( p_cmd, p_res, i_overlay_image );
348 DeleteImage( p_cmd, p_res, i_overlay_text );