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_default",
|
||||||
":audio_device_generic",
|
":audio_device_generic",
|
||||||
"../../api:array_view",
|
"../../api:array_view",
|
||||||
|
"../../api:refcountedbase",
|
||||||
"../../api:scoped_refptr",
|
"../../api:scoped_refptr",
|
||||||
"../../api/task_queue",
|
"../../api/task_queue",
|
||||||
"../../api/task_queue:global_task_queue_factory",
|
"../../api/task_queue:global_task_queue_factory",
|
||||||
|
|||||||
@ -913,6 +913,7 @@ if (is_android) {
|
|||||||
":base_jni",
|
":base_jni",
|
||||||
":java_audio_device_module",
|
":java_audio_device_module",
|
||||||
":opensles_audio_device_module",
|
":opensles_audio_device_module",
|
||||||
|
"../../api:scoped_refptr",
|
||||||
"../../modules/audio_device",
|
"../../modules/audio_device",
|
||||||
"../../rtc_base:checks",
|
"../../rtc_base:checks",
|
||||||
"../../rtc_base:rtc_base_approved",
|
"../../rtc_base:rtc_base_approved",
|
||||||
@ -1201,6 +1202,8 @@ if (is_android) {
|
|||||||
":audio_device_module_base",
|
":audio_device_module_base",
|
||||||
":base_jni",
|
":base_jni",
|
||||||
"../../api:array_view",
|
"../../api:array_view",
|
||||||
|
"../../api:refcountedbase",
|
||||||
|
"../../api:scoped_refptr",
|
||||||
"../../modules/audio_device",
|
"../../modules/audio_device",
|
||||||
"../../modules/audio_device:audio_device_buffer",
|
"../../modules/audio_device:audio_device_buffer",
|
||||||
"../../rtc_base:checks",
|
"../../rtc_base:checks",
|
||||||
|
|||||||
@ -14,6 +14,7 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
#include "absl/memory/memory.h"
|
#include "absl/memory/memory.h"
|
||||||
|
#include "api/scoped_refptr.h"
|
||||||
#include "rtc_base/logging.h"
|
#include "rtc_base/logging.h"
|
||||||
#include "rtc_base/ref_count.h"
|
#include "rtc_base/ref_count.h"
|
||||||
#include "rtc_base/ref_counted_object.h"
|
#include "rtc_base/ref_counted_object.h"
|
||||||
@ -109,9 +110,10 @@ rtc::scoped_refptr<AudioDeviceModule> CreateOpenSLESAudioDeviceModule(
|
|||||||
GetDefaultAudioParameters(env, application_context, &input_parameters,
|
GetDefaultAudioParameters(env, application_context, &input_parameters,
|
||||||
&output_parameters);
|
&output_parameters);
|
||||||
// Create ADM from OpenSLESRecorder and OpenSLESPlayer.
|
// Create ADM from OpenSLESRecorder and OpenSLESPlayer.
|
||||||
auto engine_manager = absl::make_unique<jni::OpenSLEngineManager>();
|
rtc::scoped_refptr<jni::OpenSLEngineManager> engine_manager(
|
||||||
auto audio_input = absl::make_unique<jni::OpenSLESRecorder>(
|
new jni::OpenSLEngineManager());
|
||||||
input_parameters, engine_manager.get());
|
auto audio_input = absl::make_unique<jni::OpenSLESRecorder>(input_parameters,
|
||||||
|
engine_manager);
|
||||||
auto audio_output = absl::make_unique<jni::OpenSLESPlayer>(
|
auto audio_output = absl::make_unique<jni::OpenSLESPlayer>(
|
||||||
output_parameters, std::move(engine_manager));
|
output_parameters, std::move(engine_manager));
|
||||||
return CreateAudioDeviceModuleFromInputAndOutput(
|
return CreateAudioDeviceModuleFromInputAndOutput(
|
||||||
@ -138,8 +140,11 @@ CreateJavaInputAndOpenSLESOutputAudioDeviceModule(JNIEnv* env,
|
|||||||
env, input_parameters, jni::kLowLatencyModeDelayEstimateInMilliseconds,
|
env, input_parameters, jni::kLowLatencyModeDelayEstimateInMilliseconds,
|
||||||
jni::AudioRecordJni::CreateJavaWebRtcAudioRecord(env, j_context,
|
jni::AudioRecordJni::CreateJavaWebRtcAudioRecord(env, j_context,
|
||||||
j_audio_manager));
|
j_audio_manager));
|
||||||
|
|
||||||
|
rtc::scoped_refptr<jni::OpenSLEngineManager> engine_manager(
|
||||||
|
new jni::OpenSLEngineManager());
|
||||||
auto audio_output = absl::make_unique<jni::OpenSLESPlayer>(
|
auto audio_output = absl::make_unique<jni::OpenSLESPlayer>(
|
||||||
output_parameters, absl::make_unique<jni::OpenSLEngineManager>());
|
output_parameters, std::move(engine_manager));
|
||||||
return CreateAudioDeviceModuleFromInputAndOutput(
|
return CreateAudioDeviceModuleFromInputAndOutput(
|
||||||
AudioDeviceModule::kAndroidJavaInputAndOpenSLESOutputAudio,
|
AudioDeviceModule::kAndroidJavaInputAndOpenSLESOutputAudio,
|
||||||
false /* use_stereo_input */, false /* use_stereo_output */,
|
false /* use_stereo_input */, false /* use_stereo_output */,
|
||||||
|
|||||||
@ -14,6 +14,7 @@
|
|||||||
#include <SLES/OpenSLES.h>
|
#include <SLES/OpenSLES.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#include "api/ref_counted_base.h"
|
||||||
#include "rtc_base/checks.h"
|
#include "rtc_base/checks.h"
|
||||||
#include "rtc_base/logging.h"
|
#include "rtc_base/logging.h"
|
||||||
#include "rtc_base/thread_checker.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
|
// 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.
|
// since OpenSL ES for Android only supports a single engine per application.
|
||||||
// Subsequent calls returns the already created engine.
|
// Subsequent calls returns the already created engine.
|
||||||
// Note: This class must be used single threaded and this is enfored by a thread
|
// Note: This class must be used single threaded and this is enforced by a
|
||||||
// checker.
|
// thread checker.
|
||||||
class OpenSLEngineManager {
|
class OpenSLEngineManager : public rtc::RefCountedBase {
|
||||||
public:
|
public:
|
||||||
OpenSLEngineManager();
|
OpenSLEngineManager();
|
||||||
~OpenSLEngineManager();
|
~OpenSLEngineManager() override;
|
||||||
SLObjectItf GetOpenSLEngine();
|
SLObjectItf GetOpenSLEngine();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@ -44,7 +44,7 @@ namespace jni {
|
|||||||
|
|
||||||
OpenSLESPlayer::OpenSLESPlayer(
|
OpenSLESPlayer::OpenSLESPlayer(
|
||||||
const AudioParameters& audio_parameters,
|
const AudioParameters& audio_parameters,
|
||||||
std::unique_ptr<OpenSLEngineManager> engine_manager)
|
rtc::scoped_refptr<OpenSLEngineManager> engine_manager)
|
||||||
: audio_parameters_(audio_parameters),
|
: audio_parameters_(audio_parameters),
|
||||||
audio_device_buffer_(nullptr),
|
audio_device_buffer_(nullptr),
|
||||||
initialized_(false),
|
initialized_(false),
|
||||||
|
|||||||
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include "absl/types/optional.h"
|
#include "absl/types/optional.h"
|
||||||
|
#include "api/scoped_refptr.h"
|
||||||
#include "modules/audio_device/audio_device_buffer.h"
|
#include "modules/audio_device/audio_device_buffer.h"
|
||||||
#include "modules/audio_device/fine_audio_buffer.h"
|
#include "modules/audio_device/fine_audio_buffer.h"
|
||||||
#include "modules/audio_device/include/audio_device_defines.h"
|
#include "modules/audio_device/include/audio_device_defines.h"
|
||||||
@ -60,7 +61,7 @@ class OpenSLESPlayer : public AudioOutput {
|
|||||||
static const int kNumOfOpenSLESBuffers = 2;
|
static const int kNumOfOpenSLESBuffers = 2;
|
||||||
|
|
||||||
OpenSLESPlayer(const AudioParameters& audio_parameters,
|
OpenSLESPlayer(const AudioParameters& audio_parameters,
|
||||||
std::unique_ptr<OpenSLEngineManager> engine_manager);
|
rtc::scoped_refptr<OpenSLEngineManager> engine_manager);
|
||||||
~OpenSLESPlayer() override;
|
~OpenSLESPlayer() override;
|
||||||
|
|
||||||
int Init() override;
|
int Init() override;
|
||||||
@ -159,7 +160,7 @@ class OpenSLESPlayer : public AudioOutput {
|
|||||||
// Example (kNumOfOpenSLESBuffers = 2): counts 0, 1, 0, 1, ...
|
// Example (kNumOfOpenSLESBuffers = 2): counts 0, 1, 0, 1, ...
|
||||||
int buffer_index_;
|
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.
|
// This interface exposes creation methods for all the OpenSL ES object types.
|
||||||
// It is the OpenSL ES API entry point.
|
// It is the OpenSL ES API entry point.
|
||||||
SLEngineItf engine_;
|
SLEngineItf engine_;
|
||||||
|
|||||||
@ -43,13 +43,14 @@ namespace webrtc {
|
|||||||
|
|
||||||
namespace jni {
|
namespace jni {
|
||||||
|
|
||||||
OpenSLESRecorder::OpenSLESRecorder(const AudioParameters& audio_parameters,
|
OpenSLESRecorder::OpenSLESRecorder(
|
||||||
OpenSLEngineManager* engine_manager)
|
const AudioParameters& audio_parameters,
|
||||||
|
rtc::scoped_refptr<OpenSLEngineManager> engine_manager)
|
||||||
: audio_parameters_(audio_parameters),
|
: audio_parameters_(audio_parameters),
|
||||||
audio_device_buffer_(nullptr),
|
audio_device_buffer_(nullptr),
|
||||||
initialized_(false),
|
initialized_(false),
|
||||||
recording_(false),
|
recording_(false),
|
||||||
engine_manager_(engine_manager),
|
engine_manager_(std::move(engine_manager)),
|
||||||
engine_(nullptr),
|
engine_(nullptr),
|
||||||
recorder_(nullptr),
|
recorder_(nullptr),
|
||||||
simple_buffer_queue_(nullptr),
|
simple_buffer_queue_(nullptr),
|
||||||
|
|||||||
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
#include "api/scoped_refptr.h"
|
||||||
#include "modules/audio_device/audio_device_buffer.h"
|
#include "modules/audio_device/audio_device_buffer.h"
|
||||||
#include "modules/audio_device/fine_audio_buffer.h"
|
#include "modules/audio_device/fine_audio_buffer.h"
|
||||||
#include "modules/audio_device/include/audio_device_defines.h"
|
#include "modules/audio_device/include/audio_device_defines.h"
|
||||||
@ -63,7 +64,7 @@ class OpenSLESRecorder : public AudioInput {
|
|||||||
static const int kNumOfOpenSLESBuffers = 2;
|
static const int kNumOfOpenSLESBuffers = 2;
|
||||||
|
|
||||||
OpenSLESRecorder(const AudioParameters& audio_parameters,
|
OpenSLESRecorder(const AudioParameters& audio_parameters,
|
||||||
OpenSLEngineManager* engine_manager);
|
rtc::scoped_refptr<OpenSLEngineManager> engine_manager);
|
||||||
~OpenSLESRecorder() override;
|
~OpenSLESRecorder() override;
|
||||||
|
|
||||||
int Init() override;
|
int Init() override;
|
||||||
@ -148,7 +149,7 @@ class OpenSLESRecorder : public AudioInput {
|
|||||||
bool initialized_;
|
bool initialized_;
|
||||||
bool recording_;
|
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.
|
// This interface exposes creation methods for all the OpenSL ES object types.
|
||||||
// It is the OpenSL ES API entry point.
|
// It is the OpenSL ES API entry point.
|
||||||
SLEngineItf engine_;
|
SLEngineItf engine_;
|
||||||
|
|||||||
Reference in New Issue
Block a user