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