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