1 /*****************************************************************************
2  * time.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id: 0089a6e036e5e88422b1561e8150d98d95069285 $
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
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.
14  *
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.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24 
25 #include "time.hpp"
26 #include <vlc_input.h>
27 
28 
havePosition() const29 inline bool StreamTime::havePosition() const {
30     input_thread_t *p_input = getIntf()->p_sys->p_input;
31     return p_input && ( var_GetFloat( p_input, "position" ) != 0.0 );
32 }
33 
34 
set(float percentage,bool updateVLC)35 void StreamTime::set( float percentage, bool updateVLC )
36 {
37     VarPercent::set( percentage );
38 
39     // Avoid looping forever...
40     if( updateVLC && getIntf()->p_sys->p_input )
41         var_SetFloat( getIntf()->p_sys->p_input, "position", percentage );
42 }
43 
44 
getAsStringPercent() const45 std::string StreamTime::getAsStringPercent() const
46 {
47     int value = (int)(100. * get());
48     // 0 <= value <= 100, so we need 4 chars
49     char str[4];
50     snprintf( str, 4, "%d", value );
51     return std::string(str);
52 }
53 
54 
formatTime(int seconds,bool bShortFormat) const55 std::string StreamTime::formatTime( int seconds, bool bShortFormat ) const
56 {
57     char psz_time[MSTRTIME_MAX_SIZE];
58     if( bShortFormat && (seconds < 60 * 60) )
59     {
60         snprintf( psz_time, MSTRTIME_MAX_SIZE, "%02d:%02d",
61                   (int) (seconds / 60 % 60),
62                   (int) (seconds % 60) );
63     }
64     else
65     {
66         snprintf( psz_time, MSTRTIME_MAX_SIZE, "%d:%02d:%02d",
67                   (int) (seconds / (60 * 60)),
68                   (int) (seconds / 60 % 60),
69                   (int) (seconds % 60) );
70     }
71     return std::string(psz_time);
72 }
73 
74 
getAsStringCurrTime(bool bShortFormat) const75 std::string StreamTime::getAsStringCurrTime( bool bShortFormat ) const
76 {
77     if( !havePosition() )
78         return "-:--:--";
79 
80     mtime_t time = var_GetInteger( getIntf()->p_sys->p_input, "time" );
81     return formatTime( time / CLOCK_FREQ, bShortFormat );
82 }
83 
84 
getAsStringTimeLeft(bool bShortFormat) const85 std::string StreamTime::getAsStringTimeLeft( bool bShortFormat ) const
86 {
87     if( !havePosition() )
88         return "-:--:--";
89 
90     mtime_t time = var_GetInteger( getIntf()->p_sys->p_input, "time" ),
91         duration = var_GetInteger( getIntf()->p_sys->p_input, "length" );
92 
93     return formatTime( (duration - time) / CLOCK_FREQ, bShortFormat );
94 }
95 
96 
getAsStringDuration(bool bShortFormat) const97 std::string StreamTime::getAsStringDuration( bool bShortFormat ) const
98 {
99     if( !havePosition() )
100         return "-:--:--";
101 
102     mtime_t time = var_GetInteger( getIntf()->p_sys->p_input, "length" );
103     return formatTime( time / CLOCK_FREQ, bShortFormat );
104 }
105