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>
34 #include "vlcproc.hpp"
35 #include "os_factory.hpp"
36 #include "os_loop.hpp"
37 #include "os_timer.hpp"
38 #include "var_manager.hpp"
39 #include "vout_manager.hpp"
41 #include "window_manager.hpp"
42 #include "../commands/async_queue.hpp"
43 #include "../commands/cmd_change_skin.hpp"
44 #include "../commands/cmd_show_window.hpp"
45 #include "../commands/cmd_quit.hpp"
46 #include "../commands/cmd_resize.hpp"
47 #include "../commands/cmd_vars.hpp"
48 #include "../commands/cmd_dialogs.hpp"
49 #include "../commands/cmd_update_item.hpp"
50 #include "../utils/var_bool.hpp"
54 VlcProc *VlcProc::instance( intf_thread_t *pIntf )
56 if( pIntf->p_sys->p_vlcProc == NULL )
58 pIntf->p_sys->p_vlcProc = new VlcProc( pIntf );
61 return pIntf->p_sys->p_vlcProc;
65 void VlcProc::destroy( intf_thread_t *pIntf )
67 delete pIntf->p_sys->p_vlcProc;
68 pIntf->p_sys->p_vlcProc = NULL;
72 VlcProc::VlcProc( intf_thread_t *pIntf ): SkinObject( pIntf ),
73 m_varVoutSize( pIntf ), m_varEqBands( pIntf ),
74 m_pVout( NULL ), m_pAout( NULL ), m_cmdManage( this )
76 // Create a timer to poll the status of the vlc
77 OSFactory *pOsFactory = OSFactory::instance( pIntf );
78 m_pTimer = pOsFactory->createOSTimer( m_cmdManage );
79 m_pTimer->start( 100, false );
81 // Create and register VLC variables
82 VarManager *pVarManager = VarManager::instance( getIntf() );
84 #define REGISTER_VAR( var, type, name ) \
85 var = VariablePtr( new type( getIntf() ) ); \
86 pVarManager->registerVar( var, name );
87 REGISTER_VAR( m_cVarRandom, VarBoolImpl, "playlist.isRandom" )
88 REGISTER_VAR( m_cVarLoop, VarBoolImpl, "playlist.isLoop" )
89 REGISTER_VAR( m_cVarRepeat, VarBoolImpl, "playlist.isRepeat" )
90 REGISTER_VAR( m_cPlaytree, Playtree, "playtree" )
91 pVarManager->registerVar( getPlaytreeVar().getPositionVarPtr(),
93 pVarManager->registerVar( m_cVarRandom, "playtree.isRandom" );
94 pVarManager->registerVar( m_cVarLoop, "playtree.isLoop" );
96 REGISTER_VAR( m_cVarPlaying, VarBoolImpl, "vlc.isPlaying" )
97 REGISTER_VAR( m_cVarStopped, VarBoolImpl, "vlc.isStopped" )
98 REGISTER_VAR( m_cVarPaused, VarBoolImpl, "vlc.isPaused" )
100 /* Input variables */
101 pVarManager->registerVar( m_cVarRepeat, "playtree.isRepeat" );
102 REGISTER_VAR( m_cVarTime, StreamTime, "time" )
103 REGISTER_VAR( m_cVarSeekable, VarBoolImpl, "vlc.isSeekable" )
104 REGISTER_VAR( m_cVarDvdActive, VarBoolImpl, "dvd.isActive" )
107 REGISTER_VAR( m_cVarFullscreen, VarBoolImpl, "vlc.isFullscreen" )
108 REGISTER_VAR( m_cVarHasVout, VarBoolImpl, "vlc.hasVout" )
111 REGISTER_VAR( m_cVarHasAudio, VarBoolImpl, "vlc.hasAudio" )
112 REGISTER_VAR( m_cVarVolume, Volume, "volume" )
113 REGISTER_VAR( m_cVarMute, VarBoolImpl, "vlc.isMute" )
114 REGISTER_VAR( m_cVarEqualizer, VarBoolImpl, "equalizer.isEnabled" )
115 REGISTER_VAR( m_cVarEqPreamp, EqualizerPreamp, "equalizer.preamp" )
118 m_cVarStreamName = VariablePtr( new VarText( getIntf(), false ) );
119 pVarManager->registerVar( m_cVarStreamName, "streamName" );
120 m_cVarStreamURI = VariablePtr( new VarText( getIntf(), false ) );
121 pVarManager->registerVar( m_cVarStreamURI, "streamURI" );
122 m_cVarStreamBitRate = VariablePtr( new VarText( getIntf(), false ) );
123 pVarManager->registerVar( m_cVarStreamBitRate, "bitrate" );
124 m_cVarStreamSampleRate = VariablePtr( new VarText( getIntf(), false ) );
125 pVarManager->registerVar( m_cVarStreamSampleRate, "samplerate" );
127 // Register the equalizer bands
128 for( int i = 0; i < EqualizerBands::kNbBands; i++)
131 ss << "equalizer.band(" << i << ")";
132 pVarManager->registerVar( m_varEqBands.getBand( i ), ss.str() );
136 // The object variable callbacks are called from other VLC threads,
137 // so they must put commands in the queue and NOT do anything else
138 // (X11 calls are not reentrant)
140 // Called when the playlist changes
141 var_AddCallback( pIntf->p_sys->p_playlist, "intf-change",
142 onIntfChange, this );
143 // Called when a playlist item is added
144 var_AddCallback( pIntf->p_sys->p_playlist, "playlist-item-append",
145 onItemAppend, this );
146 // Called when a playlist item is deleted
147 // TODO: properly handle item-deleted
148 var_AddCallback( pIntf->p_sys->p_playlist, "playlist-item-deleted",
149 onItemDelete, this );
150 // Called when the "interface shower" wants us to show the skin
151 var_AddCallback( pIntf->p_libvlc, "intf-show",
153 // Called when the current played item changes
154 var_AddCallback( pIntf->p_sys->p_playlist, "item-current",
155 onPlaylistChange, this );
156 // Called when a playlist item changed
157 var_AddCallback( pIntf->p_sys->p_playlist, "item-change",
158 onItemChange, this );
159 // Called when our skins2 demux wants us to load a new skin
160 var_AddCallback( pIntf, "skin-to-load", onSkinToLoad, this );
162 // Called when we have an interaction dialog to display
163 var_Create( pIntf, "interaction", VLC_VAR_ADDRESS );
164 var_AddCallback( pIntf, "interaction", onInteraction, this );
165 interaction_Register( pIntf );
167 getIntf()->p_sys->p_input = NULL;
175 if( getIntf()->p_sys->p_input )
177 vlc_object_release( getIntf()->p_sys->p_input );
178 getIntf()->p_sys->p_input = NULL;
181 interaction_Unregister( getIntf() );
183 var_DelCallback( getIntf()->p_sys->p_playlist, "intf-change",
184 onIntfChange, this );
185 var_DelCallback( getIntf()->p_sys->p_playlist, "playlist-item-append",
186 onItemAppend, this );
187 var_DelCallback( getIntf()->p_sys->p_playlist, "playlist-item-deleted",
188 onItemDelete, this );
189 var_DelCallback( getIntf()->p_libvlc, "intf-show",
191 var_DelCallback( getIntf()->p_sys->p_playlist, "item-current",
192 onPlaylistChange, this );
193 var_DelCallback( getIntf()->p_sys->p_playlist, "item-change",
194 onItemChange, this );
195 var_DelCallback( getIntf(), "skin-to-load", onSkinToLoad, this );
196 var_DelCallback( getIntf(), "interaction", onInteraction, this );
199 void VlcProc::manage()
201 // Did the user request to quit vlc ?
202 if( !vlc_object_alive( getIntf() ) )
204 // Get the instance of OSFactory
205 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
207 // Exit the main OS loop
208 pOsFactory->getOSLoop()->exit();
218 void VlcProc::CmdManage::execute()
220 // Just forward to VlcProc
224 void VlcProc::refreshAudio()
228 // Check if the audio output has changed
229 aout_instance_t *pAout = (aout_instance_t *)vlc_object_find( getIntf(),
230 VLC_OBJECT_AOUT, FIND_ANYWHERE );
233 if( pAout != m_pAout )
235 // Register the equalizer callbacks
236 if( !var_AddCallback( pAout, "equalizer-bands",
237 onEqBandsChange, this ) &&
238 !var_AddCallback( pAout, "equalizer-preamp",
239 onEqPreampChange, this ) )
242 //char * psz_bands = var_GetString( p_aout, "equalizer-bands" );
245 // Get the audio filters
246 pFilters = var_GetNonEmptyString( pAout, "audio-filter" );
247 vlc_object_release( pAout );
251 // Get the audio filters
252 pFilters = config_GetPsz( getIntf(), "audio-filter" );
255 // Refresh sound volume
256 audio_volume_t volume;
257 aout_VolumeGet( getIntf()->p_sys->p_playlist, &volume );
258 Volume *pVolume = (Volume*)m_cVarVolume.get();
259 pVolume->set( (double)volume * 2.0 / AOUT_VOLUME_MAX );
261 // Set the mute variable
262 VarBoolImpl *pVarMute = (VarBoolImpl*)m_cVarMute.get();
263 pVarMute->set( volume == 0 );
265 // Refresh the equalizer variable
266 VarBoolImpl *pVarEqualizer = (VarBoolImpl*)m_cVarEqualizer.get();
267 pVarEqualizer->set( pFilters && strstr( pFilters, "equalizer" ) );
271 void VlcProc::refreshPlaylist()
273 // Refresh the random variable
274 VarBoolImpl *pVarRandom = (VarBoolImpl*)m_cVarRandom.get();
276 var_Get( getIntf()->p_sys->p_playlist, "random", &val );
277 pVarRandom->set( val.b_bool != 0 );
279 // Refresh the loop variable
280 VarBoolImpl *pVarLoop = (VarBoolImpl*)m_cVarLoop.get();
281 var_Get( getIntf()->p_sys->p_playlist, "loop", &val );
282 pVarLoop->set( val.b_bool != 0 );
284 // Refresh the repeat variable
285 VarBoolImpl *pVarRepeat = (VarBoolImpl*)m_cVarRepeat.get();
286 var_Get( getIntf()->p_sys->p_playlist, "repeat", &val );
287 pVarRepeat->set( val.b_bool != 0 );
290 void VlcProc::refreshInput()
292 StreamTime *pTime = (StreamTime*)m_cVarTime.get();
293 VarBoolImpl *pVarSeekable = (VarBoolImpl*)m_cVarSeekable.get();
294 VarBoolImpl *pVarDvdActive = (VarBoolImpl*)m_cVarDvdActive.get();
295 VarBoolImpl *pVarHasVout = (VarBoolImpl*)m_cVarHasVout.get();
296 VarBoolImpl *pVarHasAudio = (VarBoolImpl*)m_cVarHasAudio.get();
297 VarText *pBitrate = (VarText*)m_cVarStreamBitRate.get();
298 VarText *pSampleRate = (VarText*)m_cVarStreamSampleRate.get();
299 VarBoolImpl *pVarFullscreen = (VarBoolImpl*)m_cVarFullscreen.get();
300 VarBoolImpl *pVarPlaying = (VarBoolImpl*)m_cVarPlaying.get();
301 VarBoolImpl *pVarStopped = (VarBoolImpl*)m_cVarStopped.get();
302 VarBoolImpl *pVarPaused = (VarBoolImpl*)m_cVarPaused.get();
305 if( getIntf()->p_sys->p_input == NULL )
307 getIntf()->p_sys->p_input =
308 playlist_CurrentInput( getIntf()->p_sys->p_playlist );
310 else if( getIntf()->p_sys->p_input->b_dead )
312 vlc_object_release( getIntf()->p_sys->p_input );
313 getIntf()->p_sys->p_input = NULL;
316 input_thread_t *pInput = getIntf()->p_sys->p_input;
318 if( pInput && vlc_object_alive (pInput) )
320 // Refresh time variables
322 var_Get( pInput, "position", &pos );
323 pTime->set( pos.f_float, false );
324 pVarSeekable->set( pos.f_float != 0.0 );
326 // Refresh DVD detection
327 vlc_value_t chapters_count;
328 var_Change( pInput, "chapter", VLC_VAR_CHOICESCOUNT,
329 &chapters_count, NULL );
330 pVarDvdActive->set( chapters_count.i_int > 0 );
332 // Get the input bitrate
333 int bitrate = var_GetInteger( pInput, "bit-rate" ) / 1000;
334 pBitrate->set( UString::fromInt( getIntf(), bitrate ) );
336 // Get the audio sample rate
337 int sampleRate = var_GetInteger( pInput, "sample-rate" ) / 1000;
338 pSampleRate->set( UString::fromInt( getIntf(), sampleRate ) );
341 vlc_value_t audio_es;
342 var_Change( pInput, "audio-es", VLC_VAR_CHOICESCOUNT,
344 pVarHasAudio->set( audio_es.i_int > 0 );
346 // Refresh fullscreen status
347 vout_thread_t *pVout = input_GetVout( pInput );
348 pVarHasVout->set( pVout != NULL );
351 pVarFullscreen->set( var_GetBool( pVout, "fullscreen" ) );
352 vlc_object_release( pVout );
355 // Refresh play/pause status
356 int state = var_GetInteger( pInput, "state" );
357 pVarStopped->set( false );
358 pVarPlaying->set( state != PAUSE_S );
359 pVarPaused->set( state == PAUSE_S );
363 pVarSeekable->set( false );
364 pVarDvdActive->set( false );
365 pTime->set( 0, false );
366 pVarFullscreen->set( false );
367 pVarHasAudio->set( false );
368 pVarHasVout->set( false );
369 pVarStopped->set( true );
370 pVarPlaying->set( false );
371 pVarPaused->set( false );
375 int VlcProc::onIntfChange( vlc_object_t *pObj, const char *pVariable,
376 vlc_value_t oldVal, vlc_value_t newVal,
379 VlcProc *pThis = (VlcProc*)pParam;
381 // Update the stream variable
382 pThis->updateStreamName();
384 // Create a playtree notify command (for new style playtree)
385 CmdPlaytreeChanged *pCmdTree = new CmdPlaytreeChanged( pThis->getIntf() );
387 // Push the command in the asynchronous command queue
388 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
389 pQueue->push( CmdGenericPtr( pCmdTree ) );
395 int VlcProc::onIntfShow( vlc_object_t *pObj, const char *pVariable,
396 vlc_value_t oldVal, vlc_value_t newVal,
401 VlcProc *pThis = (VlcProc*)pParam;
403 // Create a raise all command
404 CmdRaiseAll *pCmd = new CmdRaiseAll( pThis->getIntf(),
405 pThis->getIntf()->p_sys->p_theme->getWindowManager() );
407 // Push the command in the asynchronous command queue
408 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
409 pQueue->push( CmdGenericPtr( pCmd ) );
416 int VlcProc::onItemChange( vlc_object_t *pObj, const char *pVariable,
417 vlc_value_t oldval, vlc_value_t newval,
420 VlcProc *pThis = (VlcProc*)pParam;
421 input_item_t *p_item = static_cast<input_item_t*>(newval.p_address);
423 // Update the stream variable
424 pThis->updateStreamName();
426 // Create a playtree notify command
427 CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(),
430 // Push the command in the asynchronous command queue
431 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
432 pQueue->push( CmdGenericPtr( pCmdTree ), true );
437 int VlcProc::onItemAppend( vlc_object_t *pObj, const char *pVariable,
438 vlc_value_t oldVal, vlc_value_t newVal,
441 VlcProc *pThis = (VlcProc*)pParam;
443 playlist_add_t *p_add = (playlist_add_t*)malloc( sizeof(
446 memcpy( p_add, newVal.p_address, sizeof( playlist_add_t ) ) ;
448 CmdGenericPtr ptrTree;
449 CmdPlaytreeAppend *pCmdTree = new CmdPlaytreeAppend( pThis->getIntf(),
451 ptrTree = CmdGenericPtr( pCmdTree );
453 // Push the command in the asynchronous command queue
454 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
455 pQueue->push( ptrTree , false );
460 int VlcProc::onItemDelete( vlc_object_t *pObj, const char *pVariable,
461 vlc_value_t oldVal, vlc_value_t newVal,
464 VlcProc *pThis = (VlcProc*)pParam;
466 int i_id = newVal.i_int;
468 CmdGenericPtr ptrTree;
469 CmdPlaytreeDelete *pCmdTree = new CmdPlaytreeDelete( pThis->getIntf(),
471 ptrTree = CmdGenericPtr( pCmdTree );
473 // Push the command in the asynchronous command queue
474 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
475 pQueue->push( ptrTree , false );
481 int VlcProc::onPlaylistChange( vlc_object_t *pObj, const char *pVariable,
482 vlc_value_t oldval, vlc_value_t newval,
485 VlcProc *pThis = (VlcProc*)pParam;
486 input_item_t *p_item = static_cast<input_item_t*>(newval.p_address);
488 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
490 // Update the stream variable
491 pThis->updateStreamName();
493 // Create two playtree notify commands: one for old item, one for new
494 #if 0 /* FIXME: Heck, no! You cannot do that.
495 There is no warranty that the old item is still valid. */
496 CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(),
498 pQueue->push( CmdGenericPtr( pCmdTree ) , true );
500 CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(), p_item->i_id );
501 pQueue->push( CmdGenericPtr( pCmdTree ) , true );
507 int VlcProc::onSkinToLoad( vlc_object_t *pObj, const char *pVariable,
508 vlc_value_t oldVal, vlc_value_t newVal,
511 VlcProc *pThis = (VlcProc*)pParam;
513 // Create a playlist notify command
514 CmdChangeSkin *pCmd =
515 new CmdChangeSkin( pThis->getIntf(), newVal.psz_string );
517 // Push the command in the asynchronous command queue
518 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
519 pQueue->push( CmdGenericPtr( pCmd ) );
524 int VlcProc::onInteraction( vlc_object_t *pObj, const char *pVariable,
525 vlc_value_t oldVal, vlc_value_t newVal,
528 VlcProc *pThis = (VlcProc*)pParam;
529 interaction_dialog_t *p_dialog = (interaction_dialog_t *)(newVal.p_address);
531 CmdInteraction *pCmd = new CmdInteraction( pThis->getIntf(), p_dialog );
532 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
533 pQueue->push( CmdGenericPtr( pCmd ) );
538 void VlcProc::updateStreamName()
540 // Create a update item command
541 CmdUpdateItem *pCmdItem = new CmdUpdateItem( getIntf(), getStreamNameVar(), getStreamURIVar() );
543 // Push the command in the asynchronous command queue
544 AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
545 pQueue->push( CmdGenericPtr( pCmdItem ) );
548 int VlcProc::onEqBandsChange( vlc_object_t *pObj, const char *pVariable,
549 vlc_value_t oldVal, vlc_value_t newVal,
552 VlcProc *pThis = (VlcProc*)pParam;
554 // Post a set equalizer bands command
555 CmdSetEqBands *pCmd = new CmdSetEqBands( pThis->getIntf(),
558 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
559 pQueue->push( CmdGenericPtr( pCmd ) );
565 int VlcProc::onEqPreampChange( vlc_object_t *pObj, const char *pVariable,
566 vlc_value_t oldVal, vlc_value_t newVal,
569 VlcProc *pThis = (VlcProc*)pParam;
570 EqualizerPreamp *pVarPreamp = (EqualizerPreamp*)(pThis->m_cVarEqPreamp.get());
572 // Post a set preamp command
573 CmdSetEqPreamp *pCmd = new CmdSetEqPreamp( pThis->getIntf(), *pVarPreamp,
574 (newVal.f_float + 20.0) / 40.0 );
575 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
576 pQueue->push( CmdGenericPtr( pCmd ) );