1 /*
2  * Copyright (C) 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 CHRE_PLATFORM_ANDROID_PLATFORM_AUDIO_BASE_H_
18 #define CHRE_PLATFORM_ANDROID_PLATFORM_AUDIO_BASE_H_
19 
20 #include <aaudio/AAudio.h>
21 #include <vector>
22 
23 #include "chre/platform/system_timer.h"
24 #include "chre/util/unique_ptr.h"
25 
26 namespace chre {
27 
28 /**
29  * The base PlatformAudio class for the Android simulator to inject platform
30  * specific functionality from.
31  */
32 class PlatformAudioBase {
33  protected:
34   //! The timer used to defer reading audio samples until they are needed.
35   SystemTimer mTimer;
36 
37   //! The number of samples to read for the current request.
38   uint32_t mNumSamples;
39 
40   //! The amount of time to wait to deliver samples for the current request.
41   Nanoseconds mEventDelay;
42 
43   //! The stream builder used to access Android Audio streams.
44   AAudioStreamBuilder *mStreamBuilder;
45 
46   //! The stream to read from to access audio data.
47   AAudioStream *mStream;
48 
49   //! The minimum buffer duration for Android audio data.
50   int32_t mMinBufferDuration;
51 
52   //! The maximum buffer duration for Android audio data.
53   int32_t mMaxBufferDuration;
54 
55   //! The buffer to read audio data into. This will be resized to the maximum
56   //! buffer size at initialization time.
57   std::vector<int16_t> mBuffer;
58 
59   //! The data event to post to CHRE. Only one can be published at a time so
60   //! it is allocated here to simplify memory management.
61   struct chreAudioDataEvent mDataEvent;
62 
63   /**
64    * Initializes the chreAudioDataEvent owned by this object by setting initial
65    * values for fields that do not change for the duration of the runtime.
66    */
67   void initAudioDataEvent();
68 
69   /**
70    * A callback to invoke when the timer has elapsed for reading audio data.
71    *
72    * @param cookie A pointer to provide context to the implementation of this
73    *        callback. This will be set to this instance of the PlatformAudio
74    *        class.
75    */
76   static void audioReadCallback(void *cookie);
77 };
78 
79 }  // namespace chre
80 
81 #endif  // CHRE_PLATFORM_ANDROID_PLATFORM_AUDIO_BASE_H_
82