xref: /aosp_15_r20/external/oboe/samples/hello-oboe/src/main/cpp/HelloOboeEngine.h (revision 05767d913155b055644481607e6fa1e35e2fe72c)
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 OBOE_HELLO_OBOE_ENGINE_H
18 #define OBOE_HELLO_OBOE_ENGINE_H
19 
20 #include <oboe/Oboe.h>
21 
22 #include "SoundGenerator.h"
23 #include "LatencyTuningCallback.h"
24 #include "IRestartable.h"
25 #include "DefaultErrorCallback.h"
26 
27 constexpr int32_t kBufferSizeAutomatic = 0;
28 
29 class HelloOboeEngine : public IRestartable {
30 
31 public:
32     HelloOboeEngine();
33 
34     virtual ~HelloOboeEngine() = default;
35 
36     void tap(bool isDown);
37 
38     /**
39      * Open and start a stream.
40      * @param deviceId the audio device id, can be obtained through an {@link AudioDeviceInfo} object
41      * using Java/JNI.
42      * @return error or OK
43      */
44     oboe::Result start(oboe::AudioApi audioApi, int deviceId, int channelCount);
45     /* Start using current settings. */
46     oboe::Result start();
47 
48     /**
49      * Stop and close the stream.
50      */
51     oboe::Result stop();
52 
53     // From IRestartable
54     void restart() override;
55 
56     void setBufferSizeInBursts(int32_t numBursts);
57 
58     /**
59      * Calculate the current latency between writing a frame to the output stream and
60      * the same frame being presented to the audio hardware.
61      *
62      * Here's how the calculation works:
63      *
64      * 1) Get the time a particular frame was presented to the audio hardware
65      * @see AudioStream::getTimestamp
66      * 2) From this extrapolate the time which the *next* audio frame written to the stream
67      * will be presented
68      * 3) Assume that the next audio frame is written at the current time
69      * 4) currentLatency = nextFramePresentationTime - nextFrameWriteTime
70      *
71      * @return  Output Latency in Milliseconds
72      */
73     double getCurrentOutputLatencyMillis();
74 
75     bool isLatencyDetectionSupported();
76 
77     bool isAAudioRecommended();
78 
79 private:
80     oboe::Result reopenStream();
81     oboe::Result openPlaybackStream();
82 
83     std::shared_ptr<oboe::AudioStream> mStream;
84     std::shared_ptr<LatencyTuningCallback> mLatencyCallback;
85     std::shared_ptr<DefaultErrorCallback> mErrorCallback;
86     std::shared_ptr<SoundGenerator> mAudioSource;
87     bool mIsLatencyDetectionSupported = false;
88 
89     int32_t        mDeviceId = oboe::Unspecified;
90     int32_t        mChannelCount = oboe::Unspecified;
91     oboe::AudioApi mAudioApi = oboe::AudioApi::Unspecified;
92     std::mutex     mLock;
93 };
94 
95 #endif //OBOE_HELLO_OBOE_ENGINE_H
96