EMIPLIB

miptime.h

Go to the documentation of this file.
00001 /*
00002     
00003   This file is a part of EMIPLIB, the EDM Media over IP Library.
00004   
00005   Copyright (C) 2006-2011  Hasselt University - Expertise Centre for
00006                       Digital Media (EDM) (http://www.edm.uhasselt.be)
00007 
00008   This library is free software; you can redistribute it and/or
00009   modify it under the terms of the GNU Lesser General Public
00010   License as published by the Free Software Foundation; either
00011   version 2.1 of the License, or (at your option) any later version.
00012 
00013   This library is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016   Lesser General Public License for more details.
00017 
00018   You should have received a copy of the GNU Lesser General Public
00019   License along with this library; if not, write to the Free Software
00020   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  
00021   USA
00022 
00023 */
00024 
00029 #ifndef MIPTIME_H
00030 
00031 #define MIPTIME_H
00032 
00033 #include "mipconfig.h"
00034 #include "miptypes.h"
00035 #include "mipcompat.h"
00036 #include <stdio.h>
00037 #include <time.h>
00038 #include <string>
00039 
00040 #if defined(WIN32) || defined(_WIN32_WCE)
00041         #include <jrtplib3/rtptimeutilities.h>
00042 #else
00043         #include <sys/time.h>
00044 #endif // Win32
00045 
00049 class EMIPLIB_IMPORTEXPORT MIPTime
00050 {
00051 public:
00053         static MIPTime getCurrentTime();
00054 
00056         static void wait(const MIPTime &delay);
00057         
00059         MIPTime(real_t t = 0.0)                                                         { m_time = t; }
00060 
00062         MIPTime(int64_t seconds, int64_t microSeconds)                                  { m_time = (((real_t)seconds)+(((real_t)microSeconds)/1000000.0)); }
00063 
00065         int64_t getSeconds() const                                                      { return (int64_t)m_time; }
00066 
00068         int64_t getMicroSeconds() const;
00069 
00071         real_t getValue() const                                                         { return m_time; }
00072 
00073         MIPTime &operator-=(const MIPTime &t);
00074         MIPTime &operator+=(const MIPTime &t);
00075         bool operator<(const MIPTime &t) const;
00076         bool operator>(const MIPTime &t) const;
00077         bool operator<=(const MIPTime &t) const;
00078         bool operator>=(const MIPTime &t) const;
00079         std::string getString() const;
00080 private:
00081         real_t m_time;
00082 };
00083 
00084 #if defined(WIN32) || defined(_WIN32_WCE)
00085 inline MIPTime MIPTime::getCurrentTime()
00086 {
00087         // we'll use the RTPTime for this
00088         jrtplib::RTPTime tv = jrtplib::RTPTime::CurrentTime();
00089         return MIPTime((int64_t)tv.GetSeconds(), (int64_t)tv.GetMicroSeconds());
00090 }
00091 #else // unix version
00092 inline MIPTime MIPTime::getCurrentTime()
00093 {
00094         struct timeval tv;
00095         
00096         gettimeofday(&tv, 0);
00097         return MIPTime((int64_t)tv.tv_sec, (int64_t)tv.tv_usec);
00098 }
00099 #endif // Win32
00100 
00101 #if defined(WIN32) || defined(_WIN32_WCE)
00102 inline void MIPTime::wait(const MIPTime &delay)
00103 {
00104         if (delay.getValue() < 0)
00105                 return;
00106         jrtplib::RTPTime::Wait(jrtplib::RTPTime((uint32_t)delay.getSeconds(),(uint32_t)delay.getMicroSeconds()));
00107 }
00108 #else // unix version
00109 inline void MIPTime::wait(const MIPTime &delay)
00110 {
00111         struct timespec req, rem;
00112 
00113         req.tv_sec = (time_t)delay.getSeconds();
00114         req.tv_nsec = ((long)delay.getMicroSeconds())*1000;
00115 
00116         // TODO: apparently this is a function with resolution only up to a few milliseconds
00117         //       got to look at some alternatives
00118         nanosleep(&req, &rem);
00119 }
00120 #endif // Win32
00121 
00122 inline int64_t MIPTime::getMicroSeconds() const
00123 {
00124         real_t t = m_time;
00125         if (t < 0)
00126                 t = -t;
00127         t -= (real_t)((int64_t)t);
00128         t *= 1000000.0;
00129         return (int64_t)t;
00130 }
00131 
00132 inline MIPTime &MIPTime::operator-=(const MIPTime &t)
00133 { 
00134         m_time -= t.m_time;
00135         return *this;
00136 }
00137 
00138 inline MIPTime &MIPTime::operator+=(const MIPTime &t)
00139 { 
00140         m_time += t.m_time;
00141         return *this;
00142 }
00143 
00144 inline bool MIPTime::operator<(const MIPTime &t) const
00145 {
00146         if (m_time < t.m_time)
00147                 return true;
00148         return false;
00149 }
00150 
00151 inline bool MIPTime::operator>(const MIPTime &t) const
00152 {
00153         if (m_time > t.m_time)
00154                 return true;
00155         return false;
00156 }
00157 
00158 inline bool MIPTime::operator<=(const MIPTime &t) const
00159 {
00160         if (m_time <= t.m_time)
00161                 return true;
00162         return false;
00163 }
00164 
00165 inline bool MIPTime::operator>=(const MIPTime &t) const
00166 {
00167         if (m_time >= t.m_time)
00168                 return true;
00169         return false;
00170 }
00171 
00172 inline std::string MIPTime::getString() const
00173 {
00174         char str[256];
00175 
00176         MIP_SNPRINTF(str, 255, "%Ld.%06Ld", (long long)getSeconds(), (long long)getMicroSeconds());
00177         
00178         return std::string(str);
00179 }
00180 
00181 #endif // MIPTIME_H
00182