1 // Copyright 2013 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.
5 #include "media/audio/android/audio_record_input.h"
7 #include "base/logging.h"
8 #include "jni/AudioRecordInput_jni.h"
9 #include "media/audio/android/audio_manager_android.h"
13 AudioRecordInputStream::AudioRecordInputStream(
14 AudioManagerAndroid
* audio_manager
, const AudioParameters
& params
)
15 : audio_manager_(audio_manager
),
17 direct_buffer_address_(NULL
) {
18 DVLOG(2) << __PRETTY_FUNCTION__
;
19 DCHECK(params
.IsValid());
20 j_audio_record_
.Reset(
21 Java_AudioRecordInput_createAudioRecordInput(
22 base::android::AttachCurrentThread(),
23 reinterpret_cast<intptr_t>(this),
26 params
.bits_per_sample(),
27 params
.GetBytesPerBuffer(),
28 params
.effects() & AudioParameters::ECHO_CANCELLER
));
31 AudioRecordInputStream::~AudioRecordInputStream() {
32 DVLOG(2) << __PRETTY_FUNCTION__
;
33 DCHECK(thread_checker_
.CalledOnValidThread());
36 void AudioRecordInputStream::CacheDirectBufferAddress(JNIEnv
* env
, jobject obj
,
37 jobject byte_buffer
) {
38 DCHECK(thread_checker_
.CalledOnValidThread());
39 direct_buffer_address_
= static_cast<uint8
*>(
40 env
->GetDirectBufferAddress(byte_buffer
));
44 bool AudioRecordInputStream::RegisterAudioRecordInput(JNIEnv
* env
) {
45 return RegisterNativesImpl(env
);
48 void AudioRecordInputStream::OnData(JNIEnv
* env
, jobject obj
, jint size
,
49 jint hardware_delay_bytes
) {
50 DCHECK(direct_buffer_address_
);
51 // Passing zero as the volume parameter indicates there is no access to a
52 // hardware volume slider.
53 callback_
->OnData(this, direct_buffer_address_
, size
, hardware_delay_bytes
,
57 bool AudioRecordInputStream::Open() {
58 DVLOG(2) << __PRETTY_FUNCTION__
;
59 DCHECK(thread_checker_
.CalledOnValidThread());
60 return Java_AudioRecordInput_open(
61 base::android::AttachCurrentThread(), j_audio_record_
.obj());
64 void AudioRecordInputStream::Start(AudioInputCallback
* callback
) {
65 DVLOG(2) << __PRETTY_FUNCTION__
;
66 DCHECK(thread_checker_
.CalledOnValidThread());
70 // Start() was already called.
71 DCHECK_EQ(callback_
, callback
);
74 // The Java thread has not yet started, so we are free to set |callback_|.
77 Java_AudioRecordInput_start(
78 base::android::AttachCurrentThread(), j_audio_record_
.obj());
81 void AudioRecordInputStream::Stop() {
82 DVLOG(2) << __PRETTY_FUNCTION__
;
83 DCHECK(thread_checker_
.CalledOnValidThread());
85 // Start() was never called, or Stop() was already called.
89 Java_AudioRecordInput_stop(
90 base::android::AttachCurrentThread(), j_audio_record_
.obj());
92 // The Java thread must have been stopped at this point, so we are free to
97 void AudioRecordInputStream::Close() {
98 DVLOG(2) << __PRETTY_FUNCTION__
;
99 DCHECK(thread_checker_
.CalledOnValidThread());
102 Java_AudioRecordInput_close(
103 base::android::AttachCurrentThread(), j_audio_record_
.obj());
104 audio_manager_
->ReleaseInputStream(this);
107 double AudioRecordInputStream::GetMaxVolume() {
112 void AudioRecordInputStream::SetVolume(double volume
) {
116 double AudioRecordInputStream::GetVolume() {
121 void AudioRecordInputStream::SetAutomaticGainControl(bool enabled
) {
125 bool AudioRecordInputStream::GetAutomaticGainControl() {