From a37e4631b1de9e77ee31a1a030f40f4f874a0b8e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Tue, 12 Oct 2021 11:27:40 -0400
Subject: [PATCH] opensl: handle Enqueue failure

Change-Id: I4bdef901c2a2bc07dc1a9263eb0a9796655c051a
---
 src/media/audio/opensl/audio_player.cpp | 16 ++++++++++------
 src/media/audio/opensl/opensllayer.cpp  |  3 ++-
 2 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/src/media/audio/opensl/audio_player.cpp b/src/media/audio/opensl/audio_player.cpp
index 7760c803f3..913afd96f4 100644
--- a/src/media/audio/opensl/audio_player.cpp
+++ b/src/media/audio/opensl/audio_player.cpp
@@ -86,11 +86,12 @@ AudioPlayer::processSLCallback(SLAndroidSimpleBufferQueueItf bq)
             playQueue_->pop();
     }
     if (devShadowQueue_.size() == 0) {
-        // JAMI_ERR("AudioPlayer: nothing to play %zu %zu %zu", freeQueue_->size(),
-        // playQueue_->size(), devShadowQueue_.size());
         for (int i = 0; i < DEVICE_SHADOW_BUFFER_QUEUE_LEN; i++) {
-            (*bq)->Enqueue(bq, silentBuf_.buf_, silentBuf_.size_);
-            devShadowQueue_.push(&silentBuf_);
+            if ((*bq)->Enqueue(bq, silentBuf_.buf_, silentBuf_.size_) == SL_RESULT_SUCCESS) {
+                devShadowQueue_.push(&silentBuf_);
+            } else {
+                JAMI_ERR("Enqueue silentBuf_ failed");
+            }
         }
     }
 }
@@ -208,9 +209,12 @@ AudioPlayer::start()
     result = (*playItf_)->SetPlayState(playItf_, SL_PLAYSTATE_STOPPED);
     SLASSERT(result);
 
-    SLASSERT(
-        (*playBufferQueueItf_)->Enqueue(playBufferQueueItf_, silentBuf_.buf_, silentBuf_.size_));
     devShadowQueue_.push(&silentBuf_);
+    result = (*playBufferQueueItf_)->Enqueue(playBufferQueueItf_, silentBuf_.buf_, silentBuf_.size_);
+    if (result != SL_RESULT_SUCCESS) {
+        JAMI_ERR("Enqueue silentBuf_ failed, result = %d", result);
+        devShadowQueue_.pop();
+    }
 
     lk.unlock();
     result = (*playItf_)->SetPlayState(playItf_, SL_PLAYSTATE_PLAYING);
diff --git a/src/media/audio/opensl/opensllayer.cpp b/src/media/audio/opensl/opensllayer.cpp
index d433389447..7e682f4e0f 100644
--- a/src/media/audio/opensl/opensllayer.cpp
+++ b/src/media/audio/opensl/opensllayer.cpp
@@ -167,7 +167,8 @@ OpenSLLayer::initAudioEngine()
     SLASSERT((*engineObject_)->Realize(engineObject_, SL_BOOLEAN_FALSE));
     SLASSERT((*engineObject_)->GetInterface(engineObject_, SL_IID_ENGINE, &engineInterface_));
 
-    uint32_t bufSize = hardwareBuffSize_ * hardwareFormat_.getBytesPerFrame();
+    size_t bufSize = hardwareBuffSize_ * hardwareFormat_.getBytesPerFrame();
+    JAMI_DBG("OpenSL init: using buffer of %u bytes to support %s with %zu samples per channel", bufSize, hardwareFormat_.toString().c_str(), hardwareBuffSize_);
     bufs_ = allocateSampleBufs(BUF_COUNT * 3, bufSize);
     for (int i = 0; i < BUF_COUNT; i++)
         freePlayBufQueue_.push(&bufs_[i]);
-- 
GitLab