xref: /aosp_15_r20/external/oboe/apps/OboeTester/app/src/main/cpp/OboeTesterStreamCallback.cpp (revision 05767d913155b055644481607e6fa1e35e2fe72c)
1 /*
2  * Copyright 2020 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 #include <sched.h>
18 #include <cstring>
19 
20 #include "AudioStreamGateway.h"
21 #include "common/OboeDebug.h"
22 #include "oboe/Oboe.h"
23 #include "OboeStreamCallbackProxy.h"
24 #include "OboeTesterStreamCallback.h"
25 #include "OboeTools.h"
26 #include "synth/IncludeMeOnce.h"
27 
28 int32_t OboeTesterStreamCallback::mHangTimeMillis = 0;
29 
30 // Print if scheduler changes.
printScheduler()31 void OboeTesterStreamCallback::printScheduler() {
32 #if OBOE_ENABLE_LOGGING
33     int scheduler = sched_getscheduler(gettid());
34     if (scheduler != mPreviousScheduler) {
35         int schedulerType = scheduler & 0xFFFF; // mask off high flags
36         LOGD("callback CPU scheduler = 0x%08x = %s",
37              scheduler,
38              ((schedulerType == SCHED_FIFO) ? "SCHED_FIFO" :
39               ((schedulerType == SCHED_OTHER) ? "SCHED_OTHER" :
40                ((schedulerType == SCHED_RR) ? "SCHED_RR" : "UNKNOWN")))
41         );
42         mPreviousScheduler = scheduler;
43     }
44 #endif
45 }
46 
47 // Sleep to cause an XRun. Then reschedule.
maybeHang(const int64_t startNanos)48 void OboeTesterStreamCallback::maybeHang(const int64_t startNanos) {
49     if (mHangTimeMillis == 0) return;
50 
51     if (startNanos > mNextTimeToHang) {
52         LOGD("%s() start sleeping", __func__);
53         // Take short naps until it is time to wake up.
54         int64_t nowNanos = startNanos;
55         int64_t wakeupNanos = startNanos + (mHangTimeMillis * NANOS_PER_MILLISECOND);
56         while (nowNanos < wakeupNanos && mHangTimeMillis > 0) {
57             int32_t sleepTimeMicros = (int32_t) ((wakeupNanos - nowNanos) / 1000);
58             if (sleepTimeMicros == 0) break;
59             // The usleep() function can fail if it sleeps for more than one second.
60             // So sleep for several small intervals.
61             // This also allows us to exit the loop if mHangTimeMillis gets set to zero.
62             const int32_t maxSleepTimeMicros =  100 * 1000;
63             sleepTimeMicros = std::min(maxSleepTimeMicros, sleepTimeMicros);
64             usleep(sleepTimeMicros);
65             nowNanos = getNanoseconds();
66         }
67         // Calculate when we hang again.
68         const int32_t minDurationMillis = 500;
69         const int32_t maxDurationMillis = std::max(10000, mHangTimeMillis * 2);
70         int32_t durationMillis = mHangTimeMillis * 10;
71         durationMillis = std::max(minDurationMillis, std::min(maxDurationMillis, durationMillis));
72         mNextTimeToHang = startNanos + (durationMillis * NANOS_PER_MILLISECOND);
73         LOGD("%s() slept for %d msec, durationMillis = %d", __func__,
74              (int)((nowNanos - startNanos) / 1e6L),
75              durationMillis);
76     }
77 }
78 
getNanoseconds(clockid_t clockId)79 int64_t OboeTesterStreamCallback::getNanoseconds(clockid_t clockId) {
80     struct timespec time;
81     int result = clock_gettime(clockId, &time);
82     if (result < 0) {
83         return result;
84     }
85     return (time.tv_sec * 1e9) + time.tv_nsec;
86 }