blob: 00939573a80cc1a35fd27a4c644cd6032d84c868 [file] [log] [blame]
Mikhail Naganovf558e022016-11-14 17:45:17 -08001/*
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_HIDL_H
18#define ANDROID_HARDWARE_STREAM_HAL_HIDL_H
19
20#include <android/hardware/audio/2.0/IStream.h>
21#include <android/hardware/audio/2.0/IStreamIn.h>
22#include <android/hardware/audio/2.0/IStreamOut.h>
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -080023#include <fmq/EventFlag.h>
24#include <fmq/MessageQueue.h>
Mikhail Naganovf558e022016-11-14 17:45:17 -080025#include <media/audiohal/StreamHalInterface.h>
26
27#include "ConversionHelperHidl.h"
28
29using ::android::hardware::audio::V2_0::IStream;
30using ::android::hardware::audio::V2_0::IStreamIn;
31using ::android::hardware::audio::V2_0::IStreamOut;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -080032using ::android::hardware::EventFlag;
33using ::android::hardware::MessageQueue;
Mikhail Naganovf558e022016-11-14 17:45:17 -080034using ::android::hardware::Return;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -080035using ReadStatus = ::android::hardware::audio::V2_0::IStreamIn::ReadStatus;
36using WriteStatus = ::android::hardware::audio::V2_0::IStreamOut::WriteStatus;
Mikhail Naganovf558e022016-11-14 17:45:17 -080037
38namespace android {
39
40class DeviceHalHidl;
41
42class StreamHalHidl : public virtual StreamHalInterface, public ConversionHelperHidl
43{
44 public:
45 // Return the sampling rate in Hz - eg. 44100.
46 virtual status_t getSampleRate(uint32_t *rate);
47
48 // Return size of input/output buffer in bytes for this stream - eg. 4800.
49 virtual status_t getBufferSize(size_t *size);
50
51 // Return the channel mask.
52 virtual status_t getChannelMask(audio_channel_mask_t *mask);
53
54 // Return the audio format - e.g. AUDIO_FORMAT_PCM_16_BIT.
55 virtual status_t getFormat(audio_format_t *format);
56
57 // Convenience method.
58 virtual status_t getAudioProperties(
59 uint32_t *sampleRate, audio_channel_mask_t *mask, audio_format_t *format);
60
61 // Set audio stream parameters.
62 virtual status_t setParameters(const String8& kvPairs);
63
64 // Get audio stream parameters.
65 virtual status_t getParameters(const String8& keys, String8 *values);
66
67 // Add or remove the effect on the stream.
68 virtual status_t addEffect(sp<EffectHalInterface> effect);
69 virtual status_t removeEffect(sp<EffectHalInterface> effect);
70
71 // Put the audio hardware input/output into standby mode.
72 virtual status_t standby();
73
74 virtual status_t dump(int fd);
75
Eric Laurentaf35aad2016-12-15 14:25:36 -080076 // Start a stream operating in mmap mode.
77 virtual status_t start();
78
79 // Stop a stream operating in mmap mode.
80 virtual status_t stop();
81
82 // Retrieve information on the data buffer in mmap mode.
83 virtual status_t createMmapBuffer(int32_t minSizeFrames,
84 struct audio_mmap_buffer_info *info);
85
86 // Get current read/write position in the mmap buffer
87 virtual status_t getMmapPosition(struct audio_mmap_position *position);
88
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -080089 // Set the priority of the thread that interacts with the HAL
90 // (must match the priority of the audioflinger's thread that calls 'read' / 'write')
91 virtual status_t setHalThreadPriority(int priority);
92
Mikhail Naganovf558e022016-11-14 17:45:17 -080093 protected:
94 // Subclasses can not be constructed directly by clients.
95 explicit StreamHalHidl(IStream *stream);
96
97 // The destructor automatically closes the stream.
98 virtual ~StreamHalHidl();
99
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800100 int mHalThreadPriority;
101
Mikhail Naganovf558e022016-11-14 17:45:17 -0800102 private:
103 IStream *mStream;
104};
105
106class StreamOutHalHidl : public StreamOutHalInterface, public StreamHalHidl {
107 public:
108 // Return the frame size (number of bytes per sample) of a stream.
109 virtual status_t getFrameSize(size_t *size);
110
111 // Return the audio hardware driver estimated latency in milliseconds.
112 virtual status_t getLatency(uint32_t *latency);
113
114 // Use this method in situations where audio mixing is done in the hardware.
115 virtual status_t setVolume(float left, float right);
116
117 // Write audio buffer to driver.
118 virtual status_t write(const void *buffer, size_t bytes, size_t *written);
119
120 // Return the number of audio frames written by the audio dsp to DAC since
121 // the output has exited standby.
122 virtual status_t getRenderPosition(uint32_t *dspFrames);
123
124 // Get the local time at which the next write to the audio driver will be presented.
125 virtual status_t getNextWriteTimestamp(int64_t *timestamp);
126
127 // Set the callback for notifying completion of non-blocking write and drain.
128 virtual status_t setCallback(wp<StreamOutHalInterfaceCallback> callback);
129
130 // Returns whether pause and resume operations are supported.
131 virtual status_t supportsPauseAndResume(bool *supportsPause, bool *supportsResume);
132
133 // Notifies to the audio driver to resume playback following a pause.
134 virtual status_t pause();
135
136 // Notifies to the audio driver to resume playback following a pause.
137 virtual status_t resume();
138
139 // Returns whether drain operation is supported.
140 virtual status_t supportsDrain(bool *supportsDrain);
141
142 // Requests notification when data buffered by the driver/hardware has been played.
143 virtual status_t drain(bool earlyNotify);
144
145 // Notifies to the audio driver to flush the queued data.
146 virtual status_t flush();
147
148 // Return a recent count of the number of audio frames presented to an external observer.
149 virtual status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp);
150
151 // Methods used by StreamOutCallback (HIDL).
152 void onWriteReady();
153 void onDrainReady();
154 void onError();
155
156 private:
157 friend class DeviceHalHidl;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800158 typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ;
159 typedef MessageQueue<WriteStatus, hardware::kSynchronizedReadWrite> StatusMQ;
Mikhail Naganovf558e022016-11-14 17:45:17 -0800160
161 wp<StreamOutHalInterfaceCallback> mCallback;
162 sp<IStreamOut> mStream;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800163 std::unique_ptr<DataMQ> mDataMQ;
164 std::unique_ptr<StatusMQ> mStatusMQ;
165 EventFlag* mEfGroup;
166 bool mGetPresentationPositionNotSupported;
167 uint64_t mPPosFromWriteObtained;
168 uint64_t mPPosFromWriteFrames;
169 struct timespec mPPosFromWriteTS;
Mikhail Naganovf558e022016-11-14 17:45:17 -0800170
171 // Can not be constructed directly by clients.
172 StreamOutHalHidl(const sp<IStreamOut>& stream);
173
174 virtual ~StreamOutHalHidl();
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800175
176 status_t prepareForWriting(size_t bufferSize);
Mikhail Naganovf558e022016-11-14 17:45:17 -0800177};
178
179class StreamInHalHidl : public StreamInHalInterface, public StreamHalHidl {
180 public:
181 // Return the frame size (number of bytes per sample) of a stream.
182 virtual status_t getFrameSize(size_t *size);
183
184 // Set the input gain for the audio driver.
185 virtual status_t setGain(float gain);
186
187 // Read audio buffer in from driver.
188 virtual status_t read(void *buffer, size_t bytes, size_t *read);
189
190 // Return the amount of input frames lost in the audio driver.
191 virtual status_t getInputFramesLost(uint32_t *framesLost);
192
193 // Return a recent count of the number of audio frames received and
194 // the clock time associated with that frame count.
195 virtual status_t getCapturePosition(int64_t *frames, int64_t *time);
196
197 private:
198 friend class DeviceHalHidl;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800199 typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ;
200 typedef MessageQueue<ReadStatus, hardware::kSynchronizedReadWrite> StatusMQ;
Mikhail Naganovf558e022016-11-14 17:45:17 -0800201
202 sp<IStreamIn> mStream;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800203 std::unique_ptr<DataMQ> mDataMQ;
204 std::unique_ptr<StatusMQ> mStatusMQ;
205 EventFlag* mEfGroup;
Mikhail Naganovf558e022016-11-14 17:45:17 -0800206
207 // Can not be constructed directly by clients.
208 StreamInHalHidl(const sp<IStreamIn>& stream);
209
210 virtual ~StreamInHalHidl();
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800211
212 status_t prepareForReading(size_t bufferSize);
Mikhail Naganovf558e022016-11-14 17:45:17 -0800213};
214
215} // namespace android
216
217#endif // ANDROID_HARDWARE_STREAM_HAL_HIDL_H