audio_track_jni.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #ifndef MODULES_AUDIO_DEVICE_ANDROID_AUDIO_TRACK_JNI_H_
  11. #define MODULES_AUDIO_DEVICE_ANDROID_AUDIO_TRACK_JNI_H_
  12. #include <jni.h>
  13. #include <memory>
  14. #include "modules/audio_device/android/audio_common.h"
  15. #include "modules/audio_device/android/audio_manager.h"
  16. #include "modules/audio_device/audio_device_generic.h"
  17. #include "modules/audio_device/include/audio_device_defines.h"
  18. #include "modules/utility/include/helpers_android.h"
  19. #include "modules/utility/include/jvm_android.h"
  20. #include "rtc_base/thread_checker.h"
  21. namespace webrtc {
  22. // Implements 16-bit mono PCM audio output support for Android using the Java
  23. // AudioTrack interface. Most of the work is done by its Java counterpart in
  24. // WebRtcAudioTrack.java. This class is created and lives on a thread in
  25. // C++-land, but decoded audio buffers are requested on a high-priority
  26. // thread managed by the Java class.
  27. //
  28. // An instance must be created and destroyed on one and the same thread.
  29. // All public methods must also be called on the same thread. A thread checker
  30. // will RTC_DCHECK if any method is called on an invalid thread.
  31. //
  32. // This class uses JvmThreadConnector to attach to a Java VM if needed
  33. // and detach when the object goes out of scope. Additional thread checking
  34. // guarantees that no other (possibly non attached) thread is used.
  35. class AudioTrackJni {
  36. public:
  37. // Wraps the Java specific parts of the AudioTrackJni into one helper class.
  38. class JavaAudioTrack {
  39. public:
  40. JavaAudioTrack(NativeRegistration* native_registration,
  41. std::unique_ptr<GlobalRef> audio_track);
  42. ~JavaAudioTrack();
  43. bool InitPlayout(int sample_rate, int channels);
  44. bool StartPlayout();
  45. bool StopPlayout();
  46. bool SetStreamVolume(int volume);
  47. int GetStreamMaxVolume();
  48. int GetStreamVolume();
  49. private:
  50. std::unique_ptr<GlobalRef> audio_track_;
  51. jmethodID init_playout_;
  52. jmethodID start_playout_;
  53. jmethodID stop_playout_;
  54. jmethodID set_stream_volume_;
  55. jmethodID get_stream_max_volume_;
  56. jmethodID get_stream_volume_;
  57. jmethodID get_buffer_size_in_frames_;
  58. };
  59. explicit AudioTrackJni(AudioManager* audio_manager);
  60. ~AudioTrackJni();
  61. int32_t Init();
  62. int32_t Terminate();
  63. int32_t InitPlayout();
  64. bool PlayoutIsInitialized() const { return initialized_; }
  65. int32_t StartPlayout();
  66. int32_t StopPlayout();
  67. bool Playing() const { return playing_; }
  68. int SpeakerVolumeIsAvailable(bool& available);
  69. int SetSpeakerVolume(uint32_t volume);
  70. int SpeakerVolume(uint32_t& volume) const;
  71. int MaxSpeakerVolume(uint32_t& max_volume) const;
  72. int MinSpeakerVolume(uint32_t& min_volume) const;
  73. void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer);
  74. private:
  75. // Called from Java side so we can cache the address of the Java-manged
  76. // |byte_buffer| in |direct_buffer_address_|. The size of the buffer
  77. // is also stored in |direct_buffer_capacity_in_bytes_|.
  78. // Called on the same thread as the creating thread.
  79. static void JNICALL CacheDirectBufferAddress(JNIEnv* env,
  80. jobject obj,
  81. jobject byte_buffer,
  82. jlong nativeAudioTrack);
  83. void OnCacheDirectBufferAddress(JNIEnv* env, jobject byte_buffer);
  84. // Called periodically by the Java based WebRtcAudioTrack object when
  85. // playout has started. Each call indicates that |length| new bytes should
  86. // be written to the memory area |direct_buffer_address_| for playout.
  87. // This method is called on a high-priority thread from Java. The name of
  88. // the thread is 'AudioTrackThread'.
  89. static void JNICALL GetPlayoutData(JNIEnv* env,
  90. jobject obj,
  91. jint length,
  92. jlong nativeAudioTrack);
  93. void OnGetPlayoutData(size_t length);
  94. // Stores thread ID in constructor.
  95. rtc::ThreadChecker thread_checker_;
  96. // Stores thread ID in first call to OnGetPlayoutData() from high-priority
  97. // thread in Java. Detached during construction of this object.
  98. rtc::ThreadChecker thread_checker_java_;
  99. // Calls JavaVM::AttachCurrentThread() if this thread is not attached at
  100. // construction.
  101. // Also ensures that DetachCurrentThread() is called at destruction.
  102. JvmThreadConnector attach_thread_if_needed_;
  103. // Wraps the JNI interface pointer and methods associated with it.
  104. std::unique_ptr<JNIEnvironment> j_environment_;
  105. // Contains factory method for creating the Java object.
  106. std::unique_ptr<NativeRegistration> j_native_registration_;
  107. // Wraps the Java specific parts of the AudioTrackJni class.
  108. std::unique_ptr<AudioTrackJni::JavaAudioTrack> j_audio_track_;
  109. // Contains audio parameters provided to this class at construction by the
  110. // AudioManager.
  111. const AudioParameters audio_parameters_;
  112. // Cached copy of address to direct audio buffer owned by |j_audio_track_|.
  113. void* direct_buffer_address_;
  114. // Number of bytes in the direct audio buffer owned by |j_audio_track_|.
  115. size_t direct_buffer_capacity_in_bytes_;
  116. // Number of audio frames per audio buffer. Each audio frame corresponds to
  117. // one sample of PCM mono data at 16 bits per sample. Hence, each audio
  118. // frame contains 2 bytes (given that the Java layer only supports mono).
  119. // Example: 480 for 48000 Hz or 441 for 44100 Hz.
  120. size_t frames_per_buffer_;
  121. bool initialized_;
  122. bool playing_;
  123. // Raw pointer handle provided to us in AttachAudioBuffer(). Owned by the
  124. // AudioDeviceModuleImpl class and called by AudioDeviceModule::Create().
  125. // The AudioDeviceBuffer is a member of the AudioDeviceModuleImpl instance
  126. // and therefore outlives this object.
  127. AudioDeviceBuffer* audio_device_buffer_;
  128. };
  129. } // namespace webrtc
  130. #endif // MODULES_AUDIO_DEVICE_ANDROID_AUDIO_TRACK_JNI_H_