Use scoped_refptr to share the instance of OpenSLEngineManager
Use rtc::scoped_refptr instead of std::unique_ptr to hold the instance of OpenSLEngineManager; this makes it safe to share it between OpenSLESRecorder and OpenSLESPlayer. Bug: webrtc:10436 Change-Id: Ibd0717e5410020c89a40bfdb05953a02378a6a4b Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/128651 Commit-Queue: Lu Liu <lliuu@webrtc.org> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Reviewed-by: Henrik Andreassson <henrika@webrtc.org> Cr-Commit-Position: refs/heads/master@{#27253}
This commit is contained in:
@ -201,6 +201,7 @@ rtc_source_set("audio_device_impl") {
|
||||
":audio_device_default",
|
||||
":audio_device_generic",
|
||||
"../../api:array_view",
|
||||
"../../api:refcountedbase",
|
||||
"../../api:scoped_refptr",
|
||||
"../../api/task_queue",
|
||||
"../../api/task_queue:global_task_queue_factory",
|
||||
|
||||
@ -913,6 +913,7 @@ if (is_android) {
|
||||
":base_jni",
|
||||
":java_audio_device_module",
|
||||
":opensles_audio_device_module",
|
||||
"../../api:scoped_refptr",
|
||||
"../../modules/audio_device",
|
||||
"../../rtc_base:checks",
|
||||
"../../rtc_base:rtc_base_approved",
|
||||
@ -1201,6 +1202,8 @@ if (is_android) {
|
||||
":audio_device_module_base",
|
||||
":base_jni",
|
||||
"../../api:array_view",
|
||||
"../../api:refcountedbase",
|
||||
"../../api:scoped_refptr",
|
||||
"../../modules/audio_device",
|
||||
"../../modules/audio_device:audio_device_buffer",
|
||||
"../../rtc_base:checks",
|
||||
|
||||
@ -14,6 +14,7 @@
|
||||
#include <utility>
|
||||
|
||||
#include "absl/memory/memory.h"
|
||||
#include "api/scoped_refptr.h"
|
||||
#include "rtc_base/logging.h"
|
||||
#include "rtc_base/ref_count.h"
|
||||
#include "rtc_base/ref_counted_object.h"
|
||||
@ -109,9 +110,10 @@ rtc::scoped_refptr<AudioDeviceModule> CreateOpenSLESAudioDeviceModule(
|
||||
GetDefaultAudioParameters(env, application_context, &input_parameters,
|
||||
&output_parameters);
|
||||
// Create ADM from OpenSLESRecorder and OpenSLESPlayer.
|
||||
auto engine_manager = absl::make_unique<jni::OpenSLEngineManager>();
|
||||
auto audio_input = absl::make_unique<jni::OpenSLESRecorder>(
|
||||
input_parameters, engine_manager.get());
|
||||
rtc::scoped_refptr<jni::OpenSLEngineManager> engine_manager(
|
||||
new jni::OpenSLEngineManager());
|
||||
auto audio_input = absl::make_unique<jni::OpenSLESRecorder>(input_parameters,
|
||||
engine_manager);
|
||||
auto audio_output = absl::make_unique<jni::OpenSLESPlayer>(
|
||||
output_parameters, std::move(engine_manager));
|
||||
return CreateAudioDeviceModuleFromInputAndOutput(
|
||||
@ -138,8 +140,11 @@ CreateJavaInputAndOpenSLESOutputAudioDeviceModule(JNIEnv* env,
|
||||
env, input_parameters, jni::kLowLatencyModeDelayEstimateInMilliseconds,
|
||||
jni::AudioRecordJni::CreateJavaWebRtcAudioRecord(env, j_context,
|
||||
j_audio_manager));
|
||||
|
||||
rtc::scoped_refptr<jni::OpenSLEngineManager> engine_manager(
|
||||
new jni::OpenSLEngineManager());
|
||||
auto audio_output = absl::make_unique<jni::OpenSLESPlayer>(
|
||||
output_parameters, absl::make_unique<jni::OpenSLEngineManager>());
|
||||
output_parameters, std::move(engine_manager));
|
||||
return CreateAudioDeviceModuleFromInputAndOutput(
|
||||
AudioDeviceModule::kAndroidJavaInputAndOpenSLESOutputAudio,
|
||||
false /* use_stereo_input */, false /* use_stereo_output */,
|
||||
|
||||
@ -14,6 +14,7 @@
|
||||
#include <SLES/OpenSLES.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "api/ref_counted_base.h"
|
||||
#include "rtc_base/checks.h"
|
||||
#include "rtc_base/logging.h"
|
||||
#include "rtc_base/thread_checker.h"
|
||||
@ -65,12 +66,12 @@ typedef ScopedSLObject<SLObjectItf, const SLObjectItf_*> ScopedSLObjectItf;
|
||||
// a reference to it. The engine object is only created at the first call
|
||||
// since OpenSL ES for Android only supports a single engine per application.
|
||||
// Subsequent calls returns the already created engine.
|
||||
// Note: This class must be used single threaded and this is enfored by a thread
|
||||
// checker.
|
||||
class OpenSLEngineManager {
|
||||
// Note: This class must be used single threaded and this is enforced by a
|
||||
// thread checker.
|
||||
class OpenSLEngineManager : public rtc::RefCountedBase {
|
||||
public:
|
||||
OpenSLEngineManager();
|
||||
~OpenSLEngineManager();
|
||||
~OpenSLEngineManager() override;
|
||||
SLObjectItf GetOpenSLEngine();
|
||||
|
||||
private:
|
||||
|
||||
@ -44,7 +44,7 @@ namespace jni {
|
||||
|
||||
OpenSLESPlayer::OpenSLESPlayer(
|
||||
const AudioParameters& audio_parameters,
|
||||
std::unique_ptr<OpenSLEngineManager> engine_manager)
|
||||
rtc::scoped_refptr<OpenSLEngineManager> engine_manager)
|
||||
: audio_parameters_(audio_parameters),
|
||||
audio_device_buffer_(nullptr),
|
||||
initialized_(false),
|
||||
|
||||
@ -17,6 +17,7 @@
|
||||
|
||||
#include <memory>
|
||||
#include "absl/types/optional.h"
|
||||
#include "api/scoped_refptr.h"
|
||||
#include "modules/audio_device/audio_device_buffer.h"
|
||||
#include "modules/audio_device/fine_audio_buffer.h"
|
||||
#include "modules/audio_device/include/audio_device_defines.h"
|
||||
@ -60,7 +61,7 @@ class OpenSLESPlayer : public AudioOutput {
|
||||
static const int kNumOfOpenSLESBuffers = 2;
|
||||
|
||||
OpenSLESPlayer(const AudioParameters& audio_parameters,
|
||||
std::unique_ptr<OpenSLEngineManager> engine_manager);
|
||||
rtc::scoped_refptr<OpenSLEngineManager> engine_manager);
|
||||
~OpenSLESPlayer() override;
|
||||
|
||||
int Init() override;
|
||||
@ -159,7 +160,7 @@ class OpenSLESPlayer : public AudioOutput {
|
||||
// Example (kNumOfOpenSLESBuffers = 2): counts 0, 1, 0, 1, ...
|
||||
int buffer_index_;
|
||||
|
||||
std::unique_ptr<OpenSLEngineManager> engine_manager_;
|
||||
const rtc::scoped_refptr<OpenSLEngineManager> engine_manager_;
|
||||
// This interface exposes creation methods for all the OpenSL ES object types.
|
||||
// It is the OpenSL ES API entry point.
|
||||
SLEngineItf engine_;
|
||||
|
||||
@ -43,13 +43,14 @@ namespace webrtc {
|
||||
|
||||
namespace jni {
|
||||
|
||||
OpenSLESRecorder::OpenSLESRecorder(const AudioParameters& audio_parameters,
|
||||
OpenSLEngineManager* engine_manager)
|
||||
OpenSLESRecorder::OpenSLESRecorder(
|
||||
const AudioParameters& audio_parameters,
|
||||
rtc::scoped_refptr<OpenSLEngineManager> engine_manager)
|
||||
: audio_parameters_(audio_parameters),
|
||||
audio_device_buffer_(nullptr),
|
||||
initialized_(false),
|
||||
recording_(false),
|
||||
engine_manager_(engine_manager),
|
||||
engine_manager_(std::move(engine_manager)),
|
||||
engine_(nullptr),
|
||||
recorder_(nullptr),
|
||||
simple_buffer_queue_(nullptr),
|
||||
|
||||
@ -17,6 +17,7 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "api/scoped_refptr.h"
|
||||
#include "modules/audio_device/audio_device_buffer.h"
|
||||
#include "modules/audio_device/fine_audio_buffer.h"
|
||||
#include "modules/audio_device/include/audio_device_defines.h"
|
||||
@ -63,7 +64,7 @@ class OpenSLESRecorder : public AudioInput {
|
||||
static const int kNumOfOpenSLESBuffers = 2;
|
||||
|
||||
OpenSLESRecorder(const AudioParameters& audio_parameters,
|
||||
OpenSLEngineManager* engine_manager);
|
||||
rtc::scoped_refptr<OpenSLEngineManager> engine_manager);
|
||||
~OpenSLESRecorder() override;
|
||||
|
||||
int Init() override;
|
||||
@ -148,7 +149,7 @@ class OpenSLESRecorder : public AudioInput {
|
||||
bool initialized_;
|
||||
bool recording_;
|
||||
|
||||
OpenSLEngineManager* const engine_manager_;
|
||||
const rtc::scoped_refptr<OpenSLEngineManager> engine_manager_;
|
||||
// This interface exposes creation methods for all the OpenSL ES object types.
|
||||
// It is the OpenSL ES API entry point.
|
||||
SLEngineItf engine_;
|
||||
|
||||
Reference in New Issue
Block a user