1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #include <stdio.h> // snprintf
28 #include <vlc/input.h>
31 void StreamTime::set( float percentage, bool updateVLC )
33 VarPercent::set( percentage );
35 // Avoid looping forever...
36 if( updateVLC && getIntf()->p_sys->p_input )
39 pos.f_float = percentage;
41 var_Set( getIntf()->p_sys->p_input, "position", pos );
46 const string StreamTime::getAsStringPercent() const
48 int value = (int)(100. * get());
49 // 0 <= value <= 100, so we need 4 chars
50 char *str = new char[4];
51 snprintf( str, 4, "%d", value );
59 const string StreamTime::getAsStringCurrTime( bool bShortFormat ) const
61 if( getIntf()->p_sys->p_input == NULL )
67 var_Get( getIntf()->p_sys->p_input, "position", &pos );
68 if( pos.f_float == 0.0 )
74 var_Get( getIntf()->p_sys->p_input, "time", &time );
76 return formatTime( time.i_time / 1000000, bShortFormat );
80 const string StreamTime::getAsStringTimeLeft( bool bShortFormat ) const
82 if( getIntf()->p_sys->p_input == NULL )
88 var_Get( getIntf()->p_sys->p_input, "position", &pos );
89 if( pos.f_float == 0.0 )
94 vlc_value_t time, duration;
95 var_Get( getIntf()->p_sys->p_input, "time", &time );
96 var_Get( getIntf()->p_sys->p_input, "length", &duration );
98 return formatTime( (duration.i_time - time.i_time) / 1000000,
103 const string StreamTime::getAsStringDuration( bool bShortFormat ) const
105 if( getIntf()->p_sys->p_input == NULL )
111 var_Get( getIntf()->p_sys->p_input, "position", &pos );
112 if( pos.f_float == 0.0 )
118 var_Get( getIntf()->p_sys->p_input, "length", &time );
120 return formatTime( time.i_time / 1000000, bShortFormat );
124 const string StreamTime::formatTime( int seconds, bool bShortFormat ) const
126 char *psz_time = new char[MSTRTIME_MAX_SIZE];
127 if( bShortFormat && (seconds < 60 * 60) )
129 snprintf( psz_time, MSTRTIME_MAX_SIZE, "%02d:%02d",
130 (int) (seconds / 60 % 60),
131 (int) (seconds % 60) );
135 snprintf( psz_time, MSTRTIME_MAX_SIZE, "%d:%02d:%02d",
136 (int) (seconds / (60 * 60)),
137 (int) (seconds / 60 % 60),
138 (int) (seconds % 60) );
141 string ret = psz_time;