1 /*****************************************************************************
2 * VlcWrapper.cpp: BeOS plugin for vlc (derived from MacOS X port)
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
7 * Authors: Florian G. Pflug <fgp@phlo.org>
8 * Jon Lech Johansen <jon-vl@nanocrew.net>
9 * Tony Casltey <tony@castley.net>
10 * Stephan Aßmus <stippi@yellowbites.com>
11 * Eric Petit <titer@videolan.org>
13 * This program is free software{} you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation{} either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY{} without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program{} if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
26 *****************************************************************************/
28 #include <InterfaceKit.h>
29 #include <SupportKit.h>
34 #include <vlc/input.h>
37 #include <audio_output.h>
38 #include <aout_internal.h>
41 #include "VlcWrapper.h"
44 const char * _AddEllipsis( char * string )
47 temp = (char*) calloc( strlen( string ) + 4, 1 );
48 sprintf( temp, "%s%s", string, B_UTF8_ELLIPSIS );
53 VlcWrapper::VlcWrapper( intf_thread_t *p_interface )
57 p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
62 VlcWrapper::~VlcWrapper()
65 vlc_object_release( p_input );
68 vlc_object_release( p_playlist );
71 /* UpdateInput: updates p_input */
72 void VlcWrapper::UpdateInput()
75 p_input = (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
81 vlc_object_release( p_input );
87 /***************************
88 * input infos and control *
89 ***************************/
91 bool VlcWrapper::HasInput()
93 return ( p_input != NULL );
96 int VlcWrapper::InputStatus()
103 return var_GetInteger( p_input, "state" );
106 int VlcWrapper::InputRate()
110 return INPUT_RATE_DEFAULT;
113 return var_GetInteger( p_input, "rate" );
116 void VlcWrapper::InputSetRate( int rate )
122 var_SetInteger( p_input, "rate", rate );
125 BList * VlcWrapper::GetChannels( int i_cat )
131 const char* fieldName;
137 what = SELECT_CHANNEL;
138 fieldName = "channel";
143 what = SELECT_SUBTITLE;
144 fieldName = "subtitle";
151 BList *list = new BList();
153 vlc_mutex_lock( &p_input->stream.stream_lock );
155 /* find which track is currently playing */
156 es_descriptor_t *p_es = NULL;
157 for( i = 0; i < p_input->stream.i_selected_es_number; i++ )
159 if( p_input->stream.pp_selected_es[i]->i_cat == i_cat )
160 p_es = p_input->stream.pp_selected_es[i];
163 /* build a list of all tracks */
169 message = new BMessage( what );
170 message->AddInt32( fieldName, -1 );
171 menuItem = new BMenuItem( _("None"), message );
173 menuItem->SetMarked( true );
174 list->AddItem( menuItem );
176 for( i = 0; i < p_input->stream.i_es_number; i++ )
178 if( p_input->stream.pp_es[i]->i_cat == i_cat )
180 message = new BMessage( what );
181 message->AddInt32( fieldName, i );
182 if( !p_input->stream.pp_es[i]->psz_desc ||
183 !*p_input->stream.pp_es[i]->psz_desc )
184 trackName = _("<unknown>");
186 trackName = strdup( p_input->stream.pp_es[i]->psz_desc );
187 menuItem = new BMenuItem( trackName, message );
188 if( p_input->stream.pp_es[i] == p_es )
189 menuItem->SetMarked( true );
190 list->AddItem( menuItem );
194 vlc_mutex_unlock( &p_input->stream.stream_lock );
202 void VlcWrapper::ToggleLanguage( int i_language )
205 var_SetInteger( p_input, "audio-es", -1 ); /* Disable audio */
207 var_SetInteger( p_input, "audio-es", i_language );
210 void VlcWrapper::ToggleSubtitle( int i_subtitle )
213 var_SetInteger( p_input, "spu-es", -1 ); /* Disable SPU */
215 var_SetInteger( p_input, "spu-es", i_subtitle );
218 const char * VlcWrapper::GetTimeAsString()
220 static char psz_time[ MSTRTIME_MAX_SIZE ];
228 var_Get( p_input, "time", &time );
230 mtime_t seconds = time.i_time / 1000000;
231 sprintf( psz_time, "%d:%02d:%02d",
232 (int) ( seconds / (60 * 60 ) ),
233 (int) ( ( seconds / 60 ) % 60 ),
234 (int) ( seconds % 60 ) );
239 float VlcWrapper::GetTimeAsFloat()
247 var_Get( p_input, "position", &pos );
251 void VlcWrapper::SetTimeAsFloat( float f_position )
259 pos.f_float = f_position / SEEKSLIDER_RANGE;
260 var_Set( p_input, "position", pos );
263 bool VlcWrapper::IsPlaying()
265 bool playing = false;
268 switch( var_GetInteger( p_input, "state" ) )
285 void VlcWrapper::OpenFiles( BList* o_files, bool replace, int32 index )
287 if ( o_files && o_files->CountItems() > 0)
289 int size = PlaylistSize();
290 bool wasEmpty = ( size < 1 );
292 index = PLAYLIST_END;
293 int mode = index == PLAYLIST_END ? PLAYLIST_APPEND : PLAYLIST_INSERT;
295 /* delete current playlist */
298 for( int i = 0; i < size; i++ )
300 playlist_Delete( p_playlist, 0 );
305 int32 count = o_files->CountItems();
306 for ( int32 i = count - 1; i >= 0; i-- )
308 if ( BString* o_file = (BString *)o_files->RemoveItem( i ) )
310 playlist_Add( p_playlist, o_file->String(),
311 o_file->String(), mode, index );
312 if ( mode == PLAYLIST_INSERT )
317 // TODO: implement a user setting
318 // if to start automatically
319 /* eventually restart playing */
320 if( replace || wasEmpty )
322 playlist_Stop( p_playlist );
323 playlist_Play( p_playlist );
328 void VlcWrapper::OpenDisc(BString o_type, BString o_device, int i_title, int i_chapter)
330 if( config_GetInt( p_intf, "beos-dvdmenus" ) )
331 o_device.Prepend( "dvdplay:" );
333 o_device.Prepend( "dvdold:" );
334 playlist_Add( p_playlist, o_device.String(), o_device.String(),
335 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
338 int VlcWrapper::PlaylistSize()
340 vlc_mutex_lock( &p_playlist->object_lock );
341 int i_size = p_playlist->i_size;
342 vlc_mutex_unlock( &p_playlist->object_lock );
346 char * VlcWrapper::PlaylistItemName( int i )
348 return p_playlist->pp_items[i]->input.psz_name;
351 int VlcWrapper::PlaylistCurrent()
353 return p_playlist->i_index;
356 bool VlcWrapper::PlaylistPlay()
360 playlist_Play( p_playlist );
365 void VlcWrapper::PlaylistPause()
369 var_SetInteger( p_input, "state", PAUSE_S );
373 void VlcWrapper::PlaylistStop()
375 playlist_Stop( p_playlist );
378 void VlcWrapper::PlaylistNext()
380 playlist_Next( p_playlist );
383 void VlcWrapper::PlaylistPrev()
385 playlist_Prev( p_playlist );
388 void VlcWrapper::GetPlaylistInfo( int32& currentIndex, int32& maxIndex )
394 vlc_mutex_lock( &p_playlist->object_lock );
396 maxIndex = p_playlist->i_size;
398 currentIndex = p_playlist->i_index/* + 1 -> why?!?*/;
402 vlc_mutex_unlock( &p_playlist->object_lock );
406 void VlcWrapper::PlaylistJumpTo( int pos )
408 playlist_Goto( p_playlist, pos );
411 void VlcWrapper::GetNavCapabilities( bool *canSkipPrev, bool *canSkipNext )
413 if ( canSkipPrev && canSkipNext )
415 // init the parameters
416 *canSkipPrev = false;
417 *canSkipNext = false;
419 int pos = PlaylistCurrent();
420 int size = PlaylistSize();
422 // see if we have got a stream going
426 vlc_mutex_lock( &p_input->stream.stream_lock );
428 bool hasTitles = p_input->stream.i_area_nb > 1;
429 int numChapters = p_input->stream.p_selected_area->i_part_nb;
430 bool hasChapters = numChapters > 1;
431 // first, look for chapters
434 *canSkipPrev = p_input->stream.p_selected_area->i_part > 0;
435 *canSkipNext = p_input->stream.p_selected_area->i_part <
436 p_input->stream.p_selected_area->i_part_nb - 1;
438 // if one of the skip capabilities is false,
439 // make it depend on titles instead
440 if ( !*canSkipPrev && hasTitles )
441 *canSkipPrev = p_input->stream.p_selected_area->i_id > 1;
442 if ( !*canSkipNext && hasTitles )
443 *canSkipNext = p_input->stream.p_selected_area->i_id <
444 p_input->stream.i_area_nb - 1;
446 vlc_mutex_unlock( &p_input->stream.stream_lock );
449 // last but not least, make capabilities depend on playlist
451 *canSkipPrev = pos > 0;
453 *canSkipNext = pos < size - 1;
457 void VlcWrapper::NavigatePrev()
459 bool hasSkiped = false;
461 // see if we have got a stream going
465 // get information from stream (lock it while looking at it)
466 vlc_mutex_lock( &p_input->stream.stream_lock );
468 int currentTitle = p_input->stream.p_selected_area->i_id;
469 int currentChapter = p_input->stream.p_selected_area->i_part;
470 int numTitles = p_input->stream.i_area_nb;
471 bool hasTitles = numTitles > 1;
472 int numChapters = p_input->stream.p_selected_area->i_part_nb;
473 bool hasChapters = numChapters > 1;
475 vlc_mutex_unlock( &p_input->stream.stream_lock );
477 // first, look for chapters
480 // skip to the previous chapter
483 if ( currentChapter >= 0 )
485 ToggleChapter( currentChapter );
489 // if we couldn't skip chapters, try titles instead
490 if ( !hasSkiped && hasTitles )
492 // skip to the previous title
494 // disallow area 0 since it is used for video_ts.vob
495 if( currentTitle > 0 )
497 ToggleTitle(currentTitle);
503 // last but not least, skip to previous file
508 void VlcWrapper::NavigateNext()
510 bool hasSkiped = false;
512 // see if we have got a stream going
516 // get information from stream (lock it while looking at it)
517 vlc_mutex_lock( &p_input->stream.stream_lock );
519 int currentTitle = p_input->stream.p_selected_area->i_id;
520 int currentChapter = p_input->stream.p_selected_area->i_part;
521 int numTitles = p_input->stream.i_area_nb;
522 bool hasTitles = numTitles > 1;
523 int numChapters = p_input->stream.p_selected_area->i_part_nb;
524 bool hasChapters = numChapters > 1;
526 vlc_mutex_unlock( &p_input->stream.stream_lock );
528 // first, look for chapters
531 // skip to the next chapter
533 if ( currentChapter < numChapters )
535 ToggleChapter( currentChapter );
539 // if we couldn't skip chapters, try titles instead
540 if ( !hasSkiped && hasTitles )
542 // skip to the next title
544 // disallow area 0 since it is used for video_ts.vob
545 if ( currentTitle < numTitles - 1 )
547 ToggleTitle(currentTitle);
553 // last but not least, skip to next file
558 /*************************
559 * Playlist manipulation *
560 *************************/
564 VlcWrapper::PlaylistLock() const
566 // TODO: search and destroy -> deadlock!
570 vlc_mutex_lock( &p_playlist->object_lock );
578 VlcWrapper::PlaylistUnlock() const
580 // TODO: search and destroy -> deadlock!
582 vlc_mutex_unlock( &p_playlist->object_lock );
587 VlcWrapper::PlaylistItemAt( int index ) const
589 playlist_item_t* item = NULL;
590 if ( index >= 0 && index < p_playlist->i_size )
591 item = p_playlist->pp_items[index];
595 // PlaylistRemoveItem
597 VlcWrapper::PlaylistRemoveItem( int index ) const
599 playlist_item_t* copy = NULL;
600 // check if item exists at the provided index
601 if ( index >= 0 && index < p_playlist->i_size )
603 playlist_item_t* item = p_playlist->pp_items[index];
606 // make a copy of the removed item
607 copy = (playlist_item_t*)PlaylistCloneItem( (void*)item );
608 // remove item from playlist (unfortunately, this frees it)
609 playlist_Delete( p_playlist, index );
615 // PlaylistRemoveItem
617 VlcWrapper::PlaylistRemoveItem( void* item ) const
619 playlist_item_t* copy = NULL;
620 for ( int32 i = 0; i < p_playlist->i_size; i++ )
622 if ( p_playlist->pp_items[i] == item )
624 copy = (playlist_item_t*)PlaylistRemoveItem( i );
633 VlcWrapper::PlaylistAddItem( void* item, int index ) const
637 playlist_AddItem( p_playlist, (playlist_item_t*)item,
638 PLAYLIST_INSERT, index );
640 // TODO: once playlist is returning useful info, return that instead
646 VlcWrapper::PlaylistCloneItem( void* castToItem ) const
648 playlist_item_t* copy = NULL;
649 playlist_item_t* item = (playlist_item_t*)castToItem;
652 copy = (playlist_item_t*)malloc( sizeof( playlist_item_t ) );
655 // make a copy of the item at index
657 copy->input.psz_name = strdup( item->input.psz_name );
658 copy->input.psz_uri = strdup( item->input.psz_uri );
664 // Careful! You need to know what you're doing here!
665 // The reason for having it, is to be able to deal with
666 // the rather lame list implementation of the playlist.
667 // It is meant to help manipulate the playlist with the above
668 // methods while keeping it valid.
670 // PlaylistSetPlaying
672 VlcWrapper::PlaylistSetPlaying( int index ) const
676 if ( index >= p_playlist->i_size )
677 index = p_playlist->i_size - 1;
678 p_playlist->i_index = index;
686 unsigned short VlcWrapper::GetVolume()
688 unsigned short i_volume;
689 aout_VolumeGet( p_intf, (audio_volume_t*)&i_volume );
693 void VlcWrapper::SetVolume( int value )
695 if ( p_intf->p_sys->b_mute )
697 p_intf->p_sys->b_mute = 0;
699 aout_VolumeSet( p_intf, value );
702 void VlcWrapper::VolumeMute()
704 aout_VolumeMute( p_intf, NULL );
705 p_intf->p_sys->b_mute = 1;
708 void VlcWrapper::VolumeRestore()
710 audio_volume_t dummy;
711 aout_VolumeMute( p_intf, &dummy );
712 p_intf->p_sys->b_mute = 0;
715 bool VlcWrapper::IsMuted()
717 return p_intf->p_sys->b_mute;
724 bool VlcWrapper::IsUsingMenus()
729 vlc_mutex_lock( &p_playlist->object_lock );
730 if( p_playlist->i_index < 0 )
732 vlc_mutex_unlock( &p_playlist->object_lock );
736 char * psz_name = p_playlist->pp_items[p_playlist->i_index]->input.psz_name;
737 if( !strncmp( psz_name, "dvdplay:", 8 ) )
739 vlc_mutex_unlock( &p_playlist->object_lock );
742 vlc_mutex_unlock( &p_playlist->object_lock );
747 bool VlcWrapper::HasTitles()
756 BList * VlcWrapper::GetTitles()
760 BList *list = new BList();
762 vlc_mutex_lock( &p_input->stream.stream_lock );
767 for( unsigned int i = 1; i < p_input->stream.i_area_nb; i++ )
769 message = new BMessage( TOGGLE_TITLE );
770 message->AddInt32( "index", i );
771 BString helper( "" );
773 menuItem = new BMenuItem( helper.String(), message );
774 menuItem->SetMarked( p_input->stream.p_selected_area->i_id == i );
775 list->AddItem( menuItem );
778 vlc_mutex_unlock( &p_input->stream.stream_lock );
785 void VlcWrapper::PrevTitle()
787 var_SetVoid( p_input, "prev-title" );
790 void VlcWrapper::NextTitle()
792 var_SetVoid( p_input, "next-title" );
795 void VlcWrapper::ToggleTitle(int i_title)
797 if( p_input != NULL )
799 var_SetInteger( p_input, "title", i_title );
803 void VlcWrapper::TitleInfo( int32 ¤tIndex, int32 &maxIndex )
810 vlc_mutex_lock( &p_input->stream.stream_lock );
812 maxIndex = p_input->stream.i_area_nb - 1;
814 currentIndex = p_input->stream.p_selected_area->i_id;
818 vlc_mutex_unlock( &p_input->stream.stream_lock );
823 bool VlcWrapper::HasChapters()
833 BList * VlcWrapper::GetChapters()
837 BList *list = new BList();
839 vlc_mutex_lock( &p_input->stream.stream_lock );
844 for( unsigned int i = 1;
845 i < p_input->stream.p_selected_area->i_part_nb + 1; i++ )
847 message = new BMessage( TOGGLE_CHAPTER );
848 message->AddInt32( "index", i );
849 BString helper( "" );
851 menuItem = new BMenuItem( helper.String(), message );
852 menuItem->SetMarked( p_input->stream.p_selected_area->i_part == i );
853 list->AddItem( menuItem );
856 vlc_mutex_unlock( &p_input->stream.stream_lock );
863 void VlcWrapper::PrevChapter()
865 var_SetVoid( p_input, "prev-chapter" );
868 void VlcWrapper::NextChapter()
870 var_SetVoid( p_input, "next-chapter" );
873 void VlcWrapper::ToggleChapter(int i_chapter)
875 if( p_input != NULL )
877 var_SetInteger( p_input, "chapter", i_chapter );
881 void VlcWrapper::ChapterInfo( int32 ¤tIndex, int32 &maxIndex )
888 vlc_mutex_lock( &p_input->stream.stream_lock );
890 maxIndex = p_input->stream.p_selected_area->i_part_nb - 1;
892 currentIndex = p_input->stream.p_selected_area->i_part;
896 vlc_mutex_unlock( &p_input->stream.stream_lock );
905 void VlcWrapper::LoadSubFile( const char * psz_file )
907 config_PutPsz( p_intf, "sub-file", strdup( psz_file ) );
910 void VlcWrapper::FilterChange()
916 vout_thread_t * p_vout;
917 vlc_mutex_lock( &p_input->stream.stream_lock );
919 // Warn the vout we are about to change the filter chain
920 p_vout = (vout_thread_t*)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
924 p_vout->b_filter_change = VLC_TRUE;
925 vlc_object_release( p_vout );
928 // restart all video stream
929 for( unsigned int i = 0; i < p_input->stream.i_es_number; i++ )
931 if( ( p_input->stream.pp_es[i]->i_cat == VIDEO_ES ) &&
932 ( p_input->stream.pp_es[i]->p_dec != NULL ) )
934 input_UnselectES( p_input, p_input->stream.pp_es[i] );
935 input_SelectES( p_input, p_input->stream.pp_es[i] );
938 vlc_mutex_unlock( &p_input->stream.stream_lock );