blob: 469aa82bbdca37585c3165bcca6bf57775adbf74 [file] [log] [blame]
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_DEVICE_ANDROID_AUDIO_TRACK_JNI_H_
12#define MODULES_AUDIO_DEVICE_ANDROID_AUDIO_TRACK_JNI_H_
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000013
kwibergf01633e2016-02-24 05:00:36 -080014#include <memory>
15
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000016#include <jni.h>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_device/android/audio_common.h"
19#include "modules/audio_device/android/audio_manager.h"
20#include "modules/audio_device/audio_device_generic.h"
21#include "modules/audio_device/include/audio_device_defines.h"
22#include "modules/utility/include/helpers_android.h"
23#include "modules/utility/include/jvm_android.h"
24#include "rtc_base/thread_checker.h"
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000025
26namespace webrtc {
27
henrika@webrtc.org962c6242015-02-23 11:54:05 +000028// Implements 16-bit mono PCM audio output support for Android using the Java
29// AudioTrack interface. Most of the work is done by its Java counterpart in
30// WebRtcAudioTrack.java. This class is created and lives on a thread in
31// C++-land, but decoded audio buffers are requested on a high-priority
32// thread managed by the Java class.
33//
34// An instance must be created and destroyed on one and the same thread.
35// All public methods must also be called on the same thread. A thread checker
henrikg91d6ede2015-09-17 00:24:34 -070036// will RTC_DCHECK if any method is called on an invalid thread.
henrika@webrtc.org962c6242015-02-23 11:54:05 +000037//
henrikaee369e42015-05-25 10:11:27 +020038// This class uses AttachCurrentThreadIfNeeded to attach to a Java VM if needed
39// and detach when the object goes out of scope. Additional thread checking
40// guarantees that no other (possibly non attached) thread is used.
henrikab2619892015-05-18 16:49:16 +020041class AudioTrackJni {
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000042 public:
henrikaee369e42015-05-25 10:11:27 +020043 // Wraps the Java specific parts of the AudioTrackJni into one helper class.
44 class JavaAudioTrack {
45 public:
46 JavaAudioTrack(NativeRegistration* native_registration,
kwibergf01633e2016-02-24 05:00:36 -080047 std::unique_ptr<GlobalRef> audio_track);
henrikaee369e42015-05-25 10:11:27 +020048 ~JavaAudioTrack();
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000049
henrikaef38b562016-04-05 07:20:27 -070050 bool InitPlayout(int sample_rate, int channels);
henrikaee369e42015-05-25 10:11:27 +020051 bool StartPlayout();
52 bool StopPlayout();
53 bool SetStreamVolume(int volume);
54 int GetStreamMaxVolume();
55 int GetStreamVolume();
56
57 private:
kwibergf01633e2016-02-24 05:00:36 -080058 std::unique_ptr<GlobalRef> audio_track_;
henrikaee369e42015-05-25 10:11:27 +020059 jmethodID init_playout_;
60 jmethodID start_playout_;
61 jmethodID stop_playout_;
62 jmethodID set_stream_volume_;
63 jmethodID get_stream_max_volume_;
64 jmethodID get_stream_volume_;
65 };
66
67 explicit AudioTrackJni(AudioManager* audio_manager);
henrika@webrtc.org962c6242015-02-23 11:54:05 +000068 ~AudioTrackJni();
69
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000070 int32_t Init();
71 int32_t Terminate();
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000072
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000073 int32_t InitPlayout();
henrika@webrtc.org962c6242015-02-23 11:54:05 +000074 bool PlayoutIsInitialized() const { return initialized_; }
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000075
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000076 int32_t StartPlayout();
77 int32_t StopPlayout();
henrika@webrtc.org962c6242015-02-23 11:54:05 +000078 bool Playing() const { return playing_; }
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000079
henrika8324b522015-03-27 10:56:23 +010080 int SpeakerVolumeIsAvailable(bool& available);
81 int SetSpeakerVolume(uint32_t volume);
82 int SpeakerVolume(uint32_t& volume) const;
83 int MaxSpeakerVolume(uint32_t& max_volume) const;
84 int MinSpeakerVolume(uint32_t& min_volume) const;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000085
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000086 void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer);
87
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000088 private:
henrika@webrtc.org962c6242015-02-23 11:54:05 +000089 // Called from Java side so we can cache the address of the Java-manged
90 // |byte_buffer| in |direct_buffer_address_|. The size of the buffer
91 // is also stored in |direct_buffer_capacity_in_bytes_|.
henrika8324b522015-03-27 10:56:23 +010092 // Called on the same thread as the creating thread.
Yves Gerey665174f2018-06-19 15:03:05 +020093 static void JNICALL CacheDirectBufferAddress(JNIEnv* env,
94 jobject obj,
95 jobject byte_buffer,
96 jlong nativeAudioTrack);
henrika@webrtc.org962c6242015-02-23 11:54:05 +000097 void OnCacheDirectBufferAddress(JNIEnv* env, jobject byte_buffer);
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000098
henrika@webrtc.org962c6242015-02-23 11:54:05 +000099 // Called periodically by the Java based WebRtcAudioTrack object when
100 // playout has started. Each call indicates that |length| new bytes should
101 // be written to the memory area |direct_buffer_address_| for playout.
102 // This method is called on a high-priority thread from Java. The name of
103 // the thread is 'AudioTrackThread'.
Yves Gerey665174f2018-06-19 15:03:05 +0200104 static void JNICALL GetPlayoutData(JNIEnv* env,
105 jobject obj,
106 jint length,
107 jlong nativeAudioTrack);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700108 void OnGetPlayoutData(size_t length);
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000109
henrika@webrtc.org962c6242015-02-23 11:54:05 +0000110 // Stores thread ID in constructor.
henrika@webrtc.org962c6242015-02-23 11:54:05 +0000111 rtc::ThreadChecker thread_checker_;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000112
henrika@webrtc.org962c6242015-02-23 11:54:05 +0000113 // Stores thread ID in first call to OnGetPlayoutData() from high-priority
114 // thread in Java. Detached during construction of this object.
115 rtc::ThreadChecker thread_checker_java_;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000116
henrikaee369e42015-05-25 10:11:27 +0200117 // Calls AttachCurrentThread() if this thread is not attached at construction.
118 // Also ensures that DetachCurrentThread() is called at destruction.
119 AttachCurrentThreadIfNeeded attach_thread_if_needed_;
120
121 // Wraps the JNI interface pointer and methods associated with it.
kwibergf01633e2016-02-24 05:00:36 -0800122 std::unique_ptr<JNIEnvironment> j_environment_;
henrikaee369e42015-05-25 10:11:27 +0200123
124 // Contains factory method for creating the Java object.
kwibergf01633e2016-02-24 05:00:36 -0800125 std::unique_ptr<NativeRegistration> j_native_registration_;
henrikaee369e42015-05-25 10:11:27 +0200126
127 // Wraps the Java specific parts of the AudioTrackJni class.
kwibergf01633e2016-02-24 05:00:36 -0800128 std::unique_ptr<AudioTrackJni::JavaAudioTrack> j_audio_track_;
henrikaee369e42015-05-25 10:11:27 +0200129
henrika8324b522015-03-27 10:56:23 +0100130 // Contains audio parameters provided to this class at construction by the
131 // AudioManager.
132 const AudioParameters audio_parameters_;
133
henrika@webrtc.org962c6242015-02-23 11:54:05 +0000134 // Cached copy of address to direct audio buffer owned by |j_audio_track_|.
135 void* direct_buffer_address_;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000136
henrika@webrtc.org962c6242015-02-23 11:54:05 +0000137 // Number of bytes in the direct audio buffer owned by |j_audio_track_|.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700138 size_t direct_buffer_capacity_in_bytes_;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000139
henrika@webrtc.org962c6242015-02-23 11:54:05 +0000140 // Number of audio frames per audio buffer. Each audio frame corresponds to
141 // one sample of PCM mono data at 16 bits per sample. Hence, each audio
142 // frame contains 2 bytes (given that the Java layer only supports mono).
143 // Example: 480 for 48000 Hz or 441 for 44100 Hz.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700144 size_t frames_per_buffer_;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000145
henrika@webrtc.org962c6242015-02-23 11:54:05 +0000146 bool initialized_;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000147
henrika@webrtc.org962c6242015-02-23 11:54:05 +0000148 bool playing_;
149
150 // Raw pointer handle provided to us in AttachAudioBuffer(). Owned by the
Peter Boström4adbbcf2016-05-03 15:51:26 -0400151 // AudioDeviceModuleImpl class and called by AudioDeviceModule::Create().
henrika@webrtc.org962c6242015-02-23 11:54:05 +0000152 // The AudioDeviceBuffer is a member of the AudioDeviceModuleImpl instance
153 // and therefore outlives this object.
154 AudioDeviceBuffer* audio_device_buffer_;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000155};
156
157} // namespace webrtc
158
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200159#endif // MODULES_AUDIO_DEVICE_ANDROID_AUDIO_TRACK_JNI_H_