Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2b228bb3 authored by Mikhail Naganov's avatar Mikhail Naganov
Browse files

soundtrigger: Default implementation for version 2.1

Wrapped around the 2.0 default implementation. Added functionality
for retrieving and sending the data via shared memory.

Bug: 68823037
Test: compiles
Change-Id: Ie38dd261dc8c635462a7e2ee26672a83af915e84
parent dfdc3bbc
Loading
Loading
Loading
Loading
+49 −0
Original line number Diff line number Diff line
#
# Copyright (C) 2018 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.


LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := android.hardware.soundtrigger@2.1-impl
LOCAL_VENDOR_MODULE := true
LOCAL_MODULE_RELATIVE_PATH := hw
LOCAL_SRC_FILES := \
    SoundTriggerHw.cpp

LOCAL_CFLAGS := -Wall -Werror

LOCAL_SHARED_LIBRARIES := \
        libhardware \
        libhidlbase \
        libhidlmemory \
        libhidltransport \
        liblog \
        libutils \
        android.hardware.soundtrigger@2.1 \
        android.hardware.soundtrigger@2.0 \
        android.hardware.soundtrigger@2.0-core \
        android.hidl.allocator@1.0 \
        android.hidl.memory@1.0

LOCAL_C_INCLUDE_DIRS := $(LOCAL_PATH)

ifeq ($(strip $(AUDIOSERVER_MULTILIB)),)
LOCAL_MULTILIB := 32
else
LOCAL_MULTILIB := $(AUDIOSERVER_MULTILIB)
endif

include $(BUILD_SHARED_LIBRARY)
+194 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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.
 */

#define LOG_TAG "SoundTriggerHw"

#include "SoundTriggerHw.h"

#include <utility>

#include <android/hidl/allocator/1.0/IAllocator.h>
#include <android/log.h>
#include <hidlmemory/mapping.h>

using android::hardware::hidl_memory;
using android::hidl::allocator::V1_0::IAllocator;
using android::hidl::memory::V1_0::IMemory;

