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 "../commands/cmd_audio.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 volume sound changes
142 var_AddCallback( pIntf->p_libvlc, "volume-change",
143 onVolumeChanged, this );
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_libvlc, "volume-change",
188 onVolumeChanged, this );
189 var_DelCallback( getIntf()->p_sys->p_playlist, "intf-change",
190 onIntfChange, this );
191 var_DelCallback( getIntf()->p_sys->p_playlist, "playlist-item-append",
192 onItemAppend, this );
193 var_DelCallback( getIntf()->p_sys->p_playlist, "playlist-item-deleted",
194 onItemDelete, this );
195 var_DelCallback( getIntf()->p_libvlc, "intf-show",
197 var_DelCallback( getIntf()->p_sys->p_playlist, "item-current",
198 onPlaylistChange, this );
199 var_DelCallback( getIntf()->p_sys->p_playlist, "item-change",
200 onItemChange, this );
201 var_DelCallback( getIntf(), "skin-to-load", onSkinToLoad, this );
202 var_DelCallback( getIntf(), "interaction", onInteraction, this );
205 void VlcProc::manage()
207 // Did the user request to quit vlc ?
208 if( !vlc_object_alive( getIntf() ) )
210 // Get the instance of OSFactory
211 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
213 // Exit the main OS loop
214 pOsFactory->getOSLoop()->exit();
224 void VlcProc::CmdManage::execute()
226 // Just forward to VlcProc
230 void VlcProc::refreshAudio()
233 aout_instance_t *pAout = NULL;
235 playlist_t *pPlaylist = getIntf()->p_sys->p_playlist;
236 input_thread_t *pInput = playlist_CurrentInput( pPlaylist );
238 pAout = input_GetAout( pInput );
242 // Check if the audio output has changed
243 if( pAout != m_pAout )
245 // Register the equalizer callbacks
246 if( !var_AddCallback( pAout, "equalizer-bands",
247 onEqBandsChange, this ) &&
248 !var_AddCallback( pAout, "equalizer-preamp",
249 onEqPreampChange, this ) )
252 //char * psz_bands = var_GetString( p_aout, "equalizer-bands" );
255 // Get the audio filters
256 pFilters = var_GetNonEmptyString( pAout, "audio-filter" );
260 // Get the audio filters
261 pFilters = config_GetPsz( getIntf(), "audio-filter" );
264 // Refresh the equalizer variable
265 VarBoolImpl *pVarEqualizer = (VarBoolImpl*)m_cVarEqualizer.get();
266 pVarEqualizer->set( pFilters && strstr( pFilters, "equalizer" ) );
270 vlc_object_release( pAout );
272 vlc_object_release( pInput );
275 void VlcProc::refreshVolume()
277 // Refresh sound volume
278 audio_volume_t volume;
279 aout_VolumeGet( getIntf()->p_sys->p_playlist, &volume );
280 Volume *pVolume = (Volume*)m_cVarVolume.get();
281 pVolume->set( (double)volume * 2.0 / AOUT_VOLUME_MAX , false );
283 // Set the mute variable
284 VarBoolImpl *pVarMute = (VarBoolImpl*)m_cVarMute.get();
285 pVarMute->set( volume == 0 );
288 void VlcProc::refreshPlaylist()
290 // Refresh the random variable
291 VarBoolImpl *pVarRandom = (VarBoolImpl*)m_cVarRandom.get();
293 var_Get( getIntf()->p_sys->p_playlist, "random", &val );
294 pVarRandom->set( val.b_bool != 0 );
296 // Refresh the loop variable
297 VarBoolImpl *pVarLoop = (VarBoolImpl*)m_cVarLoop.get();
298 var_Get( getIntf()->p_sys->p_playlist, "loop", &val );
299 pVarLoop->set( val.b_bool != 0 );
301 // Refresh the repeat variable
302 VarBoolImpl *pVarRepeat = (VarBoolImpl*)m_cVarRepeat.get();
303 var_Get( getIntf()->p_sys->p_playlist, "repeat", &val );
304 pVarRepeat->set( val.b_bool != 0 );
307 void VlcProc::refreshInput()
309 StreamTime *pTime = (StreamTime*)m_cVarTime.get();
310 VarBoolImpl *pVarSeekable = (VarBoolImpl*)m_cVarSeekable.get();
311 VarBoolImpl *pVarDvdActive = (VarBoolImpl*)m_cVarDvdActive.get();
312 VarBoolImpl *pVarHasVout = (VarBoolImpl*)m_cVarHasVout.get();
313 VarBoolImpl *pVarHasAudio = (VarBoolImpl*)m_cVarHasAudio.get();
314 VarText *pBitrate = (VarText*)m_cVarStreamBitRate.get();
315 VarText *pSampleRate = (VarText*)m_cVarStreamSampleRate.get();
316 VarBoolImpl *pVarFullscreen = (VarBoolImpl*)m_cVarFullscreen.get();
317 VarBoolImpl *pVarPlaying = (VarBoolImpl*)m_cVarPlaying.get();
318 VarBoolImpl *pVarStopped = (VarBoolImpl*)m_cVarStopped.get();
319 VarBoolImpl *pVarPaused = (VarBoolImpl*)m_cVarPaused.get();
322 if( getIntf()->p_sys->p_input == NULL )
324 getIntf()->p_sys->p_input =
325 playlist_CurrentInput( getIntf()->p_sys->p_playlist );
327 else if( getIntf()->p_sys->p_input->b_dead )
329 vlc_object_release( getIntf()->p_sys->p_input );
330 getIntf()->p_sys->p_input = NULL;
333 input_thread_t *pInput = getIntf()->p_sys->p_input;
335 if( pInput && vlc_object_alive (pInput) )
337 // Refresh time variables
339 var_Get( pInput, "position", &pos );
340 pTime->set( pos.f_float, false );
341 pVarSeekable->set( pos.f_float != 0.0 );
343 // Refresh DVD detection
344 vlc_value_t chapters_count;
345 var_Change( pInput, "chapter", VLC_VAR_CHOICESCOUNT,
346 &chapters_count, NULL );
347 pVarDvdActive->set( chapters_count.i_int > 0 );
349 // Get the input bitrate
350 int bitrate = var_GetInteger( pInput, "bit-rate" ) / 1000;
351 pBitrate->set( UString::fromInt( getIntf(), bitrate ) );
353 // Get the audio sample rate
354 int sampleRate = var_GetInteger( pInput, "sample-rate" ) / 1000;
355 pSampleRate->set( UString::fromInt( getIntf(), sampleRate ) );
358 vlc_value_t audio_es;
359 var_Change( pInput, "audio-es", VLC_VAR_CHOICESCOUNT,
361 pVarHasAudio->set( audio_es.i_int > 0 );
363 // Refresh fullscreen status
364 vout_thread_t *pVout = input_GetVout( pInput );
365 pVarHasVout->set( pVout != NULL );
368 pVarFullscreen->set( var_GetBool( pVout, "fullscreen" ) );
369 vlc_object_release( pVout );
372 // Refresh play/pause status
373 int state = var_GetInteger( pInput, "state" );
374 pVarStopped->set( false );
375 pVarPlaying->set( state != PAUSE_S );
376 pVarPaused->set( state == PAUSE_S );
380 pVarSeekable->set( false );
381 pVarDvdActive->set( false );
382 pTime->set( 0, false );
383 pVarFullscreen->set( false );
384 pVarHasAudio->set( false );
385 pVarHasVout->set( false );
386 pVarStopped->set( true );
387 pVarPlaying->set( false );
388 pVarPaused->set( false );
392 int VlcProc::onVolumeChanged( vlc_object_t *pObj, const char *pVariable,
393 vlc_value_t oldVal, vlc_value_t newVal,
396 VlcProc *pThis = (VlcProc*)pParam;
398 // Create a playtree notify command (for new style playtree)
399 CmdVolumeChanged *pCmd = new CmdVolumeChanged( pThis->getIntf() );
401 // Push the command in the asynchronous command queue
402 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
403 pQueue->push( CmdGenericPtr( pCmd ) );
409 int VlcProc::onIntfChange( vlc_object_t *pObj, const char *pVariable,
410 vlc_value_t oldVal, vlc_value_t newVal,
413 VlcProc *pThis = (VlcProc*)pParam;
415 // Update the stream variable
416 pThis->updateStreamName();
418 // Create a playtree notify command (for new style playtree)
419 CmdPlaytreeChanged *pCmdTree = new CmdPlaytreeChanged( pThis->getIntf() );
421 // Push the command in the asynchronous command queue
422 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
423 pQueue->push( CmdGenericPtr( pCmdTree ) );
429 int VlcProc::onIntfShow( vlc_object_t *pObj, const char *pVariable,
430 vlc_value_t oldVal, vlc_value_t newVal,
435 VlcProc *pThis = (VlcProc*)pParam;
437 // Create a raise all command
438 CmdRaiseAll *pCmd = new CmdRaiseAll( pThis->getIntf(),
439 pThis->getIntf()->p_sys->p_theme->getWindowManager() );
441 // Push the command in the asynchronous command queue
442 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
443 pQueue->push( CmdGenericPtr( pCmd ) );
450 int VlcProc::onItemChange( vlc_object_t *pObj, const char *pVariable,
451 vlc_value_t oldval, vlc_value_t newval,
454 VlcProc *pThis = (VlcProc*)pParam;
455 input_item_t *p_item = static_cast<input_item_t*>(newval.p_address);
457 // Update the stream variable
458 pThis->updateStreamName();
460 // Create a playtree notify command
461 CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(),
464 // Push the command in the asynchronous command queue
465 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
466 pQueue->push( CmdGenericPtr( pCmdTree ), true );
471 int VlcProc::onItemAppend( vlc_object_t *pObj, const char *pVariable,
472 vlc_value_t oldVal, vlc_value_t newVal,
475 VlcProc *pThis = (VlcProc*)pParam;
477 playlist_add_t *p_add = (playlist_add_t*)malloc( sizeof(
480 memcpy( p_add, newVal.p_address, sizeof( playlist_add_t ) ) ;
482 CmdGenericPtr ptrTree;
483 CmdPlaytreeAppend *pCmdTree = new CmdPlaytreeAppend( pThis->getIntf(),
485 ptrTree = CmdGenericPtr( pCmdTree );
487 // Push the command in the asynchronous command queue
488 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
489 pQueue->push( ptrTree , false );
494 int VlcProc::onItemDelete( vlc_object_t *pObj, const char *pVariable,
495 vlc_value_t oldVal, vlc_value_t newVal,
498 VlcProc *pThis = (VlcProc*)pParam;
500 int i_id = newVal.i_int;
502 CmdGenericPtr ptrTree;
503 CmdPlaytreeDelete *pCmdTree = new CmdPlaytreeDelete( pThis->getIntf(),
505 ptrTree = CmdGenericPtr( pCmdTree );
507 // Push the command in the asynchronous command queue
508 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
509 pQueue->push( ptrTree , false );
515 int VlcProc::onPlaylistChange( vlc_object_t *pObj, const char *pVariable,
516 vlc_value_t oldval, vlc_value_t newval,
519 VlcProc *pThis = (VlcProc*)pParam;
520 input_item_t *p_item = static_cast<input_item_t*>(newval.p_address);
522 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
524 // Update the stream variable
525 pThis->updateStreamName();
527 // Create two playtree notify commands: one for old item, one for new
528 #if 0 /* FIXME: Heck, no! You cannot do that.
529 There is no warranty that the old item is still valid. */
530 CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(),
532 pQueue->push( CmdGenericPtr( pCmdTree ) , true );
534 CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(), p_item->i_id );
535 pQueue->push( CmdGenericPtr( pCmdTree ) , true );
541 int VlcProc::onSkinToLoad( vlc_object_t *pObj, const char *pVariable,
542 vlc_value_t oldVal, vlc_value_t newVal,
545 VlcProc *pThis = (VlcProc*)pParam;
547 // Create a playlist notify command
548 CmdChangeSkin *pCmd =
549 new CmdChangeSkin( pThis->getIntf(), newVal.psz_string );
551 // Push the command in the asynchronous command queue
552 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
553 pQueue->push( CmdGenericPtr( pCmd ) );
558 int VlcProc::onInteraction( vlc_object_t *pObj, const char *pVariable,
559 vlc_value_t oldVal, vlc_value_t newVal,
562 VlcProc *pThis = (VlcProc*)pParam;
563 interaction_dialog_t *p_dialog = (interaction_dialog_t *)(newVal.p_address);
565 CmdInteraction *pCmd = new CmdInteraction( pThis->getIntf(), p_dialog );
566 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
567 pQueue->push( CmdGenericPtr( pCmd ) );
572 void VlcProc::updateStreamName()
574 // Create a update item command
575 CmdUpdateItem *pCmdItem = new CmdUpdateItem( getIntf(), getStreamNameVar(), getStreamURIVar() );
577 // Push the command in the asynchronous command queue
578 AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
579 pQueue->push( CmdGenericPtr( pCmdItem ) );
582 int VlcProc::onEqBandsChange( vlc_object_t *pObj, const char *pVariable,
583 vlc_value_t oldVal, vlc_value_t newVal,
586 VlcProc *pThis = (VlcProc*)pParam;
588 // Post a set equalizer bands command
589 CmdSetEqBands *pCmd = new CmdSetEqBands( pThis->getIntf(),
592 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
593 pQueue->push( CmdGenericPtr( pCmd ) );
599 int VlcProc::onEqPreampChange( vlc_object_t *pObj, const char *pVariable,
600 vlc_value_t oldVal, vlc_value_t newVal,
603 VlcProc *pThis = (VlcProc*)pParam;
604 EqualizerPreamp *pVarPreamp = (EqualizerPreamp*)(pThis->m_cVarEqPreamp.get());
606 // Post a set preamp command
607 CmdSetEqPreamp *pCmd = new CmdSetEqPreamp( pThis->getIntf(), *pVarPreamp,
608 (newVal.f_float + 20.0) / 40.0 );
609 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
610 pQueue->push( CmdGenericPtr( pCmd ) );