summaryrefslogtreecommitdiff
path: root/suite/audio_quality/lib/src/task/TaskProcess.cpp
blob: 8930e0119907b7933a71c918f804d42bd3adf245 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
#include <stdlib.h>
#include <string.h>

#include <vector>

#include "Log.h"
#include "StringUtil.h"
#include "task/TaskProcess.h"
#include "SignalProcessingImpl.h"

TaskProcess::TaskProcess()
    : TaskGeneric(TaskGeneric::ETaskProcess)
{

}

TaskProcess::~TaskProcess()
{
}

TaskGeneric::ExecutionResult TaskProcess::run()
{
    if (mType == EBuiltin) {
        return doRun(true);
    } else {
        if (mSp.get() == NULL) {
            mSp.reset(new SignalProcessingImpl());
            if (!mSp->init(SignalProcessingImpl::MAIN_PROCESSING_SCRIPT)) {
                mSp.reset(NULL);
                return TaskGeneric::EResultError;
            }
        }
        return doRun(false);
    }
}

// Allocate Buffers and Values to pass to builtin functions
bool TaskProcess::prepareParams(std::vector<TaskProcess::Param>& list,
        const bool* paramTypes,
        UniquePtr<void_ptr, DefaultDelete<void_ptr[]> > & ptrs,
        UniquePtr<UniqueValue, DefaultDelete<UniqueValue[]> > & values,
        UniquePtr<UniqueBuffer, DefaultDelete<UniqueBuffer[]> > & buffers,
        bool isInput)
{
    size_t N = list.size();

    LOGD("TaskProcess::prepareParams N = %d", N);
    ptrs.reset(new void_ptr[N]);
    if (ptrs.get() == NULL) {
        LOGE("alloc failed");
        return false;
    }
    // set to NULL to detect illegal access
    bzero(ptrs.get(), N * sizeof(void_ptr));
    values.reset(new UniqueValue[N]);
    if (values.get() == NULL) {
        LOGE("alloc failed");
        return false;
    }
    buffers.reset(new UniqueBuffer[N]);
    if (buffers.get() == NULL) {
        LOGE("alloc failed");
        return false;
    }

    void_ptr* voidPtrs = ptrs.get();
    UniqueValue* valuesPtr = values.get();
    UniqueBuffer* buffersPtr = buffers.get();
    for (size_t i = 0; i < N; i++) {
        if ((paramTypes != NULL) && paramTypes[i] && (list[i].getType() != EId)) {
            LOGE("mismatching types %d %d", paramTypes[i], list[i].getType());
            return false;
        }
        if ((paramTypes != NULL) && !paramTypes[i] && (list[i].getType() == EId)) {
            LOGE("mismatching types %d %d", paramTypes[i], list[i].getType());
            return false;
        }
        switch(list[i].getType()) {
        case EId: {
            UniquePtr<android::sp<Buffer> > buffer(new android::sp<Buffer>());
            if (buffer.get() == NULL) {
                LOGE("alloc failed");
                return false;
            }
            if (isInput) {
                *(buffer.get()) = getTestCase()->findBuffer(list[i].getParamString());
                if (buffer.get()->get() == NULL) {
                    LOGE("find failed");
                    return false;
                }
                LOGD("input buffer len %d stereo %d", (*buffer.get())->getSize(),
                        (*buffer.get())->isStereo());
            }
            buffersPtr[i].reset(buffer.release());
            voidPtrs[i] = buffersPtr[i].get();
        }
        break;
        case EVal: {
            valuesPtr[i].reset(new TaskCase::Value());
            if (isInput) {
                if (!getTestCase()->findValue(list[i].getParamString(), *(valuesPtr[i].get()))) {
                    LOGE("find %s failed", list[i].getParamString().string());
                    return false;
                }
            }
            voidPtrs[i] = valuesPtr[i].get();
        }
        break;
        case EConst: {
            if (!isInput) {
                LOGE("const for output");
                return false;
            }
            voidPtrs[i] = list[i].getValuePtr();

            if (list[i].getValue().getType() == TaskCase::Value::ETypeDouble) {
                LOGD(" %f", list[i].getValue().getDouble());
            } else {
                LOGD(" %lld", list[i].getValue().getInt64());
            }
        }
        break;
        }
        LOGD("TaskProcess::prepareParams %d-th, const 0x%x", i, voidPtrs[i]);
    }
    return true;
}