namespace android {
namespace hardware {
namespace soundtrigger {
namespace V2_1 {
namespace implementation {

namespace {

// Backs up by the vector with the contents of shared memory.
// It is assumed that the passed hidl_vector is empty, so it's
// not cleared if the memory is a null object.
// The caller needs to keep the returned sp<IMemory> as long as
// the data is needed.
std::pair<bool, sp<IMemory>> memoryAsVector(const hidl_memory& m, hidl_vec<uint8_t>* vec) {
    sp<IMemory> memory;
    if (m.size() == 0) {
        return std::make_pair(true, memory);
    }
    memory = mapMemory(m);
    if (memory != nullptr) {
        memory->read();
        vec->setToExternal(static_cast<uint8_t*>(static_cast<void*>(memory->getPointer())),
                           memory->getSize());
        return std::make_pair(true, memory);
    }
    ALOGE("%s: Could not map HIDL memory to IMemory", __func__);
    return std::make_pair(false, memory);
}

// Moves the data from the vector into allocated shared memory,
// emptying the vector.
// It is assumed that the passed hidl_memory is a null object, so it's
// not reset if the vector is empty.
// The caller needs to keep the returned sp<IMemory> as long as
// the data is needed.
std::pair<bool, sp<IMemory>> moveVectorToMemory(hidl_vec<uint8_t>* v, hidl_memory* mem) {
    sp<IMemory> memory;
    if (v->size() == 0) {
        return std::make_pair(true, memory);
    }
    sp<IAllocator> ashmem = IAllocator::getService("ashmem");
    if (ashmem == 0) {
        ALOGE("Failed to retrieve ashmem allocator service");
        return std::make_pair(false, memory);
    }
    bool success = false;
    Return<void> r = ashmem->allocate(v->size(), [&](bool s, const hidl_memory& m) {
        success = s;
        if (success) *mem = m;
    });
    if (r.isOk() && success) {
        memory = hardware::mapMemory(*mem);
        if (memory != 0) {
            memory->update();
            memcpy(memory->getPointer(), v->data(), v->size());
            memory->commit();
            v->resize(0);
            return std::make_pair(true, memory);
        } else {
            ALOGE("Failed to map allocated ashmem");
        }
    } else {
        ALOGE("Failed to allocate %llu bytes from ashmem", (unsigned long long)v->size());
    }
    return std::make_pair(false, memory);
}

}  // namespace

Return<void> SoundTriggerHw::loadSoundModel_2_1(
    const V2_1::ISoundTriggerHw::SoundModel& soundModel,
    const sp<V2_1::ISoundTriggerHwCallback>& callback, int32_t cookie,
    V2_1::ISoundTriggerHw::loadSoundModel_2_1_cb _hidl_cb) {
    // It is assumed that legacy data vector is empty, thus making copy is cheap.
    V2_0::ISoundTriggerHw::SoundModel soundModel_2_0(soundModel.header);
    auto result = memoryAsVector(soundModel.data, &soundModel_2_0.data);
    if (result.first) {
        sp<SoundModelClient> client =
            new SoundModelClient_2_1(nextUniqueModelId(), cookie, callback);
        _hidl_cb(doLoadSoundModel(soundModel_2_0, client), client->getId());
        return Void();
    }
    _hidl_cb(-ENOMEM, 0);
    return Void();
}

Return<void> SoundTriggerHw::loadPhraseSoundModel_2_1(
    const V2_1::ISoundTriggerHw::PhraseSoundModel& soundModel,
    const sp<V2_1::ISoundTriggerHwCallback>& callback, int32_t cookie,
    V2_1::ISoundTriggerHw::loadPhraseSoundModel_2_1_cb _hidl_cb) {
    V2_0::ISoundTriggerHw::PhraseSoundModel soundModel_2_0;
    // It is assumed that legacy data vector is empty, thus making copy is cheap.
    soundModel_2_0.common = soundModel.common.header;
    // Avoid copying phrases data.
    soundModel_2_0.phrases.setToExternal(
        const_cast<V2_0::ISoundTriggerHw::Phrase*>(soundModel.phrases.data()),
        soundModel.phrases.size());
    auto result = memoryAsVector(soundModel.common.data, &soundModel_2_0.common.data);
    if (result.first) {
        sp<SoundModelClient> client =
            new SoundModelClient_2_1(nextUniqueModelId(), cookie, callback);
        _hidl_cb(doLoadSoundModel((const V2_0::ISoundTriggerHw::SoundModel&)soundModel_2_0, client),
                 client->getId());
        return Void();
    }
    _hidl_cb(-ENOMEM, 0);
    return Void();
}

Return<int32_t> SoundTriggerHw::startRecognition_2_1(
    int32_t modelHandle, const V2_1::ISoundTriggerHw::RecognitionConfig& config) {
    // It is assumed that legacy data vector is empty, thus making copy is cheap.
    V2_0::ISoundTriggerHw::RecognitionConfig config_2_0(config.header);
    auto result = memoryAsVector(config.data, &config_2_0.data);
    return result.first ? startRecognition(modelHandle, config_2_0) : Return<int32_t>(-ENOMEM);
}

void SoundTriggerHw::SoundModelClient_2_1::recognitionCallback(
    struct sound_trigger_recognition_event* halEvent) {
    if (halEvent->type == SOUND_MODEL_TYPE_KEYPHRASE) {
        V2_0::ISoundTriggerHwCallback::PhraseRecognitionEvent event_2_0;
        convertPhaseRecognitionEventFromHal(
            &event_2_0, reinterpret_cast<sound_trigger_phrase_recognition_event*>(halEvent));
        event_2_0.common.model = mId;
        V2_1::ISoundTriggerHwCallback::PhraseRecognitionEvent event;
        event.phraseExtras.setToExternal(event_2_0.phraseExtras.data(),
                                         event_2_0.phraseExtras.size());
        auto result = moveVectorToMemory(&event_2_0.common.data, &event.common.data);
        if (result.first) {
            // The data vector is now empty, thus copying is cheap.
            event.common.header = event_2_0.common;
            mCallback->phraseRecognitionCallback_2_1(event, mCookie);
        }
    } else {
        V2_1::ISoundTriggerHwCallback::RecognitionEvent event;
        convertRecognitionEventFromHal(&event.header, halEvent);
        event.header.model = mId;
        auto result = moveVectorToMemory(&event.header.data, &event.data);
        if (result.first) {
            mCallback->recognitionCallback_2_1(event, mCookie);
        }
    }
}

void SoundTriggerHw::SoundModelClient_2_1::soundModelCallback(
    struct sound_trigger_model_event* halEvent) {
    V2_1::ISoundTriggerHwCallback::ModelEvent event;
    convertSoundModelEventFromHal(&event.header, halEvent);
    event.header.model = mId;
    auto result = moveVectorToMemory(&event.header.data, &event.data);
    if (result.first) {
        mCallback->soundModelCallback_2_1(event, mCookie);
    }
}

ISoundTriggerHw* HIDL_FETCH_ISoundTriggerHw(const char* /* name */) {
    return (new SoundTriggerHw())->getInterface();
}

}  // namespace implementation
}  // namespace V2_1
}  // namespace soundtrigger
}  // namespace hardware
}  // namespace android
+152 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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_SOUNDTRIGGER_V2_1_SOUNDTRIGGERHW_H
#define ANDROID_HARDWARE_SOUNDTRIGGER_V2_1_SOUNDTRIGGERHW_H

