blob: 6ad23bc092da229666ded7b7cee86805d4d9026e [file] [log] [blame]
Mikhail Naganovf558e022016-11-14 17:45:17 -08001/*
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
Steven Moreland13a92682017-02-17 12:22:38 -080020#include <android/hidl/allocator/1.0/IAllocator.h>
Mikhail Naganovf558e022016-11-14 17:45:17 -080021#include <cutils/native_handle.h>
Mikhail Naganovc2656e82017-01-27 16:10:09 -080022#include <hidl/ServiceManagement.h>
Mikhail Naganovf558e022016-11-14 17:45:17 -080023#include <media/EffectsFactoryApi.h>
24
Mikhail Naganov9f57e3c2016-12-05 12:54:36 -080025#include "ConversionHelperHidl.h"
Mikhail Naganovf558e022016-11-14 17:45:17 -080026#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
Chris Phoenixdfa54fa2017-01-24 13:46:36 -080048EffectsFactoryHalHidl::EffectsFactoryHalHidl() : ConversionHelperHidl("EffectsFactory") {
49 mEffectsFactory = IEffectsFactory::getService();
Mikhail Naganovc2656e82017-01-27 16:10:09 -080050 // TODO: Waiting should not be needed (b/34772726).
51 // Also remove include of IAllocator.h and ServiceManagement.h
52 android::hardware::details::waitForHwService(
Steven Moreland13a92682017-02-17 12:22:38 -080053 hidl::allocator::V1_0::IAllocator::descriptor, "ashmem");
Mikhail Naganovf558e022016-11-14 17:45:17 -080054}
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 });
Steven Morelande83be8a2017-01-06 11:06:33 -080069 if (ret.isOk()) {
Mikhail Naganovf558e022016-11-14 17:45:17 -080070 return retval == Result::OK ? OK : NO_INIT;
71 }
72 mLastDescriptors.resize(0);
Steven Morelande83be8a2017-01-06 11:06:33 -080073 return processReturn(__FUNCTION__, ret);
Mikhail Naganovf558e022016-11-14 17:45:17 -080074}
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 });
Steven Morelande83be8a2017-01-06 11:06:33 -0800112 if (ret.isOk()) {
Mikhail Naganovf558e022016-11-14 17:45:17 -0800113 if (retval == Result::OK) return OK;
114 else if (retval == Result::INVALID_ARGUMENTS) return NAME_NOT_FOUND;
115 else return NO_INIT;
116 }
Steven Morelande83be8a2017-01-06 11:06:33 -0800117 return processReturn(__FUNCTION__, ret);
Mikhail Naganovf558e022016-11-14 17:45:17 -0800118}
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 });
Steven Morelande83be8a2017-01-06 11:06:33 -0800135 if (ret.isOk()) {
Mikhail Naganovf558e022016-11-14 17:45:17 -0800136 if (retval == Result::OK) return OK;
137 else if (retval == Result::INVALID_ARGUMENTS) return NAME_NOT_FOUND;
138 else return NO_INIT;
139 }
Steven Morelande83be8a2017-01-06 11:06:33 -0800140 return processReturn(__FUNCTION__, ret);
Mikhail Naganovf558e022016-11-14 17:45:17 -0800141}
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);
Steven Morelande83be8a2017-01-06 11:06:33 -0800149 return processReturn(__FUNCTION__, ret);
Mikhail Naganovf558e022016-11-14 17:45:17 -0800150}
151
152} // namespace android