• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 ANDROID_HARDWARE_STREAM_HAL_INTERFACE_H
18 #define ANDROID_HARDWARE_STREAM_HAL_INTERFACE_H
19 
20 #include <vector>
21 
22 #include <media/audiohal/EffectHalInterface.h>
23 #include <media/MicrophoneInfo.h>
24 #include <system/audio.h>
25 #include <utils/Errors.h>
26 #include <utils/RefBase.h>
27 #include <utils/String8.h>
28 #include <utils/Vector.h>
29 
30 namespace android {
31 
32 class StreamHalInterface : public virtual RefBase
33 {
34   public:
35     // Return size of input/output buffer in bytes for this stream - eg. 4800.
36     virtual status_t getBufferSize(size_t *size) = 0;
37 
38     // Return the base configuration of the stream:
39     //   - channel mask;
40     //   - format - e.g. AUDIO_FORMAT_PCM_16_BIT;
41     //   - sampling rate in Hz - eg. 44100.
42     virtual status_t getAudioProperties(audio_config_base_t *configBase) = 0;
43 
44     // Convenience method.
getAudioProperties(uint32_t * sampleRate,audio_channel_mask_t * mask,audio_format_t * format)45     inline status_t getAudioProperties(
46             uint32_t *sampleRate, audio_channel_mask_t *mask, audio_format_t *format) {
47         audio_config_base_t config = AUDIO_CONFIG_BASE_INITIALIZER;
48         const status_t result = getAudioProperties(&config);
49         if (result == NO_ERROR) {
50             if (sampleRate != nullptr) *sampleRate = config.sample_rate;
51             if (mask != nullptr) *mask = config.channel_mask;
52             if (format != nullptr) *format = config.format;
53         }
54         return result;
55     }
56 
57     // Set audio stream parameters.
58     virtual status_t setParameters(const String8& kvPairs) = 0;
59 
60     // Get audio stream parameters.
61     virtual status_t getParameters(const String8& keys, String8 *values) = 0;
62 
63     // Return the frame size (number of bytes per sample) of a stream.
64     virtual status_t getFrameSize(size_t *size) = 0;
65 
66     // Add or remove the effect on the stream.
67     virtual status_t addEffect(sp<EffectHalInterface> effect) = 0;
68     virtual status_t removeEffect(sp<EffectHalInterface> effect) = 0;
69 
70     // Put the audio hardware input/output into standby mode.
71     virtual status_t standby() = 0;
72 
73     virtual status_t dump(int fd, const Vector<String16>& args = {}) = 0;
74 
75     // Start a stream operating in mmap mode.
76     virtual status_t start() = 0;
77 
78     // Stop a stream operating in mmap mode.
79     virtual status_t stop() = 0;
80 
81     // Retrieve information on the data buffer in mmap mode.
82     virtual status_t createMmapBuffer(int32_t minSizeFrames,
83                                       struct audio_mmap_buffer_info *info) = 0;
84 
85     // Get current read/write position in the mmap buffer
86     virtual status_t getMmapPosition(struct audio_mmap_position *position) = 0;
87 
88     // Set the priority of the thread that interacts with the HAL
89     // (must match the priority of the audioflinger's thread that calls 'read' / 'write')
90     virtual status_t setHalThreadPriority(int priority) = 0;
91 
92     virtual status_t legacyCreateAudioPatch(const struct audio_port_config& port,
93                                             std::optional<audio_source_t> source,
94                                             audio_devices_t type) = 0;
95 
96     virtual status_t legacyReleaseAudioPatch() = 0;
97 
98   protected:
99     // Subclasses can not be constructed directly by clients.
StreamHalInterface()100     StreamHalInterface() {}
101 
102     // The destructor automatically closes the stream.
~StreamHalInterface()103     virtual ~StreamHalInterface() {}
104 };
105 
106 class StreamOutHalInterfaceCallback : public virtual RefBase {
107   public:
onWriteReady()108     virtual void onWriteReady() {}
onDrainReady()109     virtual void onDrainReady() {}
onError()110     virtual void onError() {}
111 
112   protected:
StreamOutHalInterfaceCallback()113     StreamOutHalInterfaceCallback() {}
~StreamOutHalInterfaceCallback()114     virtual ~StreamOutHalInterfaceCallback() {}
115 };
116 
117 class StreamOutHalInterfaceEventCallback : public virtual RefBase {
118 public:
119     virtual void onCodecFormatChanged(const std::basic_string<uint8_t>& metadataBs) = 0;
120 
121 protected:
StreamOutHalInterfaceEventCallback()122     StreamOutHalInterfaceEventCallback() {}
~StreamOutHalInterfaceEventCallback()123     virtual ~StreamOutHalInterfaceEventCallback() {}
124 };
125 
126 class StreamOutHalInterfaceLatencyModeCallback : public virtual RefBase {
127 public:
128     /**
129      * Called with the new list of supported latency modes when a change occurs.
130      */
131     virtual void onRecommendedLatencyModeChanged(std::vector<audio_latency_mode_t> modes) = 0;
132 
133 protected:
StreamOutHalInterfaceLatencyModeCallback()134     StreamOutHalInterfaceLatencyModeCallback() {}
~StreamOutHalInterfaceLatencyModeCallback()135     virtual ~StreamOutHalInterfaceLatencyModeCallback() {}
136 };
137 
138 class StreamOutHalInterface : public virtual StreamHalInterface {
139   public:
140     // Return the audio hardware driver estimated latency in milliseconds.
141     virtual status_t getLatency(uint32_t *latency) = 0;
142 
143     // Use this method in situations where audio mixing is done in the hardware.
144     virtual status_t setVolume(float left, float right) = 0;
145 
146     // Selects the audio presentation (if available).
147     virtual status_t selectPresentation(int presentationId, int programId) = 0;
148 
149     // Write audio buffer to driver.
150     virtual status_t write(const void *buffer, size_t bytes, size_t *written) = 0;
151 
152     // Return the number of audio frames written by the audio dsp to DAC since
153     // the output has exited standby.
154     virtual status_t getRenderPosition(uint32_t *dspFrames) = 0;
155 
156     // Get the local time at which the next write to the audio driver will be presented.
157     virtual status_t getNextWriteTimestamp(int64_t *timestamp) = 0;
158 
159     // Set the callback for notifying completion of non-blocking write and drain.
160     // The callback must be owned by someone else. The output stream does not own it
161     // to avoid strong pointer loops.
162     virtual status_t setCallback(wp<StreamOutHalInterfaceCallback> callback) = 0;
163 
164     // Returns whether pause and resume operations are supported.
165     virtual status_t supportsPauseAndResume(bool *supportsPause, bool *supportsResume) = 0;
166 
167     // Notifies to the audio driver to resume playback following a pause.
168     virtual status_t pause() = 0;
169 
170     // Notifies to the audio driver to resume playback following a pause.
171     virtual status_t resume() = 0;
172 
173     // Returns whether drain operation is supported.
174     virtual status_t supportsDrain(bool *supportsDrain) = 0;
175 
176     // Requests notification when data buffered by the driver/hardware has been played.
177     virtual status_t drain(bool earlyNotify) = 0;
178 
179     // Notifies to the audio driver to flush the queued data.
180     virtual status_t flush() = 0;
181 
182     // Return a recent count of the number of audio frames presented to an external observer.
183     virtual status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp) = 0;
184 
185     struct SourceMetadata {
186         std::vector<playback_track_metadata_v7_t> tracks;
187     };
188 
189     /**
190      * Called when the metadata of the stream's source has been changed.
191      * @param sourceMetadata Description of the audio that is played by the clients.
192      */
193     virtual status_t updateSourceMetadata(const SourceMetadata& sourceMetadata) = 0;
194 
195     // Returns the Dual Mono mode presentation setting.
196     virtual status_t getDualMonoMode(audio_dual_mono_mode_t* mode) = 0;
197 
198     // Sets the Dual Mono mode presentation on the output device.
199     virtual status_t setDualMonoMode(audio_dual_mono_mode_t mode) = 0;
200 
201     // Returns the Audio Description Mix level in dB.
202     virtual status_t getAudioDescriptionMixLevel(float* leveldB) = 0;
203 
204     // Sets the Audio Description Mix level in dB.
205     virtual status_t setAudioDescriptionMixLevel(float leveldB) = 0;
206 
207     // Retrieves current playback rate parameters.
208     virtual status_t getPlaybackRateParameters(audio_playback_rate_t* playbackRate) = 0;
209 
210     // Sets the playback rate parameters that control playback behavior.
211     virtual status_t setPlaybackRateParameters(const audio_playback_rate_t& playbackRate) = 0;
212 
213     virtual status_t setEventCallback(const sp<StreamOutHalInterfaceEventCallback>& callback) = 0;
214 
215     /**
216      * Indicates the requested latency mode for this output stream.
217      *
218      * The requested mode can be one of the modes returned by
219      * getRecommendedLatencyModes() API.
220      *
221      * @param mode the requested latency mode.
222      * @return operation completion status.
223      */
224     virtual status_t setLatencyMode(audio_latency_mode_t mode) = 0;
225 
226     /**
227      * Indicates which latency modes are currently supported on this output stream.
228      * If the transport protocol (e.g Bluetooth A2DP) used by this output stream to reach
229      * the output device supports variable latency modes, the HAL indicates which
230      * modes are currently supported.
231      * The framework can then call setLatencyMode() with one of the supported modes to select
232      * the desired operation mode.
233      *
234      * @param modes currrently supported latency modes.
235      * @return operation completion status.
236      */
237     virtual status_t getRecommendedLatencyModes(std::vector<audio_latency_mode_t> *modes) = 0;
238 
239     /**
240      * Set the callback interface for notifying changes in supported latency modes.
241      *
242      * Calling this method with a null pointer will result in releasing
243      * the callback.
244      *
245      * @param callback the registered callback or null to unregister.
246      * @return operation completion status.
247      */
248     virtual status_t setLatencyModeCallback(
249             const sp<StreamOutHalInterfaceLatencyModeCallback>& callback) = 0;
250 
251     /**
252      * Signal the end of audio output, interrupting an ongoing 'write' operation.
253      */
254     virtual status_t exit() = 0;
255 
256   protected:
~StreamOutHalInterface()257     virtual ~StreamOutHalInterface() {}
258 };
259 
260 class StreamInHalInterface : public virtual StreamHalInterface {
261   public:
262     // Set the input gain for the audio driver.
263     virtual status_t setGain(float gain) = 0;
264 
265     // Read audio buffer in from driver.
266     virtual status_t read(void *buffer, size_t bytes, size_t *read) = 0;
267 
268     // Return the amount of input frames lost in the audio driver.
269     virtual status_t getInputFramesLost(uint32_t *framesLost) = 0;
270 
271     // Return a recent count of the number of audio frames received and
272     // the clock time associated with that frame count.
273     virtual status_t getCapturePosition(int64_t *frames, int64_t *time) = 0;
274 
275     // Get active microphones
276     virtual status_t getActiveMicrophones(std::vector<media::MicrophoneInfo> *microphones) = 0;
277 
278     // Set direction for capture processing
279     virtual status_t setPreferredMicrophoneDirection(audio_microphone_direction_t) = 0;
280 
281     // Set zoom factor for capture stream
282     virtual status_t setPreferredMicrophoneFieldDimension(float zoom) = 0;
283 
284     struct SinkMetadata {
285         std::vector<record_track_metadata_v7_t> tracks;
286     };
287     /**
288      * Called when the metadata of the stream's sink has been changed.
289      * @param sinkMetadata Description of the audio that is suggested by the clients.
290      */
291     virtual status_t updateSinkMetadata(const SinkMetadata& sinkMetadata) = 0;
292 
293   protected:
~StreamInHalInterface()294     virtual ~StreamInHalInterface() {}
295 };
296 
297 } // namespace android
298 
299 #endif // ANDROID_HARDWARE_STREAM_HAL_INTERFACE_H
300