1 /*
2 * Copyright 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef ANDROID_AUDIO_CLOCK_H
18 #define ANDROID_AUDIO_CLOCK_H
19
20 // This file can be included for either C or C++ source.
21
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <sys/time.h>
25 #include <time.h>
26
27 /**
28 * \brief Converts time in ns to a time string, with format similar to logcat.
29 * \param ns input time in nanoseconds to convert.
30 * \param buffer caller allocated string buffer, buffer_length must be >= 19 chars
31 * in order to fully fit in time. The string is always returned
32 * null terminated if buffer_size is greater than zero.
33 * \param buffer_size size of buffer.
34 */
audio_utils_ns_to_string(int64_t ns,char * buffer,size_t buffer_size)35 static inline void audio_utils_ns_to_string(int64_t ns, char *buffer, size_t buffer_size)
36 {
37 if (buffer_size == 0) return;
38
39 const int one_second = 1000000000;
40 const time_t sec = ns / one_second;
41 struct tm tm;
42
43 // Supported on bionic, glibc, and macOS, but not mingw.
44 if (localtime_r(&sec, &tm) == NULL) {
45 buffer[0] = '\0';
46 return;
47 }
48
49 if (snprintf(buffer, buffer_size, "%02d-%02d %02d:%02d:%02d.%03d",
50 tm.tm_mon + 1, // localtime_r uses months in 0 - 11 range
51 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
52 (int)(ns % one_second / 1000000)) < 0) {
53 buffer[0] = '\0'; // null terminate on format error, which should not happen
54 }
55 }
56
57 /**
58 * An object that contains the formatted time string.
59 *
60 * The time string is 19 characters (including null termination).
61 * Example: "03-27 16:47:06.187"
62 * MM DD HH MM SS MS
63 */
64 typedef struct audio_utils_time_string {
65 char time[19]; /* minimum size buffer */
66 } audio_utils_time_string_t;
67
68 /**
69 * \brief Converts time in ns to a time string object, with format similar to logcat.
70 * \param ns input time in nanoseconds to convert.
71 */
audio_utils_time_string_from_ns(int64_t ns)72 static inline audio_utils_time_string_t audio_utils_time_string_from_ns(int64_t ns)
73 {
74 audio_utils_time_string_t ts;
75
76 audio_utils_ns_to_string(ns, ts.time, sizeof(ts.time));
77 return ts;
78 }
79
80 /**
81 * \brief Converts a timespec to nanoseconds.
82 * \param ts input timespec to convert.
83 * \return timespec converted to nanoseconds.
84 */
audio_utils_ns_from_timespec(const struct timespec * ts)85 static inline int64_t audio_utils_ns_from_timespec(const struct timespec *ts)
86 {
87 return ts->tv_sec * 1000000000LL + ts->tv_nsec;
88 }
89
90 /**
91 * \brief Gets the real time clock in nanoseconds.
92 * \return the real time clock in nanoseconds, or 0 on error.
93 */
audio_utils_get_real_time_ns()94 static inline int64_t audio_utils_get_real_time_ns() {
95
96 #if defined(__linux__)
97
98 struct timespec now_ts;
99 if (clock_gettime(CLOCK_REALTIME, &now_ts) == 0) {
100 return audio_utils_ns_from_timespec(&now_ts);
101 }
102 return 0; // should not happen.
103
104 #else
105
106 // Mac OS X compatible
107 struct timeval now_tv;
108 if (gettimeofday(&now_tv, NULL /* struct timezone * */) == 0) {
109 return now_tv.tv_sec * 1000000000LL + now_tv.tv_usec * 1000LL;
110 }
111 return 0; // should not happen.
112
113 #endif
114
115 }
116
117 #endif // !ANDROID_AUDIO_CLOCK_H
118