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_AUDIOHARDWARE_H
18 #define CTSAUDIO_AUDIOHARDWARE_H
19 
20 #include <utils/StrongPointer.h>
21 #include <utils/RefBase.h>
22 #include "Buffer.h"
23 
24 class TaskCase;
25 /**
26  * Utility class for H/W detection
27  */
28 class AudioHardware : virtual public android::RefBase {
29 public:
30     /** audio length should be multiple of this */
31     static const int SAMPLES_PER_ONE_GO = 4096;
32 
33     enum SamplingRate {
34         ESamplingRateInvald = 0,
35         ESampleRate_16000 = 16000,
36         ESampleRate_44100 = 44100
37     };
38     enum BytesPerSample {
39         E2BPS = 2
40     };
41     enum AudioMode {
42         EModeVoice = 0,
43         EModeMusic = 1
44     };
45 
46     /**
47      * detect supported audio H/W
48      * @return card number of detected H/W. -1 if not found.
49      */
50     static int detectAudioHw();
51 
52     /**
53      * Factory method
54      * options are : local or remote, playback or recording
55      * can return NULL(sp.get() == NULL) if H/W not found
56      */
57     static android::sp<AudioHardware> createAudioHw(bool local, bool playback,
58             TaskCase* testCase = NULL);
59 
60     virtual ~AudioHardware();
61     /**
62      * prepare playback or recording
63      */
64     virtual bool prepare(SamplingRate samplingRate, int volume, int mode = EModeVoice) = 0;
65 
66     /**
67      * Convenience API to pass buffer ID. The buffer can be either present in testCase
68      * or in remote device (when testCase is NULL)
69      */
70     virtual bool startPlaybackOrRecordById(const android::String8& id, TaskCase* testCase = NULL);
71 
72     /**
73      *  Playback / Record with given buffer
74      *  @param buffer buffer to play / record
75      *  @param numberRepetition How many times to repeat playback / record for given buffer.
76      *         For record, it does not have much meaning as the last recording will always
77      *         override.
78      */
79     virtual bool startPlaybackOrRecord(android::sp<Buffer>& buffer,
80             int numberRepetition = 1) = 0;
81     /**
82      * Wait for the playback / recording to complete. return true when successfully finished.
83      * Calling waitForCompletion after calling stopPlaybackOrRecord will lead into blocking
84      * the calling thread for some time.
85      */
86     virtual bool waitForCompletion() = 0;
87     /// stops the on-going action. The active task can be canceled.
88     virtual void stopPlaybackOrRecord() = 0;
89 
90 protected:
91     static int mHwId;
92 };
93 
94 
95 #endif // CTSAUDIO_AUDIOHARDWARE_H
96