#include "media_editor.h"
#include "media_editor_private.h"
+#include <rm_type.h>
+#include <resource_center.h>
-//LCOV_EXCL_START
#ifndef TIZEN_TV
-static int __resource_release_cb(mm_resource_manager_h mgr,
- mm_resource_manager_res_h res, void *user_data)
+//LCOV_EXCL_START
+static int __get_appid_by_pid(int pid, char *name, size_t size)
{
- int ret = true;
- mediaeditor_s *editor = (mediaeditor_s *)user_data;
+ g_autofree gchar *cmdline = NULL;
+ g_autofree gchar *contents = NULL;
+ g_autofree gchar *base = NULL;
+ g_autoptr(GError) error = NULL;
- RET_VAL_IF(editor == NULL, false, "editor is NULL");
+ RET_VAL_IF(name == NULL, MEDIAEDITOR_ERROR_INVALID_PARAMETER, "name is NULL");
+ RET_VAL_IF(size == 0, MEDIAEDITOR_ERROR_INVALID_PARAMETER, "size is 0");
- editor->resource.release_cb_is_calling = true;
+ cmdline = g_strdup_printf("/proc/%d/cmdline", pid);
- for (int i = 0; i < RESOURCE_TYPE_MAX; i++) {
- if (editor->resource.res[i] == res) {
- LOG_INFO("type[%d] resource was released by resource manager", i);
- editor->resource.res[i] = NULL;
- }
+ if (!g_file_get_contents(cmdline, &contents, NULL, &error)) {
+ LOG_ERROR("error : %s", error->message);
+ return MEDIAEDITOR_ERROR_INVALID_OPERATION;
}
- if (_mediaeditor_stop(editor) != MEDIAEDITOR_ERROR_NONE)
- ret = false;
+ base = g_path_get_basename(contents);
- _post_error_cb_in_idle(editor, MEDIAEDITOR_ERROR_RESOURCE_CONFLICT);
+ if (g_strlcpy(name, base, size) >= size) {
+ LOG_ERROR("string truncated");
+ return MEDIAEDITOR_ERROR_INVALID_OPERATION;
+ }
- editor->resource.release_cb_is_calling = false;
+ return MEDIAEDITOR_ERROR_NONE;
+}
- return ret;
+static void __resource_release(mediaeditor_s *editor)
+{
+ RET_IF(editor == NULL, "editor is NULL");
+
+ if (_mediaeditor_stop(editor) != MEDIAEDITOR_ERROR_NONE) {
+ LOG_ERROR("_mediaeditor_stop is failed");
+ return;
+ }
+
+ _post_error_cb_in_idle(editor, MEDIAEDITOR_ERROR_RESOURCE_CONFLICT);
}
-//LCOV_EXCL_STOP
-static bool __is_valid_resource_type(mm_resource_manager_res_type_e type)
+static rm_cb_result __rm_callback(int handle, rm_callback_type event_src,
+ rm_device_request_s *info, void *cb_data)
{
- if (type < MM_RESOURCE_MANAGER_RES_TYPE_VIDEO_DECODER || type >= RESOURCE_TYPE_MAX) {
- LOG_ERROR("Type[%d] is a invalid resource type", type);
- return false;
+ mediaeditor_s *editor = (mediaeditor_s *)cb_data;
+ g_autoptr(GMutexLocker) locker = NULL;
+
+ RET_VAL_IF(editor == NULL, RM_CB_RESULT_ERROR, "editor is NULL");
+
+ locker = g_mutex_locker_new(&editor->resource.callback_lock);
+
+ LOG_INFO("editor[%p] rm handle[%d] event_src[%d]", editor, handle, event_src);
+
+ switch (event_src) {
+ case RM_CALLBACK_TYPE_RESOURCE_CONFLICT:
+ case RM_CALLBACK_TYPE_RESOURCE_CONFLICT_UD:
+ __resource_release(editor);
+ break;
+ default:
+ break;
}
- return true;
+
+ LOG_DEBUG_LEAVE();
+
+ return RM_CB_RESULT_OK;
}
+//LCOV_EXCL_STOP
int _create_resource_manager(mediaeditor_s *editor)
{
+ int ret = 0;
NULL_PARAM_CHECK(editor);
- if (mm_resource_manager_create(MM_RESOURCE_MANAGER_APP_CLASS_MEDIA,
- __resource_release_cb, editor,
- &editor->resource.mgr) != MM_RESOURCE_MANAGER_ERROR_NONE) {
- LOG_ERROR("Failed to init resource manager for media");
+ g_mutex_init(&editor->resource.control_lock);
+ g_mutex_init(&editor->resource.callback_lock);
+
+ memset(&editor->resource.rci, 0x00, sizeof(rm_consumer_info));
+
+ editor->resource.rci.app_pid = (int)getpid();
+
+ if (__get_appid_by_pid(editor->resource.rci.app_pid,
+ editor->resource.rci.app_id,
+ sizeof(editor->resource.rci.app_id)) != MEDIAEDITOR_ERROR_NONE) {
+ LOGE("__get_appid_by_pid is failed");
+ return MEDIAEDITOR_ERROR_RESOURCE_FAILED;
+ }
+
+ ret = rm_register((rm_resource_cb)__rm_callback,
+ (void *)editor,
+ &(editor->resource.rm_h),
+ (editor->resource.rci.app_id[0] != '\0') ? &editor->resource.rci : NULL);
+
+ if (ret != RM_OK) {
+ LOG_ERROR("rm_register fail %d", ret);
return MEDIAEDITOR_ERROR_RESOURCE_FAILED;
}
+ LOG_INFO("app pid %d app id %s resource h %d", editor->resource.rci.app_pid, editor->resource.rci.app_id, editor->resource.rm_h);
+
return MEDIAEDITOR_ERROR_NONE;
}
-int _acquire_resource_for_type(mediaeditor_s *editor, mm_resource_manager_res_type_e type)
+int _acquire_resource_for_type(mediaeditor_s *editor, res_type_e type)
{
- int ret = MM_RESOURCE_MANAGER_ERROR_NONE;
-
NULL_PARAM_CHECK(editor);
- if (!editor->resource.mgr) {
- LOG_DEBUG("There's no acquired hw encoder, decoder resources");
- return MEDIAEDITOR_ERROR_NONE;
- }
-
- RET_VAL_IF(!__is_valid_resource_type(type), MEDIAEDITOR_ERROR_INVALID_PARAMETER,
- "type is wrong");
+ int ret = RM_OK;
+ int idx = 0;
+ int category_option = 0;
+ rm_rsc_category_e category_id = RM_CATEGORY_NONE;
+ rm_requests_resource_state_e state;
+ rm_category_request_s request_resources;
+ rm_device_return_s *device;
+ g_autoptr(GMutexLocker) locker = NULL;
+
+ RET_VAL_IF(editor == NULL, MEDIAEDITOR_ERROR_INVALID_PARAMETER, "editor is NULL");
+ RET_VAL_IF(type >= RES_TYPE_MAX, MEDIAEDITOR_ERROR_INVALID_PARAMETER, "invalid type(%d)", type);
+
+ locker = g_mutex_locker_new(&editor->resource.control_lock);
+
+ LOG_INFO("app id : %s type %d", editor->resource.rci.app_id, type);
+ memset(&request_resources, 0x0, sizeof(rm_category_request_s));
+
+ device = &editor->resource.devices[type];
+ memset(device, 0x0, sizeof(rm_device_return_s));
+
+ switch (type) {
+ case RES_TYPE_VIDEO_DECODER:
+ state = RM_STATE_EXCLUSIVE;
+ category_id = RM_CATEGORY_VIDEO_DECODER;
+ break;
+ case RES_TYPE_VIDEO_ENCODER:
+ state = RM_STATE_EXCLUSIVE;
+ category_id = RM_CATEGORY_VIDEO_ENCODER;
+ break;
+ default:
+ LOG_ERROR("category id can't set");
- if (editor->resource.res[type] != NULL) {
- LOG_ERROR("type[%d] resource was already acquired", type);
return MEDIAEDITOR_ERROR_RESOURCE_FAILED;
}
- LOG_DEBUG("mark for acquire type[%d] resource", type);
- ret = mm_resource_manager_mark_for_acquire(editor->resource.mgr, type,
- MM_RESOURCE_MANAGER_RES_VOLUME_FULL, &editor->resource.res[type]);
- if (ret != MM_RESOURCE_MANAGER_ERROR_NONE) {
- LOG_ERROR("failed to mark resource for acquire, ret[0x%x]", ret);
- return MEDIAEDITOR_ERROR_RESOURCE_FAILED;
- }
+ category_option = rc_get_capable_category_id(editor->resource.rm_h, editor->resource.rci.app_id, category_id);
+
+ request_resources.request_num = 1;
+ request_resources.state[0] = state;
+ request_resources.category_id[0] = category_id;
+ request_resources.category_option[0] = category_option;
+ LOG_INFO("state %d category id 0x%x category option %d", state, category_id, category_option);
- LOG_DEBUG("commit type[%d] resource", type);
- ret = mm_resource_manager_commit(editor->resource.mgr);
- if (ret != MM_RESOURCE_MANAGER_ERROR_NONE) {
- LOG_ERROR("failed to commit of resource, ret([0x%x]", ret);
+ ret = rm_allocate_resources(editor->resource.rm_h, &request_resources, device);
+ if (ret != RM_OK) {
+ LOG_ERROR("Resource allocation request failed ret %d [error type %d]", ret, device->error_type);
return MEDIAEDITOR_ERROR_RESOURCE_FAILED;
}
+ for (idx = 0; idx < device->allocated_num; idx++)
+ LOG_INFO("#%d / %d [%p] device %d %s %s", idx, device->allocated_num, device,
+ device->device_id[idx], device->device_node[idx],
+ device->device_name[idx]);
+
return MEDIAEDITOR_ERROR_NONE;
}
int _release_all_resources(mediaeditor_s *editor)
{
- int ret = MM_RESOURCE_MANAGER_ERROR_NONE;
-
NULL_PARAM_CHECK(editor);
- if (!editor->resource.mgr) {
- LOG_DEBUG("There's no acquired hw encoder, decoder resources");
- return MEDIAEDITOR_ERROR_NONE;
- }
+ int rm_ret = RM_OK;
+ int idx = 0;
+ rm_device_request_s requested;
+ rm_device_return_s *devices;
+ res_type_e type;
+ g_autoptr(GMutexLocker) locker = NULL;
- if (editor->resource.release_cb_is_calling) {
- LOG_INFO("__resource_release_cb is calling, so skip");
- return MEDIAEDITOR_ERROR_NONE;
- }
+ RET_VAL_IF(editor == NULL, MEDIAEDITOR_ERROR_INVALID_PARAMETER, "editor is NULL");
+ RET_VAL_IF(editor->resource.rm_h == 0, MEDIAEDITOR_ERROR_RESOURCE_FAILED, "editor resource handle is NULL");
- ret = mm_resource_manager_mark_all_for_release(editor->resource.mgr);
- if (ret != MM_RESOURCE_MANAGER_ERROR_NONE) {
- LOG_ERROR("failed to mark all for release, ret[0x%x]", ret);
- return MEDIAEDITOR_ERROR_RESOURCE_FAILED;
- }
+ locker = g_mutex_locker_new(&editor->resource.control_lock);
- ret = mm_resource_manager_commit(editor->resource.mgr);
- if (ret != MM_RESOURCE_MANAGER_ERROR_NONE) {
- LOG_ERROR("failed to commit resource, ret[0x%x]", ret);
- return MEDIAEDITOR_ERROR_RESOURCE_FAILED;
- }
- LOG_DEBUG("all resources were released by resource manager");
+ for (type = RES_TYPE_VIDEO_DECODER; type < RES_TYPE_MAX; type++) {
+ devices = &editor->resource.devices[type];
+
+ LOG_INFO("[%p] #%d (type %d) alloc num %d", devices, idx, type, devices->allocated_num);
+
+ if (devices->allocated_num > 0) {
+ memset(&requested, 0x0, sizeof(rm_device_request_s));
+ requested.request_num = devices->allocated_num;
+ for (idx = 0; idx < requested.request_num; idx++) {
+ requested.device_id[idx] = devices->device_id[idx];
+ LOG_INFO("[device id %d] [device name %s]", devices->device_id[idx], devices->device_name[idx]);
+ }
- for (int i = 0; i < RESOURCE_TYPE_MAX; i++) {
- editor->resource.need_to_acquire[i] = false;
- editor->resource.res[i] = NULL;
+ rm_ret = rm_deallocate_resources(editor->resource.rm_h, &requested);
+ if (rm_ret != RM_OK) {
+ LOG_ERROR("Resource deallocation request failed [%d] [request num %d]", rm_ret, requested.request_num);
+ return MEDIAEDITOR_ERROR_RESOURCE_FAILED;
+ }
+ }
+
+ for (idx = 0; idx < devices->allocated_num; idx++) {
+ if (devices->device_node[idx]) {
+ free(devices->device_node[idx]);
+ devices->device_node[idx] = NULL;
+ }
+
+ if (devices->omx_comp_name[idx]) {
+ free(devices->omx_comp_name[idx]);
+ devices->omx_comp_name[idx] = NULL;
+ }
+ }
}
return MEDIAEDITOR_ERROR_NONE;
int _destroy_resource_manager(mediaeditor_s *editor)
{
- int ret = MM_RESOURCE_MANAGER_ERROR_NONE;
+ int ret = MEDIAEDITOR_ERROR_NONE;
NULL_PARAM_CHECK(editor);
- if (!editor->resource.mgr) {
- LOG_DEBUG("There's no acquired hw encoder, decoder resources");
- return MEDIAEDITOR_ERROR_NONE;
- }
+ RET_VAL_IF(editor == NULL, MEDIAEDITOR_ERROR_INVALID_PARAMETER, "editor is NULL");
+ RET_VAL_IF(editor->resource.rm_h == 0, MEDIAEDITOR_ERROR_RESOURCE_FAILED, "editor resource handle is NULL");
- ret = mm_resource_manager_destroy(editor->resource.mgr);
- if (ret != MM_RESOURCE_MANAGER_ERROR_NONE) {
- LOG_ERROR("failed to destroy resource manager, ret[0x%x]", ret);
+ ret = rm_unregister(editor->resource.rm_h);
+ if (ret != RM_OK) {
+ LOGE("rm_unregister fail %d", ret);
return MEDIAEDITOR_ERROR_RESOURCE_FAILED;
}
- editor->resource.mgr = NULL;
+ g_mutex_clear(&editor->resource.control_lock);
+
LOG_DEBUG("destroyed resource manager");
return MEDIAEDITOR_ERROR_NONE;