1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003-2009 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
29 #include <vlc_common.h>
32 #include <vlc_playlist.h>
33 #include <vlc_window.h>
35 #include "vlcproc.hpp"
36 #include "os_factory.hpp"
37 #include "os_loop.hpp"
38 #include "os_timer.hpp"
39 #include "var_manager.hpp"
40 #include "vout_manager.hpp"
42 #include "window_manager.hpp"
43 #include "../commands/async_queue.hpp"
44 #include "../commands/cmd_change_skin.hpp"
45 #include "../commands/cmd_show_window.hpp"
46 #include "../commands/cmd_quit.hpp"
47 #include "../commands/cmd_resize.hpp"
48 #include "../commands/cmd_vars.hpp"
49 #include "../commands/cmd_dialogs.hpp"
50 #include "../commands/cmd_update_item.hpp"
51 #include "../utils/var_bool.hpp"
55 VlcProc *VlcProc::instance( intf_thread_t *pIntf )
57 if( pIntf->p_sys->p_vlcProc == NULL )
59 pIntf->p_sys->p_vlcProc = new VlcProc( pIntf );
62 return pIntf->p_sys->p_vlcProc;
66 void VlcProc::destroy( intf_thread_t *pIntf )
68 delete pIntf->p_sys->p_vlcProc;
69 pIntf->p_sys->p_vlcProc = NULL;
73 VlcProc::VlcProc( intf_thread_t *pIntf ): SkinObject( pIntf ),
74 m_varVoutSize( pIntf ), m_varEqBands( pIntf ),
75 m_pVout( NULL ), m_pAout( NULL ), m_cmdManage( this )
77 // Create a timer to poll the status of the vlc
78 OSFactory *pOsFactory = OSFactory::instance( pIntf );
79 m_pTimer = pOsFactory->createOSTimer( m_cmdManage );
80 m_pTimer->start( 100, false );
82 // Create and register VLC variables
83 VarManager *pVarManager = VarManager::instance( getIntf() );
85 #define REGISTER_VAR( var, type, name ) \
86 var = VariablePtr( new type( getIntf() ) ); \
87 pVarManager->registerVar( var, name );
88 REGISTER_VAR( m_cVarRandom, VarBoolImpl, "playlist.isRandom" )
89 REGISTER_VAR( m_cVarLoop, VarBoolImpl, "playlist.isLoop" )
90 REGISTER_VAR( m_cVarRepeat, VarBoolImpl, "playlist.isRepeat" )
91 REGISTER_VAR( m_cPlaytree, Playtree, "playtree" )
92 pVarManager->registerVar( getPlaytreeVar().getPositionVarPtr(),
94 pVarManager->registerVar( m_cVarRandom, "playtree.isRandom" );
95 pVarManager->registerVar( m_cVarLoop, "playtree.isLoop" );
97 REGISTER_VAR( m_cVarPlaying, VarBoolImpl, "vlc.isPlaying" )
98 REGISTER_VAR( m_cVarStopped, VarBoolImpl, "vlc.isStopped" )
99 REGISTER_VAR( m_cVarPaused, VarBoolImpl, "vlc.isPaused" )
101 /* Input variables */
102 pVarManager->registerVar( m_cVarRepeat, "playtree.isRepeat" );
103 REGISTER_VAR( m_cVarTime, StreamTime, "time" )
104 REGISTER_VAR( m_cVarSeekable, VarBoolImpl, "vlc.isSeekable" )
105 REGISTER_VAR( m_cVarDvdActive, VarBoolImpl, "dvd.isActive" )
108 REGISTER_VAR( m_cVarFullscreen, VarBoolImpl, "vlc.isFullscreen" )
109 REGISTER_VAR( m_cVarHasVout, VarBoolImpl, "vlc.hasVout" )
112 REGISTER_VAR( m_cVarHasAudio, VarBoolImpl, "vlc.hasAudio" )
113 REGISTER_VAR( m_cVarVolume, Volume, "volume" )
114 REGISTER_VAR( m_cVarMute, VarBoolImpl, "vlc.isMute" )
115 REGISTER_VAR( m_cVarEqualizer, VarBoolImpl, "equalizer.isEnabled" )
116 REGISTER_VAR( m_cVarEqPreamp, EqualizerPreamp, "equalizer.preamp" )
119 m_cVarStreamName = VariablePtr( new VarText( getIntf(), false ) );
120 pVarManager->registerVar( m_cVarStreamName, "streamName" );
121 m_cVarStreamURI = VariablePtr( new VarText( getIntf(), false ) );
122 pVarManager->registerVar( m_cVarStreamURI, "streamURI" );
123 m_cVarStreamBitRate = VariablePtr( new VarText( getIntf(), false ) );
124 pVarManager->registerVar( m_cVarStreamBitRate, "bitrate" );
125 m_cVarStreamSampleRate = VariablePtr( new VarText( getIntf(), false ) );
126 pVarManager->registerVar( m_cVarStreamSampleRate, "samplerate" );
128 // Register the equalizer bands
129 for( int i = 0; i < EqualizerBands::kNbBands; i++)
132 ss << "equalizer.band(" << i << ")";
133 pVarManager->registerVar( m_varEqBands.getBand( i ), ss.str() );
137 // The object variable callbacks are called from other VLC threads,
138 // so they must put commands in the queue and NOT do anything else
139 // (X11 calls are not reentrant)
141 // Called when the playlist changes
142 var_AddCallback( pIntf->p_sys->p_playlist, "intf-change",
143 onIntfChange, this );
144 // Called when a playlist item is added
145 var_AddCallback( pIntf->p_sys->p_playlist, "playlist-item-append",
146 onItemAppend, this );
147 // Called when a playlist item is deleted
148 // TODO: properly handle item-deleted
149 var_AddCallback( pIntf->p_sys->p_playlist, "playlist-item-deleted",
150 onItemDelete, this );
151 // Called when the "interface shower" wants us to show the skin
152 var_AddCallback( pIntf->p_libvlc, "intf-show",
154 // Called when the current played item changes
155 var_AddCallback( pIntf->p_sys->p_playlist, "item-current",
156 onPlaylistChange, this );
157 // Called when a playlist item changed
158 var_AddCallback( pIntf->p_sys->p_playlist, "item-change",
159 onItemChange, this );
160 // Called when our skins2 demux wants us to load a new skin
161 var_AddCallback( pIntf, "skin-to-load", onSkinToLoad, this );
163 // Called when we have an interaction dialog to display
164 var_Create( pIntf, "interaction", VLC_VAR_ADDRESS );
165 var_AddCallback( pIntf, "interaction", onInteraction, this );
166 interaction_Register( pIntf );
168 getIntf()->p_sys->p_input = NULL;
176 if( getIntf()->p_sys->p_input )
178 vlc_object_release( getIntf()->p_sys->p_input );
179 getIntf()->p_sys->p_input = NULL;
182 interaction_Unregister( getIntf() );
184 var_DelCallback( getIntf()->p_sys->p_playlist, "intf-change",
185 onIntfChange, this );
186 var_DelCallback( getIntf()->p_sys->p_playlist, "playlist-item-append",
187 onItemAppend, this );
188 var_DelCallback( getIntf()->p_sys->p_playlist, "playlist-item-deleted",
189 onItemDelete, this );
190 var_DelCallback( getIntf()->p_libvlc, "intf-show",
192 var_DelCallback( getIntf()->p_sys->p_playlist, "item-current",
193 onPlaylistChange, this );
194 var_DelCallback( getIntf()->p_sys->p_playlist, "item-change",
195 onItemChange, this );
196 var_DelCallback( getIntf(), "skin-to-load", onSkinToLoad, this );
197 var_DelCallback( getIntf(), "interaction", onInteraction, this );
200 void VlcProc::manage()
202 // Did the user request to quit vlc ?
203 if( !vlc_object_alive( getIntf() ) )
205 // Get the instance of OSFactory
206 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
208 // Exit the main OS loop
209 pOsFactory->getOSLoop()->exit();
219 void VlcProc::CmdManage::execute()
221 // Just forward to VlcProc
225 void VlcProc::refreshAudio()
229 // Check if the audio output has changed
230 aout_instance_t *pAout = (aout_instance_t *)vlc_object_find( getIntf(),
231 VLC_OBJECT_AOUT, FIND_ANYWHERE );
234 if( pAout != m_pAout )
236 // Register the equalizer callbacks
237 if( !var_AddCallback( pAout, "equalizer-bands",
238 onEqBandsChange, this ) &&
239 !var_AddCallback( pAout, "equalizer-preamp",
240 onEqPreampChange, this ) )
243 //char * psz_bands = var_GetString( p_aout, "equalizer-bands" );
246 // Get the audio filters
247 pFilters = var_GetNonEmptyString( pAout, "audio-filter" );
248 vlc_object_release( pAout );
252 // Get the audio filters
253 pFilters = config_GetPsz( getIntf(), "audio-filter" );
256 // Refresh sound volume
257 audio_volume_t volume;
258 aout_VolumeGet( getIntf()->p_sys->p_playlist, &volume );
259 Volume *pVolume = (Volume*)m_cVarVolume.get();
260 pVolume->set( (double)volume * 2.0 / AOUT_VOLUME_MAX );
262 // Set the mute variable
263 VarBoolImpl *pVarMute = (VarBoolImpl*)m_cVarMute.get();
264 pVarMute->set( volume == 0 );
266 // Refresh the equalizer variable
267 VarBoolImpl *pVarEqualizer = (VarBoolImpl*)m_cVarEqualizer.get();
268 pVarEqualizer->set( pFilters && strstr( pFilters, "equalizer" ) );
272 void VlcProc::refreshPlaylist()
274 // Refresh the random variable
275 VarBoolImpl *pVarRandom = (VarBoolImpl*)m_cVarRandom.get();
277 var_Get( getIntf()->p_sys->p_playlist, "random", &val );
278 pVarRandom->set( val.b_bool != 0 );
280 // Refresh the loop variable
281 VarBoolImpl *pVarLoop = (VarBoolImpl*)m_cVarLoop.get();
282 var_Get( getIntf()->p_sys->p_playlist, "loop", &val );
283 pVarLoop->set( val.b_bool != 0 );
285 // Refresh the repeat variable
286 VarBoolImpl *pVarRepeat = (VarBoolImpl*)m_cVarRepeat.get();
287 var_Get( getIntf()->p_sys->p_playlist, "repeat", &val );
288 pVarRepeat->set( val.b_bool != 0 );
291 void VlcProc::refreshInput()
293 StreamTime *pTime = (StreamTime*)m_cVarTime.get();
294 VarBoolImpl *pVarSeekable = (VarBoolImpl*)m_cVarSeekable.get();
295 VarBoolImpl *pVarDvdActive = (VarBoolImpl*)m_cVarDvdActive.get();
296 VarBoolImpl *pVarHasVout = (VarBoolImpl*)m_cVarHasVout.get();
297 VarBoolImpl *pVarHasAudio = (VarBoolImpl*)m_cVarHasAudio.get();
298 VarText *pBitrate = (VarText*)m_cVarStreamBitRate.get();
299 VarText *pSampleRate = (VarText*)m_cVarStreamSampleRate.get();
300 VarBoolImpl *pVarFullscreen = (VarBoolImpl*)m_cVarFullscreen.get();
301 VarBoolImpl *pVarPlaying = (VarBoolImpl*)m_cVarPlaying.get();
302 VarBoolImpl *pVarStopped = (VarBoolImpl*)m_cVarStopped.get();
303 VarBoolImpl *pVarPaused = (VarBoolImpl*)m_cVarPaused.get();
306 if( getIntf()->p_sys->p_input == NULL )
308 getIntf()->p_sys->p_input =
309 playlist_CurrentInput( getIntf()->p_sys->p_playlist );
311 else if( getIntf()->p_sys->p_input->b_dead )
313 vlc_object_release( getIntf()->p_sys->p_input );
314 getIntf()->p_sys->p_input = NULL;
317 input_thread_t *pInput = getIntf()->p_sys->p_input;
319 if( pInput && vlc_object_alive (pInput) )
321 // Refresh time variables
323 var_Get( pInput, "position", &pos );
324 pTime->set( pos.f_float, false );
325 pVarSeekable->set( pos.f_float != 0.0 );
327 // Refresh DVD detection
328 vlc_value_t chapters_count;
329 var_Change( pInput, "chapter", VLC_VAR_CHOICESCOUNT,
330 &chapters_count, NULL );
331 pVarDvdActive->set( chapters_count.i_int > 0 );
333 // Get the input bitrate
334 int bitrate = var_GetInteger( pInput, "bit-rate" ) / 1000;
335 pBitrate->set( UString::fromInt( getIntf(), bitrate ) );
337 // Get the audio sample rate
338 int sampleRate = var_GetInteger( pInput, "sample-rate" ) / 1000;
339 pSampleRate->set( UString::fromInt( getIntf(), sampleRate ) );
342 vlc_value_t audio_es;
343 var_Change( pInput, "audio-es", VLC_VAR_CHOICESCOUNT,
345 pVarHasAudio->set( audio_es.i_int > 0 );
347 // Refresh fullscreen status
348 vout_thread_t *pVout = input_GetVout( pInput );
349 pVarHasVout->set( pVout != NULL );
352 pVarFullscreen->set( var_GetBool( pVout, "fullscreen" ) );
353 vlc_object_release( pVout );
356 // Refresh play/pause status
357 int state = var_GetInteger( pInput, "state" );
358 pVarStopped->set( false );
359 pVarPlaying->set( state != PAUSE_S );
360 pVarPaused->set( state == PAUSE_S );
364 pVarSeekable->set( false );
365 pVarDvdActive->set( false );
366 pTime->set( 0, false );
367 pVarFullscreen->set( false );
368 pVarHasAudio->set( false );
369 pVarHasVout->set( false );
370 pVarStopped->set( true );
371 pVarPlaying->set( false );
372 pVarPaused->set( false );
376 int VlcProc::onIntfChange( vlc_object_t *pObj, const char *pVariable,
377 vlc_value_t oldVal, vlc_value_t newVal,
380 VlcProc *pThis = (VlcProc*)pParam;
382 // Update the stream variable
383 pThis->updateStreamName();
385 // Create a playtree notify command (for new style playtree)
386 CmdPlaytreeChanged *pCmdTree = new CmdPlaytreeChanged( pThis->getIntf() );
388 // Push the command in the asynchronous command queue
389 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
390 pQueue->push( CmdGenericPtr( pCmdTree ) );
396 int VlcProc::onIntfShow( vlc_object_t *pObj, const char *pVariable,
397 vlc_value_t oldVal, vlc_value_t newVal,
402 VlcProc *pThis = (VlcProc*)pParam;
404 // Create a raise all command
405 CmdRaiseAll *pCmd = new CmdRaiseAll( pThis->getIntf(),
406 pThis->getIntf()->p_sys->p_theme->getWindowManager() );
408 // Push the command in the asynchronous command queue
409 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
410 pQueue->push( CmdGenericPtr( pCmd ) );
417 int VlcProc::onItemChange( vlc_object_t *pObj, const char *pVariable,
418 vlc_value_t oldval, vlc_value_t newval,
421 VlcProc *pThis = (VlcProc*)pParam;
422 input_item_t *p_item = static_cast<input_item_t*>(newval.p_address);
424 // Update the stream variable
425 pThis->updateStreamName();
427 // Create a playtree notify command
428 CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(),
431 // Push the command in the asynchronous command queue
432 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
433 pQueue->push( CmdGenericPtr( pCmdTree ), true );
438 int VlcProc::onItemAppend( vlc_object_t *pObj, const char *pVariable,
439 vlc_value_t oldVal, vlc_value_t newVal,
442 VlcProc *pThis = (VlcProc*)pParam;
444 playlist_add_t *p_add = (playlist_add_t*)malloc( sizeof(
447 memcpy( p_add, newVal.p_address, sizeof( playlist_add_t ) ) ;
449 CmdGenericPtr ptrTree;
450 CmdPlaytreeAppend *pCmdTree = new CmdPlaytreeAppend( pThis->getIntf(),
452 ptrTree = CmdGenericPtr( pCmdTree );
454 // Push the command in the asynchronous command queue
455 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
456 pQueue->push( ptrTree , false );
461 int VlcProc::onItemDelete( vlc_object_t *pObj, const char *pVariable,
462 vlc_value_t oldVal, vlc_value_t newVal,
465 VlcProc *pThis = (VlcProc*)pParam;
467 int i_id = newVal.i_int;
469 CmdGenericPtr ptrTree;
470 CmdPlaytreeDelete *pCmdTree = new CmdPlaytreeDelete( pThis->getIntf(),
472 ptrTree = CmdGenericPtr( pCmdTree );
474 // Push the command in the asynchronous command queue
475 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
476 pQueue->push( ptrTree , false );
482 int VlcProc::onPlaylistChange( vlc_object_t *pObj, const char *pVariable,
483 vlc_value_t oldval, vlc_value_t newval,
486 VlcProc *pThis = (VlcProc*)pParam;
487 input_item_t *p_item = static_cast<input_item_t*>(newval.p_address);
489 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
491 // Update the stream variable
492 pThis->updateStreamName();
494 // Create two playtree notify commands: one for old item, one for new
495 #if 0 /* FIXME: Heck, no! You cannot do that.
496 There is no warranty that the old item is still valid. */
497 CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(),
499 pQueue->push( CmdGenericPtr( pCmdTree ) , true );
501 CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(), p_item->i_id );
502 pQueue->push( CmdGenericPtr( pCmdTree ) , true );
508 int VlcProc::onSkinToLoad( vlc_object_t *pObj, const char *pVariable,
509 vlc_value_t oldVal, vlc_value_t newVal,
512 VlcProc *pThis = (VlcProc*)pParam;
514 // Create a playlist notify command
515 CmdChangeSkin *pCmd =
516 new CmdChangeSkin( pThis->getIntf(), newVal.psz_string );
518 // Push the command in the asynchronous command queue
519 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
520 pQueue->push( CmdGenericPtr( pCmd ) );
525 int VlcProc::onInteraction( vlc_object_t *pObj, const char *pVariable,
526 vlc_value_t oldVal, vlc_value_t newVal,
529 VlcProc *pThis = (VlcProc*)pParam;
530 interaction_dialog_t *p_dialog = (interaction_dialog_t *)(newVal.p_address);
532 CmdInteraction *pCmd = new CmdInteraction( pThis->getIntf(), p_dialog );
533 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
534 pQueue->push( CmdGenericPtr( pCmd ) );
539 void VlcProc::updateStreamName()
541 // Create a update item command
542 CmdUpdateItem *pCmdItem = new CmdUpdateItem( getIntf(), getStreamNameVar(), getStreamURIVar() );
544 // Push the command in the asynchronous command queue
545 AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
546 pQueue->push( CmdGenericPtr( pCmdItem ) );
549 int VlcProc::onEqBandsChange( vlc_object_t *pObj, const char *pVariable,
550 vlc_value_t oldVal, vlc_value_t newVal,
553 VlcProc *pThis = (VlcProc*)pParam;
555 // Post a set equalizer bands command
556 CmdSetEqBands *pCmd = new CmdSetEqBands( pThis->getIntf(),
559 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
560 pQueue->push( CmdGenericPtr( pCmd ) );
566 int VlcProc::onEqPreampChange( vlc_object_t *pObj, const char *pVariable,
567 vlc_value_t oldVal, vlc_value_t newVal,
570 VlcProc *pThis = (VlcProc*)pParam;
571 EqualizerPreamp *pVarPreamp = (EqualizerPreamp*)(pThis->m_cVarEqPreamp.get());
573 // Post a set preamp command
574 CmdSetEqPreamp *pCmd = new CmdSetEqPreamp( pThis->getIntf(), *pVarPreamp,
575 (newVal.f_float + 20.0) / 40.0 );
576 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
577 pQueue->push( CmdGenericPtr( pCmd ) );