]> git.sesse.net Git - vlc/blob - extras/misc/overlay-test.c
Overlay - Updated test program
[vlc] / extras / misc / 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 /*****************************************************************************
145  * Test Routines
146  *****************************************************************************/
147
148 void BasicTest( FILE *p_cmd, FILE *p_res, int i_overlay ) {
149     printf( "Activating overlay..." );
150     SetVisibility( p_cmd, p_res, i_overlay, 1 );
151     printf( " done\n" );
152
153     printf( "Sweeping alpha..." );
154     for( int i_alpha = 0xFF; i_alpha >= -0xFF ; i_alpha -= 8 ) {
155         SetAlpha( p_cmd, p_res, i_overlay, abs( i_alpha ) );
156         usleep( 20000 );
157     }
158     SetAlpha( p_cmd, p_res, i_overlay, 255 );
159     printf( " done\n" );
160
161     printf( "Circle motion..." );
162     for( float f_theta = 0; f_theta <= 2 * M_PI ; f_theta += M_PI / 64.0 ) {
163         SetPosition( p_cmd, p_res, i_overlay,
164                      (int)( - cos( f_theta ) * 100.0 + 100.0 ),
165                      (int)( - sin( f_theta ) * 100.0 + 100.0 ) );
166         usleep( 20000 );
167     }
168     SetPosition( p_cmd, p_res, i_overlay, 0, 100 );
169     printf( " done\n" );
170
171     printf( "Atomic motion..." );
172     StartAtomic( p_cmd, p_res );
173     SetPosition( p_cmd, NULL, i_overlay, 200, 50 );
174     sleep( 1 );
175     SetPosition( p_cmd, NULL, i_overlay, 0, 0 );
176     EndAtomic( p_cmd, p_res );
177     CheckResult( p_res );
178     CheckResult( p_res );
179     printf( " done\n" );
180
181     sleep( 5 );
182 }
183
184 /*****************************************************************************
185  * main
186  *****************************************************************************/
187
188 int main( int i_argc, char *ppsz_argv[] ) {
189     if( i_argc != 3 ) {
190         printf( "Incorrect number of parameters.\n"
191                 "Usage is: %s command-fifo response-fifo\n", ppsz_argv[0] );
192         exit( -2 );
193     }
194
195     printf( "Creating shared memory for RGBA..." );
196     int i_shmRGBA = shmget( IPC_PRIVATE, WIDTH * HEIGHT * 4, S_IRWXU );
197     if( i_shmRGBA == -1 ) {
198         printf( " failed\n" );
199         exit( -1 );
200     }
201     printf( " done, ID is %d. Text...", i_shmRGBA );
202     int i_shmText = shmget( IPC_PRIVATE, TEXTSIZE, S_IRWXU );
203     if( i_shmText == -1 ) {
204         printf( " failed\n" );
205         exit( -1 );
206     }
207     printf( " done, ID is %d\n", i_shmText );
208
209     printf( "Attaching shared memory for RGBA..." );
210     p_imageRGBA = shmat( i_shmRGBA, NULL, 0 );
211     if( p_imageRGBA == -1 ) {
212         printf( " failed\n" );
213         exit( -1 );
214     }
215     printf( " done. Text..." );
216     p_text = shmat( i_shmText, NULL, 0 );
217     if( p_text == -1 ) {
218         printf( " failed\n" );
219         exit( -1 );
220     }
221     printf( " done\n" );
222
223     printf( "Queueing shared memory for destruction, RGBA..." );
224     if( shmctl( i_shmRGBA, IPC_RMID, 0 ) == -1 ) {
225         printf( " failed\n" );
226         exit( -1 );
227     }
228     printf( " done. Text..." );
229     if( shmctl( i_shmText, IPC_RMID, 0 ) == -1 ) {
230         printf( " failed\n" );
231         exit( -1 );
232     }
233     printf( " done\n" );
234
235     printf( "Generating data..." );
236     DataCreate();
237     printf( " done\n" );
238
239     printf( "Making FIFOs..." );
240     if( mkfifo( ppsz_argv[1], S_IRWXU ) ) {
241         if( errno != EEXIST ) {
242             printf( " failed\n" );
243             exit( -1 );
244         }
245         printf( " input already exists..." );
246     }
247     if( mkfifo( ppsz_argv[2], S_IRWXU ) ) {
248         if( errno != EEXIST ) {
249             printf( " failed\n" );
250             exit( -1 );
251         }
252         printf( " output already exists..." );
253     }
254     printf( " done\n" );
255
256     printf( "Please make sure vlc is running.\n"
257             "You should append parameters similar to the following:\n"
258             "--sub-filter overlay{input=%s,output=%s}\n",
259             ppsz_argv[1], ppsz_argv[2] );
260
261     printf( "Opening FIFOs..." );
262     FILE *p_cmd = fopen( ppsz_argv[1], "w" );
263     if( p_cmd == NULL ) {
264         printf( " failed\n" );
265         exit( -1 );
266     }
267     FILE *p_res = fopen( ppsz_argv[2], "r" );
268     if( p_res == NULL ) {
269         printf( " failed\n" );
270         exit( -1 );
271     }
272     printf( " done\n" );
273
274     int i_overlay_image = GenImage( p_cmd, p_res );
275     int i_overlay_text = GenImage( p_cmd, p_res );
276     DataSharedMem( p_cmd, p_res, i_overlay_image, WIDTH, HEIGHT, "RGBA",
277                    i_shmRGBA );
278     DataSharedMem( p_cmd, p_res, i_overlay_text, TEXTSIZE, 1, "TEXT",
279                    i_shmText );
280
281     BasicTest( p_cmd, p_res, i_overlay_image );
282     BasicTest( p_cmd, p_res, i_overlay_text );
283
284     DeleteImage( p_cmd, p_res, i_overlay_image );
285     DeleteImage( p_cmd, p_res, i_overlay_text );
286 }