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 MODULES_AUDIO_DEVICE_ANDROID_AUDIO_TRACK_JNI_H_
12 #define MODULES_AUDIO_DEVICE_ANDROID_AUDIO_TRACK_JNI_H_
13 
14 #include <memory>
15 
16 #include <jni.h>
17 
18 #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"
25 
26 namespace webrtc {
27 
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
36 // will RTC_DCHECK if any method is called on an invalid thread.
37 //
38 // 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.
41 class AudioTrackJni {
42  public:
43   // Wraps the Java specific parts of the AudioTrackJni into one helper class.
44   class JavaAudioTrack {
45    public:
46     JavaAudioTrack(NativeRegistration* native_registration,
47                    std::unique_ptr<GlobalRef> audio_track);
48     ~JavaAudioTrack();
49 
50     bool InitPlayout(int sample_rate, int channels);
51     bool StartPlayout();
52     bool StopPlayout();
53     bool SetStreamVolume(int volume);
54     int GetStreamMaxVolume();
55     int GetStreamVolume();
56 
57    private:
58     std::unique_ptr<GlobalRef> audio_track_;
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_;
65   };
66 
67   explicit AudioTrackJni(AudioManager* audio_manager);
68   ~AudioTrackJni();
69 
70   int32_t Init();
71   int32_t Terminate();
72 
73   int32_t InitPlayout();
PlayoutIsInitialized()74   bool PlayoutIsInitialized() const { return initialized_; }
75 
76   int32_t StartPlayout();
77   int32_t StopPlayout();
Playing()78   bool Playing() const { return playing_; }
79 
80   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;
85 
86   void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer);
87 
88  private:
89   // 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_|.
92   // Called on the same thread as the creating thread.
93   static void JNICALL CacheDirectBufferAddress(
94     JNIEnv* env, jobject obj, jobject byte_buffer, jlong nativeAudioTrack);
95   void OnCacheDirectBufferAddress(JNIEnv* env, jobject byte_buffer);
96 
97   // Called periodically by the Java based WebRtcAudioTrack object when
98   // playout has started. Each call indicates that |length| new bytes should
99   // be written to the memory area |direct_buffer_address_| for playout.
100   // This method is called on a high-priority thread from Java. The name of
101   // the thread is 'AudioTrackThread'.
102   static void JNICALL GetPlayoutData(
103     JNIEnv* env, jobject obj, jint length, jlong nativeAudioTrack);
104   void OnGetPlayoutData(size_t length);
105 
106   // Stores thread ID in constructor.
107   rtc::ThreadChecker thread_checker_;
108 
109   // Stores thread ID in first call to OnGetPlayoutData() from high-priority
110   // thread in Java. Detached during construction of this object.
111   rtc::ThreadChecker thread_checker_java_;
112 
113   // Calls AttachCurrentThread() if this thread is not attached at construction.
114   // Also ensures that DetachCurrentThread() is called at destruction.
115   AttachCurrentThreadIfNeeded attach_thread_if_needed_;
116 
117   // Wraps the JNI interface pointer and methods associated with it.
118   std::unique_ptr<JNIEnvironment> j_environment_;
119 
120   // Contains factory method for creating the Java object.
121   std::unique_ptr<NativeRegistration> j_native_registration_;
122 
123   // Wraps the Java specific parts of the AudioTrackJni class.
124   std::unique_ptr<AudioTrackJni::JavaAudioTrack> j_audio_track_;
125 
126   // Contains audio parameters provided to this class at construction by the
127   // AudioManager.
128   const AudioParameters audio_parameters_;
129 
130   // Cached copy of address to direct audio buffer owned by |j_audio_track_|.
131   void* direct_buffer_address_;
132 
133   // Number of bytes in the direct audio buffer owned by |j_audio_track_|.
134   size_t direct_buffer_capacity_in_bytes_;
135 
136   // Number of audio frames per audio buffer. Each audio frame corresponds to
137   // one sample of PCM mono data at 16 bits per sample. Hence, each audio
138   // frame contains 2 bytes (given that the Java layer only supports mono).
139   // Example: 480 for 48000 Hz or 441 for 44100 Hz.
140   size_t frames_per_buffer_;
141 
142   bool initialized_;
143 
144   bool playing_;
145 
146   // Raw pointer handle provided to us in AttachAudioBuffer(). Owned by the
147   // AudioDeviceModuleImpl class and called by AudioDeviceModule::Create().
148   // The AudioDeviceBuffer is a member of the AudioDeviceModuleImpl instance
149   // and therefore outlives this object.
150   AudioDeviceBuffer* audio_device_buffer_;
151 };
152 
153 }  // namespace webrtc
154 
155 #endif  // MODULES_AUDIO_DEVICE_ANDROID_AUDIO_TRACK_JNI_H_
156