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 
18 #ifndef CTSAUDIO_AUDIORECORDPLAYTESTCOMMON_H
19 #define CTSAUDIO_AUDIORECORDPLAYTESTCOMMON_H
20 
21 #include <gtest/gtest.h>
22 #include <utils/threads.h>
23 #include <utils/StrongPointer.h>
24 
25 #include <audio/AudioHardware.h>
26 #include <audio/AudioPlaybackLocal.h>
27 #include <audio/AudioRecordingLocal.h>
28 #include <audio/AudioSignalFactory.h>
29 #include <audio/AudioLocal.h>
30 #include <audio/Buffer.h>
31 #include <Log.h>
32 
33 class AudioRecordPlayTestCommon : public testing::Test {
34 protected:
35     android::sp<Buffer> mBufferRecording;
36     android::sp<Buffer> mBufferPlayback;
37     android::sp<AudioHardware> mAudioRecordingHw;
38     android::sp<AudioHardware> mAudioPlaybackHw;
39 
40     static const int MAX_POSITIVE_AMPLITUDE = 10000;
41     static const int SIGNAL_FREQ = 1000;
42     static const int NUMBER_SAMPLES = AudioHardware::SAMPLES_PER_ONE_GO * 4;
43     static const int DEFAULT_VOLUME = 10;
44 protected:
SetUp()45     virtual void SetUp() {
46         mAudioPlaybackHw = createPlaybackHw();
47         ASSERT_TRUE(mAudioPlaybackHw.get() != NULL);
48         mAudioRecordingHw = createRecordingHw();
49         ASSERT_TRUE(mAudioRecordingHw.get() != NULL);
50         mBufferPlayback = AudioSignalFactory::generateSineWave(AudioHardware::E2BPS,
51                 MAX_POSITIVE_AMPLITUDE, AudioHardware::ESampleRate_44100,
52                 SIGNAL_FREQ, NUMBER_SAMPLES);
53         ASSERT_TRUE(mBufferPlayback.get() != NULL);
54         mBufferRecording = new Buffer(NUMBER_SAMPLES * 4, NUMBER_SAMPLES * 4);
55         ASSERT_TRUE(mBufferRecording.get() != NULL);
56     }
57 
TearDown()58     virtual void TearDown() {
59         mAudioRecordingHw->stopPlaybackOrRecord();
60         mAudioPlaybackHw->stopPlaybackOrRecord();
61         mAudioRecordingHw.clear();
62         mAudioPlaybackHw.clear();
63     }
64 
PlayAndRecord(int numberRepetition)65     void PlayAndRecord(int numberRepetition) {
66         ASSERT_TRUE(mAudioPlaybackHw->prepare(AudioHardware::ESampleRate_44100, DEFAULT_VOLUME));
67         ASSERT_TRUE(mAudioRecordingHw->prepare(AudioHardware::ESampleRate_44100, DEFAULT_VOLUME));
68         ASSERT_TRUE(mAudioRecordingHw->startPlaybackOrRecord(mBufferRecording,
69                 numberRepetition));
70         ASSERT_TRUE(mAudioPlaybackHw->startPlaybackOrRecord(mBufferPlayback,
71                 numberRepetition));
72 
73         ASSERT_TRUE(mAudioRecordingHw->waitForCompletion());
74         ASSERT_TRUE(mAudioPlaybackHw->waitForCompletion());
75         mAudioPlaybackHw->stopPlaybackOrRecord();
76         mAudioRecordingHw->stopPlaybackOrRecord();
77         LOGV("Audio playback buffer size %d, handled %d", mBufferPlayback->getSize(),
78                 mBufferPlayback->amountHandled());
79         ASSERT_TRUE(mBufferPlayback->amountHandled() == mBufferPlayback->getSize());
80         LOGV("Audio recording buffer size %d, handled %d", mBufferRecording->getSize(),
81                 mBufferRecording->amountHandled());
82         ASSERT_TRUE(mBufferRecording->amountHandled() == mBufferRecording->getSize());
83     }
84 
85     virtual android::sp<AudioHardware> createRecordingHw() = 0;
86     virtual android::sp<AudioHardware> createPlaybackHw() = 0;
87 };
88 
89 
90 
91 
92 #endif // CTSAUDIO_AUDIORECORDPLAYTESTCOMMON_H
93