1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef MEDIA_AUDIO_ANDROID_OPENSLES_INPUT_H_
6 #define MEDIA_AUDIO_ANDROID_OPENSLES_INPUT_H_
7 
8 #include <SLES/OpenSLES.h>
9 #include <SLES/OpenSLES_Android.h>
10 #include <stdint.h>
11 
12 #include <memory>
13 
14 #include "base/compiler_specific.h"
15 #include "base/macros.h"
16 #include "base/synchronization/lock.h"
17 #include "base/threading/thread_checker.h"
18 #include "media/audio/android/opensles_util.h"
19 #include "media/audio/audio_io.h"
20 #include "media/base/audio_parameters.h"
21 
22 namespace media {
23 
24 class AudioBus;
25 class AudioManagerAndroid;
26 
27 // Implements PCM audio input support for Android using the OpenSLES API.
28 // This class is created and lives on the Audio Manager thread but recorded
29 // audio buffers are delivered on an internal OpenSLES audio thread. All public
30 // methods should be called on the Audio Manager thread.
31 class OpenSLESInputStream : public AudioInputStream {
32  public:
33   static const int kMaxNumOfBuffersInQueue = 2;
34 
35   OpenSLESInputStream(AudioManagerAndroid* manager,
36                       const AudioParameters& params);
37 
38   ~OpenSLESInputStream() override;
39 
40   // Implementation of AudioInputStream.
41   bool Open() override;
42   void Start(AudioInputCallback* callback) override;
43   void Stop() override;
44   void Close() override;
45   double GetMaxVolume() override;
46   void SetVolume(double volume) override;
47   double GetVolume() override;
48   bool SetAutomaticGainControl(bool enabled) override;
49   bool GetAutomaticGainControl() override;
50   bool IsMuted() override;
51   void SetOutputDeviceForAec(const std::string& output_device_id) override;
52 
53  private:
54   bool CreateRecorder();
55 
56   // Called from OpenSLES specific audio worker thread.
57   static void SimpleBufferQueueCallback(
58       SLAndroidSimpleBufferQueueItf buffer_queue,
59       void* instance);
60 
61   // Called from OpenSLES specific audio worker thread.
62   void ReadBufferQueue();
63 
64   // Called in Open();
65   void SetupAudioBuffer();
66 
67   // Called in Close();
68   void ReleaseAudioBuffer();
69 
70   // If OpenSLES reports an error this function handles it and passes it to
71   // the attached AudioInputCallback::OnError().
72   void HandleError(SLresult error);
73 
74   base::ThreadChecker thread_checker_;
75 
76   // Protects |callback_|, |active_buffer_index_|, |audio_data_|,
77   // |buffer_size_bytes_| and |simple_buffer_queue_|.
78   base::Lock lock_;
79 
80   AudioManagerAndroid* audio_manager_;
81 
82   AudioInputCallback* callback_;
83 
84   // Shared engine interfaces for the app.
85   media::ScopedSLObjectItf recorder_object_;
86   media::ScopedSLObjectItf engine_object_;
87 
88   SLRecordItf recorder_;
89 
90   // Buffer queue recorder interface.
91   SLAndroidSimpleBufferQueueItf simple_buffer_queue_;
92 
93   SLDataFormat_PCM format_;
94 
95   // Audio buffers that are allocated in the constructor based on
96   // info from audio parameters.
97   uint8_t* audio_data_[kMaxNumOfBuffersInQueue];
98 
99   int active_buffer_index_;
100   int buffer_size_bytes_;
101 
102   bool started_;
103 
104   base::TimeDelta hardware_delay_;
105 
106   std::unique_ptr<media::AudioBus> audio_bus_;
107 
108   // Set to true at construction if user wants to disable all audio effects.
109   const bool no_effects_ = false;
110 
111   DISALLOW_COPY_AND_ASSIGN(OpenSLESInputStream);
112 };
113 
114 }  // namespace media
115 
116 #endif  // MEDIA_AUDIO_ANDROID_OPENSLES_INPUT_H_
117