From b3506346f721e38695839ccafc65be1e6435f6f7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Fri, 24 Sep 2021 23:02:00 -0400
Subject: [PATCH] video: synchronize surface access

Change-Id: Idb2520831ccbb40cdf1dfeb71b8f6318026a1206
---
 .../cx/ring/services/HardwareServiceImpl.kt   | 20 +++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/ring-android/app/src/main/java/cx/ring/services/HardwareServiceImpl.kt b/ring-android/app/src/main/java/cx/ring/services/HardwareServiceImpl.kt
index 0b52f3e6b..e72884415 100644
--- a/ring-android/app/src/main/java/cx/ring/services/HardwareServiceImpl.kt
+++ b/ring-android/app/src/main/java/cx/ring/services/HardwareServiceImpl.kt
@@ -319,8 +319,8 @@ class HardwareServiceImpl(
         bluetoothEvents.onNext(event)
     }
 
-    override fun decodingStarted(id: String, shmPath: String, width: Int, height: Int, isMixer: Boolean) {
-        Log.i(TAG, "DEBUG decodingStarted() " + id + " " + width + "x" + height)
+    @Synchronized override fun decodingStarted(id: String, shmPath: String, width: Int, height: Int, isMixer: Boolean) {
+        Log.i(TAG, "decodingStarted() " + id + " " + width + "x" + height)
         val shm = Shm(id, width, height)
         videoInputs[id] = shm
         videoEvents.onNext(VideoEvent(id, start = true))
@@ -334,8 +334,8 @@ class HardwareServiceImpl(
         }
     }
 
-    override fun decodingStopped(id: String, shmPath: String, isMixer: Boolean) {
-        Log.i(TAG, "DEBUG decodingStopped() $id")
+    @Synchronized override fun decodingStopped(id: String, shmPath: String, isMixer: Boolean) {
+        Log.i(TAG, "decodingStopped() $id")
         val shm = videoInputs.remove(id) ?: return
         if (shm.window != 0L) {
             try {
@@ -521,7 +521,7 @@ class HardwareServiceImpl(
         mIsCapturing = false
     }
 
-    override fun addVideoSurface(id: String, holder: Any) {
+    @Synchronized override fun addVideoSurface(id: String, holder: Any) {
         if (holder !is SurfaceHolder) {
             return
         }
@@ -540,7 +540,7 @@ class HardwareServiceImpl(
         }
     }
 
-    override fun updateVideoSurfaceId(currentId: String, newId: String) {
+    @Synchronized override fun updateVideoSurfaceId(currentId: String, newId: String) {
         Log.w(TAG, "updateVideoSurfaceId $currentId $newId")
         val surfaceHolder = videoSurfaces[currentId] ?: return
         val surface = surfaceHolder.get()
@@ -557,7 +557,7 @@ class HardwareServiceImpl(
         surface?.let { addVideoSurface(newId, it) }
     }
 
-    override fun addPreviewVideoSurface(holder: Any, conference: Conference?) {
+    @Synchronized override fun addPreviewVideoSurface(holder: Any, conference: Conference?) {
         if (holder !is TextureView)
             return
         Log.w(TAG, "addPreviewVideoSurface " + holder.hashCode() + " mCapturingId " + mCapturingId)
@@ -569,7 +569,7 @@ class HardwareServiceImpl(
         }
     }
 
-    override fun updatePreviewVideoSurface(conference: Conference) {
+    @Synchronized override fun updatePreviewVideoSurface(conference: Conference) {
         val old = mCameraPreviewCall.get()
         mCameraPreviewCall = WeakReference(conference)
         if (old !== conference && mIsCapturing) {
@@ -579,7 +579,7 @@ class HardwareServiceImpl(
         }
     }
 
-    override fun removeVideoSurface(id: String) {
+    @Synchronized override fun removeVideoSurface(id: String) {
         Log.i(TAG, "removeVideoSurface $id")
         videoSurfaces.remove(id)
         val shm = videoInputs[id] ?: return
@@ -652,6 +652,6 @@ class HardwareServiceImpl(
         private val TAG = HardwareServiceImpl::class.simpleName!!
         private var mCameraPreviewSurface = WeakReference<TextureView>(null)
         private var mCameraPreviewCall = WeakReference<Conference>(null)
-        private val videoSurfaces = Collections.synchronizedMap(HashMap<String, WeakReference<SurfaceHolder>>())
+        private val videoSurfaces = HashMap<String, WeakReference<SurfaceHolder>>()
     }
 }
\ No newline at end of file
-- 
GitLab