]> git.sesse.net Git - vlc/blob - share/http/js/mosaic.js
Code the mosaic wizard
[vlc] / share / http / js / mosaic.js
1 /*****************************************************************************
2  * mosaic.js: VLC media player web interface - Mosaic specific functions
3  *****************************************************************************
4  * Copyright (C) 2005-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- 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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /**********************************************************************
25  * 
26  *********************************************************************/
27
28 var mosaic_alpha    = 255;
29 var mosaic_height   = 0;
30 var mosaic_width    = 0;
31 var mosaic_align    = 5;
32 var mosaic_xoffset  = 0;
33 var mosaic_yoffset  = 0;
34 var mosaic_vborder  = 0;
35 var mosaic_hborder  = 0;
36 var mosaic_position = 1;
37 var mosaic_rows     = 0;
38 var mosaic_cols     = 0;
39 var mosaic_delay    = 0;
40
41 var cell_width  = 0;
42 var cell_height = 0;
43
44 var streams = Object;
45 var cells   = Object;
46
47 function mosaic_init()
48 {
49     document.getElementById( 'sout_extra' ).value = ",sfilter=mosaic";
50     mosaic_size_change();
51 }
52
53 function mosaic_size_change()
54 {
55     var x,y;
56
57     var bg_width    = check_and_replace_int( "bg_width", "400" );
58     var bg_height   = check_and_replace_int( "bg_height", "300" );
59
60     mosaic_height   = check_and_replace_int( "mosaic_height", "100" );
61     mosaic_width    = check_and_replace_int( "mosaic_width", "100" );
62     mosaic_xoffset  = check_and_replace_int( "mosaic_xoffset", "10" );
63     mosaic_yoffset  = check_and_replace_int( "mosaic_yoffset", "10" );
64     mosaic_vborder  = check_and_replace_int( "mosaic_vborder", "5" );
65     mosaic_hborder  = check_and_replace_int( "mosaic_hborder", "10" );
66     mosaic_rows     = check_and_replace_int( "mosaic_rows", "1" );
67     mosaic_cols     = check_and_replace_int( "mosaic_cols", "1" );
68     
69     cell_width  = Math.floor((mosaic_width-(mosaic_cols-1)*mosaic_hborder)/mosaic_cols);
70     cell_height = Math.floor((mosaic_height-(mosaic_rows-1)*mosaic_vborder)/mosaic_rows);
71     
72     var mlayout = document.getElementById( "mosaic_layout" );
73
74     /* TODO: keep 'common' cells when resizing */
75     while( mlayout.hasChildNodes() )
76         mlayout.removeChild( mlayout.firstChild );
77
78     mlayout.style.width = bg_width + "px";
79     mlayout.style.height = bg_height + "px";
80     if( mosaic_cols && mosaic_rows )
81     {
82         var mdt = document.createElement( 'div' );
83         mdt.setAttribute( 'id',    'mosaic_dt'  );
84         mdt.setAttribute( 'class', 'mosaic_tbl' );
85         
86         mdt.style.width  = mosaic_width   + "px";
87         mdt.style.height = mosaic_height  + "px";
88         mdt.style.top    = mosaic_yoffset + "px";
89         mdt.style.left   = mosaic_xoffset + "px";
90
91         var mtable = document.createElement( 'table' );
92         mtable.setAttribute( 'id', 'mosaic_table' );
93         mtable.style.top    = "-" + mosaic_vborder + "px";
94         mtable.style.left   = "-" + mosaic_hborder + "px";
95         mtable.style.width  = (1*mosaic_width +2*mosaic_hborder)  + "px";
96         mtable.style.height = (1*mosaic_height+2*mosaic_vborder) + "px";
97         mtable.style.borderSpacing = mosaic_hborder + "px " +
98                                      mosaic_vborder + "px";
99
100         for( y = 0; y < mosaic_rows; y++ )
101         {
102             var mrow = document.createElement( 'tr' );
103             for( x = 0; x < mosaic_cols; x++ )
104             {
105                 var mcell = document.createElement( 'td' );
106                 mcell.setAttribute( 'class', 'mosaic_itm' );
107                 mcell.style.width  = cell_width  + "px";
108                 mcell.style.height = cell_height + "px";
109                 
110                 var melt = document.createElement( 'input' );
111                 melt.setAttribute( 'type', 'button' );
112                 var id = x+'_'+y;
113                 melt.setAttribute( 'id', id );
114                 melt.setAttribute( 'onclick', 'mosaic_elt_choose(\''+id+'\');' );
115                 if( cells[id] )
116                     melt.setAttribute( 'value', cells[id] );
117                 else
118                     melt.setAttribute( 'value', '?' );
119                 melt.setAttribute( 'title', 'Click to choose stream' );
120                 
121                 mcell.appendChild( melt );
122                 mrow.appendChild( mcell );
123             }
124             mtable.appendChild( mrow );
125         }
126         mdt.appendChild( mtable );
127         mlayout.appendChild( mdt );
128     }
129     mosaic_code_update();
130 }
131
132 function mosaic_add_input()
133 {
134     streams[ addunderscores( value('mosaic_input_name') ) ] =
135         value('mosaic_input');
136
137     var mlist = document.getElementById( "mosaic_list_content" );
138     while( mlist.hasChildNodes() )
139         mlist.removeChild( mlist.firstChild );
140     
141     for( var name in streams )
142     {
143         var mrl = streams[name];
144         
145         var minput = document.createElement( 'a' );
146         minput.setAttribute( 'href', 'javascript:mosaic_elt_select(\''+name+'\');');
147         minput.setAttribute( 'id', name );
148         minput.setAttribute( 'value', mrl );
149         minput.setAttribute( 'title', mrl );
150         
151         var minputtxt = document.createTextNode( name );
152
153         minput.appendChild( minputtxt );
154         mlist.appendChild( minput );
155         mlist.appendChild( document.createElement( 'br' ) );
156     }
157 }
158
159 function mosaic_elt_select( id )
160 {
161     hide( 'mosaic_list' );
162     document.getElementById( document.getElementById( 'mosaic_list' ).value ).value = id;
163     cells[ document.getElementById( 'mosaic_list' ).value ] = id;
164     mosaic_code_update();
165 }
166
167 function mosaic_elt_choose( id )
168 {
169     document.getElementById( 'mosaic_list' ).value = id;
170     show( 'mosaic_list' );
171 }
172
173 function mosaic_code_update()
174 {
175
176     var code = document.getElementById( 'mosaic_code' );
177     code.value =
178 "##################################\n"+
179 "## HTTP interface mosaic wizard ##\n"+
180 "##################################\n"+
181 "\n"+
182 "# Comment the following line if you don't want to reset you VLM configuration\n"+
183 "del all\n"+
184 "\n"+
185 "# Background options\n"+
186 "new   bg broadcast enabled\n"+
187 "setup bg input     " + value( 'mosaic_bg_input' ) + "\n";
188     if( value( 'mosaic_output' ) )
189     {
190         code.value +=
191 "setup bg output    " + value( 'mosaic_output' )+ "\n";
192     }
193     var o = /.*transcode.*/;
194     if(! o.test( value( 'mosaic_output' ) ) )
195     {
196         code.value +=
197 "setup bg option    sub-filter=mosaic\n";
198     }
199     code.value+=
200 "\n"+
201 "# Mosaic options\n"+
202 "setup bg option mosaic-alpha="    + mosaic_alpha    + "\n"+
203 "setup bg option mosaic-height="   + mosaic_height   + "\n"+
204 "setup bg option mosaic-width="    + mosaic_width    + "\n"+
205 "setup bg option mosaic-align="    + mosaic_align    + "\n"+
206 "setup bg option mosaic-xoffset="  + mosaic_xoffset  + "\n"+
207 "setup bg option mosaic-yoffset="  + mosaic_yoffset  + "\n"+
208 "setup bg option mosaic-vborder="  + mosaic_vborder  + "\n"+
209 "setup bg option mosaic-hborder="  + mosaic_hborder  + "\n"+
210 "setup bg option mosaic-position=" + mosaic_position + "\n"+
211 "setup bg option mosaic-rows="     + mosaic_rows     + "\n"+
212 "setup bg option mosaic-cols="     + mosaic_cols     + "\n"+
213 "setup bg option mosaic-order=";
214     for( y = 0; y < mosaic_rows; y++ )
215     {
216         for( x = 0; x < mosaic_cols; x++ )
217         {
218             var id = x+'_'+y;
219             if( cells[id] )
220                 code.value += cells[id];
221             else
222                 code.value += '_';
223             if( y != mosaic_rows - 1 || x != mosaic_cols - 1 )
224                 code.value += ',';
225         }
226     }
227     code.value += "\n"+
228 "setup bg option mosaic-delay="    + mosaic_delay    + "\n"+
229 "setup bg option mosaic-keep-picture\n"+
230 "\n"+
231 "# Input options\n";
232     var x, y;
233     for( y = 0; y < mosaic_rows; y++ )
234     {
235         for( x = 0; x < mosaic_cols; x++ )
236         {
237             var id = x+'_'+y;
238             if( cells[id] )
239             {
240                 var s = cells[id];
241                 code.value +=
242 "new   " + s + " broadcast enabled\n"+
243 "setup " + s + " input     " + streams[s] + "\n"+
244 "setup " + s + " output #duplicate{dst=mosaic-bridge{id=" + s + ",width="+cell_width+",height="+cell_height+"},select=video,dst=bridge-out,select=audio}\n"+
245 "\n";
246             }
247         }
248     }
249     code.value +=
250 "# Launch everything\n"+
251 "control bg play\n";
252     for( y = 0; y < mosaic_rows; y++ )
253     {
254         for( x = 0; x < mosaic_cols; x++ )
255         {
256             var id = x+'_'+y;
257             if( cells[id] )
258             {
259                 var s = cells[id];
260                 code.value +=
261 "control " + s + " play\n";
262             }
263         }
264     }
265     code.value +=
266 "\n"+
267 "# end of mosaic batch\n";
268 }
269
270 function mosaic_batch( batch )
271 {
272     var i;
273     var commands = batch.split( '\n' );
274     for( i = 0; i < commands.length; i++ )
275     {
276         mosaic_cmd( commands[i] );
277     }
278 }
279
280 function mosaic_cmd( cmd )
281 {
282     loadXMLDoc( 'requests/vlm_cmd.xml?command='+cmd.replace(/\#/g, '%23'), parse_mosaic_cmd );
283 }
284
285 function parse_mosaic_cmd()
286 {
287     /* TODO */
288 }
289
290 function mosaic_stop()
291 {
292     var cmd;
293     cmd = "control bg stop\n";
294     var x,y;
295     for( y = 0; y < mosaic_rows; y++ )
296     {
297         for( x = 0; x < mosaic_cols; x++ )
298         {
299             var id = x+'_'+y;
300             if( cells[id] )
301             {
302                 var s = cells[id];
303                 cmd += "control " + s + " stop\n";
304             }
305         }
306     }
307     mosaic_batch( cmd );
308 }