1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 #include <memory>
18 #include "Log.h"
19 #include "audio/AudioSignalFactory.h"
20 #include "audio/RemoteAudio.h"
21 #include "StringUtil.h"
22 #include "task/TaskCase.h"
23 #include "task/TaskSound.h"
24 
25 static const android::String8 STR_ID("id");
26 static const android::String8 STR_TYPE("type");
27 
TaskSound()28 TaskSound::TaskSound()
29     : TaskGeneric(TaskGeneric::ETaskSound),
30       mPreload(false)
31 {
32     const android::String8* list[] = {&STR_ID, &STR_TYPE, NULL};
33     registerSupportedStringAttributes(list);
34 }
35 
~TaskSound()36 TaskSound::~TaskSound()
37 {
38 
39 }
40 
parseAttribute(const android::String8 & name,const android::String8 & value)41 bool TaskSound::parseAttribute(const android::String8& name, const android::String8& value)
42 {
43     if (StringUtil::compare(name, "preload") == 0) {
44             if (StringUtil::compare(value, "1") == 0) {
45                 mPreload = true;
46             }
47             return true;
48     }
49     return TaskGeneric::parseAttribute(name, value);
50 }
51 
run()52 TaskGeneric::ExecutionResult TaskSound::run()
53 {
54     android::String8 id;
55     if (!findStringAttribute(STR_ID, id)) {
56         LOGE("TaskSound::run %s string not found", STR_ID.string());
57         return TaskGeneric::EResultError;
58     }
59     android::String8 type;
60     if (!findStringAttribute(STR_TYPE, type)) {
61         LOGE("TaskSound::run %s string not found", STR_TYPE.string());
62         return TaskGeneric::EResultError;
63     }
64     std::unique_ptr<std::vector<android::String8> > tokens(StringUtil::split(type, ':'));
65     if (tokens.get() == NULL) {
66         LOGE("alloc failed");
67         return TaskGeneric::EResultError;
68     }
69     android::sp<Buffer> buffer;
70     if (StringUtil::compare(tokens->at(0), "file") == 0) {
71         if (tokens->size() != 2) {
72             LOGE("Wrong number of parameters %d", tokens->size());
73         }
74         buffer = Buffer::loadFromFile(tokens->at(1));
75     } else if (StringUtil::compare(tokens->at(0), "sin") == 0) {
76         if (tokens->size() != 4) {
77             LOGE("Wrong number of parameters %d", tokens->size());
78         }
79         int amplitude = atoi(tokens->at(1).string());
80         int freq = atoi(tokens->at(2).string());
81         int time = atoi(tokens->at(3).string());
82         int samples = time * AudioHardware::ESampleRate_44100 / 1000;
83         buffer = AudioSignalFactory::generateSineWave(AudioHardware::E2BPS, amplitude,
84                 AudioHardware::ESampleRate_44100, freq, samples, true);
85     } else if (StringUtil::compare(tokens->at(0), "random") == 0) {
86         // TODO FIXME it does not seem to work well.
87         if (tokens->size() != 3) {
88             LOGE("Wrong number of parameters %d", tokens->size());
89         }
90         int amplitude = atoi(tokens->at(1).string());
91         int time = atoi(tokens->at(2).string());
92         int samples = time * AudioHardware::ESampleRate_44100 / 1000;
93         buffer = AudioSignalFactory::generateWhiteNoise(AudioHardware::E2BPS, amplitude,
94                 samples, true);
95     } else { // unknown word
96         LOGE("TaskSound::run unknown word in type %s", type.string());
97         // next buffer check will return
98     }
99 
100     if (buffer.get() == NULL) {
101         return TaskGeneric::EResultError;
102     }
103     if (!getTestCase()->registerBuffer(id, buffer)) {
104         LOGE("TaskSound::run registering buffer %s failed", id.string());
105         return TaskGeneric::EResultError;
106     }
107     if (mPreload) {
108         int downloadId;
109         if (!getTestCase()->getRemoteAudio()->downloadData(id, buffer, downloadId)) {
110             return TaskGeneric::EResultError;
111         }
112         LOGI("Downloaded buffer %s to DUT with id %d", id.string(), downloadId);
113     }
114     return TaskGeneric::EResultOK;
115 }
116 
117 
118 
119