• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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_AUDIO_STREAM_OPENSL_ES_H_
18 #define OBOE_AUDIO_STREAM_OPENSL_ES_H_
19 
20 #include <memory>
21 
22 #include <SLES/OpenSLES.h>
23 #include <SLES/OpenSLES_Android.h>
24 
25 #include "oboe/Oboe.h"
26 #include "common/MonotonicCounter.h"
27 #include "opensles/AudioStreamBuffered.h"
28 #include "opensles/EngineOpenSLES.h"
29 
30 namespace oboe {
31 
32 constexpr int kBitsPerByte = 8;
33 constexpr int kBufferQueueLength = 2; // double buffered for callbacks
34 
35 /**
36  * INTERNAL USE ONLY
37  *
38  * A stream that wraps OpenSL ES.
39  *
40  * Do not instantiate this class directly.
41  * Use an OboeStreamBuilder to create one.
42  */
43 
44 class AudioStreamOpenSLES : public AudioStreamBuffered {
45 public:
46 
47     AudioStreamOpenSLES();
48     explicit AudioStreamOpenSLES(const AudioStreamBuilder &builder);
49 
50     virtual ~AudioStreamOpenSLES() = default;
51 
52     virtual Result open() override;
53     virtual Result close() override;
54 
55     /**
56      * Query the current state, eg. OBOE_STREAM_STATE_PAUSING
57      *
58      * @return state or a negative error.
59      */
getState()60     StreamState getState() const override { return mState.load(); }
61 
62     int32_t getFramesPerBurst() override;
63 
64 
getAudioApi()65     AudioApi getAudioApi() const override {
66         return AudioApi::OpenSLES;
67     }
68 
69     /**
70      * Process next OpenSL ES buffer.
71      * Called by by OpenSL ES framework.
72      *
73      * This is public, but don't call it directly.
74      */
75     void processBufferCallback(SLAndroidSimpleBufferQueueItf bq);
76 
77     Result waitForStateChange(StreamState currentState,
78                               StreamState *nextState,
79                               int64_t timeoutNanoseconds) override;
80 
81 protected:
82 
83     SLuint32 channelCountToChannelMaskDefault(int channelCount) const;
84 
onBeforeDestroy()85     virtual Result onBeforeDestroy() { return Result::OK; }
onAfterDestroy()86     virtual Result onAfterDestroy() { return Result::OK; }
87 
88     static SLuint32 getDefaultByteOrder();
89 
90     SLresult registerBufferQueueCallback();
91 
92     int32_t getBufferDepth(SLAndroidSimpleBufferQueueItf bq);
93 
94     SLresult enqueueCallbackBuffer(SLAndroidSimpleBufferQueueItf bq);
95 
96     SLresult configurePerformanceMode(SLAndroidConfigurationItf configItf);
97 
98     SLresult updateStreamParameters(SLAndroidConfigurationItf configItf);
99 
100     PerformanceMode convertPerformanceMode(SLuint32 openslMode) const;
101     SLuint32 convertPerformanceMode(PerformanceMode oboeMode) const;
102 
103     Result configureBufferSizes(int32_t sampleRate);
104 
105     void logUnsupportedAttributes();
106 
107     /**
108      * Internal use only.
109      * Use this instead of directly setting the internal state variable.
110      */
setState(StreamState state)111     void setState(StreamState state) {
112         mState.store(state);
113     }
114 
115     int64_t getFramesProcessedByServer();
116 
117     // OpenSLES stuff
118     SLObjectItf                   mObjectInterface = nullptr;
119     SLAndroidSimpleBufferQueueItf mSimpleBufferQueueInterface = nullptr;
120 
121     int32_t                       mBytesPerCallback = oboe::kUnspecified;
122     MonotonicCounter              mPositionMillis; // for tracking OpenSL ES service position
123 
124 private:
125     std::unique_ptr<uint8_t[]>    mCallbackBuffer;
126     std::atomic<StreamState>      mState{StreamState::Uninitialized};
127 
128 };
129 
130 } // namespace oboe
131 
132 #endif // OBOE_AUDIO_STREAM_OPENSL_ES_H_
133