1 /*****************************************************************************
2 * VlcWrapper.cpp: BeOS plugin for vlc (derived from MacOS X port)
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: VlcWrapper.cpp,v 1.37 2003/09/07 22:53:09 fenrir Exp $
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>
36 #include <input_ext-plugins.h> // needed here when compiling without plugins
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()
101 return p_input->stream.control.i_status;
104 int VlcWrapper::InputRate()
109 return p_input->stream.control.i_rate;
112 void VlcWrapper::InputSetRate( int rate )
117 input_SetRate( p_input, rate );
120 BList * VlcWrapper::GetChannels( int i_cat )
126 const char* fieldName;
132 what = SELECT_CHANNEL;
133 fieldName = "channel";
138 what = SELECT_SUBTITLE;
139 fieldName = "subtitle";
146 vlc_mutex_lock( &p_input->stream.stream_lock );
148 /* find which track is currently playing */
149 es_descriptor_t *p_es = NULL;
150 for( i = 0; i < p_input->stream.i_selected_es_number; i++ )
152 if( p_input->stream.pp_selected_es[i]->i_cat == i_cat )
153 p_es = p_input->stream.pp_selected_es[i];
156 /* build a list of all tracks */
157 BList *list = new BList( p_input->stream.i_es_number );
163 message = new BMessage( what );
164 message->AddInt32( fieldName, -1 );
165 menuItem = new BMenuItem( _("None"), message );
167 menuItem->SetMarked( true );
168 list->AddItem( menuItem );
170 for( i = 0; i < p_input->stream.i_es_number; i++ )
172 if( p_input->stream.pp_es[i]->i_cat == i_cat )
174 message = new BMessage( what );
175 message->AddInt32( fieldName, i );
176 if( !p_input->stream.pp_es[i]->psz_desc ||
177 !*p_input->stream.pp_es[i]->psz_desc )
178 trackName = _("<unknown>");
180 trackName = strdup( p_input->stream.pp_es[i]->psz_desc );
181 menuItem = new BMenuItem( trackName, message );
182 if( p_input->stream.pp_es[i] == p_es )
183 menuItem->SetMarked( true );
184 list->AddItem( menuItem );
188 vlc_mutex_unlock( &p_input->stream.stream_lock );
195 void VlcWrapper::ToggleLanguage( int i_language )
197 es_descriptor_t * p_es = NULL;
198 es_descriptor_t * p_es_old = NULL;
200 vlc_mutex_lock( &p_input->stream.stream_lock );
201 for( unsigned int i = 0; i < p_input->stream.i_selected_es_number ; i++ )
203 if( p_input->stream.pp_selected_es[i]->i_cat == AUDIO_ES )
205 p_es_old = p_input->stream.pp_selected_es[i];
209 vlc_mutex_unlock( &p_input->stream.stream_lock );
211 if( i_language != -1 )
213 p_es = p_input->stream.pp_es[i_language];
215 if( p_es == p_es_old )
221 input_ToggleES( p_input, p_es_old, VLC_FALSE );
225 input_ToggleES( p_input, p_es, VLC_TRUE );
229 void VlcWrapper::ToggleSubtitle( int i_subtitle )
231 es_descriptor_t * p_es = NULL;
232 es_descriptor_t * p_es_old = NULL;
234 vlc_mutex_lock( &p_input->stream.stream_lock );
235 for( unsigned int i = 0; i < p_input->stream.i_selected_es_number ; i++ )
237 if( p_input->stream.pp_selected_es[i]->i_cat == SPU_ES )
239 p_es_old = p_input->stream.pp_selected_es[i];
243 vlc_mutex_unlock( &p_input->stream.stream_lock );
245 if( i_subtitle != -1 )
247 p_es = p_input->stream.pp_es[i_subtitle];
249 if( p_es == p_es_old )
255 input_ToggleES( p_input, p_es_old, VLC_FALSE );
259 input_ToggleES( p_input, p_es, VLC_TRUE );
263 const char * VlcWrapper::GetTimeAsString()
265 static char psz_currenttime[ OFFSETTOTIME_MAX_SIZE ];
267 if( p_input == NULL )
272 input_OffsetToTime( p_input,
274 p_input->stream.p_selected_area->i_tell );
276 return(psz_currenttime);
279 float VlcWrapper::GetTimeAsFloat()
283 if( p_input != NULL )
285 f_time = (float)p_input->stream.p_selected_area->i_tell /
286 (float)p_input->stream.p_selected_area->i_size;
295 void VlcWrapper::SetTimeAsFloat( float f_position )
297 if( p_input != NULL )
300 (long long)(p_input->stream.p_selected_area->i_size
301 * f_position / SEEKSLIDER_RANGE ),
306 bool VlcWrapper::IsPlaying()
309 bool playing = false;
312 switch ( p_input->stream.control.i_status )
333 void VlcWrapper::OpenFiles( BList* o_files, bool replace, int32 index )
335 if ( o_files && o_files->CountItems() > 0)
337 int size = PlaylistSize();
338 bool wasEmpty = ( size < 1 );
340 index = PLAYLIST_END;
341 int mode = index == PLAYLIST_END ? PLAYLIST_APPEND : PLAYLIST_INSERT;
343 /* delete current playlist */
346 for( int i = 0; i < size; i++ )
348 playlist_Delete( p_playlist, 0 );
353 int32 count = o_files->CountItems();
354 for ( int32 i = count - 1; i >= 0; i-- )
356 if ( BString* o_file = (BString *)o_files->RemoveItem( i ) )
358 playlist_Add( p_playlist, o_file->String(),
360 if ( mode == PLAYLIST_INSERT )
365 // TODO: implement a user setting
366 // if to start automatically
367 /* eventually restart playing */
368 if( replace || wasEmpty )
370 playlist_Stop( p_playlist );
371 playlist_Play( p_playlist );
376 void VlcWrapper::OpenDisc(BString o_type, BString o_device, int i_title, int i_chapter)
378 if( config_GetInt( p_intf, "beos-dvdmenus" ) )
379 o_device.Prepend( "dvdplay:" );
381 o_device.Prepend( "dvdold:" );
382 playlist_Add( p_playlist, o_device.String(), 0, 0,
383 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
386 int VlcWrapper::PlaylistSize()
388 vlc_mutex_lock( &p_playlist->object_lock );
389 int i_size = p_playlist->i_size;
390 vlc_mutex_unlock( &p_playlist->object_lock );
394 char * VlcWrapper::PlaylistItemName( int i )
396 return p_playlist->pp_items[i]->psz_name;
399 int VlcWrapper::PlaylistCurrent()
401 return p_playlist->i_index;
404 bool VlcWrapper::PlaylistPlay()
408 playlist_Play( p_playlist );
413 void VlcWrapper::PlaylistPause()
417 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
421 void VlcWrapper::PlaylistStop()
423 playlist_Stop( p_playlist );
426 void VlcWrapper::PlaylistNext()
428 playlist_Next( p_playlist );
431 void VlcWrapper::PlaylistPrev()
433 playlist_Prev( p_playlist );
436 void VlcWrapper::GetPlaylistInfo( int32& currentIndex, int32& maxIndex )
442 vlc_mutex_lock( &p_playlist->object_lock );
444 maxIndex = p_playlist->i_size;
446 currentIndex = p_playlist->i_index/* + 1 -> why?!?*/;
450 vlc_mutex_unlock( &p_playlist->object_lock );
454 void VlcWrapper::PlaylistJumpTo( int pos )
456 playlist_Goto( p_playlist, pos );
459 void VlcWrapper::GetNavCapabilities( bool *canSkipPrev, bool *canSkipNext )
461 if ( canSkipPrev && canSkipNext )
463 // init the parameters
464 *canSkipPrev = false;
465 *canSkipNext = false;
467 int pos = PlaylistCurrent();
468 int size = PlaylistSize();
470 // see if we have got a stream going
473 vlc_mutex_lock( &p_input->stream.stream_lock );
475 bool hasTitles = p_input->stream.i_area_nb > 1;
476 int numChapters = p_input->stream.p_selected_area->i_part_nb;
477 bool hasChapters = numChapters > 1;
478 // first, look for chapters
481 *canSkipPrev = p_input->stream.p_selected_area->i_part > 0;
482 *canSkipNext = p_input->stream.p_selected_area->i_part <
483 p_input->stream.p_selected_area->i_part_nb - 1;
485 // if one of the skip capabilities is false,
486 // make it depend on titles instead
487 if ( !*canSkipPrev && hasTitles )
488 *canSkipPrev = p_input->stream.p_selected_area->i_id > 1;
489 if ( !*canSkipNext && hasTitles )
490 *canSkipNext = p_input->stream.p_selected_area->i_id <
491 p_input->stream.i_area_nb - 1;
493 vlc_mutex_unlock( &p_input->stream.stream_lock );
495 // last but not least, make capabilities depend on playlist
497 *canSkipPrev = pos > 0;
499 *canSkipNext = pos < size - 1;
503 void VlcWrapper::NavigatePrev()
505 bool hasSkiped = false;
507 // see if we have got a stream going
510 // get information from stream (lock it while looking at it)
511 vlc_mutex_lock( &p_input->stream.stream_lock );
513 int currentTitle = p_input->stream.p_selected_area->i_id;
514 int currentChapter = p_input->stream.p_selected_area->i_part;
515 int numTitles = p_input->stream.i_area_nb;
516 bool hasTitles = numTitles > 1;
517 int numChapters = p_input->stream.p_selected_area->i_part_nb;
518 bool hasChapters = numChapters > 1;
520 vlc_mutex_unlock( &p_input->stream.stream_lock );
522 // first, look for chapters
525 // skip to the previous chapter
528 if ( currentChapter >= 0 )
530 ToggleChapter( currentChapter );
534 // if we couldn't skip chapters, try titles instead
535 if ( !hasSkiped && hasTitles )
537 // skip to the previous title
539 // disallow area 0 since it is used for video_ts.vob
540 if( currentTitle > 0 )
542 ToggleTitle(currentTitle);
548 // last but not least, skip to previous file
553 void VlcWrapper::NavigateNext()
555 bool hasSkiped = false;
557 // see if we have got a stream going
560 // get information from stream (lock it while looking at it)
561 vlc_mutex_lock( &p_input->stream.stream_lock );
563 int currentTitle = p_input->stream.p_selected_area->i_id;
564 int currentChapter = p_input->stream.p_selected_area->i_part;
565 int numTitles = p_input->stream.i_area_nb;
566 bool hasTitles = numTitles > 1;
567 int numChapters = p_input->stream.p_selected_area->i_part_nb;
568 bool hasChapters = numChapters > 1;
570 vlc_mutex_unlock( &p_input->stream.stream_lock );
572 // first, look for chapters
575 // skip to the next chapter
577 if ( currentChapter < numChapters )
579 ToggleChapter( currentChapter );
583 // if we couldn't skip chapters, try titles instead
584 if ( !hasSkiped && hasTitles )
586 // skip to the next title
588 // disallow area 0 since it is used for video_ts.vob
589 if ( currentTitle < numTitles - 1 )
591 ToggleTitle(currentTitle);
597 // last but not least, skip to next file
602 /*************************
603 * Playlist manipulation *
604 *************************/
608 VlcWrapper::PlaylistLock() const
610 // TODO: search and destroy -> deadlock!
614 vlc_mutex_lock( &p_playlist->object_lock );
622 VlcWrapper::PlaylistUnlock() const
624 // TODO: search and destroy -> deadlock!
626 vlc_mutex_unlock( &p_playlist->object_lock );
631 VlcWrapper::PlaylistItemAt( int index ) const
633 playlist_item_t* item = NULL;
634 if ( index >= 0 && index < p_playlist->i_size )
635 item = p_playlist->pp_items[index];
639 // PlaylistRemoveItem
641 VlcWrapper::PlaylistRemoveItem( int index ) const
643 playlist_item_t* copy = NULL;
644 // check if item exists at the provided index
645 if ( index >= 0 && index < p_playlist->i_size )
647 playlist_item_t* item = p_playlist->pp_items[index];
650 // make a copy of the removed item
651 copy = (playlist_item_t*)PlaylistCloneItem( (void*)item );
652 // remove item from playlist (unfortunately, this frees it)
653 playlist_Delete( p_playlist, index );
659 // PlaylistRemoveItem
661 VlcWrapper::PlaylistRemoveItem( void* item ) const
663 playlist_item_t* copy = NULL;
664 for ( int32 i = 0; i < p_playlist->i_size; i++ )
666 if ( p_playlist->pp_items[i] == item )
668 copy = (playlist_item_t*)PlaylistRemoveItem( i );
677 VlcWrapper::PlaylistAddItem( void* item, int index ) const
681 playlist_AddItem( p_playlist, (playlist_item_t*)item,
682 PLAYLIST_INSERT, index );
684 // TODO: once playlist is returning useful info, return that instead
690 VlcWrapper::PlaylistCloneItem( void* castToItem ) const
692 playlist_item_t* copy = NULL;
693 playlist_item_t* item = (playlist_item_t*)castToItem;
696 copy = (playlist_item_t*)malloc( sizeof( playlist_item_t ) );
699 // make a copy of the item at index
700 copy->psz_name = strdup( item->psz_name );
701 copy->psz_uri = strdup( item->psz_uri );
702 copy->i_type = item->i_type;
703 copy->i_status = item->i_status;
704 copy->b_autodeletion = item->b_autodeletion;
710 // Careful! You need to know what you're doing here!
711 // The reason for having it, is to be able to deal with
712 // the rather lame list implementation of the playlist.
713 // It is meant to help manipulate the playlist with the above
714 // methods while keeping it valid.
716 // PlaylistSetPlaying
718 VlcWrapper::PlaylistSetPlaying( int index ) const
722 if ( index >= p_playlist->i_size )
723 index = p_playlist->i_size - 1;
724 p_playlist->i_index = index;
732 unsigned short VlcWrapper::GetVolume()
734 unsigned short i_volume;
735 aout_VolumeGet( p_intf, (audio_volume_t*)&i_volume );
739 void VlcWrapper::SetVolume( int value )
741 if ( p_intf->p_sys->b_mute )
743 p_intf->p_sys->b_mute = 0;
745 aout_VolumeSet( p_intf, value );
748 void VlcWrapper::VolumeMute()
750 aout_VolumeMute( p_intf, NULL );
751 p_intf->p_sys->b_mute = 1;
754 void VlcWrapper::VolumeRestore()
756 audio_volume_t dummy;
757 aout_VolumeMute( p_intf, &dummy );
758 p_intf->p_sys->b_mute = 0;
761 bool VlcWrapper::IsMuted()
763 return p_intf->p_sys->b_mute;
770 bool VlcWrapper::IsUsingMenus()
775 vlc_mutex_lock( &p_playlist->object_lock );
776 if( p_playlist->i_index < 0 )
778 vlc_mutex_unlock( &p_playlist->object_lock );
782 char * psz_name = p_playlist->pp_items[p_playlist->i_index]->psz_name;
783 if( !strncmp( psz_name, "dvdplay:", 8 ) )
785 vlc_mutex_unlock( &p_playlist->object_lock );
788 vlc_mutex_unlock( &p_playlist->object_lock );
793 bool VlcWrapper::HasTitles()
798 return ( p_input->stream.i_area_nb > 1 );
801 BList * VlcWrapper::GetTitles()
805 vlc_mutex_lock( &p_input->stream.stream_lock );
807 BList *list = new BList( p_input->stream.i_area_nb );
811 for( unsigned int i = 1; i < p_input->stream.i_area_nb; i++ )
813 message = new BMessage( TOGGLE_TITLE );
814 message->AddInt32( "index", i );
815 BString helper( "" );
817 menuItem = new BMenuItem( helper.String(), message );
818 menuItem->SetMarked( p_input->stream.p_selected_area->i_id == i );
819 list->AddItem( menuItem );
822 vlc_mutex_unlock( &p_input->stream.stream_lock );
829 void VlcWrapper::PrevTitle()
832 i_id = p_input->stream.p_selected_area->i_id - 1;
839 void VlcWrapper::NextTitle()
842 i_id = p_input->stream.p_selected_area->i_id + 1;
843 if( i_id < p_input->stream.i_area_nb )
849 void VlcWrapper::ToggleTitle(int i_title)
851 if( p_input != NULL )
853 input_ChangeArea( p_input,
854 p_input->stream.pp_areas[i_title] );
856 vlc_mutex_lock( &p_input->stream.stream_lock );
858 vlc_mutex_unlock( &p_input->stream.stream_lock );
862 void VlcWrapper::TitleInfo( int32 ¤tIndex, int32 &maxIndex )
868 vlc_mutex_lock( &p_input->stream.stream_lock );
870 maxIndex = p_input->stream.i_area_nb - 1;
872 currentIndex = p_input->stream.p_selected_area->i_id;
876 vlc_mutex_unlock( &p_input->stream.stream_lock );
880 bool VlcWrapper::HasChapters()
886 return ( p_input->stream.p_selected_area->i_part_nb > 1 );
889 BList * VlcWrapper::GetChapters()
893 vlc_mutex_lock( &p_input->stream.stream_lock );
895 BList *list = new BList( p_input->stream.p_selected_area->i_part_nb );
899 for( unsigned int i = 1;
900 i < p_input->stream.p_selected_area->i_part_nb + 1; i++ )
902 message = new BMessage( TOGGLE_CHAPTER );
903 message->AddInt32( "index", i );
904 BString helper( "" );
906 menuItem = new BMenuItem( helper.String(), message );
907 menuItem->SetMarked( p_input->stream.p_selected_area->i_part == i );
908 list->AddItem( menuItem );
911 vlc_mutex_unlock( &p_input->stream.stream_lock );
918 void VlcWrapper::PrevChapter()
921 i_id = p_input->stream.p_selected_area->i_part - 1;
928 void VlcWrapper::NextChapter()
931 i_id = p_input->stream.p_selected_area->i_part + 1;
938 void VlcWrapper::ToggleChapter(int i_chapter)
940 if( p_input != NULL )
942 p_input->stream.p_selected_area->i_part = i_chapter;
943 input_ChangeArea( p_input,
944 p_input->stream.p_selected_area );
946 vlc_mutex_lock( &p_input->stream.stream_lock );
947 vlc_mutex_unlock( &p_input->stream.stream_lock );
951 void VlcWrapper::ChapterInfo( int32 ¤tIndex, int32 &maxIndex )
957 vlc_mutex_lock( &p_input->stream.stream_lock );
959 maxIndex = p_input->stream.p_selected_area->i_part_nb - 1;
961 currentIndex = p_input->stream.p_selected_area->i_part;
965 vlc_mutex_unlock( &p_input->stream.stream_lock );
973 void VlcWrapper::LoadSubFile( const char * psz_file )
975 config_PutPsz( p_intf, "sub-file", strdup( psz_file ) );
978 void VlcWrapper::FilterChange()
983 vout_thread_t * p_vout;
984 vlc_mutex_lock( &p_input->stream.stream_lock );
986 // Warn the vout we are about to change the filter chain
987 p_vout = (vout_thread_t*)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
991 p_vout->b_filter_change = VLC_TRUE;
992 vlc_object_release( p_vout );
995 // restart all video stream
996 for( unsigned int i = 0; i < p_input->stream.i_es_number; i++ )
998 if( ( p_input->stream.pp_es[i]->i_cat == VIDEO_ES ) &&
999 ( p_input->stream.pp_es[i]->p_decoder_fifo != NULL ) )
1001 input_UnselectES( p_input, p_input->stream.pp_es[i] );
1002 input_SelectES( p_input, p_input->stream.pp_es[i] );
1005 vlc_mutex_unlock( &p_input->stream.stream_lock );