#include <SoundTriggerHalImpl.h>
#include <android/hardware/soundtrigger/2.1/ISoundTriggerHw.h>
#include <hidl/Status.h>

namespace android {
namespace hardware {
namespace soundtrigger {
namespace V2_1 {
namespace implementation {

using ::android::sp;
using ::android::hardware::hidl_string;
using ::android::hardware::hidl_vec;
using ::android::hardware::Return;
using ::android::hardware::Void;

struct SoundTriggerHw : public V2_0::implementation::SoundTriggerHalImpl {
    SoundTriggerHw() = default;
    ISoundTriggerHw* getInterface() { return new TrampolineSoundTriggerHw_2_1(this); }

   protected:
    virtual ~SoundTriggerHw() = default;

    Return<void> loadSoundModel_2_1(const V2_1::ISoundTriggerHw::SoundModel& soundModel,
                                    const sp<V2_1::ISoundTriggerHwCallback>& callback,
                                    int32_t cookie,
                                    V2_1::ISoundTriggerHw::loadSoundModel_2_1_cb _hidl_cb);
    Return<void> loadPhraseSoundModel_2_1(
        const V2_1::ISoundTriggerHw::PhraseSoundModel& soundModel,
        const sp<V2_1::ISoundTriggerHwCallback>& callback, int32_t cookie,
        V2_1::ISoundTriggerHw::loadPhraseSoundModel_2_1_cb _hidl_cb);
    Return<int32_t> startRecognition_2_1(int32_t modelHandle,
                                         const V2_1::ISoundTriggerHw::RecognitionConfig& config);

   private:
    struct TrampolineSoundTriggerHw_2_1 : public ISoundTriggerHw {
        explicit TrampolineSoundTriggerHw_2_1(sp<SoundTriggerHw> impl) : mImpl(impl) {}

        // Methods from ::android::hardware::soundtrigger::V2_0::ISoundTriggerHw follow.
        Return<void> getProperties(getProperties_cb _hidl_cb) override {
            return mImpl->getProperties(_hidl_cb);
        }
        Return<void> loadSoundModel(const V2_0::ISoundTriggerHw::SoundModel& soundModel,
                                    const sp<V2_0::ISoundTriggerHwCallback>& callback,
                                    int32_t cookie, loadSoundModel_cb _hidl_cb) override {
            return mImpl->loadSoundModel(soundModel, callback, cookie, _hidl_cb);
        }
        Return<void> loadPhraseSoundModel(const V2_0::ISoundTriggerHw::PhraseSoundModel& soundModel,
                                          const sp<V2_0::ISoundTriggerHwCallback>& callback,
                                          int32_t cookie,
                                          loadPhraseSoundModel_cb _hidl_cb) override {
            return mImpl->loadPhraseSoundModel(soundModel, callback, cookie, _hidl_cb);
        }
        Return<int32_t> unloadSoundModel(V2_0::SoundModelHandle modelHandle) override {
            return mImpl->unloadSoundModel(modelHandle);
        }
        Return<int32_t> startRecognition(int32_t modelHandle,
                                         const V2_0::ISoundTriggerHw::RecognitionConfig& config,
                                         const sp<V2_0::ISoundTriggerHwCallback>& /*callback*/,
                                         int32_t /*cookie*/) override {
            return mImpl->startRecognition(modelHandle, config);
        }
        Return<int32_t> stopRecognition(V2_0::SoundModelHandle modelHandle) override {
            return mImpl->stopRecognition(modelHandle);
        }
        Return<int32_t> stopAllRecognitions() override { return mImpl->stopAllRecognitions(); }

