Skip to content
Snippets Groups Projects
Commit 2377472f authored by Adrien Béraud's avatar Adrien Béraud
Browse files

OpenSL: prevent buffer copy, cleanup

Make AudioQueue and sample_buf non-copyable to avoid
implicit copies and memory corruption.
Fixes crashes on Android 4.4

Change-Id: I274dc5d3ecf63ea328e577e69e217f572408db57
parent c9b0a0f3
No related branches found
No related tags found
No related merge requests found
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "audio/audiobuffer.h" #include "audio/audiobuffer.h"
#include "logger.h" #include "logger.h"
#include "noncopyable.h"
#include <SLES/OpenSLES.h> #include <SLES/OpenSLES.h>
#include <sys/types.h> #include <sys/types.h>
...@@ -146,6 +147,7 @@ public: ...@@ -146,6 +147,7 @@ public:
} }
private: private:
NON_COPYABLE(ProducerConsumerQueue);
std::vector<T> buffer_; std::vector<T> buffer_;
// forcing cache line alignment to eliminate false sharing of the // forcing cache line alignment to eliminate false sharing of the
...@@ -162,9 +164,16 @@ struct sample_buf { ...@@ -162,9 +164,16 @@ struct sample_buf {
size_t size_ {0}; // audio sample size (n buf) in byte size_t size_ {0}; // audio sample size (n buf) in byte
sample_buf() {} sample_buf() {}
sample_buf(size_t alloc, size_t size) : buf_(new uint8_t[alloc]), cap_(size) {} sample_buf(size_t alloc, size_t size) : buf_(new uint8_t[alloc]), cap_(size) {}
sample_buf(sample_buf&& o) : buf_(o.buf_), cap_(o.cap_), size_(o.size_) {
o.buf_ = nullptr;
o.cap_ = 0;
o.size_ = 0;
}
~sample_buf() { ~sample_buf() {
if (buf_) delete[] buf_; if (buf_) delete[] buf_;
} }
NON_COPYABLE(sample_buf);
}; };
using AudioQueue = ProducerConsumerQueue<sample_buf*>; using AudioQueue = ProducerConsumerQueue<sample_buf*>;
......
...@@ -48,7 +48,6 @@ namespace ring { ...@@ -48,7 +48,6 @@ namespace ring {
// Constructor // Constructor
OpenSLLayer::OpenSLLayer(const AudioPreference &pref) OpenSLLayer::OpenSLLayer(const AudioPreference &pref)
: AudioLayer(pref), : AudioLayer(pref),
audioInputDescriptor_(),
mainRingBuffer_(Manager::instance().getRingBufferPool().getRingBuffer(RingBufferPool::DEFAULT_ID)) mainRingBuffer_(Manager::instance().getRingBufferPool().getRingBuffer(RingBufferPool::DEFAULT_ID))
{} {}
...@@ -408,25 +407,23 @@ OpenSLLayer::getCaptureDeviceList() const ...@@ -408,25 +407,23 @@ OpenSLLayer::getCaptureDeviceList() const
SLresult res; SLresult res;
// Get the Audio IO DEVICE CAPABILITIES interface, implicit // Get the Audio IO DEVICE CAPABILITIES interface, implicit
RING_DBG("Get the Audio IO DEVICE CAPABILITIES interface, implicit"); SLAudioIODeviceCapabilitiesItf deviceCapabilities {nullptr};
res = (*engineObject_)->GetInterface(engineObject_, SL_IID_AUDIOIODEVICECAPABILITIES, (void*)&deviceCapabilities);
res = (*engineObject_)->GetInterface(engineObject_, SL_IID_AUDIOIODEVICECAPABILITIES, (void*)&AudioIODeviceCapabilitiesItf);
if (res != SL_RESULT_SUCCESS) if (res != SL_RESULT_SUCCESS)
return captureDeviceList; return captureDeviceList;
RING_DBG("Get the Audio IO DEVICE CAPABILITIES interface, implicit");
numInputs = MAX_NUMBER_INPUT_DEVICES; numInputs = MAX_NUMBER_INPUT_DEVICES;
res = (*deviceCapabilities)->GetAvailableAudioInputs(deviceCapabilities, &numInputs, InputDeviceIDs);
res = (*AudioIODeviceCapabilitiesItf)->GetAvailableAudioInputs(AudioIODeviceCapabilitiesItf, &numInputs, InputDeviceIDs);
if (res != SL_RESULT_SUCCESS) if (res != SL_RESULT_SUCCESS)
return captureDeviceList; return captureDeviceList;
// Search for either earpiece microphone or headset microphone input // Search for either earpiece microphone or headset microphone input
// device - with a preference for the latter // device - with a preference for the latter
for (int i = 0; i < numInputs; i++) { for (int i = 0; i < numInputs; i++) {
res = (*AudioIODeviceCapabilitiesItf)->QueryAudioInputCapabilities(AudioIODeviceCapabilitiesItf, SLAudioInputDescriptor audioInputDescriptor_;
res = (*deviceCapabilities)->QueryAudioInputCapabilities(deviceCapabilities,
InputDeviceIDs[i], InputDeviceIDs[i],
(SLAudioInputDescriptor_ *)&audioInputDescriptor_); &audioInputDescriptor_);
if (res != SL_RESULT_SUCCESS) if (res != SL_RESULT_SUCCESS)
return captureDeviceList; return captureDeviceList;
......
...@@ -188,9 +188,6 @@ class OpenSLLayer : public AudioLayer { ...@@ -188,9 +188,6 @@ class OpenSLLayer : public AudioLayer {
std::vector<sample_buf> bufs_; std::vector<sample_buf> bufs_;
SLAudioIODeviceCapabilitiesItf AudioIODeviceCapabilitiesItf {nullptr};
SLAudioInputDescriptor audioInputDescriptor_;
AudioFormat hardwareFormat_ {AudioFormat::MONO()}; AudioFormat hardwareFormat_ {AudioFormat::MONO()};
size_t hardwareBuffSize_ {BUFFER_SIZE}; size_t hardwareBuffSize_ {BUFFER_SIZE};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment