/* * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ANDROID_HARDWARE_AUDIO_V2_0_STREAM_H #define ANDROID_HARDWARE_AUDIO_V2_0_STREAM_H #include #include #include #include #include #include "ParametersUtil.h" namespace android { namespace hardware { namespace audio { namespace V2_0 { namespace implementation { using ::android::hardware::audio::common::V2_0::AudioChannelMask; using ::android::hardware::audio::common::V2_0::AudioDevice; using ::android::hardware::audio::common::V2_0::AudioFormat; using ::android::hardware::audio::V2_0::DeviceAddress; using ::android::hardware::audio::V2_0::IStream; using ::android::hardware::audio::V2_0::ParameterValue; using ::android::hardware::audio::V2_0::Result; using ::android::hardware::Return; using ::android::hardware::Void; using ::android::hardware::hidl_vec; using ::android::hardware::hidl_string; using ::android::sp; struct Stream : public IStream, public ParametersUtil { explicit Stream(audio_stream_t* stream); // Methods from ::android::hardware::audio::V2_0::IStream follow. Return getFrameSize() override; Return getFrameCount() override; Return getBufferSize() override; Return getSampleRate() override; Return getSupportedSampleRates(getSupportedSampleRates_cb _hidl_cb) override; Return setSampleRate(uint32_t sampleRateHz) override; Return getChannelMask() override; Return getSupportedChannelMasks(getSupportedChannelMasks_cb _hidl_cb) override; Return setChannelMask(AudioChannelMask mask) override; Return getFormat() override; Return getSupportedFormats(getSupportedFormats_cb _hidl_cb) override; Return setFormat(AudioFormat format) override; Return getAudioProperties(getAudioProperties_cb _hidl_cb) override; Return addEffect(uint64_t effectId) override; Return removeEffect(uint64_t effectId) override; Return standby() override; Return getDevice() override; Return setDevice(const DeviceAddress& address) override; Return setConnectedState(const DeviceAddress& address, bool connected) override; Return setHwAvSync(uint32_t hwAvSync) override; Return getParameters( const hidl_vec& keys, getParameters_cb _hidl_cb) override; Return setParameters(const hidl_vec& parameters) override; Return debugDump(const hidl_handle& fd) override; Return start() override; Return stop() override; Return createMmapBuffer(int32_t minSizeFrames, createMmapBuffer_cb _hidl_cb) override; Return getMmapPosition(getMmapPosition_cb _hidl_cb) override; Return close() override; // Utility methods for extending interfaces. static Result analyzeStatus(const char* funcName, int status); static Result analyzeStatus(const char* funcName, int status, const std::vector& ignoreErrors); private: audio_stream_t *mStream; virtual ~Stream(); // Methods from ParametersUtil. char* halGetParameters(const char* keys) override; int halSetParameters(const char* keysAndValues) override; }; template struct StreamMmap : public RefBase { explicit StreamMmap(T* stream) : mStream(stream) {} Return start(); Return stop(); Return createMmapBuffer( int32_t minSizeFrames, size_t frameSize, IStream::createMmapBuffer_cb _hidl_cb); Return getMmapPosition(IStream::getMmapPosition_cb _hidl_cb); private: StreamMmap() {} T *mStream; }; template Return StreamMmap::start() { if (mStream->start == NULL) return Result::NOT_SUPPORTED; int result = mStream->start(mStream); return Stream::analyzeStatus("start", result); } template Return StreamMmap::stop() { if (mStream->stop == NULL) return Result::NOT_SUPPORTED; int result = mStream->stop(mStream); return Stream::analyzeStatus("stop", result); } template Return StreamMmap::createMmapBuffer(int32_t minSizeFrames, size_t frameSize, IStream::createMmapBuffer_cb _hidl_cb) { Result retval(Result::NOT_SUPPORTED); MmapBufferInfo info; native_handle_t* hidlHandle = nullptr; if (mStream->create_mmap_buffer != NULL) { struct audio_mmap_buffer_info halInfo; retval = Stream::analyzeStatus( "create_mmap_buffer", mStream->create_mmap_buffer(mStream, minSizeFrames, &halInfo)); if (retval == Result::OK) { hidlHandle = native_handle_create(1, 0); hidlHandle->data[0] = halInfo.shared_memory_fd; info.sharedMemory = hidl_memory("audio_buffer", hidlHandle, frameSize *halInfo.buffer_size_frames); info.bufferSizeFrames = halInfo.buffer_size_frames; info.burstSizeFrames = halInfo.burst_size_frames; } } _hidl_cb(retval, info); if (hidlHandle != nullptr) { native_handle_delete(hidlHandle); } return Void(); } template Return StreamMmap::getMmapPosition(IStream::getMmapPosition_cb _hidl_cb) { Result retval(Result::NOT_SUPPORTED); MmapPosition position; if (mStream->get_mmap_position != NULL) { struct audio_mmap_position halPosition; retval = Stream::analyzeStatus( "get_mmap_position", mStream->get_mmap_position(mStream, &halPosition)); if (retval == Result::OK) { position.timeNanoseconds = halPosition.time_nanoseconds; position.positionFrames = halPosition.position_frames; } } _hidl_cb(retval, position); return Void(); } } // namespace implementation } // namespace V2_0 } // namespace audio } // namespace hardware } // namespace android #endif // ANDROID_HARDWARE_AUDIO_V2_0_STREAM_H