        // Methods from V2_1::ISoundTriggerHw follow.
        Return<void> loadSoundModel_2_1(const V2_1::ISoundTriggerHw::SoundModel& soundModel,
                                        const sp<V2_1::ISoundTriggerHwCallback>& callback,
                                        int32_t cookie, loadSoundModel_2_1_cb _hidl_cb) override {
            return mImpl->loadSoundModel_2_1(soundModel, callback, cookie, _hidl_cb);
        }
        Return<void> loadPhraseSoundModel_2_1(
            const V2_1::ISoundTriggerHw::PhraseSoundModel& soundModel,
            const sp<V2_1::ISoundTriggerHwCallback>& callback, int32_t cookie,
            loadPhraseSoundModel_2_1_cb _hidl_cb) override {
            return mImpl->loadPhraseSoundModel_2_1(soundModel, callback, cookie, _hidl_cb);
        }
        Return<int32_t> startRecognition_2_1(int32_t modelHandle,
                                             const V2_1::ISoundTriggerHw::RecognitionConfig& config,
                                             const sp<V2_1::ISoundTriggerHwCallback>& /*callback*/,
                                             int32_t /*cookie*/) override {
            return mImpl->startRecognition_2_1(modelHandle, config);
        }
        Return<void> getParameters(const hidl_vec<hidl_string>& /*keys*/,
                                   getParameters_cb _hidl_cb) override {
            _hidl_cb(-ENOSYS, hidl_vec<ParameterValue>());
            return Void();
        }
        Return<int32_t> setParameters(
            const hidl_vec<V2_1::ISoundTriggerHw::ParameterValue>& /*parameters*/) override {
            return -ENOSYS;
        }
        Return<void> getSoundModelParameters(int32_t /*modelHandle*/,
                                             const hidl_vec<hidl_string>& /*keys*/,
                                             getSoundModelParameters_cb _hidl_cb) override {
            _hidl_cb(-ENOSYS, hidl_vec<ParameterValue>());
            return Void();
        }
        Return<int32_t> setSoundModelParameters(
            int32_t /*modelHandle*/,
            const hidl_vec<V2_1::ISoundTriggerHw::ParameterValue>& /*parameters*/) override {
            return -ENOSYS;
        }

       private:
        sp<SoundTriggerHw> mImpl;
    };

    class SoundModelClient_2_1 : public SoundModelClient {
       public:
        SoundModelClient_2_1(uint32_t id, V2_1::ISoundTriggerHwCallback::CallbackCookie cookie,
                             sp<V2_1::ISoundTriggerHwCallback> callback)
            : SoundModelClient(id, cookie), mCallback(callback) {}

        void recognitionCallback(struct sound_trigger_recognition_event* halEvent) override;
        void soundModelCallback(struct sound_trigger_model_event* halEvent) override;

       private:
        sp<V2_1::ISoundTriggerHwCallback> mCallback;
    };
};

extern "C" ISoundTriggerHw* HIDL_FETCH_ISoundTriggerHw(const char* name);

}  // namespace implementation
}  // namespace V2_1
}  // namespace soundtrigger
}  // namespace hardware
}  // namespace android

#endif  // ANDROID_HARDWARE_SOUNDTRIGGER_V2_1_SOUNDTRIGGERHW_H