secmem: get handle available [2/2]

PD#SWPL-38713

Problem:
Amazon alloc memory failed

Solution:
Get available count before alloc

Verify:
RDK + U212

Change-Id: Ida36b8539e656f58a7f77dd43cf1645493495ffd
Signed-off-by: Tao Guo <tao.guo@amlogic.com>
diff --git a/gst-aml-drm-plugins-1.0/src/secmem/gstsecmemallocator.c b/gst-aml-drm-plugins-1.0/src/secmem/gstsecmemallocator.c
index d00242f..8dc355a 100644
--- a/gst-aml-drm-plugins-1.0/src/secmem/gstsecmemallocator.c
+++ b/gst-aml-drm-plugins-1.0/src/secmem/gstsecmemallocator.c
@@ -12,8 +12,6 @@
 #include "gstsecmemallocator.h"
 #include "secmem_ca.h"
 
-#define MAX_BUFS_COUNT    (511)
-
 GST_DEBUG_CATEGORY_STATIC (gst_secmem_allocator_debug);
 #define GST_CAT_DEFAULT gst_secmem_allocator_debug
 
@@ -48,7 +46,6 @@
     GstAllocator *allocator = GST_ALLOCATOR_CAST(self);
     g_mutex_init (&self->mutex);
     g_cond_init (&self->cond);
-    self->counter = 0;
     allocator->mem_type = GST_ALLOCATOR_SECMEM;
     allocator->mem_map = gst_secmem_mem_map;
     allocator->mem_unmap = gst_secmem_mem_unmap;
@@ -79,7 +76,8 @@
     secmem_handle_t handle;
     unsigned int ret;
     uint32_t maxsize = 0;
-    uint32_t available = 0;
+    uint32_t mem_available = 0;
+    uint32_t handle_available = 0;
 
     g_return_val_if_fail (GST_IS_SECMEM_ALLOCATOR (allocator), NULL);
     g_return_val_if_fail(self->sess != NULL, NULL);
@@ -87,9 +85,9 @@
     g_mutex_lock (&self->mutex);
 
     do {
-        if (Secure_V2_GetSecmemSize(self->sess, NULL, &available))
+        if (Secure_V2_GetSecmemSize(self->sess, NULL, &mem_available, NULL, &handle_available))
             goto error_create;
-        if (self->counter < MAX_BUFS_COUNT && size < available)
+        if (handle_available > 0 && size < mem_available)
             break;
         g_cond_wait (&self->cond, &self->mutex);
     } while (1);
@@ -118,7 +116,6 @@
     }
     mem->size = size;
     GST_INFO("alloc dma %d maxsize %d", fd, maxsize);
-    self->counter++;
     g_mutex_unlock (&self->mutex);
     return mem;
 
@@ -138,7 +135,7 @@
     GstSecmemAllocator *self = GST_SECMEM_ALLOCATOR (allocator);
     g_return_val_if_fail(self->sess != NULL, NULL);
     g_mutex_lock (&self->mutex);
-    g_return_val_if_fail(Secure_V2_GetSecmemSize(self->sess, NULL, &available) == 0, -1);
+    g_return_val_if_fail(Secure_V2_GetSecmemSize(self->sess, NULL, &available, NULL, NULL) == 0, -1);
     g_mutex_unlock (&self->mutex);
     return available;
 }
@@ -159,7 +156,6 @@
     GST_ALLOCATOR_CLASS (parent_class)->free(allocator, memory);
     Secure_V2_MemFree(self->sess, handle);
     Secure_V2_MemRelease(self->sess, handle);
-    self->counter--;
     g_cond_broadcast (&self->cond);
     g_mutex_unlock (&self->mutex);
 }
@@ -365,15 +361,15 @@
 
 gint gst_secmem_get_free_buf_num(GstMemory *mem)
 {
-    gint cnt;
+    unsigned int handle_available = 0;
     g_return_val_if_fail(mem != NULL, -1);
     g_return_val_if_fail(GST_IS_SECMEM_ALLOCATOR (mem->allocator), -1);
 
     GstSecmemAllocator *self = GST_SECMEM_ALLOCATOR (mem->allocator);
     g_mutex_lock (&self->mutex);
-    cnt = MAX_BUFS_COUNT - self->counter;
+    Secure_V2_GetSecmemSize(self->sess, NULL, NULL, NULL, &handle_available);
     g_mutex_unlock (&self->mutex);
-    return cnt;
+    return (gint)handle_available;
 
 }
 
@@ -386,7 +382,7 @@
 
     GstSecmemAllocator *self = GST_SECMEM_ALLOCATOR (mem->allocator);
     g_mutex_lock (&self->mutex);
-    g_return_val_if_fail(Secure_V2_GetSecmemSize(self->sess, NULL, &available) == 0, -1);
+    g_return_val_if_fail(Secure_V2_GetSecmemSize(self->sess, NULL, &available, NULL, NULL) == 0, -1);
     g_mutex_unlock (&self->mutex);
     return available;
 }
diff --git a/gst-aml-drm-plugins-1.0/src/secmem/gstsecmemallocator.h b/gst-aml-drm-plugins-1.0/src/secmem/gstsecmemallocator.h
index 1a29749..3874728 100644
--- a/gst-aml-drm-plugins-1.0/src/secmem/gstsecmemallocator.h
+++ b/gst-aml-drm-plugins-1.0/src/secmem/gstsecmemallocator.h
@@ -43,7 +43,6 @@
     gboolean                is_4k;
     gboolean                is_vp9;
     gboolean                is_av1;
-    gsize                   counter;
     GCond                   cond;
     GMutex                  mutex;
 };