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 if( pIntf->p_sys->p_vlcProc )
70 delete pIntf->p_sys->p_vlcProc;
71 pIntf->p_sys->p_vlcProc = NULL;
76 VlcProc::VlcProc( intf_thread_t *pIntf ): SkinObject( pIntf ),
77 m_varVoutSize( pIntf ), m_varEqBands( pIntf ),
78 m_pVout( NULL ), m_pAout( NULL ), m_cmdManage( this )
80 // Create a timer to poll the status of the vlc
81 OSFactory *pOsFactory = OSFactory::instance( pIntf );
82 m_pTimer = pOsFactory->createOSTimer( m_cmdManage );
83 m_pTimer->start( 100, false );
85 // Create and register VLC variables
86 VarManager *pVarManager = VarManager::instance( getIntf() );
88 #define REGISTER_VAR( var, type, name ) \
89 var = VariablePtr( new type( getIntf() ) ); \
90 pVarManager->registerVar( var, name );
91 REGISTER_VAR( m_cVarRandom, VarBoolImpl, "playlist.isRandom" )
92 REGISTER_VAR( m_cVarLoop, VarBoolImpl, "playlist.isLoop" )
93 REGISTER_VAR( m_cVarRepeat, VarBoolImpl, "playlist.isRepeat" )
94 REGISTER_VAR( m_cPlaytree, Playtree, "playtree" )
95 pVarManager->registerVar( getPlaytreeVar().getPositionVarPtr(),
97 pVarManager->registerVar( m_cVarRandom, "playtree.isRandom" );
98 pVarManager->registerVar( m_cVarLoop, "playtree.isLoop" );
100 REGISTER_VAR( m_cVarPlaying, VarBoolImpl, "vlc.isPlaying" )
101 REGISTER_VAR( m_cVarStopped, VarBoolImpl, "vlc.isStopped" )
102 REGISTER_VAR( m_cVarPaused, VarBoolImpl, "vlc.isPaused" )
104 /* Input variables */
105 pVarManager->registerVar( m_cVarRepeat, "playtree.isRepeat" );
106 REGISTER_VAR( m_cVarTime, StreamTime, "time" )
107 REGISTER_VAR( m_cVarSeekable, VarBoolImpl, "vlc.isSeekable" )
108 REGISTER_VAR( m_cVarDvdActive, VarBoolImpl, "dvd.isActive" )
111 REGISTER_VAR( m_cVarFullscreen, VarBoolImpl, "vlc.isFullscreen" )
112 REGISTER_VAR( m_cVarHasVout, VarBoolImpl, "vlc.hasVout" )
115 REGISTER_VAR( m_cVarHasAudio, VarBoolImpl, "vlc.hasAudio" )
116 REGISTER_VAR( m_cVarVolume, Volume, "volume" )
117 REGISTER_VAR( m_cVarMute, VarBoolImpl, "vlc.isMute" )
118 REGISTER_VAR( m_cVarEqualizer, VarBoolImpl, "equalizer.isEnabled" )
119 REGISTER_VAR( m_cVarEqPreamp, EqualizerPreamp, "equalizer.preamp" )
122 m_cVarStreamName = VariablePtr( new VarText( getIntf(), false ) );
123 pVarManager->registerVar( m_cVarStreamName, "streamName" );
124 m_cVarStreamURI = VariablePtr( new VarText( getIntf(), false ) );
125 pVarManager->registerVar( m_cVarStreamURI, "streamURI" );
126 m_cVarStreamBitRate = VariablePtr( new VarText( getIntf(), false ) );
127 pVarManager->registerVar( m_cVarStreamBitRate, "bitrate" );
128 m_cVarStreamSampleRate = VariablePtr( new VarText( getIntf(), false ) );
129 pVarManager->registerVar( m_cVarStreamSampleRate, "samplerate" );
131 // Register the equalizer bands
132 for( int i = 0; i < EqualizerBands::kNbBands; i++)
135 ss << "equalizer.band(" << i << ")";
136 pVarManager->registerVar( m_varEqBands.getBand( i ), ss.str() );
140 // The object variable callbacks are called from other VLC threads,
141 // so they must put commands in the queue and NOT do anything else
142 // (X11 calls are not reentrant)
144 // Called when the playlist changes
145 var_AddCallback( pIntf->p_sys->p_playlist, "intf-change",
146 onIntfChange, this );
147 // Called when a playlist item is added
148 var_AddCallback( pIntf->p_sys->p_playlist, "playlist-item-append",
149 onItemAppend, this );
150 // Called when a playlist item is deleted
151 // TODO: properly handle item-deleted
152 var_AddCallback( pIntf->p_sys->p_playlist, "playlist-item-deleted",
153 onItemDelete, this );
154 // Called when the "interface shower" wants us to show the skin
155 var_AddCallback( pIntf->p_libvlc, "intf-show",
157 // Called when the current played item changes
158 var_AddCallback( pIntf->p_sys->p_playlist, "item-current",
159 onPlaylistChange, this );
160 // Called when a playlist item changed
161 var_AddCallback( pIntf->p_sys->p_playlist, "item-change",
162 onItemChange, this );
163 // Called when our skins2 demux wants us to load a new skin
164 var_AddCallback( pIntf, "skin-to-load", onSkinToLoad, this );
166 // Called when we have an interaction dialog to display
167 var_Create( pIntf, "interaction", VLC_VAR_ADDRESS );
168 var_AddCallback( pIntf, "interaction", onInteraction, this );
169 interaction_Register( pIntf );
171 getIntf()->p_sys->p_input = NULL;
179 if( getIntf()->p_sys->p_input )
181 vlc_object_release( getIntf()->p_sys->p_input );
182 getIntf()->p_sys->p_input = NULL;
185 interaction_Unregister( getIntf() );
187 var_DelCallback( getIntf()->p_sys->p_playlist, "intf-change",
188 onIntfChange, this );
189 var_DelCallback( getIntf()->p_sys->p_playlist, "playlist-item-append",
190 onItemAppend, this );
191 var_DelCallback( getIntf()->p_sys->p_playlist, "playlist-item-deleted",
192 onItemDelete, this );
193 var_DelCallback( getIntf()->p_libvlc, "intf-show",
195 var_DelCallback( getIntf()->p_sys->p_playlist, "item-current",
196 onPlaylistChange, this );
197 var_DelCallback( getIntf()->p_sys->p_playlist, "item-change",
198 onItemChange, this );
199 var_DelCallback( getIntf(), "skin-to-load", onSkinToLoad, this );
200 var_DelCallback( getIntf(), "interaction", onInteraction, this );
203 void VlcProc::manage()
205 // Did the user request to quit vlc ?
206 if( !vlc_object_alive( getIntf() ) )
208 // Get the instance of OSFactory
209 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
211 // Exit the main OS loop
212 pOsFactory->getOSLoop()->exit();
222 void VlcProc::CmdManage::execute()
224 // Just forward to VlcProc
228 void VlcProc::refreshAudio()
232 // Check if the audio output has changed
233 aout_instance_t *pAout = (aout_instance_t *)vlc_object_find( getIntf(),
234 VLC_OBJECT_AOUT, FIND_ANYWHERE );
237 if( pAout != m_pAout )
239 // Register the equalizer callbacks
240 if( !var_AddCallback( pAout, "equalizer-bands",
241 onEqBandsChange, this ) &&
242 !var_AddCallback( pAout, "equalizer-preamp",
243 onEqPreampChange, this ) )
246 //char * psz_bands = var_GetString( p_aout, "equalizer-bands" );
249 // Get the audio filters
250 pFilters = var_GetNonEmptyString( pAout, "audio-filter" );
251 vlc_object_release( pAout );
255 // Get the audio filters
256 pFilters = config_GetPsz( getIntf(), "audio-filter" );
259 // Refresh sound volume
260 audio_volume_t volume;
261 aout_VolumeGet( getIntf()->p_sys->p_playlist, &volume );
262 Volume *pVolume = (Volume*)m_cVarVolume.get();
263 pVolume->set( (double)volume * 2.0 / AOUT_VOLUME_MAX );
265 // Set the mute variable
266 VarBoolImpl *pVarMute = (VarBoolImpl*)m_cVarMute.get();
267 pVarMute->set( volume == 0 );
269 // Refresh the equalizer variable
270 VarBoolImpl *pVarEqualizer = (VarBoolImpl*)m_cVarEqualizer.get();
271 pVarEqualizer->set( pFilters && strstr( pFilters, "equalizer" ) );
275 void VlcProc::refreshPlaylist()
277 // Refresh the random variable
278 VarBoolImpl *pVarRandom = (VarBoolImpl*)m_cVarRandom.get();
280 var_Get( getIntf()->p_sys->p_playlist, "random", &val );
281 pVarRandom->set( val.b_bool != 0 );
283 // Refresh the loop variable
284 VarBoolImpl *pVarLoop = (VarBoolImpl*)m_cVarLoop.get();
285 var_Get( getIntf()->p_sys->p_playlist, "loop", &val );
286 pVarLoop->set( val.b_bool != 0 );
288 // Refresh the repeat variable
289 VarBoolImpl *pVarRepeat = (VarBoolImpl*)m_cVarRepeat.get();
290 var_Get( getIntf()->p_sys->p_playlist, "repeat", &val );
291 pVarRepeat->set( val.b_bool != 0 );
294 void VlcProc::refreshInput()
296 StreamTime *pTime = (StreamTime*)m_cVarTime.get();
297 VarBoolImpl *pVarSeekable = (VarBoolImpl*)m_cVarSeekable.get();
298 VarBoolImpl *pVarDvdActive = (VarBoolImpl*)m_cVarDvdActive.get();
299 VarBoolImpl *pVarHasVout = (VarBoolImpl*)m_cVarHasVout.get();
300 VarBoolImpl *pVarHasAudio = (VarBoolImpl*)m_cVarHasAudio.get();
301 VarText *pBitrate = (VarText*)m_cVarStreamBitRate.get();
302 VarText *pSampleRate = (VarText*)m_cVarStreamSampleRate.get();
303 VarBoolImpl *pVarFullscreen = (VarBoolImpl*)m_cVarFullscreen.get();
304 VarBoolImpl *pVarPlaying = (VarBoolImpl*)m_cVarPlaying.get();
305 VarBoolImpl *pVarStopped = (VarBoolImpl*)m_cVarStopped.get();
306 VarBoolImpl *pVarPaused = (VarBoolImpl*)m_cVarPaused.get();
309 if( getIntf()->p_sys->p_input == NULL )
311 getIntf()->p_sys->p_input =
312 playlist_CurrentInput( getIntf()->p_sys->p_playlist );
314 else if( getIntf()->p_sys->p_input->b_dead )
316 vlc_object_release( getIntf()->p_sys->p_input );
317 getIntf()->p_sys->p_input = NULL;
320 input_thread_t *pInput = getIntf()->p_sys->p_input;
322 if( pInput && vlc_object_alive (pInput) )
324 // Refresh time variables
326 var_Get( pInput, "position", &pos );
327 pTime->set( pos.f_float, false );
328 pVarSeekable->set( pos.f_float != 0.0 );
330 // Refresh DVD detection
331 vlc_value_t chapters_count;
332 var_Change( pInput, "chapter", VLC_VAR_CHOICESCOUNT,
333 &chapters_count, NULL );
334 pVarDvdActive->set( chapters_count.i_int > 0 );
336 // Get the input bitrate
337 int bitrate = var_GetInteger( pInput, "bit-rate" ) / 1000;
338 pBitrate->set( UString::fromInt( getIntf(), bitrate ) );
340 // Get the audio sample rate
341 int sampleRate = var_GetInteger( pInput, "sample-rate" ) / 1000;
342 pSampleRate->set( UString::fromInt( getIntf(), sampleRate ) );
345 vlc_value_t audio_es;
346 var_Change( pInput, "audio-es", VLC_VAR_CHOICESCOUNT,
348 pVarHasAudio->set( audio_es.i_int > 0 );
350 // Refresh fullscreen status
351 vout_thread_t *pVout = input_GetVout( pInput );
352 pVarHasVout->set( pVout != NULL );
355 pVarFullscreen->set( var_GetBool( pVout, "fullscreen" ) );
356 vlc_object_release( pVout );
359 // Refresh play/pause status
360 int state = var_GetInteger( pInput, "state" );
361 pVarStopped->set( false );
362 pVarPlaying->set( state != PAUSE_S );
363 pVarPaused->set( state == PAUSE_S );
367 pVarSeekable->set( false );
368 pVarDvdActive->set( false );
369 pTime->set( 0, false );
370 pVarFullscreen->set( false );
371 pVarHasAudio->set( false );
372 pVarHasVout->set( false );
373 pVarStopped->set( true );
374 pVarPlaying->set( false );
375 pVarPaused->set( false );
379 int VlcProc::onIntfChange( vlc_object_t *pObj, const char *pVariable,
380 vlc_value_t oldVal, vlc_value_t newVal,
383 VlcProc *pThis = (VlcProc*)pParam;
385 // Update the stream variable
386 pThis->updateStreamName();
388 // Create a playtree notify command (for new style playtree)
389 CmdPlaytreeChanged *pCmdTree = new CmdPlaytreeChanged( pThis->getIntf() );
391 // Push the command in the asynchronous command queue
392 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
393 pQueue->push( CmdGenericPtr( pCmdTree ) );
399 int VlcProc::onIntfShow( vlc_object_t *pObj, const char *pVariable,
400 vlc_value_t oldVal, vlc_value_t newVal,
405 VlcProc *pThis = (VlcProc*)pParam;
407 // Create a raise all command
408 CmdRaiseAll *pCmd = new CmdRaiseAll( pThis->getIntf(),
409 pThis->getIntf()->p_sys->p_theme->getWindowManager() );
411 // Push the command in the asynchronous command queue
412 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
413 pQueue->push( CmdGenericPtr( pCmd ) );
420 int VlcProc::onItemChange( vlc_object_t *pObj, const char *pVariable,
421 vlc_value_t oldval, vlc_value_t newval,
424 VlcProc *pThis = (VlcProc*)pParam;
425 input_item_t *p_item = static_cast<input_item_t*>(newval.p_address);
427 // Update the stream variable
428 pThis->updateStreamName();
430 // Create a playtree notify command
431 CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(),
434 // Push the command in the asynchronous command queue
435 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
436 pQueue->push( CmdGenericPtr( pCmdTree ), true );
441 int VlcProc::onItemAppend( vlc_object_t *pObj, const char *pVariable,
442 vlc_value_t oldVal, vlc_value_t newVal,
445 VlcProc *pThis = (VlcProc*)pParam;
447 playlist_add_t *p_add = (playlist_add_t*)malloc( sizeof(
450 memcpy( p_add, newVal.p_address, sizeof( playlist_add_t ) ) ;
452 CmdGenericPtr ptrTree;
453 CmdPlaytreeAppend *pCmdTree = new CmdPlaytreeAppend( pThis->getIntf(),
455 ptrTree = CmdGenericPtr( pCmdTree );
457 // Push the command in the asynchronous command queue
458 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
459 pQueue->push( ptrTree , false );
464 int VlcProc::onItemDelete( vlc_object_t *pObj, const char *pVariable,
465 vlc_value_t oldVal, vlc_value_t newVal,
468 VlcProc *pThis = (VlcProc*)pParam;
470 int i_id = newVal.i_int;
472 CmdGenericPtr ptrTree;
473 CmdPlaytreeDelete *pCmdTree = new CmdPlaytreeDelete( pThis->getIntf(),
475 ptrTree = CmdGenericPtr( pCmdTree );
477 // Push the command in the asynchronous command queue
478 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
479 pQueue->push( ptrTree , false );
485 int VlcProc::onPlaylistChange( vlc_object_t *pObj, const char *pVariable,
486 vlc_value_t oldval, vlc_value_t newval,
489 VlcProc *pThis = (VlcProc*)pParam;
490 input_item_t *p_item = static_cast<input_item_t*>(newval.p_address);
492 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
494 // Update the stream variable
495 pThis->updateStreamName();
497 // Create two playtree notify commands: one for old item, one for new
498 #if 0 /* FIXME: Heck, no! You cannot do that.
499 There is no warranty that the old item is still valid. */
500 CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(),
502 pQueue->push( CmdGenericPtr( pCmdTree ) , true );
504 CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(), p_item->i_id );
505 pQueue->push( CmdGenericPtr( pCmdTree ) , true );
511 int VlcProc::onSkinToLoad( vlc_object_t *pObj, const char *pVariable,
512 vlc_value_t oldVal, vlc_value_t newVal,
515 VlcProc *pThis = (VlcProc*)pParam;
517 // Create a playlist notify command
518 CmdChangeSkin *pCmd =
519 new CmdChangeSkin( pThis->getIntf(), newVal.psz_string );
521 // Push the command in the asynchronous command queue
522 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
523 pQueue->push( CmdGenericPtr( pCmd ) );
528 int VlcProc::onInteraction( vlc_object_t *pObj, const char *pVariable,
529 vlc_value_t oldVal, vlc_value_t newVal,
532 VlcProc *pThis = (VlcProc*)pParam;
533 interaction_dialog_t *p_dialog = (interaction_dialog_t *)(newVal.p_address);
535 CmdInteraction *pCmd = new CmdInteraction( pThis->getIntf(), p_dialog );
536 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
537 pQueue->push( CmdGenericPtr( pCmd ) );
542 void VlcProc::updateStreamName()
544 // Create a update item command
545 CmdUpdateItem *pCmdItem = new CmdUpdateItem( getIntf(), getStreamNameVar(), getStreamURIVar() );
547 // Push the command in the asynchronous command queue
548 AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
549 pQueue->push( CmdGenericPtr( pCmdItem ) );
552 int VlcProc::onEqBandsChange( vlc_object_t *pObj, const char *pVariable,
553 vlc_value_t oldVal, vlc_value_t newVal,
556 VlcProc *pThis = (VlcProc*)pParam;
558 // Post a set equalizer bands command
559 CmdSetEqBands *pCmd = new CmdSetEqBands( pThis->getIntf(),
562 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
563 pQueue->push( CmdGenericPtr( pCmd ) );
569 int VlcProc::onEqPreampChange( vlc_object_t *pObj, const char *pVariable,
570 vlc_value_t oldVal, vlc_value_t newVal,
573 VlcProc *pThis = (VlcProc*)pParam;
574 EqualizerPreamp *pVarPreamp = (EqualizerPreamp*)(pThis->m_cVarEqPreamp.get());
576 // Post a set preamp command
577 CmdSetEqPreamp *pCmd = new CmdSetEqPreamp( pThis->getIntf(), *pVarPreamp,
578 (newVal.f_float + 20.0) / 40.0 );
579 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
580 pQueue->push( CmdGenericPtr( pCmd ) );