From e0627a18e64c49a201942b01c8da73965756e9ad Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Mon, 17 Sep 2018 17:28:47 -0400
Subject: [PATCH] OpenSL: fix occasional crash

Change-Id: I8fcee2c5d722ca56db65f1ccc1051a3a4ee63664
---
 src/media/audio/opensl/audio_player.h   |  3 +++
 src/media/audio/opensl/audio_recorder.h |  3 +++
 src/media/audio/opensl/buf_manager.h    | 31 +++++++++----------------
 src/media/audio/opensl/opensllayer.cpp  | 13 +++++++++++
 src/media/audio/opensl/opensllayer.h    | 14 +++++------
 5 files changed, 37 insertions(+), 27 deletions(-)

diff --git a/src/media/audio/opensl/audio_player.h b/src/media/audio/opensl/audio_player.h
index 8f50bb2623..8a2b69ae21 100644
--- a/src/media/audio/opensl/audio_player.h
+++ b/src/media/audio/opensl/audio_player.h
@@ -19,6 +19,7 @@
 
 #include "audio_common.h"
 #include "buf_manager.h"
+#include "noncopyable.h"
 
 #include <sys/types.h>
 #include <SLES/OpenSLES_Android.h>
@@ -45,6 +46,8 @@ class AudioPlayer {
 public:
     explicit AudioPlayer(ring::AudioFormat sampleFormat, SLEngineItf engine, SLint32 streamType);
     ~AudioPlayer();
+    NON_COPYABLE(AudioPlayer);
+
     bool start();
     void stop();
     bool started() const;
diff --git a/src/media/audio/opensl/audio_recorder.h b/src/media/audio/opensl/audio_recorder.h
index b528b8f20b..a091d210fb 100644
--- a/src/media/audio/opensl/audio_recorder.h
+++ b/src/media/audio/opensl/audio_recorder.h
@@ -23,6 +23,7 @@
 
 #include "audio_common.h"
 #include "buf_manager.h"
+#include "noncopyable.h"
 
 namespace ring {
 namespace opensl {
@@ -43,6 +44,8 @@ class AudioRecorder {
 public:
     explicit AudioRecorder(ring::AudioFormat, SLEngineItf engineEngine);
     ~AudioRecorder();
+    NON_COPYABLE(AudioRecorder);
+
     bool start();
     bool stop();
     void setBufQueues(AudioQueue *freeQ, AudioQueue *recQ);
diff --git a/src/media/audio/opensl/buf_manager.h b/src/media/audio/opensl/buf_manager.h
index 497402e648..0260bf0d6e 100644
--- a/src/media/audio/opensl/buf_manager.h
+++ b/src/media/audio/opensl/buf_manager.h
@@ -149,13 +149,8 @@ public:
 private:
     NON_COPYABLE(ProducerConsumerQueue);
     std::vector<T> buffer_;
-
-    // forcing cache line alignment to eliminate false sharing of the
-    // frequently-updated read and write pointers. The object is to never
-    // let these get into the "shared" state where they'd cause a cache miss
-    // for every write.
-    alignas(CACHE_ALIGN) std::atomic<int> read_ { 0 };
-    alignas(CACHE_ALIGN) std::atomic<int> write_ { 0 };
+    std::atomic<int> read_ { 0 };
+    std::atomic<int> write_ { 0 };
 };
 
 struct sample_buf {
@@ -169,6 +164,15 @@ struct sample_buf {
         o.cap_ = 0;
         o.size_ = 0;
     }
+    sample_buf& operator=(sample_buf&& o) {
+        buf_ = o.buf_;
+        cap_ = o.cap_;
+        size_ = o.size_;
+        o.buf_ = nullptr;
+        o.cap_ = 0;
+        o.size_ = 0;
+        return *this;
+    }
 
     ~sample_buf() {
         if (buf_) delete[] buf_;
@@ -177,16 +181,3 @@ struct sample_buf {
 };
 
 using AudioQueue = ProducerConsumerQueue<sample_buf*>;
-
-__inline__ std::vector<sample_buf>
-allocateSampleBufs(unsigned count, size_t sizeInByte)
-{
-    std::vector<sample_buf> bufs;
-    if (!count || !sizeInByte)
-        return bufs;
-    bufs.reserve(count);
-    size_t allocSize = (sizeInByte + 3) & ~3;   // padding to 4 bytes aligned
-    for(unsigned i =0; i < count; i++)
-        bufs.emplace_back(allocSize, sizeInByte);
-    return bufs;
-}
diff --git a/src/media/audio/opensl/opensllayer.cpp b/src/media/audio/opensl/opensllayer.cpp
index d02e010e19..51e0283c57 100644
--- a/src/media/audio/opensl/opensllayer.cpp
+++ b/src/media/audio/opensl/opensllayer.cpp
@@ -136,6 +136,19 @@ OpenSLLayer::stopStream()
     bufs_.clear();
 }
 
+std::vector<sample_buf>
+allocateSampleBufs(unsigned count, size_t sizeInByte)
+{
+    std::vector<sample_buf> bufs;
+    if (!count || !sizeInByte)
+        return bufs;
+    bufs.reserve(count);
+    size_t allocSize = (sizeInByte + 3) & ~3;   // padding to 4 bytes aligned
+    for(unsigned i =0; i < count; i++)
+        bufs.emplace_back(allocSize, sizeInByte);
+    return bufs;
+}
+
 void
 OpenSLLayer::initAudioEngine()
 {
diff --git a/src/media/audio/opensl/opensllayer.h b/src/media/audio/opensl/opensllayer.h
index f3785b8fc3..03da9ca85d 100644
--- a/src/media/audio/opensl/opensllayer.h
+++ b/src/media/audio/opensl/opensllayer.h
@@ -175,18 +175,18 @@ class OpenSLLayer : public AudioLayer {
         AudioQueue     freeRingBufQueue_ {BUF_COUNT};
         AudioQueue     ringBufQueue_ {BUF_COUNT};
 
-        std::thread    playThread;
-        std::mutex     playMtx;
-        std::condition_variable playCv;
+        std::mutex     playMtx {};
+        std::condition_variable playCv {};
+        std::thread    playThread {};
 
         AudioQueue     freeRecBufQueue_ {BUF_COUNT};    //Owner of the queue
         AudioQueue     recBufQueue_ {BUF_COUNT};     //Owner of the queue
 
-        std::thread    recThread;
-        std::mutex     recMtx;
-        std::condition_variable recCv;
+        std::mutex     recMtx {};
+        std::condition_variable recCv {};
+        std::thread    recThread {};
 
-        std::vector<sample_buf> bufs_;
+        std::vector<sample_buf> bufs_ {};
 
         AudioFormat hardwareFormat_ {AudioFormat::MONO()};
         size_t hardwareBuffSize_ {BUFFER_SIZE};
-- 
GitLab