posixtimer.c 4.17 KB
Newer Older
aymeric's avatar
aymeric committed
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * The oRTP library is an RTP (Realtime Transport Protocol - rfc3550) implementation with additional features.
 * Copyright (C) 2017 Belledonne Communications SARL
 *
 *  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-1307, USA.
 */
aymeric's avatar
aymeric committed
19

20
#ifdef HAVE_CONFIG_H
aymeric's avatar
aymeric committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include "ortp-config.h"
#endif

#include "ortp/ortp.h"
#include "rtptimer.h"

#if	!defined(_WIN32) && !defined(_WIN32_WCE)

#ifdef __linux__
#include <sys/select.h>
#endif

#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>


static struct timeval orig,cur;
static uint32_t posix_timer_time=0;		/*in milisecond */

41
void posix_timer_init(void)
aymeric's avatar
aymeric committed
42 43
{
	posix_timer.state=RTP_TIMER_RUNNING;
44
	ortp_gettimeofday(&orig,NULL);
aymeric's avatar
aymeric committed
45 46 47 48 49 50
	posix_timer_time=0;
}




51
void posix_timer_do(void)
aymeric's avatar
aymeric committed
52 53 54
{
	int diff,time;
	struct timeval tv;
55
	ortp_gettimeofday(&cur,NULL);
aymeric's avatar
aymeric committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69
	time=((cur.tv_usec-orig.tv_usec)/1000 ) + ((cur.tv_sec-orig.tv_sec)*1000 );
	if ( (diff=time-posix_timer_time)>50){
		ortp_warning("Must catchup %i miliseconds.",diff);
	}
	while((diff = posix_timer_time-time) > 0)
	{
		tv.tv_sec = diff/1000;
		tv.tv_usec = (diff%1000)*1000;
#if	defined(_WIN32) || defined(_WIN32_WCE)
        /* this kind of select is not supported on windows */
		Sleep(tv.tv_usec/1000 + tv.tv_sec * 1000);
#else
		select(0,NULL,NULL,NULL,&tv);
#endif
70
		ortp_gettimeofday(&cur,NULL);
aymeric's avatar
aymeric committed
71 72 73 74 75 76
		time=((cur.tv_usec-orig.tv_usec)/1000 ) + ((cur.tv_sec-orig.tv_sec)*1000 );
	}
	posix_timer_time+=POSIXTIMER_INTERVAL/1000;
	
}

77
void posix_timer_uninit(void)
aymeric's avatar
aymeric committed
78 79 80 81 82 83 84 85 86 87 88
{
	posix_timer.state=RTP_TIMER_STOPPED;
}

RtpTimer posix_timer={	0,
						posix_timer_init,
						posix_timer_do,
						posix_timer_uninit,
						{0,POSIXTIMER_INTERVAL}};
							
							
89
#else //_WIN32
aymeric's avatar
aymeric committed
90

91
#ifdef ORTP_WINDOWS_DESKTOP
aymeric's avatar
aymeric committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

#include <windows.h>
#include <mmsystem.h>


MMRESULT timerId;
HANDLE   TimeEvent;
int      late_ticks;


static DWORD posix_timer_time;
static DWORD offset_time;


#define TIME_INTERVAL           50
#define TIME_RESOLUTION         10
#define TIME_TIMEOUT            100



jehan's avatar
jehan committed
112
void CALLBACK timerCb(UINT uID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
aymeric's avatar
aymeric committed
113 114 115 116 117 118 119 120 121 122 123 124
{
        // Check timerId
        if (timerId == uID)
        {
                SetEvent(TimeEvent);
                posix_timer_time += TIME_INTERVAL;
        }
}


void win_timer_init(void)
{
jehan's avatar
jehan committed
125
	timerId = timeSetEvent(TIME_INTERVAL, 10, timerCb, (DWORD)0, (UINT)(TIME_PERIODIC | TIME_CALLBACK_FUNCTION));
aymeric's avatar
aymeric committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
        TimeEvent = CreateEvent(NULL,FALSE,FALSE,NULL);

        late_ticks = 0;

        offset_time = GetTickCount();
        posix_timer_time=0;
}


void win_timer_do(void)
{
        DWORD diff;

        // If timer have expired while we where out of this method
        // Try to run after lost time.
        if (late_ticks > 0)
        {
                late_ticks--;
                posix_timer_time+=TIME_INTERVAL;
                return;
        }


        diff = GetTickCount() - posix_timer_time - offset_time;
        if( diff>TIME_INTERVAL && (diff<(1<<31)))
        {
                late_ticks = diff/TIME_INTERVAL;
                ortp_warning("we must catchup %i ticks.",late_ticks);
                return;
        }

        WaitForSingleObject(TimeEvent,TIME_TIMEOUT);
        return;
}


void win_timer_close(void)
{
	timeKillEvent(timerId); 
}

RtpTimer toto;

RtpTimer posix_timer={	0,
						win_timer_init,
						win_timer_do,
						win_timer_close,
						{0,TIME_INTERVAL * 1000}};
Ghislain MARY's avatar
Ghislain MARY committed
174

175
#elif defined(ORTP_WINDOWS_PHONE)
176 177 178 179 180 181 182 183 184

#include "winrttimer.h"

RtpTimer posix_timer={	0,
						winrt_timer_init,
						winrt_timer_do,
						winrt_timer_close,
						{0, TIME_INTERVAL * 1000}};

Ghislain MARY's avatar
Ghislain MARY committed
185
#endif
aymeric's avatar
aymeric committed
186 187

#endif // _WIN32