1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teuli�e <ipkiss@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
27 #include <aout_internal.h>
29 #include "vlcproc.hpp"
30 #include "os_factory.hpp"
31 #include "os_timer.hpp"
32 #include "var_manager.hpp"
34 #include "window_manager.hpp"
35 #include "../commands/async_queue.hpp"
36 #include "../commands/cmd_change_skin.hpp"
37 #include "../commands/cmd_show_window.hpp"
38 #include "../commands/cmd_quit.hpp"
39 #include "../commands/cmd_resize.hpp"
40 #include "../commands/cmd_vars.hpp"
41 #include "../utils/var_bool.hpp"
45 VlcProc *VlcProc::instance( intf_thread_t *pIntf )
47 if( pIntf->p_sys->p_vlcProc == NULL )
49 pIntf->p_sys->p_vlcProc = new VlcProc( pIntf );
52 return pIntf->p_sys->p_vlcProc;
56 void VlcProc::destroy( intf_thread_t *pIntf )
58 if( pIntf->p_sys->p_vlcProc )
60 delete pIntf->p_sys->p_vlcProc;
61 pIntf->p_sys->p_vlcProc = NULL;
66 VlcProc::VlcProc( intf_thread_t *pIntf ): SkinObject( pIntf ),
67 m_varVoutSize( pIntf ), m_varEqBands( pIntf ),
68 m_pVout( NULL ), m_pAout( NULL ), m_cmdManage( this )
70 // Create a timer to poll the status of the vlc
71 OSFactory *pOsFactory = OSFactory::instance( pIntf );
72 m_pTimer = pOsFactory->createOSTimer( m_cmdManage );
73 m_pTimer->start( 100, false );
75 // Create and register VLC variables
76 VarManager *pVarManager = VarManager::instance( getIntf() );
78 #define REGISTER_VAR( var, type, name ) \
79 var = VariablePtr( new type( getIntf() ) ); \
80 pVarManager->registerVar( var, name );
81 REGISTER_VAR( m_cPlaylist, Playlist, "playlist" )
82 pVarManager->registerVar( getPlaylistVar().getPositionVarPtr(),
84 REGISTER_VAR( m_cVarRandom, VarBoolImpl, "playlist.isRandom" )
85 REGISTER_VAR( m_cVarLoop, VarBoolImpl, "playlist.isLoop" )
86 REGISTER_VAR( m_cVarRepeat, VarBoolImpl, "playlist.isRepeat" )
87 REGISTER_VAR( m_cPlaytree, Playtree, "playtree" )
88 pVarManager->registerVar( getPlaytreeVar().getPositionVarPtr(),
90 pVarManager->registerVar( m_cVarRandom, "playtree.isRandom" );
91 pVarManager->registerVar( m_cVarLoop, "playtree.isLoop" );
92 pVarManager->registerVar( m_cVarRepeat, "playtree.isRepeat" );
93 REGISTER_VAR( m_cVarTime, StreamTime, "time" )
94 REGISTER_VAR( m_cVarVolume, Volume, "volume" )
95 REGISTER_VAR( m_cVarMute, VarBoolImpl, "vlc.isMute" )
96 REGISTER_VAR( m_cVarPlaying, VarBoolImpl, "vlc.isPlaying" )
97 REGISTER_VAR( m_cVarStopped, VarBoolImpl, "vlc.isStopped" )
98 REGISTER_VAR( m_cVarPaused, VarBoolImpl, "vlc.isPaused" )
99 REGISTER_VAR( m_cVarSeekable, VarBoolImpl, "vlc.isSeekable" )
100 REGISTER_VAR( m_cVarEqualizer, VarBoolImpl, "equalizer.isEnabled" )
101 REGISTER_VAR( m_cVarEqPreamp, EqualizerPreamp, "equalizer.preamp" )
103 m_cVarStreamName = VariablePtr( new VarText( getIntf(), false ) );
104 pVarManager->registerVar( m_cVarStreamName, "streamName" );
105 m_cVarStreamURI = VariablePtr( new VarText( getIntf(), false ) );
106 pVarManager->registerVar( m_cVarStreamURI, "streamURI" );
108 // Register the equalizer bands
109 for( int i = 0; i < EqualizerBands::kNbBands; i++)
112 ss << "equalizer.band(" << i << ")";
113 pVarManager->registerVar( m_varEqBands.getBand( i ), ss.str() );
117 // The object variable callbacks are called from other VLC threads,
118 // so they must put commands in the queue and NOT do anything else
119 // (X11 calls are not reentrant)
121 // Called when the playlist changes
122 var_AddCallback( pIntf->p_sys->p_playlist, "intf-change",
123 onIntfChange, this );
124 // Called when a playlist item is added
125 // TODO: properly handle item-append
126 var_AddCallback( pIntf->p_sys->p_playlist, "item-append",
127 onIntfChange, this );
128 // Called when a playlist item is deleted
129 // TODO: properly handle item-deleted
130 var_AddCallback( pIntf->p_sys->p_playlist, "item-deleted",
131 onIntfChange, this );
132 // Called when the "interface shower" wants us to show the skin
133 var_AddCallback( pIntf->p_sys->p_playlist, "intf-show",
135 // Called when the current played item changes
136 var_AddCallback( pIntf->p_sys->p_playlist, "playlist-current",
137 onPlaylistChange, this );
138 // Called when a playlist item changed
139 var_AddCallback( pIntf->p_sys->p_playlist, "item-change",
140 onItemChange, this );
141 // Called when our skins2 demux wants us to load a new skin
142 var_AddCallback( pIntf, "skin-to-load", onSkinToLoad, this );
144 // Callbacks for vout requests
145 getIntf()->pf_request_window = &getWindow;
146 getIntf()->pf_release_window = &releaseWindow;
147 getIntf()->pf_control_window = &controlWindow;
149 getIntf()->p_sys->p_input = NULL;
157 if( getIntf()->p_sys->p_input )
159 vlc_object_release( getIntf()->p_sys->p_input );
162 // Callbacks for vout requests
163 getIntf()->pf_request_window = NULL;
164 getIntf()->pf_release_window = NULL;
165 getIntf()->pf_control_window = NULL;
167 var_DelCallback( getIntf()->p_sys->p_playlist, "intf-change",
168 onIntfChange, this );
169 var_DelCallback( getIntf()->p_sys->p_playlist, "item-append",
170 onIntfChange, this );
171 var_DelCallback( getIntf()->p_sys->p_playlist, "item-deleted",
172 onIntfChange, this );
173 var_DelCallback( getIntf()->p_sys->p_playlist, "intf-show",
175 var_DelCallback( getIntf()->p_sys->p_playlist, "playlist-current",
176 onPlaylistChange, this );
177 var_DelCallback( getIntf()->p_sys->p_playlist, "item-change",
178 onItemChange, this );
179 var_DelCallback( getIntf(), "skin-to-load", onSkinToLoad, this );
183 void VlcProc::registerVoutWindow( void *pVoutWindow )
185 m_handleSet.insert( pVoutWindow );
186 // Reparent the vout window
189 if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
190 vout_Control( m_pVout, VOUT_CLOSE );
195 void VlcProc::unregisterVoutWindow( void *pVoutWindow )
197 m_handleSet.erase( pVoutWindow );
201 void VlcProc::dropVout()
205 if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
206 vout_Control( m_pVout, VOUT_CLOSE );
212 void VlcProc::manage()
214 // Did the user requested to quit vlc ?
215 if( getIntf()->b_die || getIntf()->p_vlc->b_die )
217 CmdQuit *pCmd = new CmdQuit( getIntf() );
218 AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
219 pQueue->push( CmdGenericPtr( pCmd ) );
222 // Get the VLC variables
223 StreamTime *pTime = (StreamTime*)m_cVarTime.get();
224 VarBoolImpl *pVarPlaying = (VarBoolImpl*)m_cVarPlaying.get();
225 VarBoolImpl *pVarStopped = (VarBoolImpl*)m_cVarStopped.get();
226 VarBoolImpl *pVarPaused = (VarBoolImpl*)m_cVarPaused.get();
227 VarBoolImpl *pVarSeekable = (VarBoolImpl*)m_cVarSeekable.get();
228 VarBoolImpl *pVarRandom = (VarBoolImpl*)m_cVarRandom.get();
229 VarBoolImpl *pVarLoop = (VarBoolImpl*)m_cVarLoop.get();
230 VarBoolImpl *pVarRepeat = (VarBoolImpl*)m_cVarRepeat.get();
232 // Refresh audio variables
236 if( getIntf()->p_sys->p_input == NULL )
238 getIntf()->p_sys->p_input = (input_thread_t *)vlc_object_find(
239 getIntf(), VLC_OBJECT_INPUT, FIND_ANYWHERE );
241 else if( getIntf()->p_sys->p_input->b_dead )
243 vlc_object_release( getIntf()->p_sys->p_input );
244 getIntf()->p_sys->p_input = NULL;
247 input_thread_t *pInput = getIntf()->p_sys->p_input;
249 if( pInput && !pInput->b_die )
251 // Refresh time variables
253 var_Get( pInput, "position", &pos );
254 pTime->set( pos.f_float, false );
256 // Get the status of the playlist
257 playlist_status_t status =
258 getIntf()->p_sys->p_playlist->status.i_status;
260 pVarPlaying->set( status == PLAYLIST_RUNNING );
261 pVarStopped->set( status == PLAYLIST_STOPPED );
262 pVarPaused->set( status == PLAYLIST_PAUSED );
264 pVarSeekable->set( pos.f_float != 0.0 );
268 pVarPlaying->set( false );
269 pVarPaused->set( false );
270 pVarStopped->set( true );
271 pVarSeekable->set( false );
272 pTime->set( 0, false );
275 // Refresh the random variable
277 var_Get( getIntf()->p_sys->p_playlist, "random", &val );
278 pVarRandom->set( val.b_bool != 0 );
280 // Refresh the loop variable
281 var_Get( getIntf()->p_sys->p_playlist, "loop", &val );
282 pVarLoop->set( val.b_bool != 0 );
284 // Refresh the repeat variable
285 var_Get( getIntf()->p_sys->p_playlist, "repeat", &val );
286 pVarRepeat->set( val.b_bool != 0 );
290 void VlcProc::CmdManage::execute()
292 // Just forward to VlcProc
297 void VlcProc::refreshAudio()
299 char *pFilters = NULL;
301 // Check if the audio output has changed
302 aout_instance_t *pAout = (aout_instance_t *)vlc_object_find( getIntf(),
303 VLC_OBJECT_AOUT, FIND_ANYWHERE );
306 if( pAout != m_pAout )
308 // Register the equalizer callbacks
309 if( !var_AddCallback( pAout, "equalizer-bands",
310 onEqBandsChange, this ) &&
311 !var_AddCallback( pAout, "equalizer-preamp",
312 onEqPreampChange, this ) )
315 //char * psz_bands = var_GetString( p_aout, "equalizer-bands" );
318 // Get the audio filters
319 pFilters = var_GetString( pAout, "audio-filter" );
320 vlc_object_release( pAout );
324 // Get the audio filters
325 pFilters = config_GetPsz( getIntf(), "audio-filter" );
328 // Refresh sound volume
329 audio_volume_t volume;
330 aout_VolumeGet( getIntf(), &volume );
331 Volume *pVolume = (Volume*)m_cVarVolume.get();
332 pVolume->set( (double)volume * 2.0 / AOUT_VOLUME_MAX );
334 // Set the mute variable
335 VarBoolImpl *pVarMute = (VarBoolImpl*)m_cVarMute.get();
336 pVarMute->set( volume == 0 );
338 // Refresh the equalizer variable
339 VarBoolImpl *pVarEqualizer = (VarBoolImpl*)m_cVarEqualizer.get();
340 pVarEqualizer->set( pFilters && strstr( pFilters, "equalizer" ) );
344 int VlcProc::onIntfChange( vlc_object_t *pObj, const char *pVariable,
345 vlc_value_t oldVal, vlc_value_t newVal,
348 VlcProc *pThis = (VlcProc*)pParam;
350 // Update the stream variable
351 playlist_t *p_playlist = (playlist_t*)pObj;
352 pThis->updateStreamName(p_playlist);
354 // Create a playlist notify command
355 CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
356 // Create a playtree notify command
357 CmdPlaytreeChanged *pCmdTree = new CmdPlaytreeChanged( pThis->getIntf() );
359 // Push the command in the asynchronous command queue
360 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
361 pQueue->push( CmdGenericPtr( pCmd ) );
362 pQueue->push( CmdGenericPtr( pCmdTree ) );
368 int VlcProc::onIntfShow( vlc_object_t *pObj, const char *pVariable,
369 vlc_value_t oldVal, vlc_value_t newVal,
374 VlcProc *pThis = (VlcProc*)pParam;
376 // Create a raise all command
377 CmdRaiseAll *pCmd = new CmdRaiseAll( pThis->getIntf(),
378 pThis->getIntf()->p_sys->p_theme->getWindowManager() );
380 // Push the command in the asynchronous command queue
381 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
382 pQueue->push( CmdGenericPtr( pCmd ) );
389 int VlcProc::onItemChange( vlc_object_t *pObj, const char *pVariable,
390 vlc_value_t oldVal, vlc_value_t newVal,
393 VlcProc *pThis = (VlcProc*)pParam;
395 // Update the stream variable
396 playlist_t *p_playlist = (playlist_t*)pObj;
397 pThis->updateStreamName(p_playlist);
399 // Create a playlist notify command
400 // TODO: selective update
401 CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
402 // Create a playtree notify command
403 CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(),
406 // Push the command in the asynchronous command queue
407 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
408 pQueue->push( CmdGenericPtr( pCmd ) );
409 pQueue->push( CmdGenericPtr( pCmdTree ), false );
415 int VlcProc::onPlaylistChange( vlc_object_t *pObj, const char *pVariable,
416 vlc_value_t oldVal, vlc_value_t newVal,
419 VlcProc *pThis = (VlcProc*)pParam;
421 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
423 // Update the stream variable
424 playlist_t *p_playlist = (playlist_t*)pObj;
425 pThis->updateStreamName(p_playlist);
427 // Create a playlist notify command
428 // TODO: selective update
429 CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
430 // Create a playtree notify command
431 CmdPlaytreeChanged *pCmdTree = new CmdPlaytreeChanged( pThis->getIntf() );
433 // Push the command in the asynchronous command queue
434 pQueue->push( CmdGenericPtr( pCmd ) );
435 pQueue->push( CmdGenericPtr( pCmdTree ) );
441 int VlcProc::onSkinToLoad( vlc_object_t *pObj, const char *pVariable,
442 vlc_value_t oldVal, vlc_value_t newVal,
445 VlcProc *pThis = (VlcProc*)pParam;
447 // Create a playlist notify command
448 CmdChangeSkin *pCmd =
449 new CmdChangeSkin( pThis->getIntf(), newVal.psz_string );
451 // Push the command in the asynchronous command queue
452 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
453 pQueue->push( CmdGenericPtr( pCmd ) );
459 void VlcProc::updateStreamName( playlist_t *p_playlist )
461 if( p_playlist->p_input )
463 VarText &rStreamName = getStreamNameVar();
464 VarText &rStreamURI = getStreamURIVar();
465 // XXX: we should not need to access p_input->psz_source directly, a
466 // getter should be provided by VLC core
467 string name = p_playlist->p_input->input.p_item->psz_name;
468 // XXX: This should be done in VLC core, not here...
469 // Remove path information if any
470 OSFactory *pFactory = OSFactory::instance( getIntf() );
471 string::size_type pos = name.rfind( pFactory->getDirSeparator() );
472 if( pos != string::npos )
474 name = name.substr( pos + 1, name.size() - pos + 1 );
476 UString srcName( getIntf(), name.c_str() );
477 UString srcURI( getIntf(),
478 p_playlist->p_input->input.p_item->psz_uri );
480 // Create commands to update the stream variables
481 CmdSetText *pCmd1 = new CmdSetText( getIntf(), rStreamName, srcName );
482 CmdSetText *pCmd2 = new CmdSetText( getIntf(), rStreamURI, srcURI );
483 // Push the commands in the asynchronous command queue
484 AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
485 pQueue->push( CmdGenericPtr( pCmd1 ), false );
486 pQueue->push( CmdGenericPtr( pCmd2 ), false );
491 void *VlcProc::getWindow( intf_thread_t *pIntf, vout_thread_t *pVout,
492 int *pXHint, int *pYHint,
493 unsigned int *pWidthHint,
494 unsigned int *pHeightHint )
496 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
497 pThis->m_pVout = pVout;
498 if( pThis->m_handleSet.empty() )
504 // Get the window handle
505 void *pWindow = *pThis->m_handleSet.begin();
506 // Post a resize vout command
507 CmdResizeVout *pCmd = new CmdResizeVout( pThis->getIntf(), pWindow,
508 *pWidthHint, *pHeightHint );
509 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
510 pQueue->push( CmdGenericPtr( pCmd ) );
516 void VlcProc::releaseWindow( intf_thread_t *pIntf, void *pWindow )
518 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
519 pThis->m_pVout = NULL;
523 int VlcProc::controlWindow( intf_thread_t *pIntf, void *pWindow,
524 int query, va_list args )
526 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
534 unsigned int i_width = va_arg( args, unsigned int );
535 unsigned int i_height = va_arg( args, unsigned int );
536 if( !i_width ) i_width = pThis->m_pVout->i_window_width;
537 if( !i_height ) i_height = pThis->m_pVout->i_window_height;
539 // Post a resize vout command
540 CmdResizeVout *pCmd =
541 new CmdResizeVout( pThis->getIntf(), pWindow,
543 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
544 pQueue->push( CmdGenericPtr( pCmd ) );
549 msg_Dbg( pIntf, "control query not supported" );
557 int VlcProc::onEqBandsChange( vlc_object_t *pObj, const char *pVariable,
558 vlc_value_t oldVal, vlc_value_t newVal,
561 VlcProc *pThis = (VlcProc*)pParam;
563 // Post a set equalizer bands command
564 CmdSetEqBands *pCmd = new CmdSetEqBands( pThis->getIntf(),
567 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
568 pQueue->push( CmdGenericPtr( pCmd ) );
574 int VlcProc::onEqPreampChange( vlc_object_t *pObj, const char *pVariable,
575 vlc_value_t oldVal, vlc_value_t newVal,
578 VlcProc *pThis = (VlcProc*)pParam;
579 EqualizerPreamp *pVarPreamp = (EqualizerPreamp*)(pThis->m_cVarEqPreamp.get());
581 // Post a set preamp command
582 CmdSetEqPreamp *pCmd = new CmdSetEqPreamp( pThis->getIntf(), *pVarPreamp,
583 (newVal.f_float + 20.0) / 40.0 );
584 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
585 pQueue->push( CmdGenericPtr( pCmd ) );