UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
tv.c
Go to the documentation of this file.
1/*
2 * FILE: tv.c
3 * AUTHOR: Colin Perkins <csp@csperkins.org>
4 * MODIFIED: Ladan Gharai <ladan@isi.edu>
5 *
6 * Copyright (c) 2001-2003 University of Southern California
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, is permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 *
22 * This product includes software developed by the University of Southern
23 * California Information Sciences Institute.
24 *
25 * 4. Neither the name of the University nor of the Institute may be used
26 * to endorse or promote products derived from this software without
27 * specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
31 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
32 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
33 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
34 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
39 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
40 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * $Revision: 1.1 $
43 * $Date: 2007/11/08 09:48:59 $
44 *
45 */
46
47#include "config.h"
48#include "config_unix.h"
49#include "config_win32.h"
50#include "debug.h"
51#include "tv.h"
52#include "utils/random.h"
53
54#include <pthread.h>
55
56pthread_once_t once_control = PTHREAD_ONCE_INIT;
57static struct timeval start_time;
58static uint32_t random_offset;
59static void init_first(void)
60{
61 gettimeofday(&start_time, NULL);
63}
64
65uint32_t get_local_mediatime(void)
66{
67 pthread_once(&once_control, init_first);
68 struct timeval curr_time;
69 gettimeofday(&curr_time, NULL);
70 return (tv_diff(curr_time, start_time) * 90000) + random_offset;
71}
72
74{
75 pthread_once(&once_control, init_first);
76 return random_offset;
77}
78
79void ts_add_nsec(struct timespec *ts, long long offset)
80{
81 long long new_nsec = ts->tv_nsec + offset;
82
83 if (new_nsec < NS_IN_SEC) {
84 ts->tv_nsec = new_nsec;
85 return;
86 }
87 if (new_nsec < 2 * NS_IN_SEC) {
88 ts->tv_sec++;
89 ts->tv_nsec = new_nsec - NS_IN_SEC;
90 return;
91 }
92 lldiv_t d = lldiv(new_nsec, NS_IN_SEC);
93 ts->tv_sec += d.quot;
94 ts->tv_nsec = d.rem;
95}
96
97double tv_diff(struct timeval curr_time, struct timeval prev_time)
98{
99 /* Return (curr_time - prev_time) in seconds */
100 double ct, pt;
101
102 ct = (double)curr_time.tv_sec +
103 (((double)curr_time.tv_usec) / 1000000.0);
104 pt = (double)prev_time.tv_sec +
105 (((double)prev_time.tv_usec) / 1000000.0);
106 return (ct - pt);
107}
108
109uint32_t tv_diff_usec(struct timeval curr_time, struct timeval prev_time)
110{
111 /* Return curr_time - prev_time in usec - i wonder if these numbers will be too big? */
112 uint32_t tmp, tmp1, tmp2;
113
114 /* We return an unsigned, so fail is prev_time is later than curr_time */
115 if (curr_time.tv_sec < prev_time.tv_sec ||
116 (curr_time.tv_sec == prev_time.tv_sec && curr_time.tv_usec < prev_time.tv_usec)) {
117 log_msg(LOG_LEVEL_WARNING, "Discontinuity in time: %lf s!\n", tv_diff(curr_time, prev_time));
118 return 0;
119 }
120
121 tmp1 = (curr_time.tv_sec - prev_time.tv_sec) * ((uint32_t) 1000000);
122 tmp2 = curr_time.tv_usec - prev_time.tv_usec;
123 tmp = tmp1 + tmp2;
124
125 return tmp;
126}
127
128inline void tv_add_usec(struct timeval *ts, long long offset) __attribute__((always_inline)); // to allow tv_add inline this function
129
130void tv_add(struct timeval *ts, double offset_secs)
131{
132 tv_add_usec(ts, offset_secs * 1000000.0);
133}
134
135void tv_add_usec(struct timeval *ts, long long offset)
136{
137 long long new_usec = ts->tv_usec + offset;
138
139 if (new_usec < US_IN_SEC) {
140 ts->tv_usec = new_usec;
141 return;
142 }
143 if (new_usec < 2 * US_IN_SEC) {
144 ts->tv_sec++;
145 ts->tv_usec = new_usec - US_IN_SEC;
146 return;
147 }
148 lldiv_t d = lldiv(new_usec, US_IN_SEC);
149 ts->tv_sec += d.quot;
150 ts->tv_usec = d.rem;
151}
152
153int tv_gt(struct timeval a, struct timeval b)
154{
155 /* Returns (a>b) */
156 if (a.tv_sec > b.tv_sec) {
157 return TRUE;
158 }
159 if (a.tv_sec < b.tv_sec) {
160 return FALSE;
161 }
162 assert(a.tv_sec == b.tv_sec);
163 return a.tv_usec > b.tv_usec;
164}
165
166/*
167 * STANDARD TRANSPORT - RTP STANDARD
168 * Calculate initial time on first execution, add per 'sample' time otherwise.
169 */
170typedef struct { //shared struct for audio and video streams (sync.)
171 bool init;
173 struct timeval vtime;
174 double vfps;
175 struct timeval atime;
176 struct timeval start_time;
178
179std_time_struct standard_time = { true, 0, { 0, 0 }, 25, { 0, 0 }, { 0, 0 } };
180
185uint32_t get_std_audio_local_mediatime(double samples, int rate)
186{
187 if (standard_time.init) {
188 gettimeofday(&standard_time.start_time, NULL);
194
195 standard_time.init = false;
196 }
197 else {
198 tv_add(&standard_time.atime, samples);
199 }
200
201 return ((double)standard_time.atime.tv_sec + (((double)standard_time.atime.tv_usec) / 1000000.0)) * rate;
202}
203
205{
206 double vrate = 90000; //default and standard video sample rate (Hz)
207 double nextFraction;
208 unsigned nextSecsIncrement;
209 static struct timeval t0;
210 struct timeval tcurr;
211
212 if (standard_time.init) {
213 gettimeofday(&standard_time.start_time, NULL);
214 gettimeofday(&t0, NULL);
220
221 standard_time.init = false;
222 }
223 else {
224 gettimeofday(&tcurr, NULL);
225 nextFraction = ( standard_time.vtime.tv_usec / 1000000.0 ) + ( tv_diff(tcurr,t0));
226 nextSecsIncrement = (long) nextFraction;
227 standard_time.vtime.tv_sec += (long) nextSecsIncrement;
228 standard_time.vtime.tv_usec = (long) ((nextFraction - nextSecsIncrement) * 1000000);
229 t0 = tcurr;
230 }
231
232 return ((double)standard_time.vtime.tv_sec + (((double)standard_time.vtime.tv_usec) / 1000000.0)) * vrate;
233}
234
235#if !defined HAVE_TIMESPEC_GET
236int timespec_get(struct timespec *ts, int base)
237{
238 struct timeval tv = { 0, 0 };
239 gettimeofday(&tv, NULL);
240 ts->tv_sec = tv.tv_sec;
241 ts->tv_nsec = tv.tv_usec * 1000L;
242 return base; // returning base means success
243
244}
245#endif
#define TRUE
#define FALSE
static __device__ const float offset
Definition cuda_dxt.cu:139
void log_msg(int level, const char *format,...)
Definition debug.cpp:130
#define LOG_LEVEL_WARNING
less severe errors
Definition debug.h:60
struct __attribute__((packed))
Definition rtp.h:88
bool init
Definition tv.c:171
struct timeval start_time
Definition tv.c:176
struct timeval atime
Definition tv.c:175
double vfps
Definition tv.c:174
uint32_t random_startime_offset
Definition tv.c:172
struct timeval vtime
Definition tv.c:173
uint32_t ts
Definition tfrc.c:101
uint32_t tv_diff_usec(struct timeval curr_time, struct timeval prev_time)
Definition tv.c:109
uint32_t get_local_mediatime_offset(void)
Definition tv.c:73
static uint32_t random_offset
Definition tv.c:58
void ts_add_nsec(struct timespec *ts, long long offset)
Definition tv.c:79
static void init_first(void)
Definition tv.c:59
uint32_t get_std_video_local_mediatime(void)
Definition tv.c:204
std_time_struct standard_time
Definition tv.c:179
void tv_add_usec(struct timeval *ts, long long offset) __attribute__((always_inline))
Definition tv.c:135
void tv_add(struct timeval *ts, double offset_secs)
Definition tv.c:130
pthread_once_t once_control
Definition tv.c:56
int timespec_get(struct timespec *ts, int base)
Definition tv.c:236
int tv_gt(struct timeval a, struct timeval b)
Definition tv.c:153
static struct timeval start_time
Definition tv.c:57
uint32_t get_std_audio_local_mediatime(double samples, int rate)
Definition tv.c:185
double tv_diff(struct timeval curr_time, struct timeval prev_time)
Definition tv.c:97
uint32_t get_local_mediatime(void)
Definition tv.c:65
#define NS_IN_SEC
Definition tv.h:92
#define US_IN_SEC
Definition tv.h:89
uint32_t ug_rand(void)
Definition random.c:88