henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_DEVICE_ANDROID_AUDIO_TRACK_JNI_H_ |
| 12 | #define MODULES_AUDIO_DEVICE_ANDROID_AUDIO_TRACK_JNI_H_ |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 13 | |
| 14 | #include <jni.h> |
| 15 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 16 | #include <memory> |
| 17 | |
Artem Titov | d15a575 | 2021-02-10 14:31:24 +0100 | [diff] [blame] | 18 | #include "api/sequence_checker.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "modules/audio_device/android/audio_common.h" |
| 20 | #include "modules/audio_device/android/audio_manager.h" |
| 21 | #include "modules/audio_device/audio_device_generic.h" |
| 22 | #include "modules/audio_device/include/audio_device_defines.h" |
| 23 | #include "modules/utility/include/helpers_android.h" |
| 24 | #include "modules/utility/include/jvm_android.h" |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | |
henrika@webrtc.org | 962c624 | 2015-02-23 11:54:05 +0000 | [diff] [blame] | 28 | // 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 |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 36 | // will RTC_DCHECK if any method is called on an invalid thread. |
henrika@webrtc.org | 962c624 | 2015-02-23 11:54:05 +0000 | [diff] [blame] | 37 | // |
Mirko Bonadei | 977c820 | 2019-01-11 19:26:40 +0100 | [diff] [blame] | 38 | // This class uses JvmThreadConnector to attach to a Java VM if needed |
henrika | ee369e4 | 2015-05-25 10:11:27 +0200 | [diff] [blame] | 39 | // and detach when the object goes out of scope. Additional thread checking |
| 40 | // guarantees that no other (possibly non attached) thread is used. |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 41 | class AudioTrackJni { |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 42 | public: |
henrika | ee369e4 | 2015-05-25 10:11:27 +0200 | [diff] [blame] | 43 | // Wraps the Java specific parts of the AudioTrackJni into one helper class. |
| 44 | class JavaAudioTrack { |
| 45 | public: |
| 46 | JavaAudioTrack(NativeRegistration* native_registration, |
kwiberg | f01633e | 2016-02-24 05:00:36 -0800 | [diff] [blame] | 47 | std::unique_ptr<GlobalRef> audio_track); |
henrika | ee369e4 | 2015-05-25 10:11:27 +0200 | [diff] [blame] | 48 | ~JavaAudioTrack(); |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 49 | |
henrika | ef38b56 | 2016-04-05 07:20:27 -0700 | [diff] [blame] | 50 | bool InitPlayout(int sample_rate, int channels); |
henrika | ee369e4 | 2015-05-25 10:11:27 +0200 | [diff] [blame] | 51 | bool StartPlayout(); |
| 52 | bool StopPlayout(); |
| 53 | bool SetStreamVolume(int volume); |
| 54 | int GetStreamMaxVolume(); |
| 55 | int GetStreamVolume(); |
| 56 | |
| 57 | private: |
kwiberg | f01633e | 2016-02-24 05:00:36 -0800 | [diff] [blame] | 58 | std::unique_ptr<GlobalRef> audio_track_; |
henrika | ee369e4 | 2015-05-25 10:11:27 +0200 | [diff] [blame] | 59 | 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_; |
Ivo Creusen | f1393e2 | 2020-05-28 13:54:49 +0200 | [diff] [blame] | 65 | jmethodID get_buffer_size_in_frames_; |
henrika | ee369e4 | 2015-05-25 10:11:27 +0200 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | explicit AudioTrackJni(AudioManager* audio_manager); |
henrika@webrtc.org | 962c624 | 2015-02-23 11:54:05 +0000 | [diff] [blame] | 69 | ~AudioTrackJni(); |
| 70 | |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 71 | int32_t Init(); |
| 72 | int32_t Terminate(); |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 73 | |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 74 | int32_t InitPlayout(); |
henrika@webrtc.org | 962c624 | 2015-02-23 11:54:05 +0000 | [diff] [blame] | 75 | bool PlayoutIsInitialized() const { return initialized_; } |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 76 | |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 77 | int32_t StartPlayout(); |
| 78 | int32_t StopPlayout(); |
henrika@webrtc.org | 962c624 | 2015-02-23 11:54:05 +0000 | [diff] [blame] | 79 | bool Playing() const { return playing_; } |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 80 | |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 81 | int SpeakerVolumeIsAvailable(bool& available); |
| 82 | int SetSpeakerVolume(uint32_t volume); |
| 83 | int SpeakerVolume(uint32_t& volume) const; |
| 84 | int MaxSpeakerVolume(uint32_t& max_volume) const; |
| 85 | int MinSpeakerVolume(uint32_t& min_volume) const; |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 86 | |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 87 | void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer); |
| 88 | |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 89 | private: |
henrika@webrtc.org | 962c624 | 2015-02-23 11:54:05 +0000 | [diff] [blame] | 90 | // Called from Java side so we can cache the address of the Java-manged |
| 91 | // |byte_buffer| in |direct_buffer_address_|. The size of the buffer |
| 92 | // is also stored in |direct_buffer_capacity_in_bytes_|. |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 93 | // Called on the same thread as the creating thread. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 94 | static void JNICALL CacheDirectBufferAddress(JNIEnv* env, |
| 95 | jobject obj, |
| 96 | jobject byte_buffer, |
| 97 | jlong nativeAudioTrack); |
henrika@webrtc.org | 962c624 | 2015-02-23 11:54:05 +0000 | [diff] [blame] | 98 | void OnCacheDirectBufferAddress(JNIEnv* env, jobject byte_buffer); |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 99 | |
henrika@webrtc.org | 962c624 | 2015-02-23 11:54:05 +0000 | [diff] [blame] | 100 | // Called periodically by the Java based WebRtcAudioTrack object when |
| 101 | // playout has started. Each call indicates that |length| new bytes should |
| 102 | // be written to the memory area |direct_buffer_address_| for playout. |
| 103 | // This method is called on a high-priority thread from Java. The name of |
| 104 | // the thread is 'AudioTrackThread'. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 105 | static void JNICALL GetPlayoutData(JNIEnv* env, |
| 106 | jobject obj, |
| 107 | jint length, |
| 108 | jlong nativeAudioTrack); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 109 | void OnGetPlayoutData(size_t length); |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 110 | |
henrika@webrtc.org | 962c624 | 2015-02-23 11:54:05 +0000 | [diff] [blame] | 111 | // Stores thread ID in constructor. |
Artem Titov | c8421c4 | 2021-02-02 10:57:19 +0100 | [diff] [blame] | 112 | SequenceChecker thread_checker_; |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 113 | |
henrika@webrtc.org | 962c624 | 2015-02-23 11:54:05 +0000 | [diff] [blame] | 114 | // Stores thread ID in first call to OnGetPlayoutData() from high-priority |
| 115 | // thread in Java. Detached during construction of this object. |
Artem Titov | c8421c4 | 2021-02-02 10:57:19 +0100 | [diff] [blame] | 116 | SequenceChecker thread_checker_java_; |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 117 | |
Mirko Bonadei | 977c820 | 2019-01-11 19:26:40 +0100 | [diff] [blame] | 118 | // Calls JavaVM::AttachCurrentThread() if this thread is not attached at |
| 119 | // construction. |
henrika | ee369e4 | 2015-05-25 10:11:27 +0200 | [diff] [blame] | 120 | // Also ensures that DetachCurrentThread() is called at destruction. |
Mirko Bonadei | 977c820 | 2019-01-11 19:26:40 +0100 | [diff] [blame] | 121 | JvmThreadConnector attach_thread_if_needed_; |
henrika | ee369e4 | 2015-05-25 10:11:27 +0200 | [diff] [blame] | 122 | |
| 123 | // Wraps the JNI interface pointer and methods associated with it. |
kwiberg | f01633e | 2016-02-24 05:00:36 -0800 | [diff] [blame] | 124 | std::unique_ptr<JNIEnvironment> j_environment_; |
henrika | ee369e4 | 2015-05-25 10:11:27 +0200 | [diff] [blame] | 125 | |
| 126 | // Contains factory method for creating the Java object. |
kwiberg | f01633e | 2016-02-24 05:00:36 -0800 | [diff] [blame] | 127 | std::unique_ptr<NativeRegistration> j_native_registration_; |
henrika | ee369e4 | 2015-05-25 10:11:27 +0200 | [diff] [blame] | 128 | |
| 129 | // Wraps the Java specific parts of the AudioTrackJni class. |
kwiberg | f01633e | 2016-02-24 05:00:36 -0800 | [diff] [blame] | 130 | std::unique_ptr<AudioTrackJni::JavaAudioTrack> j_audio_track_; |
henrika | ee369e4 | 2015-05-25 10:11:27 +0200 | [diff] [blame] | 131 | |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 132 | // Contains audio parameters provided to this class at construction by the |
| 133 | // AudioManager. |
| 134 | const AudioParameters audio_parameters_; |
| 135 | |
henrika@webrtc.org | 962c624 | 2015-02-23 11:54:05 +0000 | [diff] [blame] | 136 | // Cached copy of address to direct audio buffer owned by |j_audio_track_|. |
| 137 | void* direct_buffer_address_; |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 138 | |
henrika@webrtc.org | 962c624 | 2015-02-23 11:54:05 +0000 | [diff] [blame] | 139 | // Number of bytes in the direct audio buffer owned by |j_audio_track_|. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 140 | size_t direct_buffer_capacity_in_bytes_; |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 141 | |
henrika@webrtc.org | 962c624 | 2015-02-23 11:54:05 +0000 | [diff] [blame] | 142 | // Number of audio frames per audio buffer. Each audio frame corresponds to |
| 143 | // one sample of PCM mono data at 16 bits per sample. Hence, each audio |
| 144 | // frame contains 2 bytes (given that the Java layer only supports mono). |
| 145 | // Example: 480 for 48000 Hz or 441 for 44100 Hz. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 146 | size_t frames_per_buffer_; |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 147 | |
henrika@webrtc.org | 962c624 | 2015-02-23 11:54:05 +0000 | [diff] [blame] | 148 | bool initialized_; |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 149 | |
henrika@webrtc.org | 962c624 | 2015-02-23 11:54:05 +0000 | [diff] [blame] | 150 | bool playing_; |
| 151 | |
| 152 | // Raw pointer handle provided to us in AttachAudioBuffer(). Owned by the |
Peter Boström | 4adbbcf | 2016-05-03 15:51:26 -0400 | [diff] [blame] | 153 | // AudioDeviceModuleImpl class and called by AudioDeviceModule::Create(). |
henrika@webrtc.org | 962c624 | 2015-02-23 11:54:05 +0000 | [diff] [blame] | 154 | // The AudioDeviceBuffer is a member of the AudioDeviceModuleImpl instance |
| 155 | // and therefore outlives this object. |
| 156 | AudioDeviceBuffer* audio_device_buffer_; |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | } // namespace webrtc |
| 160 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 161 | #endif // MODULES_AUDIO_DEVICE_ANDROID_AUDIO_TRACK_JNI_H_ |