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