blob: 928e0fbd33d35f4f47e9c0e8f1526c9b85dca30d [file] [log] [blame]
Don Turner3bf32ae2017-11-27 13:25:05 +00001/*
2 * Copyright 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
17#ifndef OBOE_STREAM_AAUDIO_H_
18#define OBOE_STREAM_AAUDIO_H_
19
20#include <atomic>
21#include <mutex>
22#include <thread>
23
24#include "aaudio/AAudio.h"
25
Don Turner379e8e52017-11-29 15:49:29 +000026#include "oboe/AudioStreamBuilder.h"
27#include "oboe/AudioStream.h"
Don Turner3bf32ae2017-11-27 13:25:05 +000028#include "oboe/Definitions.h"
29
30namespace oboe {
31
32class AAudioLoader;
33
34/**
35 * Implementation of OboeStream that uses AAudio.
36 *
37 * Do not create this class directly.
38 * Use an OboeStreamBuilder to create one.
39 */
Don Turner379e8e52017-11-29 15:49:29 +000040class AudioStreamAAudio : public AudioStream {
Don Turner3bf32ae2017-11-27 13:25:05 +000041public:
Don Turner379e8e52017-11-29 15:49:29 +000042 AudioStreamAAudio();
43 explicit AudioStreamAAudio(const AudioStreamBuilder &builder);
Don Turner3bf32ae2017-11-27 13:25:05 +000044
Don Turner379e8e52017-11-29 15:49:29 +000045 ~AudioStreamAAudio();
Don Turner3bf32ae2017-11-27 13:25:05 +000046
47 /**
48 *
49 * @return true if AAudio is supported on this device.
50 */
51 static bool isSupported();
52
Don Turner379e8e52017-11-29 15:49:29 +000053 // These functions override methods in AudioStream.
54 // See AudioStream for documentation.
Don Turner3bf32ae2017-11-27 13:25:05 +000055 Result open() override;
56 Result close() override;
57
58 Result requestStart() override;
59 Result requestPause() override;
60 Result requestFlush() override;
61 Result requestStop() override;
62
Phil Burk4cf25f12018-02-14 08:05:01 -080063 ErrorOrValue<int32_t> write(const void *buffer,
Phil Burk34217a72018-02-07 20:37:17 -080064 int32_t numFrames,
65 int64_t timeoutNanoseconds) override;
66
Phil Burk4cf25f12018-02-14 08:05:01 -080067 ErrorOrValue<int32_t> read(void *buffer,
Phil Burk34217a72018-02-07 20:37:17 -080068 int32_t numFrames,
69 int64_t timeoutNanoseconds) override;
Don Turner3bf32ae2017-11-27 13:25:05 +000070
71 Result setBufferSizeInFrames(int32_t requestedFrames) override;
72 int32_t getBufferSizeInFrames() const override;
73 int32_t getFramesPerBurst() override;
Phil Burka5d66372018-02-09 14:38:23 -080074 int32_t getXRunCount() const override;
Don Turner3bf32ae2017-11-27 13:25:05 +000075
Phil Burk34217a72018-02-07 20:37:17 -080076 int64_t getFramesRead() const override;
77 int64_t getFramesWritten() const override;
Don Turner3bf32ae2017-11-27 13:25:05 +000078
Phil Burkc07dc622018-04-29 13:41:19 -070079 ErrorOrValue<double> calculateLatencyMillis() override;
80
Don Turner3bf32ae2017-11-27 13:25:05 +000081 Result waitForStateChange(StreamState currentState,
82 StreamState *nextState,
83 int64_t timeoutNanoseconds) override;
84
85 Result getTimestamp(clockid_t clockId,
86 int64_t *framePosition,
87 int64_t *timeNanoseconds) override;
88
89 StreamState getState() override;
90
Phil Burkc668c412018-02-15 17:48:18 -080091 AudioApi getAudioApi() const override {
92 return AudioApi::AAudio;
Don Turner3bf32ae2017-11-27 13:25:05 +000093 }
94
Don Turner3bf32ae2017-11-27 13:25:05 +000095 DataCallbackResult callOnAudioReady(AAudioStream *stream,
96 void *audioData,
97 int32_t numFrames);
98
99 void onErrorCallback(AAudioStream *stream, Result error);
100
101 void onErrorInThread(AAudioStream *stream, Result error);
102
Phil Burka22f3012018-03-23 17:32:57 -0700103
104 void *getUnderlyingStream() const override {
105 return mAAudioStream.load();
106 }
107
Don Turner3bf32ae2017-11-27 13:25:05 +0000108protected:
109 Result convertApplicationDataToNative(int32_t numFrames); // TODO remove?
110
111private:
112
113 float *mFloatCallbackBuffer;
114 int16_t *mShortCallbackBuffer;
115 std::atomic<bool> mCallbackThreadEnabled;
116 std::thread *mErrorHandlingThread = nullptr;
117
118 std::mutex mLock; // for synchronizing start/stop/close
119 std::atomic<AAudioStream *> mAAudioStream{nullptr};
120
121 static AAudioLoader *mLibLoader;
122};
123
124} // namespace oboe
125
126#endif // OBOE_STREAM_AAUDIO_H_