Revise log related code
[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 "mm_camcorder_rm.h"
31 #include "mm_camcorder_internal.h"
32
33 static rm_cb_result __mmcamcorder_rm_callback(int handle, rm_callback_type event_src,
34         rm_device_request_s *info, void* cb_data)
35 {
36         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(cb_data);
37         int current_state = MM_CAMCORDER_STATE_NONE;
38         rm_cb_result cb_res = RM_CB_RESULT_OK;
39
40         mmf_return_val_if_fail((MMHandleType)hcamcorder, RM_CB_RESULT_OK);
41
42         current_state = _mmcamcorder_get_state((MMHandleType)hcamcorder);
43
44         MMCAM_LOG_WARNING("current state %d (handle %p)", current_state, hcamcorder);
45
46         _MMCAMCORDER_LOCK_INTERRUPT(hcamcorder);
47
48         /* set RM event code for sending it to application */
49         hcamcorder->interrupt_code = event_src;
50
51         MMCAM_LOG_INFO("RM conflict callback : event code 0x%x", event_src);
52         switch (event_src) {
53         case RM_CALLBACK_TYPE_RESOURCE_CONFLICT:
54         case RM_CALLBACK_TYPE_RESOURCE_CONFLICT_UD:
55                 __mmcamcorder_force_stop(hcamcorder, _MMCAMCORDER_STATE_CHANGE_BY_RM);
56                 break;
57         default:
58                 break;
59         }
60
61         _MMCAMCORDER_UNLOCK_INTERRUPT(hcamcorder);
62
63         return cb_res;
64 }
65
66
67
68 int _mmcamcorder_rm_create(MMHandleType handle)
69 {
70         int ret = RM_OK;
71         rm_consumer_info rci;
72         int app_pid = 0;
73
74         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
75         if (!hcamcorder) {
76                 MMCAM_LOG_ERROR("Not initialized");
77                 return MM_ERROR_CAMCORDER_NOT_INITIALIZED;
78         }
79
80         memset(&rci, 0x00, sizeof(rm_consumer_info));
81
82         mm_camcorder_get_attributes(handle, NULL,
83                 MMCAM_CLIENT_PID, &app_pid,
84                 NULL);
85         rci.app_pid = app_pid;
86         aul_app_get_appid_bypid(rci.app_pid, rci.app_id, sizeof(rci.app_id));
87
88         /* RM register */
89         if (hcamcorder->rm_handle == 0) {
90                 ret = rm_register((rm_resource_cb)__mmcamcorder_rm_callback,
91                         (void*)hcamcorder,
92                         &(hcamcorder->rm_handle),
93                         (rci.app_id[0] != '\0') ? &rci : NULL);
94                 if (ret != RM_OK) {
95                         MMCAM_LOG_ERROR("rm_register fail ret = %d",ret);
96                         return MM_ERROR_RESOURCE_INTERNAL;
97                 }
98         }
99         return MM_ERROR_NONE;
100 }
101
102 int _mmcamcorder_rm_allocate(MMHandleType handle)
103 {
104         int iret = RM_OK;
105         int preview_format = MM_PIXEL_FORMAT_NV12;
106         int qret = RM_OK;
107         int qret_avail = 0; /* 0: not available, 1: available */
108         int resource_count = 0;
109         int display_surface_type = MM_DISPLAY_SURFACE_OVERLAY;
110         int camera_width = 0;
111
112         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
113         if (!hcamcorder) {
114                 MMCAM_LOG_ERROR("Not initialized");
115                 return MM_ERROR_CAMCORDER_NOT_INITIALIZED;
116         }
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
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
143                         hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
144                         hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_MJPEG_DECODER;
145                         hcamcorder->request_resources.category_option[resource_count] = ri_get_jpeg_category_id("MJPEG", camera_width);
146                         MMCAM_LOG_INFO("request dec rsc - category 0x%x, option width [%d]", RM_CATEGORY_MJPEG_DECODER, camera_width);
147                         resource_count++;
148                 }
149                 if (display_surface_type == MM_DISPLAY_SURFACE_OVERLAY) {
150                         hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
151                         hcamcorder->request_resources.category_id[resource_count] = 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
163                                 resource_count = 0;
164                                 memset(&hcamcorder->request_resources, 0x0, sizeof(rm_category_request_s));
165                                 memset(&hcamcorder->returned_devices, 0x0, sizeof(rm_device_return_s));
166
167                                 if (preview_format == MM_PIXEL_FORMAT_ENCODED_H264) {
168                                         hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
169                                         hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_VIDEO_DECODER_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] = 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                                         if (preview_format == MM_PIXEL_FORMAT_ENCODED_H264) {
181                                                 hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
182                                                 hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_SCALER_SUB;
183                                                 MMCAM_LOG_INFO("request scaler rsc - category 0x%x", RM_CATEGORY_SCALER_SUB);
184                                                 resource_count++;
185                                         } else if (preview_format == MM_PIXEL_FORMAT_ENCODED_MJPEG) {
186                                                 hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
187                                                 hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_SCALER;
188                                                 MMCAM_LOG_INFO("request scaler rsc - category 0x%x", RM_CATEGORY_SCALER);
189                                                 resource_count++;
190                                         }
191                                 }
192                         }
193                 }
194         }
195
196         hcamcorder->request_resources.state[resource_count] = RM_STATE_EXCLUSIVE;
197         hcamcorder->request_resources.category_id[resource_count] = RM_CATEGORY_CAMERA;
198
199         hcamcorder->request_resources.request_num = resource_count + 1;
200         MMCAM_LOG_INFO("request camera rsc - category 0x%x", RM_CATEGORY_CAMERA);
201
202         iret = rm_allocate_resources(hcamcorder->rm_handle, &(hcamcorder->request_resources), &hcamcorder->returned_devices);
203         if (iret != RM_OK) {
204                 MMCAM_LOG_ERROR("Resource allocation request failed ret = %d",iret);
205                 return MM_ERROR_RESOURCE_INTERNAL;
206         }
207
208         return MM_ERROR_NONE;
209 }
210
211
212 int _mmcamcorder_rm_deallocate(MMHandleType handle)
213 {
214         int rm_ret = RM_OK;
215         int idx = 0;
216         rm_device_request_s requested;
217
218         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
219         rm_device_return_s *r_devices;
220
221         if (!hcamcorder->rm_handle) {
222                 MMCAM_LOG_ERROR("Resource is not initialized ");
223                 return MM_ERROR_RESOURCE_NOT_INITIALIZED;
224         }
225
226         r_devices = &hcamcorder->returned_devices;
227
228         if (r_devices->allocated_num > 0) {
229                 memset(&requested, 0x0, sizeof(rm_device_request_s));
230                 requested.request_num = r_devices->allocated_num;
231                 for (idx = 0; idx < requested.request_num; idx++)
232                         requested.device_id[idx] = r_devices->device_id[idx];
233
234                 rm_ret = rm_deallocate_resources(hcamcorder->rm_handle, &requested);
235                 if (rm_ret != RM_OK)
236                         MMCAM_LOG_ERROR("Resource deallocation request failed ");
237         }
238
239         for (idx = 0; idx < r_devices->allocated_num; idx++) {
240                 if (r_devices->device_node[idx]) {
241                         free(r_devices->device_node[idx]);
242                         r_devices->device_node[idx] = NULL;
243                 }
244                 if (r_devices->omx_comp_name[idx]) {
245                         free(r_devices->omx_comp_name[idx]);
246                         r_devices->omx_comp_name[idx] = NULL;
247                 }
248         }
249
250         return MM_ERROR_NONE;
251 }
252
253 int _mmcamcorder_rm_release(MMHandleType handle)
254 {
255         int rm_ret = RM_OK;
256
257         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
258
259         if (!hcamcorder->rm_handle) {
260                 MMCAM_LOG_ERROR("Resource is not initialized ");
261                 return MM_ERROR_RESOURCE_NOT_INITIALIZED;
262         }
263
264         /* unregister RM */
265         rm_ret = rm_unregister(hcamcorder->rm_handle);
266         if (rm_ret != RM_OK)
267                 MMCAM_LOG_ERROR("rm_unregister() failed");
268         hcamcorder->rm_handle = 0;
269
270         return MM_ERROR_NONE;
271 }
272
273 #endif /* _MMCAMCORDER_RM_SUPPORT*/
274