📄 vlm.c
字号:
/***************************************************************************** * vlm.c: VLM interface plugin ***************************************************************************** * Copyright (C) 2000, 2001 VideoLAN * $Id: vlm.c 11497 2005-06-22 16:43:05Z dionoea $ * * Authors: Simon Latapie <garf@videolan.org> * Laurent Aimar <fenrir@videolan.org> * Gildas Bazin <gbazin@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 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. *****************************************************************************//***************************************************************************** * Preamble *****************************************************************************/#include <stdlib.h> /* malloc(), free() */#include <ctype.h> /* tolower() */#include <vlc/vlc.h>#ifdef ENABLE_VLM#include <vlc/intf.h>#include <vlc/input.h>#ifdef HAVE_TIME_H# include <time.h> /* ctime() */# include <sys/timeb.h> /* ftime() */#endif#include "vlc_vlm.h"#include "vlc_vod.h"#define FREE( p ) \ if( p ) { free( p ); (p) = NULL; }/***************************************************************************** * Local prototypes. *****************************************************************************/static vlm_message_t *vlm_Show( vlm_t *, vlm_media_t *, vlm_schedule_t *, char * );static vlm_message_t *vlm_Help( vlm_t *, char * );static vlm_media_t *vlm_MediaSearch ( vlm_t *, char * );static vlm_media_instance_t *vlm_MediaInstanceSearch( vlm_t *, vlm_media_t *, char * );static vlm_message_t *vlm_MessageNew( char *, const char *, ... );static vlm_message_t *vlm_MessageAdd( vlm_message_t *, vlm_message_t * );static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *, char * );static char *Save( vlm_t * );static int Load( vlm_t *, char * );static int ExecuteCommand( vlm_t *, char *, vlm_message_t ** );static int Manage( vlc_object_t * );/***************************************************************************** * vlm_New: *****************************************************************************/vlm_t *__vlm_New ( vlc_object_t *p_this ){ vlc_value_t lockval; vlm_t *p_vlm = NULL; char *psz_vlmconf; /* to be sure to avoid multiple creation */ var_Create( p_this->p_libvlc, "vlm_mutex", VLC_VAR_MUTEX ); var_Get( p_this->p_libvlc, "vlm_mutex", &lockval ); vlc_mutex_lock( lockval.p_address ); if( !(p_vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE )) ) { msg_Info( p_this, "creating vlm" ); if( ( p_vlm = vlc_object_create( p_this, VLC_OBJECT_VLM ) ) == NULL ) { vlc_mutex_unlock( lockval.p_address ); return NULL; } vlc_mutex_init( p_this->p_vlc, &p_vlm->lock ); p_vlm->i_media = 0; p_vlm->media = NULL; p_vlm->i_vod = 0; p_vlm->i_schedule = 0; p_vlm->schedule = NULL; vlc_object_yield( p_vlm ); vlc_object_attach( p_vlm, p_this->p_vlc ); } vlc_mutex_unlock( lockval.p_address ); if( vlc_thread_create( p_vlm, "vlm thread", Manage, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) ) { vlc_mutex_destroy( &p_vlm->lock ); vlc_object_destroy( p_vlm ); return NULL; } /* Try loading the vlm conf file given by --vlm-conf */ psz_vlmconf = config_GetPsz( p_vlm, "vlm-conf" ); if( psz_vlmconf && *psz_vlmconf ) { vlm_message_t *p_message = NULL; char *psz_buffer = NULL; msg_Dbg( p_this, "loading vlm conf ..." ); asprintf(&psz_buffer, "load %s", psz_vlmconf ); if( psz_buffer ) { msg_Dbg( p_this, psz_buffer); if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) ){ msg_Warn( p_this, "error while loading the vlm conf file" ); } free(p_message); free(psz_buffer); } } free(psz_vlmconf); return p_vlm;}/***************************************************************************** * vlm_Delete: *****************************************************************************/void vlm_Delete( vlm_t *p_vlm ){ vlc_value_t lockval; var_Get( p_vlm->p_libvlc, "vlm_mutex", &lockval ); vlc_mutex_lock( lockval.p_address ); vlc_object_release( p_vlm ); if( p_vlm->i_refcount > 0 ) { vlc_mutex_unlock( lockval.p_address ); return; } p_vlm->b_die = VLC_TRUE; vlc_thread_join( p_vlm ); vlc_mutex_destroy( &p_vlm->lock ); while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0], NULL ); FREE( p_vlm->media ); while( p_vlm->i_schedule ) vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL ); FREE( p_vlm->schedule ); vlc_object_detach( p_vlm ); vlc_object_destroy( p_vlm ); vlc_mutex_unlock( lockval.p_address );}/***************************************************************************** * vlm_ExecuteCommand: *****************************************************************************/int vlm_ExecuteCommand( vlm_t *p_vlm, char *psz_command, vlm_message_t **pp_message){ int i_result; vlc_mutex_lock( &p_vlm->lock ); i_result = ExecuteCommand( p_vlm, psz_command, pp_message ); vlc_mutex_unlock( &p_vlm->lock ); return i_result;}/***************************************************************************** * vlm_Save: *****************************************************************************/int vlm_Save( vlm_t *p_vlm, char *psz_file ){ FILE *file; char *psz_save; if( !p_vlm || !psz_file ) return 1; file = fopen( psz_file, "wt" ); if( file == NULL ) return 1; psz_save = Save( p_vlm ); if( psz_save == NULL ) { fclose( file ); return 1; } fwrite( psz_save, strlen( psz_save ), 1, file ); fclose( file ); free( psz_save ); return 0;}/***************************************************************************** * vlm_Load: *****************************************************************************/int vlm_Load( vlm_t *p_vlm, char *psz_file ){ FILE *file; int64_t i_size; char *psz_buffer; if( !p_vlm || !psz_file ) return 1; file = fopen( psz_file, "r" ); if( file == NULL ) return 1; if( fseek( file, 0, SEEK_END) != 0 ) { fclose( file ); return 2; } i_size = ftell( file ); fseek( file, 0, SEEK_SET ); psz_buffer = malloc( i_size + 1 ); if( !psz_buffer ) { fclose( file ); return 2; } fread( psz_buffer, 1, i_size, file ); psz_buffer[ i_size ] = '\0'; if( Load( p_vlm, psz_buffer ) ) { fclose( file ); free( psz_buffer ); return 3; } free( psz_buffer ); fclose( file ); return 0;}/***************************************************************************** * FindEndCommand *****************************************************************************/static char *FindEndCommand( char *psz ){ char *psz_sent = psz; switch( *psz_sent ) { case '\"': psz_sent++; while( ( *psz_sent != '\"' ) && ( *psz_sent != '\0' ) ) { if( *psz_sent == '\'' ) { psz_sent = FindEndCommand( psz_sent ); if( psz_sent == NULL ) return NULL; } else psz_sent++; } if( *psz_sent == '\"' ) { psz_sent++; return psz_sent; } /* *psz_sent == '\0' -> number of " is incorrect */ else return NULL; break; case '\'': psz_sent++; while( ( *psz_sent != '\'' ) && ( *psz_sent != '\0' ) ) { if( *psz_sent == '\"' ) { psz_sent = FindEndCommand( psz_sent ); if( psz_sent == NULL ) return NULL; } else psz_sent++; } if( *psz_sent == '\'' ) { psz_sent++; return psz_sent; } /* *psz_sent == '\0' -> number of " is incorrect */ else return NULL; break; default: /* now we can look for spaces */ while( ( *psz_sent != ' ' ) && ( *psz_sent != '\0' ) ) { if( ( *psz_sent == '\'' ) || ( *psz_sent == '\"' ) ) { psz_sent = FindEndCommand( psz_sent ); } else psz_sent++; } return psz_sent; }}/***************************************************************************** * ExecuteCommand: The main state machine ***************************************************************************** * Execute a command which ends with '\0' (string) *****************************************************************************/static int ExecuteCommand( vlm_t *p_vlm, char *psz_command, vlm_message_t **pp_message ){ int i_command = 0; char **ppsz_command = NULL; char *psz_cmd = psz_command; vlm_message_t *p_message = NULL; int i, j; /* First, parse the line and cut it */ while( *psz_cmd != '\0' ) { if( *psz_cmd == ' ' || *psz_cmd == '\t' ) { psz_cmd++; } else { char *psz_temp; int i_temp; /* support for comments */ if( i_command == 0 && *psz_cmd == '#') { p_message = vlm_MessageNew( "", NULL ); goto success; } psz_temp = FindEndCommand( psz_cmd ); if( psz_temp == NULL ) goto error; i_temp = psz_temp - psz_cmd; ppsz_command = realloc( ppsz_command, (i_command + 1) * sizeof(char*) ); ppsz_command[ i_command ] = malloc( (i_temp + 1) * sizeof(char) ); strncpy( ppsz_command[ i_command ], psz_cmd, i_temp ); ppsz_command[ i_command ][ i_temp ] = '\0'; i_command++; psz_cmd = psz_temp; } } /* * And then Interpret it */ if( i_command == 0 ) { p_message = vlm_MessageNew( "", NULL ); goto success; } if( !strcmp(ppsz_command[0], "new") ) { int i_type; /* Check the number of arguments */ if( i_command < 3 ) goto syntax_error; /* Get type */ if( !strcmp(ppsz_command[2], "vod") ) { i_type = VOD_TYPE; } else if( !strcmp(ppsz_command[2], "broadcast") ) { i_type = BROADCAST_TYPE; } else if( !strcmp(ppsz_command[2], "schedule") ) { i_type = SCHEDULE_TYPE; } else { p_message = vlm_MessageNew( "new", "%s: Choose between vod, " "broadcast or schedule", ppsz_command[1] ); goto error; } /* Check for forbidden media names */ if( !strcmp(ppsz_command[1], "all") || !strcmp(ppsz_command[1], "media") || !strcmp(ppsz_command[1], "schedule") ) { p_message = vlm_MessageNew( "new", "\"all\", \"media\" and " "\"schedule\" are reserved names" ); goto error; } /* Check the name is not already in use */ if( vlm_ScheduleSearch( p_vlm, ppsz_command[1] ) || vlm_MediaSearch( p_vlm, ppsz_command[1] ) ) { p_message = vlm_MessageNew( "new", "%s: Name already in use", ppsz_command[1] ); goto error; } /* Schedule */ if( i_type == SCHEDULE_TYPE )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -