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 #define LOG_TAG "StreamHalLocal"
18 //#define LOG_NDEBUG 0
19
20 #include <hardware/audio.h>
21 #include <media/AudioParameter.h>
22 #include <utils/Log.h>
23
24 #include "DeviceHalLocal.h"
25 #include "StreamHalLocal.h"
26 #include "VersionUtils.h"
27
28 namespace android {
29 namespace CPP_VERSION {
30
StreamHalLocal(audio_stream_t * stream,sp<DeviceHalLocal> device)31 StreamHalLocal::StreamHalLocal(audio_stream_t *stream, sp<DeviceHalLocal> device)
32 : mDevice(device),
33 mStream(stream) {
34 // Instrument audio signal power logging.
35 // Note: This assumes channel mask, format, and sample rate do not change after creation.
36 if (mStream != nullptr /* && mStreamPowerLog.isUserDebugOrEngBuild() */) {
37 mStreamPowerLog.init(mStream->get_sample_rate(mStream),
38 mStream->get_channels(mStream),
39 mStream->get_format(mStream));
40 }
41 }
42
~StreamHalLocal()43 StreamHalLocal::~StreamHalLocal() {
44 mStream = 0;
45 mDevice.clear();
46 }
47
getSampleRate(uint32_t * rate)48 status_t StreamHalLocal::getSampleRate(uint32_t *rate) {
49 *rate = mStream->get_sample_rate(mStream);
50 return OK;
51 }
52
getBufferSize(size_t * size)53 status_t StreamHalLocal::getBufferSize(size_t *size) {
54 *size = mStream->get_buffer_size(mStream);
55 return OK;
56 }
57
getChannelMask(audio_channel_mask_t * mask)58 status_t StreamHalLocal::getChannelMask(audio_channel_mask_t *mask) {
59 *mask = mStream->get_channels(mStream);
60 return OK;
61 }
62
getFormat(audio_format_t * format)63 status_t StreamHalLocal::getFormat(audio_format_t *format) {
64 *format = mStream->get_format(mStream);
65 return OK;
66 }
67
getAudioProperties(uint32_t * sampleRate,audio_channel_mask_t * mask,audio_format_t * format)68 status_t StreamHalLocal::getAudioProperties(
69 uint32_t *sampleRate, audio_channel_mask_t *mask, audio_format_t *format) {
70 *sampleRate = mStream->get_sample_rate(mStream);
71 *mask = mStream->get_channels(mStream);
72 *format = mStream->get_format(mStream);
73 return OK;
74 }
75
setParameters(const String8 & kvPairs)76 status_t StreamHalLocal::setParameters(const String8& kvPairs) {
77 return mStream->set_parameters(mStream, kvPairs.string());
78 }
79
getParameters(const String8 & keys,String8 * values)80 status_t StreamHalLocal::getParameters(const String8& keys, String8 *values) {
81 char *halValues = mStream->get_parameters(mStream, keys.string());
82 if (halValues != NULL) {
83 values->setTo(halValues);
84 free(halValues);
85 } else {
86 values->clear();
87 }
88 return OK;
89 }
90
addEffect(sp<EffectHalInterface>)91 status_t StreamHalLocal::addEffect(sp<EffectHalInterface>) {
92 LOG_ALWAYS_FATAL("Local streams can not have effects");
93 return INVALID_OPERATION;
94 }
95
removeEffect(sp<EffectHalInterface>)96 status_t StreamHalLocal::removeEffect(sp<EffectHalInterface>) {
97 LOG_ALWAYS_FATAL("Local streams can not have effects");
98 return INVALID_OPERATION;
99 }
100
standby()101 status_t StreamHalLocal::standby() {
102 return mStream->standby(mStream);
103 }
104
dump(int fd)105 status_t StreamHalLocal::dump(int fd) {
106 status_t status = mStream->dump(mStream, fd);
107 mStreamPowerLog.dump(fd);
108 return status;
109 }
110
setHalThreadPriority(int)111 status_t StreamHalLocal::setHalThreadPriority(int) {
112 // Don't need to do anything as local hal is executed by audioflinger directly
113 // on the same thread.
114 return OK;
115 }
116
StreamOutHalLocal(audio_stream_out_t * stream,sp<DeviceHalLocal> device)117 StreamOutHalLocal::StreamOutHalLocal(audio_stream_out_t *stream, sp<DeviceHalLocal> device)
118 : StreamHalLocal(&stream->common, device), mStream(stream) {
119 }
120
~StreamOutHalLocal()121 StreamOutHalLocal::~StreamOutHalLocal() {
122 mCallback.clear();
123 mDevice->closeOutputStream(mStream);
124 mStream = 0;
125 }
126
getFrameSize(size_t * size)127 status_t StreamOutHalLocal::getFrameSize(size_t *size) {
128 *size = audio_stream_out_frame_size(mStream);
129 return OK;
130 }
131
getLatency(uint32_t * latency)132 status_t StreamOutHalLocal::getLatency(uint32_t *latency) {
133 *latency = mStream->get_latency(mStream);
134 return OK;
135 }
136
setVolume(float left,float right)137 status_t StreamOutHalLocal::setVolume(float left, float right) {
138 if (mStream->set_volume == NULL) return INVALID_OPERATION;
139 return mStream->set_volume(mStream, left, right);
140 }
141
selectPresentation(int presentationId,int programId)142 status_t StreamOutHalLocal::selectPresentation(int presentationId, int programId) {
143 AudioParameter param;
144 param.addInt(String8(AudioParameter::keyPresentationId), presentationId);
145 param.addInt(String8(AudioParameter::keyProgramId), programId);
146 return setParameters(param.toString());
147 }
148
write(const void * buffer,size_t bytes,size_t * written)149 status_t StreamOutHalLocal::write(const void *buffer, size_t bytes, size_t *written) {
150 ssize_t writeResult = mStream->write(mStream, buffer, bytes);
151 if (writeResult > 0) {
152 *written = writeResult;
153 mStreamPowerLog.log(buffer, *written);
154 return OK;
155 } else {
156 *written = 0;
157 return writeResult;
158 }
159 }
160
getRenderPosition(uint32_t * dspFrames)161 status_t StreamOutHalLocal::getRenderPosition(uint32_t *dspFrames) {
162 return mStream->get_render_position(mStream, dspFrames);
163 }
164
getNextWriteTimestamp(int64_t * timestamp)165 status_t StreamOutHalLocal::getNextWriteTimestamp(int64_t *timestamp) {
166 if (mStream->get_next_write_timestamp == NULL) return INVALID_OPERATION;
167 return mStream->get_next_write_timestamp(mStream, timestamp);
168 }
169
setCallback(wp<StreamOutHalInterfaceCallback> callback)170 status_t StreamOutHalLocal::setCallback(wp<StreamOutHalInterfaceCallback> callback) {
171 if (mStream->set_callback == NULL) return INVALID_OPERATION;
172 status_t result = mStream->set_callback(mStream, StreamOutHalLocal::asyncCallback, this);
173 if (result == OK) {
174 mCallback = callback;
175 }
176 return result;
177 }
178
179 // static
asyncCallback(stream_callback_event_t event,void *,void * cookie)180 int StreamOutHalLocal::asyncCallback(stream_callback_event_t event, void*, void *cookie) {
181 // We act as if we gave a wp<StreamOutHalLocal> to HAL. This way we should handle
182 // correctly the case when the callback is invoked while StreamOutHalLocal's destructor is
183 // already running, because the destructor is invoked after the refcount has been atomically
184 // decremented.
185 wp<StreamOutHalLocal> weakSelf(static_cast<StreamOutHalLocal*>(cookie));
186 sp<StreamOutHalLocal> self = weakSelf.promote();
187 if (self == 0) return 0;
188 sp<StreamOutHalInterfaceCallback> callback = self->mCallback.promote();
189 if (callback == 0) return 0;
190 ALOGV("asyncCallback() event %d", event);
191 switch (event) {
192 case STREAM_CBK_EVENT_WRITE_READY:
193 callback->onWriteReady();
194 break;
195 case STREAM_CBK_EVENT_DRAIN_READY:
196 callback->onDrainReady();
197 break;
198 case STREAM_CBK_EVENT_ERROR:
199 callback->onError();
200 break;
201 default:
202 ALOGW("asyncCallback() unknown event %d", event);
203 break;
204 }
205 return 0;
206 }
207
supportsPauseAndResume(bool * supportsPause,bool * supportsResume)208 status_t StreamOutHalLocal::supportsPauseAndResume(bool *supportsPause, bool *supportsResume) {
209 *supportsPause = mStream->pause != NULL;
210 *supportsResume = mStream->resume != NULL;
211 return OK;
212 }
213
pause()214 status_t StreamOutHalLocal::pause() {
215 if (mStream->pause == NULL) return INVALID_OPERATION;
216 return mStream->pause(mStream);
217 }
218
resume()219 status_t StreamOutHalLocal::resume() {
220 if (mStream->resume == NULL) return INVALID_OPERATION;
221 return mStream->resume(mStream);
222 }
223
supportsDrain(bool * supportsDrain)224 status_t StreamOutHalLocal::supportsDrain(bool *supportsDrain) {
225 *supportsDrain = mStream->drain != NULL;
226 return OK;
227 }
228
drain(bool earlyNotify)229 status_t StreamOutHalLocal::drain(bool earlyNotify) {
230 if (mStream->drain == NULL) return INVALID_OPERATION;
231 return mStream->drain(mStream, earlyNotify ? AUDIO_DRAIN_EARLY_NOTIFY : AUDIO_DRAIN_ALL);
232 }
233
flush()234 status_t StreamOutHalLocal::flush() {
235 if (mStream->flush == NULL) return INVALID_OPERATION;
236 return mStream->flush(mStream);
237 }
238
getPresentationPosition(uint64_t * frames,struct timespec * timestamp)239 status_t StreamOutHalLocal::getPresentationPosition(uint64_t *frames, struct timespec *timestamp) {
240 if (mStream->get_presentation_position == NULL) return INVALID_OPERATION;
241 return mStream->get_presentation_position(mStream, frames, timestamp);
242 }
243
updateSourceMetadata(const SourceMetadata & sourceMetadata)244 status_t StreamOutHalLocal::updateSourceMetadata(const SourceMetadata& sourceMetadata) {
245 if (mStream->update_source_metadata == nullptr) {
246 return INVALID_OPERATION;
247 }
248 const source_metadata_t metadata {
249 .track_count = sourceMetadata.tracks.size(),
250 // const cast is fine as it is in a const structure
251 .tracks = const_cast<playback_track_metadata*>(sourceMetadata.tracks.data()),
252 };
253 mStream->update_source_metadata(mStream, &metadata);
254 return OK;
255 }
256
start()257 status_t StreamOutHalLocal::start() {
258 if (mStream->start == NULL) return INVALID_OPERATION;
259 return mStream->start(mStream);
260 }
261
stop()262 status_t StreamOutHalLocal::stop() {
263 if (mStream->stop == NULL) return INVALID_OPERATION;
264 return mStream->stop(mStream);
265 }
266
createMmapBuffer(int32_t minSizeFrames,struct audio_mmap_buffer_info * info)267 status_t StreamOutHalLocal::createMmapBuffer(int32_t minSizeFrames,
268 struct audio_mmap_buffer_info *info) {
269 if (mStream->create_mmap_buffer == NULL) return INVALID_OPERATION;
270 return mStream->create_mmap_buffer(mStream, minSizeFrames, info);
271 }
272
getMmapPosition(struct audio_mmap_position * position)273 status_t StreamOutHalLocal::getMmapPosition(struct audio_mmap_position *position) {
274 if (mStream->get_mmap_position == NULL) return INVALID_OPERATION;
275 return mStream->get_mmap_position(mStream, position);
276 }
277
StreamInHalLocal(audio_stream_in_t * stream,sp<DeviceHalLocal> device)278 StreamInHalLocal::StreamInHalLocal(audio_stream_in_t *stream, sp<DeviceHalLocal> device)
279 : StreamHalLocal(&stream->common, device), mStream(stream) {
280 }
281
~StreamInHalLocal()282 StreamInHalLocal::~StreamInHalLocal() {
283 mDevice->closeInputStream(mStream);
284 mStream = 0;
285 }
286
getFrameSize(size_t * size)287 status_t StreamInHalLocal::getFrameSize(size_t *size) {
288 *size = audio_stream_in_frame_size(mStream);
289 return OK;
290 }
291
setGain(float gain)292 status_t StreamInHalLocal::setGain(float gain) {
293 return mStream->set_gain(mStream, gain);
294 }
295
read(void * buffer,size_t bytes,size_t * read)296 status_t StreamInHalLocal::read(void *buffer, size_t bytes, size_t *read) {
297 ssize_t readResult = mStream->read(mStream, buffer, bytes);
298 if (readResult > 0) {
299 *read = readResult;
300 mStreamPowerLog.log( buffer, *read);
301 return OK;
302 } else {
303 *read = 0;
304 return readResult;
305 }
306 }
307
getInputFramesLost(uint32_t * framesLost)308 status_t StreamInHalLocal::getInputFramesLost(uint32_t *framesLost) {
309 *framesLost = mStream->get_input_frames_lost(mStream);
310 return OK;
311 }
312
getCapturePosition(int64_t * frames,int64_t * time)313 status_t StreamInHalLocal::getCapturePosition(int64_t *frames, int64_t *time) {
314 if (mStream->get_capture_position == NULL) return INVALID_OPERATION;
315 return mStream->get_capture_position(mStream, frames, time);
316 }
317
updateSinkMetadata(const SinkMetadata & sinkMetadata)318 status_t StreamInHalLocal::updateSinkMetadata(const SinkMetadata& sinkMetadata) {
319 if (mStream->update_sink_metadata == nullptr) {
320 return INVALID_OPERATION;
321 }
322 const sink_metadata_t metadata {
323 .track_count = sinkMetadata.tracks.size(),
324 // const cast is fine as it is in a const structure
325 .tracks = const_cast<record_track_metadata*>(sinkMetadata.tracks.data()),
326 };
327 mStream->update_sink_metadata(mStream, &metadata);
328 return OK;
329 }
330
start()331 status_t StreamInHalLocal::start() {
332 if (mStream->start == NULL) return INVALID_OPERATION;
333 return mStream->start(mStream);
334 }
335
stop()336 status_t StreamInHalLocal::stop() {
337 if (mStream->stop == NULL) return INVALID_OPERATION;
338 return mStream->stop(mStream);
339 }
340
createMmapBuffer(int32_t minSizeFrames,struct audio_mmap_buffer_info * info)341 status_t StreamInHalLocal::createMmapBuffer(int32_t minSizeFrames,
342 struct audio_mmap_buffer_info *info) {
343 if (mStream->create_mmap_buffer == NULL) return INVALID_OPERATION;
344 return mStream->create_mmap_buffer(mStream, minSizeFrames, info);
345 }
346
getMmapPosition(struct audio_mmap_position * position)347 status_t StreamInHalLocal::getMmapPosition(struct audio_mmap_position *position) {
348 if (mStream->get_mmap_position == NULL) return INVALID_OPERATION;
349 return mStream->get_mmap_position(mStream, position);
350 }
351
352 #if MAJOR_VERSION == 2
getActiveMicrophones(std::vector<media::MicrophoneInfo> * microphones __unused)353 status_t StreamInHalLocal::getActiveMicrophones(
354 std::vector<media::MicrophoneInfo> *microphones __unused) {
355 return INVALID_OPERATION;
356 }
357 #elif MAJOR_VERSION >= 4
getActiveMicrophones(std::vector<media::MicrophoneInfo> * microphones)358 status_t StreamInHalLocal::getActiveMicrophones(std::vector<media::MicrophoneInfo> *microphones) {
359 if (mStream->get_active_microphones == NULL) return INVALID_OPERATION;
360 size_t actual_mics = AUDIO_MICROPHONE_MAX_COUNT;
361 audio_microphone_characteristic_t mic_array[AUDIO_MICROPHONE_MAX_COUNT];
362 status_t status = mStream->get_active_microphones(mStream, &mic_array[0], &actual_mics);
363 for (size_t i = 0; i < actual_mics; i++) {
364 media::MicrophoneInfo microphoneInfo = media::MicrophoneInfo(mic_array[i]);
365 microphones->push_back(microphoneInfo);
366 }
367 return status;
368 }
369 #endif
370
371 #if MAJOR_VERSION < 5
setPreferredMicrophoneDirection(audio_microphone_direction_t direction __unused)372 status_t StreamInHalLocal::setPreferredMicrophoneDirection(
373 audio_microphone_direction_t direction __unused) {
374 return INVALID_OPERATION;
375 }
376
setPreferredMicrophoneFieldDimension(float zoom __unused)377 status_t StreamInHalLocal::setPreferredMicrophoneFieldDimension(float zoom __unused) {
378 return INVALID_OPERATION;
379 }
380 #else
setPreferredMicrophoneDirection(audio_microphone_direction_t direction)381 status_t StreamInHalLocal::setPreferredMicrophoneDirection(audio_microphone_direction_t direction) {
382 if (mStream->set_microphone_direction == NULL) return INVALID_OPERATION;
383 return mStream->set_microphone_direction(mStream, direction);
384 }
385
setPreferredMicrophoneFieldDimension(float zoom)386 status_t StreamInHalLocal::setPreferredMicrophoneFieldDimension(float zoom) {
387 if (mStream->set_microphone_field_dimension == NULL) return INVALID_OPERATION;
388 return mStream->set_microphone_field_dimension(mStream, zoom);
389
390 }
391 #endif
392
393 } // namespace CPP_VERSION
394 } // namespace android
395
396
397