Skip to content

Commit

Permalink
Move AudioClock to Oboe (#2080)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertwu1 authored Aug 5, 2024
1 parent 907c35a commit 811a999
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 18 deletions.
2 changes: 1 addition & 1 deletion apps/OboeTester/app/src/main/cpp/NativeAudioContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#endif // DEBUG_CLOSE_RACE
#include <vector>

#include <common/AudioClock.h>
#include "oboe/AudioClock.h"
#include "util/WaveFileWriter.h"

#include "NativeAudioContext.h"
Expand Down
2 changes: 1 addition & 1 deletion apps/OboeTester/app/src/main/cpp/TestColdStartLatency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <aaudio/AAudioExtensions.h>

#include "common/OboeDebug.h"
#include "common/AudioClock.h"
#include "oboe/AudioClock.h"
#include "TestColdStartLatency.h"
#include "OboeTools.h"

Expand Down
2 changes: 1 addition & 1 deletion apps/OboeTester/app/src/main/cpp/TestRapidCycle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <aaudio/AAudioExtensions.h>

#include "common/OboeDebug.h"
#include "common/AudioClock.h"
#include "oboe/AudioClock.h"
#include "TestRapidCycle.h"

using namespace oboe;
Expand Down
2 changes: 1 addition & 1 deletion apps/OboeTester/app/src/main/cpp/TestRoutingCrash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <aaudio/AAudioExtensions.h>

#include "common/OboeDebug.h"
#include "common/AudioClock.h"
#include "oboe/AudioClock.h"
#include "TestRoutingCrash.h"

using namespace oboe;
Expand Down
1 change: 0 additions & 1 deletion src/common/AudioClock.h → include/oboe/AudioClock.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

namespace oboe {

// TODO: Move this class into the public headers because it is useful when calculating stream latency
class AudioClock {
public:
static int64_t getNanoseconds(clockid_t clockId = CLOCK_MONOTONIC) {
Expand Down
1 change: 1 addition & 0 deletions include/oboe/Oboe.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@
#include "oboe/FifoBuffer.h"
#include "oboe/OboeExtensions.h"
#include "oboe/FullDuplexStream.h"
#include "oboe/AudioClock.h"

#endif //OBOE_OBOE_H
2 changes: 1 addition & 1 deletion src/aaudio/AudioStreamAAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

#include "aaudio/AAudioLoader.h"
#include "aaudio/AudioStreamAAudio.h"
#include "common/AudioClock.h"
#include "common/OboeDebug.h"
#include "oboe/AudioClock.h"
#include "oboe/Utilities.h"
#include "AAudioExtensions.h"

Expand Down
2 changes: 1 addition & 1 deletion src/common/AdpfWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#include <stdint.h>
#include <sys/types.h>

#include "oboe/AudioClock.h"
#include "AdpfWrapper.h"
#include "AudioClock.h"
#include "OboeDebug.h"

typedef APerformanceHintManager* (*APH_getManager)();
Expand Down
6 changes: 3 additions & 3 deletions src/common/AudioStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
#include <pthread.h>
#include <thread>

#include <oboe/AudioStream.h>
#include "oboe/AudioClock.h"
#include "oboe/AudioStream.h"
#include "oboe/Utilities.h"
#include "OboeDebug.h"
#include "AudioClock.h"
#include <oboe/Utilities.h>

namespace oboe {

Expand Down
4 changes: 2 additions & 2 deletions src/common/StabilizedCallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/

#include "oboe/StabilizedCallback.h"
#include "common/AudioClock.h"
#include "common/Trace.h"
#include "oboe/AudioClock.h"
#include "oboe/StabilizedCallback.h"

constexpr int32_t kLoadGenerationStepSizeNanos = 20000;
constexpr float kPercentageOfCallbackToUse = 0.8;
Expand Down
3 changes: 1 addition & 2 deletions src/opensles/AudioOutputStreamOpenSLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@

#include <cassert>

#include <common/AudioClock.h>

#include "common/OboeDebug.h"
#include "oboe/AudioClock.h"
#include "oboe/AudioStreamBuilder.h"
#include "AudioOutputStreamOpenSLES.h"
#include "AudioStreamOpenSLES.h"
Expand Down
2 changes: 1 addition & 1 deletion src/opensles/AudioStreamBuffered.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#include "oboe/Oboe.h"

#include "common/OboeDebug.h"
#include "oboe/AudioClock.h"
#include "opensles/AudioStreamBuffered.h"
#include "common/AudioClock.h"

namespace oboe {

Expand Down
5 changes: 2 additions & 3 deletions src/opensles/AudioStreamOpenSLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
#include <cassert>
#include <android/log.h>

#include <oboe/AudioStream.h>
#include <common/AudioClock.h>

#include "common/OboeDebug.h"
#include "oboe/AudioClock.h"
#include "oboe/AudioStream.h"
#include "oboe/AudioStreamBuilder.h"
#include "EngineOpenSLES.h"
#include "AudioStreamOpenSLES.h"
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ include_directories(
add_executable(
testOboe
testAAudio.cpp
testAudioClock.cpp
testFlowgraph.cpp
testFullDuplexStream.cpp
testResampler.cpp
Expand Down
92 changes: 92 additions & 0 deletions tests/testAudioClock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* Test FlowGraph
*/

#include "math.h"
#include "stdio.h"

#include <gtest/gtest.h>
#include <oboe/Oboe.h>

using namespace oboe;

#define NANOS_PER_MICROSECOND ((int64_t) 1000)

constexpr int64_t kSleepTimeMicroSec = 50 * 1000;
constexpr double kMaxLatenessMicroSec = 20 * 1000;

TEST(TestAudioClock, GetNanosecondsMonotonic) {

int64_t startNanos = AudioClock::getNanoseconds(CLOCK_MONOTONIC);
usleep(kSleepTimeMicroSec);
int64_t endNanos = AudioClock::getNanoseconds(CLOCK_MONOTONIC);
ASSERT_GE(endNanos, startNanos + kSleepTimeMicroSec * kNanosPerMicrosecond);
ASSERT_LT(endNanos, startNanos + ((kSleepTimeMicroSec + kMaxLatenessMicroSec)
* kNanosPerMicrosecond));
}

TEST(TestAudioClock, GetNanosecondsRealtime) {

int64_t startNanos = AudioClock::getNanoseconds(CLOCK_REALTIME);
usleep(kSleepTimeMicroSec);
int64_t endNanos = AudioClock::getNanoseconds(CLOCK_REALTIME);
ASSERT_GE(endNanos, startNanos + kSleepTimeMicroSec * kNanosPerMicrosecond);
ASSERT_LT(endNanos, startNanos + ((kSleepTimeMicroSec + kMaxLatenessMicroSec)
* kNanosPerMicrosecond));
}

TEST(TestAudioClock, SleepUntilNanoTimeMonotonic) {

int64_t startNanos = AudioClock::getNanoseconds(CLOCK_MONOTONIC);
AudioClock::sleepUntilNanoTime(startNanos + kSleepTimeMicroSec * kNanosPerMicrosecond, CLOCK_MONOTONIC);
int64_t endNanos = AudioClock::getNanoseconds(CLOCK_MONOTONIC);
ASSERT_GE(endNanos, startNanos + kSleepTimeMicroSec * kNanosPerMicrosecond);
ASSERT_LT(endNanos, startNanos + ((kSleepTimeMicroSec + kMaxLatenessMicroSec)
* kNanosPerMicrosecond));
}

TEST(TestAudioClock, SleepUntilNanoTimeRealtime) {

int64_t startNanos = AudioClock::getNanoseconds(CLOCK_REALTIME);
AudioClock::sleepUntilNanoTime(startNanos + kSleepTimeMicroSec * kNanosPerMicrosecond, CLOCK_REALTIME);
int64_t endNanos = AudioClock::getNanoseconds(CLOCK_REALTIME);
ASSERT_GE(endNanos, startNanos + kSleepTimeMicroSec * kNanosPerMicrosecond);
ASSERT_LT(endNanos, startNanos + ((kSleepTimeMicroSec + kMaxLatenessMicroSec)
* kNanosPerMicrosecond));
}

TEST(TestAudioClock, SleepForNanosMonotonic) {

int64_t startNanos = AudioClock::getNanoseconds(CLOCK_MONOTONIC);
AudioClock::sleepForNanos(kSleepTimeMicroSec * kNanosPerMicrosecond, CLOCK_MONOTONIC);
int64_t endNanos = AudioClock::getNanoseconds(CLOCK_MONOTONIC);
ASSERT_GE(endNanos, startNanos + kSleepTimeMicroSec * kNanosPerMicrosecond);
ASSERT_LT(endNanos, startNanos + ((kSleepTimeMicroSec + kMaxLatenessMicroSec)
* kNanosPerMicrosecond));
}

TEST(TestAudioClock, SleepForNanosRealtime) {

int64_t startNanos = AudioClock::getNanoseconds(CLOCK_REALTIME);
AudioClock::sleepForNanos(kSleepTimeMicroSec * kNanosPerMicrosecond, CLOCK_REALTIME);
int64_t endNanos = AudioClock::getNanoseconds(CLOCK_REALTIME);
ASSERT_GE(endNanos, startNanos + kSleepTimeMicroSec * kNanosPerMicrosecond);
ASSERT_LT(endNanos, startNanos + ((kSleepTimeMicroSec + kMaxLatenessMicroSec)
* kNanosPerMicrosecond));
}

0 comments on commit 811a999

Please sign in to comment.