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 "task/TaskCase.h" 18 #include "StringUtil.h" 19 #include "task/TaskOutput.h" 20 #include "audio/AudioRemote.h" 21 #include "audio/RemoteAudio.h" 22 23 24 TaskOutput::TaskOutput() 25 : TaskAsync(TaskGeneric::ETaskOutput), 26 mWaitForCompletion(false) 27 { 28 29 } 30 31 TaskOutput::~TaskOutput() 32 { 33 34 } 35 bool TaskOutput::parseAttribute(const android::String8& name, const android::String8& value) 36 { 37 if (StringUtil::compare(name, "waitforcompletion") == 0) { 38 if (StringUtil::compare(value, "1") == 0) { 39 mWaitForCompletion = true; 40 } 41 return true; 42 } 43 return TaskAsync::parseAttribute(name, value); 44 } 45 TaskGeneric::ExecutionResult TaskOutput::start() 46 { 47 bool localDevice = (mDeviceType == TaskAsync::EDeviceHost); 48 android::sp<AudioHardware> hw = AudioHardware::createAudioHw(localDevice, true, getTestCase()); 49 if (hw.get() == NULL) { 50 LOGE("cannot create Audio HW"); 51 return TaskGeneric::EResultError; 52 } 53 if (!hw->prepare(AudioHardware::ESampleRate_44100, mVolume, mMode)) { 54 LOGE("prepare failed"); 55 return TaskGeneric::EResultError; 56 } 57 android::sp<Buffer> buffer = getTestCase()->findBuffer(mId); 58 if (buffer.get() == NULL) { 59 LOGE("cannot find buffer %s", mId.string()); 60 return TaskGeneric::EResultError; 61 } 62 buffer->restart(); // reset to play from beginning 63 if (localDevice) { 64 if (!hw->startPlaybackOrRecord(buffer)) { 65 LOGE("play failed"); 66 return TaskGeneric::EResultError; 67 } 68 } else { 69 int id = getTestCase()->getRemoteAudio()->getDataId(mId); 70 if (id < 0) { 71 return TaskGeneric::EResultError; 72 } 73 AudioRemotePlayback* remote = reinterpret_cast<AudioRemotePlayback*>(hw.get()); 74 if (!remote->startPlaybackForRemoteData(id, buffer->isStereo())) { 75 return TaskGeneric::EResultError; 76 } 77 } 78 // now store sp 79 mHw = hw; 80 81 return TaskGeneric::EResultOK; 82 } 83 84 TaskGeneric::ExecutionResult TaskOutput::complete() 85 { 86 bool result = true; 87 if (mWaitForCompletion) { 88 result = mHw->waitForCompletion(); 89 } 90 mHw->stopPlaybackOrRecord(); 91 mHw.clear(); 92 if (!result) { 93 LOGE("waitForCompletion failed"); 94 return TaskGeneric::EResultError; 95 } 96 return TaskGeneric::EResultOK; 97 } 98 99 100