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