]> git.sesse.net Git - vlc/blob - share/http-lua/js/mosaic.js
Mozilla : use \n
[vlc] / share / http-lua / 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_transcode_extra' ).value = ",sfilter=mosaic}:bridge-in{offset=100";
50     mosaic_size_change();
51
52     /* Force usage of transcode in sout */
53     document.getElementById( 'sout_vcodec_s' ).checked = 'checked';
54     disable( 'sout_vcodec_s' );
55     update_sout();
56 }
57
58 function mosaic_size_change()
59 {
60     var x,y;
61
62     var bg_width    = check_and_replace_int( "bg_width", "400" );
63     var bg_height   = check_and_replace_int( "bg_height", "300" );
64
65     mosaic_height   = check_and_replace_int( "mosaic_height", "100" );
66     mosaic_width    = check_and_replace_int( "mosaic_width", "100" );
67     mosaic_xoffset  = check_and_replace_int( "mosaic_xoffset", "10" );
68     mosaic_yoffset  = check_and_replace_int( "mosaic_yoffset", "10" );
69     mosaic_vborder  = check_and_replace_int( "mosaic_vborder", "5" );
70     mosaic_hborder  = check_and_replace_int( "mosaic_hborder", "10" );
71     mosaic_rows     = check_and_replace_int( "mosaic_rows", "1" );
72     mosaic_cols     = check_and_replace_int( "mosaic_cols", "1" );
73     
74     cell_width  = Math.floor((mosaic_width-(mosaic_cols-1)*mosaic_hborder)/mosaic_cols);
75     cell_height = Math.floor((mosaic_height-(mosaic_rows-1)*mosaic_vborder)/mosaic_rows);
76     
77     var mlayout = document.getElementById( "mosaic_layout" );
78
79     while( mlayout.hasChildNodes() )
80         mlayout.removeChild( mlayout.firstChild );
81
82     mlayout.style.width = bg_width + "px";
83     mlayout.style.height = bg_height + "px";
84     if( mosaic_cols && mosaic_rows )
85     {
86         var mdt = document.createElement( 'div' );
87         mdt.setAttribute( 'id',    'mosaic_dt'  );
88         setclass( mdt, 'mosaic_tbl' );
89         
90         mdt.style.width  = mosaic_width   + "px";
91         mdt.style.height = mosaic_height  + "px";
92         mdt.style.top    = mosaic_yoffset + "px";
93         mdt.style.left   = mosaic_xoffset + "px";
94
95         var mtable = document.createElement( 'table' );
96         mtable.setAttribute( 'id', 'mosaic_table' );
97         mtable.style.top    = "-" + mosaic_vborder + "px";
98         mtable.style.left   = "-" + mosaic_hborder + "px";
99         mtable.style.width  = (1*mosaic_width +2*mosaic_hborder)  + "px";
100         mtable.style.height = (1*mosaic_height+2*mosaic_vborder) + "px";
101         mtable.style.borderSpacing = mosaic_hborder + "px " +
102                                      mosaic_vborder + "px";
103
104         var mtbody = document.createElement( 'tbody' );
105
106         for( y = 0; y < mosaic_rows; y++ )
107         {
108             var mrow = document.createElement( 'tr' );
109             for( x = 0; x < mosaic_cols; x++ )
110             {
111                 var mcell = document.createElement( 'td' );
112                 setclass( mcell, 'mosaic_itm' );
113                 mcell.style.width  = cell_width  + "px";
114                 mcell.style.height = cell_height + "px";
115                 
116                 var id = x+'_'+y;
117                 var melt = create_button( cells[id] ? cells[id] : '?', 'mosaic_elt_choose(\"'+id+'\");' );
118                 melt.setAttribute( 'id', id );
119                 melt.setAttribute( 'title', 'Click to choose stream' );
120                 
121                 mcell.appendChild( melt );
122                 mrow.appendChild( mcell );
123             }
124             mtbody.appendChild( mrow );
125         }
126         mtable.appendChild( mtbody );
127         mdt.appendChild( mtable );
128         mlayout.appendChild( mdt );
129     }
130     mosaic_code_update();
131 }
132
133 function mosaic_add_input()
134 {
135     streams[ addunderscores( value('mosaic_input_name') ) ] =
136         value('mosaic_input');
137
138     mosaic_feedback( addunderscores( value('mosaic_input_name') ) + " ( " + value('mosaic_input') + " ) added to input list.", true );
139
140     var mlist = document.getElementById( "mosaic_list_content" );
141     while( mlist.hasChildNodes() )
142         mlist.removeChild( mlist.firstChild );
143     
144     for( var name in streams )
145     {
146         var mrl = streams[name];
147         
148         var minput = document.createElement( 'a' );
149         minput.setAttribute( 'href', 'javascript:mosaic_elt_select(\''+name+'\');');
150         minput.setAttribute( 'id', name );
151         minput.setAttribute( 'value', mrl );
152         
153         var minputtxt = document.createTextNode( name );
154
155         minput.appendChild( minputtxt );
156         mlist.appendChild( minput );
157         mlist.appendChild( document.createTextNode( " ( "+mrl+" )" ) );
158         mlist.appendChild( document.createElement( 'br' ) );
159     }
160 }
161
162 function mosaic_elt_select( id )
163 {
164     hide( 'mosaic_list' );
165     var ml = document.getElementById( 'mosaic_list' ).value;
166     if( ml )
167     {
168         document.getElementById( ml ).value = id;
169         cells[ ml ] = id;
170         mosaic_code_update();
171     }
172 }
173
174 function mosaic_elt_choose( id )
175 {
176     document.getElementById( 'mosaic_list' ).value = id;
177     show( 'mosaic_list' );
178 }
179
180 function mosaic_code_update()
181 {
182
183     var code = document.getElementById( 'mosaic_code' );
184     code.value =
185 "##################################\n"+
186 "## HTTP interface mosaic wizard ##\n"+
187 "##################################\n"+
188 "\n"+
189 "# Comment the following line if you don't want to reset your VLM configuration\n"+
190 "del all\n"+
191 "\n"+
192 "# Background options\n"+
193 "new   bg broadcast enabled\n"+
194 "setup bg input     " + sanitize_input( value( 'mosaic_bg_input' ) ) + "\n";
195     if( value( 'mosaic_output' ) )
196     {
197         code.value +=
198 "setup bg output    " + value( 'mosaic_output' )+ "\n";
199     }
200     var o = /.*transcode.*/;
201     if(! o.test( value( 'mosaic_output' ) ) )
202     {
203         code.value +=
204 "setup bg option    sub-filter=mosaic\n"+
205 "setup bg output    #bridge-in{offset=100}:display\n";
206     }
207     code.value+=
208 "\n"+
209 "# Mosaic options\n"+
210 "setup bg option mosaic-alpha="    + mosaic_alpha    + "\n"+
211 "setup bg option mosaic-height="   + mosaic_height   + "\n"+
212 "setup bg option mosaic-width="    + mosaic_width    + "\n"+
213 "setup bg option mosaic-align="    + mosaic_align    + "\n"+
214 "setup bg option mosaic-xoffset="  + mosaic_xoffset  + "\n"+
215 "setup bg option mosaic-yoffset="  + mosaic_yoffset  + "\n"+
216 "setup bg option mosaic-vborder="  + mosaic_vborder  + "\n"+
217 "setup bg option mosaic-hborder="  + mosaic_hborder  + "\n"+
218 "setup bg option mosaic-position=" + mosaic_position + "\n"+
219 "setup bg option mosaic-rows="     + mosaic_rows     + "\n"+
220 "setup bg option mosaic-cols="     + mosaic_cols     + "\n"+
221 "setup bg option mosaic-order=";
222     for( y = 0; y < mosaic_rows; y++ )
223     {
224         for( x = 0; x < mosaic_cols; x++ )
225         {
226             var id = x+'_'+y;
227             if( cells[id] )
228                 code.value += cells[id];
229             else
230                 code.value += '_';
231             if( y != mosaic_rows - 1 || x != mosaic_cols - 1 )
232                 code.value += ',';
233         }
234     }
235     code.value += "\n"+
236 "setup bg option mosaic-delay="    + mosaic_delay    + "\n"+
237 "setup bg option mosaic-keep-picture\n"+
238 "\n"+
239 "# Input options\n";
240     var x, y;
241     for( y = 0; y < mosaic_rows; y++ )
242     {
243         for( x = 0; x < mosaic_cols; x++ )
244         {
245             var id = x+'_'+y;
246             if( cells[id] )
247             {
248                 var s = cells[id];
249                 code.value +=
250 "new   " + s + " broadcast enabled\n"+
251 "setup " + s + " input     " + sanitize_input( streams[s] ) + "\n"+
252 "setup " + s + " output #duplicate{dst=mosaic-bridge{id=" + s + ",width="+cell_width+",height="+cell_height+"},select=video,dst=bridge-out{id="+(y*mosaic_cols+x)+"},select=audio}\n"+
253 "\n";
254             }
255         }
256     }
257     code.value +=
258 "# Launch everything\n"+
259 "control bg play\n";
260     for( y = 0; y < mosaic_rows; y++ )
261     {
262         for( x = 0; x < mosaic_cols; x++ )
263         {
264             var id = x+'_'+y;
265             if( cells[id] )
266             {
267                 var s = cells[id];
268                 code.value +=
269 "control " + s + " play\n";
270             }
271         }
272     }
273     code.value +=
274 "\n"+
275 "# end of mosaic batch\n";
276 }
277
278 function mosaic_batch( batch )
279 {
280     var i;
281     var commands = batch.split( '\n' );
282     for( i = 0; i < commands.length; i++ )
283     {
284         mosaic_cmd( commands[i] );
285     }
286 }
287
288 function mosaic_cmd( cmd )
289 {
290     loadXMLDoc( 'requests/vlm_cmd.xml?command='+cmd.replace(/\#/g, '%23'), parse_mosaic_cmd );
291 }
292
293 function parse_mosaic_cmd()
294 {
295     /* TODO */
296 }
297
298 function mosaic_stop()
299 {
300     var cmd;
301     cmd = "control bg stop\n";
302     var x,y;
303     for( y = 0; y < mosaic_rows; y++ )
304     {
305         for( x = 0; x < mosaic_cols; x++ )
306         {
307             var id = x+'_'+y;
308             if( cells[id] )
309             {
310                 var s = cells[id];
311                 cmd += "control " + s + " stop\n";
312             }
313         }
314     }
315     mosaic_batch( cmd );
316 }
317
318 function mosaic_feedback( msg, ok )
319 {
320     var f = document.getElementById( "mosaic_feedback" );
321     while( f.hasChildNodes() )
322         f.removeChild( f.firstChild );
323
324     f.style.fontWeight = "bold";
325     if( ok )
326         f.style.color = "#0f0";
327     else
328         f.style.color = "#f00";
329
330     var t = document.createTextNode( ( ok ? "Info: " : "Error: " ) + msg );
331     f.appendChild( t );
332
333 }