// run builtin function by searching BuiltinProcessing::BUINTIN_FN_TABLE
TaskGeneric::ExecutionResult TaskProcess::doRun(bool builtIn)
{
    BuiltinProcessing::BuiltinInfo* info = NULL;
    if (builtIn) {
        for (int i = 0; i < BuiltinProcessing::N_BUILTIN_FNS; i++) {
            if (StringUtil::compare(mName, BuiltinProcessing::BUINTIN_FN_TABLE[i].mName) == 0) {
                info = &BuiltinProcessing::BUINTIN_FN_TABLE[i];
                break;
            }
        }
        if (info == NULL) {
            LOGE("TaskProcess::runBuiltin no match for %s", mName.string());
            return TaskGeneric::EResultError;
        }
        if (mInput.size() != info->mNInput) {
            LOGE("TaskProcess::runBuiltin size mismatch %d vs %d", mInput.size(), info->mNInput);
            return TaskGeneric::EResultError;
        }
        if (mOutput.size() != info->mNOutput) {
            LOGE("TaskProcess::runBuiltin size mismatch %d vs %d", mOutput.size(), info->mNOutput);
            return TaskGeneric::EResultError;
        }
    }
    // This is for passing to builtin fns. Just void pts will be cleared in exit
    UniquePtr<void_ptr, DefaultDelete<void_ptr[]> > inputs;
    // This is for holding Value instances. Will be destroyed in exit
    UniquePtr<UniqueValue, DefaultDelete<UniqueValue[]> > inputValues;
    // This is for holding android::sp<Buffer>. Buffer itself is from the global map.
    UniquePtr<UniqueBuffer, DefaultDelete<UniqueBuffer[]> > inputBuffers;

    UniquePtr<void_ptr, DefaultDelete<void_ptr[]> > outputs;
    // Value is created here. Builtin function just need to set it.
    UniquePtr<UniqueValue, DefaultDelete<UniqueValue[]> > outputValues;
    // Buffer itself should be allocated by the builtin function itself.
    UniquePtr<UniqueBuffer, DefaultDelete<UniqueBuffer[]> > outputBuffers;

    if (!prepareParams(mInput, builtIn ? info->mInputTypes : NULL, inputs, inputValues,
            inputBuffers, true)) {
        return TaskGeneric::EResultError;
    }

    if (!prepareParams(mOutput, builtIn ? info->mOutputTypes : NULL, outputs, outputValues,
            outputBuffers, false)) {
        return TaskGeneric::EResultError;
    }

    TaskGeneric::ExecutionResult result;
    if (builtIn) {
        result = (mBuiltin.*(info->mFunction))(inputs.get(), outputs.get());
    } else {
        UniquePtr<bool, DefaultDelete<bool[]> > inputTypes(new bool[mInput.size()]);
        for (size_t i = 0; i < mInput.size(); i++) {
            (inputTypes.get())[i] = mInput[i].isIdType();
        }
        UniquePtr<bool, DefaultDelete<bool[]> > outputTypes(new bool[mOutput.size()]);
        for (size_t i = 0; i < mOutput.size(); i++) {
            (outputTypes.get())[i] = mOutput[i].isIdType();
        }
        result = mSp->run( mName,
                mInput.size(), inputTypes.get(), inputs.get(),
                mOutput.size(), outputTypes.get(), outputs.get());
    }
    if ((result == TaskGeneric::EResultOK) || (result == TaskGeneric::EResultFail)
            || (result == TaskGeneric::EResultPass)) {
        // try to save result
        bool saveResultFailed = false;
        for (size_t i = 0; i < mOutput.size(); i++) {
            if (mOutput[i].isIdType()) { // Buffer
                android::sp<Buffer>* bufferp =
                        reinterpret_cast<android::sp<Buffer>*>((outputs.get())[i]);
                if (!getTestCase()->registerBuffer(mOutput[i].getParamString(), *bufferp)) {
                    // maybe already there, try update
                    if (!getTestCase()->updateBuffer(mOutput[i].getParamString(), *bufferp)) {
                        LOGE("cannot register / update %d-th output Buffer for builtin fn %s",
                                i, mName.string());
                        saveResultFailed = true; // mark failure, but continue
                    }
                }
            } else { // Value
                TaskCase::Value* valuep =
                        reinterpret_cast<TaskCase::Value*>((outputs.get())[i]);
                if (!getTestCase()->registerValue(mOutput[i].getParamString(), *valuep)) {
                    if (!getTestCase()->updateValue(mOutput[i].getParamString(), *valuep)) {
                        LOGE("cannot register / update %d-th output Value for builtin fn %s",
                                i, mName.string());
                        saveResultFailed = true; // mark failure, but continue
                    }
                }
            }
        }
        if (saveResultFailed) {
            LOGE("TaskProcess::runBuiltin cannot save result");
            return TaskGeneric::EResultError;
        }
    }
    LOGV("TaskProcess::runBuiltin return %d", result);
    return result;
}

