aboutsummaryrefslogtreecommitdiff
path: root/apps/OboeTester/app/src/main/cpp/OboeTesterStreamCallback.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'apps/OboeTester/app/src/main/cpp/OboeTesterStreamCallback.cpp')
-rw-r--r--apps/OboeTester/app/src/main/cpp/OboeTesterStreamCallback.cpp52
1 files changed, 49 insertions, 3 deletions
diff --git a/apps/OboeTester/app/src/main/cpp/OboeTesterStreamCallback.cpp b/apps/OboeTester/app/src/main/cpp/OboeTesterStreamCallback.cpp
index 583c9846..5109f67c 100644
--- a/apps/OboeTester/app/src/main/cpp/OboeTesterStreamCallback.cpp
+++ b/apps/OboeTester/app/src/main/cpp/OboeTesterStreamCallback.cpp
@@ -14,13 +14,18 @@
* limitations under the License.
*/
+#include <sched.h>
+#include <cstring>
#include "AudioStreamGateway.h"
-#include "oboe/Oboe.h"
#include "common/OboeDebug.h"
-#include <sched.h>
-#include <cstring>
+#include "oboe/Oboe.h"
+#include "OboeStreamCallbackProxy.h"
#include "OboeTesterStreamCallback.h"
+#include "OboeTools.h"
+#include "synth/IncludeMeOnce.h"
+
+int32_t OboeTesterStreamCallback::mHangTimeMillis = 0;
// Print if scheduler changes.
void OboeTesterStreamCallback::printScheduler() {
@@ -38,3 +43,44 @@ void OboeTesterStreamCallback::printScheduler() {
}
#endif
}
+
+// Sleep to cause an XRun. Then reschedule.
+void OboeTesterStreamCallback::maybeHang(const int64_t startNanos) {
+ if (mHangTimeMillis == 0) return;
+
+ if (startNanos > mNextTimeToHang) {
+ LOGD("%s() start sleeping", __func__);
+ // Take short naps until it is time to wake up.
+ int64_t nowNanos = startNanos;
+ int64_t wakeupNanos = startNanos + (mHangTimeMillis * NANOS_PER_MILLISECOND);
+ while (nowNanos < wakeupNanos && mHangTimeMillis > 0) {
+ int32_t sleepTimeMicros = (int32_t) ((wakeupNanos - nowNanos) / 1000);
+ if (sleepTimeMicros == 0) break;
+ // The usleep() function can fail if it sleeps for more than one second.
+ // So sleep for several small intervals.
+ // This also allows us to exit the loop if mHangTimeMillis gets set to zero.
+ const int32_t maxSleepTimeMicros = 100 * 1000;
+ sleepTimeMicros = std::min(maxSleepTimeMicros, sleepTimeMicros);
+ usleep(sleepTimeMicros);
+ nowNanos = getNanoseconds();
+ }
+ // Calculate when we hang again.
+ const int32_t minDurationMillis = 500;
+ const int32_t maxDurationMillis = std::max(10000, mHangTimeMillis * 2);
+ int32_t durationMillis = mHangTimeMillis * 10;
+ durationMillis = std::max(minDurationMillis, std::min(maxDurationMillis, durationMillis));
+ mNextTimeToHang = startNanos + (durationMillis * NANOS_PER_MILLISECOND);
+ LOGD("%s() slept for %d msec, durationMillis = %d", __func__,
+ (int)((nowNanos - startNanos) / 1e6L),
+ durationMillis);
+ }
+}
+
+int64_t OboeTesterStreamCallback::getNanoseconds(clockid_t clockId) {
+ struct timespec time;
+ int result = clock_gettime(clockId, &time);
+ if (result < 0) {
+ return result;
+ }
+ return (time.tv_sec * 1e9) + time.tv_nsec;
+} \ No newline at end of file