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