time.cpp
资源名称:vlc-1.0.5.zip [点击查看]
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:4k
源码类别:
midi
开发平台:
Unix_Linux
- /*****************************************************************************
- * time.cpp
- *****************************************************************************
- * Copyright (C) 2003 the VideoLAN team
- * $Id: c0fe90a1f5095d94aef154841f482b421026e0df $
- *
- * Authors: Cyril Deguet <asmax@via.ecp.fr>
- * Olivier Teulière <ipkiss@via.ecp.fr>
- *
- * 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
- *****************************************************************************/
- #include "time.hpp"
- #include <vlc_input.h>
- void StreamTime::set( float percentage, bool updateVLC )
- {
- VarPercent::set( percentage );
- // Avoid looping forever...
- if( updateVLC && getIntf()->p_sys->p_input )
- {
- vlc_value_t pos;
- pos.f_float = percentage;
- var_Set( getIntf()->p_sys->p_input, "position", pos );
- }
- }
- const string StreamTime::getAsStringPercent() const
- {
- int value = (int)(100. * get());
- // 0 <= value <= 100, so we need 4 chars
- char *str = new char[4];
- snprintf( str, 4, "%d", value );
- string ret = str;
- delete[] str;
- return ret;
- }
- const string StreamTime::getAsStringCurrTime( bool bShortFormat ) const
- {
- if( getIntf()->p_sys->p_input == NULL )
- {
- return "-:--:--";
- }
- vlc_value_t pos; pos.f_float = 0.0;
- var_Get( getIntf()->p_sys->p_input, "position", &pos );
- if( pos.f_float == 0.0 )
- {
- return "-:--:--";
- }
- vlc_value_t time; time.i_time = 0L;
- var_Get( getIntf()->p_sys->p_input, "time", &time );
- return formatTime( time.i_time / 1000000, bShortFormat );
- }
- const string StreamTime::getAsStringTimeLeft( bool bShortFormat ) const
- {
- if( getIntf()->p_sys->p_input == NULL )
- {
- return "-:--:--";
- }
- vlc_value_t pos;
- var_Get( getIntf()->p_sys->p_input, "position", &pos );
- if( pos.f_float == 0.0 )
- {
- return "-:--:--";
- }
- vlc_value_t time, duration;
- var_Get( getIntf()->p_sys->p_input, "time", &time );
- var_Get( getIntf()->p_sys->p_input, "length", &duration );
- return formatTime( (duration.i_time - time.i_time) / 1000000,
- bShortFormat );
- }
- const string StreamTime::getAsStringDuration( bool bShortFormat ) const
- {
- if( getIntf()->p_sys->p_input == NULL )
- {
- return "-:--:--";
- }
- vlc_value_t pos; pos.f_float = 0.0;
- var_Get( getIntf()->p_sys->p_input, "position", &pos );
- if( pos.f_float == 0.0 )
- {
- return "-:--:--";
- }
- vlc_value_t time;
- var_Get( getIntf()->p_sys->p_input, "length", &time );
- return formatTime( time.i_time / 1000000, bShortFormat );
- }
- const string StreamTime::formatTime( int seconds, bool bShortFormat ) const
- {
- char *psz_time = new char[MSTRTIME_MAX_SIZE];
- if( bShortFormat && (seconds < 60 * 60) )
- {
- snprintf( psz_time, MSTRTIME_MAX_SIZE, "%02d:%02d",
- (int) (seconds / 60 % 60),
- (int) (seconds % 60) );
- }
- else
- {
- snprintf( psz_time, MSTRTIME_MAX_SIZE, "%d:%02d:%02d",
- (int) (seconds / (60 * 60)),
- (int) (seconds / 60 % 60),
- (int) (seconds % 60) );
- }
- string ret = psz_time;
- delete[] psz_time;
- return ret;
- }