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 <utils/Log.h>
22
23#include "DeviceHalLocal.h"
24#include "EffectHalLocal.h"
25#include "StreamHalLocal.h"
26
27namespace android {
28
29StreamHalLocal::StreamHalLocal(audio_stream_t *stream, sp<DeviceHalLocal> device)
30        : mDevice(device), mStream(stream) {
31}
32
33StreamHalLocal::~StreamHalLocal() {
34    mStream = 0;
35    mDevice.clear();
36}
37
38status_t StreamHalLocal::getSampleRate(uint32_t *rate) {
39    *rate = mStream->get_sample_rate(mStream);
40    return OK;
41}
42
43status_t StreamHalLocal::getBufferSize(size_t *size) {
44    *size = mStream->get_buffer_size(mStream);
45    return OK;
46}
47
48status_t StreamHalLocal::getChannelMask(audio_channel_mask_t *mask) {
49    *mask = mStream->get_channels(mStream);
50    return OK;
51}
52
53status_t StreamHalLocal::getFormat(audio_format_t *format) {
54    *format = mStream->get_format(mStream);
55    return OK;
56}
57
58status_t StreamHalLocal::getAudioProperties(
59        uint32_t *sampleRate, audio_channel_mask_t *mask, audio_format_t *format) {
60    *sampleRate = mStream->get_sample_rate(mStream);
61    *mask = mStream->get_channels(mStream);
62    *format = mStream->get_format(mStream);
63    return OK;
64}
65
66status_t StreamHalLocal::setParameters(const String8& kvPairs) {
67    return mStream->set_parameters(mStream, kvPairs.string());
68}
69
70status_t StreamHalLocal::getParameters(const String8& keys, String8 *values) {
71    char *halValues = mStream->get_parameters(mStream, keys.string());
72    if (halValues != NULL) {
73        values->setTo(halValues);
74        free(halValues);
75    } else {
76        values->clear();
77    }
78    return OK;
79}
80
81status_t StreamHalLocal::addEffect(sp<EffectHalInterface> effect) {
82    LOG_ALWAYS_FATAL_IF(!effect->isLocal(), "Only local effects can be added for a local stream");
83    return mStream->add_audio_effect(mStream,
84            static_cast<EffectHalLocal*>(effect.get())->handle());
85}
86
87status_t StreamHalLocal::removeEffect(sp<EffectHalInterface> effect) {
88    LOG_ALWAYS_FATAL_IF(!effect->isLocal(), "Only local effects can be removed for a local stream");
89    return mStream->remove_audio_effect(mStream,
90            static_cast<EffectHalLocal*>(effect.get())->handle());
91}
92
93status_t StreamHalLocal::standby() {
94    return mStream->standby(mStream);
95}
96
97status_t StreamHalLocal::dump(int fd) {
98    return mStream->dump(mStream, fd);
99}
100
101status_t StreamHalLocal::setHalThreadPriority(int) {
102    // Don't need to do anything as local hal is executed by audioflinger directly
103    // on the same thread.
104    return OK;
105}
106
107StreamOutHalLocal::StreamOutHalLocal(audio_stream_out_t *stream, sp<DeviceHalLocal> device)
108        : StreamHalLocal(&stream->common, device), mStream(stream) {
109}
110
111StreamOutHalLocal::~StreamOutHalLocal() {
112    mCallback.clear();
113    mDevice->closeOutputStream(mStream);
114    mStream = 0;
115}
116
117status_t StreamOutHalLocal::getFrameSize(size_t *size) {
118    *size = audio_stream_out_frame_size(mStream);
119    return OK;
120}
121
122status_t StreamOutHalLocal::getLatency(uint32_t *latency) {
123    *latency = mStream->get_latency(mStream);
124    return OK;
125}
126
127status_t StreamOutHalLocal::setVolume(float left, float right) {
128    if (mStream->set_volume == NULL) return INVALID_OPERATION;
129    return mStream->set_volume(mStream, left, right);
130}
131
132status_t StreamOutHalLocal::write(const void *buffer, size_t bytes, size_t *written) {
133    ssize_t writeResult = mStream->write(mStream, buffer, bytes);
134    if (writeResult > 0) {
135        *written = writeResult;
136        return OK;
137    } else {
138        *written = 0;
139        return writeResult;
140    }
141}
142
143status_t StreamOutHalLocal::getRenderPosition(uint32_t *dspFrames) {
144    return mStream->get_render_position(mStream, dspFrames);
145}
146
147status_t StreamOutHalLocal::getNextWriteTimestamp(int64_t *timestamp) {
148    if (mStream->get_next_write_timestamp == NULL) return INVALID_OPERATION;
149    return mStream->get_next_write_timestamp(mStream, timestamp);
150}
151
152status_t StreamOutHalLocal::setCallback(wp<StreamOutHalInterfaceCallback> callback) {
153    if (mStream->set_callback == NULL) return INVALID_OPERATION;
154    status_t result = mStream->set_callback(mStream, StreamOutHalLocal::asyncCallback, this);
155    if (result == OK) {
156        mCallback = callback;
157    }
158    return result;
159}
160
161// static
162int StreamOutHalLocal::asyncCallback(stream_callback_event_t event, void*, void *cookie) {
163    // We act as if we gave a wp<StreamOutHalLocal> to HAL. This way we should handle
164    // correctly the case when the callback is invoked while StreamOutHalLocal's destructor is
165    // already running, because the destructor is invoked after the refcount has been atomically
166    // decremented.
167    wp<StreamOutHalLocal> weakSelf(static_cast<StreamOutHalLocal*>(cookie));
168    sp<StreamOutHalLocal> self = weakSelf.promote();
169    if (self == 0) return 0;
170    sp<StreamOutHalInterfaceCallback> callback = self->mCallback.promote();
171    if (callback == 0) return 0;
172    ALOGV("asyncCallback() event %d", event);
173    switch (event) {
174        case STREAM_CBK_EVENT_WRITE_READY:
175            callback->onWriteReady();
176            break;
177        case STREAM_CBK_EVENT_DRAIN_READY:
178            callback->onDrainReady();
179            break;
180        case STREAM_CBK_EVENT_ERROR:
181            callback->onError();
182            break;
183        default:
184            ALOGW("asyncCallback() unknown event %d", event);
185            break;
186    }
187    return 0;
188}
189
190status_t StreamOutHalLocal::supportsPauseAndResume(bool *supportsPause, bool *supportsResume) {
191    *supportsPause = mStream->pause != NULL;
192    *supportsResume = mStream->resume != NULL;
193    return OK;
194}
195
196status_t StreamOutHalLocal::pause() {
197    if (mStream->pause == NULL) return INVALID_OPERATION;
198    return mStream->pause(mStream);
199}
200
201status_t StreamOutHalLocal::resume() {
202    if (mStream->resume == NULL) return INVALID_OPERATION;
203    return mStream->resume(mStream);
204}
205
206status_t StreamOutHalLocal::supportsDrain(bool *supportsDrain) {
207    *supportsDrain = mStream->drain != NULL;
208    return OK;
209}
210
211status_t StreamOutHalLocal::drain(bool earlyNotify) {
212    if (mStream->drain == NULL) return INVALID_OPERATION;
213    return mStream->drain(mStream, earlyNotify ? AUDIO_DRAIN_EARLY_NOTIFY : AUDIO_DRAIN_ALL);
214}
215
216status_t StreamOutHalLocal::flush() {
217    if (mStream->flush == NULL) return INVALID_OPERATION;
218    return mStream->flush(mStream);
219}
220
221status_t StreamOutHalLocal::getPresentationPosition(uint64_t *frames, struct timespec *timestamp) {
222    if (mStream->get_presentation_position == NULL) return INVALID_OPERATION;
223    return mStream->get_presentation_position(mStream, frames, timestamp);
224}
225
226status_t StreamOutHalLocal::start() {
227    if (mStream->start == NULL) return INVALID_OPERATION;
228    return mStream->start(mStream);
229}
230
231status_t StreamOutHalLocal::stop() {
232    if (mStream->stop == NULL) return INVALID_OPERATION;
233    return mStream->stop(mStream);
234}
235
236status_t StreamOutHalLocal::createMmapBuffer(int32_t minSizeFrames,
237                                  struct audio_mmap_buffer_info *info) {
238    if (mStream->create_mmap_buffer == NULL) return INVALID_OPERATION;
239    return mStream->create_mmap_buffer(mStream, minSizeFrames, info);
240}
241
242status_t StreamOutHalLocal::getMmapPosition(struct audio_mmap_position *position) {
243    if (mStream->get_mmap_position == NULL) return INVALID_OPERATION;
244    return mStream->get_mmap_position(mStream, position);
245}
246
247StreamInHalLocal::StreamInHalLocal(audio_stream_in_t *stream, sp<DeviceHalLocal> device)
248        : StreamHalLocal(&stream->common, device), mStream(stream) {
249}
250
251StreamInHalLocal::~StreamInHalLocal() {
252    mDevice->closeInputStream(mStream);
253    mStream = 0;
254}
255
256status_t StreamInHalLocal::getFrameSize(size_t *size) {
257    *size = audio_stream_in_frame_size(mStream);
258    return OK;
259}
260
261status_t StreamInHalLocal::setGain(float gain) {
262    return mStream->set_gain(mStream, gain);
263}
264
265status_t StreamInHalLocal::read(void *buffer, size_t bytes, size_t *read) {
266    ssize_t readResult = mStream->read(mStream, buffer, bytes);
267    if (readResult > 0) {
268        *read = readResult;
269        return OK;
270    } else {
271        *read = 0;
272        return readResult;
273    }
274}
275
276status_t StreamInHalLocal::getInputFramesLost(uint32_t *framesLost) {
277    *framesLost = mStream->get_input_frames_lost(mStream);
278    return OK;
279}
280
281status_t StreamInHalLocal::getCapturePosition(int64_t *frames, int64_t *time) {
282    if (mStream->get_capture_position == NULL) return INVALID_OPERATION;
283    return mStream->get_capture_position(mStream, frames, time);
284}
285
286status_t StreamInHalLocal::start() {
287    if (mStream->start == NULL) return INVALID_OPERATION;
288    return mStream->start(mStream);
289}
290
291status_t StreamInHalLocal::stop() {
292    if (mStream->stop == NULL) return INVALID_OPERATION;
293    return mStream->stop(mStream);
294}
295
296status_t StreamInHalLocal::createMmapBuffer(int32_t minSizeFrames,
297                                  struct audio_mmap_buffer_info *info) {
298    if (mStream->create_mmap_buffer == NULL) return INVALID_OPERATION;
299    return mStream->create_mmap_buffer(mStream, minSizeFrames, info);
300}
301
302status_t StreamInHalLocal::getMmapPosition(struct audio_mmap_position *position) {
303    if (mStream->get_mmap_position == NULL) return INVALID_OPERATION;
304    return mStream->get_mmap_position(mStream, position);
305}
306
307} // namespace android
308