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