vlc/src/libvlc.c

673 lines
18 KiB
C
Raw Normal View History

/*****************************************************************************
* libvlc.c: Implementation of the old libvlc API
*****************************************************************************
2007-04-05 18:42:16 +02:00
* Copyright (C) 1998-2007 the VideoLAN team
* $Id$
*
* Authors: Vincent Seguin <seguin@via.ecp.fr>
* Samuel Hocevar <sam@zoy.org>
* Gildas Bazin <gbazin@videolan.org>
* Derk-Jan Hartman <hartman at videolan dot org>
2006-01-30 23:02:47 +01:00
* Rémi Denis-Courmont <rem # videolan : org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
2006-01-13 00:10:04 +01:00
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
* Pretend we are a builtin module
*****************************************************************************/
#define MODULE_NAME main
#define MODULE_PATH main
#define __BUILTIN__
/*****************************************************************************
* Preamble
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <vlc_common.h>
#include "control/libvlc_internal.h"
2007-08-19 16:54:33 +02:00
#include "libvlc.h"
#include <vlc_playlist.h>
#include <vlc_aout.h>
#include <vlc_vout.h>
#define LIBVLC_FUNC \
libvlc_int_t * p_libvlc = vlc_current_object( i_object ); \
if( !p_libvlc ) return VLC_ENOOBJ;
#define LIBVLC_FUNC_END \
if( i_object ) vlc_object_release( p_libvlc );
/*****************************************************************************
* VLC_VariableSet: set a "safe" vlc variable
*****************************************************************************/
int VLC_VariableSet( int i_object, char const *psz_var, vlc_value_t value )
{
int i_ret;
LIBVLC_FUNC;
/* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
* we handle it as a configuration variable. Don't tell Gildas :) -- sam */
if( !strncmp( psz_var, "conf::", 6 ) )
{
module_config_t *p_item;
char const *psz_newvar = psz_var + 6;
p_item = config_FindConfig( VLC_OBJECT(p_libvlc), psz_newvar );
if( p_item )
{
/* VLC_VariableSet is only used from the browser plugins, so we
* can pretty much assume that the input is _not_ trusted. */
if( !p_item->b_safe )
return VLC_EGENERIC;
switch( p_item->i_type )
{
case CONFIG_ITEM_BOOL:
config_PutInt( p_libvlc, psz_newvar, value.b_bool );
break;
case CONFIG_ITEM_INTEGER:
config_PutInt( p_libvlc, psz_newvar, value.i_int );
break;
case CONFIG_ITEM_FLOAT:
config_PutFloat( p_libvlc, psz_newvar, value.f_float );
break;
default:
config_PutPsz( p_libvlc, psz_newvar, value.psz_string );
break;
}
if( i_object ) vlc_object_release( p_libvlc );
return VLC_SUCCESS;
}
}
/* EXPLICIT HACK (this is the legacy API anyway):
* VLC_VariableSet is only used from the browser plugins, so we
* can pretty much assume that the input is _not_ trusted. */
module_config_t *p_item;
p_item = config_FindConfig( VLC_OBJECT(p_libvlc), psz_var );
if( !p_item )
return VLC_ENOVAR;
if( !p_item->b_safe )
return VLC_EGENERIC;
i_ret = var_Set( p_libvlc, psz_var, value );
LIBVLC_FUNC_END;
return i_ret;
}
/*****************************************************************************
* VLC_VariableGet: get a vlc variable
*****************************************************************************/
int VLC_VariableGet( int i_object, char const *psz_var, vlc_value_t *p_value )
{
int i_ret;
LIBVLC_FUNC;
i_ret = var_Get( p_libvlc , psz_var, p_value );
LIBVLC_FUNC_END;
return i_ret;
}
/*****************************************************************************
* VLC_VariableType: get a vlc variable type
*****************************************************************************/
int VLC_VariableType( int i_object, char const *psz_var, int *pi_type )
{
int i_type;
LIBVLC_FUNC;
/* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
* we handle it as a configuration variable. Don't tell Gildas :) -- sam */
if( !strncmp( psz_var, "conf::", 6 ) )
{
module_config_t *p_item;
char const *psz_newvar = psz_var + 6;
p_item = config_FindConfig( VLC_OBJECT(p_libvlc), psz_newvar );
if( p_item )
{
switch( p_item->i_type )
{
case CONFIG_ITEM_BOOL:
i_type = VLC_VAR_BOOL;
break;
case CONFIG_ITEM_INTEGER:
i_type = VLC_VAR_INTEGER;
break;
case CONFIG_ITEM_FLOAT:
i_type = VLC_VAR_FLOAT;
break;
default:
i_type = VLC_VAR_STRING;
break;
}
}
else
i_type = 0;
}
else
i_type = VLC_VAR_TYPE & var_Type( p_libvlc , psz_var );
LIBVLC_FUNC_END;
if( i_type > 0 )
{
*pi_type = i_type;
return VLC_SUCCESS;
}
return VLC_ENOVAR;
}
#define LIBVLC_PLAYLIST_FUNC \
libvlc_int_t *p_libvlc = vlc_current_object( i_object );\
if( !p_libvlc ) return VLC_ENOOBJ; \
playlist_t *p_playlist = pl_Yield( p_libvlc ); \
if( !p_playlist ) return VLC_ENOOBJ
#define LIBVLC_PLAYLIST_FUNC_END \
pl_Release( p_libvlc ); \
if( i_object ) vlc_object_release( p_libvlc );
/*****************************************************************************
* VLC_AddTarget: adds a target for playing.
*****************************************************************************
* This function adds psz_target to the playlist
*****************************************************************************/
int VLC_AddTarget( int i_object, char const *psz_target,
char const **ppsz_options, int i_options,
int i_mode, int i_pos )
{
int i_err;
LIBVLC_PLAYLIST_FUNC;
i_err = playlist_AddExt( p_playlist, psz_target,
NULL, i_mode, i_pos, -1,
ppsz_options, i_options, true, false );
LIBVLC_PLAYLIST_FUNC_END;
return i_err;
}
/*****************************************************************************
* VLC_Play: play the playlist
*****************************************************************************/
int VLC_Play( int i_object )
{
LIBVLC_PLAYLIST_FUNC;
playlist_Play( p_playlist );
LIBVLC_PLAYLIST_FUNC_END;
2004-06-05 18:58:57 +02:00
return VLC_SUCCESS;
}
/*****************************************************************************
* VLC_Pause: toggle pause
*****************************************************************************/
int VLC_Pause( int i_object )
{
LIBVLC_PLAYLIST_FUNC;
playlist_Pause( p_playlist );
LIBVLC_PLAYLIST_FUNC_END;
2004-06-05 18:58:57 +02:00
return VLC_SUCCESS;
}
/*****************************************************************************
2006-07-16 18:06:29 +02:00
* VLC_Stop: stop playback
*****************************************************************************/
int VLC_Stop( int i_object )
{
LIBVLC_PLAYLIST_FUNC;
playlist_Stop( p_playlist );
LIBVLC_PLAYLIST_FUNC_END;
2004-06-05 18:58:57 +02:00
return VLC_SUCCESS;
}
/*****************************************************************************
* VLC_IsPlaying: Query for Playlist Status
*****************************************************************************/
bool VLC_IsPlaying( int i_object )
{
bool b_playing;
LIBVLC_PLAYLIST_FUNC;
if( p_playlist->p_input )
{
vlc_value_t val;
var_Get( p_playlist->p_input, "state", &val );
b_playing = ( val.i_int == PLAYING_S );
}
else
{
b_playing = playlist_IsPlaying( p_playlist );
}
LIBVLC_PLAYLIST_FUNC_END;
return b_playing;
}
/**
* Get the current position in a input
*
* Return the current position as a float
* \note For some inputs, this will be unknown.
*
* \param i_object a vlc object id
* \return a float in the range of 0.0 - 1.0
*/
float VLC_PositionGet( int i_object )
{
input_thread_t *p_input;
vlc_value_t val;
LIBVLC_FUNC;
p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
if( !p_input )
{
if( i_object ) vlc_object_release( p_libvlc );
return VLC_ENOOBJ;
}
var_Get( p_input, "position", &val );
vlc_object_release( p_input );
LIBVLC_FUNC_END;
return val.f_float;
}
/**
* Set the current position in a input
*
* Set the current position in a input and then return
* the current position as a float.
* \note For some inputs, this will be unknown.
*
* \param i_object a vlc object id
* \param i_position a float in the range of 0.0 - 1.0
* \return a float in the range of 0.0 - 1.0
*/
float VLC_PositionSet( int i_object, float i_position )
{
input_thread_t *p_input;
vlc_value_t val;
libvlc_int_t *p_libvlc = vlc_current_object( i_object );
/* Check that the handle is valid */
if( !p_libvlc )
{
return VLC_ENOOBJ;
}
p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
if( !p_input )
{
if( i_object ) vlc_object_release( p_libvlc );
return VLC_ENOOBJ;
}
val.f_float = i_position;
var_Set( p_input, "position", val );
var_Get( p_input, "position", &val );
vlc_object_release( p_input );
if( i_object ) vlc_object_release( p_libvlc );
return val.f_float;
}
/**
* Get the current position in a input
*
* Return the current position in seconds from the start.
* \note For some inputs, this will be unknown.
*
* \param i_object a vlc object id
* \return the offset from 0:00 in seconds
*/
int VLC_TimeGet( int i_object )
{
input_thread_t *p_input;
vlc_value_t val;
libvlc_int_t *p_libvlc = vlc_current_object( i_object );
/* Check that the handle is valid */
if( !p_libvlc )
{
return VLC_ENOOBJ;
}
p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
if( !p_input )
{
if( i_object ) vlc_object_release( p_libvlc );
return VLC_ENOOBJ;
}
var_Get( p_input, "time", &val );
vlc_object_release( p_input );
if( i_object ) vlc_object_release( p_libvlc );
return val.i_time / 1000000;
}
/**
* Seek to a position in the current input
*
* Seek i_seconds in the current input. If b_relative is set,
* then the seek will be relative to the current position, otherwise
* it will seek to i_seconds from the beginning of the input.
* \note For some inputs, this will be unknown.
*
* \param i_object a vlc object id
* \param i_seconds seconds from current position or from beginning of input
* \param b_relative seek relative from current position
* \return VLC_SUCCESS on success
*/
int VLC_TimeSet( int i_object, int i_seconds, bool b_relative )
{
input_thread_t *p_input;
vlc_value_t val;
libvlc_int_t *p_libvlc = vlc_current_object( i_object );
/* Check that the handle is valid */
if( !p_libvlc )
{
return VLC_ENOOBJ;
}
p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
if( !p_input )
{
if( i_object ) vlc_object_release( p_libvlc );
return VLC_ENOOBJ;
}
if( b_relative )
{
val.i_time = i_seconds;
val.i_time = val.i_time * 1000000L;
var_Set( p_input, "time-offset", val );
}
else
{
val.i_time = i_seconds;
val.i_time = val.i_time * 1000000L;
var_Set( p_input, "time", val );
}
vlc_object_release( p_input );
if( i_object ) vlc_object_release( p_libvlc );
return VLC_SUCCESS;
}
/**
* Get the total length of a input
*
* Return the total length in seconds from the current input.
* \note For some inputs, this will be unknown.
*
* \param i_object a vlc object id
* \return the length in seconds
*/
int VLC_LengthGet( int i_object )
{
input_thread_t *p_input;
vlc_value_t val;
libvlc_int_t *p_libvlc = vlc_current_object( i_object );
/* Check that the handle is valid */
if( !p_libvlc )
{
return VLC_ENOOBJ;
}
p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
if( !p_input )
{
if( i_object ) vlc_object_release( p_libvlc );
return VLC_ENOOBJ;
}
var_Get( p_input, "length", &val );
vlc_object_release( p_input );
if( i_object ) vlc_object_release( p_libvlc );
return val.i_time / 1000000L;
}
/**
* Play the input faster than realtime
*
* 2x, 4x, 8x faster than realtime
* \note For some inputs, this will be impossible.
*
* \param i_object a vlc object id
* \return the current speedrate
*/
float VLC_SpeedFaster( int i_object )
{
input_thread_t *p_input;
vlc_value_t val;
libvlc_int_t *p_libvlc = vlc_current_object( i_object );
/* Check that the handle is valid */
if( !p_libvlc )
{
return VLC_ENOOBJ;
}
p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
if( !p_input )
{
if( i_object ) vlc_object_release( p_libvlc );
return VLC_ENOOBJ;
}
val.b_bool = true;
var_Set( p_input, "rate-faster", val );
var_Get( p_input, "rate", &val );
vlc_object_release( p_input );
if( i_object ) vlc_object_release( p_libvlc );
2004-06-22 21:31:54 +02:00
return val.f_float / INPUT_RATE_DEFAULT;
}
/**
* Play the input slower than realtime
*
* 1/2x, 1/4x, 1/8x slower than realtime
* \note For some inputs, this will be impossible.
*
* \param i_object a vlc object id
* \return the current speedrate
*/
float VLC_SpeedSlower( int i_object )
{
input_thread_t *p_input;
vlc_value_t val;
libvlc_int_t *p_libvlc = vlc_current_object( i_object );
/* Check that the handle is valid */
if( !p_libvlc )
{
return VLC_ENOOBJ;
}
p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
if( !p_input )
{
if( i_object ) vlc_object_release( p_libvlc );
return VLC_ENOOBJ;
}
val.b_bool = true;
var_Set( p_input, "rate-slower", val );
var_Get( p_input, "rate", &val );
vlc_object_release( p_input );
if( i_object ) vlc_object_release( p_libvlc );
2004-06-22 21:31:54 +02:00
return val.f_float / INPUT_RATE_DEFAULT;
}
/**
* Return the current playlist item
*
* Returns the index of the playlistitem that is currently selected for play.
* This is valid even if nothing is currently playing.
*
* \param i_object a vlc object id
* \return the current index
*/
int VLC_PlaylistIndex( int i_object )
{
2007-04-05 18:42:16 +02:00
(void)i_object;
printf( "This function is deprecated and should not be used anymore" );
return -1;
}
/**
* Total number of items in the playlist
*
* \param i_object a vlc object id
* \return amount of playlist items
*/
int VLC_PlaylistNumberOfItems( int i_object )
{
int i_size;
LIBVLC_PLAYLIST_FUNC;
i_size = p_playlist->items.i_size;
LIBVLC_PLAYLIST_FUNC_END;
return i_size;
}
/**
* Go to next playlist item
* \param i_object a vlc object id
* \return VLC_SUCCESS on success
*/
int VLC_PlaylistNext( int i_object )
{
LIBVLC_PLAYLIST_FUNC;
playlist_Next( p_playlist );
LIBVLC_PLAYLIST_FUNC_END;
return VLC_SUCCESS;
}
/**
* Go to previous playlist item
* \param i_object a vlc object id
* \return VLC_SUCCESS on success
*/
int VLC_PlaylistPrev( int i_object )
{
LIBVLC_PLAYLIST_FUNC;
playlist_Prev( p_playlist );
LIBVLC_PLAYLIST_FUNC_END;
return VLC_SUCCESS;
}
/**
* Empty the playlist
*/
int VLC_PlaylistClear( int i_object )
{
LIBVLC_PLAYLIST_FUNC;
playlist_Clear( p_playlist, true );
LIBVLC_PLAYLIST_FUNC_END;
return VLC_SUCCESS;
}
/**
* Change the volume
*
* \param i_object a vlc object id
* \param i_volume something in a range from 0-200
* \return the new volume (range 0-200 %)
*/
int VLC_VolumeSet( int i_object, int i_volume )
{
audio_volume_t i_vol = 0;
LIBVLC_FUNC;
if( i_volume >= 0 && i_volume <= 200 )
{
i_vol = i_volume * AOUT_VOLUME_MAX / 200;
aout_VolumeSet( p_libvlc, i_vol );
}
LIBVLC_FUNC_END;
return i_vol * 200 / AOUT_VOLUME_MAX;
}
/**
* Get the current volume
*
* Retrieve the current volume.
*
* \param i_object a vlc object id
* \return the current volume (range 0-200 %)
*/
int VLC_VolumeGet( int i_object )
{
audio_volume_t i_volume;
LIBVLC_FUNC;
aout_VolumeGet( p_libvlc, &i_volume );
LIBVLC_FUNC_END;
return i_volume*200/AOUT_VOLUME_MAX;
}
/**
* Mute/Unmute the volume
*
* \param i_object a vlc object id
* \return VLC_SUCCESS on success
*/
int VLC_VolumeMute( int i_object )
{
LIBVLC_FUNC;
aout_VolumeMute( p_libvlc, NULL );
LIBVLC_FUNC_END;
return VLC_SUCCESS;
}
/*****************************************************************************
* VLC_FullScreen: toggle fullscreen mode
*****************************************************************************/
int VLC_FullScreen( int i_object )
{
vout_thread_t *p_vout;
LIBVLC_FUNC;
p_vout = vlc_object_find( p_libvlc, VLC_OBJECT_VOUT, FIND_CHILD );
if( !p_vout )
{
if( i_object ) vlc_object_release( p_libvlc );
return VLC_ENOOBJ;
}
p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
vlc_object_release( p_vout );
LIBVLC_FUNC_END;
return VLC_SUCCESS;
}