1/*
2 *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_EXAMPLES_ANDROID_OPENSL_LOOPBACK_FAKE_AUDIO_DEVICE_BUFFER_H_
12#define WEBRTC_EXAMPLES_ANDROID_OPENSL_LOOPBACK_FAKE_AUDIO_DEVICE_BUFFER_H_
13
14#include "webrtc/modules/audio_device/android/audio_manager_jni.h"
15#include "webrtc/modules/audio_device/android/single_rw_fifo.h"
16#include "webrtc/modules/audio_device/audio_device_buffer.h"
17#include "webrtc/system_wrappers/interface/scoped_ptr.h"
18
19namespace webrtc {
20
21// Fake AudioDeviceBuffer implementation that returns audio data that is pushed
22// to it. It implements all APIs used by the OpenSL implementation.
23class FakeAudioDeviceBuffer : public AudioDeviceBuffer {
24 public:
25  FakeAudioDeviceBuffer();
26  virtual ~FakeAudioDeviceBuffer() {}
27
28  virtual int32_t SetRecordingSampleRate(uint32_t fsHz);
29  virtual int32_t SetPlayoutSampleRate(uint32_t fsHz);
30  virtual int32_t SetRecordingChannels(uint8_t channels);
31  virtual int32_t SetPlayoutChannels(uint8_t channels);
32  virtual int32_t SetRecordedBuffer(const void* audioBuffer,
33                                    uint32_t nSamples);
34  virtual void SetVQEData(int playDelayMS,
35                          int recDelayMS,
36                          int clockDrift) {}
37  virtual int32_t DeliverRecordedData() { return 0; }
38  virtual int32_t RequestPlayoutData(uint32_t nSamples);
39  virtual int32_t GetPlayoutData(void* audioBuffer);
40
41  void ClearBuffer();
42
43 private:
44  enum {
45    // Each buffer contains 10 ms of data since that is what OpenSlesInput
46    // delivers. Keep 7 buffers which would cover 70 ms of data. These buffers
47    // are needed because of jitter between OpenSl recording and playing.
48    kNumBuffers = 7,
49  };
50  int sample_rate() const;
51  int buffer_size_samples() const;
52  int buffer_size_bytes() const;
53
54  // Java API handle
55  AudioManagerJni audio_manager_;
56
57  SingleRwFifo fifo_;
58  scoped_ptr<scoped_ptr<int8_t[]>[]> buf_;
59  int next_available_buffer_;
60
61  uint8_t record_channels_;
62  uint8_t play_channels_;
63};
64
65}  // namespace webrtc
66
67#endif  // WEBRTC_EXAMPLES_ANDROID_OPENSL_LOOPBACK_FAKE_AUDIO_DEVICE_BUFFER_H_
68