Add version of so files
[platform/core/multimedia/libmm-camcorder.git] / src / mm_camcorder_rm.c
1 /*
2  * libmm-camcorder
3  *
4  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyuntae Kim <ht1211.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22  /*=======================================================================================
23 |  INCLUDE FILES                                                                        |
24 ========================================================================================*/
25 #ifdef _MMCAMCORDER_RM_SUPPORT
26 #include <aul.h>
27 #include <rm_api.h>
28 #include <ri-api.h>
29 #include <ri-module-api.h>
30 #include <resource_center.h>
31 #include "mm_camcorder_rm.h"
32 #include "mm_camcorder_internal.h"
33
34 static rm_cb_result __mmcamcorder_rm_callback(int handle, rm_callback_type event_src,
35         rm_device_request_s *info, void* cb_data)
36 {
37         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(cb_data);
38         int current_state = MM_CAMCORDER_STATE_NONE;
39         rm_cb_result cb_res = RM_CB_RESULT_OK;
40
41         mmf_return_val_if_fail((MMHandleType)hcamcorder, RM_CB_RESULT_OK);
42
43         current_state = _mmcamcorder_get_state((MMHandleType)hcamcorder);
44
45         MMCAM_LOG_WARNING("current state %d (handle %p)", current_state, hcamcorder);
46
47         _MMCAMCORDER_LOCK_INTERRUPT(hcamcorder);
48
49         /* set RM event code for sending it to application */
50         hcamcorder->interrupt_code = event_src;
51
52         MMCAM_LOG_INFO("RM conflict callback : event code 0x%x", event_src);
53         switch (event_src) {
54         case RM_CALLBACK_TYPE_RESOURCE_CONFLICT:
55         case RM_CALLBACK_TYPE_RESOURCE_CONFLICT_UD:
56                 __mmcamcorder_force_stop(hcamcorder, _MMCAMCORDER_STATE_CHANGE_BY_RM);
57                 break;
58         default:
59                 break;
60         }
61
62         _MMCAMCORDER_UNLOCK_INTERRUPT(hcamcorder);
63
64         return cb_res;
65 }
66
67 int _mmcamcorder_rm_create(MMHandleType handle)
68 {
69         int ret = RM_OK;
70
71         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
72         if (!hcamcorder) {
73                 MMCAM_LOG_ERROR("Not initialized");
74                 return MM_ERROR_CAMCORDER_NOT_INITIALIZED;
75         }
76         memset(&hcamcorder->rci, 0x00, sizeof(rm_consumer_info));
77         mm_camcorder_get_attributes(handle, NULL,
78                 MMCAM_CLIENT_PID, &hcamcorder->rci.app_pid,
79                 NULL);
80         aul_app_get_appid_bypid(hcamcorder->rci.app_pid, hcamcorder->rci.app_id, sizeof(hcamcorder->rci.app_id));
81
82         /* RM register */
83         /* NULL should be passed for the rci parameter if the application does not have "visibility" */
84         if (hcamcorder->rm_handle == 0) {
85                 ret = rm_register((rm_resource_cb)__mmcamcorder_rm_callback,
86                         (void*)hcamcorder,
87                         &(hcamcorder->rm_handle),
88                         (hcamcorder->rci.app_id[0] != '\0') ? &hcamcorder->rci : NULL);
89                 if (ret != RM_OK) {
90                         MMCAM_LOG_ERROR("rm_register fail ret = %d",ret);
91                         return MM_ERROR_RESOURCE_INTERNAL;
92                 }
93         }
94         return MM_ERROR_NONE;
95 }
96
97 int _mmcamcorder_rm_allocate(MMHandleType handle)
98 {
99         int iret = RM_OK;
100         int preview_format = MM_PIXEL_FORMAT_NV12;
101         int qret = RM_OK;
102         int qret_avail = 0; /* 0: not available, 1: available */
103         int resource_count = 0;
104         int display_surface_type = MM_DISPLAY_SURFACE_OVERLAY;
105         int camera_width = 0;
106
107         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
108         if (!hcamcorder) {
109                 MMCAM_LOG_ERROR("Not initialized");
110                 return MM_ERROR_CAMCORDER_NOT_INITIALIZED;
111         }
112
113         MMCAM_LOG_INFO("app id : %s", hcamcorder->rci.app_id);
114         /* if the app_id is null,
115            In the multiview mode, the rc_get_capable_category_id returns the error(RI_CATEGORY_NOT_PERMITTED).
116            In the normal mode, it returns the category itself which is passed in the parameter. */
117
118         mm_camcorder_get_attributes(handle, NULL,
119                 MMCAM_DISPLAY_SURFACE, &display_surface_type,
120                 NULL);
121
122         if (display_surface_type != MM_DISPLAY_SURFACE_NULL) {
123                 mm_camcorder_get_attributes(handle, NULL,
124                         MMCAM_CAMERA_FORMAT, &preview_format,
125                         NULL);
126
127                 resource_count = 0;
128                 memset(&hcamcorder->request_resources, 0x0, sizeof(rm_category_request_s));
129                 memset(&hcamcorder->returned_devices, 0x0, sizeof(rm_device_return_s));
130
131                 if (preview_format == MM_PIXEL_FORMAT_ENCODED_H264) {
132                         hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
133                         hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_VIDEO_DECODER;
134                         hcamcorder->request_resources.category_option[resource_count] = rc_get_capable_category_id(hcamcorder->rm_handle, hcamcorder->rci.app_id, RM_CATEGORY_VIDEO_DECODER);
135                         MMCAM_LOG_INFO("request dec rsc - category 0x%x", RM_CATEGORY_VIDEO_DECODER);
136
137                         resource_count++;
138                 } else if (preview_format == MM_PIXEL_FORMAT_ENCODED_MJPEG) {
139                         mm_camcorder_get_attributes(handle, NULL,
140                                 MMCAM_CAMERA_WIDTH, &camera_width,
141                                 NULL);
142                         hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
143                         hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_MJPEG_DECODER;
144                         hcamcorder->request_resources.category_option[resource_count] = rc_get_capable_category_id(hcamcorder->rm_handle, hcamcorder->rci.app_id, ri_get_jpeg_category_id("MJPEG", camera_width));
145                         MMCAM_LOG_INFO("request dec rsc - category 0x%x, option width [%d]", RM_CATEGORY_MJPEG_DECODER, camera_width);
146                         resource_count++;
147                 }
148                 if (display_surface_type == MM_DISPLAY_SURFACE_OVERLAY) {
149                         hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
150                         hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_SCALER;
151                         hcamcorder->request_resources.category_option[resource_count] = rc_get_capable_category_id(hcamcorder->rm_handle, hcamcorder->rci.app_id, RM_CATEGORY_SCALER);
152                         MMCAM_LOG_INFO("request scaler rsc - category 0x%x", RM_CATEGORY_SCALER);
153                         resource_count++;
154                 }
155
156                 hcamcorder->request_resources.request_num = resource_count;
157
158                 if (resource_count > 0) {
159                         qret = rm_query(hcamcorder->rm_handle, RM_QUERY_ALLOCATION, &(hcamcorder->request_resources), &qret_avail);
160                         if (qret != RM_OK || qret_avail != 1) {
161                                 MMCAM_LOG_INFO("rm query failed. retry with sub devices");
162                                 resource_count = 0;
163                                 memset(&hcamcorder->request_resources, 0x0, sizeof(rm_category_request_s));
164                                 memset(&hcamcorder->returned_devices, 0x0, sizeof(rm_device_return_s));
165
166                                 if (preview_format == MM_PIXEL_FORMAT_ENCODED_H264) {
167                                         hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
168                                         hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_VIDEO_DECODER_SUB;
169                                         hcamcorder->request_resources.category_option[resource_count] = rc_get_capable_category_id(hcamcorder->rm_handle, hcamcorder->rci.app_id, RM_DEVICE_OPT_SUB);
170                                         MMCAM_LOG_INFO("request dec rsc - category 0x%x", RM_CATEGORY_VIDEO_DECODER_SUB);
171                                         resource_count++;
172                                 } else if (preview_format == MM_PIXEL_FORMAT_ENCODED_MJPEG) {
173                                         hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
174                                         hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_MJPEG_DECODER;
175                                         hcamcorder->request_resources.category_option[resource_count] = rc_get_capable_category_id(hcamcorder->rm_handle, hcamcorder->rci.app_id, ri_get_jpeg_category_id("MJPEG", camera_width));
176                                         MMCAM_LOG_INFO("request dec rsc - category 0x%x", RM_CATEGORY_MJPEG_DECODER);
177                                         resource_count++;
178                                 }
179                                 if ((display_surface_type == MM_DISPLAY_SURFACE_OVERLAY) &&
180                                         (preview_format == MM_PIXEL_FORMAT_ENCODED_H264 || preview_format == MM_PIXEL_FORMAT_ENCODED_MJPEG)) {
181                                         hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
182                                         hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_SCALER_SUB;
183                                         hcamcorder->request_resources.category_option[resource_count] = rc_get_capable_category_id(hcamcorder->rm_handle, hcamcorder->rci.app_id, RM_DEVICE_OPT_SUB);
184                                         MMCAM_LOG_INFO("request scaler rsc - category 0x%x", RM_CATEGORY_SCALER_SUB);
185                                         resource_count++;
186                                 }
187                         }
188                 }
189         }
190
191         hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
192         hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_CAMERA;
193         hcamcorder->request_resources.category_option[resource_count] = rc_get_capable_category_id(hcamcorder->rm_handle, hcamcorder->rci.app_id, RM_CATEGORY_CAMERA);
194
195         hcamcorder->request_resources.request_num = resource_count + 1;
196         MMCAM_LOG_INFO("request camera rsc - category 0x%x", RM_CATEGORY_CAMERA);
197
198         iret = rm_allocate_resources(hcamcorder->rm_handle, &(hcamcorder->request_resources), &hcamcorder->returned_devices);
199         if (iret != RM_OK) {
200                 MMCAM_LOG_ERROR("Resource allocation request failed ret = %d",iret);
201                 return MM_ERROR_RESOURCE_INTERNAL;
202         }
203
204         return MM_ERROR_NONE;
205 }
206
207
208 int _mmcamcorder_rm_deallocate(MMHandleType handle)
209 {
210         int rm_ret = RM_OK;
211         int idx = 0;
212         rm_device_request_s requested;
213
214         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
215         rm_device_return_s *r_devices;
216
217         if (!hcamcorder->rm_handle) {
218                 MMCAM_LOG_ERROR("Resource is not initialized ");
219                 return MM_ERROR_RESOURCE_NOT_INITIALIZED;
220         }
221
222         r_devices = &hcamcorder->returned_devices;
223
224         if (r_devices->allocated_num > 0) {
225                 memset(&requested, 0x0, sizeof(rm_device_request_s));
226                 requested.request_num = r_devices->allocated_num;
227                 for (idx = 0; idx < requested.request_num; idx++)
228                         requested.device_id[idx] = r_devices->device_id[idx];
229
230                 rm_ret = rm_deallocate_resources(hcamcorder->rm_handle, &requested);
231                 if (rm_ret != RM_OK)
232                         MMCAM_LOG_ERROR("Resource deallocation request failed ");
233         }
234
235         for (idx = 0; idx < r_devices->allocated_num; idx++) {
236                 if (r_devices->device_node[idx]) {
237                         free(r_devices->device_node[idx]);
238                         r_devices->device_node[idx] = NULL;
239                 }
240                 if (r_devices->omx_comp_name[idx]) {
241                         free(r_devices->omx_comp_name[idx]);
242                         r_devices->omx_comp_name[idx] = NULL;
243                 }
244         }
245
246         return MM_ERROR_NONE;
247 }
248
249 int _mmcamcorder_rm_release(MMHandleType handle)
250 {
251         int rm_ret = RM_OK;
252
253         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
254
255         if (!hcamcorder->rm_handle) {
256                 MMCAM_LOG_ERROR("Resource is not initialized ");
257                 return MM_ERROR_RESOURCE_NOT_INITIALIZED;
258         }
259
260         /* unregister RM */
261         rm_ret = rm_unregister(hcamcorder->rm_handle);
262         if (rm_ret != RM_OK)
263                 MMCAM_LOG_ERROR("rm_unregister() failed");
264         hcamcorder->rm_handle = 0;
265
266         return MM_ERROR_NONE;
267 }
268
269 #endif /* _MMCAMCORDER_RM_SUPPORT*/
270