Home | History | Annotate | Download | only in lb2
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of 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,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef LB2_SOUND_SYSTEM_H_
     18 #define LB2_SOUND_SYSTEM_H_
     19 
     20 #include <functional>
     21 
     22 #include "lb2/audio_buffer.h"
     23 #include "lb2/test_context.h"  // for PerformanceMode
     24 
     25 // Interface for sound systems.
     26 // It is assumed that "pull" model (callback) is used for providing
     27 // sound data to the system, and "push" model (sync read) is used
     28 // for sound input.
     29 class SoundSystem {
     30   public:
     31     // The memory region pointed by this buffer must remain
     32     // valid until the write callback is called the next time,
     33     // or until 'shutdown' is called.
     34     using WriteCallback = std::function<AudioBufferView<sample_t>(size_t expectedFrames)>;
     35 
     36     SoundSystem() = default;
     37     SoundSystem(const SoundSystem&) = delete;
     38     SoundSystem& operator=(const SoundSystem&) = delete;
     39     virtual ~SoundSystem() {}
     40 
     41     // Probes the output hardware for the recommended parameters for input
     42     // and output streams. Returns 'false' if probing is impossible or has failed.
     43     // Note that this is a separate use case for the sound system. After commencing
     44     // probing, the instance of the sound system used for probing must be shut down.
     45     virtual bool probeDefaultSettings(PerformanceMode /*performanceMode*/, int* /*samplingRate*/,
     46             int* /*playerBufferFrameCount*/, int* /*recorderBufferFrameCount*/) { return false; }
     47     // Initializes the sound system for the regular testing scenario.
     48     // Returns 'true' if initialization was successful, 'false' otherwise.
     49     virtual bool init(WriteCallback callback) = 0;
     50     // Make sure the buffer of the input stream is empty, so fresh audio data
     51     // can be received immediately on the next call to 'readAudio'.
     52     // Returns 'true' if there were no errors, 'false' otherwise.
     53     virtual bool drainInput() = 0;
     54     // Reads from audio input into the provided buffer. A non-negative result value
     55     // indicates success, a negative return value indicates an error.
     56     virtual ssize_t readAudio(AudioBufferView<sample_t> buffer) = 0;
     57     // Shuts the sound system down.
     58     virtual void shutdown() = 0;
     59 };
     60 
     61 #endif  // LB2_SOUND_SYSTEM_H_
     62