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