• 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 PATH(android/hardware/audio/FILE_VERSION/IStream.h)
23 #include PATH(android/hardware/audio/FILE_VERSION/IStreamIn.h)
24 #include PATH(android/hardware/audio/FILE_VERSION/IStreamOut.h)
25 #include <fmq/EventFlag.h>
26 #include <fmq/MessageQueue.h>
27 #include <media/audiohal/StreamHalInterface.h>
28 #include <mediautils/Synchronization.h>
29 
30 #include "ConversionHelperHidl.h"
31 #include "StreamPowerLog.h"
32 
33 using ::android::hardware::audio::CPP_VERSION::IStream;
34 using ::android::hardware::audio::CPP_VERSION::IStreamIn;
35 using ::android::hardware::audio::CPP_VERSION::IStreamOut;
36 using ::android::hardware::EventFlag;
37 using ::android::hardware::MessageQueue;
38 using ::android::hardware::Return;
39 using ReadParameters = ::android::hardware::audio::CPP_VERSION::IStreamIn::ReadParameters;
40 using ReadStatus = ::android::hardware::audio::CPP_VERSION::IStreamIn::ReadStatus;
41 using WriteCommand = ::android::hardware::audio::CPP_VERSION::IStreamOut::WriteCommand;
42 using WriteStatus = ::android::hardware::audio::CPP_VERSION::IStreamOut::WriteStatus;
43 
44 namespace android {
45 namespace CPP_VERSION {
46 
47 class DeviceHalHidl;
48 
49 class StreamHalHidl : public virtual StreamHalInterface, public ConversionHelperHidl
50 {
51   public:
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 base configuration of the stream:
56     //   - channel mask;
57     //   - format - e.g. AUDIO_FORMAT_PCM_16_BIT;
58     //   - sampling rate in Hz - eg. 44100.
59     virtual status_t getAudioProperties(audio_config_base_t *configBase);
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 
76     // 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 
89     // 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 
93   protected:
94     // Subclasses can not be constructed directly by clients.
95     explicit StreamHalHidl(IStream *stream);
96 
97     ~StreamHalHidl() override;
98 
99     status_t getCachedBufferSize(size_t *size);
100 
101     status_t getHalPid(pid_t *pid);
102 
103     bool requestHalThreadPriority(pid_t threadPid, pid_t threadId);
104 
105     // mStreamPowerLog is used for audio signal power logging.
106     StreamPowerLog mStreamPowerLog;
107 
108   private:
109     const int HAL_THREAD_PRIORITY_DEFAULT = -1;
110     IStream * const mStream;
111     int mHalThreadPriority;
112     size_t mCachedBufferSize;
113 };
114 
115 class StreamOutHalHidl : public StreamOutHalInterface, public StreamHalHidl {
116   public:
117     // Return the frame size (number of bytes per sample) of a stream.
118     virtual status_t getFrameSize(size_t *size);
119 
120     // Return the audio hardware driver estimated latency in milliseconds.
121     virtual status_t getLatency(uint32_t *latency);
122 
123     // Use this method in situations where audio mixing is done in the hardware.
124     virtual status_t setVolume(float left, float right);
125 
126     // Selects the audio presentation (if available).
127     virtual status_t selectPresentation(int presentationId, int programId);
128 
129     // Write audio buffer to driver.
130     virtual status_t write(const void *buffer, size_t bytes, size_t *written);
131 
132     // Return the number of audio frames written by the audio dsp to DAC since
133     // the output has exited standby.
134     virtual status_t getRenderPosition(uint32_t *dspFrames);
135 
136     // Get the local time at which the next write to the audio driver will be presented.
137     virtual status_t getNextWriteTimestamp(int64_t *timestamp);
138 
139     // Set the callback for notifying completion of non-blocking write and drain.
140     virtual status_t setCallback(wp<StreamOutHalInterfaceCallback> callback);
141 
142     // Returns whether pause and resume operations are supported.
143     virtual status_t supportsPauseAndResume(bool *supportsPause, bool *supportsResume);
144 
145     // Notifies to the audio driver to resume playback following a pause.
146     virtual status_t pause();
147 
148     // Notifies to the audio driver to resume playback following a pause.
149     virtual status_t resume();
150 
151     // Returns whether drain operation is supported.
152     virtual status_t supportsDrain(bool *supportsDrain);
153 
154     // Requests notification when data buffered by the driver/hardware has been played.
155     virtual status_t drain(bool earlyNotify);
156 
157     // Notifies to the audio driver to flush the queued data.
158     virtual status_t flush();
159 
160     // Return a recent count of the number of audio frames presented to an external observer.
161     virtual status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp);
162 
163     // Called when the metadata of the stream's source has been changed.
164     status_t updateSourceMetadata(const SourceMetadata& sourceMetadata) override;
165 
166     // Methods used by StreamOutCallback (HIDL).
167     void onWriteReady();
168     void onDrainReady();
169     void onError();
170 
171     // Returns the Dual Mono mode presentation setting.
172     status_t getDualMonoMode(audio_dual_mono_mode_t* mode) override;
173 
174     // Sets the Dual Mono mode presentation on the output device.
175     status_t setDualMonoMode(audio_dual_mono_mode_t mode) override;
176 
177     // Returns the Audio Description Mix level in dB.
178     status_t getAudioDescriptionMixLevel(float* leveldB) override;
179 
180     // Sets the Audio Description Mix level in dB.
181     status_t setAudioDescriptionMixLevel(float leveldB) override;
182 
183     // Retrieves current playback rate parameters.
184     status_t getPlaybackRateParameters(audio_playback_rate_t* playbackRate) override;
185 
186     // Sets the playback rate parameters that control playback behavior.
187     status_t setPlaybackRateParameters(const audio_playback_rate_t& playbackRate) override;
188 
189     status_t setEventCallback(const sp<StreamOutHalInterfaceEventCallback>& callback) override;
190 
191     // Methods used by StreamCodecFormatCallback (HIDL).
192     void onCodecFormatChanged(const std::basic_string<uint8_t>& metadataBs);
193 
194   private:
195     friend class DeviceHalHidl;
196     typedef MessageQueue<WriteCommand, hardware::kSynchronizedReadWrite> CommandMQ;
197     typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ;
198     typedef MessageQueue<WriteStatus, hardware::kSynchronizedReadWrite> StatusMQ;
199 
200     mediautils::atomic_wp<StreamOutHalInterfaceCallback> mCallback;
201     mediautils::atomic_wp<StreamOutHalInterfaceEventCallback> mEventCallback;
202     const sp<IStreamOut> mStream;
203     std::unique_ptr<CommandMQ> mCommandMQ;
204     std::unique_ptr<DataMQ> mDataMQ;
205     std::unique_ptr<StatusMQ> mStatusMQ;
206     std::atomic<pid_t> mWriterClient;
207     EventFlag* mEfGroup;
208 
209     // Can not be constructed directly by clients.
210     StreamOutHalHidl(const sp<IStreamOut>& stream);
211 
212     virtual ~StreamOutHalHidl();
213 
214     using WriterCallback = std::function<void(const WriteStatus& writeStatus)>;
215     status_t callWriterThread(
216             WriteCommand cmd, const char* cmdName,
217             const uint8_t* data, size_t dataSize, WriterCallback callback);
218     status_t prepareForWriting(size_t bufferSize);
219 };
220 
221 class StreamInHalHidl : public StreamInHalInterface, public StreamHalHidl {
222   public:
223     // Return the frame size (number of bytes per sample) of a stream.
224     virtual status_t getFrameSize(size_t *size);
225 
226     // Set the input gain for the audio driver.
227     virtual status_t setGain(float gain);
228 
229     // Read audio buffer in from driver.
230     virtual status_t read(void *buffer, size_t bytes, size_t *read);
231 
232     // Return the amount of input frames lost in the audio driver.
233     virtual status_t getInputFramesLost(uint32_t *framesLost);
234 
235     // Return a recent count of the number of audio frames received and
236     // the clock time associated with that frame count.
237     virtual status_t getCapturePosition(int64_t *frames, int64_t *time);
238 
239     // Get active microphones
240     virtual status_t getActiveMicrophones(std::vector<media::MicrophoneInfo> *microphones);
241 
242     // Set microphone direction (for processing)
243     virtual status_t setPreferredMicrophoneDirection(
244                             audio_microphone_direction_t direction) override;
245 
246     // Set microphone zoom (for processing)
247     virtual status_t setPreferredMicrophoneFieldDimension(float zoom) override;
248 
249     // Called when the metadata of the stream's sink has been changed.
250     status_t updateSinkMetadata(const SinkMetadata& sinkMetadata) override;
251 
252   private:
253     friend class DeviceHalHidl;
254     typedef MessageQueue<ReadParameters, hardware::kSynchronizedReadWrite> CommandMQ;
255     typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ;
256     typedef MessageQueue<ReadStatus, hardware::kSynchronizedReadWrite> StatusMQ;
257 
258     const sp<IStreamIn> mStream;
259     std::unique_ptr<CommandMQ> mCommandMQ;
260     std::unique_ptr<DataMQ> mDataMQ;
261     std::unique_ptr<StatusMQ> mStatusMQ;
262     std::atomic<pid_t> mReaderClient;
263     EventFlag* mEfGroup;
264 
265     // Can not be constructed directly by clients.
266     StreamInHalHidl(const sp<IStreamIn>& stream);
267 
268     virtual ~StreamInHalHidl();
269 
270     using ReaderCallback = std::function<void(const ReadStatus& readStatus)>;
271     status_t callReaderThread(
272             const ReadParameters& params, const char* cmdName, ReaderCallback callback);
273     status_t prepareForReading(size_t bufferSize);
274 };
275 
276 } // namespace CPP_VERSION
277 } // namespace android
278 
279 #endif // ANDROID_HARDWARE_STREAM_HAL_HIDL_H
280