]> git.sesse.net Git - vlc/blob - extras/misc/overlay-test.c
Patch from Søren Bøg <avacore@videolan.org> GSoC 2007
[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 <string.h>
30 #include <math.h>
31
32 #include <sys/fcntl.h>
33 #include <sys/ipc.h>
34 #include <sys/shm.h>
35 #include <unistd.h>
36
37 /*****************************************************************************
38  * Images
39  *****************************************************************************/
40
41 #define WIDTH 128
42 #define HEIGHT 128
43
44 char *p_imageRGBA;
45
46 void ImagesCreate( void ) {
47     char *p_data = p_imageRGBA;
48     for( size_t i = 0; i < HEIGHT; ++i ) {
49         for( size_t j = 0; j < HEIGHT; ++j ) {
50             *(p_data++) = i * 4 & 0xFF;
51             *(p_data++) = 0xFF;
52             *(p_data++) = 0x00;
53             *(p_data++) = j * 4 & 0xFF;
54         }
55     }
56 }
57
58 /*****************************************************************************
59  * I/O Helpers
60  *****************************************************************************/
61
62 int IsFailure( char *psz_text ) {
63     return strncmp( psz_text, "SUCCESS:", 8 );
64 }
65
66 /*****************************************************************************
67  * main
68  *****************************************************************************/
69
70 int main( int i_argc, char *ppsz_argv[] ) {
71     if( i_argc != 3 ) {
72         printf( "Incorrect number of parameters.\n"
73                 "Usage is: %s command-fifo response-fifo\n", ppsz_argv[0] );
74         exit( -2 );
75     }
76
77     printf( "Creating shared memory..." );
78     int i_shmRGBA = shmget( IPC_PRIVATE, WIDTH * HEIGHT * 4, S_IRWXU );
79     if( i_shmRGBA == -1 ) {
80         printf( " failed\n" );
81         exit( -1 );
82     }
83     printf( " done. ID is %d\n", i_shmRGBA );
84
85     printf( "Attaching shared memory..." );
86     p_imageRGBA = shmat( i_shmRGBA, NULL, 0 );
87     if( p_imageRGBA == -1 ) {
88         printf( " failed\n" );
89         exit( -1 );
90     }
91     printf( " done\n" );
92
93     printf( "Queueing shared memory for destruction..." );
94     if( shmctl( i_shmRGBA, IPC_RMID, 0 ) == -1 ) {
95         printf( " failed\n" );
96         exit( -1 );
97     }
98     printf( " done\n" );
99
100     printf( "Generating images..." );
101     ImagesCreate();
102     printf( " done\n" );
103
104     printf( "Please make sure vlc is running.\n"
105             "You should append parameters similar to the following:\n"
106             "--sub-filter overlay --overlay-input %s --overlay-output %s\n",
107             ppsz_argv[1], ppsz_argv[2] );
108
109     printf( "Opening FIFOs..." );
110     FILE *f_cmd = fopen( ppsz_argv[1], "w" );
111     if( f_cmd == NULL ) {
112         printf( " failed\n" );
113         exit( -1 );
114     }
115     FILE *f_res = fopen( ppsz_argv[2], "r" );
116     if( f_res == NULL ) {
117         printf( " failed\n" );
118         exit( -1 );
119     }
120     printf( " done\n" );
121
122     printf( "Getting an overlay..." );
123     int i_overlay;
124     char psz_resp[9];
125     fprintf( f_cmd, "GenImage\n" );
126     fflush( f_cmd );
127     fscanf( f_res, "%8s", &psz_resp );
128     if( IsFailure( psz_resp ) ) {
129         printf( " failed\n" );
130         exit( -1 );
131     }
132     fscanf( f_res, "%d", &i_overlay );
133     printf( " done. Overlay is %d\n", i_overlay );
134
135     printf( "Sending data..." );
136     fprintf( f_cmd, "DataSharedMem %d %d %d RGBA %d\n", i_overlay, WIDTH,
137              HEIGHT, i_shmRGBA );
138     fflush( f_cmd );
139     fscanf( f_res, "%8s", &psz_resp );
140     if( IsFailure( psz_resp ) ) {
141         printf( " failed\n" );
142         exit( -1 );
143     }
144     printf( " done\n" );
145
146     printf( "Activating overlay..." );
147     fprintf( f_cmd, "SetVisibility %d 1\n", i_overlay );
148     fflush( f_cmd );
149     fscanf( f_res, "%8s", &psz_resp );
150     if( IsFailure( psz_resp ) ) {
151         printf( " failed\n" );
152         exit( -1 );
153     }
154     printf( " done\n" );
155
156     printf( "Sweeping alpha..." );
157     for( int i_alpha = 0xFF; i_alpha >= -0xFF ; i_alpha -= 8 ) {
158         fprintf( f_cmd, "SetAlpha %d %d\n", i_overlay, abs( i_alpha ) );
159         fflush( f_cmd );
160         fscanf( f_res, "%8s", &psz_resp );
161         if( IsFailure( psz_resp ) ) {
162             printf( " failed\n" );
163             exit( -1 );
164         }
165         usleep( 20000 );
166     }
167     fprintf( f_cmd, "SetAlpha %d 255\n", i_overlay );
168     fflush( f_cmd );
169     fscanf( f_res, "%8s", &psz_resp );
170     if( IsFailure( psz_resp ) ) {
171         printf( " failed\n" );
172         exit( -1 );
173     }
174     printf( " done\n" );
175
176     printf( "Circle motion..." );
177     for( float f_theta = 0; f_theta <= 2 * M_PI ; f_theta += M_PI / 64.0 ) {
178         fprintf( f_cmd, "SetPosition %d %d %d\n", i_overlay,
179                  (int)( - cos( f_theta ) * 100.0 + 100.0 ),
180                  (int)( - sin( f_theta ) * 100.0 + 100.0 ) );
181         fflush( f_cmd );
182         fscanf( f_res, "%8s", &psz_resp );
183         if( IsFailure( psz_resp ) ) {
184             printf( " failed\n" );
185             exit( -1 );
186         }
187         usleep( 20000 );
188     }
189     fprintf( f_cmd, "SetPosition %d 0 0\n", i_overlay );
190     fflush( f_cmd );
191     fscanf( f_res, "%8s", &psz_resp );
192     if( IsFailure( psz_resp ) ) {
193         printf( " failed\n" );
194         exit( -1 );
195     }
196     printf( " done\n" );
197
198     printf( "Removing image..." );
199     fprintf( f_cmd, "DeleteImage %d\n", i_overlay );
200     fflush( f_cmd );
201     fscanf( f_res, "%8s", &psz_resp );
202     if( IsFailure( psz_resp ) ) {
203         printf( " failed\n" );
204         exit( -1 );
205     }
206     printf( " done\n" );
207 }