Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame^] | 17 | #ifndef UTILITY_AUDIO_CLOCK_H |
| 18 | #define UTILITY_AUDIO_CLOCK_H |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 19 | |
Phil Burk | d8bdcab | 2017-01-03 17:20:30 -0800 | [diff] [blame] | 20 | #include <stdint.h> |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 21 | #include <time.h> |
Phil Burk | d8bdcab | 2017-01-03 17:20:30 -0800 | [diff] [blame] | 22 | |
| 23 | #include <oboe/OboeDefinitions.h> |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 24 | |
| 25 | class AudioClock { |
| 26 | public: |
| 27 | static oboe_nanoseconds_t getNanoseconds(clockid_t clockId = CLOCK_MONOTONIC) { |
| 28 | struct timespec time; |
| 29 | int result = clock_gettime(clockId, &time); |
| 30 | if (result < 0) { |
| 31 | return -errno; |
| 32 | } |
| 33 | return (time.tv_sec * OBOE_NANOS_PER_SECOND) + time.tv_nsec; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Sleep until the specified absolute time. |
| 38 | * Return immediately with OBOE_ERROR_ILLEGAL_ARGUMENT if a negative |
| 39 | * nanoTime is specified. |
| 40 | * |
| 41 | * @param nanoTime time to wake up |
| 42 | * @param clockId CLOCK_MONOTONIC is default |
| 43 | * @return 0, a negative error, or 1 if the call is interrupted by a signal handler (EINTR) |
| 44 | */ |
| 45 | static int sleepUntilNanoTime(oboe_nanoseconds_t nanoTime, |
| 46 | clockid_t clockId = CLOCK_MONOTONIC) { |
| 47 | if (nanoTime > 0) { |
| 48 | struct timespec time; |
| 49 | time.tv_sec = nanoTime / OBOE_NANOS_PER_SECOND; |
| 50 | // Calculate the fractional nanoseconds. Avoids expensive % operation. |
| 51 | time.tv_nsec = nanoTime - (time.tv_sec * OBOE_NANOS_PER_SECOND); |
| 52 | int err = clock_nanosleep(clockId, TIMER_ABSTIME, &time, nullptr); |
| 53 | switch (err) { |
| 54 | case EINTR: |
| 55 | return 1; |
| 56 | case 0: |
| 57 | return 0; |
| 58 | default: |
| 59 | // Subtract because clock_nanosleep() returns a positive error number! |
| 60 | return 0 - err; |
| 61 | } |
| 62 | } else { |
| 63 | return OBOE_ERROR_ILLEGAL_ARGUMENT; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Sleep for the specified number of relative nanoseconds in real-time. |
| 69 | * Return immediately with 0 if a negative nanoseconds is specified. |
| 70 | * |
| 71 | * @param nanoseconds time to sleep |
| 72 | * @param clockId CLOCK_MONOTONIC is default |
| 73 | * @return 0, a negative error, or 1 if the call is interrupted by a signal handler (EINTR) |
| 74 | */ |
| 75 | static int sleepForNanos(oboe_nanoseconds_t nanoseconds, clockid_t clockId = CLOCK_MONOTONIC) { |
| 76 | if (nanoseconds > 0) { |
| 77 | struct timespec time; |
| 78 | time.tv_sec = nanoseconds / OBOE_NANOS_PER_SECOND; |
| 79 | // Calculate the fractional nanoseconds. Avoids expensive % operation. |
| 80 | time.tv_nsec = nanoseconds - (time.tv_sec * OBOE_NANOS_PER_SECOND); |
| 81 | const int flags = 0; // documented as relative sleep |
| 82 | int err = clock_nanosleep(clockId, flags, &time, nullptr); |
| 83 | switch (err) { |
| 84 | case EINTR: |
| 85 | return 1; |
| 86 | case 0: |
| 87 | return 0; |
| 88 | default: |
| 89 | // Subtract because clock_nanosleep() returns a positive error number! |
| 90 | return 0 - err; |
| 91 | } |
| 92 | } |
| 93 | return 0; |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame^] | 98 | #endif // UTILITY_AUDIO_CLOCK_H |