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