EffectsFactoryHalHidl.cpp revision dfa54faf298f6b617bf1d7684418fc8a2e6e4858
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 "EffectsFactoryHalHidl"
18//#define LOG_NDEBUG 0
19
20#include <android/hidl/allocator/1.0/IAllocator.h>
21#include <cutils/native_handle.h>
22#include <hidl/ServiceManagement.h>
23#include <media/EffectsFactoryApi.h>
24
25#include "ConversionHelperHidl.h"
26#include "EffectHalHidl.h"
27#include "EffectsFactoryHalHidl.h"
28#include "HidlUtils.h"
29
30using ::android::hardware::audio::common::V2_0::Uuid;
31using ::android::hardware::audio::effect::V2_0::IEffect;
32using ::android::hardware::audio::effect::V2_0::Result;
33using ::android::hardware::Return;
34using ::android::hardware::Status;
35
36namespace android {
37
38// static
39sp<EffectsFactoryHalInterface> EffectsFactoryHalInterface::create() {
40    return new EffectsFactoryHalHidl();
41}
42
43// static
44bool EffectsFactoryHalInterface::isNullUuid(const effect_uuid_t *pEffectUuid) {
45    return EffectIsNullUuid(pEffectUuid);
46}
47
48EffectsFactoryHalHidl::EffectsFactoryHalHidl() : ConversionHelperHidl("EffectsFactory") {
49    mEffectsFactory = IEffectsFactory::getService();
50    // TODO: Waiting should not be needed (b/34772726).
51    // Also remove include of IAllocator.h and ServiceManagement.h
52    android::hardware::details::waitForHwService(
53            hidl::allocator::V1_0::IAllocator::descriptor, "ashmem");
54}
55
56EffectsFactoryHalHidl::~EffectsFactoryHalHidl() {
57}
58
59status_t EffectsFactoryHalHidl::queryAllDescriptors() {
60    if (mEffectsFactory == 0) return NO_INIT;
61    Result retval = Result::NOT_INITIALIZED;
62    Return<void> ret = mEffectsFactory->getAllDescriptors(
63            [&](Result r, const hidl_vec<EffectDescriptor>& result) {
64                retval = r;
65                if (retval == Result::OK) {
66                    mLastDescriptors = result;
67                }
68            });
69    if (ret.isOk()) {
70        return retval == Result::OK ? OK : NO_INIT;
71    }
72    mLastDescriptors.resize(0);
73    return processReturn(__FUNCTION__, ret);
74}
75
76status_t EffectsFactoryHalHidl::queryNumberEffects(uint32_t *pNumEffects) {
77    status_t queryResult = queryAllDescriptors();
78    if (queryResult == OK) {
79        *pNumEffects = mLastDescriptors.size();
80    }
81    return queryResult;
82}
83
84status_t EffectsFactoryHalHidl::getDescriptor(
85        uint32_t index, effect_descriptor_t *pDescriptor) {
86    // TODO: We need somehow to track the changes on the server side
87    // or figure out how to convert everybody to query all the descriptors at once.
88    // TODO: check for nullptr
89    if (mLastDescriptors.size() == 0) {
90        status_t queryResult = queryAllDescriptors();
91        if (queryResult != OK) return queryResult;
92    }
93    if (index >= mLastDescriptors.size()) return NAME_NOT_FOUND;
94    EffectHalHidl::effectDescriptorToHal(mLastDescriptors[index], pDescriptor);
95    return OK;
96}
97
98status_t EffectsFactoryHalHidl::getDescriptor(
99        const effect_uuid_t *pEffectUuid, effect_descriptor_t *pDescriptor) {
100    // TODO: check for nullptr
101    if (mEffectsFactory == 0) return NO_INIT;
102    Uuid hidlUuid;
103    HidlUtils::uuidFromHal(*pEffectUuid, &hidlUuid);
104    Result retval = Result::NOT_INITIALIZED;
105    Return<void> ret = mEffectsFactory->getDescriptor(hidlUuid,
106            [&](Result r, const EffectDescriptor& result) {
107                retval = r;
108                if (retval == Result::OK) {
109                    EffectHalHidl::effectDescriptorToHal(result, pDescriptor);
110                }
111            });
112    if (ret.isOk()) {
113        if (retval == Result::OK) return OK;
114        else if (retval == Result::INVALID_ARGUMENTS) return NAME_NOT_FOUND;
115        else return NO_INIT;
116    }
117    return processReturn(__FUNCTION__, ret);
118}
119
120status_t EffectsFactoryHalHidl::createEffect(
121        const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t ioId,
122        sp<EffectHalInterface> *effect) {
123    if (mEffectsFactory == 0) return NO_INIT;
124    Uuid hidlUuid;
125    HidlUtils::uuidFromHal(*pEffectUuid, &hidlUuid);
126    Result retval = Result::NOT_INITIALIZED;
127    Return<void> ret = mEffectsFactory->createEffect(
128            hidlUuid, sessionId, ioId,
129            [&](Result r, const sp<IEffect>& result, uint64_t effectId) {
130                retval = r;
131                if (retval == Result::OK) {
132                    *effect = new EffectHalHidl(result, effectId);
133                }
134            });
135    if (ret.isOk()) {
136        if (retval == Result::OK) return OK;
137        else if (retval == Result::INVALID_ARGUMENTS) return NAME_NOT_FOUND;
138        else return NO_INIT;
139    }
140    return processReturn(__FUNCTION__, ret);
141}
142
143status_t EffectsFactoryHalHidl::dumpEffects(int fd) {
144    if (mEffectsFactory == 0) return NO_INIT;
145    native_handle_t* hidlHandle = native_handle_create(1, 0);
146    hidlHandle->data[0] = fd;
147    Return<void> ret = mEffectsFactory->debugDump(hidlHandle);
148    native_handle_delete(hidlHandle);
149    return processReturn(__FUNCTION__, ret);
150}
151
152} // namespace android
153