bool TaskProcess::parseParams(std::vector<TaskProcess::Param>& list, const char* str, bool isInput)
{
    LOGV("TaskProcess::parseParams will parse %s", str);
    android::String8 paramStr(str);
    UniquePtr<std::vector<android::String8> > paramTokens(StringUtil::split(paramStr, ','));
    if (paramTokens.get() == NULL) {
        LOGE("split failed");
        return false;
    }
    std::vector<android::String8>& tokens = *(paramTokens.get());
    for (size_t i = 0; i < tokens.size(); i++) {
        UniquePtr<std::vector<android::String8> > itemTokens(StringUtil::split(tokens[i], ':'));
        if (itemTokens.get() == NULL) {
            LOGE("split failed");
            return false;
        }
        if (itemTokens->size() != 2) {
            LOGE("size mismatch %d", itemTokens->size());
            return false;
        }
        std::vector<android::String8>& item = *(itemTokens.get());
        if (StringUtil::compare(item[0], "id") == 0) {
            Param param(EId, item[1]);
            list.push_back(param);
            LOGD(" id %s", param.getParamString().string());
        } else if (StringUtil::compare(item[0], "val") == 0) {
            Param param(EVal, item[1]);
            list.push_back(param);
            LOGD(" val %s", param.getParamString().string());
        } else if (isInput && (StringUtil::compare(item[0], "consti") == 0)) {
            int64_t value = (int64_t)atoll(item[1].string());
            TaskCase::Value v(value);
            Param param(v);
            list.push_back(param);
            LOGD("consti %lld", value);
        } else if (isInput && (StringUtil::compare(item[0], "constf") == 0)) {
            double value = atof(item[1].string());
            TaskCase::Value v(value);
            Param param(v);
            list.push_back(param);
            LOGD("constf %f", value);
        } else {
            LOGE("unrecognized word %s", item[0].string());
            return false;
        }
        LOGV("TaskProcess::parseParams %d-th type %d", i, list[i].getType());
    }
   return true;
}

bool TaskProcess::parseAttribute(const android::String8& name, const android::String8& value)
{
    if (StringUtil::compare(name, "method") == 0) {
        UniquePtr<std::vector<android::String8> > tokenPtr(StringUtil::split(value, ':'));
        std::vector<android::String8>* tokens = tokenPtr.get();
        if (tokens == NULL) {
            LOGE("split failed");
            return false;
        }
        if (tokens->size() != 2) {
            LOGE("cannot parse attr %s %s", name.string(), value.string());
            return false;
        }
        if (StringUtil::compare(tokens->at(0), "builtin") == 0) {
            mType = EBuiltin;
        } else if (StringUtil::compare(tokens->at(0), "script") == 0) {
            mType = EScript;
        } else {
            LOGE("cannot parse attr %s %s", name.string(), value.string());
            return false;
        }
        mName.append(tokens->at(1));
        return true;
    } else if (StringUtil::compare(name, "input") == 0) {
        return parseParams(mInput, value, true);
    } else if (StringUtil::compare(name, "output") == 0) {
        return parseParams(mOutput, value, false);
    } else {
        LOGE("cannot parse attr %s %s", name.string(), value.string());
        return false;
    }
}

TaskProcess::Param::Param(TaskProcess::ParamType type, android::String8& string)
    : mType(type),
      mString(string)
{
    ASSERT((type == TaskProcess::EId) || (type == TaskProcess::EVal));

}

TaskProcess::Param::Param(TaskCase::Value& val)
    : mType(TaskProcess::EConst),
      mValue(val)
{

}

TaskProcess::ParamType TaskProcess::Param::getType()
{
    return mType;
}

android::String8& TaskProcess::Param::getParamString()
{
    ASSERT((mType == TaskProcess::EId) || (mType == TaskProcess::EVal));
    return mString;
}

TaskCase::Value& TaskProcess::Param::getValue()
{
    ASSERT(mType == TaskProcess::EConst);
    return mValue;
}

TaskCase::Value* TaskProcess::Param::getValuePtr()
{
    ASSERT(mType == TaskProcess::EConst);
    return &mValue;
}