blob: f7dbb9c5d647ed8d3ee453dbf2e18e23889b14c4 [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
20#include <cutils/native_handle.h>
21#include <media/EffectsFactoryApi.h>
22
Mikhail Naganov9f57e3c2016-12-05 12:54:36 -080023#include "ConversionHelperHidl.h"
Mikhail Naganovf558e022016-11-14 17:45:17 -080024#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;
Mikhail Naganovf558e022016-11-14 17:45:17 -080032
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
Chris Phoenixdfa54fa2017-01-24 13:46:36 -080045EffectsFactoryHalHidl::EffectsFactoryHalHidl() : ConversionHelperHidl("EffectsFactory") {
46 mEffectsFactory = IEffectsFactory::getService();
Mikhail Naganov425a5022017-03-14 13:35:37 -070047 LOG_ALWAYS_FATAL_IF(mEffectsFactory == 0, "Failed to obtain IEffectsFactory service");
Mikhail Naganovf558e022016-11-14 17:45:17 -080048}
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 });
Steven Morelande83be8a2017-01-06 11:06:33 -080063 if (ret.isOk()) {
Mikhail Naganovf558e022016-11-14 17:45:17 -080064 return retval == Result::OK ? OK : NO_INIT;
65 }
66 mLastDescriptors.resize(0);
Steven Morelande83be8a2017-01-06 11:06:33 -080067 return processReturn(__FUNCTION__, ret);
Mikhail Naganovf558e022016-11-14 17:45:17 -080068}
69
70status_t EffectsFactoryHalHidl::queryNumberEffects(uint32_t *pNumEffects) {
71 status_t queryResult = queryAllDescriptors();
72 if (queryResult == OK) {
73 *pNumEffects = mLastDescriptors.size();
74 }
75 return queryResult;
76}
77
78status_t EffectsFactoryHalHidl::getDescriptor(
79 uint32_t index, effect_descriptor_t *pDescriptor) {
80 // TODO: We need somehow to track the changes on the server side
81 // or figure out how to convert everybody to query all the descriptors at once.
82 // TODO: check for nullptr
83 if (mLastDescriptors.size() == 0) {
84 status_t queryResult = queryAllDescriptors();
85 if (queryResult != OK) return queryResult;
86 }
87 if (index >= mLastDescriptors.size()) return NAME_NOT_FOUND;
88 EffectHalHidl::effectDescriptorToHal(mLastDescriptors[index], pDescriptor);
89 return OK;
90}
91
92status_t EffectsFactoryHalHidl::getDescriptor(
93 const effect_uuid_t *pEffectUuid, effect_descriptor_t *pDescriptor) {
94 // TODO: check for nullptr
95 if (mEffectsFactory == 0) return NO_INIT;
96 Uuid hidlUuid;
97 HidlUtils::uuidFromHal(*pEffectUuid, &hidlUuid);
98 Result retval = Result::NOT_INITIALIZED;
99 Return<void> ret = mEffectsFactory->getDescriptor(hidlUuid,
100 [&](Result r, const EffectDescriptor& result) {
101 retval = r;
102 if (retval == Result::OK) {
103 EffectHalHidl::effectDescriptorToHal(result, pDescriptor);
104 }
105 });
Steven Morelande83be8a2017-01-06 11:06:33 -0800106 if (ret.isOk()) {
Mikhail Naganovf558e022016-11-14 17:45:17 -0800107 if (retval == Result::OK) return OK;
108 else if (retval == Result::INVALID_ARGUMENTS) return NAME_NOT_FOUND;
109 else return NO_INIT;
110 }
Steven Morelande83be8a2017-01-06 11:06:33 -0800111 return processReturn(__FUNCTION__, ret);
Mikhail Naganovf558e022016-11-14 17:45:17 -0800112}
113
114status_t EffectsFactoryHalHidl::createEffect(
115 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t ioId,
116 sp<EffectHalInterface> *effect) {
117 if (mEffectsFactory == 0) return NO_INIT;
118 Uuid hidlUuid;
119 HidlUtils::uuidFromHal(*pEffectUuid, &hidlUuid);
120 Result retval = Result::NOT_INITIALIZED;
121 Return<void> ret = mEffectsFactory->createEffect(
122 hidlUuid, sessionId, ioId,
123 [&](Result r, const sp<IEffect>& result, uint64_t effectId) {
124 retval = r;
125 if (retval == Result::OK) {
126 *effect = new EffectHalHidl(result, effectId);
127 }
128 });
Steven Morelande83be8a2017-01-06 11:06:33 -0800129 if (ret.isOk()) {
Mikhail Naganovf558e022016-11-14 17:45:17 -0800130 if (retval == Result::OK) return OK;
131 else if (retval == Result::INVALID_ARGUMENTS) return NAME_NOT_FOUND;
132 else return NO_INIT;
133 }
Steven Morelande83be8a2017-01-06 11:06:33 -0800134 return processReturn(__FUNCTION__, ret);
Mikhail Naganovf558e022016-11-14 17:45:17 -0800135}
136
137status_t EffectsFactoryHalHidl::dumpEffects(int fd) {
138 if (mEffectsFactory == 0) return NO_INIT;
139 native_handle_t* hidlHandle = native_handle_create(1, 0);
140 hidlHandle->data[0] = fd;
141 Return<void> ret = mEffectsFactory->debugDump(hidlHandle);
142 native_handle_delete(hidlHandle);
Steven Morelande83be8a2017-01-06 11:06:33 -0800143 return processReturn(__FUNCTION__, ret);
Mikhail Naganovf558e022016-11-14 17:45:17 -0800144}
145
146} // namespace android