2005-06-24 22:50:07 +00:00
|
|
|
/*
|
2005-08-30 18:32:10 +00:00
|
|
|
* Asterisk -- An open source telephony toolkit.
|
2005-06-24 22:50:07 +00:00
|
|
|
*
|
2005-08-30 18:32:10 +00:00
|
|
|
* Copyright (C) 1999 - 2005, Digium, Inc.
|
|
|
|
*
|
|
|
|
* Mark Spencer <markster@digium.com>
|
2005-06-24 22:50:07 +00:00
|
|
|
*
|
2005-08-30 18:32:10 +00:00
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
2005-06-24 22:50:07 +00:00
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
2005-08-30 18:32:10 +00:00
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
2005-10-24 20:12:06 +00:00
|
|
|
/*! \file
|
|
|
|
* \brief Time-related functions and macros
|
2005-06-24 22:50:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _ASTERISK_TIME_H
|
|
|
|
#define _ASTERISK_TIME_H
|
|
|
|
|
2015-07-24 17:04:35 -05:00
|
|
|
#include "asterisk/autoconfig.h"
|
|
|
|
|
Start untangling header inclusion in a way that does not affect
build times - tested, there is no measureable difference before and
after this commit.
In this change:
use asterisk/compat.h to include a small set of system headers:
inttypes.h, unistd.h, stddef.h, stddint.h, sys/types.h, stdarg.h,
stdlib.h, alloca.h, stdio.h
Where available, the inclusion is conditional on HAVE_FOO_H as determined
by autoconf.
Normally, source files should not include any of the above system headers,
and instead use either "asterisk.h" or "asterisk/compat.h" which does it
better.
For the time being I have left alone second-level directories
(main/db1-ast, etc.).
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89333 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-16 20:04:58 +00:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
2005-06-24 22:50:07 +00:00
|
|
|
#include <sys/time.h>
|
Start untangling header inclusion in a way that does not affect
build times - tested, there is no measureable difference before and
after this commit.
In this change:
use asterisk/compat.h to include a small set of system headers:
inttypes.h, unistd.h, stddef.h, stddint.h, sys/types.h, stdarg.h,
stdlib.h, alloca.h, stdio.h
Where available, the inclusion is conditional on HAVE_FOO_H as determined
by autoconf.
Normally, source files should not include any of the above system headers,
and instead use either "asterisk.h" or "asterisk/compat.h" which does it
better.
For the time being I have left alone second-level directories
(main/db1-ast, etc.).
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89333 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-16 20:04:58 +00:00
|
|
|
#endif
|
2005-06-24 22:50:07 +00:00
|
|
|
|
2015-07-24 17:04:35 -05:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2005-07-11 23:25:31 +00:00
|
|
|
#include "asterisk/inline_api.h"
|
|
|
|
|
2005-07-19 19:06:24 +00:00
|
|
|
/* We have to let the compiler learn what types to use for the elements of a
|
|
|
|
struct timeval since on linux, it's time_t and suseconds_t, but on *BSD,
|
2016-01-07 10:21:03 +01:00
|
|
|
they are just a long.
|
|
|
|
note:dummy_tv_var_for_types never actually gets exported, only used as
|
|
|
|
local place holder. */
|
|
|
|
extern struct timeval dummy_tv_var_for_types;
|
|
|
|
typedef typeof(dummy_tv_var_for_types.tv_sec) ast_time_t;
|
|
|
|
typedef typeof(dummy_tv_var_for_types.tv_usec) ast_suseconds_t;
|
2005-07-19 19:06:24 +00:00
|
|
|
|
2007-12-19 19:29:14 +00:00
|
|
|
/*!
|
|
|
|
* \brief Computes the difference (in seconds) between two \c struct \c timeval instances.
|
|
|
|
* \param end the end of the time period
|
|
|
|
* \param start the beginning of the time period
|
|
|
|
* \return the difference in seconds
|
|
|
|
*/
|
|
|
|
AST_INLINE_API(
|
2010-01-18 22:31:25 +00:00
|
|
|
int64_t ast_tvdiff_sec(struct timeval end, struct timeval start),
|
2007-12-19 19:29:14 +00:00
|
|
|
{
|
2010-01-18 22:31:25 +00:00
|
|
|
int64_t result = end.tv_sec - start.tv_sec;
|
2007-12-19 19:29:14 +00:00
|
|
|
if (result > 0 && end.tv_usec < start.tv_usec)
|
|
|
|
result--;
|
|
|
|
else if (result < 0 && end.tv_usec > start.tv_usec)
|
|
|
|
result++;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Computes the difference (in microseconds) between two \c struct \c timeval instances.
|
2008-07-02 14:50:45 +00:00
|
|
|
* \param end the end of the time period
|
|
|
|
* \param start the beginning of the time period
|
2007-12-19 19:29:14 +00:00
|
|
|
* \return the difference in microseconds
|
|
|
|
*/
|
|
|
|
AST_INLINE_API(
|
|
|
|
int64_t ast_tvdiff_us(struct timeval end, struct timeval start),
|
|
|
|
{
|
|
|
|
return (end.tv_sec - start.tv_sec) * (int64_t) 1000000 +
|
|
|
|
end.tv_usec - start.tv_usec;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2005-06-24 22:50:07 +00:00
|
|
|
/*!
|
|
|
|
* \brief Computes the difference (in milliseconds) between two \c struct \c timeval instances.
|
2007-12-17 21:14:45 +00:00
|
|
|
* \param end end of the time period
|
|
|
|
* \param start beginning of the time period
|
2005-06-24 22:50:07 +00:00
|
|
|
* \return the difference in milliseconds
|
|
|
|
*/
|
2005-07-11 23:25:31 +00:00
|
|
|
AST_INLINE_API(
|
2010-01-18 22:31:25 +00:00
|
|
|
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start),
|
2005-06-24 22:50:07 +00:00
|
|
|
{
|
2005-07-19 23:28:12 +00:00
|
|
|
/* the offset by 1,000,000 below is intentional...
|
|
|
|
it avoids differences in the way that division
|
|
|
|
is handled for positive and negative numbers, by ensuring
|
|
|
|
that the divisor is always positive
|
|
|
|
*/
|
Prevent overflow in calculation in ast_tvdiff_ms on 32-bit machines
The method ast_tvdiff_ms attempts to calculate the difference, in milliseconds,
between two timeval structs, and return the difference in a 64-bit integer.
Unfortunately, it assumes that the long tv_sec/tv_usec members in the timeval
struct are large enough to hold the calculated values before it returns. On
64-bit machines, this might be the case, as a long may be 64-bits. On 32-bit
machines, however, a long may be less (32-bits), in which case, the calculation
can overflow.
This overflow caused significant problems in MixMonitor, which uses the method
to determine if an audio factory, which has not presented audio to an audiohook,
is merely late in providing said audio or will never provide audio. In an
overflow situation, the audiohook would incorrectly determine that an audio
factory that will never provide audio is merely late instead. This led to
situations where a MixMonitor never recorded any audio. Note that this happened
most frequently when that MixMonitor was started by the ConfBridge application
itself, or when the MixMonitor was attached to a Local channel.
(issue ASTERISK-19497)
Reported by: Ben Klang
Tested by: Ben Klang
Patches:
32-bit-time-overflow-10-2012-04-26.diff (license #6283) by mjordan
(closes issue ASTERISK-19727)
Reported by: Mark Murawski
Tested by: Michael L. Young
Patches:
32-bit-time-overflow-2012-04-27.diff (license #6283) by mjordan)
(closes issue ASTERISK-19471)
Reported by: feyfre
Tested by: feyfre
(issue ASTERISK-19426)
Reported by: Johan Wilfer
Review: https://reviewboard.asterisk.org/r/1889/
........
Merged revisions 364277 from http://svn.asterisk.org/svn/asterisk/branches/1.8
........
Merged revisions 364285 from http://svn.asterisk.org/svn/asterisk/branches/10
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@364287 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2012-04-27 19:30:59 +00:00
|
|
|
int64_t sec_dif = (int64_t)(end.tv_sec - start.tv_sec) * 1000;
|
|
|
|
int64_t usec_dif = (1000000 + end.tv_usec - start.tv_usec) / 1000 - 1000;
|
|
|
|
return sec_dif + usec_dif;
|
2005-07-15 23:00:47 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Returns true if the argument is 0,0
|
|
|
|
*/
|
|
|
|
AST_INLINE_API(
|
|
|
|
int ast_tvzero(const struct timeval t),
|
|
|
|
{
|
|
|
|
return (t.tv_sec == 0 && t.tv_usec == 0);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Compres two \c struct \c timeval instances returning
|
|
|
|
* -1, 0, 1 if the first arg is smaller, equal or greater to the second.
|
|
|
|
*/
|
|
|
|
AST_INLINE_API(
|
|
|
|
int ast_tvcmp(struct timeval _a, struct timeval _b),
|
|
|
|
{
|
|
|
|
if (_a.tv_sec < _b.tv_sec)
|
|
|
|
return -1;
|
|
|
|
if (_a.tv_sec > _b.tv_sec)
|
|
|
|
return 1;
|
|
|
|
/* now seconds are equal */
|
|
|
|
if (_a.tv_usec < _b.tv_usec)
|
|
|
|
return -1;
|
|
|
|
if (_a.tv_usec > _b.tv_usec)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Returns true if the two \c struct \c timeval arguments are equal.
|
|
|
|
*/
|
|
|
|
AST_INLINE_API(
|
|
|
|
int ast_tveq(struct timeval _a, struct timeval _b),
|
|
|
|
{
|
|
|
|
return (_a.tv_sec == _b.tv_sec && _a.tv_usec == _b.tv_usec);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Returns current timeval. Meant to replace calls to gettimeofday().
|
|
|
|
*/
|
|
|
|
AST_INLINE_API(
|
|
|
|
struct timeval ast_tvnow(void),
|
|
|
|
{
|
|
|
|
struct timeval t;
|
|
|
|
gettimeofday(&t, NULL);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2015-07-24 17:04:35 -05:00
|
|
|
/*!
|
|
|
|
* \brief Returns current timespec. Meant to avoid calling ast_tvnow() just to
|
|
|
|
* create a timespec from the timeval it returns.
|
|
|
|
*/
|
|
|
|
#if defined _POSIX_TIMERS && _POSIX_TIMERS > 0
|
|
|
|
AST_INLINE_API(
|
|
|
|
struct timespec ast_tsnow(void),
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
#else
|
|
|
|
AST_INLINE_API(
|
|
|
|
struct timespec ast_tsnow(void),
|
|
|
|
{
|
|
|
|
struct timeval tv = ast_tvnow();
|
|
|
|
struct timespec ts;
|
|
|
|
/* Can't use designated initializer, because it does odd things with
|
|
|
|
* the AST_INLINE_API macro. Go figure. */
|
|
|
|
ts.tv_sec = tv.tv_sec;
|
|
|
|
ts.tv_nsec = tv.tv_usec * 1000;
|
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
#endif
|
|
|
|
|
2005-07-15 23:00:47 +00:00
|
|
|
/*!
|
|
|
|
* \brief Returns the sum of two timevals a + b
|
|
|
|
*/
|
|
|
|
struct timeval ast_tvadd(struct timeval a, struct timeval b);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Returns the difference of two timevals a - b
|
|
|
|
*/
|
|
|
|
struct timeval ast_tvsub(struct timeval a, struct timeval b);
|
|
|
|
|
2013-06-17 03:00:38 +00:00
|
|
|
/*!
|
|
|
|
* \since 12
|
|
|
|
* \brief Formats a duration into HH:MM:SS
|
|
|
|
*
|
|
|
|
* \param duration The time (in seconds) to format
|
|
|
|
* \param buf A buffer to hold the formatted string'
|
|
|
|
* \param length The size of the buffer
|
|
|
|
*/
|
|
|
|
void ast_format_duration_hh_mm_ss(int duration, char *buf, size_t length);
|
|
|
|
|
|
|
|
|
2012-11-07 19:15:26 +00:00
|
|
|
/*!
|
|
|
|
* \brief Calculate remaining milliseconds given a starting timestamp
|
|
|
|
* and upper bound
|
|
|
|
*
|
|
|
|
* If the upper bound is negative, then this indicates that there is no
|
|
|
|
* upper bound on the amount of time to wait. This will result in a
|
|
|
|
* negative return.
|
|
|
|
*
|
|
|
|
* \param start When timing started being calculated
|
|
|
|
* \param max_ms The maximum number of milliseconds to wait from start. May be negative.
|
|
|
|
* \return The number of milliseconds left to wait for. May be negative.
|
|
|
|
*/
|
|
|
|
int ast_remaining_ms(struct timeval start, int max_ms);
|
|
|
|
|
2005-07-15 23:00:47 +00:00
|
|
|
/*!
|
|
|
|
* \brief Returns a timeval from sec, usec
|
|
|
|
*/
|
|
|
|
AST_INLINE_API(
|
2005-07-19 19:06:24 +00:00
|
|
|
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec),
|
2005-07-15 23:00:47 +00:00
|
|
|
{
|
|
|
|
struct timeval t;
|
|
|
|
t.tv_sec = sec;
|
|
|
|
t.tv_usec = usec;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Returns a timeval corresponding to the duration of n samples at rate r.
|
|
|
|
* Useful to convert samples to timevals, or even milliseconds to timevals
|
|
|
|
* in the form ast_samp2tv(milliseconds, 1000)
|
|
|
|
*/
|
|
|
|
AST_INLINE_API(
|
2005-07-19 15:30:31 +00:00
|
|
|
struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate),
|
2005-07-15 23:00:47 +00:00
|
|
|
{
|
Media Project Phase2: SILK 8khz-24khz, SLINEAR 8khz-192khz, SPEEX 32khz, hd audio ConfBridge, and other stuff
-Functional changes
1. Dynamic global format list build by codecs defined in codecs.conf
2. SILK 8khz, 12khz, 16khz, and 24khz with custom attributes defined in codecs.conf
3. Negotiation of SILK attributes in chan_sip.
4. SPEEX 32khz with translation
5. SLINEAR 8khz, 12khz, 24khz, 32khz, 44.1khz, 48khz, 96khz, 192khz with translation
using codec_resample.c
6. Various changes to RTP code required to properly handle the dynamic format list
and formats with attributes.
7. ConfBridge now dynamically jumps to the best possible sample rate. This allows
for conferences to take advantage of HD audio (Which sounds awesome)
8. Audiohooks are no longer limited to 8khz audio, and most effects have been
updated to take advantage of this such as Volume, DENOISE, PITCH_SHIFT.
9. codec_resample now uses its own code rather than depending on libresample.
-Organizational changes
Global format list is moved from frame.c to format.c
Various format specific functions moved from frame.c to format.c
Review: https://reviewboard.asterisk.org/r/1104/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@308582 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2011-02-22 23:04:49 +00:00
|
|
|
return ast_tv(_nsamp / _rate, (_nsamp % _rate) * (1000000 / (float) _rate));
|
2005-06-24 22:50:07 +00:00
|
|
|
}
|
2005-07-11 23:25:31 +00:00
|
|
|
)
|
2005-06-24 22:50:07 +00:00
|
|
|
|
|
|
|
#endif /* _ASTERISK_TIME_H */
|