Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

core.c

00001 /*****************************************************************************
00002  * core.c: Core functions : init, playlist, stream management
00003  *****************************************************************************
00004  * Copyright (C) 2005 the VideoLAN team
00005  * $Id: vlc.c 10786 2005-04-23 23:19:17Z zorglub $
00006  *
00007  * Authors: Olivier Aubert <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 #include <vlc/control.h>
00025 
00026 #include <vlc/intf.h>
00027 #include <vlc/vout.h>
00028 #include <vlc/aout.h>
00029 #include <vlc_demux.h>
00030 
00031 #include <vlc_osd.h>
00032 
00033 #define HAS_SNAPSHOT 1
00034 
00035 #ifdef HAS_SNAPSHOT
00036 #include <snapshot.h>
00037 #endif
00038 
00039 #include <stdlib.h>                                      /* malloc(), free() */
00040 #include <string.h>
00041 
00042 #include <errno.h>                                                 /* ENOMEM */
00043 #include <stdio.h>
00044 #include <ctype.h>
00045 
00046 #ifdef HAVE_UNISTD_H
00047 #    include <unistd.h>
00048 #endif
00049 #ifdef HAVE_SYS_TIME_H
00050 #    include <sys/time.h>
00051 #endif
00052 #ifdef HAVE_SYS_TYPES_H
00053 #    include <sys/types.h>
00054 #endif
00055 
00056 #define RAISE( c, m )  exception->code = c; \
00057                        exception->message = strdup(m);
00058 
00059 
00060 mediacontrol_Instance* mediacontrol_new_from_object( vlc_object_t* p_object,
00061                                                      mediacontrol_Exception *exception )
00062 {
00063     mediacontrol_Instance* retval;
00064     vlc_object_t *p_vlc;
00065 
00066     p_vlc = vlc_object_find( p_object, VLC_OBJECT_ROOT, FIND_PARENT );
00067     if( ! p_vlc )
00068     {
00069         RAISE( mediacontrol_InternalException, "Unable to initialize VLC" );
00070         return NULL;
00071     }
00072     retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
00073     retval->p_vlc = p_vlc;
00074     retval->vlc_object_id = p_vlc->i_object_id;
00075 
00076     /* We can keep references on these, which should not change. Is it true ? */
00077     retval->p_playlist = vlc_object_find( p_vlc, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
00078     retval->p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_ANYWHERE );
00079 
00080     if( ! retval->p_playlist || ! retval->p_intf )
00081     {
00082         RAISE( mediacontrol_InternalException, "No available interface" );
00083         return NULL;
00084     }
00085     return retval;
00086 };
00087 
00088 
00089 /**************************************************************************
00090  * Playback management
00091  **************************************************************************/
00092 mediacontrol_Position*
00093 mediacontrol_get_media_position( mediacontrol_Instance *self,
00094                                  const mediacontrol_PositionOrigin an_origin,
00095                                  const mediacontrol_PositionKey a_key,
00096                                  mediacontrol_Exception *exception )
00097 {
00098     mediacontrol_Position* retval;
00099     vlc_value_t val;
00100     input_thread_t * p_input = self->p_playlist->p_input;
00101 
00102     exception = mediacontrol_exception_init( exception );
00103 
00104     retval = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) );
00105     retval->origin = an_origin;
00106     retval->key = a_key;
00107 
00108     if( ! p_input )
00109     {
00110         /*
00111            RAISE( mediacontrol_InternalException, "No input thread." );
00112            return( NULL );
00113         */
00114         retval->value = 0;
00115         return retval;
00116     }
00117 
00118     if(  an_origin == mediacontrol_RelativePosition
00119          || an_origin == mediacontrol_ModuloPosition )
00120     {
00121         /* Relative or ModuloPosition make no sense */
00122         retval->value = 0;
00123         return retval;
00124     }
00125 
00126     /* We are asked for an AbsolutePosition. */
00127     val.i_time = 0;
00128     var_Get( p_input, "time", &val );
00129     /* FIXME: check val.i_time > 0 */
00130 
00131     retval->value = mediacontrol_unit_convert( p_input,
00132                                                mediacontrol_MediaTime,
00133                                                a_key,
00134                                                val.i_time / 1000 );
00135     return retval;
00136 }
00137 
00138 /* Sets the media position */
00139 void
00140 mediacontrol_set_media_position( mediacontrol_Instance *self,
00141                                  const mediacontrol_Position * a_position,
00142                                  mediacontrol_Exception *exception )
00143 {
00144     vlc_value_t val;
00145     input_thread_t * p_input = self->p_playlist->p_input;
00146 
00147     exception=mediacontrol_exception_init( exception );
00148     if( ! p_input )
00149     {
00150         RAISE( mediacontrol_InternalException, "No input thread." );
00151         return;
00152     }
00153 
00154     if( !var_GetBool( p_input, "seekable" ) )
00155     {
00156         RAISE( mediacontrol_InvalidPosition, "Stream not seekable" );
00157         return;
00158     }
00159 
00160     val.i_time = mediacontrol_position2microsecond( p_input, a_position );
00161     var_Set( p_input, "time", val );
00162     return;
00163 }
00164 
00165 /* Starts playing a stream */
00166 void
00167 mediacontrol_start( mediacontrol_Instance *self,
00168                     const mediacontrol_Position * a_position,
00169                     mediacontrol_Exception *exception )
00170 {
00171     playlist_t * p_playlist = self->p_playlist;
00172 
00173     exception = mediacontrol_exception_init( exception );
00174     if( ! p_playlist )
00175     {
00176         RAISE( mediacontrol_PlaylistException, "No available playlist" );
00177         return;
00178     }
00179 
00180     vlc_mutex_lock( &p_playlist->object_lock );
00181     if( p_playlist->i_size )
00182     {
00183         vlc_value_t val;
00184 
00185         vlc_mutex_unlock( &p_playlist->object_lock );
00186 
00187         /* Set start time */
00188         val.i_int = mediacontrol_position2microsecond( p_playlist->p_input, a_position ) / 1000000;
00189         var_Set( p_playlist, "start-time", val );
00190 
00191         playlist_Play( p_playlist );
00192     }
00193     else
00194     {
00195         RAISE( mediacontrol_PlaylistException, "Empty playlist." );
00196         vlc_mutex_unlock( &p_playlist->object_lock );
00197         return;
00198     }
00199 
00200     return;
00201 }
00202 
00203 void
00204 mediacontrol_pause( mediacontrol_Instance *self,
00205                     const mediacontrol_Position * a_position,
00206                     mediacontrol_Exception *exception )
00207 {
00208     input_thread_t *p_input = self->p_playlist->p_input;;
00209 
00210     /* FIXME: use the a_position parameter */
00211     exception=mediacontrol_exception_init( exception );
00212     if( p_input != NULL )
00213     {
00214         var_SetInteger( p_input, "state", PAUSE_S );
00215     }
00216     else
00217     {
00218         RAISE( mediacontrol_InternalException, "No input" );
00219     }
00220 
00221     return;
00222 }
00223 
00224 void
00225 mediacontrol_resume( mediacontrol_Instance *self,
00226                      const mediacontrol_Position * a_position,
00227                      mediacontrol_Exception *exception )
00228 {
00229     input_thread_t *p_input = self->p_playlist->p_input;
00230 
00231     /* FIXME: use the a_position parameter */
00232     exception=mediacontrol_exception_init( exception );
00233     if( p_input != NULL )
00234     {
00235         var_SetInteger( p_input, "state", PAUSE_S );
00236     }
00237     else
00238     {
00239         RAISE( mediacontrol_InternalException, "No input" );
00240     }
00241 }
00242 
00243 void
00244 mediacontrol_stop( mediacontrol_Instance *self,
00245                    const mediacontrol_Position * a_position,
00246                    mediacontrol_Exception *exception )
00247 {
00248     /* FIXME: use the a_position parameter */
00249     exception=mediacontrol_exception_init( exception );
00250     if( !self->p_playlist )
00251     {
00252         RAISE( mediacontrol_PlaylistException, "No playlist" );
00253         return;
00254     }
00255 
00256     playlist_Stop( self->p_playlist );
00257 }
00258 
00259 /**************************************************************************
00260  * Playlist management
00261  **************************************************************************/
00262 
00263 void
00264 mediacontrol_playlist_add_item( mediacontrol_Instance *self,
00265                                 const char * psz_file,
00266                                 mediacontrol_Exception *exception )
00267 {
00268     exception=mediacontrol_exception_init( exception );
00269     if( !self->p_playlist )
00270     {
00271         RAISE( mediacontrol_InternalException, "No playlist" );
00272         return;
00273     }
00274 
00275     playlist_Add( self->p_playlist, psz_file, psz_file , PLAYLIST_REPLACE, 0 );
00276 }
00277 
00278 void
00279 mediacontrol_playlist_clear( mediacontrol_Instance *self,
00280                              mediacontrol_Exception *exception )
00281 {
00282     exception=mediacontrol_exception_init( exception );
00283     if( !self->p_playlist )
00284     {
00285         RAISE( mediacontrol_PlaylistException, "No playlist" );
00286         return;
00287     }
00288 
00289     playlist_Clear( self->p_playlist );
00290 
00291     return;
00292 }
00293 
00294 mediacontrol_PlaylistSeq *
00295 mediacontrol_playlist_get_list( mediacontrol_Instance *self,
00296                                 mediacontrol_Exception *exception )
00297 {
00298     mediacontrol_PlaylistSeq *retval;
00299     int i_index;
00300     playlist_t * p_playlist = self->p_playlist;
00301     int i_playlist_size;
00302 
00303     exception=mediacontrol_exception_init( exception );
00304     if( !p_playlist )
00305     {
00306         RAISE( mediacontrol_PlaylistException, "No playlist" );
00307         return NULL;
00308     }
00309 
00310     vlc_mutex_lock( &p_playlist->object_lock );
00311     i_playlist_size = p_playlist->i_size;
00312 
00313     retval = mediacontrol_PlaylistSeq__alloc( i_playlist_size );
00314 
00315     for( i_index = 0 ; i_index < i_playlist_size ; i_index++ )
00316     {
00317         retval->data[i_index] = strdup( p_playlist->pp_items[i_index]->input.psz_uri );
00318     }
00319     vlc_mutex_unlock( &p_playlist->object_lock );
00320 
00321     return retval;
00322 }
00323 
00324 /***************************************************************************
00325  * Status feedback
00326  ***************************************************************************/
00327 
00328 mediacontrol_StreamInformation *
00329 mediacontrol_get_stream_information( mediacontrol_Instance *self,
00330                                      mediacontrol_PositionKey a_key,
00331                                      mediacontrol_Exception *exception )
00332 {
00333     mediacontrol_StreamInformation *retval;
00334     input_thread_t *p_input = self->p_playlist->p_input;
00335     vlc_value_t val;
00336 
00337     retval = ( mediacontrol_StreamInformation* )malloc( sizeof( mediacontrol_StreamInformation ) );
00338     if( ! retval )
00339     {
00340         RAISE( mediacontrol_InternalException, "Out of memory" );
00341         return NULL;
00342     }
00343 
00344     if( ! p_input )
00345     {
00346         /* No p_input defined */
00347         retval->streamstatus = mediacontrol_UndefinedStatus;
00348         retval->url          = strdup( "None" );
00349         retval->position     = 0;
00350         retval->length       = 0;
00351     }
00352     else
00353     {
00354         switch( var_GetInteger( p_input, "state" ) )
00355         {
00356         case PLAYING_S     :
00357             retval->streamstatus = mediacontrol_PlayingStatus;
00358             break;
00359         case PAUSE_S       :
00360             retval->streamstatus = mediacontrol_PauseStatus;
00361             break;
00362         case INIT_S        :
00363             retval->streamstatus = mediacontrol_InitStatus;
00364             break;
00365         case END_S         :
00366             retval->streamstatus = mediacontrol_EndStatus;
00367             break;
00368         default :
00369             retval->streamstatus = mediacontrol_UndefinedStatus;
00370             break;
00371         }
00372 
00373         retval->url = strdup( p_input->input.p_item->psz_uri );
00374 
00375         /* TIME and LENGTH are in microseconds. We want them in ms */
00376         var_Get( p_input, "time", &val);
00377         retval->position = val.i_time / 1000;
00378 
00379         var_Get( p_input, "length", &val);
00380         retval->length = val.i_time / 1000;
00381 
00382         retval->position = mediacontrol_unit_convert( p_input,
00383                                                       mediacontrol_MediaTime, a_key,
00384                                                       retval->position );
00385         retval->length   = mediacontrol_unit_convert( p_input,
00386                                                       mediacontrol_MediaTime, a_key,
00387                                                       retval->length );
00388     }
00389     return retval;
00390 }

Generated on Tue Dec 20 10:14:59 2005 for vlc-0.8.4a by  doxygen 1.4.2