]> git.sesse.net Git - vlc/blob - extras/misc/overlay-test.c
Overlay - Extended demonstration 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 <string.h>
31 #include <math.h>
32
33 #include <sys/fcntl.h>
34 #include <sys/ipc.h>
35 #include <sys/shm.h>
36 #include <unistd.h>
37
38 /*****************************************************************************
39  * Images
40  *****************************************************************************/
41
42 #define WIDTH 128
43 #define HEIGHT 128
44
45 #define TEXT "Hello world!"
46 #define TEXTSIZE sizeof( TEXT )
47
48 char *p_imageRGBA;
49 char *p_text;
50
51 void DataCreate( void ) {
52     char *p_data = p_imageRGBA;
53     for( size_t i = 0; i < HEIGHT; ++i ) {
54         for( size_t j = 0; j < HEIGHT; ++j ) {
55             *(p_data++) = i * 4 & 0xFF;
56             *(p_data++) = 0xFF;
57             *(p_data++) = 0x00;
58             *(p_data++) = j * 4 & 0xFF;
59         }
60     }
61
62     memcpy( p_text, TEXT, TEXTSIZE );
63 }
64
65 /*****************************************************************************
66  * I/O Helpers
67  *****************************************************************************/
68
69 int IsFailure( char *psz_text ) {
70     return strncmp( psz_text, "SUCCESS:", 8 );
71 }
72
73 void CheckResult( FILE *p_res ) {
74     char psz_resp[9];
75
76     fscanf( p_res, "%8s", &psz_resp );
77     if( IsFailure( psz_resp ) ) {
78         printf( " failed\n" );
79         exit( -1 );
80     }
81 }
82
83 void CheckedCommand( FILE *p_cmd, FILE *p_res, char *p_format, ... ) {
84     va_list ap;
85     va_start( ap, p_format );
86
87     vfprintf( p_cmd, p_format, ap );
88     fflush( p_cmd );
89     if( p_res != NULL ) {
90         CheckResult( p_res );
91     }
92     va_end( ap );
93 }
94
95 int GenImage( FILE *p_cmd, FILE *p_res ) {
96     int i_overlay;
97
98     printf( "Getting an overlay..." );
99     CheckedCommand( p_cmd, p_res, "GenImage\n" );
100     fscanf( p_res, "%d", &i_overlay );
101     printf( " done. Overlay is %d\n", i_overlay );
102
103     return i_overlay;
104 }
105
106 void DeleteImage( FILE *p_cmd, FILE *p_res, int i_overlay ) {
107     printf( "Removing image..." );
108     CheckedCommand( p_cmd, p_res, "DeleteImage %d\n", i_overlay );
109     printf( " done\n" );
110 }
111
112 void StartAtomic( FILE *p_cmd, FILE *p_res ) {
113     CheckedCommand( p_cmd, p_res, "StartAtomic\n" );
114 }
115
116 void EndAtomic( FILE *p_cmd, FILE *p_res ) {
117     CheckedCommand( p_cmd, p_res, "EndAtomic\n" );
118 }
119
120 void DataSharedMem( FILE *p_cmd, FILE *p_res, int i_overlay, int i_width,
121                     int i_height, char *psz_format, int i_shmid ) {
122
123     printf( "Sending data via shared memory..." );
124     CheckedCommand( p_cmd, p_res, "DataSharedMem %d %d %d %s %d\n", i_overlay,
125                     i_width, i_height, psz_format, i_shmid );
126     printf( " done\n" );
127 }
128
129 void SetAlpha( FILE *p_cmd, FILE *p_res, int i_overlay, int i_alpha ) {
130     CheckedCommand( p_cmd, p_res, "SetAlpha %d %d\n", i_overlay, i_alpha );
131 }
132
133 void SetPosition( FILE *p_cmd, FILE *p_res, int i_overlay, int i_x, int i_y ) {
134     CheckedCommand( p_cmd, p_res, "SetPosition %d %d %d\n", i_overlay, i_x,
135                     i_y );
136 }
137
138 void SetVisibility( FILE *p_cmd, FILE *p_res, int i_overlay, int i_visible ) {
139     CheckedCommand( p_cmd, p_res, "SetVisibility %d %d\n", i_overlay,
140                     i_visible );
141 }
142
143 /*****************************************************************************
144  * Test Routines
145  *****************************************************************************/
146
147 void BasicTest( FILE *p_cmd, FILE *p_res, int i_overlay ) {
148     printf( "Activating overlay..." );
149     SetVisibility( p_cmd, p_res, i_overlay, 1 );
150     printf( " done\n" );
151
152     printf( "Sweeping alpha..." );
153     for( int i_alpha = 0xFF; i_alpha >= -0xFF ; i_alpha -= 8 ) {
154         SetAlpha( p_cmd, p_res, i_overlay, abs( i_alpha ) );
155         usleep( 20000 );
156     }
157     SetAlpha( p_cmd, p_res, i_overlay, 255 );
158     printf( " done\n" );
159
160     printf( "Circle motion..." );
161     for( float f_theta = 0; f_theta <= 2 * M_PI ; f_theta += M_PI / 64.0 ) {
162         SetPosition( p_cmd, p_res, i_overlay,
163                      (int)( - cos( f_theta ) * 100.0 + 100.0 ),
164                      (int)( - sin( f_theta ) * 100.0 + 100.0 ) );
165         usleep( 20000 );
166     }
167     SetPosition( p_cmd, p_res, i_overlay, 0, 100 );
168     printf( " done\n" );
169
170     printf( "Atomic motion..." );
171     StartAtomic( p_cmd, p_res );
172     SetPosition( p_cmd, NULL, i_overlay, 200, 50 );
173     sleep( 1 );
174     SetPosition( p_cmd, NULL, i_overlay, 0, 0 );
175     EndAtomic( p_cmd, p_res );
176     CheckResult( p_res );
177     CheckResult( p_res );
178     printf( " done\n" );
179
180     sleep( 5 );
181 }
182
183 /*****************************************************************************
184  * main
185  *****************************************************************************/
186
187 int main( int i_argc, char *ppsz_argv[] ) {
188     if( i_argc != 3 ) {
189         printf( "Incorrect number of parameters.\n"
190                 "Usage is: %s command-fifo response-fifo\n", ppsz_argv[0] );
191         exit( -2 );
192     }
193
194     printf( "Creating shared memory for RGBA..." );
195     int i_shmRGBA = shmget( IPC_PRIVATE, WIDTH * HEIGHT * 4, S_IRWXU );
196     if( i_shmRGBA == -1 ) {
197         printf( " failed\n" );
198         exit( -1 );
199     }
200     printf( " done, ID is %d. Text...", i_shmRGBA );
201     int i_shmText = shmget( IPC_PRIVATE, TEXTSIZE, S_IRWXU );
202     if( i_shmText == -1 ) {
203         printf( " failed\n" );
204         exit( -1 );
205     }
206     printf( " done, ID is %d\n", i_shmText );
207
208     printf( "Attaching shared memory for RGBA..." );
209     p_imageRGBA = shmat( i_shmRGBA, NULL, 0 );
210     if( p_imageRGBA == -1 ) {
211         printf( " failed\n" );
212         exit( -1 );
213     }
214     printf( " done. Text..." );
215     p_text = shmat( i_shmText, NULL, 0 );
216     if( p_text == -1 ) {
217         printf( " failed\n" );
218         exit( -1 );
219     }
220     printf( " done\n" );
221
222     printf( "Queueing shared memory for destruction, RGBA..." );
223     if( shmctl( i_shmRGBA, IPC_RMID, 0 ) == -1 ) {
224         printf( " failed\n" );
225         exit( -1 );
226     }
227     printf( " done. Text..." );
228     if( shmctl( i_shmText, IPC_RMID, 0 ) == -1 ) {
229         printf( " failed\n" );
230         exit( -1 );
231     }
232     printf( " done\n" );
233
234     printf( "Generating data..." );
235     DataCreate();
236     printf( " done\n" );
237
238     printf( "Please make sure vlc is running.\n"
239             "You should append parameters similar to the following:\n"
240             "--sub-filter overlay --overlay-input %s --overlay-output %s\n",
241             ppsz_argv[1], ppsz_argv[2] );
242
243     printf( "Opening FIFOs..." );
244     FILE *p_cmd = fopen( ppsz_argv[1], "w" );
245     if( p_cmd == NULL ) {
246         printf( " failed\n" );
247         exit( -1 );
248     }
249     FILE *p_res = fopen( ppsz_argv[2], "r" );
250     if( p_res == NULL ) {
251         printf( " failed\n" );
252         exit( -1 );
253     }
254     printf( " done\n" );
255
256     int i_overlay_image = GenImage( p_cmd, p_res );
257     int i_overlay_text = GenImage( p_cmd, p_res );
258     DataSharedMem( p_cmd, p_res, i_overlay_image, WIDTH, HEIGHT, "RGBA",
259                    i_shmRGBA );
260     DataSharedMem( p_cmd, p_res, i_overlay_text, TEXTSIZE, 1, "TEXT",
261                    i_shmText );
262
263     BasicTest( p_cmd, p_res, i_overlay_image );
264     BasicTest( p_cmd, p_res, i_overlay_text );
265
266     DeleteImage( p_cmd, p_res, i_overlay_image );
267     DeleteImage( p_cmd, p_res, i_overlay_text );
268 }