• 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 
29 namespace android {
30 
31 class StreamHalInterface : public virtual RefBase
32 {
33   public:
34     // Return size of input/output buffer in bytes for this stream - eg. 4800.
35     virtual status_t getBufferSize(size_t *size) = 0;
36 
37     // Return the base configuration of the stream:
38     //   - channel mask;
39     //   - format - e.g. AUDIO_FORMAT_PCM_16_BIT;
40     //   - sampling rate in Hz - eg. 44100.
41     virtual status_t getAudioProperties(audio_config_base_t *configBase) = 0;
42 
43     // Convenience method.
getAudioProperties(uint32_t * sampleRate,audio_channel_mask_t * mask,audio_format_t * format)44     inline status_t getAudioProperties(
45             uint32_t *sampleRate, audio_channel_mask_t *mask, audio_format_t *format) {
46         audio_config_base_t config = AUDIO_CONFIG_BASE_INITIALIZER;
47         const status_t result = getAudioProperties(&config);
48         if (result == NO_ERROR) {
49             if (sampleRate != nullptr) *sampleRate = config.sample_rate;
50             if (mask != nullptr) *mask = config.channel_mask;
51             if (format != nullptr) *format = config.format;
52         }
53         return result;
54     }
55 
56     // Set audio stream parameters.
57     virtual status_t setParameters(const String8& kvPairs) = 0;
58 
59     // Get audio stream parameters.
60     virtual status_t getParameters(const String8& keys, String8 *values) = 0;
61 
62     // Return the frame size (number of bytes per sample) of a stream.
63     virtual status_t getFrameSize(size_t *size) = 0;
64 
65     // Add or remove the effect on the stream.
66     virtual status_t addEffect(sp<EffectHalInterface> effect) = 0;
67     virtual status_t removeEffect(sp<EffectHalInterface> effect) = 0;
68 
69     // Put the audio hardware input/output into standby mode.
70     virtual status_t standby() = 0;
71 
72     virtual status_t dump(int fd) = 0;
73 
74     // Start a stream operating in mmap mode.
75     virtual status_t start() = 0;
76 
77     // Stop a stream operating in mmap mode.
78     virtual status_t stop() = 0;
79 
80     // Retrieve information on the data buffer in mmap mode.
81     virtual status_t createMmapBuffer(int32_t minSizeFrames,
82                                       struct audio_mmap_buffer_info *info) = 0;
83 
84     // Get current read/write position in the mmap buffer
85     virtual status_t getMmapPosition(struct audio_mmap_position *position) = 0;
86 
87     // Set the priority of the thread that interacts with the HAL
88     // (must match the priority of the audioflinger's thread that calls 'read' / 'write')
89     virtual status_t setHalThreadPriority(int priority) = 0;
90 
91   protected:
92     // Subclasses can not be constructed directly by clients.
StreamHalInterface()93     StreamHalInterface() {}
94 
95     // The destructor automatically closes the stream.
~StreamHalInterface()96     virtual ~StreamHalInterface() {}
97 };
98 
99 class StreamOutHalInterfaceCallback : public virtual RefBase {
100   public:
onWriteReady()101     virtual void onWriteReady() {}
onDrainReady()102     virtual void onDrainReady() {}
onError()103     virtual void onError() {}
104 
105   protected:
StreamOutHalInterfaceCallback()106     StreamOutHalInterfaceCallback() {}
~StreamOutHalInterfaceCallback()107     virtual ~StreamOutHalInterfaceCallback() {}
108 };
109 
110 class StreamOutHalInterfaceEventCallback : public virtual RefBase {
111 public:
112     virtual void onCodecFormatChanged(const std::basic_string<uint8_t>& metadataBs) = 0;
113 
114 protected:
StreamOutHalInterfaceEventCallback()115     StreamOutHalInterfaceEventCallback() {}
~StreamOutHalInterfaceEventCallback()116     virtual ~StreamOutHalInterfaceEventCallback() {}
117 };
118 
119 class StreamOutHalInterface : public virtual StreamHalInterface {
120   public:
121     // Return the audio hardware driver estimated latency in milliseconds.
122     virtual status_t getLatency(uint32_t *latency) = 0;
123 
124     // Use this method in situations where audio mixing is done in the hardware.
125     virtual status_t setVolume(float left, float right) = 0;
126 
127     // Selects the audio presentation (if available).
128     virtual status_t selectPresentation(int presentationId, int programId) = 0;
129 
130     // Write audio buffer to driver.
131     virtual status_t write(const void *buffer, size_t bytes, size_t *written) = 0;
132 
133     // Return the number of audio frames written by the audio dsp to DAC since
134     // the output has exited standby.
135     virtual status_t getRenderPosition(uint32_t *dspFrames) = 0;
136 
137     // Get the local time at which the next write to the audio driver will be presented.
138     virtual status_t getNextWriteTimestamp(int64_t *timestamp) = 0;
139 
140     // Set the callback for notifying completion of non-blocking write and drain.
141     // The callback must be owned by someone else. The output stream does not own it
142     // to avoid strong pointer loops.
143     virtual status_t setCallback(wp<StreamOutHalInterfaceCallback> callback) = 0;
144 
145     // Returns whether pause and resume operations are supported.
146     virtual status_t supportsPauseAndResume(bool *supportsPause, bool *supportsResume) = 0;
147 
148     // Notifies to the audio driver to resume playback following a pause.
149     virtual status_t pause() = 0;
150 
151     // Notifies to the audio driver to resume playback following a pause.
152     virtual status_t resume() = 0;
153 
154     // Returns whether drain operation is supported.
155     virtual status_t supportsDrain(bool *supportsDrain) = 0;
156 
157     // Requests notification when data buffered by the driver/hardware has been played.
158     virtual status_t drain(bool earlyNotify) = 0;
159 
160     // Notifies to the audio driver to flush the queued data.
161     virtual status_t flush() = 0;
162 
163     // Return a recent count of the number of audio frames presented to an external observer.
164     virtual status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp) = 0;
165 
166     struct SourceMetadata {
167         std::vector<playback_track_metadata_v7_t> tracks;
168     };
169 
170     /**
171      * Called when the metadata of the stream's source has been changed.
172      * @param sourceMetadata Description of the audio that is played by the clients.
173      */
174     virtual status_t updateSourceMetadata(const SourceMetadata& sourceMetadata) = 0;
175 
176     // Returns the Dual Mono mode presentation setting.
177     virtual status_t getDualMonoMode(audio_dual_mono_mode_t* mode) = 0;
178 
179     // Sets the Dual Mono mode presentation on the output device.
180     virtual status_t setDualMonoMode(audio_dual_mono_mode_t mode) = 0;
181 
182     // Returns the Audio Description Mix level in dB.
183     virtual status_t getAudioDescriptionMixLevel(float* leveldB) = 0;
184 
185     // Sets the Audio Description Mix level in dB.
186     virtual status_t setAudioDescriptionMixLevel(float leveldB) = 0;
187 
188     // Retrieves current playback rate parameters.
189     virtual status_t getPlaybackRateParameters(audio_playback_rate_t* playbackRate) = 0;
190 
191     // Sets the playback rate parameters that control playback behavior.
192     virtual status_t setPlaybackRateParameters(const audio_playback_rate_t& playbackRate) = 0;
193 
194     virtual status_t setEventCallback(const sp<StreamOutHalInterfaceEventCallback>& callback) = 0;
195 
196   protected:
~StreamOutHalInterface()197     virtual ~StreamOutHalInterface() {}
198 };
199 
200 class StreamInHalInterface : public virtual StreamHalInterface {
201   public:
202     // Set the input gain for the audio driver.
203     virtual status_t setGain(float gain) = 0;
204 
205     // Read audio buffer in from driver.
206     virtual status_t read(void *buffer, size_t bytes, size_t *read) = 0;
207 
208     // Return the amount of input frames lost in the audio driver.
209     virtual status_t getInputFramesLost(uint32_t *framesLost) = 0;
210 
211     // Return a recent count of the number of audio frames received and
212     // the clock time associated with that frame count.
213     virtual status_t getCapturePosition(int64_t *frames, int64_t *time) = 0;
214 
215     // Get active microphones
216     virtual status_t getActiveMicrophones(std::vector<media::MicrophoneInfo> *microphones) = 0;
217 
218     // Set direction for capture processing
219     virtual status_t setPreferredMicrophoneDirection(audio_microphone_direction_t) = 0;
220 
221     // Set zoom factor for capture stream
222     virtual status_t setPreferredMicrophoneFieldDimension(float zoom) = 0;
223 
224     struct SinkMetadata {
225         std::vector<record_track_metadata_v7_t> tracks;
226     };
227     /**
228      * Called when the metadata of the stream's sink has been changed.
229      * @param sinkMetadata Description of the audio that is suggested by the clients.
230      */
231     virtual status_t updateSinkMetadata(const SinkMetadata& sinkMetadata) = 0;
232 
233   protected:
~StreamInHalInterface()234     virtual ~StreamInHalInterface() {}
235 };
236 
237 } // namespace android
238 
239 #endif // ANDROID_HARDWARE_STREAM_HAL_INTERFACE_H
240