pool_handle->pool_size = 0;
pool_handle->pool_size_min = 0;
pool_handle->pool_size_max = 0;
- pool_handle->pool_allocated = 0;
+ pool_handle->pool_allocated = FALSE;
/* take memory packet pool handle */
*pool = (media_packet_pool_h) pool_handle;
int media_packet_pool_set_media_format(media_packet_pool_h pool, media_format_h fmt)
{
- int ret = MEDIA_PACKET_ERROR_NONE;
+ int ret = 0;
media_packet_pool_s *pool_handle = NULL;
media_format_type_e type;
+ g_autoptr(GMutexLocker) locker = NULL;
MEDIA_PACKET_POOL_INSTANCE_CHECK(pool);
MEDIA_PACKET_POOL_NULL_ARG_CHECK(fmt);
+ pool_handle = (media_packet_pool_s *)pool;
+
+ locker = g_mutex_locker_new(&pool_handle->mutex);
+
ret = media_format_get_type(fmt, &type);
if (ret != MEDIA_FORMAT_ERROR_NONE) {
- LOGE("failed to media_format_get_type()");
+ LOGE("media_format_get_type() failed[0x%x]", ret);
return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
}
+
if (type != MEDIA_FORMAT_AUDIO && type != MEDIA_FORMAT_VIDEO) {
LOGE("invalid format type, type[0x%x]", type);
return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
}
- pool_handle = (media_packet_pool_s *)pool;
+ ret = media_format_ref(fmt);
+ if (ret != MEDIA_FORMAT_ERROR_NONE) {
+ LOGE("media_format_ref() failed[0x%x]", ret); //LCOV_EXCL_LINE
+ return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
+ }
+
+ if (pool_handle->fmt_h) {
+ LOGI("unref media format[%p]", pool_handle->fmt_h);
+ media_format_unref(pool_handle->fmt_h);
+ }
+
pool_handle->fmt_h = fmt;
- return ret;
+ return MEDIA_PACKET_ERROR_NONE;
}
int media_packet_pool_set_size(media_packet_pool_h pool, int min_buffers, int max_buffers)
{
media_packet_pool_s *pool_handle = NULL;
+ g_autoptr(GMutexLocker) locker = NULL;
MEDIA_PACKET_POOL_INSTANCE_CHECK(pool);
pool_handle = (media_packet_pool_s *)pool;
+ locker = g_mutex_locker_new(&pool_handle->mutex);
+
if (max_buffers <= 0 || min_buffers <= 0 ||
min_buffers > max_buffers || max_buffers > MAX_PACKET) {
LOGE("invalid size - set[min:%d,max:%d], pool max[%d]",
int media_packet_pool_get_size(media_packet_pool_h pool, int *min_size, int *max_size, int *curr_size)
{
media_packet_pool_s *pool_handle = NULL;
+ g_autoptr(GMutexLocker) locker = NULL;
MEDIA_PACKET_POOL_INSTANCE_CHECK(pool);
pool_handle = (media_packet_pool_s *)pool;
+ locker = g_mutex_locker_new(&pool_handle->mutex);
+
if (curr_size)
*curr_size = pool_handle->pool_size;
if (min_size)
{
media_packet_pool_h pool = NULL;
media_packet_pool_s *pool_handle = NULL;
+ g_autoptr(GMutexLocker) locker = NULL;
pool = (media_packet_pool_h)user_data;
pool_handle = (media_packet_pool_s *)pool;
- if (g_atomic_int_get(&pool_handle->pool_allocated)) {
- if (media_packet_pool_release_packet(pool, packet) != MEDIA_PACKET_ERROR_NONE)
- LOGW("media packet couldn't be released. packet(%p) might be in queue", packet);
- return MEDIA_PACKET_REUSE;
+ locker = g_mutex_locker_new(&pool_handle->mutex);
+
+ if (!pool_handle->pool_allocated) {
+ LOGW("pool is not allocated");
+ return MEDIA_PACKET_FINALIZE;
}
- return MEDIA_PACKET_FINALIZE;
+ /* early unlock for media_packet_pool_release_packet() */
+ g_clear_pointer(&locker, g_mutex_locker_free);
+
+ if (media_packet_pool_release_packet(pool, packet) != MEDIA_PACKET_ERROR_NONE)
+ LOGW("media packet couldn't be released. packet(%p) might be in queue", packet);
+
+ return MEDIA_PACKET_REUSE;
}
int media_packet_pool_allocate(media_packet_pool_h pool)
{
- int i, num_pkts = 0;
-
+ int i = 0;
+ int num_pkts = 0;
+ int ret = 0;
media_packet_pool_s *pool_handle = NULL;
+ g_autoptr(GMutexLocker) locker = NULL;
MEDIA_PACKET_POOL_INSTANCE_CHECK(pool);
pool_handle = (media_packet_pool_s *)pool;
+ locker = g_mutex_locker_new(&pool_handle->mutex);
+
if (pool_handle->pool_size_min == 0) {
LOGE("The media packet pool size is not set. set pool size..."); //LCOV_EXCL_LINE
return MEDIA_PACKET_ERROR_INVALID_OPERATION;
}
- if (g_atomic_int_get(&pool_handle->pool_allocated)) {
- LOGE("The media packet is already allocated. set media format size..."); //LCOV_EXCL_LINE
+ if (pool_handle->pool_allocated) {
+ LOGE("The media packet is already allocated"); //LCOV_EXCL_LINE
return MEDIA_PACKET_ERROR_INVALID_OPERATION;
}
for (i = 0; i < pool_handle->pool_size_min; i++) {
- if (media_packet_create_alloc(pool_handle->fmt_h, __packet_finalize_cb, pool_handle, &pool_handle->packet[i]) != MEDIA_PACKET_ERROR_NONE) {
- LOGE("The media packet pool is full or out of memory..."); //LCOV_EXCL_LINE
+ ret = media_packet_create_alloc(pool_handle->fmt_h,
+ __packet_finalize_cb, pool_handle, &pool_handle->packet[i]);
+
+ if (ret != MEDIA_PACKET_ERROR_NONE) {
+ LOGE("media_packet_create_alloc() failed[0x%x]", ret); //LCOV_EXCL_LINE
return MEDIA_PACKET_ERROR_INVALID_OPERATION;
}
- g_queue_push_tail(pool_handle->queue, pool_handle->packet[i]);
+
LOGD("[%d]%p queued", i, pool_handle->packet[i]);
- pool_handle->pool_size++;
- }
- /* increase format reference count */
- if (media_format_ref(pool_handle->fmt_h) != MEDIA_FORMAT_ERROR_NONE) {
- LOGE("failed to increase ref count"); //LCOV_EXCL_LINE
- return MEDIA_PACKET_ERROR_INVALID_OPERATION;
+ g_queue_push_tail(pool_handle->queue, pool_handle->packet[i]);
+
+ pool_handle->pool_size++;
}
- g_atomic_int_set(&pool_handle->pool_allocated, 1);
+ pool_handle->pool_allocated = TRUE;
return MEDIA_PACKET_ERROR_NONE;
}
gint64 wait_until = -1;
MEDIA_PACKET_POOL_INSTANCE_CHECK(pool);
pool_handle = (media_packet_pool_s *)pool;
-
- if (!g_atomic_int_get(&pool_handle->pool_allocated)) {
- LOGE("The media packet pool is not allocated..."); //LCOV_EXCL_LINE
- return MEDIA_PACKET_ERROR_INVALID_OPERATION;
- }
+ g_autoptr(GMutexLocker) locker = NULL;
if (timeout != -1)
wait_until = g_get_monotonic_time() + (gint64)timeout;
- g_mutex_lock(&pool_handle->mutex);
+ locker = g_mutex_locker_new(&pool_handle->mutex);
+
+ if (!pool_handle->pool_allocated) {
+ LOGE("pool is not allocated..."); //LCOV_EXCL_LINE
+ return MEDIA_PACKET_ERROR_INVALID_OPERATION;
+ }
if (pool_handle->pool_size == 0) {
LOGE("The media packet pool size is not set. set pool size..."); //LCOV_EXCL_LINE
- g_mutex_unlock(&pool_handle->mutex);
return MEDIA_PACKET_ERROR_INVALID_OPERATION;
}
*pkt = (media_packet_h)packet;
}
- g_mutex_unlock(&pool_handle->mutex);
-
return ret;
}
int num_pkts = 0;
media_packet_pool_s *pool_handle = NULL;
GList *find;
+ g_autoptr(GMutexLocker) locker = NULL;
MEDIA_PACKET_POOL_INSTANCE_CHECK(pool);
pool_handle = (media_packet_pool_s *)pool;
- g_mutex_lock(&pool_handle->mutex);
+ locker = g_mutex_locker_new(&pool_handle->mutex);
if ((find = g_queue_find(pool_handle->queue, pkt))) {
LOGE("unable to release '%p' to the pool. Already released", pkt); //LCOV_EXCL_LINE
- g_mutex_unlock(&pool_handle->mutex);
return MEDIA_PACKET_ERROR_INVALID_OPERATION;
}
if (num_pkts == pool_handle->pool_size) {
LOGE("Queue is already full"); //LCOV_EXCL_LINE
- g_mutex_unlock(&pool_handle->mutex);
return MEDIA_PACKET_ERROR_INVALID_OPERATION;
}
LOGD("The packet released to pool is %p..\n", pkt);
} else {
LOGE("packet is not aquired from pool %p", pkt); //LCOV_EXCL_LINE
- g_mutex_unlock(&pool_handle->mutex);
return MEDIA_PACKET_ERROR_INVALID_OPERATION;
}
media_packet_unset_flags(pkt, MEDIA_PACKET_END_OF_STREAM);
media_packet_unset_flags(pkt, MEDIA_PACKET_SYNC_FRAME);
- g_mutex_unlock(&pool_handle->mutex);
-
return MEDIA_PACKET_ERROR_NONE;
}
pool_handle = (media_packet_pool_s *)pool;
g_mutex_lock(&pool_handle->mutex);
+
+ if (!pool_handle->pool_allocated) {
+ LOGE("pool is not allocated");
+ g_mutex_unlock(&pool_handle->mutex);
+ return MEDIA_PACKET_ERROR_INVALID_OPERATION;
+ }
+
num_pkts = g_queue_get_length(pool_handle->queue);
if (num_pkts < pool_handle->pool_size) {
return MEDIA_PACKET_ERROR_INVALID_OPERATION;
}
- g_atomic_int_set(&pool_handle->pool_allocated, 0);
+ pool_handle->pool_allocated = FALSE;
while (!g_queue_is_empty(pool_handle->queue)) {
-
packet = g_queue_pop_head(pool_handle->queue);
if (packet == NULL) {
- g_mutex_unlock(&pool_handle->mutex);
- LOGE("Failed to get packet handle from Queue "); //LCOV_EXCL_LINE
- return MEDIA_PACKET_ERROR_INVALID_OPERATION;
- }
- ret = media_packet_destroy(packet);
- LOGI("The packet handle(%p) will be deallocated..", packet);
- if (ret != MEDIA_PACKET_ERROR_NONE) {
- g_mutex_unlock(&pool_handle->mutex);
- return ret;
+ LOGW("NULL packet from queue"); //LCOV_EXCL_LINE
+ continue;
}
- }
- /* unreference media_format */
- if (media_format_unref(pool_handle->fmt_h) != MEDIA_FORMAT_ERROR_NONE) {
- LOGE("failed to decrease ref count"); //LCOV_EXCL_LINE
g_mutex_unlock(&pool_handle->mutex);
- return MEDIA_PACKET_ERROR_INVALID_OPERATION;
+
+ LOGI("destroy packet[%p]", packet);
+
+ ret = media_packet_destroy(packet);
+ if (ret != MEDIA_PACKET_ERROR_NONE)
+ LOGW("destroy packet[%p] failed[0x%x]", packet, ret);
+
+ g_mutex_lock(&pool_handle->mutex);
}
g_mutex_unlock(&pool_handle->mutex);
- return ret;
+ return MEDIA_PACKET_ERROR_NONE;
}
int media_packet_pool_destroy(media_packet_pool_h pool)
{
+ int ret = 0;
int num_pkts = 0;
media_packet_pool_s *pool_handle = NULL;
pool_handle = (media_packet_pool_s *)pool;
g_mutex_lock(&pool_handle->mutex);
- num_pkts = g_queue_get_length(pool_handle->queue);
- g_mutex_unlock(&pool_handle->mutex);
+ num_pkts = g_queue_get_length(pool_handle->queue);
if (num_pkts > 0) {
- LOGE("The packet pool needs to deallocate first "); //LCOV_EXCL_LINE
+ LOGE("The packet pool needs to deallocate first[num_pkts:%d]", num_pkts); //LCOV_EXCL_LINE
+ g_mutex_unlock(&pool_handle->mutex);
return MEDIA_PACKET_ERROR_INVALID_OPERATION;
}
+ if (pool_handle->fmt_h) {
+ ret = media_format_unref(pool_handle->fmt_h);
+ if (ret != MEDIA_FORMAT_ERROR_NONE)
+ LOGW("media_format_unref() failed[0x%x]", ret); //LCOV_EXCL_LINE
+
+ pool_handle->fmt_h = NULL;
+ }
+
+ g_mutex_unlock(&pool_handle->mutex);
+
g_queue_free(pool_handle->queue);
LOGI("The packet pool handle(%p) will be destroyed..", pool_handle);
free(pool_handle);