Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 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 "StreamInHAL" |
| 18 | |
| 19 | #include <hardware/audio.h> |
| 20 | #include <utils/Log.h> |
| 21 | |
| 22 | #include "StreamIn.h" |
| 23 | |
| 24 | namespace android { |
| 25 | namespace hardware { |
| 26 | namespace audio { |
| 27 | namespace V2_0 { |
| 28 | namespace implementation { |
| 29 | |
| 30 | StreamIn::StreamIn(audio_hw_device_t* device, audio_stream_in_t* stream) |
| 31 | : mDevice(device), mStream(stream), mStreamCommon(new Stream(&stream->common)) { |
| 32 | } |
| 33 | |
| 34 | StreamIn::~StreamIn() { |
| 35 | mDevice->close_input_stream(mDevice, mStream); |
| 36 | mStream = nullptr; |
| 37 | mDevice = nullptr; |
| 38 | } |
| 39 | |
| 40 | // Methods from ::android::hardware::audio::V2_0::IStream follow. |
| 41 | Return<uint64_t> StreamIn::getFrameSize() { |
| 42 | return audio_stream_in_frame_size(mStream); |
| 43 | } |
| 44 | |
| 45 | Return<uint64_t> StreamIn::getFrameCount() { |
| 46 | return mStreamCommon->getFrameCount(); |
| 47 | } |
| 48 | |
| 49 | Return<uint64_t> StreamIn::getBufferSize() { |
| 50 | return mStreamCommon->getBufferSize(); |
| 51 | } |
| 52 | |
| 53 | Return<uint32_t> StreamIn::getSampleRate() { |
| 54 | return mStreamCommon->getSampleRate(); |
| 55 | } |
| 56 | |
| 57 | Return<void> StreamIn::getSupportedSampleRates(getSupportedSampleRates_cb _hidl_cb) { |
| 58 | return mStreamCommon->getSupportedSampleRates(_hidl_cb); |
| 59 | } |
| 60 | |
| 61 | Return<Result> StreamIn::setSampleRate(uint32_t sampleRateHz) { |
| 62 | return mStreamCommon->setSampleRate(sampleRateHz); |
| 63 | } |
| 64 | |
| 65 | Return<AudioChannelMask> StreamIn::getChannelMask() { |
| 66 | return mStreamCommon->getChannelMask(); |
| 67 | } |
| 68 | |
| 69 | Return<void> StreamIn::getSupportedChannelMasks(getSupportedChannelMasks_cb _hidl_cb) { |
| 70 | return mStreamCommon->getSupportedChannelMasks(_hidl_cb); |
| 71 | } |
| 72 | |
| 73 | Return<Result> StreamIn::setChannelMask(AudioChannelMask mask) { |
| 74 | return mStreamCommon->setChannelMask(mask); |
| 75 | } |
| 76 | |
| 77 | Return<AudioFormat> StreamIn::getFormat() { |
| 78 | return mStreamCommon->getFormat(); |
| 79 | } |
| 80 | |
| 81 | Return<void> StreamIn::getSupportedFormats(getSupportedFormats_cb _hidl_cb) { |
| 82 | return mStreamCommon->getSupportedFormats(_hidl_cb); |
| 83 | } |
| 84 | |
| 85 | Return<Result> StreamIn::setFormat(AudioFormat format) { |
| 86 | return mStreamCommon->setFormat(format); |
| 87 | } |
| 88 | |
| 89 | Return<void> StreamIn::getAudioProperties(getAudioProperties_cb _hidl_cb) { |
| 90 | return mStreamCommon->getAudioProperties(_hidl_cb); |
| 91 | } |
| 92 | |
| 93 | Return<Result> StreamIn::addEffect(uint64_t effectId) { |
| 94 | return mStreamCommon->addEffect(effectId); |
| 95 | } |
| 96 | |
| 97 | Return<Result> StreamIn::removeEffect(uint64_t effectId) { |
| 98 | return mStreamCommon->removeEffect(effectId); |
| 99 | } |
| 100 | |
| 101 | Return<Result> StreamIn::standby() { |
| 102 | return mStreamCommon->standby(); |
| 103 | } |
| 104 | |
| 105 | Return<AudioDevice> StreamIn::getDevice() { |
| 106 | return mStreamCommon->getDevice(); |
| 107 | } |
| 108 | |
| 109 | Return<Result> StreamIn::setDevice(const DeviceAddress& address) { |
| 110 | return mStreamCommon->setDevice(address); |
| 111 | } |
| 112 | |
| 113 | Return<Result> StreamIn::setConnectedState(const DeviceAddress& address, bool connected) { |
| 114 | return mStreamCommon->setConnectedState(address, connected); |
| 115 | } |
| 116 | |
| 117 | Return<Result> StreamIn::setHwAvSync(uint32_t hwAvSync) { |
| 118 | return mStreamCommon->setHwAvSync(hwAvSync); |
| 119 | } |
| 120 | |
| 121 | Return<void> StreamIn::getParameters(const hidl_vec<hidl_string>& keys, getParameters_cb _hidl_cb) { |
| 122 | return mStreamCommon->getParameters(keys, _hidl_cb); |
| 123 | } |
| 124 | |
| 125 | Return<Result> StreamIn::setParameters(const hidl_vec<ParameterValue>& parameters) { |
| 126 | return mStreamCommon->setParameters(parameters); |
| 127 | } |
| 128 | |
Martijn Coenen | 70b9a15 | 2016-11-18 15:29:32 +0100 | [diff] [blame^] | 129 | Return<void> StreamIn::debugDump(const hidl_handle& fd) { |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 130 | return mStreamCommon->debugDump(fd); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | // Methods from ::android::hardware::audio::V2_0::IStreamIn follow. |
| 135 | Return<void> StreamIn::getAudioSource(getAudioSource_cb _hidl_cb) { |
| 136 | int halSource; |
| 137 | Result retval = mStreamCommon->getParam(AudioParameter::keyInputSource, &halSource); |
| 138 | AudioSource source(AudioSource::DEFAULT); |
| 139 | if (retval == Result::OK) { |
| 140 | source = AudioSource(halSource); |
| 141 | } |
| 142 | _hidl_cb(retval, source); |
| 143 | return Void(); |
| 144 | } |
| 145 | |
| 146 | Return<Result> StreamIn::setGain(float gain) { |
| 147 | return mStreamCommon->analyzeStatus("set_gain", mStream->set_gain(mStream, gain)); |
| 148 | } |
| 149 | |
| 150 | Return<void> StreamIn::read(uint64_t size, read_cb _hidl_cb) { |
| 151 | // TODO(mnaganov): Replace with FMQ version. |
| 152 | hidl_vec<uint8_t> data; |
| 153 | data.resize(size); |
| 154 | Result retval(Result::OK); |
| 155 | ssize_t readResult = mStream->read(mStream, &data[0], data.size()); |
| 156 | if (readResult >= 0 && static_cast<size_t>(readResult) != data.size()) { |
| 157 | data.resize(readResult); |
| 158 | } else if (readResult < 0) { |
| 159 | data.resize(0); |
| 160 | retval = mStreamCommon->analyzeStatus("read", readResult); |
| 161 | } |
| 162 | _hidl_cb(retval, data); |
| 163 | return Void(); |
| 164 | } |
| 165 | |
| 166 | Return<uint32_t> StreamIn::getInputFramesLost() { |
| 167 | return mStream->get_input_frames_lost(mStream); |
| 168 | } |
| 169 | |
| 170 | Return<void> StreamIn::getCapturePosition(getCapturePosition_cb _hidl_cb) { |
| 171 | Result retval(Result::NOT_SUPPORTED); |
| 172 | uint64_t frames = 0, time = 0; |
| 173 | if (mStream->get_capture_position != NULL) { |
| 174 | int64_t halFrames, halTime; |
| 175 | retval = mStreamCommon->analyzeStatus( |
| 176 | "get_capture_position", |
| 177 | mStream->get_capture_position(mStream, &halFrames, &halTime)); |
| 178 | if (retval == Result::OK) { |
| 179 | frames = halFrames; |
| 180 | time = halTime; |
| 181 | } |
| 182 | } |
| 183 | _hidl_cb(retval, frames, time); |
| 184 | return Void(); |
| 185 | } |
| 186 | |
| 187 | } // namespace implementation |
| 188 | } // namespace V2_0 |
| 189 | } // namespace audio |
| 190 | } // namespace hardware |
| 191 | } // namespace android |