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_PLATFORM_AUDIO_H_ 18 #define CHRE_PLATFORM_PLATFORM_AUDIO_H_ 19 20 #include "chre_api/chre/audio.h" 21 #include "chre/target_platform/platform_audio_base.h" 22 #include "chre/util/non_copyable.h" 23 #include "chre/util/time.h" 24 25 namespace chre { 26 27 /** 28 * Defines the common interface to audio functionality. 29 */ 30 class PlatformAudio : public PlatformAudioBase, 31 public NonCopyable { 32 public: 33 /** 34 * Initializes the audio subsystem. This is invoked as part of the 35 * construction of the EventLoopManager. 36 */ 37 PlatformAudio(); 38 39 /** 40 * Deinitializes the audio subsystem. This is invoked as part of the 41 * destruction of the EventLoopManager. 42 */ 43 ~PlatformAudio(); 44 45 /** 46 * Initializes the platform-specific audio implementation. This is potentially 47 * called at a later stage of initialization than the constructor to allow the 48 * rest of CHRE to initialize. This permits use of deferCallback. This method 49 * must be invoked before methods of this class can be invoked. 50 */ 51 void init(); 52 53 /* 54 * Allows the CHRE common code to notify the platform that the enabled state 55 * of a given audio handle has changed. This will only be invoked with true 56 * when the number of clients for the handle is greater than zero or false 57 * when it is equal to zero. 58 * 59 * @param handle The handle for which audio enabled state is changing. 60 * @param enabled true if an active request is open for this handle, false 61 * otherwise. 62 */ 63 void setHandleEnabled(uint32_t handle, bool enabled); 64 65 /** 66 * Requests an audio data event from the platform for the provided handle. A 67 * call to this method must cancel any previous request. 68 * 69 * The event requested here may contain data from previously posted events. 70 * The concept is to allow the platform to manage its own buffers for audio 71 * data. If a request comes in for 8000 samples of data and the most recent 72 * request was for 4000 samples of data, the platform implementation may reuse 73 * the existing 4000 samples of data and append 4000 samples of new data 74 * (assuming that the arguments passed here allow that). 75 * 76 * Once a request for a given source has been made, the platform 77 * implementation must maintain a buffer of previously collected audio samples 78 * to provide when a request comes in for data in the past (up to the maximum 79 * buffer size for this source). This happens when numSamples at the source 80 * sample rate is a greater amount of time than eventDelay. This buffer can be 81 * released once cancelAudioDataEventRequest has been invoked for a given 82 * source. 83 * 84 * The event is provided to CHRE through the handleAudioDataEvent function of 85 * the AudioRequestManager. 86 * 87 * @param handle The handle for which an audio event is requested. 88 * @param numSamples The number of samples to send once the request has been 89 * completed. 90 * @param eventDelay The amount of time that must pass before providing the 91 * data event to CHRE. 92 */ 93 bool requestAudioDataEvent(uint32_t handle, 94 uint32_t numSamples, 95 Nanoseconds eventDelay); 96 97 /** 98 * Cancels the previous call to requestAudioDataEvent. No audio data is 99 * allowed to be posted to CHRE after this function has been called and before 100 * the next call to requestAudioDataEvent. 101 * 102 * @param handle The handle for which the most recent call to 103 * requestAudioDataEvent will be cancelled. 104 */ 105 void cancelAudioDataEventRequest(uint32_t handle); 106 107 /** 108 * Releases a previously posted audio event. This will be invoked by CHRE to 109 * say that all nanoapps have processed the previously scheduled data event. 110 * 111 * @param event An audio data event that was previously provided to 112 * CHRE as a result of a request for audio data. 113 */ 114 void releaseAudioDataEvent(struct chreAudioDataEvent *event); 115 116 /** 117 * @return the number of sources supported by the implementation. The returned 118 * value must be exactly one greater than the maximum supported audio handle. 119 */ 120 size_t getSourceCount(); 121 122 /** 123 * Obtains the audio source description for a given handle. 124 * 125 * @param handle the handle for the requested audio source. 126 * @param audioSource the chreAudioSource to populate with details of the 127 * audio source. This pointer must never be null. 128 */ 129 bool getAudioSource(uint32_t handle, chreAudioSource *audioSource) const; 130 }; 131 132 } // namespace chre 133 134 #endif // CHRE_PLATFORM_PLATFORM_AUDIO_H_ 135