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 #ifndef CTSAUDIO_AUDIOPLAYTESTCOMMON_H 18 #define CTSAUDIO_AUDIOPLAYTESTCOMMON_H 19 20 #include <gtest/gtest.h> 21 #include <utils/threads.h> 22 #include <utils/StrongPointer.h> 23 24 #include <audio/AudioHardware.h> 25 #include <audio/AudioPlaybackLocal.h> 26 #include <audio/AudioRecordingLocal.h> 27 #include <audio/AudioSignalFactory.h> 28 #include <audio/AudioLocal.h> 29 #include <audio/Buffer.h> 30 31 32 #include <Log.h> 33 34 35 class AudioPlayTestCommon : public testing::Test { 36 protected: 37 android::sp<Buffer> mBuffer; 38 android::sp<AudioHardware> mAudioHw; 39 40 static const int MAX_POSITIVE_AMPLITUDE = 1000; 41 static const int SIGNAL_FREQ = 1000; 42 static const int SIGNAL_LENGTH = AudioHardware::SAMPLES_PER_ONE_GO * 2; 43 static const int DEFAULT_VOLUME = 10; 44 45 protected: 46 virtual ~AudioPlayTestCommon() { 47 LOGV("~AudioPlayTestCommon"); 48 } 49 virtual void SetUp() { 50 mAudioHw = createAudioHw(); 51 ASSERT_TRUE(mAudioHw.get() != NULL); 52 mBuffer = AudioSignalFactory::generateSineWave(AudioHardware::E2BPS, 53 MAX_POSITIVE_AMPLITUDE, AudioHardware::ESampleRate_44100, 54 SIGNAL_FREQ, SIGNAL_LENGTH); 55 ASSERT_TRUE(mBuffer.get() != NULL); 56 } 57 58 virtual void TearDown() { 59 LOGV("AudioPlayTestCommon::TearDown"); 60 mAudioHw->stopPlaybackOrRecord(); // this stops the thread 61 mAudioHw.clear(); 62 } 63 64 void playAll(int numberRepetition) { 65 ASSERT_TRUE(mAudioHw->prepare(AudioHardware::ESampleRate_44100, DEFAULT_VOLUME)); 66 ASSERT_TRUE(mAudioHw->startPlaybackOrRecord(mBuffer, numberRepetition)); 67 ASSERT_TRUE(mAudioHw->waitForCompletion()); 68 mAudioHw->stopPlaybackOrRecord(); 69 LOGV("size %d, handled %d", mBuffer->getSize(), mBuffer->amountHandled()); 70 ASSERT_TRUE(mBuffer->amountHandled() == mBuffer->getSize()); 71 } 72 73 void repeatPlayStop() { 74 for (int i = 0; i < 2; i++) { 75 ASSERT_TRUE(mAudioHw->prepare(AudioHardware::ESampleRate_44100, DEFAULT_VOLUME)); 76 mBuffer->restart(); 77 ASSERT_TRUE(mAudioHw->startPlaybackOrRecord(mBuffer, 10)); 78 mAudioHw->stopPlaybackOrRecord(); 79 } 80 } 81 82 void playWrongUsage() { 83 ASSERT_FALSE(mAudioHw->startPlaybackOrRecord(mBuffer)); 84 ASSERT_TRUE(mAudioHw->prepare(AudioHardware::ESampleRate_44100, DEFAULT_VOLUME)); 85 playAll(1); 86 } 87 88 virtual android::sp<AudioHardware> createAudioHw() = 0; 89 }; 90 91 #endif // CTSAUDIO_AUDIOPLAYTESTCOMMON_H 92